PSAR Indicator erase past dots

S

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.
 
T

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
 
S

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.
 
T

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

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));
 
T

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

https://usethinkscript.com/threads/supertrend-cci-atr-trend-for-thinkorswim.1090/#post-14027
 
Last edited:
markos

markos

Well-known member
VIP
@RobertPayne welcome back, I haven't seen you in a while.
We really appreciate your added voice and assistance to this community.
Markos
 
RobertPayne

RobertPayne

Member
@RobertPayne welcome back, I haven't seen you in a while.
We really appreciate your added voice and assistance to this community.
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

RmS59

Member
you could also use:

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

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

Similar threads

Top