如何部分关闭pinescript交易(tradingview)?

时间:2020-04-18 12:02:04

标签: pine-script trading

我正在努力寻找如何根据不同条件在Tradingview中部分关闭pinescript中的未平仓交易的方法。我正在尝试使用pos_size2部分关闭大小为strategy.order的交易,条件是: 以long_stop2的价格出售50% -以long_stop1

为条件出售剩余的50%

任何帮助将不胜感激。

// LONG
//my_margin = strategy.equity * margin_pct
//pos_size = (my_margin * leverage) / price
//pos_size2 = (my_margin * leverage2) / price
//symbol = security(syminfo.tickerid, resolution, close)
//percent = input(defval=50.0, title='Percentage of position to take profit.')
//size_trim = (pos_size2 / 2)

//Open Order
entry = 0

if trendhi_ema > trendlo_ema
    entry:=2
    strategy.entry(id='Long Signal', long=true, qty=pos_size2, when= window() and long_signal, comment = "2.5x Long")
else
    entry:=1
    strategy.entry(id='Long Signal', long=true, qty=pos_size, when= window() and long_signal, comment = "1.5x Long")

//Close Order

if trendhi_ema > trendlo_ema
    if entry == 2
        strategy.order(id='Long Signal', long=false, qty=size_trim, when= window() and long_stop2 and strategy.position_size > 0, comment = "close first 50% long")
        strategy.order(id='Long Signal', long=false, qty=size_trim, when= window() and long_stop1 and strategy.position_size > 0, comment = "close second 50% long")
    if entry == 1
        strategy.order(id='Long Signal', long=false, qty=pos_size, when= window() and long_stop2 and strategy.position_size > 0, comment = "close long")
else
    strategy.order(id='Long Signal', long=false, qty=pos_size, when= window() and long_stop2 and strategy.position_size > 0, comment = "close")

1 个答案:

答案 0 :(得分:1)

id参数很重要。要修改id命令参数时,请使用相同的strategy.***。如果要指定其他命令,请使用其他id

//Close Order

if trendhi_ema > trendlo_ema
    if entry == 2
        strategy.order(id='Long Signal 1', long=false, qty=size_trim, when= window() and long_stop2 and strategy.position_size > 0, comment = "close first 50% long")
        strategy.order(id='Long Signal 2', long=false, qty=size_trim, when= window() and long_stop1 and strategy.position_size > 0, comment = "close second 50% long")
    if entry == 1
        strategy.order(id='Long Signal 3', long=false, qty=pos_size, when= window() and long_stop2 and strategy.position_size > 0, comment = "close long")
else
    strategy.order(id='Long Signal 4', long=false, qty=pos_size, when= window() and long_stop2 and strategy.position_size > 0, comment = "close")