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:

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: 

class MovingAverageCrossAlgorithm(QCAlgorithm):

def Initialize(self):
self.SetStartDate(2021, 1, 1)
#self.SetEndDate(2021,8,1)
self.SetCash('USDC',100000)
self.tickers = ["BTCUSDC", "ETHUSDC", "SOLUSDC", "DOGEUSDC", "ADAUSDC"]
self.symbols = [self.AddCrypto(ticker, Resolution.Minute, Market.Binance).Symbol for ticker in self.tickers]
self.fast = {}
self.slow = {}
for symbol in self.symbols:
self.fast[symbol] = self.SMA(symbol, 20, Resolution.Hour)
self.slow[symbol] = self.SMA(symbol, 200, Resolution.Hour)
self.SetWarmUp(200, Resolution.Hour)

self.SetTimeZone("Europe/London")

def OnData(self, data):
if self.IsWarmingUp: return

for symbol in self.symbols:
holdings = self.Portfolio[symbol].Quantity
#self.Plot("holdings", symbol, holdings)

for symbol in self.symbols:
if not self.fast[symbol].IsReady: continue
if not self.slow[symbol].IsReady: continue
fast = self.fast[symbol].Current.Value
slow = self.slow[symbol].Current.Value
#self.Plot(symbol, "fast", fast)
#self.Plot(symbol, "slow", slow)
if fast > slow and self.Portfolio[symbol].Quantity <= 0:
self.SetHoldings(symbol, 0.8/len(self.symbols))
elif fast < slow and self.Portfolio[symbol].Quantity > 0:
self.Liquidate(symbol, 0.8/len(self.symbols))

 

At first, the last line in the code: 

'elif fast < slow and self.Portfolio[symbol].Quantity > 0: 
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