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:
...
// Determine stop loss price based on swinglow/high
longStopPrice = periodHighestSwingLow
shortStopPrice = periodLowestSwingHigh
// Submit entry orders
if (enterLong)
strategy.entry(id="EL", long=true)
if (enterShort)
strategy.entry(id="ES", long=false)
// Submit exit orders based on calculated stop loss price
if (strategy.position_size > 0)
strategy.exit(id="XL STP", stop=longStopPrice)
if (strategy.position_size < 0)
strategy.exit(id="XS STP", stop=shortStopPrice)
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 trailling take profit) caught my attention. I'm now looking forward to implement the corresponding code in trading view to backtest the following scenario:
So far, what I tried was to implement logic inspired from this tradingview article and this one without success (as none of them actually use multiple exits orders based logic to exit their positions).
I also looked into docs strategy.order
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:
if (enterLong)
strategy.entry(id="EL", long=true)
strategy.order(id="stopLossLong", long=true, qty=(strategy.position_size/3), stop=(close + (close*0.01)))
if (enterShort)
strategy.entry(id="ES", long=false)
strategy.order(id="stopLossShort", long=false, qty=(strategy.position_size/3), stop=(close - (close*0.01)))
My current atempt is to use different strategy.exit
calls with the same ID, nevertheless, the take profit based on % never seems to be triggered with the following code.
// Submit entry orders
if (enterLong)
strategy.entry(id="EL", long=true)
if (enterShort)
strategy.entry(id="ES", long=false)
// STEP 3: Submit exit orders based on calculated stop loss price
if (strategy.position_size > 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 >= 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="XL STP", stop=longStopPrice)
if (strategy.position_size < 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 <= 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="XS STP", stop=shortStopPrice)
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).
Any input truly appreciated
For the record, I could achieve the desired result through the following code:
if (strategy.position_size > 0)
// if current closing price is upper position entry price plus 1%
target_take_profit_long_1 = strategy.position_avg_price * (1 + 0.01)
if close >= target_take_profit_long_1
strategy.exit(id='PROFIT LONG 1', from_entry="EL", limit=target_take_profit_long_1, qty_percent=25, comment="long: +1% / 25% of pos / 1% total TP")
stop=longStopPrice, comment="swinglow long exit")
strategy.exit(id="STOP LONG", from_entry="EL", stop=longStopPrice, comment="swinglow long exit")
if (strategy.position_size < 0)
target_take_profit_short_1 = strategy.position_avg_price * (1 - 0.01)
if close <= target_take_profit_short_1
strategy.exit(id='PROFIT SHORT 1', from_entry="ES", limit=target_take_profit_short_1, qty_percent=25, comment="short: +1% / 25% of pos / 1% total TP")
strategy.exit(id="STOP SHORT", from_entry="ES", stop=shortStopPrice, comment="swinghigh short exit")
The idea is to use different strategy.exit
calls based on conditions, specifying a stop
parameter alongside a qty_percent
one.
The result seems to work in backtest, adding trades events as desired between entry and exit, taking desired percents.