乖離率と移動平均線_PINEコード

PINEコード

 

//Added a moving average line for the deviation rate itself.

//@version=6
indicator(‘Moving Average Deviation Rate with MA Type’,shorttitle=”MA Deviation Rate with MA”, overlay = false)

//User input
i_Period = input(75, title = ‘Moving Average Period’)
i_type = input.string(defval = ‘EMA’, title = ‘Moving Average Type’, options = [‘SMA’, ‘EMA’])
i_hline = input.float(0.0, minval=0, title = ‘Upper Band’)
i_lline = input.float(0.0, maxval=0, title = ‘Lower Band’)
i_sig = input(true, title = ‘Signal’, tooltip = ‘Show signals when deviation rate crosses over/under upper/lower band.’)
i_sig_loc = input.string(defval = ‘Sub Chart’, title = ‘Signal Location’, options = [‘Main Chart’, ‘Sub Chart’])

// — ここから追加 —
i_devrate_ma_period = input.int(20, title=”乖離率の移動平均線 期間”, group=”乖離率MA設定”)
// — ここまで追加 —

//Declare function to calculate MA based on selected MA type: SMA or EMA or others
ma1(i_type, close, i_Period) =>
i_type == ‘EMA’ ? ta.ema(close, i_Period) : i_type == ‘SMA’ ? ta.sma(close, i_Period) : i_type == ‘SMMA (RMA/MMA)’ ? ta.rma(close, i_Period) : i_type == ‘WMA’ ? ta.wma(close, i_Period) : i_type == ‘VWMA’ ? ta.vwma(close, i_Period) : na

//Deviation rate
v_ma = i_type == ‘SMA’ ? ta.sma(close, i_Period) : i_type == ‘EMA’ ? ta.ema(close, i_Period) : na
devrate = close / v_ma * 100 – 100

// — ここから追加 —
// 乖離率の移動平均線を計算
v_devrate_ma = ta.sma(devrate, i_devrate_ma_period)
// — ここまで追加 —

//Plot
// 元のプロットにtitleを追加して設定しやすくしました
plot(devrate, title=”乖離率”, color = color.new(color.blue, 0), linewidth=2)
// — ここから追加 —
// 計算した乖離率の移動平均線を描画
plot(v_devrate_ma, title=”乖離率のMA”, color=color.new(color.red, 0), linewidth=1)
// — ここまで追加 —

hline(i_hline, “上限バンド”, color = color.black)
hline(i_lline, “下限バンド”, color = color.black)

//Signals
XO = ta.crossover(devrate, i_hline)
XU = ta.crossunder(devrate, i_lline)

//Sub chart
plotshape(i_sig == true and i_sig_loc == “Sub Chart” ? XO : false, style = shape.triangledown, color = color.new(color.green, 0), location = location.top, size = size.tiny, title = ‘Upper Band Cross’)
plotshape(i_sig == true and i_sig_loc == “Sub Chart” ? XU : false, style = shape.triangleup, color = color.new(color.red, 0), location = location.bottom, size = size.tiny, title = ‘Lower Band Cross’)

//Main chart
plotshape(i_sig == true and i_sig_loc == “Main Chart” ? XO : false, style = shape.triangledown, color = color.new(color.green, 0), location = location.abovebar, size = size.tiny, title = ‘Upper Band Cross’, force_overlay = true)
plotshape(i_sig == true and i_sig_loc == “Main Chart” ? XU : false, style = shape.triangleup, color = color.new(color.red, 0), location = location.belowbar, size = size.tiny, title = ‘Lower Band Cross’,force_overlay = true)

//Alert conditions
alertcondition(XO, title=’Upper Band Cross’, message=”Deciation rate crosses over upper band” )
alertcondition(XU, title=’Lower Band Cross’, message=”Deciation rate crosses under upper band” )

 

コメント

タイトルとURLをコピーしました