Framework v8
Search
K

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.
import datetime as dtm
import sigtech.framework as sig
from sigtech.framework.default_strategy_objects.single_stock_strategies import get_single_stock_strategy
if not sig.config.is_initialised():
env = sig.init(env_date='2022-01-30')
# To set up a custom default start date
sig.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:
Input
Output
%%time
tickers = ['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
Input
Output
STRATEGY_SERVICE.cache_strategy(rs)
['AAPL REINV STRATEGY|2022-01-30T00:00:00+00:00||41160d054edf7f0960015019d902b27e',
'MSFT REINV STRATEGY|2022-01-30T00:00:00+00:00||41160d054edf7f0960015019d902b27e',
'NFLX REINV STRATEGY|2022-01-30T00:00:00+00:00||41160d054edf7f0960015019d902b27e',
'CSCO REINV STRATEGY|2022-01-30T00:00:00+00:00||41160d054edf7f0960015019d902b27e']

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:
sig.de_init()
env = sig.init(env_date='2022-01-30')
STRATEGY_SERVICE = env.strategy_service()
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
appl_keys = STRATEGY_SERVICE.get_keys_by_prefix('AAPL')
appl_keys
['AAPL REINV STRATEGY|2022-01-31T00:00:00+00:00||41160d054edf7f0960015019d902b27e',
'AAPL REINV STRATEGY|2022-01-30T00:00:00+00:00||41160d054edf7f0960015019d902b27e',
'AAPL REINV STRATEGY|2021-11-30T00:00:00+00:00||07f24d6b1ef83e37aeb5dc5443ff48aa',
'AAPL REINV STRATEGY|2022-01-20T00:00:00+00:00||07f24d6b1ef83e37aeb5dc5443ff48aa']
Individual strategy objects can now be retrieved by key or name:
STRATEGY_SERVICE.get_strategy_by_name(f'AAPL REINV STRATEGY')
STRATEGY_SERVICE.get_strategy_by_key(appl_keys[0])
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
%%time
rs = [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:
STRATEGY_SERVICE.remove_strategy_by_name(f'AAPL REINV STRATEGY')
STRATEGY_SERVICE.remove_strategy_by_key(appl_keys[0])

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.
from sigtech.framework.services.strategy_service.service import strategy_service_cache
@strategy_service_cache('AAPL REINV STRATEGY')
def get_appl_rs():
return get_single_stock_strategy(exchange_ticker='AAPL')
get_appl_rs().build()

Caching Objects

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

1. Initiate object_service

OBJECT_SERVICE = env.object_service()

2. Create & cache universe series

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

3. Restart session & load pre-built universe

sig.de_init()
env = sig.init(env_date='2022-01-30')
OBJECT_SERVICE = env.object_service()
All cached objects can be queried by prefix:
Input
Output
OBJECT_SERVICE.keys('')
['DAX 20220104',
'SPX UNIVERSE 2008']
The prebuilt universe is retrieved with the get method:
Input
Output
%%time
universe = OBJECT_SERVICE.get('DAX 20220104')
CPU times: user 6.71 ms, sys: 640 µs, total: 7.35 ms
Wall time: 52.1 ms
Objects can equally be removed from the cache:
OBJECT_SERVICE.delete('DAX 20220104')
Like the Strategy Service, the object_service_cache decorator can wrap a function and return a built object:
from sigtech.framework.services.strategy_service.service import object_service_cache
@object_service_cache('DAX 20220104')
def get_universe():
return universe
get_universe()
In this example, the object is saved with the {name}_{env.asofdate} key.
© 2023 SIG Technologies Limited