backtrader.writer module

Writer Module - Output writing for strategy execution results.

This module provides classes for writing strategy execution results to files or stdout. It supports CSV output and custom formatting.

Classes:

WriterBase: Base class for writers. WriterFile: Writes execution results to file or stdout.

Example

Using WriterFile with cerebro: >>> cerebro = bt.Cerebro() >>> cerebro.addwriter(bt.WriterFile, out=’results.csv’, csv=True) >>> results = cerebro.run()

class backtrader.writer.WriterBase[source]

Bases: ParameterizedBase

Base class for writers.

This is the base class for all writer implementations. Subclasses should override the writing methods to provide custom output formatting.

class backtrader.writer.WriterFile[source]

Bases: WriterBase

The system-wide writer class.

Writes strategy execution results to a file or stdout.

Params:
out: Output stream (default: sys.stdout). If a string is passed,

it’s treated as a filename. Use None for multiprocess optimization.

close_out: If True, explicitly close the output stream (default: False). csv: If True, write CSV data during execution (default: False). csv_filternan: If True, replace NaN values with empty fields (default: True). csvsep: CSV separator character (default: ‘,’). indent: Indentation for formatted output (default: 2).

Example

>>> cerebro.addwriter(bt.WriterFile, out='results.csv', csv=True)
params = (('out', None), ('close_out', False), ('csv', False), ('csvsep', ','), ('csv_filternan', True), ('csv_counter', True), ('indent', 2), ('separators', ['=', '-', '+', '*', '.', '~', '"', '^', '#']), ('seplen', 79), ('rounding', None))
__init__(**kwargs)[source]

Initialize the WriterFile instance.

Sets up the counter, headers list, and values list for tracking data during execution.

Parameters:

**kwargs – Keyword arguments for writer parameters.

start()[source]

Initialize the writer at the start of execution.

Opens the output file/stream and writes CSV headers if CSV mode is enabled.

stop()[source]

Close the output stream when execution ends.

Closes the output file if close_out parameter is True.

next()[source]

Write accumulated values to output.

Called during execution to write the current set of values to the CSV output and reset the values list.

addheaders(headers)[source]

Add column headers for CSV output.

Parameters:

headers – List of header names to add.

addvalues(values)[source]

Add values to be written to CSV output.

Parameters:

values – Iterable of values to add.

writeiterable(iterable, func=None, counter='')[source]

Write an iterable to the output as a CSV line.

Parameters:
  • iterable – The data to write.

  • func – Optional function to apply to each element.

  • counter – Optional counter value to prepend.

writeline(line)[source]

Write a single line to the output.

Parameters:

line – The line content to write.

writelines(lines)[source]

Write multiple lines to the output.

Parameters:

lines – Iterable of line contents to write.

writelineseparator(level=0)[source]

Write a separator line for visual formatting.

Parameters:

level – Indentation level that determines separator style.

writedict(dct, level=0, recurse=False)[source]

Write a dictionary to the output with formatting.

Parameters:
  • dct – Dictionary to write.

  • level – Indentation level for nested output.

  • recurse – Whether this is a recursive call.

class backtrader.writer.WriterStringIO[source]

Bases: WriterFile

Writer that outputs to an in-memory StringIO buffer.

This writer stores all output in memory rather than writing to a file. Useful for testing or when you need to capture output programmatically.

_stringio

The StringIO buffer holding the output.

Example

>>> writer = WriterStringIO()
>>> cerebro.addwriter(writer)
>>> results = cerebro.run()
>>> output = writer.getvalue()
params = (('out', <class '_io.StringIO'>),)
__init__(**kwargs)[source]

Initialize the WriterStringIO instance.

Creates a new StringIO buffer for storing output.

Parameters:

**kwargs – Keyword arguments for writer parameters.

property out

Always return our StringIO object.

stop()[source]

Seek to beginning for reading.

getvalue()[source]

Get the content from the StringIO object.