写入器#

将以下内容写入流中:

  • 带有数据源、策略、指标和观察者的 csv 流

    可以使用每个对象的 csv 属性来控制实际写入 csv 流的对象(默认为 True 表示 数据源观察者False 表示 指标

  • 概述

    • 数据源

    • 策略(线条和参数)

    • 指标/观察者(线条和参数)

    • 分析器(参数和分析结果)

只定义了一个名为 WriterFile 的写入器,可以将其添加到系统中:

  • 通过将 cerebro 的 writer 参数设置为 True

    将创建一个标准的 WriterFile 实例

  • 通过调用 Cerebro.addwriter(writerclass, **kwargs) 添加写入器 writerclass 在回测执行期间将使用给定的 kwargs 实例化。

由于标准的 WriterFile 默认不会输出 csv ,下面的 addwriter 调用会处理这个问题:

cerebro.addwriter(bt.WriterFile, csv=True)

参考#

class backtrader.WriterFile(*args, **kwargs)#

The system wide writer class.

It can be parametrized with:

  • out (default: sys.stdout): output stream to write to

    If a string is passed a filename with the content of the parameter will be used.

    If you wish to run with sys.stdout while doing multiprocess optimization, leave it as None, which will automatically initiate sys.stdout on the child processes.

  • close_out (default: False)

    If out is a stream whether it has to be explicitly closed by the writer

  • csv (default: False)

    If a csv stream of the data feeds, strategies, observers and indicators has to be written to the stream during execution

    Which objects actually go into the csv stream can be controlled with the csv attribute of each object (defaults to True for data feeds and observers / False for indicators)

  • csv_filternan (default: True) whether nan values have to be purged out of the csv stream (replaced by an empty field)

  • csv_counter (default: True) if the writer shall keep and print out a counter of the lines actually output

  • indent (default: 2) indentation spaces for each level

  • separators (default: ['=', '-', '+', '*', '.', '~', '"', '^', '#'])

    Characters used for line separators across section/sub(sub)sections

  • seplen (default: 79)

    total length of a line separator including indentation

  • rounding (default: None)

    Number of decimal places to round floats down to. With None no rounding is performed