PSAR Transition Indicator for ThinkorSwim

This study looks for a PSAR state transition (either from bullish to bearish or vice versa) and plots a horizontal price line into the expansion area.
If the state transition already triggered, it calculates the number of bars ago the event happened. I've included labels, alerts as well as chart bubbles.
As of this moment if you load a daily chart of GS, you'll see a state transition TODAY!

Here is the code

Code:
# PSAR Transition Indicator
# tomsk
# 11.20.2019

# V1.0 - 11.20.2019 - tomsk - Initial release of PSAR Transition Indicator

# This study looks for a PSAR state transition (either from bullish to bearish
# or vice versa) and plots a horizontal price line into the expansion area. If
# the state transition already triggered, it calculates the number of bars ago
# the event happened. I've included labels, alerts as well as chart bubbles

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 bar = barNumber();
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));
def transitionBull = state[1] == state.short and state == state.long;
def transitionBear = state[1] == state.long and state == state.short;
def transitionBar = if transitionBull or transitionBear
                    then bar
                    else transitionBar[1];
def transitionPrice = if bar == HighestAll(transitionBar)
                      then close
                      else transitionPrice[1];
def transitionBarsAgo = if bar != transitionBar
                 then bar - transitionBar
                 else if bar == transitionBar
                      then Double.NaN
                 else transitionBarsAgo[1];
def timeAxis = if IsNaN(close[-1]) and !IsNaN(close) then bar else timeAxis[1];
plot horizLine = if GetTime() <= RegularTradingEnd(getYYYYMMDD()) and
                 transitionBar == HighestAll(transitionBar)
                 then transitionPrice
                 else Double.NaN;
horizLine.SetLineWeight(2);
horizLine.SetDefaultColor(Color.Yellow);
AddLabel(transitionBull or transitionBear, "PSAR Transition Detected Now!", Color.Yellow);
AddLabel(!(transitionBull or transitionBear), "PSAR Transition " + transitionBarsAgo + " bars ago", Color.Cyan);
Alert(transitionBull, "PSAR Transition Bull", Alert.BAR, Sound.Ding);
Alert(transitionBear, "PSAR Transition Bear", Alert.Bar, Sound.Ring);
AddChartBubble(bar == HighestAll(timeAxis+offSet), transitionPrice, "PSAR\nTransitioned", Color.Pink);
# End PSAR Transition Indicator
one more thing, the current most PSAR state regardless of bullish/bearish is displaying red dots - searching for where to assign colors based on above/below dot position in the code
 

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

one more thing, the current most PSAR state regardless of bullish/bearish is displaying red dots - searching for where to assign colors based on above/below dot position in the code

Replace this code

Ruby:
parSAR.SetDefaultColor(GetColor(5));

With

Ruby:
parSAR.AssignValueColor(if state == state.short then Color.RED else Color.GREEN);
 
unfortunately didn't work, not sure why

image

Here it is in the original code in post #1 that I modified it in. Perhaps you did not comment out (#) the code or delete it that was to be replaced in the post above.

Capture.jpg
Ruby:
# PSAR Transition Indicator
# tomsk
# 11.20.2019

# V1.0 - 11.20.2019 - tomsk - Initial release of PSAR Transition Indicator

# This study looks for a PSAR state transition (either from bullish to bearish
# or vice versa) and plots a horizontal price line into the expansion area. If
# the state transition already triggered, it calculates the number of bars ago
# the event happened. I've included labels, alerts as well as chart bubbles

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 bar = BarNumber();
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));
parSAR.AssignValueColor(if state == state.short then Color.RED else Color.GREEN);
def transitionBull = state[1] == state.short and state == state.long;
def transitionBear = state[1] == state.long and state == state.short;
def transitionBar = if transitionBull or transitionBear
                    then bar
                    else transitionBar[1];
def transitionPrice = if bar == (transitionBar)
                      then close
                      else transitionPrice[1];
def transitionBarsAgo = if bar != transitionBar
                 then bar - transitionBar
                 else if bar == transitionBar
                      then Double.NaN
                 else transitionBarsAgo[1];
def timeAxis = if IsNaN(close[-1]) and !IsNaN(close) then bar else timeAxis[1];
def hline = if GetTime() <= RegularTradingEnd(GetYYYYMMDD()) and
                 transitionBar == (transitionBar)
                 then transitionPrice
                 else hline[1];
plot horizLine = hline;
horizLine.SetLineWeight(2);
def hlinecolor = if state[1] == state.short then 1 else if state == state.long then -1 else hlinecolor[1];
horizLine.AssignValueColor(if hlinecolor[-1] == 1 then Color.RED else Color.GREEN);
#horizLine.SetDefaultColor(Color.Yellow);
horizLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
AddLabel(transitionBull or transitionBear, "PSAR Transition Detected Now!", Color.YELLOW);
AddLabel(!(transitionBull or transitionBear), "PSAR Transition " + transitionBarsAgo + " bars ago", Color.CYAN);
Alert(transitionBull, "PSAR Transition Bull", Alert.BAR, Sound.Ding);
Alert(transitionBear, "PSAR Transition Bear", Alert.BAR, Sound.Ring);


def bull = if transitionBull then 1 else if bull[1] == 1 and !transitionBear then 1 else 0;

input showpricecolor = yes;
AssignPriceColor(if !showpricecolor then Color.CURRENT else if bull and close > hline then Color.GREEN else if !bull and close < hline then Color.RED else Color.GRAY);

input showbubble = yes;
AddChartBubble(showbubble and bar == HighestAll(timeAxis + offSet), transitionPrice, "PSAR\nTransitioned", if bull then Color.GREEN else Color.RED, if bull then no else yes);
# End PSAR Transition Indicator
 
Here it is in the original code in post #1 that I modified it in. Perhaps you did not comment out (#) the code or delete it that was to be replaced in the post above.
not to belabor the point, no worries, appreciate the help. The dot color isn't the deciding crux of profitability :) I've disabled bar color painting, chart bubble and alerts, the label is all I needed, the dots are fine as it is
 
how do I edit the code to get rid of the pink text bubble on the chart?
You can comment out (put a hashtag in front of) the 2nd to last line of code to "get rid of the pink text bubble on the chart"
change:
Rich (BB code):
AddChartBubble(bar == HighestAll(timeAxis+offSet), transitionPrice, "PSAR\nTransitioned", Color.Pink);
to this:
Rich (BB code):
#AddChartBubble(bar == HighestAll(timeAxis+offSet), transitionPrice, "PSAR\nTransitioned", Color.Pink);
 
Well, I finally got this to work. I placed bubbles and labels with a win loss percentages based upon the candle count and pars crossover. I put marker lines in to make it easier to review instead of looking for the up signals. I also placed an active price line and previous close marker on the bars to make finding support and resistance easier when marking up a chart. I created a HighLine as well as a LowLine for better functionality of the script. http://tos.mx/nOc6qox

Code:
# PSAR Transition Indicator
# tomsk
# 11.20.2019

# V1.0 - 11.20.2019 - tomsk - Initial release of PSAR Transition Indicator

# This study looks for a PSAR state transition (either from bullish to bearish
# or vice versa) and plots a horizontal price line into the expansion area. If
# the state transition already triggered, it calculates the number of bars ago
# the event happened. I've included labels, alerts as well as chart bubbles

# Update 09/21/22 by powerhousescott
# I have added price lines that you can turn on and off.  If working with upper charts you will #have to adjust the aggregation up by a couple of time frames as desired.
# I have added an adjustable candle count and bubbles with win percentage labels

declare upper;

#Price lines  will plot a horizontal bar at each close and and Active price line from a larger #time frame as desired.
input ShowPrice = yes;
input ShowPrevClose= yes;
input PriceAgg = AggregationPeriod.THREE_DAYS;

plot PrevClose = if !ShowPrevClose then double.NaN else close;
PrevClose.SetLineWeight(5);
PrevClose.SetDefaultColor(Color.CYAN);
PrevClose.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

plot ActivePriceLine=
if  !ShowPrice then Double.NaN else Highest(close(period = AggregationPeriod.TWO_DAYS), 1);
ActivePriceLine.SetLineWeight(5);
ActivePriceLine.SetDefaultColor(Color.White);
ActivePriceLine.SetPaintingStrategy(PaintingStrategy.LINE);



#PSAR Code

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 bar = BarNumber();
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.SetLineWeight(2);
parSAR.SetPaintingStrategy(PaintingStrategy.POINTS);
parSAR.AssignValueColor(if state == state.short then Color.MAGENTA else Color.GREEN);

plot parsar2 = SAR;
parsar2.SetLineWeight(5);
parsar2.SetPaintingStrategy(PaintingStrategy.SQUARES);
parsar2.SetDefaultColor(Color.WHITE);

def transitionBull = state[1] == state.short and state == state.long;
def transitionBear = state[1] == state.long and state == state.short;
def transitionBar = if transitionBull or transitionBear
                    then bar
                    else (transitionBar[1]);
def transitionPrice = if bar == transitionBar
                      then close
                      else transitionPrice[1];
def transitionBarsAgo = if bar != transitionBar
                 then bar - transitionBar
                 else if bar ==  transitionBar
                      then Double.NaN
                 else transitionBarsAgo[1];

#PSAR Transition Labels
AddLabel(transitionBull or transitionBear, "PSAR Trans Now!!!", Color.YELLOW);
AddLabel(!(transitionBull or transitionBear), "PSAR Trans " + transitionBarsAgo + " bars ago", Color.CYAN);
Alert(transitionBull, "PSAR Transition Bull", Alert.BAR, Sound.Ding);
Alert(transitionBear, "PSAR Transition Bear", Alert.BAR, Sound.Ring);

#Time Axis, Highline, Lowline
def timeAxis = if IsNaN(close[-1]) and !IsNaN(close) then bar else timeAxis[1];

def highline = if transitionBar == Highest(transitionBar)
                 then transitionPrice
                 else highline[1];

def lowline = if transitionBar == LowestAll(transitionBar)
                 then  (transitionPrice)
                 else lowline[1];

#Bull and Highline Plots
def bull = if transitionBull then 1 else if bull[1] == 1 and !transitionBear then 1 else 0;
def Bear = if transitionBear then 1 else if Bear[1] == 1 and !transitionBull then 1 else 0;


plot hLine = highline;
hLine.SetLineWeight(4);
hLine.AssignValueColor(if bull then Color.GREEN else Color.RED);
hLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

plot LLine = lowline;
LLine.SetLineWeight(4);
LLine.AssignValueColor(if Bear then Color.RED else Color.CYAN);
LLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

#BullHi Plot, Bubbles, Marker

input show_bubbles = yes;
input count_labels = yes;
input markers=yes;
input Candle_Count_1 = 3;
input Candle_Count_2 = 4;
input Candle_Count_3 = 5;
input Pars_Cross = 1;

plot bullHi = close >= hLine and transitionBull [Pars_Cross] ;
bullHi.SetLineWeight (5);
bullHi.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
bullHi.SetDefaultColor(Color.LIGHT_GREEN);

plot BHmark = if !markers then double.nan else bullHi * 2;

def bullHi1 =   bullhi  from Candle_Count_1 bars ago and close > close [Candle_Count_1];
AddChartBubble(if !Show_Bubbles then double.NaN else bullHi1, close, Candle_Count_1 , Color.LIGHT_GREEN);


def bullHi2 =  bullhi  from Candle_Count_2 bars ago and close > close [Candle_Count_2];
AddChartBubble(if !Show_Bubbles then double.Nan else bullHi2, close, Candle_Count_2, Color.LIGHT_GREEN);

def bullHi3 =  bullhi  from Candle_Count_3 bars ago and close > close [Candle_Count_3] ;;
AddChartBubble(if !Show_Bubbles then double.nan else bullHi3, close, Candle_Count_3, Color.LIGHT_GREEN);



#Bull_high Counts and Labels
def CountBH = if bar == !bullHi then 0 else if bullHi then (CountBH [1] + 1) else CountBH [1];

def CountBH1 = if bar == !bullHi1 then 0 else if bullHi1  then (CountBH1[1] + 1) else CountBH1[1];

def CountBH2 = if bar == !bullHi2 then 0 else if bullHi2  then (CountBH2[1] + 1) else CountBH2[1];

def CountBH3 = if bar + 1 == !bullHi3 then 0 else if bullHi3  then (CountBH3[1] + 1) else CountBH3[1];

AddLabel(count_labels, "PARS Hi Cnt " + CountBH + "  [CC " + Candle_Count_1 + "] " + Round(CountBH1 / CountBH * 100, 0) + "%" + "  [CC " + Candle_Count_2 + "] " + Round(CountBH2 / CountBH * 100, 0) + "%" + "  [CC " + Candle_Count_3 + "] " + Round(CountBH3 / CountBH * 100, 0) + "%", Color.LIGHT_GREEN);

#Bear Low Plot and bubbles
plot BearLo = close <= LLine and transitionBear [PARS_Cross];
BearLo.SetLineWeight (5);
BearLo.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
BearLo.SetDefaultColor(Color.LIGHT_ORANGE);

plot BLoClose = close and BearLo;
BLoClose.SetLineWeight (5);
BLoClose.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
BLoClose.SetDefaultColor(Color.LIGHT_ORANGE);

plot BLoMark = if !markers then double.nan else BLoClose * 2;


def BearLo1 = BearLo  from Candle_Count_1 bars ago and close < close [Candle_Count_1] ;
AddChartBubble(BearLo1, close, Candle_Count_1, Color.LIGHT_ORANGE);

def BearLo2 = BearLo  from Candle_Count_2 bars ago and close < close [Candle_Count_2] ;
AddChartBubble(BearLo2, close, Candle_Count_2, Color.LIGHT_ORANGE);

def BearLo3 = BearLo  from Candle_Count_3 bars ago and close < close [Candle_Count_3] ;
AddChartBubble(BearLo3, close, Candle_Count_3, Color.LIGHT_ORANGE);

#Bull_Low Counts and Labels
def CountBL = if bar == !BearLo then 0 else if BearLo  then (CountBL[1] + 1) else CountBL[1];

def CountBL1 = if bar == !BearLo1 then 0 else if BearLo1  then (CountBL1[1] + 1) else CountBL1[1];

def CountBL2 = if bar == !BearLo2 then 0 else if BearLo2  then (CountBL2[1] + 1) else CountBL2[1];

def CountBL3 = if bar == !BearLo3 then 0 else if BearLo3  then (CountBL3[1] + 1) else CountBL3[1];

AddLabel(count_labels, "PSAR Lo Cnt " + CountBL + "  [CC " + Candle_Count_1 + "] " + Round(CountBL1 / CountBL * 100, 0) + "%" + "  [CC " + Candle_Count_2 + "] " + Round(CountBL2 / CountBL * 100, 0) + "%" + "  [CC " + Candle_Count_3 + "] " + Round(CountBL3 / CountBL * 100, 0) + "%", Color.LIGHT_ORANGE);




# End PARS Transition Indicator
Hopefully this helps others with their trading. You can turn on and off the various settings as desired. http://tos.mx/nOc6qox

 
I just left it at the factory settings. If you desire it to give you more signals that adjust the acceleration factor only unless you desire to increase the factor greater than two. Here is a tip just go in to the acceleration factor and change it to .002 and see the results. In effect with this indicator the lower the acceleration factor the more accurate it will be. It is up to you as to what your trading styles are. The candle count settings can be adjusted to find the highest settings or lowest settings to give you the best percentages. If I am looking for options I set the candle count to 7,12,21 and then adjust the acceleration factor to a little faster to give me a predictive alert and make that decision based upon the overall trend of the symbol. I actually have the majority of the PSAR settings turned off and just view the overall trend and support and resistance for the swing. I just used this indicator as an experiment to create the bubbles and count along with the percentages. I knew once I accomplished this in a more complex indicator that I would be able to do it in about any setup that I desire. I hope that helps.
 
Last edited by a moderator:
Can someone help me to get a horizontal line from the last bearish dot and continue until until the start of the next bearish dot?
 
Here it is in the original code in post #1 that I modified it in. Perhaps you did not comment out (#) the code or delete it that was to be replaced in the post above.
Can you add an input for having a horizontal line at the last bearish dot that will continue until the next bearish dot and then start over once the chart has changed back to bearish from bullish.
 
Can you add an input for having a horizontal line at the last bearish dot that will continue until the next bearish dot and then start over once the chart has changed back to bearish from bullish.

Since the last bearish dot woud denote a green long, this horizontal line is called linebull. Include is a red short horizontal controlled at the input.

All other inputs have been set to no so you can see the green lines with heiken ashi candles on the following image.

Snip.png
Ruby:
# PSAR Transition Indicator
# tomsk
# 11.20.2019

# V1.0 - 11.20.2019 - tomsk - Initial release of PSAR Transition Indicator

# This study looks for a PSAR state transition (either from bullish to bearish
# or vice versa) and plots a horizontal price line into the expansion area. If
# the state transition already triggered, it calculates the number of bars ago
# the event happened. I've included labels, alerts as well as chart bubbles

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 bar = BarNumber();
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 showsarline = no;
plot parSAR = SAR;
parSAR.SetPaintingStrategy(PaintingStrategy.POINTS);
#parSAR.SetDefaultColor(GetColor(5));
parSAR.AssignValueColor(if state == state.short then Color.RED else Color.GREEN);
parsar.sethiding(!showsarline);

def transitionBull = state[1] == state.short and state == state.long;
def transitionBear = state[1] == state.long and state == state.short;
def transitionBar = if transitionBull or transitionBear
                    then bar
                    else transitionBar[1];
def transitionPrice = if bar == (transitionBar)
                      then close
                      else transitionPrice[1];
def transitionBarsAgo = if bar != transitionBar
                 then bar - transitionBar
                 else if bar == transitionBar
                      then Double.NaN
                 else transitionBarsAgo[1];
def timeAxis = if IsNaN(close[-1]) and !IsNaN(close) then bar else timeAxis[1];
def hline = if GetTime() <= RegularTradingEnd(GetYYYYMMDD()) and
                 transitionBar == (transitionBar)
                 then transitionPrice
                 else hline[1];

input showtransitionline = no;
plot horizLine = hline;
horizLine.SetLineWeight(2);
horizline.sethiding(!showtransitionline);

def hlinecolor = if state[1] == state.short then 1 else if state == state.long then -1 else hlinecolor[1];
horizLine.AssignValueColor(if hlinecolor[-1] == 1 then Color.RED else Color.GREEN);
#horizLine.SetDefaultColor(Color.Yellow);
horizLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

input showbullline = yes;
def bull = if transitionBull then sar[1] else bull[1];
plot linebull = bull;
linebull.setpaintingStrategy(PaintingStrategy.DASHES);
linebull.setdefaultColor(color.green);
linebull.sethiding(!showbullline);

input showbearline = no;
def bear = if transitionBear then sar[1] else bear[1];
plot linebear = bear;
linebear.setpaintingStrategy(PaintingStrategy.DASHES);
linebear.setdefaultColor(color.red);
linebear.sethiding(!showbearline);

AddLabel(transitionBull or transitionBear, "PSAR Transition Detected Now!", Color.YELLOW);
AddLabel(!(transitionBull or transitionBear), "PSAR Transition " + transitionBarsAgo + " bars ago", Color.CYAN);

Alert(transitionBull, "PSAR Transition Bull", Alert.BAR, Sound.Ding);
Alert(transitionBear, "PSAR Transition Bear", Alert.BAR, Sound.Ring);

def bulltransition = if transitionBull then 1 else if bulltransition[1] == 1 and !transitionBear then 1 else 0;

input showpricecolor = no;
AssignPriceColor(if !showpricecolor then Color.CURRENT else if bulltransition and close > hline then Color.GREEN else if !bulltransition and close < hline then Color.RED else Color.GRAY);

input showtransitionbubble = no;
AddChartBubble(showtransitionbubble and bar == HighestAll(timeAxis + offSet), transitionPrice, "PSAR\nTransitioned", if bulltransition then Color.GREEN else Color.RED, if bull then no else yes);
# End PSAR Transition Indicator
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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