How do you Convert Time?

shakib3585

Active member
VIP
Hello,

I would like to extract the opening of the very first traded bar (including pre). I have tried with a code, but it seems it's giving me in milliseconds. I want to convert it to something like 11:05. I would like to be able to extract the time and assign it to a variable "t" as it is. In this example "t" should be assigned to "1105". I do not know how to include daylight savings with it. Please help.

Thank you

Code:
def firstBarNumber = if GetDay() <> GetDay()[1] then BarNumber() else firstBarNumber[1];
def time = if BarNumber() == firstBarNumber then gettime() else time[1];
 
Hello,

I would like to extract the opening of the very first traded bar (including pre). I have tried with a code, but it seems it's giving me in milliseconds. I want to convert it to something like 11:05. I would like to be able to extract the time and assign it to a variable "t" as it is. In this example "t" should be assigned to "1105". I do not know how to include daylight savings with it. Please help.

Thank you

Code:
def firstBarNumber = if GetDay() <> GetDay()[1] then BarNumber() else firstBarNumber[1];
def time = if BarNumber() == firstBarNumber then gettime() else time[1];

unclear what you want,
you start out asking for a price.
then you mention 11:05, a time?

what do you really want?
what do you want to see on the chart?


the first what?

first bar of each day?
use this to tell when a new day starts, and read the open.
def diffday = if GetDay() != GetDay()[1] then 1 else 0;
def o = if diffday then open else o[1];

or first bar on the chart?
def x = if barnumber() == 1 then 1 else 0;
def o = if x then open else o[1];
 
unclear what you want,
you start out asking for a price.
then you mention 11:05, a time?

what do you really want?
what do you want to see on the chart?


the first what?

first bar of each day?
use this to tell when a new day starts, and read the open.
def diffday = if GetDay() != GetDay()[1] then 1 else 0;
def o = if diffday then open else o[1];

or first bar on the chart?
def x = if barnumber() == 1 then 1 else 0;
def o = if x then open else o[1];
Sorry for the confusion @halcyonguy. I would like to extract the time at the very first opening of a bar. For example, if a bar opens at 6:25 then I would like to extract that time and assign it to a variable "t" as "0625". It would be great if daylight savings is also considered. Thank you
 
Sorry for the confusion @halcyonguy. I would like to extract the time at the very first opening of a bar. For example, if a bar opens at 6:25 then I would like to extract that time and assign it to a variable "t" as "0625". It would be great if daylight savings is also considered. Thank you
I have found a code which I modified as the following @halcyonguy . My intention is to get the high, low, and close based on the first opening of the bar. I tried to sum up all the previous codes I have into the following one, but TOS says the function "SecondsTillTime" is not able to work with variables. Can you please help me out on how to modify the following code to get the target period high, low, and close? I know it's a different question now, but I do not want to duplicate a help request. Thank you.

Code:
def Daylight_Savings = yes;

def YyyyMmDd = GetYYYYMMDD();

def D = 86400;

def H = 3600;

def epoch = (GetTime() / 1000);

def hour = Floor((epoch % D) / H) + If(Daylight_Savings, -1, 0); # 4 hour diff for GMT DST else 5

def roll = if hour > 4

then hour - 4

else if hour < 4

then (hour + 24) - 4

else 0;

def min = Floor(((epoch % D) % H)) / 60;

def newDay = YyyyMmDd != YyyyMmDd[1];

def tH = if newDay then roll else tH[1];

def tM = if newDay then min else tM[1];

def a1 = if tM < 10 then 0 else tM;

def a2 = ((tH + 1) * 100) + tM;

def a3close = if a2 < 0800 then 0800 else 0900;


def startTime = a2;
def endTime = a3close;
def timeUntilClose = SecondsTillTime(endTime);
def timeUntilOpen = SecondsTillTime(startTime);
def targetPeriod = timeUntilClose > 0;
rec targetPeriodHigh = if targetPeriod and !targetPeriod[1] then high(period = AggregationPeriod.THREE_MIN) else if targetPeriod and high(period = AggregationPeriod.THREE_MIN) > targetPeriodHigh[1] then high(period = AggregationPeriod.THREE_MIN) else targetPeriodHigh[1];
rec targetPeriodLow = if targetPeriod and !targetPeriod[1] then low(period = AggregationPeriod.THREE_MIN) else if targetPeriod and low(period = AggregationPeriod.THREE_MIN) < targetPeriodLow[1] and low(period = AggregationPeriod.THREE_MIN) > 0 then low(period = AggregationPeriod.THREE_MIN) else targetPeriodLow[1];

def na = Double.NaN;
def start = a2;
def end = a3close;
def period =  ( SecondsFromTime(start) >= 0 and SecondsTillTime(end) > 0 );

def cls = if newDay then 0
else if !period then cls[1]
else close;
 
Sorry for the confusion @halcyonguy. I would like to extract the time at the very first opening of a bar. For example, if a bar opens at 6:25 then I would like to extract that time and assign it to a variable "t" as "0625". It would be great if daylight savings is also considered. Thank you

still confusing
the first of something implies there are several.
there is only 1 open for each bar.
so, no idea what you are after.
first of day? first on chart?

i try to ask specific questions, to try to figure out what people want. i (we) can't help if you don't answer them.

what do you really want?
what are you going to do with this time? display it in a bubble?

what do you want to see on the chart?
please answer this one. it usually results in guiding the person in a different direction than the method they are trying.
 
I have found a code which I modified as the following @halcyonguy . My intention is to get the high, low, and close based on the first opening of the bar. I tried to sum up all the previous codes I have into the following one, but TOS says the function "SecondsTillTime" is not able to work with variables. Can you please help me out on how to modify the following code to get the target period high, low, and close? I know it's a different question now, but I do not want to duplicate a help request. Thank you.

Code:
def Daylight_Savings = yes;

def YyyyMmDd = GetYYYYMMDD();

def D = 86400;

def H = 3600;

def epoch = (GetTime() / 1000);

def hour = Floor((epoch % D) / H) + If(Daylight_Savings, -1, 0); # 4 hour diff for GMT DST else 5

def roll = if hour > 4

then hour - 4

else if hour < 4

then (hour + 24) - 4

else 0;

def min = Floor(((epoch % D) % H)) / 60;

def newDay = YyyyMmDd != YyyyMmDd[1];

def tH = if newDay then roll else tH[1];

def tM = if newDay then min else tM[1];

def a1 = if tM < 10 then 0 else tM;

def a2 = ((tH + 1) * 100) + tM;

def a3close = if a2 < 0800 then 0800 else 0900;


def startTime = a2;
def endTime = a3close;
def timeUntilClose = SecondsTillTime(endTime);
def timeUntilOpen = SecondsTillTime(startTime);
def targetPeriod = timeUntilClose > 0;
rec targetPeriodHigh = if targetPeriod and !targetPeriod[1] then high(period = AggregationPeriod.THREE_MIN) else if targetPeriod and high(period = AggregationPeriod.THREE_MIN) > targetPeriodHigh[1] then high(period = AggregationPeriod.THREE_MIN) else targetPeriodHigh[1];
rec targetPeriodLow = if targetPeriod and !targetPeriod[1] then low(period = AggregationPeriod.THREE_MIN) else if targetPeriod and low(period = AggregationPeriod.THREE_MIN) < targetPeriodLow[1] and low(period = AggregationPeriod.THREE_MIN) > 0 then low(period = AggregationPeriod.THREE_MIN) else targetPeriodLow[1];

def na = Double.NaN;
def start = a2;
def end = a3close;
def period =  ( SecondsFromTime(start) >= 0 and SecondsTillTime(end) > 0 );

def cls = if newDay then 0
else if !period then cls[1]
else close;

i will look at this code to try to figure out what your words are not describing.
 
i will look at this code to try to figure out what your words are not describing.
I would request to have the following. For example, if a ticker starts at 05:25 then I would want to extract that time and assign that to "a2" as "525". Then based off this start time time, I would have "a3close". Now, between " a2" and "a3close", I would want to have high, low and close price of that stock @halcyonguy. Thank you so much
 
I would request to have the following. For example, if a ticker starts at 05:25 then I would want to extract that time and assign that to "a2" as "525". Then based off this start time time, I would have "a3close". Now, between " a2" and "a3close", I would want to have high, low and close price of that stock @halcyonguy. Thank you so much
I have come up with a code, but it seems it's not catching the highest high, lowest low and close within the target period. Since time function is causing problems, I used barnumber() to track. Can you please help? @halcyonguy


Code:
def Daylight_Savings = yes;

def YyyyMmDd = GetYYYYMMDD();

def D = 86400;

def H = 3600;

def epoch = (GetTime() / 1000);

def hour = Floor((epoch % D) / H) + If(Daylight_Savings, -1, 0); # 4 hour diff for GMT DST else 5

def roll = if hour > 4

then hour - 4

else if hour < 4

then (hour + 24) - 4

else 0;

def min = Floor(((epoch % D) % H)) / 60;

def newDay = YyyyMmDd != YyyyMmDd[1];

def tH = if newDay then roll else tH[1];

def tM = if newDay then min else tM[1];


def a21 = ((tH + 1) * 100) + tM;

def a3close = if a21 < 600 then 600 else 930;

def c11 = 600;

def c22 = 930;

def c111 = if secondstillTime(c11)==0 then barnumber() else c111[1];

def c222 = if secondstillTime(c22)==0 then barnumber() else c222[1];

def bno = if getday()<>getday()[1] then barnumber() else bno[1];

def bnc = if a3close<=c11 then c111 else c222;

def tih = if barnumber()>=bno and barnumber()<=bnc then high(period = AggregationPeriod.THREE_MIN) else tih[1];

def til = if barnumber()>=bno and barnumber()<=bnc then low(period = AggregationPeriod.THREE_MIN)  else til[1];

def tic = if barnumber()>=bno and barnumber()<=bnc then close(period = AggregationPeriod.THREE_MIN) else tic[1];
 

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