Hi, this has had me stumped for awhile now, this is just a basic algorithm using a liquid universe; the issue I'm facing is that sometimes orders are getting cancelled before they get filled, and I can't figure out what the trigger is, so far it seems to be happening randomly.
for security in self.changes.RemovedSecurities:
if security.Invested:
self.Liquidate(security.Symbol)
for security in self.changes.AddedSecurities:
if not security.Invested:
self.SetHoldings(security.Symbol, 0.05)
The algorithm first liquidates securities that have been removed from the universe and then sends orders for securities that have been added to the universe. What's happening is the algorithm cancels the liquidation orders before sending the buy orders(I think). What's strange is this doesn't happen every time; I tried it with a 5 symbol universe, and this issue was basically non-existent over a 10 year period, but when I try a 20 symbol universe, the issue started popping up a lot within the same backtest period, no idea if there's a correlation there.
I figure I'm missing something very obvious..
Derek Melchin
Hi Vncne,
The problem arises because the securities in `self.changes` don't change until OnSecuritiesChanged is called again on the next universe change.
To resolve this, we can define OnSecuritiesChanged to be
def OnSecuritiesChanged(self, changes): self.added = changes.AddedSecurities self.removed = changes.RemovedSecurities
Then, after trading in OnData, we empty these lists
def OnData(self, data): if self.IsWarmingUp: return for security in self.removed: if security.Invested: self.Liquidate(security.Symbol) self.removed = [] for security in self.added: if not security.Invested and security.Symbol not in self.symbols: self.SetHoldings(security.Symbol, 0.05) self.added = []
See the attached backtest and plot for reference.
Best,
Derek Melchin
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Vncne
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!