Parabolic SAR Structural Levels For ThinkOrSwim

Cwparker23

Active member
Plus
mod note:
This is not just another PSAR.

This is a custom‑rolled Parabolic SAR engine with two meaningful upgrades:
1. It exposes the last confirmed SAR levels as horizontal support/resistance, which classic PSAR does not do.​
2. It gives you a stable midline between those levels, which is actually the most useful part for intraday structure.​
Screenshot 2026-02-07 165901.png

Code:
################################
# Original script by Cwparker23#
################################
#-----------------
#- DISCLAIMER
#-----------------
#- I am not a certified financial advisor. The content of this page/site and tools are for informational purposes only and does not constitute financial or legal advice. Under no circumstances will the author be responsible for errors or use of this tool and site. User assumes all risks.

input accelerationFactor = 0.02;
input accelerationLimit  = 0.2;

assert(accelerationFactor > 0, "'acceleration factor' must be positive: " + accelerationFactor);
assert(accelerationLimit >= accelerationFactor,
       "'acceleration limit' (" + accelerationLimit + ") must be greater than or equal to 'acceleration factor' (" + accelerationFactor + ")");

def state = {default init, long, short};
def extreme;
def SAR;
def acc;

def first = IsNaN(SAR[1]);

switch (state[1]) {

case init:
    state   = state.long;
    acc     = accelerationFactor;
    extreme = high;
    SAR     = low;

case short:
    if (first or SAR[1] < high) then {
        state   = state.long;
        acc     = accelerationFactor;
        extreme = high;
        SAR     = extreme[1];
    } else {
        state = state.short;

        if (low < extreme[1]) then {
            acc     = Min(acc[1] + accelerationFactor, accelerationLimit);
            extreme = low;
        } else {
            acc     = acc[1];
            extreme = extreme[1];
        }

        SAR = Max(Max(high, high[1]), SAR[1] + acc * (extreme - SAR[1]));
    }

case long:
    if (first or SAR[1] > low) then {
        state   = state.short;
        acc     = accelerationFactor;
        extreme = low;
        SAR     = extreme[1];
    } else {
        state = state.long;

        if (high > extreme[1]) then {
            acc     = Min(acc[1] + accelerationFactor, accelerationLimit);
            extreme = high;
        } else {
            acc     = acc[1];
            extreme = extreme[1];
        }

        SAR = Min(Min(low, low[1]), SAR[1] + acc * (extreme - SAR[1]));
    }
}

plot Psar = sar;
Psar.SetPaintingStrategy(PaintingStrategy.points);
Psar.DefineColor("Bearish", Color.red);
Psar.DefineColor("Bullish", Color.green);
Psar.AssignValueColor(if Psar > close then Psar.color("Bearish") else Psar.color("Bullish"));

# Detect flips
def flippedToLong  = state == state.long  and state[1] == state.short;
def flippedToShort = state == state.short and state[1] == state.long;

# Store only the most recent completed levels
def lastBullSAR = CompoundValue(1,
    if flippedToLong then SAR else lastBullSAR[1],
    Double.NaN);

def lastBearSAR = CompoundValue(1,
    if flippedToShort then SAR else lastBearSAR[1],
    Double.NaN);

plot SAR_Support = lastBullSAR;
plot SAR_Resistance = lastBearSAR;
plot Mid = (SAR_Support + SAR_Resistance)/2;
mid.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
mid.SetDefaultColor(Color.white);
mid.SetLineWeight(2);

SAR_Support.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
SAR_Resistance.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

SAR_Support.SetDefaultColor(Color.GREEN);
SAR_Resistance.SetDefaultColor(Color.RED);

SAR_Support.SetLineWeight(2);
SAR_Resistance.SetLineWeight(2);
 
Last edited by a moderator:

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
1197 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