title: Take profit for a certain percent of the position, leave the rest to stop loss
body: <p>In tradingview I use a study and its related strategy version to backtest indicators. At the moment, I'm using very basic code in the strategy to exit the trades (based on a stop loss price derivated from swinglow/high calculated earlier in the script), the end of the script where the strategy order exit logic lives looks like this:</p>
  <pre><code>...
  
  // Determine stop loss price based on swinglow/high
  longStopPrice  = periodHighestSwingLow
  shortStopPrice = periodLowestSwingHigh
  
  // Submit entry orders
  if (enterLong)
      strategy.entry(id=&quot;EL&quot;, long=true)
  
  if (enterShort)
      strategy.entry(id=&quot;ES&quot;, long=false)
  
  // Submit exit orders based on calculated stop loss price
  if (strategy.position_size &gt; 0)
      strategy.exit(id=&quot;XL STP&quot;, stop=longStopPrice)
  
  if (strategy.position_size &lt; 0)
      strategy.exit(id=&quot;XS STP&quot;, stop=shortStopPrice)
  
  </code></pre>
  <p>As I'm using alertatron to interract with exchanges, the fact that you can use take profit for a certain percent of your position (for the record, through some function they call <a href="https://alertatron.com/docs/automated-trading/trailing-take-profit-order" rel="nofollow noreferrer">trailling take profit</a>) caught my attention. I'm now looking forward to implement the corresponding code in trading view to backtest the following scenario:</p>
  <ul>
  <li>if price raises of 1%, sell 1/3 of your position</li>
  <li>leave the rest of your position (2/3) and let it exit with current stop/loss logic (the one based on swinglows/high)</li>
  </ul>
  <p>So far, what I tried was to implement logic inspired from <a href="https://kodify.net/tradingview/orders/percentage-profit/" rel="nofollow noreferrer">this tradingview article</a> and <a href="https://kodify.net/tradingview/orders/percentage-trail/" rel="nofollow noreferrer">this one</a> without success (as none of them actually use multiple exits orders based logic to exit their positions).</p>
  <p>I also looked into docs <code>strategy.order</code> but it doesn't seem to be available examples in the docs. here is what I ended up with trying to place an additional order on entry but it doesn't provide data in the strategy tester output:</p>
  <pre><code>if (enterLong)
      strategy.entry(id=&quot;EL&quot;, long=true)
      strategy.order(id=&quot;stopLossLong&quot;, long=true, qty=(strategy.position_size/3), stop=(close + (close*0.01)))
      
  if (enterShort)
      strategy.entry(id=&quot;ES&quot;, long=false)
      strategy.order(id=&quot;stopLossShort&quot;, long=false, qty=(strategy.position_size/3), stop=(close - (close*0.01)))
  </code></pre>
  <p>My current atempt is to use different <code>strategy.exit</code> calls with the same ID, nevertheless, the take profit based on % never seems to be triggered with the following code.</p>
  <pre><code>// Submit entry orders
  if (enterLong)
      strategy.entry(id=&quot;EL&quot;, long=true)
  
  if (enterShort)
      strategy.entry(id=&quot;ES&quot;, long=false)
  
  // STEP 3: Submit exit orders based on calculated stop loss price
  if (strategy.position_size &gt; 0)
      // if current closing price is upper position entry price plus 1%
      target_take_profit_long = strategy.position_avg_price * (1 + 0.01)
      if close &gt;= target_take_profit_long
          strategy.exit('XS STP', 'Short', limit=target_take_profit_long, qty_percent=25, comment='Close-Sell-Profit')
      // else, wait for current stop loss
      strategy.exit(id=&quot;XL STP&quot;, stop=longStopPrice)
  
  if (strategy.position_size &lt; 0)
      // if current price (close) is below position entry price minus 1%
      target_take_profit_short = strategy.position_avg_price * (1 - 0.01)
      if close &lt;= target_take_profit_short
          strategy.exit('XS STP', 'Long', limit=target_take_profit_short, qty_percent=25, comment='Close-Buy-Profit')
      // else, wait for current stop loss
      strategy.exit(id=&quot;XS STP&quot;, stop=shortStopPrice)
  
  </code></pre>
  <p>So here is the question: is it any way to implement multiple exits in a TradingView strategy so I can do both, securize à part of my position when some %of initial price is reached, and leave the rest to stop loss rule (in the context of a strategy).</p>
  <p>Any input truly appreciated</p>