SAR dots Color Change

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.
 

BenTen

Administrative
Staff member
Staff
VIP
Lifetime
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());
 

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