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:
+ Expand
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
Fred Painchaud
Hi Mikey,
If you may, try/look at this one. Basically, when your fast is below your slow, for a given symbol, you want to liquidate that symbol. With liquidate, you do not have to care about if you hold it or not, or the size of your position, you just get rid of it…
Cheers,
Fred
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.
Mike A
Hi Fred,
Thanks for the feedback.
This issue is primarily concerning a live algorithm. I currently see on my ‘algorithm holdings‘ that I own few cryptos, and specifically 9.2 ADAUSDC. When I manually attempt to liquidate the position, I get the same error:
It seems the displayed holdings is different from what the ’actual’ holdings are, by actual I mean the quantity displayed in the error. The same error shows for my other crypto holdings too, and seems the common thread is that my holdings round up, while ‘actual’ is few decimal places smaller.
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.
Mike A
See below for screenshot:
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.
Fred Painchaud
Hi Mikey,
I see. Yes, it looks like the display rounds up your holdings to one decimal place.
And can you manually specify how much of the position you want to liquidate? If so, now that you know the exact number, try 9.1908, or 9.19, or 100% (if you can give it in %). If you have no choice but to let the app liquidate for you with its own number, then it looks like a bug since the rounding is not only taking place for display but also internally, as it seems (it's trying to liquidate 9.2 ADA, which is more than you have).
In this latter situation, please also email support@quantconnect.com to flag this issue.
In the meantime, if you really want to liquidate that position now, you can also liquidate directly through your broker, of course.
Cheers,
Fred
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.
Mike A
Fred,
It certainly does seem like it is a technical issue, as I did try to manually close the position and it prevents me from placing the order with more than 1 decimal place.
Will contact support.
Thank you for the feedback.
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.
Varad Kabade
Hi Mikey,
Thank you, Fred, for your response.
In the above snippet, we need to note that the Liquidate looks at the quantity from the Portfolio[symbol].Quantity but to get the real quantity is available inside the cashbook as Portfolio.Cashbook['ADA'] therefore when buying ADAUSDC, the fees were deducted as ADA therefore, we observe the difference between 9.2 and 9.1908.
When trading crypto, we should manually calculate the order sizes. This technique is demonstrated in the BasicTemplateCryptoAlgorithm.
Best,
Varad Kabade
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.
Mike A
Hi Varad,
Appreciate your feedback.
I have tried to add the cashbook, replacing Portfolio[symbol].Quantity in the OnData function. But getting the following error:
See entire code below
Am I incorrectly applying the cashbook here, I've tried to reconcile it to the algo in your response, maybe I've missed something.
Cheers
Mike
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.
Fred Painchaud
Hi Mike,
Did you try with “CashBook” instead of “Cashbook”?
😊
Moreover, the symbols of your crypto pairs won't be found in the CashBook. You won't have, say, BTCUSDC in your CashBook, but BTC, USDC, etc, depending on what you buy (you start with 100 USDC and then buy other coins).
Cheers,
Fred
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.
Mike A
Hi Fred,
Silly mistake on my part ahah, CashBook is working just fine and can access the USDC cash.
You're right, in that CashBook for the crypto coins can't be accessed through the for symbol loop since it's all pairs. I just need to figure out how to access the cash balance for each crypto so orders could be input accordingly. Say for example as per the BasicCryptoTemplate for a sell limit order for a single coin.
This wouldn't work where you have multiple coins, since manually inputting the pair into the limitPrice variable would just apply the quoted pair's price to whichever coin's MA have crossed over.
Thank you
Mike
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.
Varad Kabade
Hi Mikey A,
Would you mind attaching the backtest highlighting the abovementioned issue for a precise diagnosis?
Best,
Varad Kabade
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.
Mike A
Hi Varad,
Please see my attempt at sizing orders limit orders based on cash position for each symbol. The code was drawn from the basic crypto algorithm template.
Cheers
Mike
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.
Derek Melchin
Hi Mike,
It's not possible to submit an order for 9.1908 ADA because the lot size for ADAUSDC is 0.1 for Binance. For reference, see the Symbol Properties Database.
I just need to figure out how to access the cash balance for each crypto so orders could be input accordingly.
To get the cash balance of a crypto holding, we can use
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.
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!