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

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.
 
C

christophercttf

New member
Hi guys,

I am wondering if I can only include 9:30am - 4:00pm period when backtesting on /ES? Thank you
 
J

jcga1981

Member
Good evening, I wanted to know if there is any code to apply to a strategy that does the following:
The strategy starts when the market opens and if the closing conditions of the strategy do not exist, it closes one minute before the market closes.
Is what I request viable?
 
StockT8er

StockT8er

Member
VIP
Try this
# Positioning --------------------------------------------------------

input usetimefilter = Yes;
input rthopen = 0931;
input rthclose = 1555;
def RTH = if usetimefilter == yes then if SecondsFromTime(rthopen) >= 0 and
SecondsTillTime(rthclose) >= 0
then 1
else 0 else 1;

Then just enter RTH as one of the conditions change the time to whatever you like

Thus If RTH and other condition ect.
 
B

Bobbydigital83

New member
VIP
I want to backtest a strategy but I am confused on how you can set the code up to trade only during specific hours. I seen a few strategies presented here that do that but cannot replicate it and thought I could ask for some advice here.

Thanks!
 
rad14733

rad14733

Active member
VIP
I want to backtest a strategy but I am confused on how you can set the code up to trade only during specific hours. I seen a few strategies presented here that do that but cannot replicate it and thought I could ask for some advice here.

Thanks!
Time simply becomes part of your conditional triggers for Buys and Sells in your AddOrder() function calls... 💡
 

Similar threads

Top