ATR Parabolic SAR (PSAR ) Strategy [QuantNomad] for ThinkOrSwim

samer800

Moderator - Expert
VIP
Lifetime
8bESDcD.png


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
 

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
463 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