Backtrader中文笔记之Sizers Reference

Sizers Reference

FixedSize

class backtrader.sizers.FixedSize()

This sizer simply returns a fixed size for any operation. Size can be controlled by number of tranches that a system wishes to use to scale into trades by specifying the tranches parameter.

此sizer只返回任何操作的固定大小。大小可以通过指定tranches参数,由系统希望用来扩展到交易中的份额数量来控制。

Params:

* `stake` (default: `1`)

* `tranches` (default: `1`)

# 部分源码
params = (('stake', 1),
              ('tranches', 1))

    def _getsizing(self, comminfo, cash, data, isbuy):
        if self.p.tranches > 1:
            return abs(int(self.p.stake / self.p.tranches))
        else:
            return self.p.stake

 

FixedReverser

class backtrader.sizers.FixedReverser()

This sizer returns the needes fixed size to reverse an open position or the fixed size to open one

这个sizer返回需要的固定大小来反转打开的位置,或者返回固定的大小来开仓

  • To open a position: return the param stake

  • To reverse a position: return 2 * stake

Params:

* `stake` (default: `1`)
params = (('stake', 1),)

    def _getsizing(self, comminfo, cash, data, isbuy):
        position = self.strategy.getposition(data)
        size = self.p.stake * (1 + (position.size != 0))
        return size

 

PercentSizer

class backtrader.sizers.PercentSizer()

This sizer return percents of available cash

这个sizer将返回现金可用百分比购买

Params:

* `percents` (default: `20`)
    params = (
        ('percents', 20),
        ('retint', False),  # return an int size or rather the float value
    )

    def __init__(self):
        pass

    def _getsizing(self, comminfo, cash, data, isbuy):
        position = self.broker.getposition(data)
        if not position:
            size = cash / data.close[0] * (self.params.percents / 100)
        else:
            size = position.size

        if self.p.retint:
            size = int(size)

        return size

 

AllInSizer

class backtrader.sizers.AllInSizer()

This sizer return all available cash of broker

100%购买的方式

Params:

* `percents` (default: `100`)
class AllInSizer(PercentSizer):
    '''This sizer return all available cash of broker

     Params:
       - ``percents`` (default: ``100``)
     '''
    params = (
        ('percents', 100),
    )

 



PercentSizerInt

class backtrader.sizers.PercentSizerInt()

This sizer return percents of available cash in form of size truncated to an int

Params:

* `percents` (default: `20`)
class PercentSizerInt(PercentSizer):
    '''This sizer return percents of available cash in form of size truncated
    to an int

    Params:
      - ``percents`` (default: ``20``)
    '''

    params = (
        ('retint', True),  # return an int size or rather the float value
    )

 

AllInSizerInt

class backtrader.sizers.AllInSizerInt()

This sizer return all available cash of broker with the size truncated to an int

Params:

* `percents` (default: `100`)

class AllInSizerInt(PercentSizerInt):
    '''This sizer return all available cash of broker with the
    size truncated to an int

     Params:
       - ``percents`` (default: ``100``)
     '''
    params = (
        ('percents', 100),
    )

 该类属性params只要里面的内容不重名,现在来看类属性应该是继承的

posted @ 2020-09-21 23:50  就是想学习  阅读(1155)  评论(0编辑  收藏  举报