OPEN-SOURCE SCRIPT
Telah dikemas kini 通用經濟數據年增率 (YoY %)

主要觀看經濟數據使用
可將主數據統計週期切換月度年增率或季度年增率
希望能幫到有在關注經濟數據但沒有年增率的網友們
The primary purpose is to view economic data. We can switch the statistical period to monthly year-over-year (YoY) growth or quarterly year-over-year (YoY) growth.
This is intended to assist users who follow economic data but lack access to year-over-year growth rates.
可將主數據統計週期切換月度年增率或季度年增率
希望能幫到有在關注經濟數據但沒有年增率的網友們
The primary purpose is to view economic data. We can switch the statistical period to monthly year-over-year (YoY) growth or quarterly year-over-year (YoY) growth.
This is intended to assist users who follow economic data but lack access to year-over-year growth rates.
Nota Keluaran
Add a customizable length Simple Moving Average (SMA) for Year-over-Year (YoY) growth rate.新增可自行設定長度的年增率簡單移動均線
Nota Keluaran
//version=6indicator("通用經濟數據年增率 (YoY %)", shorttitle="通用 YoY %", format=format.price, precision=2)
// —————— 1. 輸入設定 (Inputs) ——————
group_main = "核心設定"
source_input = input.source(close, "資料來源 (Source)", group=group_main)
// 新增:讓使用者選擇數據的原始頻率是「月」還是「季」
string freq_option = input.string("月度 (Monthly)", "數據頻率 (Data Frequency)",
options=["月度 (Monthly)", "季度 (Quarterly)"],
tooltip="請根據您分析的數據類型選擇。\n月度數據 (如 CPI, PMI): 選擇此項並使用月線圖。\n季度數據 (如 GDP): 選擇此項並使用季線或月線圖。",
group=group_main)
// —————— 2. 核心計算 (Calculation) ——————
// 根據使用者的選擇,決定正確的回溯期
// 月度數據年增率 -> 回溯 12 個月
// 季度數據年增率 -> 回溯 4 個季度
int lookback_period = switch freq_option
"月度 (Monthly)" => 12
"季度 (Quarterly)" => 4
// 取得對應週期前的數據值
float prev_year_source = source_input[lookback_period]
// 計算年增率 (YoY)
float yoy_rate = prev_year_source != 0 ? ((source_input - prev_year_source) / math.abs(prev_year_source)) * 100 : na
// 計算上一個週期的年增率
float yoy_rate_prev = yoy_rate[1]
// 計算年增率的百分點變化 (Current YoY - Previous YoY)
// 如果當前或前一個年增率為 NA,則結果也為 NA
float yoy_rate_diff_points = na(yoy_rate) or na(yoy_rate_prev) ? na : (yoy_rate - yoy_rate_prev)
// 計算年增率的百分比變化 ((Current YoY - Previous YoY) / Previous YoY * 100)
// 避免除以零,並處理 NA 值
float yoy_rate_percentage_change = na(yoy_rate) or na(yoy_rate_prev) or yoy_rate_prev == 0 ? na : (yoy_rate_diff_points / math.abs(yoy_rate_prev)) * 100
// —————— 3. 移動均線設定與計算 (Moving Average Settings & Calculation) ——————
group_ma = "移動均線設定"
ma_length = input.int(5, "移動均線長度 (MA Length)", minval=1, group=group_ma)
ma_type = input.string("SMA", "移動均線類型 (MA Type)", options=["SMA", "EMA", "WMA", "RMA"], group=group_ma)
// 移除了 ma_color 輸入,因為用戶表示樣式中可以設定
// 根據選擇的類型計算移動均線
float ma_value = switch ma_type
"SMA" => ta.sma(yoy_rate, ma_length) // Simple Moving Average
"EMA" => ta.ema(yoy_rate, ma_length) // Exponential Moving Average
"WMA" => ta.wma(yoy_rate, ma_length) // Weighted Moving Average
"RMA" => ta.rma(yoy_rate, ma_length) // Relative Moving Average (also known as Wilder's Smoothing Average)
=> na // Default case, should not happen
// —————— 4. 繪圖與邏輯檢查 (Plotting & Logic Check) ——————
// 繪製年增率曲線
plot(yoy_rate, "年增率 (YoY %)", color=color.new(#e2a60d, 0))
// 繪製移動均線,顏色將由用戶在 TradingView 的樣式設定中調整
plot(ma_value, "移動均線 (MA)", color=color.blue, linewidth=2) // 預設為藍色,用戶可在樣式中更改
// 繪製零軸線
hline(0, "零軸", color=color.new(color.gray, 50), linestyle=hline.style_dashed)
// 新增:繪製 +10 軸線
hline(10, "上軸", color=color.new(color.green, 50), linestyle=hline.style_dashed)
// 新增:繪製 -10 軸線
hline(-10, "下軸", color=color.new(color.red, 50), linestyle=hline.style_dashed)
// 新增一個更智能的警告系統,防止在錯誤的圖表週期下進行分析
bool is_monthly_mode = freq_option == "月度 (Monthly)"
bool is_quarterly_mode = freq_option == "季度 (Quarterly)"
// 獲取當前圖表的時間週期字串
string current_timeframe_period = timeframe.period
// 邏輯檢查:
// 1. 如果在「月度」模式下,但圖表不是月線 -> 發出警告 (1M 表示月線)
// 2. 如果在「季度」模式下,但圖表不是季線或月線 -> 發出警告 (3M 表示季線,1M 表示月線)
// (註:季度數據在月線圖上看也是合理的,因為一個季度包含三根月線K棒)
if (is_monthly_mode and current_timeframe_period != "1M") or (is_quarterly_mode and current_timeframe_period != "3M" and current_timeframe_period != "1M")
label.new(bar_index, ta.highest(high, 20), "警告:請檢查您的圖表時間週期是否與所選的數據頻率匹配!",
color=color.new(color.red, 20), textcolor=color.white,
style=label.style_label_down, yloc=yloc.price)
// —————— 5. 狀態列顯示 (Status Line Display) ——————
// 繪製新的數據,使其顯示在數據視窗/狀態列中
// 將 plot 的 color 設為完全透明,使其不繪製線條,但值仍會顯示在數據視窗和狀態列中
plot(yoy_rate_diff_points, "年增率月增減百分點 (YoY % Point Change MoM)", color=color.rgb(16, 199, 202), precision=2)
Skrip sumber terbuka
Dalam semangat sebenar TradingView, pencipta skrip ini telah menjadikannya sumber terbuka supaya pedagang dapat menilai dan mengesahkan kefungsiannya. Terima kasih kepada penulis! Walaupun anda boleh menggunakannya secara percuma, ingat bahawa menerbitkan semula kod ini adalah tertakluk kepada Peraturan Dalaman kami.
Penafian
Maklumat dan penerbitan adalah tidak dimaksudkan untuk menjadi, dan tidak membentuk, nasihat untuk kewangan, pelaburan, perdagangan dan jenis-jenis lain atau cadangan yang dibekalkan atau disahkan oleh TradingView. Baca dengan lebih lanjut di Terma Penggunaan.
Skrip sumber terbuka
Dalam semangat sebenar TradingView, pencipta skrip ini telah menjadikannya sumber terbuka supaya pedagang dapat menilai dan mengesahkan kefungsiannya. Terima kasih kepada penulis! Walaupun anda boleh menggunakannya secara percuma, ingat bahawa menerbitkan semula kod ini adalah tertakluk kepada Peraturan Dalaman kami.
Penafian
Maklumat dan penerbitan adalah tidak dimaksudkan untuk menjadi, dan tidak membentuk, nasihat untuk kewangan, pelaburan, perdagangan dan jenis-jenis lain atau cadangan yang dibekalkan atau disahkan oleh TradingView. Baca dengan lebih lanjut di Terma Penggunaan.