Search…
Caching service
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.
1
# To set up a custom default start date
2
sig.env()[sig.config.DEFAULT_RS_START_DATE] = dtm.date(2009, 1, 2)
3
4
import datetime as dtm
5
6
import sigtech.framework as sig
7
8
from sigtech.framework.default_strategy_objects.single_stock_strategies import get_single_stock_strategy
9
10
if not sig.config.is_initialised():
11
env = sig.init(env_date='2022-01-30')
Copied!

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:
1
STRATEGY_SERVICE = env.strategy_service()
Copied!
Time the creation of the four reinvestment strategies with the %%time magic method:
Input
Output
1
%%time
2
3
tickers = ['AAPL', 'MSFT', 'NFLX', 'CSCO']
4
5
rs = [get_single_stock_strategy(exchange_ticker=t) for t in tickers]
6
7
rs
Copied!
1
[AAPL REINV STRATEGY <class 'sigtech.framework.strategies.reinvestment_strategy.ReinvestmentStrategy'>[139936205997264],
2
MSFT REINV STRATEGY <class 'sigtech.framework.strategies.reinvestment_strategy.ReinvestmentStrategy'>[139936205805520],
3
NFLX REINV STRATEGY <class 'sigtech.framework.strategies.reinvestment_strategy.ReinvestmentStrategy'>[139936482567888],
4
CSCO REINV STRATEGY <class 'sigtech.framework.strategies.reinvestment_strategy.ReinvestmentStrategy'>[139936205522128]]
5
6
CPU times: user 11.9 s, sys: 11.9 s, total 13.9 s
7
Wall time: 13.9s
Copied!
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
Input
Output
1
STRATEGY_SERVICE.cache_strategy(rs)
Copied!
1
['AAPL REINV STRATEGY|2022-01-30T00:00:00+00:00||41160d054edf7f0960015019d902b27e',
2
'MSFT REINV STRATEGY|2022-01-30T00:00:00+00:00||41160d054edf7f0960015019d902b27e',
3
'NFLX REINV STRATEGY|2022-01-30T00:00:00+00:00||41160d054edf7f0960015019d902b27e',
4
'CSCO REINV STRATEGY|2022-01-30T00:00:00+00:00||41160d054edf7f0960015019d902b27e']
Copied!

Retrieve cached strategies in a separate session

The environment is recreated. The env_date must be consistent with the cached strategies you want to access:
1
sig.de_init()
2
3
env = sig.init(env_date='2022-01-30')
4
5
STRATEGY_SERVICE = env.strategy_service()
Copied!
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.
Input
Output
1
appl_keys = STRATEGY_SERVICE.get_keys_by_prefix('AAPL')
2
appl_keys
Copied!
1
['AAPL REINV STRATEGY|2022-01-31T00:00:00+00:00||41160d054edf7f0960015019d902b27e',
2
'AAPL REINV STRATEGY|2022-01-30T00:00:00+00:00||41160d054edf7f0960015019d902b27e',
3
'AAPL REINV STRATEGY|2021-11-30T00:00:00+00:00||07f24d6b1ef83e37aeb5dc5443ff48aa',
4
'AAPL REINV STRATEGY|2022-01-20T00:00:00+00:00||07f24d6b1ef83e37aeb5dc5443ff48aa']
Copied!
Individual strategy objects can now be retrieved by key or name:
1
STRATEGY_SERVICE.get_strategy_by_name(f'AAPL REINV STRATEGY')
Copied!
1
STRATEGY_SERVICE..get_strategy_by_key(appl_keys[0])
Copied!
Retrieving the four reinvestment strategies is much faster than building them from scratch. Ensure the env_date matches the env_date of the cached strategies being accessed:
Input
Output
1
%%time
2
3
rs = [STRATEGY_SERVICE.get_strategy_by_name(f'{t} REINV STRATEGY') for t in tickers]
4
5
rs
Copied!
1
[AAPL REINV STRATEGY <class 'sigtech.framework.strategies.reinvestment_strategy.ReinvestmentStrategy'>[139936205997264],
2
MSFT REINV STRATEGY <class 'sigtech.framework.strategies.reinvestment_strategy.ReinvestmentStrategy'>[139936205805520],
3
NFLX REINV STRATEGY <class 'sigtech.framework.strategies.reinvestment_strategy.ReinvestmentStrategy'>[139936482567888],
4
CSCO REINV STRATEGY <class 'sigtech.framework.strategies.reinvestment_strategy.ReinvestmentStrategy'>[139936205522128]]
5
6
CPU times: user 43.7 ms, sys: 0 ns, total: 43.7 ms
7
Wall time: 141 ms
Copied!

Remove strategies from the cache

Cached objects can be removed from the service by key or name:
1
STRATEGY_SERVICE.remove_strategy_by_name(f'AAPL REINV STRATEGY')
Copied!
1
STRATEGY_SERVICE.remove_strategy_by_key(appl_keys[0])
Copied!

Strategy cache decorator

The strategy_service_cache decorator can be used to wrap a function returning a built strategy object.
  • The decorator attempts to retrieve a strategy from the strategy service.
  • If found, the strategy object is returned.
  • If not found, the wrapped function is called and the returned strategy object is cached using the Strategy Service.
1
from sigtech.framework.services.strategy_service.service import strategy_service_cache
2
3
@strategy_service_cache('AAPL REINV STRATEGY')
4
def get_appl_rs():
5
return get_single_stock_strategy(exchange_ticker='AAPL')
6
7
get_appl_rs().build()
Copied!

Caching DataFrames

The DataFrame Service is analogous to the Strategy Service, and can be used to cache and persist DataFrames.
The following example builds and caches an equity universe from the equity_universe_filter:

1. Instantiate dataframe_service

1
DATAFRAME_SERVICE = env.dataframe_service()
Copied!

2. Create & cache universe series

Input
Output
1
%%time
2
3
universe = sig.EquityUniverseFilter('DAX INDEX')
4
5
universe = universe.apply_filter(dtm.date(2022, 1, 4))
Copied!
1
2022-01-04 [1225253.SINGLE_STOCK.TRADABLE, 1324073.SINGLE...
2
dtype: object
3
4
CPU times: user 43.7 ms, sys: 0 ns, total: 43.7 ms
5
Wall time: 141 ms
Copied!
The DataFrame is cached using the DataFrame Service's put method:
1
DATAFRAME_SERVICE.put('DAX 20220104', universe)
Copied!

3. Restart session & load pre-built universe

1
sig.de_init()
2
3
env = sig.init(env_date='2022-01-30')
4
5
DATAFRAME_SERVICE = env.dataframe_service()
Copied!
All cached DataFrames can be queried by prefix:
Input
Output
1
DATAFRAME_SERVICE.keys('')
Copied!
1
['DAX 20220104',
2
'SPX UNIVERSE 2008']
Copied!
The prebuilt universe is retrieved with the get method:
Input
Output
1
%%time
2
universe = DATAFRAME_SERVICE.get('DAX 20220104')
Copied!
1
CPU times: user 6.71 ms, sys: 640 µs, total: 7.35 ms
2
Wall time: 52.1 ms
Copied!
DataFrames can equally be removed from the cache:
1
DATAFRAME_SERVICE.delete('DAX 20220104')
Copied!
Like the Strategy Service, the dataframe_service_cache decorator can wrap a function and return a built DataFrame:
1
from sigtech.framework.services.strategy_service.service import dataframe_service_cache
2
3
@dataframe_service_cache('DAX 20220104')
4
def get_universe():
5
return universe
6
7
get_universe()
Copied!
In this example, the DataFrame is saved with the {name}_{env.asofdate} key.