8

I'm trying to implement a simple strategy where I enter a long when I receive a buy signal, then I want to take multiple profits and set a stop loss :

  • Sell 25% quantity at 1% profit
  • Sell 25% quantity at 2% profit
  • Sell 25% quantity at 3% profit
  • Sell 25% quantity at 4% profit
  • Stop loss at 2%

I've tried many things based on strategy.close, strategy.exit, strategy.entry but didn't find anything working. Does anyone have experience with that kind of strategy?

Thanks

Jul
  • 1,039
  • 3
  • 12
  • 20
  • 1
    Hi, I have managed in a script of mine to do something in that direction that works fine BUT having only one take profit plateau, with or without stoploss at will. I have the impression you can't launch multiple 'strategy.exit' commands from a same entry. `stragtegy.close` closes trades at market condition, and in backtest mode you won't get the take profit's necessarily at exact prices. I'm looking forward to see if a nice solutions pops up for your question. Maybe the way is using intermediate variables to save last trade prices, and use them as condition to trigger the next order. – Glauco Esturilio Apr 30 '20 at 00:39

1 Answers1

8

The example of such strategy:

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © adolgov

//@version=4
strategy("Multiple %% profit exits example", overlay=false, default_qty_value = 100)

longCondition = crossover(sma(close, 14), sma(close, 28))
if (longCondition)
    strategy.entry("My Long Entry Id", strategy.long)

shortCondition = crossunder(sma(close, 14), sma(close, 28))
if (shortCondition)
    strategy.entry("My Short Entry Id", strategy.short)

percentAsPoints(pcnt) =>
    strategy.position_size != 0 ? round(pcnt / 100 * strategy.position_avg_price / syminfo.mintick) : float(na)

lossPnt = percentAsPoints(2)

strategy.exit("x1", qty_percent = 25, profit = percentAsPoints(1), loss = lossPnt)
strategy.exit("x2", qty_percent = 25, profit = percentAsPoints(2), loss = lossPnt)
strategy.exit("x3", qty_percent = 25, profit = percentAsPoints(3), loss = lossPnt)
strategy.exit("x4", profit = percentAsPoints(4), loss = lossPnt)

profitPercent(price) =>
    posSign = strategy.position_size > 0 ? 1 : strategy.position_size < 0 ? -1 : 0
    (price - strategy.position_avg_price) / strategy.position_avg_price * posSign * 100

p1 = plot(profitPercent(high), style=plot.style_linebr, title = "open profit % upper bound")
p2 = plot(profitPercent(low), style=plot.style_linebr, title = "open profit % lower bound")
fill(p1, p2, color = color.red)

How it works you can see on https://www.tradingview.com/script/kHhCik9f-Multiple-profit-exits-example/

Andrey D
  • 1,534
  • 1
  • 5
  • 7
  • 1
    Please could you add to your post that you're the author of this code? Please also make sure that you're happy licencing it under the terms that Stack Overflow uses which is CC BY-SA – Jon Clements Apr 30 '20 at 15:33
  • That's really useful, thanks. I have a question. In a strategy I'm working on I have two Strategy.Exit orders A and B. A has a stop-market and B is close 50% at percentProfit=5%. However sometimes A executes before B profit is hit and only closes half. Is there a way to strategy.exit on A and cancel other strategy.exit B at the same time? – Dr. Andrew Burnett-Thompson Jan 25 '22 at 09:18
  • There are several ways to achieve this. But it would be better to consider this task in a separate question. – Andrey D Jan 26 '22 at 10:20