使用 barssince(change(strategy.position_size)) > 10 时条目不起作用

Entry is not working when using barssince(change(strategy.position_size)) > 10

我的脚本有一个奇怪的问题。

这是工作代码:

//@version=4
strategy("Test script", overlay=true, pyramiding=100)
process_orders_on_close=true

// FACTOR 1X MACD
fastMA = round(12*1)
slowMA = round(26*1)
signal = round(9*1)
[Macd1x,_,Hist] = macd(close[0], fastMA, slowMA, signal)

// FACTOR 4X MACD
fastMA4x = round(12*4)
slowMA4x = round(26*4)
signal4x = round(9*4)
[Macd4x,_,_] = macd(close[0], fastMA4x, slowMA4x, signal4x)

// TRADE CONDITIONS
PreventMultipleEntrys = barssince(change(strategy.position_size)) > 10

BuySignal = Macd1x > 0 and Macd4x > 0 and PreventMultipleEntrys
SellSignal = Macd1x < 0 and Macd4x < 0

strategy.entry(id="Enter Long", long=true, when=BuySignal)
strategy.entry(id="Enter Short", long=false, when=SellSignal)

所以我在这里得到多头和空头交易条目。 但是,当我也将 PreventMultipleEntrys 添加到我的卖出信号时,一切都停止了。我没有收到任何买入或卖出信号,但编译器仍然没有错误?

聪明的人可以帮我解决这个奇怪的错误吗? 这是 NONE 工作代码:

//@version=4
strategy("Test script", overlay=true, pyramiding=100)
process_orders_on_close=true

// FACTOR 1X MACD
fastMA = round(12*1)
slowMA = round(26*1)
signal = round(9*1)
[Macd1x,_,Hist] = macd(close[0], fastMA, slowMA, signal)

// FACTOR 4X MACD
fastMA4x = round(12*4)
slowMA4x = round(26*4)
signal4x = round(9*4)
[Macd4x,_,_] = macd(close[0], fastMA4x, slowMA4x, signal4x)

// TRADE CONDITIONS
PreventMultipleEntrys = barssince(change(strategy.position_size)) > 10

BuySignal = Macd1x > 0 and Macd4x > 0 and PreventMultipleEntrys
SellSignal = Macd1x < 0 and Macd4x < 0 and PreventMultipleEntrys //This line makes everything stop working

strategy.entry(id="Enter Long", long=true, when=BuySignal)
strategy.entry(id="Enter Short", long=false, when=SellSignal)

这使用不同的逻辑来防止多头和空头的早期进入。您的代码编写方式的问题在于,永远不会发生第一笔交易来让事情继续下去,因为 10 根柱线之前头寸规模的第一次变化从未发生过:

//@version=4
strategy("Test script", overlay=true, pyramiding=100)
process_orders_on_close=true

// FACTOR 1X MACD
fastMA = round(12*1)
slowMA = round(26*1)
signal = round(9*1)
[Macd1x,_,Hist] = macd(close[0], fastMA, slowMA, signal)

// FACTOR 4X MACD
fastMA4x = round(12*4)
slowMA4x = round(26*4)
signal4x = round(9*4)
[Macd4x,_,_] = macd(close[0], fastMA4x, slowMA4x, signal4x)

// TRADE CONDITIONS
PreventMultipleLongEntries = sum(change(strategy.position_size) > 0 ? 1 : 0, 10) == 0
PreventMultipleShortEntries = sum(change(strategy.position_size) < 0 ? 1 : 0, 10) == 0

BuySignal = Macd1x > 0 and Macd4x > 0 and PreventMultipleLongEntries
SellSignal = Macd1x < 0 and Macd4x < 0 and PreventMultipleShortEntries

strategy.entry(id="Enter Long", long=true, when=BuySignal)
strategy.entry(id="Enter Short", long=false, when=SellSignal)