Comment on page
5. Single-instrument signal strategy
To build on your RollingFutureStrategy, add signals to take long or short positions during different time periods.
The first few code blocks are very close to what you already used in the previous two tutorials:
import sigtech.framework as sig
import datetime as dtm
my_instrument = sig.obj.get("KC COMDTY FUTURES GROUP")
my_start = dtm.date(2019, 1, 4)
my_end = dtm.date(2021, 12, 31)
my_rfs = sig.RollingFutureStrategy(
RollingFutureStrategyinstance has a history, in the format of a simple table of dates and values:
# View your RollingFutureStrategy instance's history
# Make a copy of the history Series
signals = my_rfs.history().copy()
# Reset its values to 0
signals.values[:] = 0
# View it
signalsseries you've just created has all the same dates as the history of your
RollingFutureStrategy, but the values are now reset to 0. In the next step you will repopulate the values with signals—indicators of what position to take.
A signal of
1indicates a 100% long position, while a signal of
-1indicates a 100% short position. Set a signal of
-1for the first four months of each year in the time period, and
1for the final four months of each year, assuming there is a strategic rationale for this relating to seasonal market activity:
# short position for first 4 months of each year
signals.loc[signals.index.month <= 4] = -1
# long position for last 4 months of each year
signals.loc[signals.index.month >= 8] = 1
# view new values
For future steps to work,
signalsneeds to be in DataFrame format and have the exact same name as your
RollingFutureStrategyinstance. This name-sharing allows the
SignalStrategyinstance to reference the relevant
# Convert pandas Series to pandas Dataframe
# and give it the same name as your RollingFutureStrategy instance
signals = signals.to_frame(my_rfs.name)
# View the dataframe
seasonal_strategy = sig.SignalStrategy(
# Reference the signals DataFrame
# This also tells the SignalStrategy which underyling strategy to use
# (the RollingFutureStrategy instance) as they have identical names
# Note that this means only the signals for the start of the month matter.
# All the other signal values are irrelevant.
# Set allocation function to normalize number of short and long positions
# (hold the same number of long and short positions at any given time).
# Use .? to view other possible arguments for the SignalStrategy building block.
Try a different set of signals.