backtrader.feeds.rollover module¶
Rollover Data Feed Module - Futures contract rollover.
This module provides the RollOver feed for automatically rolling over to the next futures contract when conditions are met.
- Classes:
RollOver: Rolls over to the next future when conditions are met.
Example
>>> data_old = bt.feeds.BacktraderCSVData(dataname='contract_old.csv')
>>> data_new = bt.feeds.BacktraderCSVData(dataname='contract_new.csv')
>>> data = bt.feeds.RollOver(data_old, data_new, checkdate=my_check_func)
>>> cerebro.adddata(data)
- class backtrader.feeds.rollover.RollOver[source]¶
Bases:
DataBaseClass that rolls over to the next future when a condition is met
Params:
checkdate(default:None)This must be a callable with the following signature:
Checkdate(dt, d):
Where:
dtis adatetime.datetimeobjectdis the current data feed for the active future
Expected Return Values:
True: as long as the callable returns this, a switchover can happen to the next future
If a commodity expires on the 3rd Friday of March, checkdate could return True for the entire week in which the expiration takes place.
False: the expiration cannot take place
# This parameter is a callable object checkdate(dt,d), where dt is a time object, d is current active data, # If return value is True, will switch to next contract; if False, will not switch to next contract
checkcondition(default:None)Note: This will only be called if
checkdatehas returnedTrueIf
Nonethis will evaluate toTrue(execute roll over) internallyElse this must be a callable with this signature:
Checkcondition(d0, d1)
Where:
d0is the current data feed for the active futured1is the data feed for the next expiration
Expected Return Values:
True: roll-over to the next future
Following with the example from
checkdate, this could say that the roll-over can only happen if the volume fromd0is already less than the volume fromd1False: the expiration cannot take place
# When checkdate returns True, this function will be called, this must be a callable object, checkcondition(d0,d1) # Where d0 is current active futures contract, d1 is next expiring contract, if True, will switch from d0 to d1, if not, switch will not happen.
- __init__(*args, **kwargs)¶
Initialize a LineMultiple instance.
Sets up the internal state for managing multiple lines, including line type indicator, lines collection, clock reference, and line iterator tracking.
- Initializes:
_ltype: Line type indicator (None for base LineMultiple). lines: Collection of line objects (creates if not exists). _clock: Clock reference for synchronization. _lineiterators: Dictionary tracking registered lineiterators. _minperiod: Minimum period requirement (defaults to 1).
- start()[source]¶
Start the RollOver data feed.
Initializes all data feeds for rollover functionality.
- frompackages = ()¶
- packages = ()¶