thinkScript Expected Double Error: How to Fix?

mike8u

New member
Hi I am not a coder so I ran into a problem off the bat: What does it mean when it expects double? How can I fix it?

Code:
input last = pricetype.LAST;
plot DeltaOpen  =  (last - open) / open;
 
Solution
@majidg The error is likely coming from your AddLabel() function. The correct method is "AddLabel( boolean visible, Any text, CustomColor color)"; the problem label attempts to combine the text and color parameters. Change it to something such as
Code:
AddLabel(x, if UpSignal then "correct" else "your text here",color.cyan);
The "boolean visible" parameter is simply yes/no, true/false or or 1/0. By your using plot "x" as the "boolean visible", you already have the label turning on when x equals 1 and off when x equals 0, so there's no need to add "else double.nan" in the label.
Looking at other labels in the forum can also help you see the different ways people code the AddLabel() function...
Here you go:

Code:
input priceType = PriceType.LAST;
def LastPrice = close(priceType = priceType);
plot DeltaOpen  = (LastPrice - open) / open;
 
I am trying to script a Watchlist Custom Column. I get an ” expected double” error code at the end. I am fairly new to coding and have no idea what that means or how to fix it. Can you help?

Code:
declare lower;
input price = close;
input setting_R = {"5", "10", default "15"};
input fastLength = 5;
input slowLength = 20;
input oversold = 20;
input overbought = 80;
def os = oversold;
def ob = overbought;
def period;
switch (setting_R) {
case "5":
period = 5;
case "10":
period = 10;
case "15":
period = 15;
}
def fast = ExpAverage(price, fastLength) - ExpAverage(price, slowLength);
def base = ExpAverage(price, fastLength);
def slow = ExpAverage(price, slowLength);
def hs = Highest(slow, period);
def ls = Lowest(slow, period);
def JKTA_Slow = if hs == ls then 0 else ((hs - slow) / (hs - ls) * (-100) + 100);
def hb = Highest(base, period);
def lb = Lowest(base, period);
def JKTA_Base = if hb == lb then 0 else ((hb - base) / (hb - lb) * (-100) + 100);
def hf = Highest(fast, period);
def lf = Lowest(fast, period);
def JKTA_Fast = if hf == lf then 0 else ((hf - fast) / (hf - lf) * (-100) + 100);
def line50 = 50;
def line0 = 0;
def line100 = 100;
plot BuySignal =  if("JKTA_Slow" >= 100 and "JKTA_Base" >= 100 and "JKTA_Fast" >=  99) then 1 else 0 ;
AssignBackgroundColor( if BuySignal then Color.LIGHT_GREEN else Color.DARK_RED);
BuySignal. AssignValueColor( if BuySignal then Color.BLACK else Color.RED);
 
@sgbaum88 TOS does not use "marks around variable names"

Code:
#change your line of code:
plot BuySignal = if("JKTA_Slow" >= 100 and "JKTA_Base" >= 100 and "JKTA_Fast" >= 99) then 1 else 0 ;

#to this line of code:
plot BuySignal = JKTA_Slow >= 100 and JKTA_Base >= 100 and JKTA_Fast >= 99 ;

This will correct the syntax so you won't get an error code. I didn't check the logic of what you were trying to do.
 
Thank you so much, MerryDay! That corrected the problem. I am making a watchlist custom column the displays a 1 when thee JK Trend Analyzer Study shows the Fast,Slow, and Base Lines all reach the 100 line, and a 0 when they do not.I use this as a possible buy signal.
 
Ruby:
def TF = GetAggregationPeriod();
def Displayed_TF = if TF < 3600000 then TF / 60000 + "m" else if TF < 86400000 then TF / 3600000 + "h" else if TF < 604800000 then TF / 86400000 + "D" else TF / 604800000 + "W";

Error: Expected double at 14:5
(this is the last line)

No clue why this is happening, I've tried using the AsText() function, which converts from number to String but no clue why that's happening :/
 
i wrote this. it looks good but it says its a invalid script because of expected double

Code:
input length = 50;

plot Vol = volume;
plot VolAvg = Average(volume, length);

Vol.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
Vol.SetLineWeight(3);
Vol.DefineColor("Up", Color.UPTICK);
Vol.DefineColor("Down", Color.DOWNTICK);
Vol.AssignValueColor(if close > close[1] then Vol.Color("Up") else if close < close[1] then Vol.Color("Down") else GetColor(1));
VolAvg.SetDefaultColor(GetColor(8));

#Inputs
input factor           =     0.001;
def haclose = (open + high + low + close) / 4;
def haopen = CompoundValue(1, (haopen[1] + haclose[1]) / 2, (open[1] + close[1]) / 2);
def haHigh = Max(high, haopen);
def haHigh2 = Max(haHigh, haclose);
def haLow = Min(low, haopen);
def haLow2 = Min(haLow, haclose);
def RSI = RSI();

def HAdoji = ((haclose >= (1 - factor) * haopen and haclose <= (1 + factor) * haopen) or (haopen >= (1 - factor) * haclose and haopen <= (1 + factor) * haclose)) and (haclose > haLow2) and (haopen > haLow2);

def doji = hadoji and Vol.Color("Up");

plot signal =doji;
signal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
signal.SetDefaultColor(CreateColor(0, 0, 153));
 
@joshua74133 In line 25, you reference
def doji = hadoji and Vol.Color("Up"); Vol.Color("Up") is a format statement not a variable, perhaps you mean:
def doji = hadoji and close>close[1];
 
Last edited:
I am trying to script a Watchlist Custom Column. I get an ” expected double” error code at the end. I am fairly new to coding and have no idea what that means or how to fix it. Can you help?

Code:
declare lower;
input price = close;
input setting_R = {"5", "10", default "15"};
input fastLength = 5;
input slowLength = 20;
input oversold = 20;
input overbought = 80;
def os = oversold;
def ob = overbought;
def period;
switch (setting_R) {
case "5":
period = 5;
case "10":
period = 10;
case "15":
period = 15;
}
def fast = ExpAverage(price, fastLength) - ExpAverage(price, slowLength);
def base = ExpAverage(price, fastLength);
def slow = ExpAverage(price, slowLength);
def hs = Highest(slow, period);
def ls = Lowest(slow, period);
def JKTA_Slow = if hs == ls then 0 else ((hs - slow) / (hs - ls) * (-100) + 100);
def hb = Highest(base, period);
def lb = Lowest(base, period);
def JKTA_Base = if hb == lb then 0 else ((hb - base) / (hb - lb) * (-100) + 100);
def hf = Highest(fast, period);
def lf = Lowest(fast, period);
def JKTA_Fast = if hf == lf then 0 else ((hf - fast) / (hf - lf) * (-100) + 100);
def line50 = 50;
def line0 = 0;
def line100 = 100;
plot BuySignal =  if("JKTA_Slow" >= 100 and "JKTA_Base" >= 100 and "JKTA_Fast" >=  99) then 1 else 0 ;
AssignBackgroundColor( if BuySignal then Color.LIGHT_GREEN else Color.DARK_RED);
BuySignal. AssignValueColor( if BuySignal then Color.BLACK else Color.RED);
Can you please help with this error:
I get "double expected" error when I write the following:
plot x = if UpSignal then 1 else 0;
AddLabel(x,if upsignal then "correct" and color.cyan else double.nan);
 
@majidg The error is likely coming from your AddLabel() function. The correct method is "AddLabel( boolean visible, Any text, CustomColor color)"; the problem label attempts to combine the text and color parameters. Change it to something such as
Code:
AddLabel(x, if UpSignal then "correct" else "your text here",color.cyan);
The "boolean visible" parameter is simply yes/no, true/false or or 1/0. By your using plot "x" as the "boolean visible", you already have the label turning on when x equals 1 and off when x equals 0, so there's no need to add "else double.nan" in the label.
Looking at other labels in the forum can also help you see the different ways people code the AddLabel() function.
https://usethinkscript.com/resources/thinkscript-addlabel-adding-custom-labels-to-your-chart.8/
https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Look---Feel/AddLabel
 
Solution
@majidg The error is likely coming from your AddLabel() function. The correct method is "AddLabel( boolean visible, Any text, CustomColor color)"; the problem label attempts to combine the text and color parameters. Change it to something such as
Code:
AddLabel(x, if UpSignal then "correct" else "your text here",color.cyan);
The "boolean visible" parameter is simply yes/no, true/false or or 1/0. By your using plot "x" as the "boolean visible", you already have the label turning on when x equals 1 and off when x equals 0, so there's no need to add "else double.nan" in the label.
Looking at other labels in the forum can also help you see the different ways people code the AddLabel() function.
https://usethinkscript.com/resources/thinkscript-addlabel-adding-custom-labels-to-your-chart.8/
https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Look---Feel/AddLabel
Thanks alot Pensar for your kindness and quick response, it fixed the problem
 
Thanks alot Pensar for your kindness and quick response, it fixed the problem
Hi Pensar, one more question on addchartbubble. I want to write the last price on the current price line, is this correct?
AddChartBubble(yes, line+.2, "current_price =", Color.CYAN, no);

I want to add price for the last bar on the line, but it put price on every bar. Thanks for your help.
 
Hi Pensar, one more question on addchartbubble. I want to write the last price on the current price line, is this correct?
AddChartBubble(yes, line+.2, "current_price =", Color.CYAN, no);

I want to add price for the last bar on the line, but it put price on every bar. Thanks for your help.
Code:
AddChartBubble(!isnan(close) and isnan(close[-1]), line+.2, "current_price = " + close, Color.CYAN, no);
@majidg I think the above will work, test it and see.
Think of plotting chart bubbles as graphing a point using the x and y axis. Using the isnan() line assigns the bubble at only one point along the horizontal x-axis, and "line+.2" assigns it at only one point along the vertical y-axis. The "yes" or "no" at the end makes the bubble plot above or below the point you select.
 
Code:
AddChartBubble(!isnan(close) and isnan(close[-1]), line+.2, "current_price = " + close, Color.CYAN, no);
@majidg I think the above will work, test it and see.
Think of plotting chart bubbles as graphing a point using the x and y axis. Using the isnan() line assigns the bubble at only one point along the horizontal x-axis, and "line+.2" assigns it at only one point along the vertical y-axis. The "yes" or "no" at the end makes the bubble plot above or below the point you select.
WOW!, thanks Pensar. I like the way you spend time to explain. I really appreciate.
 
can someone please assist, I get The error message "Expected double at 5:10"


input fastLength = 5;
input slowLength = 34;
input oscillatorLength = 5;

def AO = MACD(fastLength, slowLength, "close").Diff;
def AO_EMA = ExpAverage(AO, oscillatorLength);

def l1 = low < low[1] and low[1] < low[2];
def l2 = low == lowest(low, 10);
def l3 = low < low[2] and low[2] < low[3];
def w = l1 and l2 and l3;

def ao_crossed_below_zero = AO < 0 and AO[1] > 0;
def ao_turned_red = AO_EMA < AO_EMA[1];
def ao_turned_green = AO_EMA > AO_EMA[1];

plot w_bottom = if w and ao_crossed_below_zero and ao_turned_red and ao_turned_green then low else double.nan;
 
can someone please assist, I get The error message "Expected double at 5:10"


input fastLength = 5;
input slowLength = 34;
input oscillatorLength = 5;

def AO = MACD(fastLength, slowLength, "close").Diff;
def AO_EMA = ExpAverage(AO, oscillatorLength);

def l1 = low < low[1] and low[1] < low[2];
def l2 = low == lowest(low, 10);
def l3 = low < low[2] and low[2] < low[3];
def w = l1 and l2 and l3;

def ao_crossed_below_zero = AO < 0 and AO[1] > 0;
def ao_turned_red = AO_EMA < AO_EMA[1];
def ao_turned_green = AO_EMA > AO_EMA[1];

plot w_bottom = if w and ao_crossed_below_zero and ao_turned_red and ao_turned_green then low else double.nan;


what does the whole error say?
they always give you a clue as to what is wrong.

incompatible parameter "close" at ...
expected double at ...


so the MACD function doesn't like "close" as a 3rd parameter.
it expected to see a double...
it wants to see a number as the 3rd parameter, not "close" ( which is text ).


you can try to remove the quotes, and use close.
then it argues and displays a new error message.

only constants expected here
so it doesn't like a variable with changing values.


time to go look up the study and learn what are valid input parameters for it.
https://tlc.thinkorswim.com/center/reference/Tech-Indicators/studies-library/M-N/MACD

MACD inputs
MACD(fast length , slow length, macd length, average type, show breakout signals)

there is no price parameter.
the 3rd parameter is a length, and it expects a number from an input , a constant.


-------------
if you are creating a program and come across an error, the first thing you should do is look up the function in a manual, to learn how to use it.

--------------


but

the logic is wrong in the plot formula... it will never plot,
3 of the conditions will never be true at the same time,

plot w_bottom = if w and ao_crossed_below_zero and ao_turned_red and ao_turned_green then low else Double.NaN;


i don't know what you want, so left it as is.
use the bubble to help you see some values and debug the formulas


Code:
# error2_macd

#https://usethinkscript.com/threads/thinkscript-expected-double-error-how-to-fix.3073/#post-123645
#post18

#wussay  #18
#can someone please assist, I get The error message "Expected double at 5:10"

#declare lower;

input fastLength = 5;
input slowLength = 34;
input oscillatorLength = 5;

input MACDLength = 9;

#def AO = MACD(fastLength, slowLength, "close").Diff;
#def AO = MACD(fastLength, slowLength, close).Diff;
def AO = MACD(fastLength, slowLength, macdlength).Diff;

def AO_EMA = ExpAverage(AO, oscillatorLength);

def l1 = low < low[1] and low[1] < low[2];
def l2 = low == Lowest(low, 10);
def l3 = low < low[2] and low[2] < low[3];
def w = l1 and l2 and l3;

def ao_crossed_below_zero = AO < 0 and AO[1] > 0;
def ao_turned_red = AO_EMA < AO_EMA[1];
def ao_turned_green = AO_EMA > AO_EMA[1];

def x = ao_crossed_below_zero and ao_turned_red and ao_turned_green;

plot w_bottom = if w and ao_crossed_below_zero and ao_turned_red and ao_turned_green then low else Double.NaN;
w_bottom.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
w_bottom.SetDefaultColor(Color.green);
w_bottom.setlineweight(3);
w_bottom.hidebubble();



#------------------

#plot z = 0;

addchartbubble(1, high,
#ao + "\n" +
#l1 + "\n" +
#l2 + "\n" +
#l3 + "\n" +
#w + "\n" +
 ao_crossed_below_zero + "\n" +
 ao_turned_red + "\n" +
 ao_turned_green + "\n" +
w_bottom 
, (if x then color.yellow else color.gray), yes);
#
 

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
503 Online
Create Post

Similar threads

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