Author Message:
created a version of Parabolic SAR when I accelerate it not based on the difference from the extreme point but based on current ATR. So the idea is that for a more volatile market it should move faster.
CODE:
CSS:
#//@version=4
#strategy(title="ATR Parabolic SAR Strategy [QuantNomad]", shorttitle="ATR PSAR Strategy [QN]"
# Converted and mod by Sam4Cok@Samer800 - 01/2023
input ShowLabel = yes;
input ShowLines = yes;
input atr_length = 14;
input start = 0.02;
input increment = 0.02;
input maximum = 0.2;
input entry_bars = 1;#, title = "Entry on Nth trend bar")
def na = Double.NaN;
script nz {
input data = close;
input repl = 0;
def ret_val = if !isNaN(data) then data else repl;
plot return = ret_val;
}
def nATR = ATR(Length = atr_length);
def tr = TrueRange(high, close, low);
def isfirst = BarNumber() == 1;
def ATR = if IsNaN(nATR) then tr else nATR;
def psar;# = 0.0 // PSAR
def af;# = 0.0 // Acceleration Factor
def trend_dir;# = 0 // Current direction of PSAR
def ep;# = 0.0 // Extreme point
def trend_bars;# = 0
def sar_long_to_short = trend_dir[1] == 1 and close <= psar[1]; # PSAR switches from long to short
def sar_short_to_long = trend_dir[1] == -1 and close >= psar[1]; # PSAR switches from short to long
def trend_change = isfirst[1] or sar_long_to_short or sar_short_to_long;
#// Calculate trend direction
trend_dir = if isfirst[1] and close[1] > open[1] then 1 else
if isfirst[1] and close[1] <= open[1] then -1 else
if sar_long_to_short then -1 else
if sar_short_to_long then 1 else nz(trend_dir[1]);
trend_bars = if sar_long_to_short then -1 else
if sar_short_to_long then 1 else
if trend_dir == 1 then nz(trend_bars[1]) + 1 else
if trend_dir == -1 then nz(trend_bars[1]) - 1 else nz(trend_bars[1]);
#// Calculate Acceleration Factor
af = if trend_change then start else
if (trend_dir == 1 and high > ep[1]) or (trend_dir==-1 and low<ep[1]) then Min(maximum, af[1] + increment) else af[1];
#// Calculate extreme point
ep = if trend_change and trend_dir == 1 then high else
if trend_change and trend_dir == -1 then low else
if trend_dir == 1 then Max(ep[1], high) else Min(ep[1], low);
#// Calculate PSAR
psar = if isfirst[1] and close[1] > open[1] then low[1] else
if isfirst[1] and close[1] <= open[1] then high[1] else
if trend_change then ep[1] else
if trend_dir == 1 then psar[1] + af * atr else psar[1] - af * atr;
plot atrPSAR = if psar==0 then na else psar;
atrPSAR.SetStyle(Curve.POINTS);
atrPSAR.AssignValueColor( if trend_dir == 1 then Color.UPTICK else Color.DOWNTICK);
AddChartBubble(ShowLabel and trend_bars==entry_bars, low, "Long", Color.UPTICK, no);
AddChartBubble(ShowLabel and trend_bars==-entry_bars,high, "Short",Color.DOWNTICK ,yes);
def Sup = if trend_bars==entry_bars then high else Sup[1];
def Res = if trend_bars==-entry_bars then low else Res[1];
plot SupLine = if !ShowLines or !Sup then na else sup;
plot ResLine = if !ShowLines or !Res then na else Res;
SupLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ResLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
SupLine.SetDefaultColor(Color.UPTICK);
ResLine.SetDefaultColor(Color.DOWNTICK);
#---- END CODE