Parabolic SAR (PSAR) Question

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?
 
Solution
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' (" +...

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;
 
Solution

RJRJ

New member
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:

SleepyZ

Well-known member
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);
 

Bill Belt

New member
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?
 

Bill Belt

New member
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.
 

SleepyZ

Well-known member
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);
 

Headhunter20

New 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?
Hi bill,
I have had success by right clicking on the chart (of the time frame you want to use the alert on) and clicking on the "create alert" option. In the area where the price button is, click there and scroll all the way down to the "study" option. From there select the "Edit" option, then you can fill out the parameters to any indicator you wish and get an email and/or text message when activated.
Hope that can help.
 

Bill Belt

New member
Hi bill,
I have had success by right clicking on the chart (of the time frame you want to use the alert on) and clicking on the "create alert" option. In the area where the price button is, click there and scroll all the way down to the "study" option. From there select the "Edit" option, then you can fill out the parameters to any indicator you wish and get an email and/or text message when activated.
Hope that can help.
Thanks but the Parabolic SAR isn't one of them listed. The coding by SleepyZ worked great. Used it today and very happy with it.
 

Headhunter20

New member
For future, you have to click the "edit" option at the end, and every study in the roster is available. I have found it helpful for crossovers of every kind. I work, but am able to stop when I get the text and do what the appropriate response is to my chart. Glad you have a solution.
 

halcyonguy

Well-known member
VIP
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.
i have no idea what you are trying to do.
try to list out each rule that your modified study will follow.
then list how it will be diffetent from the stock study.
 

David45

Member
2019 Donor
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:

SleepyZ

Well-known member
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:

Headhunter20

New member
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.
 

SleepyZ

Well-known member
@SleepyZ - Works Great. Thanks! Is there some way to do an alert on a HullMovingAverage? That's another study I like and a sound would be very helpful when it switches from up to down and visa versa. Thanks in advance.
Here is the HMA with similar code to PSAR request above. Please note that the alert is set to 'Alert.Bar' as there is a lot of movement in the HMA and if you want your attention brought to the chart alert, then this will keep alerting you. The 'Alert.Once' will shut off when there is a false movement and you will miss the notification when there is a confirmed change in direction.

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

input price = close;
input length = 20;
input displace = 0;

plot HMA = MovingAverage(AverageType.HULL, price, length)[-displace];

HMA.DefineColor("Up", GetColor(1));
HMA.DefineColor("Down", GetColor(0));
HMA.AssignValueColor(if HMA > HMA[1] then HMA.color("Up") else HMA.color("Down"));

def bull =  if HMA[1] < HMA[2] and HMA > HMA[1] then 1 else 0;
def bear =  if HMA[1] > HMA[2] and HMA < HMA[1] then 1 else 0;

input alerts = yes;
alert(alerts and bull, "Bull", Alert.bar, Sound.Ding);
alert(alerts and bear, "Bear", Alert.bar, Sound.Ding);

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, high, "BEAR", Color.RED);
AddChartBubble(bubbles and bull, low , "BULL", Color.LIGHT_GREEN, no);
 

Similar threads

Top