Pine script 如何部分关闭交易脚本(tradingview)?

Pine script 如何部分关闭交易脚本(tradingview)?,pine-script,trading,Pine Script,Trading,我正在努力找出如何根据不同的条件在tradingview中部分关闭pinescript中的未平仓交易。我正试图使用策略部分关闭大小pos_size2的交易。订单取决于以下条件: -出售50%,条件是long\u stop2 -出售剩余的50%,条件是long\u stop1 任何帮助都将不胜感激 // LONG //my_margin = strategy.equity * margin_pct //pos_size = (my_margin * leverage) / price //pos_

我正在努力找出如何根据不同的条件在tradingview中部分关闭pinescript中的未平仓交易。我正试图使用
策略部分关闭大小
pos_size2
的交易。订单
取决于以下条件: -出售50%,条件是
long\u stop2
-出售剩余的50%,条件是
long\u stop1

任何帮助都将不胜感激

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

id
参数很重要。要修改策略时,请使用相同的
id
。***命令参数。如果要指定不同的命令,请使用不同的
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")