strategy.position_size 不计算在同一蜡烛内开盘和收盘的交易

时间:2021-02-25 02:32:23

标签: pine-script

交易结束/退出后,无论成功与否,我都想等待 5 分钟,然后再进行任何新交易。

我正在使用“strategy.position_size”来检查前 5 个柱。如果之前的 5 根柱线都为零交易,则只有这样才能开始新的交易。

这一行似乎满足了我的需要:

BarsBack = sum(abs(change(strategy.position_size)) > 0 ? 1 : 0, 5) == 0

(感谢 Entry is not working when using barssince(change(strategy.position_size)) > 10 的想法)

但是,如果交易在同一条柱内开仓和平仓,“strategy.position_size”不会将其注册为交易。它认为它是持平的,为零,因此,如果新的机会出现在下一根蜡烛中,该策略将进入,无需等待 5 分钟。

注意:

  • 我在 1 分钟的时间范围内运行此策略
  • 在下面的代码中,我使用了固定数量“5”,但在我的实际代码中,我使用了一个输入变量,因此我可以在测试中更改它。

感谢任何帮助,因为它正在帮助我。干杯。

BarsBack = sum(abs(change(strategy.position_size)) > 0 ? 1 : 0, 5) == 0

CurrentTrade = strategy.position_size == 0  // check if no open trades

if CurrentTrade
    if EMASpreadLongActive and CrossLowestLong and timeframe.isminutes
        strategy.entry("CONTBreakLongActive", strategy.long, qty = LotSize, when=BarsBack)
        strategy.exit("LongExit","CONTBreakLongActive", comment="Exit CONT Long",profit = ProfitTarget, loss = StopLoss)
        EMASpreadLongActive := false     // resetting
        CrossLowestLong := false         // resetting

    if EMASpreadShortActive and CrossHighestShort and timeframe.isminutes
        strategy.entry("CONTBreakShortActive", strategy.short, qty = LotSize, when=BarsBack)
        strategy.exit("ShortExit","CONTBreakShortActive",comment="Exit CONT Short", profit = ProfitTarget, loss = StopLoss)
        EMASpreadShortActive := false    // resetting
        CrossHighestShort := false       // resetting

1 个答案:

答案 0 :(得分:0)

好的,我找到了两种方法来解决我的问题。

1st,感谢 Paul Mendes,一个简单的方法:

var wait = 0.
wait := change(strategy.closed_trades) ? time + (5*(60000)) : wait

if time >= wait and buy_condition
  strategy.entry....

第二,再次感谢 Paul 在 pinecoders.com 上找到它。 不过这个比较复杂。 https://www.pinecoders.com/faq_and_code/#how-can-i-implement-a-time-delay-between-orders

干杯