如何抵消两条移动平均线并在它们相互交叉时更改背景颜色?
我创建了一个穿越移动平均线的指标,当 ema1 穿越 ema2 之上时,它是上升趋势,因此背景颜色变为绿色,当 ema1 穿越 ema2 之下时,它是下降趋势,背景颜色变为红色,但我希望能够为了抵消/移动移动平均线并在它们交叉时仍然得到相同的结果,我该怎么做?
//@version=5
indicator(title="Offset Emas Crossover", shorttitle="OEC", overlay=true, timeframe="", timeframe_gaps=true)
// EMA 1
length_of_ema_1 = input.int(50, minval=1, title="Length", group="EMA 1")
source_of_ema_1 = input(close, title="Source", group="EMA 1")
offset_of_ema_1 = input.int(title="Offset", defval=0, minval=-500, maxval=500, group="EMA 1")
ema_1 = ta.ema(source_of_ema_1, length_of_ema_1)
// EMA 2
length_of_ema_2 = input.int(100, minval=1, title="Length", group="EMA 2")
source_of_ema_2 = input(close, title="Source", group="EMA 2")
offset_of_ema_2 = input.int(title="Offset", defval=0, minval=-500, maxval=500, group="EMA 2")
ema_2 = ta.ema(source_of_ema_2, length_of_ema_2)
// TREND
float color = 255
float transparency = 75
uptrend = ema_1 > ema_2
downtrend = ema_1 < ema_2
// BACKGROUND COLOR
background_color = if uptrend
color.rgb(0, colour, 0, transparency)
else
color.rgb(colour, 0, 0, transparency)
// PLOTTING
plot(ema_1, color=color.blue, title="MA", offset=offset_of_ema_1)
plot(ema_2, color=color.red, title="MA", offset=offset_of_ema_2)
I created an indicator of crossing over moving averages, when ema1 crosses above ema2 it is an uptrend so the background color changes to green and when the ema1 crosses below ema2 it is a downtrend and the background color changes to red but i want to be able to offset/shift the moving averages and still get the same result whenever they crossover, how do i go about it?
//@version=5
indicator(title="Offset Emas Crossover", shorttitle="OEC", overlay=true, timeframe="", timeframe_gaps=true)
// EMA 1
length_of_ema_1 = input.int(50, minval=1, title="Length", group="EMA 1")
source_of_ema_1 = input(close, title="Source", group="EMA 1")
offset_of_ema_1 = input.int(title="Offset", defval=0, minval=-500, maxval=500, group="EMA 1")
ema_1 = ta.ema(source_of_ema_1, length_of_ema_1)
// EMA 2
length_of_ema_2 = input.int(100, minval=1, title="Length", group="EMA 2")
source_of_ema_2 = input(close, title="Source", group="EMA 2")
offset_of_ema_2 = input.int(title="Offset", defval=0, minval=-500, maxval=500, group="EMA 2")
ema_2 = ta.ema(source_of_ema_2, length_of_ema_2)
// TREND
float color = 255
float transparency = 75
uptrend = ema_1 > ema_2
downtrend = ema_1 < ema_2
// BACKGROUND COLOR
background_color = if uptrend
color.rgb(0, colour, 0, transparency)
else
color.rgb(colour, 0, 0, transparency)
// PLOTTING
plot(ema_1, color=color.blue, title="MA", offset=offset_of_ema_1)
plot(ema_2, color=color.red, title="MA", offset=offset_of_ema_2)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
plot()
的offset
参数将在视觉上移动线条。这并不意味着您正在检查带有偏移量的值。如果您想引用历史值(例如,如果交叉发生在两根柱之前),您需要使用 历史引用运算符。
offset
parameter of theplot()
will move the line visually. It does not mean that you are checking the values with an offset.If you want to refer to historical values (e.g. if the crossover happened two bars ago), you need to use the history reference operator.