价格触及通道时如何触发交易?

时间:2019-05-13 05:03:33

标签: pine-script

我是松树的新手,当价格达到Donchian Channel的上限时,我试图触发多头交易,反之亦然。

目前,根据我当前的代码,我可以看到价格已经触及上限,但直到很久以后才触发多头交易(参见图片)。

chart

我尝试减少Donchian周期(默认为20),但是它仍然不会触发信号。

这是我使用的代码:


length = input(20, minval=1)
lower = lowest(length)
upper = highest(length)
basis = avg(upper, lower)

// 4. Long trading conditions
enterLong = upTrend and (close >= upper) or (high >= upper) 
exitLong = (low <= lower) or (close <= basis)

// 5. Short trading conditions
enterShort = downTrend and (close <= lower) or (low <= lower)
exitShort = (high >= upper) or (close >= basis)

// 6. Submit entry orders
if (enterLong)
    strategy.entry(id="OL", long=true, qty=posSize)

if (enterShort)
    strategy.entry(id="OS", long=false, qty=posSize)

// 7. Submit exit orders
strategy.close(id="OL", when=exitLong)
strategy.close(id="OS", when=exitShort)

有什么方法可以改善它吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

您的逻辑有问题:

enterLong = upTrend and (close >= upper) or (high >= upper) 

如果false则评估为upTrend and close < upper or high >= upper

将其更改为

enterLong = upTrend and high >= upper
enterShort = downTrend and low <= lower