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只要里面的內容不重名,現在來看類屬性應該是繼承的


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM