You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Pine Script中实现3时间框架收盘价与EMA对比的条件着色绘图

Pine Script多时间框架EMA柱形图修改方案

问题原因

你原来的代码同时调用了两次plot()函数分别绘制绿色、红色柱形:

  • 绿色柱形数值为当前收盘价高于对应EMA的数量之和,范围0~3
  • 红色柱形数值为当前收盘价低于对应EMA的数量之和的负数,范围-3~0
    当仅满足1~2个EMA高于收盘价的条件时,两个plot都会返回非0数值,就会出现红绿柱同时展示的问题。

修改方案

直接判断「三个时间框架收盘价均高于对应EMA」的条件,单次调用plot(),根据条件动态切换柱形颜色即可。

完整修改后代码

//@version=4
// All credits go to CMoody for the original Idea...
study(title="3-EMA MTF", shorttitle="3_EMA_MTF", precision=0, overlay=false)

//candle source
src = input(close, title="Source")
col = input(title="Color EMA", type=input.bool, defval=true)

//first EMA inputs
len  = input(200, minval=1, title="1st EMA Length")

//second EMA inputs
isDisplaySecondEMA = input(false, title="Show 2nd EMA?")
len2 = input(200, minval=1, title="2nd EMA Length")

//third EMA inputs
isDisplayThirdEMA = input(false, title="Show 3rd EMA?")
len3 = input(200, minval=1, title="3rd EMA Length")

//calculate 1st EMA values
out = ema(src, len)
resCustom = input(title="EMA Timeframe", type=input.resolution, defval="720")
value1 = security(syminfo.tickerid, resCustom, out, barmerge.gaps_off, barmerge.lookahead_on)

//calculate 2nd EMA values
out2 = ema(src, len2)
resCustom2 = input(title="EMA Timeframe", type=input.resolution, defval="240")
value2 = security(syminfo.tickerid, resCustom2, out2, barmerge.gaps_off, barmerge.lookahead_on)

//calculate 3rd EMA values
out3 = ema(src, len3)
resCustom3 = input(title="EMA Timeframe", type=input.resolution, defval="60")
value3 = security(syminfo.tickerid, resCustom3, out3, barmerge.gaps_off, barmerge.lookahead_on)

// 判断三个条件是否同时满足
all_above = close >= value1 and close >= value2 and close >= value3
// 单根柱形,根据条件换颜色
plot(1, style=plot.style_columns, color=all_above ? color.lime : color.red)

补充说明

如果需要避免未来函数导致的回测偏差,可以把security函数的最后一个参数barmerge.lookahead_on替换为barmerge.lookahead_off。

内容的提问来源于stack exchange,提问作者mgoblue33

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 10:27:03