title: How to avoid TP being fired with SL, if TP has not yet been reached?
body: <p>The script sends messages to the broker for each action. Therefore to avoid errors, a TP order can not fire when an SL order does.</p>
  <p>I'd be extremely grateful for any concrete examples with the code below, on how to have TP fire only if it reaches target, otherwise have SL close entire (or remaining position) by itself.</p>
  <p>I tried various strategy.order combinations but I just can not get my head around this one.</p>
  <pre><code>//@version=5
  //©duronic12 
  strategy('TP/STOP ISSUE', overlay=true, max_bars_back=5000, process_orders_on_close=true)
  
  
      // MANAGE RISK 
  
  tradeDirection = input.string(title='Trade Direction', options=['Long', 'Short', 'Both'], defval='Long', group='Risk Management')
  longOK = tradeDirection == 'Long' or tradeDirection == 'Both'
  shortOK = tradeDirection == 'Short' or tradeDirection == 'Both'
  stopPer = input(2.0, title='SL (%)', group='Risk Management') / 100
  bep = input(4.0, title='Move SL to Breakeven (%)', group='Risk Management')
  takePer = input(6.0, title='TP (%)', group='Risk Management') / 100
  qt = input(50.0, title='% to close on TP', group='Risk Management')
  closeTC = input(true,&quot;Trail after TP&quot;, group='Risk Management')
  ////
  
  
      // ENTER
  
  HPeriod = 13
  LPeriod = 21
  hsma = 0.0
  lsma = 0.0
  hsma := ta.sma(high, HPeriod)
  lsma := ta.sma(low, LPeriod)
  iff_1 = close &lt; nz(lsma[1]) ? -1 : 0
  HLd = close &gt; nz(hsma[1]) ? 1 : iff_1
  HLv = ta.valuewhen(HLd != 0, HLd, 0)
  HiLo = HLv == -1 ? hsma : lsma
  HLcolor = HLv == -1 ? color.maroon : color.blue
  plot(HiLo, title=&quot;Entry/Trail&quot;, linewidth=1, color=HLcolor)
  
  enterLong = (HLv == 1 and HLv[1] == -1)
  enterShort = (HLv == -1 and HLv[1] == 1)
  
  if longOK and enterLong
      strategy.entry(id='EL', comment='BUY', direction=strategy.long)
  if shortOK and enterShort
      strategy.entry(id='ES', comment='SELL', direction=strategy.short)
  ////
  
  
      // EXIT 
  
  longStop = strategy.position_avg_price * (1 - stopPer)
  shortStop = strategy.position_avg_price * (1 + stopPer)
  shortTake = strategy.position_avg_price * (1 - takePer)
  longTake = strategy.position_avg_price * (1 + takePer)
  shortTake1 = strategy.position_avg_price * (1 - (99/100))
  
  ExitSell()=&gt;
      (bar_index - strategy.closedtrades.exit_bar_index(strategy.closedtrades-1)) == 0 and strategy.position_size[1]&lt;0
  ExitBuy()=&gt;
      (bar_index - strategy.closedtrades.exit_bar_index(strategy.closedtrades-1)) == 0 and strategy.position_size[1]&gt;0
  EntryBuy()=&gt;
      (strategy.opentrades &gt; 0 ? (bar_index - strategy.opentrades.entry_bar_index(strategy.opentrades-1)) : na) == 1 and strategy.position_size&gt;0
  EntrySell()=&gt;
      (strategy.opentrades &gt; 0 ? (bar_index - strategy.opentrades.entry_bar_index(strategy.opentrades-1)) : na) == 1 and strategy.position_size&lt;0
  
  H = ta.highest(high,nz(ta.barssince(EntryBuy()),1)+1)
  L = ta.lowest(low,nz(ta.barssince(EntrySell()),1)+1)
  
  profit = strategy.position_size&gt;0?((H-strategy.position_avg_price)/strategy.position_avg_price)*100:strategy.position_size&lt;0?((L-strategy.position_avg_price)/strategy.position_avg_price)*-100:na
  
  SL_long = profit&gt;=bep?strategy.position_avg_price:longStop
  SL_short = profit&gt;=bep?strategy.position_avg_price:shortStop
  
  plot_sl1 = plot(strategy.position_size&gt;0?SL_long:na,&quot;Long SL&quot;,color=color.orange,linewidth=1,style=plot.style_linebr)
  plot_sl2 = plot(strategy.position_size&lt;0?SL_short:na,&quot;Short SL&quot;,color=color.orange,linewidth=1,style=plot.style_linebr)
  
  //Partial Exit via Take Profit
  if strategy.position_size &gt; 0
      strategy.exit(id='TP', qty_percent=qt, stop=SL_long, limit=longTake)
  if strategy.position_size &lt; 0
      strategy.exit(id='TP', qty_percent=qt, stop=SL_short, limit=shortTake)
  
  //Full Exit via Stop Loss  
  if strategy.position_size &gt; 0
      strategy.exit(id='SL', qty_percent=100, stop=SL_long, limit=longTake*1000)
  if strategy.position_size &lt; 0
      strategy.exit(id='SL', qty_percent=100, stop=SL_short, limit=shortTake1)
  
  //Full Exit via Trailing Stop 
  if profit&gt;=(takePer*100) and strategy.position_size &gt; 0 and HLv == -1 and closeTC
      strategy.close('EL',comment='TSL')
  if profit&gt;=(takePer*100) and strategy.position_size &lt; 0 and HLv == 1 and closeTC
      strategy.close('ES',comment='TSL')
  ////
  </code></pre>