Time iteration

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.

import pytz
import datetime as dtm
import sigtech.framework as sig

def generate_datetime_range(start_date: dtm.date, end_date: dtm.date, freq: int = 1):
    for day in pd.bdate_range(start_date, end_date, freq="B", closed='left'):
        for t in pd.date_range("00:00", "23:59", freq="1min"):            
            yield pytz.UTC.localize(dtm.datetime.combine(day, t.time()))

class CustomIntradayStrategy(sig.Strategy):
    def __init__(self):
        super().__init__()
    
    def strategy_initialization(self):
        self.strategy_extension(self.start_date, self.end_date)
        
    def strategy_extension(self, from_dt, to_dt):        
        for dt in generate_datetime_range(from_dt, to_dt):
            self.add_method(dt, custom_trading_method)
            
    def custom_trading_method(self):
        pass

Another way to implement a time iteration is to creating a for-loop from Start to End.

class CustomIntradayStrategy(sig.Strategy):
    def __init__(self):
        super().__init__()
    
    def strategy_initialization(self):
        self.strategy_extension(self.start_date, self.end_date)
        
    def strategy_extension(self, from_dt, to_dt):        
        for dt in generate_datetime_range(from_dt, to_dt):
            self.add_method(dt, custom_trading_method)
            
    def custom_trading_method(self):
        pass