Heikin-Ashi PSAR Strategy
CODE:
CSS:
#https://www.tradingview.com/v/OBOvLOxW/
#study("QuantNomad - Heikin-Ashi PSAR Alerts", shorttitle = "HA-PSAR[QN]", overlay = false)
# Converted and mod by Sam4Cok@Samer800 - 05/2023
declare lower;
input BarColors = yes;
input start = 0.02;#, title = "PSAR Start")
input increment = 0.02;#, title = "PSAR Increment")
input maximum = 0.2;#, title = "PSAR Max")
def na = Double.NaN;
#// Calculation HA Values
#def haOpen;# = 0.0
def haclose = (open + high + low + close) / 4;
def haOpen = if (IsNaN(haOpen[1]) or haOpen[1] == 0) then (open + close) / 2 else (haOpen[1] + haclose[1]) / 2;
def hahigh = Max(high, Max(haOpen, haclose));
def halow = Min(low, Min(haOpen, haclose));
#// HA colors
def up = haclose > haOpen;
def upOp; def dnOp;
if up {
upOp = haOpen;
} else {
upOp = na;
}
if !up {
dnOp = haOpen;
} else {
dnOp = na;
}
def psar; # // PSAR
def af; # // Acceleration Factor
def trend_dir; # // Current direction of PSAR
def ep; # // Extreme point
def trend_bars;
def sar_long_to_short = trend_dir[1] == 1 and haclose <= psar[1]; #// PSAR switches from long to short
def sar_short_to_long = trend_dir[1] == -1 and haclose >= psar[1]; #// PSAR switches from short to long
def trend_change = IsNaN(psar[2]) or sar_long_to_short or sar_short_to_long;
##// Calculate trend direction
trend_dir = if (IsNaN(psar[2]) or psar[2] == 0) and haclose[1] > haOpen[1] then 1 else
if (IsNaN(psar[2]) or psar[2] == 0) and haclose[1] <= haOpen[1] then -1 else
if sar_long_to_short then -1 else
if sar_short_to_long then 1 else 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 trend_bars[1] + 1 else
if trend_dir == -1 then trend_bars[1] - 1 else trend_bars[1];
#// Calculate Acceleration Factor
af = if trend_change then start else
if (trend_dir == 1 and hahigh > 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 hahigh else
if trend_change and trend_dir == -1 then halow else
if trend_dir == 1 then Max(ep[1], hahigh) else Min(ep[1], halow);
#// Calculate PSAR
psar = if (IsNaN(psar[2]) or psar[2] == 0) and haclose[1] > haOpen[1] then halow[1] else
if (IsNaN(psar[2]) or psar[2] == 0) and haclose[1] <= haOpen[1] then hahigh[1] else
if trend_change then ep[1] else
if trend_dir == 1 then psar[1] + af * (ep - psar[1]) else psar[1] - af * (psar[1] - ep);
# Plot the new Chart
AddChart(high = if up then hahigh else na, low = halow , open = haclose, close = upOp,
type = ChartType.CANDLE, growcolor = CreateColor(38, 166, 154));
AddChart(high = if up then na else hahigh , low = halow , open = dnOp, close = haclose,
type = ChartType.CANDLE, growcolor = CreateColor(239, 83, 80));
plot parSar = psar;
parSar.AssignValueColor(if trend_dir == 1 then Color.GREEN else Color.RED);
parSar.SetPaintingStrategy(PaintingStrategy.POINTS);
AddChartBubble(sar_short_to_long, halow, "Long", Color.GREEN, no);
AddChartBubble(sar_long_to_short, hahigh, "Short", Color.RED, yes);
AssignPriceColor(if !BarColors then Color.CURRENT else
if trend_dir>0 then Color.GREEN else
if trend_dir<0 then Color.RED else Color.GRAY);
#--- END of CODE