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.

示例

>>> 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[源代码]

基类:DataBase

Class 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:

    • dt is a datetime.datetime object

    • d is 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 checkdate has returned True

    If None this will evaluate to True (execute roll over) internally

    Else this must be a callable with this signature:

    Checkcondition(d0, d1)
    

    Where:

    • d0 is the current data feed for the active future

    • d1 is 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 from d0 is already less than the volume from d1

  • False: 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.

islive()[源代码]

Returns True to notify Cerebro that preloading and runonce should be deactivated

__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()[源代码]

Start the RollOver data feed.

Initializes all data feeds for rollover functionality.

stop()[源代码]

Stop the RollOver data feed.

Stops all underlying data feeds.

frompackages = ()
packages = ()