Parabolic SAR (PSAR) Scan, Watchlist, Labels, Charts For ThinkOrSwim

I want to add a column for the parabolic SAR in my TOS watchlist.
That column should show GREEN color (backgound in the cell) when the Parabolic SAR is below the price (bullish) and a RED color when the Parabolic SAR is above the price (bearish).
I tried to write the code (see below) but it gives me syntax error. Hope someone can help me fix this. Thanks

def price = close;
def PSAR = parabolicSAR();
plot rjPSAR = if price < PSAR() color.RED
else if price is > PSAR color.CYAN
else color.WHITE;
This should help
Ruby:
def price = close;
def PSAR  = ParabolicSAR();
AddLabel(1, "PSAR", Color.WHITE);
AssignBackgroundColor( if price < PSAR then Color.RED else if price > PSAR then Color.CYAN
else Color.WHITE);
 
I am hoping that someone could help me to create a label that would list the price level of PSAR for the previous candle?
 
Here is the scan for a PSAR for EITHER a transition from bullish to bearish OR from bearish to bullish. I have listed two plot statements. Please comment out the one you don't want as the scanner only accepts a single plot statement

Code:
# PSAR Scan
# tomsk
# 11.20.2019

# Scans for a PSAR state transition from bullish to bearish or from bearish to bullish

#
# TD Ameritrade IP Company, Inc. (c) 2008-2019
#

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;

switch (state[1]) {
case init:
    state = state.long;
    acc = accelerationFactor;
    extreme = high;
    SAR = low;
case short:
    if (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 (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]));
    }
}

def transitionBull = state[1] == state.short and state == state.long;
def transitionBear = state[1] == state.long and state == state.short;

# Comment out (#) the ONE not needed
plot scan = transitionBull;
#plot scan = transitionBear;
Not having much luck. TOS compresses everything into a single horizonal line on the 15 min chart. Any ideas?
 
Here is the scan for a PSAR for EITHER a transition from bullish to bearish OR from bearish to bullish. I have listed two plot statements. Please comment out the one you don't want as the scanner only accepts a single plot statement

Code:
# PSAR Scan
# tomsk
# 11.20.2019

# Scans for a PSAR state transition from bullish to bearish or from bearish to bullish

#
# TD Ameritrade IP Company, Inc. (c) 2008-2019
#

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;

switch (state[1]) {
case init:
    state = state.long;
    acc = accelerationFactor;
    extreme = high;
    SAR = low;
case short:
    if (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 (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]));
    }
}

def transitionBull = state[1] == state.short and state == state.long;
def transitionBear = state[1] == state.long and state == state.short;

# Comment out (#) the ONE not needed
plot scan = transitionBull;
#plot scan = transitionBear;
Is there a way to make the PSAR on the chart as a study and have it ring a bell when the PSAR switches from bull to bear and visa versa? Would rather do it on the chart as opposed to a scan. Thank you in advance.
 
Is there a way to make the PSAR on the chart as a study and have it ring a bell when the PSAR switches from bull to bear and visa versa? Would rather do it on the chart as opposed to a scan. Thank you in advance.
Added alert and bubble options to TOS PSAR code

Capture.jpg
Ruby:
#
# TD Ameritrade IP Company, Inc. (c) 2008-2021
#

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;

switch (state[1]) {
case init:
    state = state.long;
    acc = accelerationFactor;
    extreme = high;
    SAR = low;
case short:
    if (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 (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 parSAR = SAR;
parSAR.SetPaintingStrategy(PaintingStrategy.POINTS);
parSAR.SetDefaultColor(GetColor(5));

input alerts = yes;
Alert(alerts and state[1] == state.long and state == state.short, "BEAR", Alert.ONCE, Sound.Bell);
Alert(alerts and state == state.long and state[1] == state.short, "BULL", Alert.ONCE, Sound.Bell);

input bubbles = yes;
AddChartBubble(bubbles and state[1] == state.long and state == state.short, parSAR, "BEAR", Color.RED);
AddChartBubble(bubbles and state == state.long and state[1] == state.short, parSAR, "BULL", Color.LIGHT_GREEN, no);
 
Is there any way to change the PSAR code so that only the most recent bullish and bearish signal plots with no historical bullish and bearish plots? Thanks in advance!
 
Last edited by a moderator:
Is there any way to change the PSAR code so that only the most recent bullish and bearish signal plots with no historical bullish and bearish plots? Thanks in advance!
Here is the code I posted above with the addition of where at count == 2, it shows the last 2 of bull or bear parsars and bubbles. Adjust the count to how many you want displayed. Added is a label option to show the last bull or bear.

Capture.jpg
Ruby:
#
# TD Ameritrade IP Company, Inc. (c) 2008-2021
#

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;

switch (state[1]) {
case init:
    state = state.long;
    acc = accelerationFactor;
    extreme = high;
    SAR = low;
case short:
    if (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 (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]));
    }
}
input Count = 2;
def bear =  state[1] == state.long and state == state.short;
def bull =  state == state.long and state[1] == state.short;
def cond = bull or bear;
def dataCount = CompoundValue(1, if (cond) then dataCount[1] + 1 else dataCount[1], 0);
plot parSAR =  if HighestAll(dataCount) - dataCount <= Count - 1 then SAR else Double.NaN;
parSAR.SetPaintingStrategy(PaintingStrategy.POINTS);
parSAR.SetDefaultColor(GetColor(5));

input alerts = yes;
Alert(alerts and bear, "BEAR", Alert.ONCE, Sound.Bell);
Alert(alerts and bull, "BULL", Alert.ONCE, Sound.Bell);

input label = yes;
def lastbullbear = if bull then 1 else if bear then -1 else lastbullbear[1];
AddLabel(label and lastbullbear == 1, "Bull" , Color.GREEN);
AddLabel(label and lastbullbear == -1, "Bear" , Color.RED);

input bubbles = yes;
AddChartBubble(bubbles and bear, parSAR, "BEAR", Color.RED);
AddChartBubble(bubbles and bull, parSAR, "BULL", Color.LIGHT_GREEN, no);
 
Last edited:
Has anyone written a parabolic mirror thinkscript. I am running into a wall trying to modify a parabolicSar. Any help would be greatly appreciated. I am not well versed in writing, but (I thought) I was capable of modifying, but no ... not so much.
 
Is there a way to code the PSAR function so it is based on the linear regression lines instead of price?
 
this may, and may not work at all.
Since the PSAR requires two lines, I chose to create two Linear Regression Curves, one on the HIGHs, and one on the LOWs and use those in place of HIGH and LOW price.
The Linear Regression length is an input so you can easily adjust it.

Code:
#
# TD Ameritrade IP Company, Inc. (c) 2008-2021
#
# Modified to use Linear Regression Curves for calculations by Mashume @ usethinkscript 2022-02

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;
input LinearRegressionLength = 9;
def LRLow =  Inertia(LOW, LinearRegressionLength);
def LRHigh = Inertia(HIGH, LinearRegressionLength);

switch (state[1]) {
case init:
    state = state.long;
    acc = accelerationFactor;
    extreme = LRHigh;
    SAR = LRLow;
case short:
    if (SAR[1] < LRHigh)
    then {
        state = state.long;
        acc = accelerationFactor;
        extreme = LRHigh;
        SAR = extreme[1];
    } else {
        state = state.short;
        if (LRLow < extreme[1])
        then {
            acc = min(acc[1] + accelerationFactor, accelerationLimit);
            extreme = LRLow;
        } else {
            acc = acc[1];
            extreme = extreme[1];
        }
        SAR = max(max(LRHigh, LRHigh[1]), SAR[1] + acc * (extreme - SAR[1]));
    }
case long:
    if (SAR[1] > LRLow)
    then {
        state = state.short;
        acc = accelerationFactor;
        extreme = LRLow;
        SAR = extreme[1];
    } else {
        state = state.long;
        if (LRHigh > extreme[1])
        then {
            acc = min(acc[1] + accelerationFactor, accelerationLimit);
            extreme = LRHigh;
        } else {
            acc = acc[1];
            extreme = extreme[1];
        }
        SAR = min(min(LRLow, LRLow[1]), SAR[1] + acc * (extreme - SAR[1]));
    }
}

plot parSAR = SAR;
parSAR.SetPaintingStrategy(PaintingStrategy.POINTS);
parSAR.SetDefaultColor(GetColor(5));

Happy Trading,
mashume
 
mashume - thank you for this.
Out of curiosity, what length Linear Regression are you using? What product? timeframe? Just curious to know how you'll use the indicator. It seems pretty cool to me with a length of 25 on /CL at 2 and 5 minutes
 
this may, and may not work at all.
Since the PSAR requires two lines, I chose to create two Linear Regression Curves, one on the HIGHs, and one on the LOWs and use those in place of HIGH and LOW price.
The Linear Regression length is an input so you can easily adjust it.

Code:
#
# TD Ameritrade IP Company, Inc. (c) 2008-2021
#
# Modified to use Linear Regression Curves for calculations by Mashume @ usethinkscript 2022-02

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;
input LinearRegressionLength = 9;
def LRLow =  Inertia(LOW, LinearRegressionLength);
def LRHigh = Inertia(HIGH, LinearRegressionLength);

switch (state[1]) {
case init:
    state = state.long;
    acc = accelerationFactor;
    extreme = LRHigh;
    SAR = LRLow;
case short:
    if (SAR[1] < LRHigh)
    then {
        state = state.long;
        acc = accelerationFactor;
        extreme = LRHigh;
        SAR = extreme[1];
    } else {
        state = state.short;
        if (LRLow < extreme[1])
        then {
            acc = min(acc[1] + accelerationFactor, accelerationLimit);
            extreme = LRLow;
        } else {
            acc = acc[1];
            extreme = extreme[1];
        }
        SAR = max(max(LRHigh, LRHigh[1]), SAR[1] + acc * (extreme - SAR[1]));
    }
case long:
    if (SAR[1] > LRLow)
    then {
        state = state.short;
        acc = accelerationFactor;
        extreme = LRLow;
        SAR = extreme[1];
    } else {
        state = state.long;
        if (LRHigh > extreme[1])
        then {
            acc = min(acc[1] + accelerationFactor, accelerationLimit);
            extreme = LRHigh;
        } else {
            acc = acc[1];
            extreme = extreme[1];
        }
        SAR = min(min(LRLow, LRLow[1]), SAR[1] + acc * (extreme - SAR[1]));
    }
}

plot parSAR = SAR;
parSAR.SetPaintingStrategy(PaintingStrategy.POINTS);
parSAR.SetDefaultColor(GetColor(5));

Happy Trading,
mashume
Can we choose two different colors for up and down since there is not an option to select two different colors in this code?
 
@petergluis
Replace the last line of the code above (SetDefaultColor) with these three lines:
Code:
DefineGlobalColor ("long", Color.Green);
DefineGlobalColor ("short", Color.Red);
parSAR.AssignValueColor(if state == state.short then GlobalColor("short") else GlobalColor("long"));
You should then, if this works as I think it should, be able to define colours in the Globals section of the indicator settings modal window.

-mashume
 
Out of curiosity, what length Linear Regression are you using? What product? timeframe? Just curious to know how you'll use the indicator. It seems pretty cool to me with a length of 25 on /CL at 2 and 5 minutes
I'm using a length of 10. I'm using to generate a BUY signal after the Linear Regression 10 has hit a low and is on a rebound.
 
I would like a label that shows 1/5/15 minute PSAR values in a label. ? like the MTF Squeezes.
Yes, you can create the code with different aggregations/mtf. It is a boring, redundant process which will result in repainting issues.
So it is unlikely a member will provide this for you.
But if you want to take it on yourself, it is easy enough even for those with no coding experience.
This easy MTF tutorial was posted as an option for non-programmers interested in creating their own MTF indicators.
 
Hi, I'm a newbie to coding, can someone pls add label top left corner for this script. Thank You

#
# TD Ameritrade IP Company, Inc. (c) 2009-2022
#

#wizard input: crossingType
#wizard text: Inputs: acceleration factor:
#wizard input: accelerationFactor
#wizard text: acceleration limit:
#wizard input: accelerationLimit

input accelerationFactor = 0.02;
input accelerationLimit = 0.2;
input crossingType = {default Bearish, Bullish};

def sar = ParabolicSAR(accelerationFactor=accelerationFactor, accelerationLimit=accelerationLimit);

plot signal = crosses(sar, close, CrossingType == CrossingType.Bearish);

signal.DefineColor("Bullish", GetColor(5));
signal.DefineColor("Bearish", GetColor(6));
signal.AssignValueColor(if crossingType == CrossingType.Bullish then signal.color("Bullish") else signal.color("Bearish"));

signal.SetPaintingStrategy(if crossingType == CrossingType.bullish
then PaintingStrategy.BOOLEAN_ARROW_UP
else PaintingStrategy.BOOLEAN_ARROW_DOWN);
 

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

Thread starter Similar threads Forum Replies Date
F Repaints Multi-Time Frame MTF PSAR for ThinkorSwim Indicators 60
T PSAR Transition Indicator for ThinkorSwim Indicators 30

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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