Hi guys, I'm pretty new to Algorithmic trading and I'm having some trouble with the backtest for this particular project I'm working on. I'm trying to build an algorithm that's able to identify candlestick shapes without using the LEAN built-in functions and then use those to make trade decisions, along with the MACD.
Any idea why my backtests show no data? It doesn't even get the cash right… I'm wondering if I did something wrong in my code. Any help appreciated!
Shane Vivek
This is my backtest btw
Mia Alissi
From the code you've shared, there are a few potential issues that might be causing your backtests to show no data:
In your
OnData
method, you're checking ifself.symbol
is inself.Portfolio.Keys
before placing any trades. However,self.symbol
will always be inself.Portfolio.Keys
because you've added it to your algorithm in theInitialize
method. This means that your trading logic might never be executed. You might want to check if you have any open positions forself.symbol
instead. You can do this withself.Portfolio[self.symbol].Invested
.In your
CheckCandle
method, you're callingCheckBull
andCheckBear
without theself
keyword, which will cause aNameError
because Python will think thatCheckBull
andCheckBear
are global functions, not methods of your algorithm class. You should call these methods asself.CheckBull
andself.CheckBear
.Similarly, in your
CheckCandle
method, you're callingRollCandleStates
without theself
keyword. You should call it asself.RollCandleStates
.In your
CheckCandle
method, you're checking if'Engulfing'
is inself.candle_states[0]
. However,self.candle_states[0]
is a string, not a list, so this check will always returnFalse
unlessself.candle_states[0]
is exactly'Engulfing'
. If you want to check if any of the strings inself.candle_states
contain'Engulfing'
, you should iterate overself.candle_states
and check each string individually.Finally, you're not doing anything with the
size
variable in the last line of yourCheckCandle
method. If you're not using this variable elsewhere in your code, you can remove this line.Here's how you can fix these issues:
I hope this helps! Let me know if you have any other questions.
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.
Shane Vivek
Shane Vivek
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!