仅当两个连续的交叉方向相同时才触发信号

时间:2019-04-25 14:01:26

标签: pine-script

我有一个简单的松木脚本,可以在蜡烛越过蜡烛线时改变蜡烛的颜色。我要实现的是,仅当两个交叉口在同一方向时才应触发。

尝试过但无法提出一个好的解决方案。

study(title="KijCrossTest", shorttitle="KijCrossTest", overlay = false )


conversionPeriods = input(9, minval=1),
basePeriods = input(26, minval=1)
laggingSpan2Periods = input(52, minval=1, title="Lagging Span 2 
Periods"),
displacement = input(26, minval=1)
donchian(len) => avg(lowest(len), highest(len))
conversionLine = donchian(conversionPeriods)
baseLine = donchian(basePeriods)
leadLine1 = avg(conversionLine, baseLine)
leadLine2 = donchian(laggingSpan2Periods)
midtk = (conversionLine+baseLine)/2


long  = iff ( (close > baseLine)  and (low<=baseLine) and (close>open) 
,1, -1)

short = iff ( (close < baseLine)and (high>=baseLine) and (close<open) 
,-1, 1)



barcolor(long == 1 ? lime: short == -1 ? red:close < open? black : white)

plot(long, title="long", style=line, linewidth=1, color=green )
plot(short, title="short", style=line, linewidth=1, color=red )

image

1 个答案:

答案 0 :(得分:1)

//@version=3
study("My Script", overlay=true, max_bars_back=5000)

s1 = sma(close, 10)
s2 = sma(close, 200)

plot(s1, color=red)
plot(s2, color=green)

crossed = cross(s1, s2)

plot(crossed ? 100 : 0, color=green, style=histogram)

//NOTE: used crosseed[1], because barssince(crosseed) would return 0
since = barssince(crossed[1])

colorTheSame(s) => 
    close > open == close[s] > open[s]

triggered = crossed and colorTheSame(since)

plot(triggered ? 100 : 0)

我想这很接近您要寻找的东西。我以sma代替您的系列作为例子