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.

示例

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

class backtrader.writer.WriterBase[源代码]

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

基类: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).

示例

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

Initialize the WriterFile instance.

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

参数:

**kwargs -- Keyword arguments for writer parameters.

start()[源代码]

Initialize the writer at the start of execution.

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

stop()[源代码]

Close the output stream when execution ends.

Closes the output file if close_out parameter is True.

next()[源代码]

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

Add column headers for CSV output.

参数:

headers -- List of header names to add.

addvalues(values)[源代码]

Add values to be written to CSV output.

参数:

values -- Iterable of values to add.

writeiterable(iterable, func=None, counter='')[源代码]

Write an iterable to the output as a CSV line.

参数:
  • iterable -- The data to write.

  • func -- Optional function to apply to each element.

  • counter -- Optional counter value to prepend.

writeline(line)[源代码]

Write a single line to the output.

参数:

line -- The line content to write.

writelines(lines)[源代码]

Write multiple lines to the output.

参数:

lines -- Iterable of line contents to write.

writelineseparator(level=0)[源代码]

Write a separator line for visual formatting.

参数:

level -- Indentation level that determines separator style.

writedict(dct, level=0, recurse=False)[源代码]

Write a dictionary to the output with formatting.

参数:
  • dct -- Dictionary to write.

  • level -- Indentation level for nested output.

  • recurse -- Whether this is a recursive call.

class backtrader.writer.WriterStringIO[源代码]

基类: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.

示例

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

Initialize the WriterStringIO instance.

Creates a new StringIO buffer for storing output.

参数:

**kwargs -- Keyword arguments for writer parameters.

property out

Always return our StringIO object.

stop()[源代码]

Seek to beginning for reading.

getvalue()[源代码]

Get the content from the StringIO object.