This page demonstrates SigTech's Strategy Service.The class allows you to:
Persist built strategies between sessions, reducing computational resources.
Share built strategies between users within an organisation.
The Strategy Service is a multi-level cache. It consists of an in-memory layer and an AWS S3 cloud storage layer. It employs a write-through caching policy, which pickles and writes objects to memory and storage simultaneously.
Environment
This section imports the relevant internal and external libraries, and sets up the platform environment.
When using the Strategy Service, pay close attention to their env_date.
import datetime as dtmimport sigtech.framework as sigfrom sigtech.framework.default_strategy_objects.single_stock_strategies import get_single_stock_strategyifnot sig.config.is_initialised(): env = sig.init(env_date='2022-01-30')# To set up a custom default start datesig.env()[sig.config.DEFAULT_RS_START_DATE] = dtm.date(2009, 1, 2)
Create & persist built strategy objects
Create, build and persist a set of reinvestment strategies within the strategy service. The cached strategy objects will then be retrieved in a separate session.
The strategy service can be instantiated through the environment method:
STRATEGY_SERVICE = env.strategy_service()
Time the creation of the four reinvestment strategies with the %%time magic method:
%%timetickers = ['AAPL','MSFT','NFLX','CSCO']rs = [get_single_stock_strategy(exchange_ticker=t)for t in tickers]rs
[AAPL REINV STRATEGY <class 'sigtech.framework.strategies.reinvestment_strategy.ReinvestmentStrategy'>[139936205997264],
MSFT REINV STRATEGY <class 'sigtech.framework.strategies.reinvestment_strategy.ReinvestmentStrategy'>[139936205805520],
NFLX REINV STRATEGY <class 'sigtech.framework.strategies.reinvestment_strategy.ReinvestmentStrategy'>[139936482567888],
CSCO REINV STRATEGY <class 'sigtech.framework.strategies.reinvestment_strategy.ReinvestmentStrategy'>[139936205522128]]
CPU times: user 11.9 s, sys: 11.9 s, total 13.9 s
Wall time: 13.9s
The four reinvestment strategies are now cached in the strategy service.
Note: the cached strategy objects must be built or the service will throw an error when attempting to retrieve the strategies.
Note: the cached strategies have been built up to and cached under the environment's env_date.
A key string is generated and outputted for each cached strategy using the following format:
Strategy Name | Environment Date || Environment Hash
All cached strategy objects matching a key prefix can be queried. Users have access to all strategies that have been cached within their organisation across all users.
Retrieving the four reinvestment strategies is much faster than building them from scratch. Ensure the env_datematchestheenv_date of the cached strategies being accessed:
%%timers = [STRATEGY_SERVICE.get_strategy_by_name(f'{t} REINV STRATEGY')for t in tickers]rs
[AAPL REINV STRATEGY <class 'sigtech.framework.strategies.reinvestment_strategy.ReinvestmentStrategy'>[139936205997264],
MSFT REINV STRATEGY <class 'sigtech.framework.strategies.reinvestment_strategy.ReinvestmentStrategy'>[139936205805520],
NFLX REINV STRATEGY <class 'sigtech.framework.strategies.reinvestment_strategy.ReinvestmentStrategy'>[139936482567888],
CSCO REINV STRATEGY <class 'sigtech.framework.strategies.reinvestment_strategy.ReinvestmentStrategy'>[139936205522128]]
CPU times: user 43.7 ms, sys: 0 ns, total: 43.7 ms
Wall time: 141 ms
Remove strategies from the cache
Cached objects can be removed from the service by key or name: