How to calculate average cash position during trade
-
Hello,
I am looking for some help calculating the average cash position over the length of a backtest. E.g. Imagine you are backtesting over only 5 days. The first day you have $10,000 cash, the second you have $5000 (because you spent $5000 on entering a position), and the final 3 days you have a cash position of $0. This would average out to $3000/day for the average cash position over that period of time: (10000+5000+0+0+0) / 5 = 3000
The significance is that it would tell me that an average day would have 30% of $10,000 allocated. Or in other words, 70% of my portfolio is invested in non-cash assets at any given time, on average.
Where can I find this data? Any help would be appreciated.
-
I'm presuming you are looking for post backtest analysis? I just shared this with another user. You could easily modify this to fit your needs. The dictionary self.rets would have the cash for each day.
class CashMarket(bt.analyzers.Analyzer): """ Analyzer returning cash and market values """ def create_analysis(self): self.rets = {} self.vals = 0.0 def notify_cashvalue(self, cash, value): self.vals = (cash, value) self.rets[self.strategy.datetime.datetime()] = self.vals def get_analysis(self): return self.rets