Wrong type cast: different types after then and else: double vs class

AntMan

New member
Hello all.. I am looking for some assistance with this script.. I am trying to code an AddLabel script to display whether we're opening up above, below or inside yesterday's trading range.. here's what I am having trouble with:

Code:
AddLabel(yes, if op < PrevDayLow then "Below yRange" else if op > PrevDayHigh then "Above yRange" else "Inside yRange");

I have defined prior day's high/low as "PrevDayHigh & "PrevDayLow" and todays open as "op".. I want the code to display "Below yRange" if open < yesterday's low, "Above yRange" if open > yesterday's high, and "Inside yRange" if the open is between yesterday's high & low..
I am getting an error message I've not seen before.. the code itself is not highlighted red like when it's wrong, but TOS won't let me save/apply the code because of the error message.. the "apply" button is grayed out and when I press "ok" the script is displayed in red as it contains an error.. the error message is:

Wrong type cast: different types after then and else: double vs class java.lang.String

I cannot figure out what I am doing wrong..

Thanks in advance!
 

DoQtorNo

Member
Working on a new label to show if a candle has closed above or below the high of the previous lowest day. I am receiving an error: Expected class com. devexperts.tos.thinkscript. I receive the error whether or not I place the "yes" before the if statement.

Below is the code:

Code:
DefineGlobalColor("NoFlag", Color.GRAY);
DefineGlobalColor("CloseAbove", color.dark_green);
DefineGlobalColor("CloseBelow", Color.dark_red);

plot last = close;

def cahold = (close > high[1] and low[1] < low[2]) or (close > high[1] and low[1] > low[2] and high[1] < high[2]) or (close > high[2] and close[1] < high[2]);
def cblohd = (close < low[1] and high[1] > high[2]) or (close < low[1] and high[1] < high[2] and low[1] > low[2]) or (close < low[2] and close[1] > low[2]);
AddLabel(yes, if cahold then GlobalColor("CloseAbove") else if cblohd then GlobalColor("CloseBelow") else GlobalColor("NoFlag"));

Thanks in Advance.
 

OBW

Member
The AddLabel() function must be in this format:

AddLabel( <when should the label be displayed>, <what text should the label display>, <what color should the label be>);

Your code is attempting to assign a conditional color to the "text" field. That's why you are getting the error.

Ruby:
AddLabel(yes, " ", if cahold then GlobalColor("CloseAbove") else if cblohd then GlobalColor("CloseBelow") else GlobalColor("NoFlag"));
 

DoQtorNo

Member
@OBW That's actually what I want to happen. I want the test Close Above to appear with green background if that condition is met, as well as the opposite or neutral. (i.e. Red or Gray background ).

Thanks you for the response and your assistance....
 
Last edited by a moderator:
Ive successfully made 4 different variations of this code but this forum is what I wanted, Where its green and says"Money Flow $+xyz" or is red and says "Money Flow $-xyz" the other forums all didn't include the + or - but I wanted to take it a step farther and am now stuck]. the 3 different ways I've playing around with it are detonated between the #---OR---- sections. I got the code from another study which the code I copied and pasted at the bottom. depending on what I do, it makes the ADDLABEL red, the 4th IF red or no red and says
"Wrong type cast: different types after then and else: double vs class com.devexperts.tos.thinkscript.data.CustomColor
Wrong type cast: different types after then and else: double vs class java.lang.String" here is the code:

Code:
#Money_Flow Declining or advancing

declare lower;
input price = close;

def MF = TotalSum(if price < price[1] then -price * volume else if price > price[1] then price * volume else 0);

def ZeroLine = 0;

#MF.SetDefaultColor(GetColor(8));
#ZeroLine.SetDefaultColor(GetColor(5));

plot MFD = MF - MF[1];
def na = Double.NaN;
#plot label_negative_Color = MFD < 0;
#plot label_positive_color = MFD > 0;
input show_MFD_Label = yes;

DefineGlobalColor("Pre_Cyan", CreateColor(0, 150, 200)) ;
DefineGlobalColor("LabelGreen",  CreateColor(0, 165, 0)) ;
DefineGlobalColor("Label2Green",  CreateColor(0, 200, 0)) ;
DefineGlobalColor("LabelRed",  CreateColor(225, 0, 0));

AddLabel(yes,
if MF - MF[1] < 0     then "Money Flow $-" + MFD else
if MF - MF[1] > 0      then "Money Flow $+" + MFD else
if MF - MF[1] < 0     then GlobalColor("LabelRed") else
if MF - MF[1] > 0      then GlobalColor("Pre_Cyan") else na);
#----------------------------OR----------------------------
#AddLabel(yes,
#if MFD <= 0     then "Money Flow $-" + MFD else
#if MFD > 0      then "Money Flow $+" + MFD else
#if MFD < 0     then GlobalColor("LabelRed") else
#if MFD > 0      then GlobalColor("Pre_Cyan") else GlobalColor("LabelGreen"));
#----------------------------OR----------------------------
#AddLabel(yes,
#if MFD <= 0     then "Money Flow $-" + MFD else
#if MFD > 0      then "Money Flow $+" + MFD else
#if MFD < 0     then GlobalColor("LabelRed") else
#if MFD > 0      then GlobalColor("Pre_Cyan") else na);
####End Code

#Addlabel section taken from the following
#Name: ChrisStoplight LABELS ONLY
#Programmed By: Chris Ball (xxx) on #1/31/09
#Added Choppiness and Squeeze Indicator by [email protected] Lounge
#Added FW_Mobo_Basic
#Modified to add/change indicators
#Modified to show labels only @MerryDay 12/2020
#AddLabel(yes,
#if ADX <= 20   then "ADX No Trend " +round(ADX,0) else
#if ADX >  40   then "ADX WOW " +round(ADX,0) else  "ADX Trending " +round(ADX,0),
#if ADX < 20    then GlobalColor("LabelRed") else
#if ADX > 40    then GlobalColor("Pre_Cyan") else GlobalColor("LabelGreen"));
 

generic

Active member
@mourningwood4521 Simpler format

Code:
AddLabel(yes,
if MF - MF[1] < 0 then "Money Flow $-" + MFD else "Money Flow $+" + MFD,
if MF - MF[1] < 0 then GlobalColor("LabelRed") else GlobalColor("Pre_Cyan"));

If you want to keep the same format then this will work.

Code:
AddLabel(yes,
if MF - MF[1] < 0     then "Money Flow $-" + MFD else
if MF - MF[1] > 0      then "Money Flow $+" + MFD else "",
if MF - MF[1] < 0     then GlobalColor("LabelRed") else
if MF - MF[1] > 0      then GlobalColor("Pre_Cyan") else color.white);
 
Amazing! Thank you for the help @generic !!

I tried adding the "", on the first line and it wouldn't take it and I was getting pretty frustrated so I didn't try it on the second line.

So all I was doing wrong was forgetting the --> "", on line 2, and needed to change the very last word on the 4th line to color.white;?

What exactly does color.white change? I don't see any white on the chart.

I saw on another thread this https://usethinkscript.com/threads/expected-class-error.3858/post-35569

Code:
AddLabel(yes, " ", if cahold then GlobalColor("CloseAbove") else if cblohd then GlobalColor("CloseBelow") else GlobalColor("NoFlag"));
 

generic

Active member
@mourningwood4521 The problem is using Double.NaN for the text and color. The text portion accepts strings hence the "double vs class java.lang.String" so I replaced it with "" which is a empty string. "double vs class com.devexperts.tos.thinkscript.data.CustomColor" is just saying you can't use Double.NaN as a color so replacing it with any color will work, I just chose white.
 

Similar threads

Top