How to set targets and stop losses dynamically in TradingView using PineScript?
How to set targets and stop losses dynamically?
In our last episode, we created a simple crossover strategy. In that strategy, the trade execution was determined by moving average crossovers and crossunders.
We will now build on that script and set specific stop losses and take profits instead of waiting for crossovers to happen to reverse the trade.
To keep this simple, we will use the Average True Range (ATR) to calculate the profit target and SL.
The ATR indicator calculates the average movement over the last number of specified bars. This is a good way to account for changes in volatility.
We can use built-in ATR function to get this data. We have already published videos and data feeds on ATR and it’s importance as part of trading strategies series. Refer the same for more info on ATR.
So let’s add another line in the script for ATR.
atr = atr(14)
Now Under our trade conditions, we can make the necessary calculations for our stop loss and target.
// Execute trade if condition is True
if (longCondition)
stopLoss = low — atr * 2
takeProfit = high + atr * 2
strategy.entry(“long”, strategy.long, 100, when = rsi > 50)
strategy.exit(“exit”, “long”, stop=stopLoss, limit=takeProfit)
In the code above, we calculated the stop loss by taking the low of the bar at the time of entry and subtracting the average true range multiplied by two.
So if the stock moves on average 50 points per bar on average, we are setting our take SL 100 below the low of our entry candle.
A similar calculation is done for the target. We will target 2X of ATR from the high of our entry candle.
Lastly, we specify the exit condition using the strategy.exit() function. Here are the parameters that were passed through.
1. “exit” — this is the trade ID for exiting out of the trade.
2. ‘long” — this is the ID that we previously set when we entered the trade. This will let Pine script know which position we are trying to exit.
3. stop=stopLoss — we are specifying that the level contained in the stopLoss variable should be used as a stop order to exit the trade.
4. limit=takeProfit = we are specifying that the level contained in the takeProfit variable should be used a limit order to exit the trade.
The syntax for our short condition is similar although some of the calculations are slightly different.
if (shortCondition)
stopLoss = high + atr * 2
takeProfit = low — atr * 2
strategy.entry(“short”, strategy.short, 100, when = rsi < 50)
strategy.exit(“exit”, “short”, stop=stopLoss, limit=takeProfit)
The rest of the script remains unchanged from the prior example. Let’s run it and see how our strategy did.
Our exits are working and being plotted on our main chart along with the long and short entries.
Check Out Script Library to get script used for this episode:
https://marketsecrets.in/script-library/
To know more, checkout:
Related
Getting started with algo-trading basics how to backtest a trading strategy? how to do algo trading in india? how to do algo trading in Zerodha? how to start with algo trading in india? how to use amibroker afl for algo trading? how to use pine script for algo trading? how to write algo trading scripts? Pine script tutorial videos step-by-step approach to start with algo-trading in India