Hi All,
I have been struggling to add simple moving averages to the coarse selection feature of my python algorithms. I have looked online on the quantconnect website how to do this but still can't figure out how to filter by comparing the current price of the security to the current moving average of the security. If anyone has any tips or advice I greatly appreciate it! My code looks something like this:
Mia Alissi
From the provided code, it seems like there is a misunderstanding about how to use the SimpleMovingAverage (SMA) indicator in the CoarseSelectionFunction. The SMA is not calculated correctly in the following line:
The SimpleMovingAverage function is not being applied to any data here, so it cannot provide a meaningful value.
Instead, you should create a dictionary to store the SMA for each symbol in your universe. You can update the SMA value for each symbol in the CoarseSelectionFunction and then use it for comparison. Here is an example of how you can do this:
In this code, we create a dictionary
self.averages
to store the SMA for each symbol. In the CoarseSelectionFunction, we update the SMA value for each symbol with the latest price. Then we use this updated SMA value for comparison.Please note that this is a simplified example. In a real-world scenario, you might want to consider additional factors such as checking if the SMA is ready before using it for comparison.
For more information on how to use indicators in QuantConnect, you can refer to the 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.
David L
Thanks again Mia! That helps me a lot actually. I'm guessing if I wanted to use the Estimate Moving Average or RSI currents values instead, the same logic would apply then?
Mia Alissi
Yes, David, the same logic would apply if you wanted to use other indicators such as Exponential Moving Average (EMA) or Relative Strength Index (RSI). You would create and update these indicators in a similar way. Here's how you could do it:
For EMA:
For RSI:
In the EMA example, we're filtering for symbols where the price is less than the EMA. In the RSI example, we're filtering for symbols where the RSI is less than 30, which is a common threshold for being oversold.
Remember to always check if the indicator is ready before using it in your logic.
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.
David L
Hi Mia, I added your code block but am stuck at the runtime error:
“Runtime Error: Please register to receive data for symbol ' W5373A5QVCRP' using the AddSecurity() function. in Indicators.cs:line 2213”,
when I try to backtest. Should I add a class for SymbolData, or change how I update the moving average current values?
David L
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!