Backtrader integration

Helper methods and classes to integrate Backtrader with Capitalgram based Pandas data.

tradingstrategy.frameworks.backtrader.convert_backtrader_timestamp(bt_time: float) pandas._libs.tslibs.timestamps.Timestamp

Convert traderader internal timestamps to Pandas.

Backtrader internally tracks time as “float of day numbers.” Go figures?

See PandasData._load() for more information.

See num2date() for more information.

class tradingstrategy.frameworks.backtrader.DEXFeed

A Pandas data feed with token metadata added.

This feed serves Backtrader Cerebro engine. It contains only raw OHLVC info.

__init__(pair_info: tradingstrategy.pair.DEXPair)
lines

alias of backtrader.lineseries.Lines_LineSeries_DataSeries_OHLC_OHLCDateTime_AbstractDataBase_DataBase_PandasData_DEXFeed

class tradingstrategy.frameworks.backtrader.DEXStrategy

A strategy base class with support for Trading Strategy DEX specific use cases.

__init__(*args, **kwargs)
last_opened_buy: Optional[backtrader.order.BuyOrder]

Currently open position

tick: Optional[int]

The next() tick counter

buy(*args, **kwargs) backtrader.order.BuyOrder

Stamps each trade with a timestamp.

Normal Backtrader does not have this functionality.

close(*args, **kwargs)

Counters a long/short position closing it

See the documentation for buy for an explanation of the parameters

Note:

  • size: automatically calculated from the existing position if not provided (default: None) by the caller

Returns: the submitted order

get_timestamp() pandas._libs.tslibs.timestamps.Timestamp

Get the timestamp of the current candle

lines

alias of backtrader.lineseries.Lines_LineSeries_LineIterator_DataAccessor_StrategyBase_Strategy_DEXStrategy

tradingstrategy.frameworks.backtrader.prepare_candles_for_backtrader(candles: pandas.core.frame.DataFrame) pandas.core.frame.DataFrame

Prepare DataFrame format so that Backtrader strategy can read it.

What assumptions Celebro.addfeed() makes about Pandas data.

tradingstrategy.frameworks.backtrader.reindex_pandas_for_backtrader(df: pandas.core.frame.DataFrame, start: pandas._libs.tslibs.timestamps.Timestamp, end: pandas._libs.tslibs.timestamps.Timestamp, bucket: tradingstrategy.timebucket.TimeBucket)

Backtrader does not allow sparse data, but all data must be filled.

Sparse data: not trades at the beginning, end of the time series. Missed trading days.

tradingstrategy.frameworks.backtrader.add_dataframes_as_feeds(cerebro: backtrader.cerebro.Cerebro, pair_universe: tradingstrategy.pair.PandasPairUniverse, datas: Iterable[pandas.core.frame.DataFrame], start: datetime.datetime, end: datetime.datetime, bucket: tradingstrategy.timebucket.TimeBucket, plot=False)

Add Pandas candle data as source feed to Backtrader strategy tester.

For each py:class:pd.DataFrame creates a new bt.Celebro.adddata() feed of the type CapitalgramFeed. Data on any missing dates is gracefully handled.

Parameters

plot – Whether Backtrader includes this series in its default plot

tradingstrategy.frameworks.backtrader.analyse_strategy_trades(trades: List[backtrader.trade.Trade]) tradingstrategy.analysis.tradeanalyzer.TradeAnalyzer

Build a trade analyzer from Backtrader executed portfolio.

class tradingstrategy.frameworks.backtrader.TradeRecorder

Record all trades during the backtest run so that they can be analysed.

create_analysis()

Meant to be overriden by subclasses. Gives a chance to create the structures that hold the analysis.

The default behaviour is to create a OrderedDict named rets

stop()

Invoked to indicate the end of operations, giving the analyzer time to shut down needed things

notify_trade(trade: backtrader.trade.Trade)

Receives trade notifications before each next cycle

get_analysis() dict

Returns a dict-like object with the results of the analysis

The keys and format of analysis results in the dictionary is implementation dependent.

It is not even enforced that the result is a dict-like object, just the convention

The default implementation returns the default OrderedDict rets created by the default create_analysis method