ThinkScript Wrong Type Cast Error

Trader1194

New member
Hello all, I am new to trying to mess with ThinkScript but I found a study that I am trying to make some small changes to. I figured out how to get the correct time to show on the chart bubbles, but I cannot get by this error when trying to add a "0" placeholder prior to any number that comes back from the minutes that is less than 10. Otherwise times like 15:02 just show up as 15:2. Any idea how to get passed this?

The error is as follows: Wrong type cast: different types after then and else: double vs class java.lang.String

Any help is really appreciated, thank you

Code:
# Identify pre-market and after-hours trading periods
input pre_market_start = 0359;
input pre_market_end = 0929;
input after_hours_start = 1559;
input after_hours_end = 2100;
def pre_market = SecondsFromTime(pre_market_start) > 0 and SecondsFromTime(pre_market_end) < 0;
def after_hours = SecondsFromTime(after_hours_start) > 0 and SecondsFromTime(after_hours_end) < 0;

# Find pre-market and after-hours highs and lows
def pre_market_low = if pre_market then low else Double.NaN;
def pre_market_high = if pre_market then high else Double.NaN;
def after_hours_low = if after_hours then low else Double.NaN;
def after_hours_high = if after_hours then high else Double.NaN;
def pm_low = LowestAll(pre_market_low);
def pm_high = HighestAll(pre_market_high);
def ah_low = LowestAll(after_hours_low);
def ah_high = HighestAll(after_hours_high);

#Example Time_Gettime
#Sleepz 20210315
#Usethinkscript request

#Time Stamp based upon Gettime and RegularTradingStart. Choose timezone
input timezone =  {default "ET", "CT", "MT", "PT"};

def starthour  = (if timezone == timezone."ET"
                        then 9
                        else if timezone == timezone."CT"
                        then 8
                        else if timezone == timezone."MT"
                        then 7
                        else 6) ;
def hour = Floor(((starthour * 60 + 30) + (GetTime() - RegularTradingStart(GetYYYYMMDD())) / 60000) / 60);
def minutes = Max(00.00, (GetTime() - RegularTradingStart(GetYYYYMMDD())) / 60000 - ((hour - starthour) * 60 + 30) + 60);
def MinutePlaceholder;
def LowMinute = minutes < 10;

# Add leading zero if minutes is less than 10
if LowMinute then {
    MinutePlaceholder = 1; } else {MinutePlaceholder = 0; } ;

# Format minutes with leading zero if necessary
def formatted_minutes = if MinutePlaceholder then Concat("0", minutes) else minutes;

AddChartBubble(pre_market and low == pm_low, pm_low, "Pre-Market Low\n" +  (hour) + ":" + formatted_minutes + "\n$" + AsPrice(pm_low), Color.RED, no);
AddChartBubble(pre_market and high == pm_high, pm_high, "Pre-Market High\n" + (hour) + ":" + formatted_minutes + "\n$" + AsPrice(pm_high), Color.GREEN, yes);
AddChartBubble(after_hours and low == ah_low, ah_low, "After-Hours Low\n" +  (hour) + ":" + formatted_minutes + "\n$" + AsPrice(ah_low), Color.RED, no);
AddChartBubble(after_hours and high == ah_high, ah_high, "After-Hours High\n" +  (hour) + ":" + formatted_minutes + "\n$" + AsPrice(ah_high), Color.GREEN, yes);
 
Solution
Hello all, I am new to trying to mess with ThinkScript but I found a study that I am trying to make some small changes to. I figured out how to get the correct time to show on the chart bubbles, but I cannot get by this error when trying to add a "0" placeholder prior to any number that comes back from the minutes that is less than 10. Otherwise times like 15:02 just show up as 15:2. Any idea how to get passed this?

The error is as follows: Wrong type cast: different types after then and else: double vs class java.lang.String

Any help is really appreciated, thank you

Code:
# Identify pre-market and after-hours trading periods
input pre_market_start = 0359;
input pre_market_end = 0929;
input after_hours_start = 1559;
input...
@Trader1194
Not at home right now to test, but you should be able to just add the zero in the string portion of the AddChartBubble based on 'minutes' being less than 10 and forgo the 'MinutePlaceholder' and 'formatted_minutes' section.

Ruby:
AddChartBubble(pre_market and low == pm_low, pm_low, if LowMinute then "Pre-Market Low\n" +  (hour) + ":" + "0" + minutes + "\n$" + AsPrice(pm_low) else "Pre-Market Low\n" +  (hour) + ":" + minutes + "\n$" + AsPrice(pm_low), Color.RED, no);
 

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

Hello all, I am new to trying to mess with ThinkScript but I found a study that I am trying to make some small changes to. I figured out how to get the correct time to show on the chart bubbles, but I cannot get by this error when trying to add a "0" placeholder prior to any number that comes back from the minutes that is less than 10. Otherwise times like 15:02 just show up as 15:2. Any idea how to get passed this?

The error is as follows: Wrong type cast: different types after then and else: double vs class java.lang.String

Any help is really appreciated, thank you

Code:
# Identify pre-market and after-hours trading periods
input pre_market_start = 0359;
input pre_market_end = 0929;
input after_hours_start = 1559;
input after_hours_end = 2100;
def pre_market = SecondsFromTime(pre_market_start) > 0 and SecondsFromTime(pre_market_end) < 0;
def after_hours = SecondsFromTime(after_hours_start) > 0 and SecondsFromTime(after_hours_end) < 0;

# Find pre-market and after-hours highs and lows
def pre_market_low = if pre_market then low else Double.NaN;
def pre_market_high = if pre_market then high else Double.NaN;
def after_hours_low = if after_hours then low else Double.NaN;
def after_hours_high = if after_hours then high else Double.NaN;
def pm_low = LowestAll(pre_market_low);
def pm_high = HighestAll(pre_market_high);
def ah_low = LowestAll(after_hours_low);
def ah_high = HighestAll(after_hours_high);

#Example Time_Gettime
#Sleepz 20210315
#Usethinkscript request

#Time Stamp based upon Gettime and RegularTradingStart. Choose timezone
input timezone =  {default "ET", "CT", "MT", "PT"};

def starthour  = (if timezone == timezone."ET"
                        then 9
                        else if timezone == timezone."CT"
                        then 8
                        else if timezone == timezone."MT"
                        then 7
                        else 6) ;
def hour = Floor(((starthour * 60 + 30) + (GetTime() - RegularTradingStart(GetYYYYMMDD())) / 60000) / 60);
def minutes = Max(00.00, (GetTime() - RegularTradingStart(GetYYYYMMDD())) / 60000 - ((hour - starthour) * 60 + 30) + 60);
def MinutePlaceholder;
def LowMinute = minutes < 10;

# Add leading zero if minutes is less than 10
if LowMinute then {
    MinutePlaceholder = 1; } else {MinutePlaceholder = 0; } ;

# Format minutes with leading zero if necessary
def formatted_minutes = if MinutePlaceholder then Concat("0", minutes) else minutes;

AddChartBubble(pre_market and low == pm_low, pm_low, "Pre-Market Low\n" +  (hour) + ":" + formatted_minutes + "\n$" + AsPrice(pm_low), Color.RED, no);
AddChartBubble(pre_market and high == pm_high, pm_high, "Pre-Market High\n" + (hour) + ":" + formatted_minutes + "\n$" + AsPrice(pm_high), Color.GREEN, yes);
AddChartBubble(after_hours and low == ah_low, ah_low, "After-Hours Low\n" +  (hour) + ":" + formatted_minutes + "\n$" + AsPrice(ah_low), Color.RED, no);
AddChartBubble(after_hours and high == ah_high, ah_high, "After-Hours High\n" +  (hour) + ":" + formatted_minutes + "\n$" + AsPrice(ah_high), Color.GREEN, yes);

as to the error,
can't assign text to a variable with def.
have to use input to assign text to a variable.
input z = "0";
but can't assign a formula with input...
so have to use a formula within the bubble function, to create the desired text. like svanoy did
 
Solution

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
362 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