I have a strategy that rellies on the fact that portfolio is invested or not:
if (!Portfolio[_btcSymbol].Invested && someCondition)
{
SetHoldings(_btcSymbol, 1);
Debug("Purchased BTC");
}
else if (Portfolio[_btcSymbol].Invested && SomeOtherCondition)
{
SetHoldings(_btcSymbol, 0);
Debug("Sold BTC");
}
When the if statement to buy executes I have aquired BTC, however when the “else if” is executed it does not sell all the btc, I am left with some small amount like 0.004 or so, so the Portfolio[_btcSymbol].Invested is always true and no more buy signals can be executed and the sell operations afterwards do not sell the remaining 0.004 btc. What is the way around it? All the tutorial show to use Portfolio[_btcSymbol].Invested.
Mia Alissi
The issue you're experiencing is related to the precision of the crypto asset. When you use
SetHoldings(_btcSymbol, 0)
, it tries to sell all the BTC, but due to the precision of the asset, a small amount might still remain in your portfolio. This is whyPortfolio[_btcSymbol].Invested
remainstrue
.To work around this, you can check if the amount of BTC in your portfolio is less than a certain threshold. If it is, you can consider your portfolio as not invested. Here's how you can do it:
In this code,
Portfolio[_btcSymbol].Quantity <= threshold
checks if the amount of BTC in your portfolio is less than or equal to the threshold. If it is, it considers your portfolio as not invested and allows the buy signal to be executed.Please note that you need to choose an appropriate threshold. If the threshold is too high, you might sell your BTC too early. If it's too low, you might still face the same issue.
Also, when trading crypto assets, it's recommended to use the cashbook to manage your holdings instead of
SetHoldings
method. You can find more information about this in the QuantConnect documentation.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.
Vladislav Sarunov
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!