首页 > 解决方案 > 多头/空头策略仅显示多头平仓挂单

问题描述

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

    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")

图表

谢谢!

标签: pine-scripttradingview-api

解决方案


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

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头寸strategy.entry("Long", strategy.long, comment="Long")


推荐阅读