首页 > 解决方案 > 为什么当蜡烛颜色改变时这个脚本(pinescript)没有退出?

问题描述

我想在蜡烛收盘红色时退出做多,当蜡烛收盘绿色时退出做空。

如果达到一定的利润百分比,也想退出。

下面的代码是打开 LONG 并关闭...类似于 SHORT。

longExitPrice  = strategy.position_avg_price * (1+longProfitPerc)
shortExitPrice = strategy.position_avg_price / (1+shortProfitPerc)

closedGreen = barstate.isconfirmed and (close > open)
closedRed = barstate.isconfirmed and (close < open)

if (inDateRange)
    if(enterLONG and strategy.position_size == 0 )
        strategy.entry(id="LONG" , long=true , when=highABO)
    if (enterSHORT and strategy.position_size == 0 )
        strategy.entry(id="SHORT" , long=false , when=lowABO)



positions = strategy.position_size



if (strategy.position_size > 0  )
    strategy.exit("Exit/TP","LONG" , limit = longExitPrice)
    if(closedRed)
        strategy.exit("Stop Loss/TP","SHORT")    

if (strategy.position_size < 0  and close > open)
    strategy.exit("Stop Loss/TP","SHORT" , limit = shortExitPrice)
    if(closedGreen)
        strategy.exit("Stop Loss/TP","SHORT" )

标签: pine-script

解决方案


不要在strategy.exit()没有参数的情况下使用。改为使用strategy.close()

if(closedRed)
    strategy.close("Short")

推荐阅读