Using ImmediateExecutionModel() with EqualWeightingPortfolioConstructionModel() runs for about half a year, then returns:

Backtest Handled Error: You have exceeded maximum number of orders (10000), for unlimited orders upgrade your account

while total insights are only 1000-1500.

Using it with MeanVarianceOptimizationPortfolioConstructionModel() runs a few days, then returns multiple:
 

 

Backtest Handled Error: Order Error: id: 5266, Insufficient buying power to complete order (Value:7955.22), Reason: Id: 5266, Initial Margin: 7233.4281818181818181818181819, Free Margin: 17.2091834683213636363637
557

 

The logic of the algorithm is: 1. Compute something every day before market open, set a boolean to True if it works 2. Within OnData, if boolean is True then compute something and emit insights 3. Further insights are emitted only if certain conditions are met

What I've tried:

  • Created custom Optimizer class with a buffer on portfolio weights (i.e. sum of weights <= 0.95, rather than 1)
  • Custom Portfolio Construction and Optimizer classes (helpfully provided by Emilio here) but this introduces other errors which I am still in the process of debugging

Any suggestions on where else to look? I've tried re-scaling the weights in Portfolio Construction (in case the optimizer was not converging), but this didn't do anything. The ImmediateExecutionModel() source code suggests that previous holdings and open orders are taken into account when placing new trades, so I'm not quite sure where else the error may be coming from.

Edit: Previous insights should be overwritten by new insights emitted (impression is that this is true by default looking at execution models, but maybe it isn't?)

Author