backtrader.observers.benchmark module¶
Benchmark Observer Module - Strategy vs benchmark comparison.
This module provides the Benchmark observer for tracking strategy returns against a reference asset (benchmark).
- Classes:
Benchmark: Observer that compares strategy performance to a benchmark.
Example
>>> cerebro = bt.Cerebro()
>>> data = bt.feeds.GenericCSVData(dataname='benchmark.csv')
>>> cerebro.adddata(data)
>>> cerebro.addobserver(bt.observers.Benchmark, data=data)
- class backtrader.observers.benchmark.Benchmark[source]¶
Bases:
TimeReturnThis observer stores the returns of the strategy and the return of a reference asset which is one of the datas passed to the system.
Params:
timeframe(default:None) IfNonethen the complete return over the entire backtested period will be reportedcompression(default:None)Only used for sub-day timeframes to, for example, work on an hourly timeframe by specifying “TimeFrame.Minutes” and 60 as compression
data(default:None)Reference asset to track to allow for comparison.
- note::
this data must have been added to a
cerebroinstance withaddata,resampledataorreplaydata.
_doprenext(default:False)Benchmarking will take place from the point at which the strategy kicks in (i.e.: when the minimum period of the strategy has been met).
Setting this to
Truewill record benchmarking values from the starting point of the data feedsfirstopen(default:False)Keepint it as
Falseensures that the 1st comparison point between the value and the benchmark starts at 0%, because the benchmark will not use its opening price.See the
TimeReturnanalyzer reference for a full explanation of the meaning of the parameterfund(default:None)If None, the actual mode of the broker (fundmode - True/False) will be autodetected to decide if the returns are based on the total net asset value or on the fund value. See
set_fundmodein the broker documentationSet it to
TrueorFalsefor a specific behavior
Remember that at any moment of a run the current values can be checked by looking at the lines by name at index
0.- plotlines = <backtrader.metabase.plotlines_obj object>¶
- __init__(*args, **kwargs)¶
Wrapped __init__ that properly handles observer initialization
- next()[source]¶
Update benchmark value for the current period.
Gets the benchmark return from the analyzer.
- prenext()[source]¶
Process bars before minimum period is reached.
Handles prenext phase for time tracking.
- frompackages = ()¶
- packages = ()¶