Search…
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:
1
import sigtech.framework as sig
2
import datetime as dtm
3
import pandas as pd
4
5
sig.init();
Copied!

Define instruments, time period, and RollingFutureStrategy function

1
my_instruments = [
2
'C COMDTY FUTURES GROUP', # corn
3
'S COMDTY FUTURES GROUP', # soybeans
4
'SB COMDTY FUTURES GROUP' # sugar
5
]
6
7
my_instruments = [sig.obj.get(i) for i in my_instruments]
8
9
my_start = dtm.date(2019, 1, 1)
10
my_end = dtm.date(2021, 12, 31)
11
12
# Define function for creating multiple RollingFutureStrategies
13
def create_rfs(instrument):
14
rfs = sig.RollingFutureStrategy(
15
start_date=my_start,
16
end_date=my_end,
17
currency = instrument.currency,
18
contract_code = instrument.contract_code,
19
contract_sector = instrument.contract_sector,
20
rolling_rule='F_0'
21
)
22
return rfs.name
Copied!

Add the RollingFutureStrategy instances to a dictionary

1
basket = {}
2
3
# Populate the dictionary with the chosen instruments:
4
for instrument in my_instruments:
5
basket[instrument.asset] = create_rfs(instrument)
Copied!

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:
1
# List to store signals for all assets
2
all_signals = []
3
4
# Loop through all provided assets
5
for key, strategy_name in basket.items():
6
7
# Duplicate the instrument's history Series and reset its values
8
signals = sig.obj.get(strategy_name).history()
9
signals.values[:] = 0
10
11
# Assign signals as per your strategy
12
signals.loc[signals.index.month <= 4] = -1
13
signals.loc[signals.index.month >= 8] = 1
14
15
# Convert the signal Series into a DataFrame
16
# Give it the same name as the corresponding RollingFutureStrategy instance
17
# Add the signal for this asset to the list of stored signals.
18
all_signals.append(signals.to_frame(strategy_name))
19
20
# Convert the list to a DataFrame and remove all NaN values
21
all_signals_df = pd.concat(all_signals, axis=1).dropna()
22
23
# View signals DataFrame
24
all_signals_df
Copied!

3. Define SignalStrategy

This step is identical to defining a single-instrument SignalStrategy, except for the reference to the new DataFrame:
1
seasonal_strategy = sig.SignalStrategy(
2
3
# Reference the multi-instrument hsignals DataFrame
4
signal_name=sig.signal_library.from_ts(all_signals_df).name,
5
6
start_date=my_start,
7
end_date=my_end,
8
currency='USD',
9
rebalance_frequency='SOM',
10
allocation_function=sig.signal_library.allocation.normalize_weights,
11
)
Copied!

4. View performance

1
seasonal_strategy.plot.performance()
Copied!

👷
Your turn: customise the strategy

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