RollingFutureStrategy#
-
class sigtech.framework.strategies.rolling_future_strategy.RollingFutureStrategy
Baseclasses:
RollingStrategyBase
Subclasses:
RollingLMEFutureStrategy
,RollingFutureFXHedgedStrategy
,DynamicRollingFutureStrategy
Class implementing rolling future strategies potentially rolled over multiple days.
Keyword arguments:
contract_code
: Contract code for futures contract group to trade.contract_sector
: Contract sector, e.g.'INDEX'
,'COMDTY'
, for futures contract group to trade.contract_offset
: Offset to add to roll table, e.g. 1 for first month, 2 for second month, etc. Optional, defaults to zero meaning no offset from the raw roll table.rolling_rule
: Available options can be found fromRollingFutureStrategy.available_rolling_rules()
. Ifcustom_rolling_table
is passed,rolling_rule
will be either'custom_roll'
or'custom_table'
according to the type passed.custom_roll_table
: Allows passing of a custom roll table. Thecustom_roll_table
can be of two types :'roll_table'
is a list of strings of month codes to hold with key 0 (this year) or 1 (next year), e.g.['H[0]', 'K[0]', 'K[0]', 'N[0]', 'N[0]', 'U[0]', 'U[0]', 'X[0]', 'X[0]', 'F[1]', 'F[1]', 'H[1]']
where the columns are Jan-Dec and correspond to the month during which to hold the said month code. E.g.F[1]
in the 10th column means the strategy will hold next year’s Jan contract in October of this year. This will setrolling_rule
to'custom_roll'
.'rolling_table'
is a DataFrame where the index is a datetime index and the columns are'rolled_in_contract'
,'rolled_out_contract'
,'rolled_in_weight'
,'rolled_out_weight'
and'first_execution_date'
. This will setrolling_rule
to'custom_table'
.
front_offset
: If usingfront
asrolling_rule
, this parameter specifies number of business days before first delivery notice date (or expiry date for cash settled futures) to start and finish the roll. Note that there are two syntax:x,y
include y in the offset range, e.g.-5,-3
will roll a third of the contract each of the 3 days from 4th to 2nd day before contract expiry (-1 means on expiry date).x:y
exclude the end of the range, e.g.-5:-3
will roll half the contracts 4 days before expiry and roll the other half 3 days before contract expiry (indexed one away from -3). This syntax will be deprecated in v10.
Use
front_offset_from_expiry=True
if you want to specify front offset from expiry date regardless of first delivery notice date. Ifrolling_rule
is not'front'
or'custom_roll'
, this argument is ignored.front_offset_from_expiry
: Specify to use expiry date as contract expiry date regardless of first notice day.month_end_offset
: Only required ifrolling_rule
set toprev_month
. Defines how many business days before end of month to start and finish the roll (identical syntax tofront_offset
).monthly_roll_days
: Defines which business day of the month to roll the contracts on. Only required ifrolling_rule
not set tofront
orprev_month
, defaults toNone
. IfNone
, will default to5:9
unlessrici
rule is used (seesigtech.framework.schedules.roll_schedules.methods.rolling_rici
for more information). Note that5,9
syntax is available for that field and will become the norm.roll_on_calendar_days
: Optional, defaults toFalse
. If True,front_offset
,month_end_offset
andmonthly_roll_days
will use calendar days instead of business days to compute the rolling dates. Rolling dates will still be adjusted.leverage
: Optional, defaults to1
.transaction_type
:'outright'
or'roll'
(default).order_type
:'group'
,'split'
or'auto'
.dv01_exposure
: Optional, only applicable to interest rate futures and bond futures, determines the fixed DV01 exposure as a percentage of theinitial_cash
.custom_roll_override
: Pass a dict {year: roll schedule} to overridecustom_roll_table
for particular years, otherwisecustom_roll_table
applies. This will setrolling_rule
to'custom_roll'
.rolling_approach
: Either'weight'
(default) or'contracts'
. If'weight'
the roll maintains the full previous contract value into the new contract, considering the difference in prices. Otherwise, ifcontracts
, the strategy initially fully invests in the first contract and rolls exactly this many units into subsequent contracts.fixed_contracts
: Optional, overwrite default behaviour and trade a fixed number of contracts throughout the strategy. If set, the strategy initially trades intofixed_contracts
contracts and rolls exactly this many into subsequent contracts. Otherwise, ifNone
(default), the strategy will fully invest in the initial contract and will roll into subsequent contracts as determined byrolling_approach
.run_with_future_check
: Optional, boolean flag to activate a check on the futures held in the strategy. This is de-activated by default.size_from_first_roll_trade
: Optional, boolean flag to size all roll trades from the first trade of the roll. This is activated by default. The alternative sizes each trade individually.trade_out_end
: If true, closes all positions on last trading day of strategy.allow_roll_adjustments
: If set to False, rolling adjustments to accommodate specific contract expirations are not allowed. If adjustments are allowed, warnings explaining the nature of the adjustments will be provided. Default is True.
Example object creation:
import datetime as dtm rfs = sig.RollingFutureStrategy( currency='USD', start_date=dtm.date(2016, 1, 4), end_date=dtm.date(2021, 11, 30), contract_code='ES', contract_sector='INDEX', rolling_rule='front', front_offset='-3,-2' )
Roll tables are generated using helper functions implementing the different roll rules. Method
build_decisions()
runs through each entry in the roll table and converts it to the format expected by the strategy API.Each row in the roll table corresponds to a roll date and consists of:
['rolled_out_contract', 'rolled_in_contract', 'rolled_out_weight', 'rolled_in_weight', 'first_execution_date']
Example rows for a roll over multiple days:
>>> date(2010, 3, 1) -> ['LPH10 COMDTY', 'LPJ10 COMDTY', '0.8', '0.2', date(2010, 3, 1)] >>> date(2010, 3, 2) -> ['LPH10 COMDTY', 'LPJ10 COMDTY', '0.6', '0.4', date(2010, 3, 1)] >>> date(2010, 3, 3) -> ['LPH10 COMDTY', 'LPJ10 COMDTY', '0.4', '0.6', date(2010, 3, 1)] >>> date(2010, 3, 4) -> ['LPH10 COMDTY', 'LPJ10 COMDTY', '0.2', '0.8', date(2010, 3, 1)] >>> date(2010, 3, 5) -> ['LPH10 COMDTY', 'LPJ10 COMDTY', '0.0', '1.0', date(2010, 3, 1)]
The required format for the strategy API is:
>>> rfs.add_position_target(decision_dt, instrument_name, weight, execution_dt, size_date, unit_type='WEIGHT')
instrument_name
,weight
andexecution_dt
are directly taken from the roll table entries (roll date ->execution_dt
).decision_dt
is calculated fromfirst_execution_date
using an API function. The same applies for the size date.In case of rolls over multiple days the size date will be the same on the roll in and roll out, i.e. one size date per future contract.
-
allow_roll_adjustments: Optional[bool]
-
contract_code: str
-
contract_offset: Optional[int]
-
contract_sector: str
-
currency: Optional[str]
-
custom_roll_override: Optional[dict]
-
custom_roll_table: Optional[Union[DataFrame, Series, list[str]]]
-
dv01_exposure: Optional[float]
-
fixed_contracts: Optional[float]
-
front_offset: Optional[str]
-
front_offset_from_expiry: Optional[bool]
-
property holidays
List of known holiday calendars.
-
leverage: Optional[float]
-
month_end_offset: Optional[str]
-
monthly_roll_days: Optional[str]
-
order_type: Optional[Literal['group', 'split', 'auto']]
-
roll_on_calendar_days: Optional[bool]
-
rolling_approach: Optional[Literal['weight', 'contracts']]
-
rolling_rule: Optional[str]
-
property rolling_table
Strategy roll table.
-
run_with_future_check: Optional[bool]
-
size_from_first_roll_trade: Optional[bool]
-
trade_out_end: Optional[bool]
-
transaction_type: Optional[str]
-
static available_rolling_rules() list[str]
Utility to quickly show the available rolling rules. See
RollSchedule
for more details.- Returns:
List of strings of rolling rule keywords to specify roll table.
-
bd_schedule()
Business days schedule for the strategy.
-
build_rolling_table()
Builds the strategy’s rolling table.
- Returns:
Built rolling table as a pandas DataFrame.
-
static build_weight_df(rolling_table, history_schedule, start_date, end_date)
Build a dataframe of weights where future contracts are the columns.
-
static calc_adj_offset(rolling_table, weight_df, back_adjustment_type, start_date, end_date, underlying_history)
Build a series of adjustment offset.
-
check_futures_valid(dt)
Check whether futures we have are not expiring while held in portfolio.
- Parameters:
dt – Reference datetime.
-
clean_up_margins(dt)
Schedule margin clean-up after we buy futures every day.
- Parameters:
dt – Reference datetime.
See also
sigtech.framework.strategies.strategy.Strategy.add_margin_cleanup()
-
earliest_start_date()
Earliest possible date where all dependencies can be computed.
-
get_rf_price_index(back_adjusted: Optional[bool] = None, back_adjustment_type: Optional[str] = None)
Returns an RFPriceIndex object with same parameters as strategy.
- Parameters:
back_adjusted – If True, calculate the back-adjusted price index by eliminating the price difference between the rolled-out and rolled-in contracts (default False unless back_adjustment_type is specified).
back_adjustment_type – If back_adjusted is true, user can choose between calculating the back adjustment by difference (‘diff’) or by ratio (‘ratio’) (default ‘diff’).
- Returns:
RFPriceIndex object.
-
handle_split(dt, ratio: float)
Handles an individual split event by adjusting the positions in the portfolio.
- Parameters:
dt – Reference datetime.
ratio – The split ratio to apply to the current positions.
-
implied_roll_yield(start=None, end=None)
Returns data frame with index equal to strategy dates but not including roll dates.
- Parameters:
start – Start date.
end – End date.
- Returns:
pandas DataFrame.
-
implied_roll_yield_offset(front_offset, back_offset, method, start=None, end=None)
Return a DataFrame with index equal to strategy dates.
- Parameters:
front_offset – Front offset.
back_offset – Back offset.
method – Roll method, should be one of:
'ROLL_METHOD_F_0'
,'ROLL_METHOD_NON_DELIVERY'
,'ROLL_METHOD_FRONT'
.start – Start date.
end – End date.
- Returns:
pandas DataFrame.
-
implied_spot_price(start=None, end=None)
Return the implied spot price timeseries.
- Parameters:
start – Start date.
end – End date.
- Returns:
pandas Series.
-
price_series(start=None, end=None, value_before_roll=False, back_adjusted=None, back_adjustment_type=None)
Return the price timeseries.
- Parameters:
start – Start date.
end – End date.
value_before_roll – At the rolls dates use the value of the contract rolled out.
back_adjusted – If True, calculate the back-adjusted price index by eliminating the price difference between the rolled-out and rolled-in contracts (default False unless back_adjustment_type is specified).
back_adjustment_type – If back_adjusted is true, user can choose between calculating the back adjustment by difference (‘diff’) or by ratio (‘ratio’) (default ‘diff’).
- Returns:
pandas Series.
-
realized_roll_yield(d)
Return the realised roll yield on a specific date.
- Parameters:
d – Reference date.
- Returns:
pandas DataFrame.
-
realized_roll_yield_full(start=None, end=None, offset=1, window_start_month=1, window_end_month=12, backward=True)
Returns data frame with index equal to strategy dates but not including roll dates and column implied_roll_yield is defined for every index date as:
\[\frac{(F_1 - F_2)}{F_1} * \frac{365}{(T_2 - T_1}\]where:
F_1 is price of previous contract to rolled_out_contract.
F_2 is price of rolled_out contract (e.g. rolled_out_contract = COZ13 COMDTY; o-> F_1 = COX13 COMDTY.
T_1 time (in calendar days) to expiration for prev rolled_out contract.
T_2 time (in calendar days) to expiration for rolled_out contract.
- Parameters:
start – Start date.
end – End date.
offset – Offset value (default is 1).
window_start_month – Starting month (default is 1).
window_end_month – End month (default is 12).
backward – Computation is made backward (default is False).
- Returns:
pandas DataFrame.
-
rolling_schedule()
Determine the dates to trade on.
-
schedule_information()
Return the schedule information for this group.
-
spot_dv01()
Returns a Series of spot_dv01 values for the underlying future that is currently part of the index
-
strategy_initialization(dt)
Initial decision run on the start date of the strategy.
- Parameters:
dt – Reference datetime.