• Get $40 off VIP by signing up for a free account! Sign Up

Want to define market time elapsed percentage: "def var = AsPercent((secondsPassed) / (secondsPassed+secondsLeft)" but get error "expected double"

Glefdar

Active member
Hi All,

What I want to do is pretty simple: I want to define a variable as the percentage of the trading session (from 9:30am to 4:00pm) that has already elapsed at the current time. So for example, the trading session is 6.5 hours and so at 12:45pm, 50% of the trading day (3 hours and 15 minutes) has elapsed, so I want the value "50% to be returned at 12:45pm.

This is the code I am trying:

Code:
input OpenTime = 0930;
input CloseTime = 1600;
def secondsPassed = SecondsFromTime(OpenTime);
def secondsLeft = SecondsTillTime(CloseTime);
def TimeElapsedPercent = AsPercent((secondsPassed) / (secondsPassed+secondsLeft));

Seems really simple, right? So I don't understand why I'm getting "Expected double" error when defining "TimeElapsedPercent" that way. Does anyone have insight they'd kindly share into how to fix this, or how to do what I'm trying to do?

EDIT: I'm just going to give up on making TOS convert the value into a percentage, and for display purposes will format the label this way whenever I'll show the percentage on a label (based on the example of how XeoNoX showed a percentage in a label for his volume forecast indicator):

Code:
input OpenTime = 0930;
input CloseTime = 1600;
def secondsPassed = SecondsFromTime(OpenTime);
def secondsLeft = SecondsTillTime(CloseTime);
def TimeElapsed = ((secondsPassed) / (secondsPassed+secondsLeft));

AddLabel (yes, if secondsPassed <= 0 then "% Time: NA" else "% Time: " + round(TimeElapsed*100,0), CreateColor(150,150,150));
 
Last edited:
Solution
Hi All,

What I want to do is pretty simple: I want to define a variable as the percentage of the trading session (from 9:30am to 4:00pm) that has already elapsed at the current time. So for example, the trading session is 6.5 hours and so at 12:45pm, 50% of the trading day (3 hours and 15 minutes) has elapsed, so I want the value "50% to be returned at 12:45pm.

This is the code I am trying:

Code:
input OpenTime = 0930;
input CloseTime = 1600;
def secondsPassed = SecondsFromTime(OpenTime);
def secondsLeft = SecondsTillTime(CloseTime);
def TimeElapsedPercent = AsPercent((secondsPassed) / (secondsPassed+secondsLeft));

Seems really simple, right? So I don't understand why I'm getting "Expected double" error when defining...
Hi All,

What I want to do is pretty simple: I want to define a variable as the percentage of the trading session (from 9:30am to 4:00pm) that has already elapsed at the current time. So for example, the trading session is 6.5 hours and so at 12:45pm, 50% of the trading day (3 hours and 15 minutes) has elapsed, so I want the value "50% to be returned at 12:45pm.

This is the code I am trying:

Code:
input OpenTime = 0930;
input CloseTime = 1600;
def secondsPassed = SecondsFromTime(OpenTime);
def secondsLeft = SecondsTillTime(CloseTime);
def TimeElapsedPercent = AsPercent((secondsPassed) / (secondsPassed+secondsLeft));

Seems really simple, right? So I don't understand why I'm getting "Expected double" error when defining "TimeElapsedPercent" that way. Does anyone have insight they'd kindly share into how to fix this, or how to do what I'm trying to do?

EDIT: I'm just going to give up on making TOS convert the value into a percentage, and for display purposes will format the label this way whenever I'll show the percentage on a label (based on the example of how XeoNoX showed a percentage in a label for his volume forecast indicator):

Code:
input OpenTime = 0930;
input CloseTime = 1600;
def secondsPassed = SecondsFromTime(OpenTime);
def secondsLeft = SecondsTillTime(CloseTime);
def TimeElapsed = ((secondsPassed) / (secondsPassed+secondsLeft));

AddLabel (yes, if secondsPassed <= 0 then "% Time: NA" else "% Time: " + round(TimeElapsed*100,0), CreateColor(150,150,150));

Aspercent converts numbers to a string and is used in labels and bubbles. The error message was indicating that a number was expected, but got a string.

Here is one way to use it in your label:

Ruby:
input OpenTime = 0930;
input CloseTime = 1600;
def secondsPassed = SecondsFromTime(OpenTime);
def secondsLeft = SecondsTillTime(CloseTime);
def TimeElapsed = Round((secondsPassed) / (secondsPassed + secondsLeft), 2);

AddLabel (yes, if secondsPassed <= 0 then "% Time: NA" else "Time: " + AsPercent(TimeElapsed), CreateColor(150, 150, 150));
 
Solution

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
258 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