Author Message - Conversion not Typical -
This script returns trailing stops on the occurrence of market structure (CHoCH/BOS labeling). Trailing stops are adjusted based on trailing maximums/minimums with the option for users to be able to control how quickly a trailing stop can converge toward the price.
CODE:
CSS:
#// This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) #https://www.tradingview.com/v/c4Z9xThG/
#// © LuxAlgo
#indicator("Market Structure Trailing Stop [LuxAlgo]"
# Converted by Sam4Cok@Samer800 - 04/2023
input PivotLookback = 14; # 'Pivot Lookback'
input IncrementFactor = 100; # 'Increment Factor %'
input ResetStopOn = {default "CHoCH", "All"}; # 'Reset Stop On'
input showStructures = yes; # "Show Structures"
#//Style
DefineGlobalColor("bull" , CreateColor(0,137,123));
DefineGlobalColor("bear" , CreateColor(255,82,82));
DefineGlobalColor("dbull" , CreateColor(0, 61, 54));
DefineGlobalColor("dbear" , CreateColor(82, 0, 0));
DefineGlobalColor("ret" , Color.GRAY);
def na = Double.NaN;
def last = isNaN(close);
def CHoCH = ResetStopOn == ResetStopOn."CHoCH";
script FindPivots {
input dat = close; # default data or study being evaluated
input HL = 0; # default high or low pivot designation, -1 low, +1 high
input lbL = 14; # default Pivot Lookback Left
input lbR = 14; # default Pivot Lookback Right
##############
def _nan; # used for non-number returns
def _BN; # the current barnumber
def _VStop; # confirms that the lookforward period continues the pivot trend
def _V; # the Value at the actual pivot point
##############
_BN = BarNumber();
_nan = Double.NaN;
_VStop = if !IsNaN(dat) and lbR > 0 and lbL > 0 then
fold a = 1 to lbR + 1 with b=1 while b do
if HL > 0 then dat > GetValue(dat, -a) else dat < GetValue(dat, -a) else _nan;
if (HL > 0) {
_V = if _BN > lbL and dat == Highest(dat, lbL + 1) and _VStop
then dat else _nan;
} else {
_V = if _BN > lbL and dat == Lowest(dat, lbL + 1) and _VStop
then dat else _nan;
}
plot result = if !IsNaN(_V) and _VStop then _V else _nan;
}
def n = floor(AbsValue(CompoundValue(1, BarNumber(), 1)));
def ph = findpivots(high, 1, PivotLookback, PivotLookback);
def pl = findpivots(low, -1, PivotLookback, PivotLookback);
def ph_y;
def ph_x;
def ph_cross;
def ph_cross_;
def os;
def bullLine;
def ms;
def btm;
def pl_y;
def pl_x;
def pl_cross;
def pl_cross_;
def bearLine;
def top;
if !IsNaN(ph) {
ph_y = ph;
ph_x = n;
ph_cross = no;
} else {
ph_y = if ph_y[1]==0 then highest(high, PivotLookback) else ph_y[1];
ph_x = if ph_x[1]<=1 then 1 else ph_x[1];
ph_cross = ph_cross_[1];
}
if !IsNaN(pl) {
pl_y = pl;
pl_x = n;
pl_cross = no;
} else {
pl_y = if pl_y[1]==0 then lowest(low, PivotLookback) else pl_y[1];
pl_x = if pl_x[1]<=1 then 1 else pl_x[1];
pl_cross = pl_cross_[1];
}
#//-----------------------------------------------------------------------------}
#//Bullish structures
#//-----------------------------------------------------------------------------{
if close > ph_y and !ph_cross[1] {
ms = if CHoCH then if os[1] == -1 then 1 else 0 else 1;
ph_cross_ = yes;
bullLine = if showStructures then ph_y else na;
btm = fold i = 0 to (n - ph_x) with p=low do
Min(GetValue(low, i), p);
top = if top[1]==0 then high else top[1];
os = 1;
pl_cross_ = pl_cross;
bearLine = bearLine[1];
} else
if close < pl_y and !pl_cross[1] {
ms = if CHoCH then if os[1] == 1 then -1 else 0 else -1;
bearLine = if showStructures then pl_y else na;
pl_cross_ = yes;
ph_cross_ = ph_cross;
bullLine = bullLine[1];
btm = if btm[1]==0 then low else btm[1];
top = fold i1 = 0 to (n - pl_x) with p1=high do
Max(GetValue(high, i1), p1);
os = -1;
} else {
ms = if isNaN(ms[1]) then 0 else
if CHoCH then ms[1] else
if close > ph_y and !ph_cross[1] then 1 else
if close < pl_y and !pl_cross[1] then -1 else 0;
pl_cross_ = pl_cross[1];
ph_cross_ = ph_cross[1];
bullLine = bullLine[1];
bearLine = bearLine[1];
btm = btm[1];
top = top[1];
os = os[1];
}
#//Trailing stop
#//Trailing max/min
def max; def min;
if ms == 1 {
max = close;
min = if min[1]==0 then average(close,PivotLookback) else min[1];
} else
if ms == -1 {
max = if max[1]==0 then average(close,PivotLookback) else max[1];
min = close;
} else {
max = max(close, if max[1]==0 then average(close,PivotLookback) else max[1]);
min = min(close, if min[1]==0 then average(close,PivotLookback) else min[1]);
}
#//Trailing stop
def ts = if ms == 1 then btm else
if ms == -1 then top else
if os == 1 then if(ts[1]==0, close, ts[1]) + (max - max[1]) * IncrementFactor / 100 else
if(ts[1]==0, close, ts[1]) + (min - min[1]) * IncrementFactor / 100;
#//Plots
def css = if ms then 0 else
if os == 1 then 1 else -1;
def plot_price = ohlc4;
plot plot_ts = if css==0 then na else ts; # 'Trailing Stop'
plot_ts.AssignValueColor(if css>0 then GlobalColor("bull") else GlobalColor("bear"));
plot CHoCHup = if bullLine==0 or last or os==-1 then na else bullLine;
plot CHoCHdn = if bearLine==0 or last or os== 1 then na else bearLine;
CHoCHup.SetPaintingStrategy(PaintingStrategy.DASHES);
CHoCHdn.SetPaintingStrategy(PaintingStrategy.DASHES);
CHoCHup.SetDefaultColor(Color.CYAN);
CHoCHdn.SetDefaultColor(Color.MAGENTA);
plot CHoCHup_ = if bullLine==0 or last or os!=-1 then na else bullLine;
plot CHoCHdn_ = if bearLine==0 or last or os!= 1 then na else bearLine;
CHoCHup_.SetStyle(Curve.LONG_DASH);
CHoCHdn_.SetStyle(Curve.LONG_DASH);
CHoCHup_.SetDefaultColor(Color.CYAN);
CHoCHdn_.SetDefaultColor(Color.MAGENTA);
def css_area = if (close - ts) * os < 0 then 3 else css;
AddCloud(if css_area==1 then plot_price else na,plot_ts, GlobalColor("dbull"));
AddCloud(if css_area==-1 then plot_ts else na,plot_price, GlobalColor("dbear"));
AddCloud(if css_area==3 then plot_ts else na,plot_price, GlobalColor("ret"), GlobalColor("ret"));
#--- END of CODE