Attribute Error: 'int' object has no attribute 'set'
-
Hello,
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[0].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()
-
Hey Carson,
I guess you are confused betweensizer
andsize
.
sizer
is a python class that comes with the backtrader module.
size
is the quantity that you want to buy during a specificself.buy()
call.In order to derive the size to be bought logically, you must create a sub class that inherits from
bt.Sizer
base class.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...
More on
bt.Sizers
available here.