Hi all,

As title says, the MA crossover algo seems to create closing orders that are slightly larger than my open position. I then get the following error:

  1. Order Error: id: 1, Insufficient buying power to complete order (Value:-12.052), Reason: Your portfolio holds 9.1908 ADA, 0 ADA of which are reserved for open orders, but your Sell order is for 9.2 ADA. Cash Modeling trading does not permit short holdings so ensure you only sell what you have, including any additional open orders.

 

In this case I hold 9.1908 ADAUSDC, and the sell order is for 9.2, I get the obvious error that cash accounts can't go short, which is something I understand, and am not looking to do. 

The code is as follow: 

  1. class MovingAverageCrossAlgorithm(QCAlgorithm):
  2. def Initialize(self):
  3. self.SetStartDate(2021, 1, 1)
  4. #self.SetEndDate(2021,8,1)
  5. self.SetCash('USDC',100000)
  6. self.tickers = ["BTCUSDC", "ETHUSDC", "SOLUSDC", "DOGEUSDC", "ADAUSDC"]
  7. self.symbols = [self.AddCrypto(ticker, Resolution.Minute, Market.Binance).Symbol for ticker in self.tickers]
  8. self.fast = {}
  9. self.slow = {}
  10. for symbol in self.symbols:
  11. self.fast[symbol] = self.SMA(symbol, 20, Resolution.Hour)
  12. self.slow[symbol] = self.SMA(symbol, 200, Resolution.Hour)
  13. self.SetWarmUp(200, Resolution.Hour)
  14. self.SetTimeZone("Europe/London")
  15. def OnData(self, data):
  16. if self.IsWarmingUp: return
  17. for symbol in self.symbols:
  18. holdings = self.Portfolio[symbol].Quantity
  19. #self.Plot("holdings", symbol, holdings)
  20. for symbol in self.symbols:
  21. if not self.fast[symbol].IsReady: continue
  22. if not self.slow[symbol].IsReady: continue
  23. fast = self.fast[symbol].Current.Value
  24. slow = self.slow[symbol].Current.Value
  25. #self.Plot(symbol, "fast", fast)
  26. #self.Plot(symbol, "slow", slow)
  27. if fast > slow and self.Portfolio[symbol].Quantity <= 0:
  28. self.SetHoldings(symbol, 0.8/len(self.symbols))
  29. elif fast < slow and self.Portfolio[symbol].Quantity > 0:
  30. self.Liquidate(symbol, 0.8/len(self.symbols))
+ Expand

 

At first, the last line in the code: 

  1. 'elif fast < slow and self.Portfolio[symbol].Quantity > 0:
  2. self.Liquidate(symbol, 0.8/len(self.symbols))

 

was initially: self.Liquidate(symbol). Which I suspected was the reason for the mismatch between the open and closing orders. But even after replacing the sizing criteria with an identical one to the open order, the error persists. 

All feedback welcome

Author

Mike A

December 2021