SAR dots Color Change

S

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

BenTen

Administrative
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
#    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());
 
S

stocksniper

Member
You are Awesome sir, how hard would it be to make this MultiTime Frame now that it has the color change.
 
S

stocksniper

Member
Thanks, I found it, but the the dots dont change colors, how do i add that to the script. Thanks
 
S

stocksniper

Member
Got it thanks, I added the bottom of this script to that one and worked. Thank you so much!
 

Similar threads

Top