Attribute Error: 'int' object has no attribute 'set'
I am trying to modify the sizer within my strategy and I am using the code exactly as stated in the docs from backtrader, but I get the error:
File "anaconda3\lib\site-packages\backtrader\strategy.py", line 1449, in setsizer sizer.set(self, self.broker) AttributeError: 'int' object has no attribute 'set'
I've tried to trouble shoot, but the documentation is not really much help as to what this error could be. How do I change my sizer based on a variable passed to the strategy class?
Here is my code
import backtrader as bt import datetime import math class buynhold_1(bt.Strategy): params = ( ('start_date', None), ('end_date', None), ('sizer', None), ) def __init__(self): self.dataclose = self.datas.close() if self.p.sizer is not None: self.sizer = self.p.sizer
and here is how I am calling my code
cerebro.adddata(data) # Set our desired cash start cerebro.broker.setcash(1000.0) # Add strategy to Cerebro cerebro.addstrategy(buynhold_1, start_date = datetime.datetime(2016,1,17), end_date = datetime.datetime(2017,1,20), sizer = 2 ) #add the sizer #cerebro.addsizer(bt.sizers.SizerFix, stake=2) if __name__ == '__main__': # Run Cerebro Engine start_portfolio_value = cerebro.broker.getvalue() cerebro.run()
Soham Jain last edited by
I guess you are confused between
sizeris a python class that comes with the backtrader module.
sizeis the quantity that you want to buy during a specific
In order to derive the size to be bought logically, you must create a sub class that inherits from
import backtrader as bt class FixedSize(bt.Sizer): params = (('stake', 1),) def _getsizing(self, comminfo, cash, data, isbuy): return self.params.stake
something like this...