3

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
Skandix
  • 1,916
  • 6
  • 27
  • 36
VincentH
  • 1,009
  • 4
  • 13
  • 24
  • Sharing your research helps everyone. Tell us what you've tried and why it didn’t meet your needs. This demonstrates that you’ve taken the time to try to help yourself, it saves us from reiterating obvious answers, and most of all it helps you get a more specific and relevant answer! See also: [ask] – YSelf Feb 07 '18 at 15:24
  • What objects do you want to pickle in particular? –  Feb 07 '18 at 15:26
  • @Yself: I am sorry I was not more specific. Basically I run a first backtest, dump the portfolio object. Then I try to run a second backtest, load the portfolio dumped previously and use it to update the current backtest from its previous state. Updating the portfolio is easy but it is not sufficient to resume the backtest as things were. That would require updating some other objects -like the perf_tracker- and I wonder if anyone tried before to do something like that. – VincentH Feb 07 '18 at 16:14
  • It doesn't look like many people have tried that. Can you provide a state or value of a backtest as an example? –  Feb 15 '18 at 10:33
  • @VincentH, i am trying the same thing. Can you share how did you achieve this ? – Punith Raj Nov 14 '18 at 15:13

1 Answers1

0

...wondering if anyone had an easy solution to do that.

I have not used zipline and don't have an implemented solution for what you are asking. All I can help you with is with pickling your test state.

Though from the information you've provided, I can only read that perf_tracker is what you need help with. Looking at its source there shouldn't be any problem with pickling it (correct me if I am wrong, because I haven't done it myself).

Also you can read about pickling custom objects here. Another interesting method is __repr__ which helps recreating objects from strings.

0xc0de
  • 8,028
  • 5
  • 49
  • 75