Closed vladisld closed 3 years ago
Are we sure we are not referring to self.position
? Your code above is working with 'self.position'.
The code works ok if BackBroker is used IIRC.
I feel like I'm missing something. in both ib and backbrokers, self.position
works fine. self.positions
returns object in both. Am I looking at the wrong thing? What are we hoping to get back from self.positions
vs. self.position
?
"self.positions returns object in both" Are you positive about it? The test I've attached shows otherwise
Hey @vladisld Here's the code I'm running:
class TestPositions(bt.Strategy):
params = (("pos", None,),)
def __init__(self):
self.traded = False
self.pos = self.position if self.p.pos == "position" else self.positions
def next(self):
if not self.traded:
self.buy()
print(f"{datetime.datetime.now()} position: {self.pos}")
self.traded = True
def run(broker=None, pos=None):
cerebro = bt.Cerebro()
if broker == "backbroker":
data = bt.feeds.GenericCSVData(
dataname="data/2006-day-001.txt",
dtformat=("%Y-%m-%d"),
timeframe=bt.TimeFrame.Days,
compression=1,
)
cerebro.adddata(data)
elif broker == 'ib':
cerebro = bt.Cerebro(live=True)
store = IBStore(host='127.0.0.1', port=7497, clientId=***)
cerebro.setbroker(store.getbroker())
data = store.getdata(dataname='EUR.USD-CASH-IDEALPRO',
historical=False,
backfill=False,
backfill_start=False)
cerebro.adddata(data)
cerebro.addstrategy(TestPositions, pos=pos)
cerebro.run()
if __name__ == '__main__':
run(broker="ib", pos="positions")
For both ib and backbroker outputs are:
for self.position
2021-03-23 18:17:15.258129 position: --- Position Begin
- Size: 3
- Price: 3.68721665
- Price orig: 3.68721665
- Closed: 0
- Opened: 0
- Adjbase: None
--- Position End
for self.positions
in ib:
Server Version: 76
TWS Time at connection:20210323 18:18:47 EST
Traceback (most recent call last):
File "20210323 ib_broker position error.py", line 48, in <module>
run(broker="ib", pos="positions")
File "20210323 ib_broker position error.py", line 45, in run
cerebro.run()
File "/home/runout/.local/lib/python3.8/site-packages/backtrader/cerebro.py", line 1127, in run
runstrat = self.runstrategies(iterstrat)
File "/home/runout/.local/lib/python3.8/site-packages/backtrader/cerebro.py", line 1217, in runstrategies
strat = stratcls(*sargs, **skwargs)
File "/home/runout/.local/lib/python3.8/site-packages/backtrader/metabase.py", line 88, in __call__
_obj, args, kwargs = cls.doinit(_obj, *args, **kwargs)
File "/home/runout/.local/lib/python3.8/site-packages/backtrader/metabase.py", line 78, in doinit
_obj.__init__(*args, **kwargs)
File "20210323 ib_broker position error.py", line 10, in __init__
self.pos = self.position if self.p.pos == "position" else self.positions
File "/home/runout/.local/lib/python3.8/site-packages/backtrader/lineseries.py", line 461, in __getattr__
return getattr(self.lines, name)
AttributeError: 'Lines_LineSeries_LineIterator_DataAccessor_Strateg' object has no attribute 'positions'
For self.positions
in backbroker.
2021-03-23 18:20:27.807886 position: defaultdict(<class 'backtrader.position.Position'>, {<backtrader.feeds.csvgeneric.GenericCSVData object at 0x7f4b37f54b80>: <backtrader.position.Position object at 0x7f4b37f54bb0>})
Is there a case where self.positions
is an attribute?
Not sure I understand your question, but in all other brokers (including BackBrocker) the positions
attribute is defined as:
self.positions = collections.defaultdict(Position)
Not sure I understand your question, but in all other brokers (including BackBrocker) the
positions
attribute is defined as:self.positions = collections.defaultdict(Position)
My misunderstanding. thanks.
Did we ever figure anything out on this?
Probably I'm not following. What is it that needs to be figured out?
The problem seems to be clear, as well as a fix. Just need to do it, no?
I added in the offending line to fix the error, I even pushed. But I realized while this stops the error, we are not really getting results for the call. I'm not familiar with IBPy will try to find some time on the weekend to include the methods necessary to return positions.
I reverted the commit.
Also having the same issue. I personally use this to check if I hold any position in all stocks. I cant use position
because it only returns the position of the data0
.
Community Forum Discussion: https://community.backtrader.com/topic/3637/self-positions-throws-attributeerror-when-cerebro-broker-set-to-ibbroker
Description
When trying to access the self.positions attribute from within the next method of a strategy, the following exception is thrown in case IBBroker is setup as a broker:
Analysis
It seems the property 'positions' is not defined in
IBBroker
class, while defined in all other broker implementations. This properly is defined inIBStore
class instead. The fix seems to be easy: just add the properly toIBBroker
class which will just return theIBStore.positions
propertyTest case