How to Use secondsFromTime() and secondsTillTime() Functions

CHOZEN

New member
2019 Donor
I want to utilize SecondsFromTime and SecondsTillTime in thinkscript to write a code to place in my scanner that will only scan premarket from 4am - 9:30 am.

Found this example:

Code:
# The following statement will be true any time AFTER 9:30 am
def Aftr930 = secondsFromTime(930) > 0;
 
Solution
Search code here that uses SecondsFromTime and SecondsTillTime. You may need to adjust for your timezone.

I use a variable in all my scripts like

Code:
input timeOffsetFromEST = -300; #hint timeOffsetFromEST: 24-hour time offset from EST (-300 for PST)

and then

Code:
startTime = 0630 - timeOffsetFromEST;
endOfTradingTime = 1315 - timeOffsetFromEST;

and then maybe something like

Code:
def isActive = if SecondsTillTime(endTime) > 0 and SecondsFromTime(startTime) >= 0
                         then 1
                         else 0;

def atEndTime = if secondsTillTime(endTime) == 0
                then 1
                else 0;

korygill

Active member
VIP
Search code here that uses SecondsFromTime and SecondsTillTime. You may need to adjust for your timezone.

I use a variable in all my scripts like

Code:
input timeOffsetFromEST = -300; #hint timeOffsetFromEST: 24-hour time offset from EST (-300 for PST)

and then

Code:
startTime = 0630 - timeOffsetFromEST;
endOfTradingTime = 1315 - timeOffsetFromEST;

and then maybe something like

Code:
def isActive = if SecondsTillTime(endTime) > 0 and SecondsFromTime(startTime) >= 0
                         then 1
                         else 0;

def atEndTime = if secondsTillTime(endTime) == 0
                then 1
                else 0;
 
Solution

JML

New member
This study plots the opening 9:30 EST price but keeps plotting till the next day at 9:30. I would like to stop plotting at 4:15EST. Can anyone help with this? thanks

Code:
input showOnlyToday = YES;
input Market_Open_Time = 0931;
input Market_Close_Time = 1615;
input Market_Close_Time2 = 1600;

def OpeningBell = if SecondsTillTime(Market_Open_Time)[1] > 0 and SecondsTillTime(Market_Open_Time) <= 0 or (SecondsTillTime(Market_Open_Time)[1] < SecondsTillTime(Market_Open_Time) and SecondsTillTime(Market_Open_Time)[1] > 0)  then 1 else 0;

def day = GetDay();
def lastDay = GetLastDay();
def isToday = If(day == lastDay, 1, 0);
def shouldPlot = If(showOnlyToday and isToday, 1, If(!showOnlyToday, 1, 0));

def pastOpen = If((SecondsTillTime(Market_Open_Time) > 0), 0, 1);
def pastClose = If((SecondsTillTime(Market_Close_Time) > 0), 0, 1);
def pastClose2 = If((SecondsTillTime(Market_Close_Time2) > 0), 0, 1);
def marketOpen = If(pastOpen and !pastClose, 1, 0);
def firstBar = If (day[1] != day, 1, 0);

def closingBell = if SecondsTillTime(Market_Close_Time)[1] > 0 and SecondsTillTime(Market_Close_Time) <= 0 or (SecondsTillTime(Market_Close_Time)[1] < SecondsTillTime(Market_Close_Time) and SecondsTillTime(Market_Close_Time)[1] > 0)  then 1 else 0;

def closingBell2 = if SecondsTillTime(Market_Close_Time2)[1] > 0 and SecondsTillTime(Market_Close_Time2) <= 0 or (SecondsTillTime(Market_Close_Time2)[1] < SecondsTillTime(Market_Close_Time2) and SecondsTillTime(Market_Close_Time2)[1] > 0)  then 1 else 0;

rec regHoursHigh = If(high > regHoursHigh[1] and marketOpen, high, If(marketOpen and !firstBar, regHoursHigh[1], high));
rec regHoursLow = If(low < regHoursLow[1] and marketOpen, low, If(marketOpen and regHoursLow[1] > 0 and !firstBar, regHoursLow[1], low));

rec runningClose = CompoundValue(1, if closingBell then close[1] else runningClose[1], close);
rec prevClose = CompoundValue(1, if closingBell  then runningClose else prevClose[1], close);

#rec runningClose2 = compoundValue(1, if closingBell2 then close[1] else runningClose2[1], close);
#rec prevClose2 = compoundValue(1, if closingBell2  then #runningClose2 else prevClose2[1], close);

rec prevHigh = CompoundValue(1, if closingBell then regHoursHigh[1] else prevHigh[1], high);
rec prevLow = CompoundValue(1, if closingBell then regHoursLow[1] else prevLow[1], low);

plot pc = if shouldPlot then prevClose else Double.NaN;
pc.SetStyle(Curve.SHORT_DASH);
pc.SetDefaultColor(Color.WHITE);

#plot pc2 = if shouldPlot then prevClose2 else double.nan;
#pc2.SetStyle(curve.SHORT_DASH);
#pc2.setDefaultColor(color.pink);

plot pl = if shouldPlot then prevLow else Double.NaN;
pl.SetDefaultColor(Color.DARK_RED);
pl.SetStyle(Curve.SHORT_DASH);

plot ph = if shouldPlot then prevHigh else Double.NaN;
ph.SetStyle(Curve.SHORT_DASH);
ph.SetDefaultColor(Color.DARK_GREEN);

rec runningOpen = CompoundValue(1, if OpeningBell then open[1] else runningOpen[1], open);
rec prevOpen = CompoundValue(1, if OpeningBell  then runningOpen else prevOpen[1], open);
plot Open = if shouldPlot then prevOpen else Double.NaN;

Open.SetStyle(Curve.SHORT_DASH);
Open.SetDefaultColor(Color.YELLOW);
 
Last edited:

easyman

New member
VIP
I want to setup scan to run ONLY after the market closes (are say at 3 PM PST). Is it possible (all the gettime and other functions are not allowing it)

Thanks unfortunately ... it's not working..I want to add a code so my scanner can run ONLY after after market ... and only once ... so i don't get multiple alerts :)

Here is the code i took.. all of them comes as ZERO only weird... ii changed the time to earlier to run... hmmm. nothing.

Code:
def startofRunTime = 1611;
def endOfTradingTime = 1645 ;

def isActive = if SecondsTillTime(endOfTradingTime) > 0 and SecondsFromTime(startofRunTime) >= 0
then 1
else 0;
 

BlueRaven

New member
I'm looking to figure the RTH hours ATR but on a extended hours chart for futures...

I know you can have thinkscript look at only certain times, but I'm not sure how to or if it's possible to only have it look at RTH timeframe over a series of days.

I'll work on this some and see if I can figure it out, but I figured I would ask here as if anyone knows it would be someone on here.

Thanks.
 

sobiswas

Member
VIP
So I wrote this for using timeFrame for having diff plot in the scanner. I used this in the scanner right now, and I was expecting 0 stocks as SecondsFromTime(OpenTime) should be greater than 28800 by now. But the scanner just plotted all stocks with change from open > 2.5% or < -2.5%. Any tips what I am doing wrong?

Code:
#9:30 to 10:30 2.5%
#10:30 to 13:30 3.5%
#13:30 to 16:00 4.5%
def OpenTime = 0930;
def x = round(100*((close/open)-1),2);
plot y = if SecondsFromTime(OpenTime) >= 0 and SecondsFromTime(OpenTime) < 3600 then (if x >= 2.5 then 1 else if x <= -2.5 then 1 else 0) else if SecondsFromTime(OpenTime) >= 3600 and SecondsFromTime(OpenTime) < 14400 then (if x >= 3.5 then 1 else if x <= -3.5 then 1 else 0) else if SecondsFromTime(OpenTime) >= 14400 and SecondsFromTime(OpenTime) < 28800 then (if x >= 4.5 then 1 else if x <= -4.5 then 1 else 0) else 0;
 

korygill

Active member
VIP
What is this supposed to do @sobiswas ? close and open are for each BAR this code runs on. did you want something that is looking at prev day close to current (open) price? If so, maybe look at close(period = Aggregation.DAY) and open.

Also, instead of calling SecondsFromTime(OpenTime), do it once with def sft = SecondsFromTime(OpenTime);

Might also consider using some different spacing to keep your if/else more readable.

I came up with this that shows % Change from previous day close. Maybe this helps...

Code:
declare lower;
declare once_per_bar;

#9:30 to 10:30 2.5%
#10:30 to 13:30 3.5%
#13:30 to 16:00 4.5%
def OpenTime = 0930;
def prevClose = close(period=AggregationPeriod.DAY)[1];
def x = ((open-prevClose)/prevClose) * 100.0;
def sft = SecondsFromTime(OpenTime);

# DEBUGGING plots
#
#plot pSFT = sft;
plot pX =x;

#plot y = if sft >= 0 and sft < 3600 then
#            (if x >= 2.5 then 1 else if x <= -2.5 then 1 else 0)
#        else if sft >= 3600 and sft < 14400 then
#            (if x >= 3.5 then 1 else if x <= -3.5 then 1 else 0)
#        else if sft >= 14400 and sft < 28800 then
#            (if x >= 4.5 then 1 else if x <= -4.5 then 1 else 0)
#        else 0;

plot zero = 0;
plot one = 1;
plot two = 2;
plot three = 3;
plot none = -1;
plot ntwo = -2;
plot nthree = -3;

AddChartBubble(
    sft == 0,
    0,
    prevClose,
    Color.Gray,
    Yes);

Tested with VLO which is around 50 so I could see 1pt change equal 2% so I could check my math.

diNc8BH.png
 

sobiswas

Member
VIP
@korygill

Let me explain a bit better

The following code finds the % change in the value of a stock from open of the same day with aggregation period set to DAY in my scanner. I have some other filters in the scan like average_vol and sizzle index.

Code:
def x = round(100*((close/open)-1),2);
plot y = if x >= 4 then 1 else if x <= -4 then 1 else 0;

What I was trying to do is, based on time after market opens, the % which is >=4 or <= -4 should change.

From
9:30 to 10:30 it should be 2.5%
10:30 to 13:30 it should be 3.5%
13:30 to 16:00 it should be 4.5%

yTDCNvB.png
 

Similar threads

Top