Include time limit in backtesting strategy?

blindsquirrel

blindsquirrel

New member
The documentation on time and date functions in thinkscript is pretty thin.

What I need is to limit my strategies trades to Mon-Fri, 8:30 - 3:00 pm (future's market day session).
  • No trades should open before 8:30.
  • All positions should flatten at 5 min before 3:00.

Looking at example scripts that use time, I attempted implementing some code:

Python:
# Flatten is true if we're 5 min or less from end of futures trading
def startTime = 0830;;
def lastFiveMinuteBar = 1455;
def endOfTradingTime = 1500;

def flatten = (GetTime() >= lastFiveMinuteBar AND GetTime() < endOfTradingTime);
But, it doesn't seem to be working.

Is there a way to get human readable time formatting from the GetTime() function?

Any help is appreciated.
 
STB

STB

New member
I use this script for time.

Code:
###### TIME ######

input zoneStartAM = 930;
input zoneEndAM = 1200;
input zoneStartPM = 1330;
input zoneEndPM = 1600;

###### TIME END #####


AddOrder(OrderType.SELL_TO_OPEN, secondsTillTime(zoneStartAM) <= 0 && secondsTillTime(zoneEndAM) >= 0 and

AddOrder(OrderType.BUY_TO_CLOSE,  secondsTillTime(zoneEndPM) <= 0 , CLOSE[0], 1, Color.ORANGE, 
Color.RED, name = "MARKET CLOSE SHORT");
It stops trading during lunch period and closes after16:00

 
Last edited:
blindsquirrel

blindsquirrel

New member
Very helpful, thanks.

Here's my working code:

Python:
# Flatten is true if we're 20 min or less from end of futures trading
def twentyMinutesTillClose = 1640;

def flatten = SecondsFromTime(twentyMinutesTillClose) > 0 AND SecondsFromTime(twentyMinutesTillClose) < 1200;

AddOrder(OrderType.BUY_TO_CLOSE, flatten, tradeSize = contracts, tickcolor = GetColor(6), arrowcolor = GetColor(6), name="flatten");
And to prevent opening trades in last 20 min.:
Python:
AddOrder(OrderType.SELL_TO_OPEN, signal AND !flatten, tradeSize = contracts, tickcolor = GetColor(6), arrowcolor = GetColor(6));
 
blindsquirrel

blindsquirrel

New member
AddOrder(OrderType.SELL_TO_OPEN, secondsTillTime(zoneStartAM) <= 0 && secondsTillTime(zoneEndAM) >= 0 and AddOrder(OrderType.BUY_TO_CLOSE, secondsTillTime(zoneEndPM) <= 0 , CLOSE[0], 1, Color.ORANGE, Color.RED, name = "MARKET CLOSE SHORT");
BTW, I assume this isn't your actual code? If I paste it into TS editor I get errors.

Curious, because it looked like you were chaining 2 AddOrders together.
 

Top