Backtrader中文笔记之Fillers

Fillers

填充

The backtrader broker simulation has a default strategy when it comes to using volume for order execution:

当使用成交量来执行订单时,backtrader-broker模拟有一个默认策略:

  • Ignore volume
  • 忽略量

This is based on 2 premises:

这基于两个前提:

  • Trade in markets liquid enough to fully absorb buy/sell orders in one go

  • 交易市场的流动性足以一次性完全吸收买卖订单
  • Real volume matching requires a real wolrd

  • 真实的匹配量需要一个真实的环境
  • A quick example is a Fill or Kill order. Even down to the tick resolution and with enough volume for a fill, the backtrader broker cannot know how many extra actors happen to be in the market to discriminate if such an order would be or would not be matched to stick to the Fill part or if the order should be Kill

  • 一个简单的例子是填充或终止定单。即使是在勾数决议和有足够的交易量来填补,回交易者经纪人也不知道市场上有多少额外的参与者能够辨别出这样一个指令是会匹配还是不匹配以坚持填充部分,或者该指令是否应该被杀掉

But the broker can accept Volume Fillers which determine how much of the volume at a given point in time has to be used for order matching.

但是券商【经纪人】能够接收成交量填充器,它决定在给定的时间点,有多少成交量必须用于定单匹配。

The fillers signature

A filler in the backtrader ecosystem can be any callable which matches the following signature:

backtrader生态系统中的填充器可以是匹配以下签名的任何可调用的:

callable(order, price, ago)

Where:

  • order is the order which is going to be executed

  • order是要执行的命令
  • This object gives access to the data object which is the target of the operation, creation sizes/prices, execution prices/sizes/remaining sizes and other details

  • 此对象允许访问作为操作目标的数据对象、创建大小/价格、执行价格/大小/剩余大小和其他详细信息
  • price at which the order is going to be executed

  • 执行订单的价格
  • ago is the index to the data in the order in which to look for the volume and price elements

    In almost all cases this will be 0 (current point in time) but in a corner case to cover Close orders this may be -1

    To for example access the bar volume do:

  • barvolume = order.data.volume[ago]
    

    The callable can be a function or for example an instance of a class supporting the __call__ method, like in:

  • class MyFiller(object):
        def __call__(self, order, price, ago):
            pass
    
  • Adding a Filler to the broker

  • The most straightforward method is to use the set_filler:
  • import backtrader as bt
    
    cerebro = Cerebro()
    cerebro.broker.set_filler(bt.broker.fillers.FixedSize())
    

    The second choice is to completely replace the broker, although this is probably only meant for subclasses of BrokerBack which have rewritten portions of the functionality:

  • import backtrader as bt
    
    cerebro = Cerebro()
    filler = bt.broker.fillers.FixedSize()
    newbroker = bt.broker.BrokerBack(filler=filler)
    cerebro.broker = newbroker
    

    The sample

  • The backtrader sources contain a sample named volumefilling which allows to test some of the integrated fillers (initially all)

Reference

class backtrader.fillers.FixedSize()

Returns the execution size for a given order using a percentage of the volume in a bar.

This percentage is set with the parameter perc

Params:

  • size (default: None) maximum size to be executed. The actual volume of the bar at execution time is also a limit if smaller than the size

    If the value of this parameter evaluates to False, the entire volume of the bar will be used to match the order

class backtrader.fillers.FixedBarPerc()

Returns the execution size for a given order using a percentage of the volume in a bar.

This percentage is set with the parameter perc

Params:

  • perc (default: 100.0) (valied values: 0.0 - 100.0)

    Percentage of the volume bar to use to execute an order

class backtrader.fillers.BarPointPerc()

Returns the execution size for a given order. The volume will be distributed uniformly in the range high-low using minmov to partition.

From the allocated volume for the given price, the perc percentage will be used

Params:

  • minmov (default: 0.01)

    Minimum price movement. Used to partition the range high-low to proportionally distribute the volume amongst possible prices

  • perc (default: 100.0) (valied values: 0.0 - 100.0)

    Percentage of the volume allocated to the order execution price to use for matching

原文地址:https://www.cnblogs.com/sidianok/p/13705813.html