使用 barsince(change(strategy.position_size)) > 时输入不起作用10 [英] Entry is not working when using barssince(change(strategy.position_size)) > 10
问题描述
我的脚本有一个奇怪的问题.
I have a wierd problem in my script.
这是工作代码:
//@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
也添加到我的卖出信号时,一切都会停止工作.我没有收到任何买入或卖出信号,但编译器中仍然没有错误?
So I get both LONG and SHORT trade entries here.
But, when I add the PreventMultipleEntrys
to my sell signal aswell, everything stops working. I dont get any buy or sell signals but still no error in the compiler?
聪明人能帮我解决这个奇怪的错误吗?这是 NONE 工作代码:
Can someone smart help me here with this wierd error? Here is the NONE working code:
//@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 柱前的头寸规模的第一次变化从未发生过:
This uses distinct logic to prevent early entries for longs and shorts. The problem with the way your code was written is that a first trade could never occur to get things going because a first change in position size 10 bars ago never happened:
//@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)
这篇关于使用 barsince(change(strategy.position_size)) > 时输入不起作用10的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!