ParabolicSAR Two Color Dots?


Category:
0
0

Hi Pete,

The ParabolicSAR in TOS platform only has one color dot for buy & sell. How would you modify the code below to have blue dot for buy and red dot for sell? Thank you!

# TD Ameritrade IP Company, Inc. (c) 2008-2018
#

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

Marked as spam
Posted by (Questions: 23, Answers: 57)
Asked on October 13, 2018 5:03 am
188 views
0
Private answer

The ParabolicSAR study keeps track of the current state, whether long or short. We can use this to dynamically set the color of the dots, based on that state. It’s either state.long or state.short.

This should do the trick. I have not tested it. Just create a new custom study and use all the code you posted in your question above. Then tack this last statement to the very bottom.

parSAR.AssignValueColor(if state == state.long then Color.CYAN else if state == state.short then Color.MAGENTA else Color.Current);

I haven’t tested it, but it should work.

Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on October 13, 2018 8:03 am
0

Thanks, Pete! It worked.

( at October 13, 2018 1:32 pm)