如何抵消两条移动平均线并在它们相互交叉时更改背景颜色?

发布于 2025-01-09 18:53:12 字数 1253 浏览 0 评论 0原文

我创建了一个穿越移动平均线的指标,当 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡笑忘祈一世凡恋 2025-01-16 18:53:12

plot()offset 参数将在视觉上移动线条。这并不意味着您正在检查带有偏移量的值。

如果您想引用历史值(例如,如果交叉发生在两根柱之前),您需要使用 历史引用运算符

uptrend = ema_1 > ema_2
downtrend = ema_1 < ema_2

// uptrend[2] -> get the value of uptrend two bars ago
// downtrend[2] -> get the value of downtrend two bars ago

offset parameter of the plot() 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.

uptrend = ema_1 > ema_2
downtrend = ema_1 < ema_2

// uptrend[2] -> get the value of uptrend two bars ago
// downtrend[2] -> get the value of downtrend two bars ago
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文