QuantNomad - Heikin-Ashi PSAR Alerts for ThinkOrSwim

samer800

Moderator - Expert
VIP
Lifetime
dbFn0N6.png


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
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

dbFn0N6.png


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

Thankyou Samer800
 
Last edited by a moderator:
What is the difference between this (QuantNomad - Heikin-Ashi PSAR)
and ATR Parabolic SAR Strategy [QuantNomad]?
 
What is the difference between this (QuantNomad - Heikin-Ashi PSAR)
and ATR Parabolic SAR Strategy [QuantNomad]?
The easier question to answer is what do they have in common.
1. They were created by the same scripter QuantNomad
2. They both reference the PSAR indicator

Other than that, nothing in common.
The script in this thread is a trending indicator,
the 2nd:
https://usethinkscript.com/threads/atr-parabolic-sar-strategy-quantnomad-for-thinkorswim.14237/
measures market volatility. It can be helpful for traders who want to adjust their trading strategy based on market conditions.
 
Last edited:
I've converted this to a Strategy for backtesting in the code below.

Code:
#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
# Converted from Study to Strategy by thinkorJim - 05/2023

input BarColors  = yes;
input start      = 0.02;#, title = "PSAR Start")
input increment  = 0.02;#, title = "PSAR Increment")
input maximum    = 0.2;#,  title = "PSAR Max")
input orderSize = 1;

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);

# Trade Logic
def long = trend_dir>0;
def short = trend_dir<0;

# Order Logic
AddOrder(OrderType.BUY_AUTO, long, open[-1], orderSize);
AddOrder(OrderType.SELL_AUTO,short, open[-1], orderSize);

#--- END of CODE
 
dbFn0N6.png


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
is there a way this could be changed to a regular candle stick pattern instead heikin ashi
 
is there a way this could be changed to a regular candle stick pattern instead heikin ashi
find below.

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 CandleStyle = {Default "Heikin-Ashi", "Candle"};
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;
def ha = CandleStyle==CandleStyle."Heikin-Ashi";
#// Calculation HA Values

#def haOpen;#  = 0.0
def haclose = if ha then (open + high + low + close) / 4 else close;
def haOpen  = if ha then if (IsNaN(haOpen[1]) or haOpen[1]==0) then (open + close) / 2 else
                    (haOpen[1] + haclose[1]) / 2 else open;
def hahigh  = if ha then Max(high, Max(haOpen, haclose)) else high;
def halow   = if ha then Min(low,  Min(haOpen, haclose)) else low;

#// 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, "L", Color.GREEN, no);
AddChartBubble(sar_long_to_short, hahigh,"S", 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
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
345 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top