Check Hold Period
How can I check how long a stock has been held for?
len(self.broker.getposition(d))however this doesn't appear to give the hold days...
@cwse I've done the following to accomplish this.
def notify_trade(self, trade): self.htrade = trade
self.maxdays = ((self.data0.datetime.datetime() - self.htrade.open_datetime()).days) >= self.p.limitdays
I am using code snippet from examples:
if order.status in [order.Completed, order.Margin]: self.bar_executed = len(self)
if self.position: pos_length = len(self) - self.bar_executed + 1
Tradeinstances carries an attribute which can help:
Which records the length of the data when opening happens from the point of view of the strategy (
Checking the difference between
trade.baropenand the current length of the data
len(self.data)gives the holding period.