Search…
Time iteration
All strategies created on the SigTech platform has a start_date (start_dt for intraday strategies) (referred to as "Start") and end_date (end_dt for intraday strategies) (referred to as "End"). The processes (all actions taken) that forms the basis of an investment strategy needs to be scheduled within the scope of the strategy, i.e. between Start and End. To schedule processes within the scope of a strategy, the custom strategy need to iterate form Start to End - where the incremental step should be tied to the type of strategy, i.e. incrementing with one day for daily strategies and a certain amount of minutes or hours for intraday strategies.
Iterating from Start to End can be done in different ways, below are a few ways of implementing such an iteration for reference.
Below are an implementation of a Generator to facilitate the iteration. In the example the method add_method is used which is introduced in the Built-in methods, and the method strategy_extension which is introduced in Strategy extension.
1
import pytz
2
import datetime as dtm
3
import sigtech.framework as sig
4
5
def generate_datetime_range(start_date: dtm.date, end_date: dtm.date, freq: int = 1):
6
for day in pd.bdate_range(start_date, end_date, freq="B", closed='left'):
7
for t in pd.date_range("00:00", "23:59", freq="1min"):
8
yield pytz.UTC.localize(dtm.datetime.combine(day, t.time()))
9
10
11
class CustomIntradayStrategy(sig.Strategy):
12
def __init__(self):
13
super().__init__()
14
15
def strategy_initialization(self):
16
self.strategy_extension(self.start_date, self.end_date)
17
18
def strategy_extension(self, from_dt, to_dt):
19
for dt in generate_datetime_range(from_dt, to_dt):
20
self.add_method(dt, custom_trading_method)
21
22
def custom_trading_method(self):
23
pass
Copied!
Another way to implement a time iteration is to creating a for-loop from Start to End.
1
class CustomIntradayStrategy(sig.Strategy):
2
def __init__(self):
3
super().__init__()
4
5
def strategy_initialization(self):
6
self.strategy_extension(self.start_date, self.end_date)
7
8
def strategy_extension(self, from_dt, to_dt):
9
for dt in generate_datetime_range(from_dt, to_dt):
10
self.add_method(dt, custom_trading_method)
11
12
def custom_trading_method(self):
13
pass
Copied!
Export as PDF
Copy link