I have been using Zipline for some time now and could highly benefit from being able to pickle a backtest in order to be able to resume it later. The idea is to save the state of the trading algorithm and update it when new data become available. I started pickling some attributes I could think of but forgot some others and was therefore wondering if anyone had an easy solution to do that. Best, Vincent
PS: I tried updating the portfolio with those few lines. It goes ok but more attributes need to be overwritten.
if self.load_former_ptf:
for k, v in context.former_portfolio.__dict__.items():
self.TradingAlgorithm.portfolio.__setattr__(k, v)
updPositionDict = {}
for p in context.former_portfolio.positions.values():
formerDelta = p.amount*p.last_sale_price
newSid = context.symbol(p.sid.symbol)
newPrice = data[newSid].price
newQuantity = int(formerDelta/newPrice)
# portfolio should be made of positions instead of plain dict
updPositionDict.update({newSid:{'amount':newQuantity, 'cost_basis':p.cost_basis,
'last_sale_date':p.last_sale_price, 'last_sale_price':newPrice,
'sid':newSid}})
self.TradingAlgorithm.portfolio.positions = updPositionDict
self.load_former_ptf = False