issue with self.Position.size
-
I am trying to use a strategy that buys or sells based on how many shares are currently in the position. My issue arises when I test multiple data feeds.
I have my next function iterate through each data, and for most things I want, I can specify to use the data that is currently being iterated through.
However, since self.position.size only produces the size for datas[0], when i try to reference size in subsequent datas it produces 0s. Below are the print functions I use.
print('Start Short {} {}'.format(Time(0),self.position.size)) print('Add Short {} {}'.format(Time(0),self.position.size)) print('Close {} {}'.format(Time(0),self.position.size))
Below are a sample of outputs where the first datafeed prints the size fine but the one that trades right after prints 0s for size.
Start Short 10:03:00 0 Add Short 10:08:00 -100 Close 10:46:00 -200 Start Short 09:40:00 0 Add Short 09:43:00 0 Add Short 09:47:00 0 Close 10:13:00 0
I was looking for functions in the documentation that allow you to specify which data to pull the size number from, but couldn't find any. The best I could find was self.broker.getposition() but that produces values in addition to size and i cant pull the size alone.
How do I get around this?
-
Try
self.getposition(data=your_data)
-
@ab_trader self.getposition(data= your_data) produces -
-Size: 0- Price: 0.0
- Price orig: 0.0
- Closed: 0
- Opened: 0
- Adjbase: None
This doesnt allow me to get just the size so that I can make trades on it. I want to be able to say something like :
if self.position_size(data = my_data) > 500: (some logic)
@backtrader do you have any recommendations?
-
Why can't you access the
size
? It's an attribute of the returnedPosition
instancethe_size = self.getposition(data=thedata).size
Isn't that working?
-
@backtrader that solved it!
I didnt know that .size was an attribute of getposition. If its in the docs i didnt see it.
Thanks for the help!
-
It's documented: Docs - Position
In any case Python doesn't hide the attributes and you can use the
inspect
module or simpledir
to see what's available.