StochasticMomentumIndex to watchlist


Category:
0
0

Pete

I’m trying to get the color of the SMI to a watchlist…If SMI is red W/L background is red and the same if SMI is green…

 

declare lower;

input audioalarm=yes;
input Price = hlc3;
input RsqLength = 5;
input RsqLimit = .5;
input SmiLimit = 30;
input chopband2= 70;
input smibarbufr = 4;
input showBreakoutSignals = yes;

def overbought = SmiLimit;
def oversold = -SmiLimit;
def percentDLength = 4;
def percentKLength = 5;

# Stochastic Momentum Index (SMI)

def min_low = Lowest(low, percentKLength);
def max_high = Highest(high, percentKLength);
def rel_diff = close – (max_high + min_low) / 2;
def diff = max_high – min_low;
def avgrel = ExpAverage(ExpAverage(rel_diff, percentDLength), percentDLength);
def avgdiff = ExpAverage(ExpAverage(diff, percentDLength), percentDLength);

plot chopband = if IsNaN(close) then Double.NaN else 0;
plot SMI = if avgdiff != 0 then avgrel / (avgdiff / 2) * 100 else 0;

#SMI.SetDefaultColor(Color.BLUE);

SMI.DefineColor(“Up”, CreateColor(0, 153, 51));
SMI.definecolor(“Weak”, Color.LIGHT_GRAY);
SMI.DefineColor(“Down”, Color.RED);
SMI.AssignValueColor(if SMI > SMI[1] then SMI.Color(“Up”) else if SMI < SMI[1] then SMI.Color(“Down”) else SMI.Color(“Weak”));
SMI.SetLineWeight(2);
SMI.SetStyle(Curve.SHORT_DASH);
SMI.SetLineWeight(3);

plot AvgSMI = ExpAverage(SMI, percentDLength);

AvgSMI.DefineColor(“Up”, CreateColor(0, 153, 51));
AvgSMI.definecolor(“Weak”, Color.LIGHT_GRAY);
AvgSMI.DefineColor(“Down”, Color.RED);
AvgSMI.AssignValueColor(if AvgSMI > AvgSMI[1] then AvgSMI.Color(“Up”) else if AvgSMI < AvgSMI[1] then AvgSMI.Color(“Down”) else AvgSMI.Color(“Weak”));
AvgSMI.SetLineWeight(3);

Marked as spam
Posted by (Questions: 49, Answers: 42)
Asked on March 18, 2020 8:16 pm
106 views
0
Private answer

The logic required to set the color of the SMI plot is in the following line:

SMI.AssignValueColor(if SMI > SMI[1] then SMI.Color(“Up”) else if SMI < SMI[1] then SMI.Color(“Down”) else SMI.Color(“Weak”));

All we need to do is change that from "SMI.AssignValueColor" to "AssignBackgroundColor":

AssignBackgroundColor(if SMI > SMI[1] then SMI.Color(“Up”) else if SMI < SMI[1] then SMI.Color(“Down”) else SMI.Color(“Weak”));

The rest of the work is just cleaning up the code by removing everything below that line. And we do need to remove the line of code that plots the "chopband". Oh, and we can remove every single one of the input statements. Dang, there is a lot of garbage in this code that does absolutely nothing. You probably would have been better off just grabbing the code from the built in version named "StochasticMomentumIndex".

At any rate, after we get rid of all the garbage and modify the one line of code for assigning background color we are left with the following:

def percentDLength = 4;
def percentKLength = 5;
# Stochastic Momentum Index (SMI)
def min_low = Lowest(low, percentKLength);
def max_high = Highest(high, percentKLength);
def rel_diff = close – (max_high + min_low) / 2;
def diff = max_high – min_low;
def avgrel = ExpAverage(ExpAverage(rel_diff, percentDLength), percentDLength);
def avgdiff = ExpAverage(ExpAverage(diff, percentDLength), percentDLength);
plot SMI = if avgdiff != 0 then avgrel / (avgdiff / 2) * 100 else 0;
SMI.DefineColor(“Up”, CreateColor(0, 153, 51));
SMI.definecolor(“Weak”, Color.LIGHT_GRAY);
SMI.DefineColor(“Down”, Color.RED);
AssignBackgroundColor(if SMI > SMI[1] then SMI.Color(“Up”) else if SMI < SMI[1] then SMI.Color(“Down”) else SMI.Color(“Weak”));

Marked as spam
Posted by (Questions: 37, Answers: 4087)
Answered on March 19, 2020 8:27 am