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:

%%time

tickers = ['AAPL', 'MSFT', 'NFLX', 'CSCO']

rs = [get_single_stock_strategy(exchange_ticker=t) for t in tickers]

rs

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

STRATEGY_SERVICE.cache_strategy(rs)

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.

appl_keys = STRATEGY_SERVICE.get_keys_by_prefix('AAPL')
appl_keys

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:

%%time

rs = [STRATEGY_SERVICE.get_strategy_by_name(f'{t} REINV STRATEGY') for t in tickers]

rs

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

%%time

universe = sig.EquityUniverseFilter('DAX INDEX', use_add_order=True)

universe = universe.apply_filter(dtm.date(2022, 1, 4))

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:

OBJECT_SERVICE.keys('')

The prebuilt universe is retrieved with the get method:

%%time
universe = OBJECT_SERVICE.get('DAX 20220104')

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.

Last updated

© 2023 SIG Technologies Limited