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

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

https://usethinkscript.com/threads/supertrend-cci-atr-trend-for-thinkorswim.1090/#post-14027
 
Last edited:
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)
Alert(BullishSignal, "Bullish PSAR", Alert.BAR, Sound.Ring);
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
Alert(BearishSignal, "Bearish PSAR", Alert.BAR, Sound.Ring);
AddLabel(close < PSAR, "     Bearish PSAR     ", color.RED);
AddLabel(if BearishSignal == 1 then yes else no, "     PSAR just flipped to Bearish     ", color.RED);
 
MddD4giy_mid.png



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.
 
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
#    2. adding alert arrows at the PSAR to enhance visibility
#        a. original alert arrows remain available but are hidden by default
#    3. add ability to color color alert arrows
#

# 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)
Alert(BullSignalAtCandle, "Bullish PSAR", Alert.BAR, Sound.Ring);
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
Alert(BearSignalAtCandle, "Bearish PSAR", Alert.BAR, Sound.Ring);
AddLabel(close < PSAR, "     Bearish PSAR     ", BearPSAR.takevalueColor());
AddLabel(if BearSignalAtCandle == 1 then yes else no, "     PSAR just flipped to Bearish     ", BearPSAR.takevalueColor());
 
I guess I have two questions. First, can anyone help me understand how the starting PSAR dot is calculated? As a visual observation, it seems like the starting PSAR point is a high or low of the last n bars but when I look at the PSAR code I don't seem to see anything that resembles that observation. Second, if I want to shift just the starting PSAR dot up or down, how would I do that? Would that only be possible to code if I shift every dot? I have tried to code this by adding to the high and low within the code, but have had no luck so far, it just ends up to be super messy to the point where it is not even worth it to include here.

I am trying to code a PSAR that will leave a little more room for volatility, hence shifting the starting dot a set amount. I hope that by having a starting point that is higher or lower than a recent high or low that it will allow price to go past a high or low without triggering a reverse signal.

Thanks!

Code:
#
# TD Ameritrade IP Company, Inc. (c) 2008-2020
#

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));
 
If I understand your intent, this is fairly simple. The starting value for every PSAR 'run' is held in variable extreme. Here, I've created an input 'extremeoffset', and wherever extreme is assigned, this offset value is added or subtracted, depending on whether it's an extreme high or extreme low:

Code:
#
# TD Ameritrade IP Company, Inc. (c) 2008-2020
#

input accelerationFactor = 0.02;
input accelerationLimit = 0.2;
input extremeoffset = 0;

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 - extremeoffset;
        SAR = extreme[1];
    } else {
        state = state.long;
        if (high > extreme[1])
        then {
            acc = min(acc[1] + accelerationFactor, accelerationLimit);
            extreme = high + extremeoffset;
        } 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));

This could be easily modified to apply a percent offset rather than a fixed number, if desired.
 
I really like using Parabolic SAR in my trading, but would love to edit the basic code with an Input to limit the number of dots shown on the chart at any given time.

How could I edit the code to:

1. Accept a user input
2. Use that input to limit the number of dots on chart and "hide" the rest

So I could, in theory, only display the last 5 dots of the P-SAR plot.

Possible?
 
need to add a variable to the plot logic , with AND , that determines when to display the dots.

here is one way, to have a variable be true, only for the recent x number of bars.
this looks at future prices to see if they are an error and check that the current price is not an error

Ruby:
# showrecentxbars
input show_x_recent_bars = 10;
def show = isnan(close[-show_x_recent_bars]) and !isnan(close);
addchartbubble(show, low, barnumber(), color.cyan, no);
#
 
@C4men In case you haven't done this yet, here is incorporating @halcyonguy code logic to do what you requested.

Code:
#
# 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 show_x_recent_bars = 5;
def show = isnan(close[-show_x_recent_bars]) and !isnan(close);

plot parSAR = if !show then double.nan else SAR;
parSAR.SetPaintingStrategy(PaintingStrategy.POINTS);
parSAR.SetDefaultColor(GetColor(5));
 
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;
 
Last edited by a moderator:

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 57
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
418 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