多头/空头策略仅显示多头平仓单

时间:2021-07-22 04:19:08

标签: pine-script tradingview-api

这里是新手。我正在制定多头/空头策略。当我运行我的策略时,多头头寸按预期关闭,但我认为空头头寸不是。因为我将每笔订单的费用设置为美元,所以我想确保每个市价订单都打开/关闭,以便我可以回测我的策略。我的代码如下。

    hist = macd - signal
if (hist > 0.15)
    strategy.entry("Long", strategy.long, comment="Long")
if (hist < -0.15)
    strategy.close("Long")
if (hist < -0.15)
    strategy.entry("Short", strategy.short, comment="Short")
if (hist > 0.15)
    strategy.close("Short")

Chart

谢谢!

1 个答案:

答案 0 :(得分:1)

这是正确的行为,因为您的条件实际上定义为

pod update

因此,当您在 hist = macd - signal if (hist > 0.15) strategy.entry("Long", strategy.long, comment="Long") strategy.close("Short") if (hist < -0.15) strategy.close("Long") strategy.entry("Short", strategy.short, comment="Short") 条件下输入空头头寸时, 在下一个小节,当 (hist < -0.15) 为真时,您将在 if (hist > 0.15) 之前恢复您的 Short 位置。