TradingView
Sharad_Gaikwad
2 jun 2022 17:40

Traling.SL.Target 

Nifty 50 IndexNSE

Beskrivning

Trailing SL and Target

I have seen few requests in PineScripters telegram group asking questions about implementation of trailing stop-loss (SL) and targets. This script is one of the way to implement the same.

This script is developed based on dark color theme and is best viewed using dark color theme.

How and where can this script be used:
The script is built to demonstrate how one can implement the trailing SL and target, so by referring the script one can mimic the approach and add trailing SL and target implementation in their own strategy.

How it works:
To demonstrate the SL and target implementation, i have considered simple EMA crossover strategy.

Key Input Parameters
Method to use for SL/Target trailing:
1. % Based Target and SL - Used to calculate trailing based on parameters defined under group '% Based Target SL'
2. Fixed point Based Target and SL - Used to calculate trailing based on parameters defined under group 'Fixed point Based Target and SL'

% Based Target and SL:
Initial profit % - This is used to calculate target when trade is initiated
Initial SL % - This is used to calculate SL when trade is initiated
Initiate trailing % - This parameter determines, when to start trailing SL and target.
Trail profit by % - Target would be trailed by % specified as this parameter
Trail SL by % - SL would be trailed by % specified as this parameter
e.g.
Trade type: - Long
Trade price: 10000
initial profit %: 1
Initial SL %: 1
Initiate trailing %: 0.5
Trail profit by %: 0.3
Trail SL by %: 0.4
Calculations based on above:
initial profit %: 10100 (trade price + 1%)
Initial SL %: 9900 (trade price - 1%)
Initiate trailing %: 10049.5 (initial profit - 0.5%)
Trail profit by %: 10130 (initial profit + 0.3%)
Trail SL by %: 9939.6 (initial SL + 0.4%)

For next iteration of Trailing SL and target above calculated values will be taken as a base and next set of values will be calculated. these calculations will continue till the trade is exited either on price reaching profit or SL point.

Fixed point Based Target and SL:
Initial profit target points - To derive initial target, parameter value is added to trade price in case of long trade.
Initial SL points - To derive SL point, parameter value is subtracted from trade price
Initiate trailing points - To derive start of trailing logic, parameter value is subtracted from initial profit point.
Trail profit by points - In case of long trade, parameter value is added to the profit target to derive new trailed profit target.
Trail SL by % - In case of long trade, parameter value is added to the SL initial point to derive new trailed SL.

Calculation of Trailing SL and target will continue till the trade is exited either on price reaching profit or SL point.


Plots displayed on the chart:
Apart from default trade markings i have added 3 shapes on the chart to describe working of Trailing SL and targets.
Diamond shape marks - These are added on the chart when trade is initiated. These shapes gives additional trade information by way of 'tooltip'. This information can be viewed by placing mouse pointer on the shape.
Circle shape marks - These are added on the chart whenever Trailing SL and targets are calculated. These shapes gives additional trade information by way of 'tooltip'. This information can be viewed by placing mouse pointer on the shape. You will also notice a number displayed just above or below circle denoting Trailing iteration.
Labels up and label down shapes - These are dynamically placed on the chart whenever trade is in progress. These labels will display ongoing trades, Target and SL points.
Kommentarer
Trendoscope
Good job mate.
PineCoders
VJind
What a superb piece of work with excellent results. Thank you for sharing this with us all Sharad Bhai :-), Is it possible to create Buy/Sell/Exit Alerts? This will help to integrate with Algo!!!
mtahreemalam
Awesome script @Sharad_Gaikwad. Would it be possible for you to plot a trailing line for SL and TP?
It would really help in understand where the trailing starts and ends etc
Sharad_Gaikwad
@mtahreemalam, Script plots Diamond shape marks when trade is initiated, Circle shape marks which denote when Trailing SL and targets calculation takes place and Label up and label down shapes to show ongoing trades Target and SL points. Diamond and Circle marks show additional information by way of tooltip.
mtahreemalam
@Sharad_Gaikwad, reply much appreciated.
fikira
Well deserved EP! Excellent!
sohith0422
can you please publish indicator code for this script...?
emadiano
Hello
I want to do this indicator with zendog strategy, is it possible? And if possible, please guide me. thank you
relevantPear84662
I think I have seen a candle closing above target in a long trade but target was revised. Is it so or I saw it in a wrong way?
Mer