Open source quantitative framework backtrader FAQ: filler, let the order execution quantity be related to the total transaction volume

The complete backtrader technical tutorial is here

By default, if the order issued in the backtrader is to be traded, the entire quantity will be traded, regardless of the total trade volume (volumn) of the bar that was traded.

For example, at the end of the current bar, create the following market buy order:

self.buy(size=100)

Then, to the next bar, 100 shares will be traded at the opening price, even if the total trading volume of the next bar is 50 shares, it will not affect the order to trade 100 shares.

So, can we change this default behavior so that the order volume is linked to the total volume of the bar? At least not to exceed the total volume. There is a way, and that is to use the order fulfillment object filler . There are three types of fillers, which are introduced below.

 

(1) Fixed number of filler: FixedSize

The method of use is as follows (assuming that a buy order for 100 shares is created in the strategy self.buy(size=100), and the total trading volume of the following two bars is 90 and 80 respectively):

...
filler = bt.broker.fillers.FixedSize(size=70)
cerebro.broker.set_filler(filler)
cerebro.run()

Then the actual number of orders on a bar (no matter buy or sell orders) = min (size, the total volume of the bar, the remaining number of orders)

In this case, the order may be executed several times separately. The order will trade 70 shares on the first bar and 30 shares on the second bar.

If the filler parameter size is set to False, then the equivalent size is the total trading volume of the bar, 90 shares are traded in the first bar and 10 shares are traded in the second bar.

(2) Filler with fixed bar percentage: FixedBarPerc

The method of use is as follows (assuming that a buy order for 100 shares is created in the strategy self.buy(size=100), and the total trading volume of the following two bars is 90 and 80 respectively):

... 
filler = bt.broker.fillers.FixedBarPerc(perc=70) # 最多执行bar总成交量的70%
cerebro.broker.set_filler(filler) cerebro.run()

Then the actual transaction quantity of an order on a bar (regardless of a buy order or a sell order) = min (the total transaction volume of the bar*perc/100, the remaining quantity of the order)

Then 63 shares (90*70%) were traded on the first bar and 37 shares were traded on the second bar.

(3) Filler of bar point percentage: BarPointPerc

The method of use is as follows (assuming that a buy order for 100 shares is created in the strategy self.buy(size=100), and the total trading volume of the following two bars is 90 and 80 respectively):

... 
filler = bt.broker.fillers.BarPointPerc(minmov=0.1, perc=70)
cerebro.broker.set_filler(filler) cerebro.run()

 

I didn't read the document too clearly for this filler. The document says: minmov is the minimum price change unit, The volume (order volume) will be distributed uniformly in the range high - low  using minmovto partition.

BarPointPerc: does a uniform distribution of the bar volume across the price range high-low and uses a percentage of the volume that would correspond to a single price point。

Everyone is welcome to discuss how this BarPointPerc filler executes orders.

 

Finally, the filler can also be customized for some complex scenarios, refer to the documentation .

Guess you like

Origin blog.csdn.net/qtbgo/article/details/111057196