cvxgrp/cvxportfolio

simulator.simulate() doesn't work unless backtest() is called first

Closed this issue · 3 comments

quant5 commented

I like the idea of using simulator.simulate() standalone to run one iteration of the optimizer, i.e., for live trading.

However, without running simulator.backtest() first, initialize_policy is not invoked and a bunch of things are not instantiated (e.g., self.cache).

Suggestion to invoke policy initiation pre-backtest, or have the simulate() function invoke things if not already?

enzbus commented

Ok, good point. Currently I separated all logic that is run before a backtest from whatever is run during it: pre_evaluation does cvxpy parameter instantiation and problem compilation, values_in_time does parameters update and problem solution. (Names are not very good, will probably change.) They're both recursive (tree) traversals of the policy object. I guess what you're asking is to make the pre_evaluation done lazily so that you can use a policy without explicitly initializing it first. That's good, something would break (only one or two objects need this separation, they can just throw SyntaxError's) but most things would work. It would also probably be needed for online usage (get trades for today at 9:31am). I'll work on it. Also the policy.cache is only needed in backtest, not for online/interactive usage. Thanks!

enzbus commented

In the meantime you can do policy.pre_evaluation(universe, [pd.Timestamp.today()]) and then pass it to simulate. I guess it would work?

quant5 commented

Yes, that would work fine in the meantime - thanks. And even running one day of backtest as a dummy instantiation works in a pinch. The lazy pre-eval is a good idea, appreciate your working on the fix.