v8 Framework
Search…
⌃K

6. Multi-instrument signal strategy

Build a signal strategy that uses multiple instruments

A multi-asset signal strategy

1. Multi-instrument RollingFutureStrategy setup

The first few code blocks are very close to what you already used to create multi-instrument Rolling Future Strategy in tutorial 4.

Set up environment

Note that for this tutorial we will need the pandas library:
import sigtech.framework as sig
import datetime as dtm
import pandas as pd
sig.init();

Define instruments, time period, and RollingFutureStrategy function

my_instruments = [
'C COMDTY FUTURES GROUP', # corn
'S COMDTY FUTURES GROUP', # soybeans
'SB COMDTY FUTURES GROUP' # sugar
]
my_instruments = [sig.obj.get(i) for i in my_instruments]
my_start = dtm.date(2019, 1, 1)
my_end = dtm.date(2021, 12, 31)
# Define function for creating multiple RollingFutureStrategies
def create_rfs(instrument):
rfs = sig.RollingFutureStrategy(
start_date=my_start,
end_date=my_end,
currency = instrument.currency,
contract_code = instrument.contract_code,
contract_sector = instrument.contract_sector,
rolling_rule='F_0'
)
return rfs.name

Add the RollingFutureStrategy instances to a dictionary

basket = {}
# Populate the dictionary with the chosen instruments:
for instrument in my_instruments:
basket[instrument.asset] = create_rfs(instrument)

2. Loop to create multi-instrument DataFrame of signals

This following code is very similar to that used in the previous tutorial, except you're using a loop to create signals for multiple instruments, and combining them into a single DataFrame:
# List to store signals for all assets
all_signals = []
# Loop through all provided assets
for key, strategy_name in basket.items():
# Duplicate the instrument's history Series and reset its values
signals = sig.obj.get(strategy_name).history()
signals.values[:] = 0
# Assign signals as per your strategy
signals.loc[signals.index.month <= 4] = -1
signals.loc[signals.index.month >= 8] = 1
# Convert the signal Series into a DataFrame
# Give it the same name as the corresponding RollingFutureStrategy instance
# Add the signal for this asset to the list of stored signals.
all_signals.append(signals.to_frame(strategy_name))
# Convert the list to a DataFrame and remove all NaN values
all_signals_df = pd.concat(all_signals, axis=1).dropna()
# View signals DataFrame
all_signals_df

3. Define SignalStrategy

This step is identical to defining a single-instrument SignalStrategy, except for the reference to the new DataFrame:
seasonal_strategy = sig.SignalStrategy(
# Reference the multi-instrument hsignals DataFrame
signal_name=sig.signal_library.from_ts(all_signals_df).name,
start_date=my_start,
end_date=my_end,
currency='USD',
rebalance_frequency='SOM',
allocation_function=sig.signal_library.allocation.normalize_weights,
)

4. View performance

seasonal_strategy.plot.performance()

👷
Your turn: customise the strategy

  1. 1.
    Add additional instruments
  2. 2.
    Try a different set of signals.