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

#### GetRichOrDieTrying

##### Member
Is it possible to create an alert when Parabolic SAR reverses in Thinkorswim? Ideally, I would like to get an alert on desktop and mobile with sound and a push notification through the app or email. I tried using the Study Alert feature under the MarketWatch -> Alerts tab, but Parabolic SAR isn't a Study that appears to be supported. Is there a custom way to do this with Thinkscript?

#### tomsk

##### Well-known member
VIP
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;``````

#### stocksniper

##### Member
Hey guys is there a way to only show the current trend sar and the remove the past history, i like to keep my charts clean and showing the past history sar on the chart just becomes so clutter. Again i just want to show the current state of the sar wether is bullish or bearish. Thanks in advance.

#### tomsk

##### Well-known member
VIP
Hey guys is there a way to only show the current trend sar and the remove the past history, i like to keep my charts clean and showing the past history sar on the chart just becomes so clutter. Again i just want to show the current state of the sar wether is bullish or bearish. Thanks in advance.

@stocksniper Here we go, here is a PSAR State Label that displays a label indicating whether PSAR current state is bullish or bearish. This would enable your charts to be real clean!

Code:
``````# PSAR State Label
# tomsk
# 1.16.2020

# V1.0 - 01.16.2020 - tomsk - Initial release of PSAR State Label

input accelerationFactor = 0.02;
input accelerationLimit = 0.2;
input offSet = 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]));
}
}

AddLabel(state == state.long, "PSAR: Bullish", Color.Yellow);
AddLabel(state == state.short, "PSAR: Bearish", Color.Pink);

# End PSAR State Label``````

#### stocksniper

##### Member
Hi, first want to thank you for taking the time to answer, but what I really want is to see the dots for the current state only not the past history dots.

#### tomsk

##### Well-known member
VIP
Unfortunately once painted, I don't see a way of erasing previously painted PSAR dots. ThinkScript is essentially a bar painting program sequentially. Someone else might have an alternate approach

#### RobertPayne

##### Member
Here you go.

Ruby:
``````# +------------------------------------------------------------+
# |          PSAR modified to only show current PSAR           |
# |                        Robert Payne                        |
# |               https://funwiththinkscript.com               |
# +------------------------------------------------------------+

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 newState = HighestAll(if state <> state[1] then BarNumber() else 0);
plot parSAR =  if BarNumber() < newState then Double.NaN else SAR;
parSAR.SetPaintingStrategy(PaintingStrategy.POINTS);
parSAR.SetDefaultColor(GetColor(5));``````

#### tomsk

##### Well-known member
VIP
Ah! A very slick approach using HighestAll(), barNumber() and state transitions - I like it! Adding that to the toolkit. There are many other applications that can benefit from using that approach me thinks

UPDATE: Thinking why wait - I'm taking immediate action and applying these ideas to one of the more popular studies. Have a look through the following link

Last edited:

#### markos

##### Well-known member
VIP
@RobertPayne welcome back, I haven't seen you in a while.
Markos

#### RobertPayne

##### Member
@RobertPayne welcome back, I haven't seen you in a while.
Markos
Thanks. Holidays, work, life in general...things get in the way. I stop by from time to time to see what's going on.

#### RmS59

##### Member
you could also use:

plot parSAR = !IsNaN(close[-1]) then Sar else Double.NaN;

#### HighBredCloud

##### Well-known member
I found this cool PSAR by Mobius...I would like to have the traditional POINTS display above and below the candles instead of coloring the candles themselves so that I can use with other SuperTrends...What needs to be changed in the code below to achieve that? I tried and no avail...
Code:
``````# Parabolic SAR modified with fractal dimension index
# Mobius
# V01.01.2010 V02.09.2013 V03.07.2014  V04.2014 Altered calculations for Fractal dimensions
#hint:<b>Parabolic Stop and Reverse:</b>\n This version is adjusted using a fractal dimension index.\n When price is trending the index will move lower compressing the PSAR's scaling factor and limit factor so that trend reversals are picked up quicker and when the market becomes more balanced and trading the scaling factor and limit factor will widen to lesson whipsaw.\n This same process could be accomplished with ADX or Implied Volatility.

# User Inputs
input n = 8;
input AscaleLow = .01;  #hint AscaleLow: Typical range from .01 to .03
input AscaleHigh = .0618;#hint AscaleHigh: Typical range from .03 to .09
input BscaleLow = .1;   #hint BscaleLow: Typical range from .1 to .3
input BscaleHigh = .99; #hint BscaleHigh: Typical range from .3 to .9
input Labels = yes;

# Variables
def o;
def h;
def l;
def c;
def TR;
#def A;
def B;
def Ascaled;
def Bscaled;

# Internal Script
script Scale {
input c = close;
input Min = .01;
input Max =   1;
def hh = HighestAll(c);
def ll   = LowestAll(c);
plot Range = (((Max - Min) * (c - ll)) /  (hh - ll)) + Min;
}

# Calculations
o = (open[1] + close[1]) / 2;
h = Max(high, close[1]);
l = Min(low, close[1]);
c = (o + h + l + close) / 4;
TR = Max(h, c[1]) - Min(l, c[1]);
# A = Log(Sum(TR, n)) / (Highest(c, n) - Lowest(c, n)) / Log(n);
# Ascaled = Round(Scale(c = A, min = AscaleLow, max = AscaleHigh), 3);
B = (Log(Sum(TR, n) / (Highest(h, n) - Lowest(l, n)))
/ Log(10)) / (Log(n) / Log(10));
Ascaled = Round(Scale(c = B, min = AscaleLow, max = AscaleHigh), 3);
Bscaled = Round(Scale(c = B, min = BscaleLow, max = BscaleHigh), 2);

AddLabel(Labels, "A Factor Scaled: " + Ascaled, Color.WHITE);
AddLabel(Labels, "B Factor Scaled: " + Bscaled, Color.WHITE);
AddLabel(Labels, if Bscaled > Bscaled[1] and Bscaled > .5
else if Bscaled < Bscaled[1] and Bscaled < .618
then "Trending"
else if Bscaled > .618
else if Bscaled < .382
then "Trending"
else "",
if Bscaled < Bscaled[1] and Bscaled < .618
then Color.Green
else if Bscaled < .382
then Color.Green
else if Bscaled > .382 and Bscaled > Bscaled[1]
then Color.Red
else if Bscaled > .618
then Color.Red
else Color.Yellow);

def state = {default init, long, short};
def extreme;
def SAR;
def acc;
switch (state[1]) {
case init:
state = state.long;
acc = Ascaled;
extreme = h;
SAR = l;
case short:
if (SAR[1] < h)
then {
state = state.long;
acc = Ascaled;
extreme = h;
SAR = extreme[1];
} else {
state = state.short;
if (l < extreme[1])
then {
acc = Min(acc[1] + Ascaled, Bscaled);
extreme = l;
} else {
acc = acc[1];
extreme = extreme[1];
}
SAR = Max(Max(h, h[1]), SAR[1] + acc * (extreme - SAR[1]));
}
case long:
if (SAR[1] > l)
then {
state = state.short;
acc = Ascaled;
extreme = l;
SAR = extreme[1];
} else {
state = state.long;
if (h > extreme[1])
then {
acc = Min(acc[1] + Ascaled, Bscaled);
extreme = h;
} else {
acc = acc[1];
extreme = extreme[1];
}
SAR = Min(Min(l, l[1]), SAR[1] + acc * (extreme - SAR[1]));
}
}

# Chart Management
AssignPriceColor(if SAR > h then Color.RED else Color.GREEN);

Last edited by a moderator:

#### horserider

##### Well-known member
VIP
Just plot it like ToS psar. My guess it is very very close.

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

##### Active member
This plots the points above/below the candles in the right colors, but you will get a dot still in wrong position. BUT what is important is the color without having to paint the candles right?! That's my best attempt
Code:
``````input offset = 0;
plot parSAR = SAR[offset];
parSAR.SetPaintingStrategy(PaintingStrategy.POINTS);
parSAR.AssignValueColor(if SAR > h then Color.RED else Color.GREEN);``````

#### jcga1981

##### Member
Hello, I downloaded this parabolic sar study that I quite like, but I would love to add the possibility of eliminating the points that are not from the day that I am analyzing, and on the other hand, I would love that when the points are above the price they are painted red and when they are below the price they are painted green. Thank you

Code:
``````#HINT: This is a copy/paste/tweak of the traditional PSAR study that includes the addition of audible and visual alerts when the dots flip + an always on chart label denoting whether the PSAR dots are above or below price.

# from input all the way down to plot is merely a copy/paste of the traditional PSAR study.  Below that is the additional lines added to the code to create audible and visual alerts + chart label.

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(color.BLUE);

#code below this line creates a visible alert (chart label) and audible alert on the first bar the PSAR dots flip (price crosses PSAR)

# because of the copy/paste of tradtional PSAR study above this it is easy to use that to create the below def and plot with simple lines of code.

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

plot BullishSignal = Crosses(PSAR, close, CrossingDirection.BELOW);
BullishSignal.SetLineWeight(5);
BullishSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
BullishSignal.AssignValueColor(CreateColor(51, 204, 0));

# in Alert you select what text will appear in the messages window when your alert trigers by placing those words between quotation marks.  Alert(condition or trigger, words to appear, how often to sound the alert, what sound to hear)
AddLabel(close > PSAR, "     Bullish PSAR     ", CreateColor(51,204,0));

# Because BullishSignal will be true ONLY for the bar where the PSAR dots flip, this label is a visual alert that will ONLY appear at the time the PSAR dots flip
AddLabel(if BullishSignal == 1 then yes else no, "     PSAR just flipped to Bullish     ", (CreateColor(51,204,0)));

plot BearishSignal = Crosses(PSAR, close, CrossingDirection.ABOVE);
BearishSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
BearishSignal.AssignValueColor(Color.RED);
BearishSignal.SetLineWeight(5);

# Because BeaarishSignal will be true ONLY for the bar where the PSAR dots flip, this label is a visual alert that will ONLY appear at the time the PSAR dots flip
AddLabel(close < PSAR, "     Bearish PSAR     ", color.RED);
AddLabel(if BearishSignal == 1 then yes else no, "     PSAR just flipped to Bearish     ", color.RED);``````

2019 Donor
VIP

#### stocksniper

##### Member

Lookingto see if anyone would be able to do this script color change when dots are on top red and when dots are at the bottom green.

#### BenTen

Staff
VIP
Here you go:

Code:
``````# ParabolicSAR_withAlerts_JQ
# 2018-04-15 Mods by Johnny Quotron
#    with a very helpful kickstart from DMonkey
# Mods include
#    1. splitting the PSAR into two visible plots so that they can be colored seperately
#        a. original alert arrows remain available but are hidden by default
#

# Original code by ToS

#HINT: This is a copy/paste/tweak of the traditional PSAR study that includes the addition of audible and visual alerts when the dots flip + an always on chart label denoting whether the PSAR dots are above or below price.

# from input all the way down to plot is merely a copy/paste of the traditional PSAR study.  Below that is the additional lines added to the code to create audible and visual alerts + chart label.

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 parSAR = SAR;
#parSAR.SetPaintingStrategy(PaintingStrategy.POINTS); #original code
#parSAR.SetDefaultColor(color.BLUE);        #original code
#parSAR.hide();    #original code

#parSAR.AssignValueColor(if parSAR > close then getcolor(bearish_PSAR_Dot_Color) else #getcolor(bullish_PSAR_Dot_Color));      #original code

plot BullPSAR = if SAR < close then SAR else double.NaN;
BullPSAR.SetPaintingStrategy(PaintingStrategy.POINTS);
BullPSAR.SetDefaultColor(color.lime);

plot BearPSAR = if SAR > close then SAR else double.NaN;
BearPSAR.SetPaintingStrategy(PaintingStrategy.POINTS);
BearPSAR.SetDefaultColor(color.pink);

#code below this line creates a visible alert (chart label) and audible alert on the first bar the PSAR dots flip (price crosses PSAR)

# because of the copy/paste of tradtional PSAR study above this it is easy to use that to create the below def and plot with simple lines of code.

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

plot BullSignalAtCandle = Crosses(PSAR, close, CrossingDirection.BELOW);
BullSignalAtCandle.SetLineWeight(5);
BullSignalAtCandle.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
BullSignalAtCandle.setdefaultcolor(color.lime);
BullSignalAtCandle.Hide();

#14:44 DMonkey assistance: plot BullishSignal = if close crosses above psar
#                     then psar
#                     else double.nan;
#BullishSignal.SetLineWeight(5);
#BullishSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
#BullishSignal.AssignValueColor(CreateColor(51, 204, 0));
#you can reverse the logic for the down arrow....

plot BullSignalAtPSAR = if close crosses above psar
then psar
else double.nan;
BullSignalAtPSAR.SetLineWeight(5);
BullSignalAtPSAR.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
BullSignalAtPSAR.setdefaultcolor(color.lime);

# in Alert you select what text will appear in the messages window when your alert trigers by placing those words between quotation marks.  Alert(condition or trigger, words to appear, how often to sound the alert, what sound to hear)
AddLabel(close > PSAR, "     Bullish PSAR     ", BullPSAR.takevalueColor());

# Because BullishSignal will be true ONLY for the bar where the PSAR dots flip, this label is a visual alert that will ONLY appear at the time the PSAR dots flip
AddLabel(if BullSignalAtCandle == 1 then yes else no, "     PSAR just flipped to Bullish     ", BearPSAR.takevalueColor());

plot BearSignalAtCandle = Crosses(PSAR, close, CrossingDirection.ABOVE);
BearSignalAtCandle.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
BearSignalAtCandle.setDefaultColor(color.pink);
BearSignalAtCandle.SetLineWeight(5);
BearSignalAtCandle.Hide();

plot BearSignalAtPSAR = if close crosses below psar
then psar
else double.nan;
BearSignalAtPSAR.SetLineWeight(5);
BearSignalAtPSAR.SetPaintingStrategy(PaintingStrategy.ARROW_Down);
BearSignalAtPSAR.setDefaultColor(color.pink);

# Because BeaarishSignal will be true ONLY for the bar where the PSAR dots flip, this label is a visual alert that will ONLY appear at the time the PSAR dots flip
AddLabel(close < PSAR, "     Bearish PSAR     ", BearPSAR.takevalueColor());
AddLabel(if BearSignalAtCandle == 1 then yes else no, "     PSAR just flipped to Bearish     ", BearPSAR.takevalueColor());``````

#### Jonas99

##### Active member
VIP
Wow, bro you are awesome!!! thank you so much chart looks so much cleaner now.
@stocksniper is it possible to share that link with the script from Robert? I copied the code posted but it does not show anything. Thank you

## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• 1 full year of unlimited support

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?