我创建了一个小脚本,目标是当macd在大多数较高时间范围(1W,3D,2D,D等)交叉看涨时,走多长时间。为此,我使用了内置的安全功能,该功能假定是返回我输入的任何分辨率的源值。
//@version=2
strategy("test strategy2", shorttitle="test2", overlay= false )
source = hlc3
fastLength = input(12, minval=1), slowLength=input(26,minval=1)
signalLength=input(9,minval=1)
macdval = ema(source, fastLength) - ema(source, slowLength)
signalval = sma(macdval, signalLength)
hist = macdval - signalval
res="1W"
macd= security(tickerid, res, macdval)
signal= security(tickerid, res, signalval)
res2="3D"
macd2 = security(tickerid, res2, macdval)
signal2= security(tickerid, res2, signalval)
res3="2D"
macd3 = security(tickerid, res3, macdval)
signal3= security(tickerid, res3, signalval)
res4="1D"
macd4 = security(tickerid, res4, macdval)
signal4= security(tickerid, res4, signalval)
macd_IsAbove(macd,signal) => macd >= signal
macd_IsBelow(macd,signal) => macd < signal
buy= macd_IsAbove(macd4,signal4) and macd_IsAbove(macd3,signal3)
and macd_IsAbove(macd2,signal2) and macd_IsAbove(macd,signal)
sell= macd_IsBelow(macd4,signal4) and macd_IsBelow(macd3,signal3)
and macd_IsBelow(macd2,signal2) and macd_IsBelow(macd,signal)
if (buy)
strategy.entry("Bull_Div", strategy.long,
oca_type=strategy.oca.cancel, comment="Bull_Div")
if (sell)
strategy.entry("Bear_Div", strategy.short,
oca_type=strategy.oca.cancel, comment="Bear_Div")
现在,当我运行脚本时,我期望所有时间范围内的买入和卖出头寸都相同,但是当我在不同时间范围内运行脚本时,我会得到不同的买入和卖出信号……我迷失了方向。为什么会这样?