AssignValueColor based on condition

Zlotko

Active member
VIP
Could someone help make the cci Length 14 change color from red to green when it crosses up the cci Length 9 and Green to red when in crosses down?
Thank you.
------------------------------------------------------------------

Code:
declare lower;

input cciLength = 14;
input cciAvgLength = 9;
input over_sold = -100;
input over_bought = 100;

plot CCI = CCI(length = cciLength);
plot CCIAvg = Average(CCI, cciAvgLength);
plot OverBought = over_bought;
plot OverSold = over_sold;

CCI.setDefaultColor(GetColor(9));
CCIAvg.setDefaultColor(GetColor(8));
OverBought.setDefaultColor(GetColor(5));
OverSold.setDefaultColor(GetColor(5));
plot ZeroLine = 0;
ZeroLine.SetDefaultColor(Color.MAGENTA);
ZeroLine.HideBubble();
ZeroLine.HideTitle();
 
Last edited by a moderator:
Thank you, I tried the same with this code, but it did not work? just for ImedLength and Zerro line. Could you help? (#ImedLength.AssignValueColor(if ImedLength > ZeroLine then Color.Green else Color.RED))

Code:
#Name: Dilbert_StanL_MarketForecast_TMF_original
#StudyName: Dilbert_StanL_MarketForecast_TMF_original
#Description: Replica of Investools MarketForecast study
#Author: Dilbert/Stanl/Mobius and assorted others you tried to crack this
#Requested By:
# Ver Date Auth Change
# v2 012817 Dilbert Finally calculate the momentum line correctly. Scale to 100 instead of 200.
# TOS.mx Link:
# Trading Notes:

#
# StanL
#
#hint:<b>Market Forecast</b>modified with upper and lower reversal zones and labels. Is a double-scale version of the original. All plots maintain their relation with 2X scale.

declare lower;
input ImedLength = 31; # orig std = 31
input MomentumLength = 5; # orig std = 31
input NearLength = 3; # orig std = 3

def c1I = close - lowest(low,ImedLength);
def c2I = highest(high,ImedLength) - lowest(low,ImedLength);
def FastK_I = c1I / c2I * 100;
def c1N = close - lowest(low,NearLength);
def c2N = highest(high,NearLength) - lowest(low,NearLength);
def FastK_N = c1N / c2N * 100;

def c1M = close - lowest(low,MomentumLength);
def c2M = highest(high,MomentumLength) - lowest(low,MomentumLength);
def FastK_M = c1M / c2M * 100;
Plot Intermediate = Average(FastK_I, 5); # = TMF's GreenLine
Intermediate.AssignValueColor(getColor(0));
Intermediate.SetStyle(Curve.FIRM);
Intermediate.SetLineWeight(2);
Plot NearTerm = Average(FastK_N, 2); # = TMF's WhiteLine
NearTerm.AssignValueColor(color.White);
NearTerm.SetStyle(Curve.FIRM);
NearTerm.SetLineWeight(1);

def Min1 = Min(low, low[1]);
def Min2 = Min(min1, Min(low[2], low[3]));
def Max1 = Max(high, Max(high[1], Max(high[2], high[3])));

plot momentum = ((close - Min1) / (Max1 - Min2)) * 100;
momentum.setDefaultColor(Color.Red);

plot bottomline = 0;
bottomline.AssignValueColor(color.gray);
bottomline.SetStyle(Curve.FIRM);

plot topLine = 100;
topLine.AssignValueColor(color.gray);
topLine.SetStyle(Curve.FIRM);

plot UpperRevZone = 80;
bottomline.AssignValueColor(color.gray);
bottomline.SetStyle(Curve.FIRM);

plot LowerRevZone = 20;
LowerRevZone.AssignValueColor(color.gray);
LowerRevZone.SetStyle(Curve.firm);
plot UpLimit = 120;
UpLimit.AssignValueColor(color.black);

AddCloud(bottomline, LowerRevZone, color.green, Color.green);
AddCloud(topLine, UpperRevZone, color.RED, color.red);

plot ZeroLine = 50;
# Below lines are related to a scan
# def ClusterHigh = Intermediate >= 80 && NearTerm >= 80 && Momentum >= 80 ;
# def ClusterLow = Intermediate <= 20 && NearTerm <= 20 && Momentum <= 20;
# plot cluster = if clusterhigh or clusterlow then 1 else 0;
#ImedLength.AssignValueColor(if ImedLength > ZeroLine then Color.Green else Color.RED)
#---------- End Of Code ---------------
 
Last edited by a moderator:
Thank you, I tried the same with this code, but it did not work? just for ImedLength and Zerro line. Could you help? (#ImedLength.AssignValueColor(if ImedLength > ZeroLine then Color.Green else Color.RED))


#Name: Dilbert_StanL_MarketForecast_TMF_original
#StudyName: Dilbert_StanL_MarketForecast_TMF_original
#Description: Replica of Investools MarketForecast study
#Author: Dilbert/Stanl/Mobius and assorted others you tried to crack this
#Requested By:
# Ver Date Auth Change
# v2 012817 Dilbert Finally calculate the momentum line correctly. Scale to 100 instead of 200.
# TOS.mx Link:
# Trading Notes:

#
# StanL
#
#hint:<b>Market Forecast</b>modified with upper and lower reversal zones and labels. Is a double-scale version of the original. All plots maintain their relation with 2X scale.

declare lower;
input ImedLength = 31; # orig std = 31
input MomentumLength = 5; # orig std = 31
input NearLength = 3; # orig std = 3

def c1I = close - lowest(low,ImedLength);
def c2I = highest(high,ImedLength) - lowest(low,ImedLength);
def FastK_I = c1I / c2I * 100;
def c1N = close - lowest(low,NearLength);
def c2N = highest(high,NearLength) - lowest(low,NearLength);
def FastK_N = c1N / c2N * 100;

def c1M = close - lowest(low,MomentumLength);
def c2M = highest(high,MomentumLength) - lowest(low,MomentumLength);
def FastK_M = c1M / c2M * 100;
Plot Intermediate = Average(FastK_I, 5); # = TMF's GreenLine
Intermediate.AssignValueColor(getColor(0));
Intermediate.SetStyle(Curve.FIRM);
Intermediate.SetLineWeight(2);
Plot NearTerm = Average(FastK_N, 2); # = TMF's WhiteLine
NearTerm.AssignValueColor(color.White);
NearTerm.SetStyle(Curve.FIRM);
NearTerm.SetLineWeight(1);

def Min1 = Min(low, low[1]);
def Min2 = Min(min1, Min(low[2], low[3]));
def Max1 = Max(high, Max(high[1], Max(high[2], high[3])));

plot momentum = ((close - Min1) / (Max1 - Min2)) * 100;
momentum.setDefaultColor(Color.Red);

plot bottomline = 0;
bottomline.AssignValueColor(color.gray);
bottomline.SetStyle(Curve.FIRM);

plot topLine = 100;
topLine.AssignValueColor(color.gray);
topLine.SetStyle(Curve.FIRM);

plot UpperRevZone = 80;
bottomline.AssignValueColor(color.gray);
bottomline.SetStyle(Curve.FIRM);

plot LowerRevZone = 20;
LowerRevZone.AssignValueColor(color.gray);
LowerRevZone.SetStyle(Curve.firm);
plot UpLimit = 120;
UpLimit.AssignValueColor(color.black);

AddCloud(bottomline, LowerRevZone, color.green, Color.green);
AddCloud(topLine, UpperRevZone, color.RED, color.red);

plot ZeroLine = 50;
# Below lines are related to a scan
# def ClusterHigh = Intermediate >= 80 && NearTerm >= 80 && Momentum >= 80 ;
# def ClusterLow = Intermediate <= 20 && NearTerm <= 20 && Momentum <= 20;
# plot cluster = if clusterhigh or clusterlow then 1 else 0;
#ImedLength.AssignValueColor(if ImedLength > ZeroLine then Color.Green else Color.RED)
#---------- End Of Code ---------------
Since ImedLength is not a plot you cannot assign a color to it. Replace it with this
Code:
Plot IL = ImedLength;
il.assignvaluecolor(if Il > zeroline then color.green else color.red);
 
It is not working the original assign color stays the same. Do I need to change anything else?

Are you sure you know which plot you want to color...??? That code could use a bit of an overhaul due to some questionable coding...

Throughout the script whoever coded it originally used AssignColorValue() on lines that would be better served with SetDefaultColor() which should be used for painting a single value color... AssignValueColor() should only be used for plots values requiring conditional coloring... It may sound like a minor issue but there is good reason for there being two different coloring functions - as describe above... Us old bit-fiddlers use optimized code which we used way back in the early days when every CPU clock cycle was a valuable commodity...

We're obfuscating this topic so perhaps management can split this second request into a new one... I am reviewing the entire script now...
 
Last edited:
@Zlotko Here is a cleaned up copy of the script with working Intermediate.AssignValueColor()...

Ruby:
#Name: Dilbert_StanL_MarketForecast_TMF_original
#StudyName: Dilbert_StanL_MarketForecast_TMF_original
#Description: Replica of Investools MarketForecast study
#Author: Dilbert/Stanl/Mobius and assorted others you tried to crack this
#Requested By:
# Ver Date Auth Change
# v2 012817 Dilbert Finally calculate the momentum line correctly. Scale to 100 instead of 200.
# TOS.mx Link:
# Trading Notes:

#
# StanL
#
#hint:<b>Market Forecast</b>modified with upper and lower reversal zones and labels. Is a double-scale version of the original. All plots maintain their relation with 2X scale.

declare lower;
input ImedLength = 31; # orig std = 31
input MomentumLength = 5; # orig std = 31
input NearLength = 3; # orig std = 3

def c1I = close - lowest(low,ImedLength);
def c2I = highest(high,ImedLength) - lowest(low,ImedLength);
def FastK_I = c1I / c2I * 100;
def c1N = close - lowest(low,NearLength);
def c2N = highest(high,NearLength) - lowest(low,NearLength);
def FastK_N = c1N / c2N * 100;

def c1M = close - lowest(low,MomentumLength);
def c2M = highest(high,MomentumLength) - lowest(low,MomentumLength);
def FastK_M = c1M / c2M * 100;

Plot Intermediate = Average(FastK_I, 5); # = TMF's GreenLine
Intermediate.SetDefaultColor(getColor(0));
Intermediate.SetStyle(Curve.FIRM);
Intermediate.SetLineWeight(2);
Intermediate.assignvaluecolor(if Intermediate > zeroline then color.green else color.red);

Plot NearTerm = Average(FastK_N, 2); # = TMF's WhiteLine
NearTerm.SetDefaultColor(color.White);
NearTerm.SetStyle(Curve.FIRM);
NearTerm.SetLineWeight(1);

def Min1 = Min(low, low[1]);
def Min2 = Min(min1, Min(low[2], low[3]));
def Max1 = Max(high, Max(high[1], Max(high[2], high[3])));

plot momentum = ((close - Min1) / (Max1 - Min2)) * 100;
momentum.setDefaultColor(Color.Red);

plot bottomline = 0;
bottomline.SetDefaultColor(color.gray);
bottomline.SetStyle(Curve.FIRM);

plot topLine = 100;
topLine.SetDefaultColor(color.gray);
topLine.SetStyle(Curve.FIRM);

plot UpperRevZone = 80;
bottomline.SetDefaultColor(color.gray);
bottomline.SetStyle(Curve.FIRM);

plot LowerRevZone = 20;
LowerRevZone.SetDefaultColor(color.gray);
LowerRevZone.SetStyle(Curve.firm);

plot UpLimit = 120;
UpLimit.SetDefaultColor(color.black);

AddCloud(bottomline, LowerRevZone, color.green, Color.green);
AddCloud(topLine, UpperRevZone, color.RED, color.red);

# Below lines are related to a scan
# def ClusterHigh = Intermediate >= 80 && NearTerm >= 80 && Momentum >= 80 ;
# def ClusterLow = Intermediate <= 20 && NearTerm <= 20 && Momentum <= 20;
# plot cluster = if clusterhigh or clusterlow then 1 else 0;

#---------- End Of Code ---------------
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
179 Online
Create Post

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