Simple Moving Average Indicator

Hi, Recently I have been attempting to use a Simple Moving Average as an indicator, however, I cannot find any documentation for it and can't find a way to use it in my current algorithm. Any help would be greatly appreciated. Thank You.

Update Backtest

The Code I am attempting to insert a Simple Moving Average indicator into resembles the code attached below.

def FineSelectionFunction(self, fine):
if self.flag1:
self.flag1 = 0
self.flag2 = 1

filtered_fine = [x for x in fine if x.EarningReports.TotalDividendPerShare.ThreeMonths
and x.ValuationRatios.PriceChange1M
and x.ValuationRatios.BookValuePerShare
and x.ValuationRatios.FCFYield]

sortedByfactor1 = sorted(filtered_fine, key=lambda x: x.EarningReports.TotalDividendPerShare.ThreeMonths, reverse=True)
sortedByfactor2 = sorted(filtered_fine, key=lambda x: x.ValuationRatios.PriceChange1M, reverse=False)
sortedByfactor3 = sorted(filtered_fine, key=lambda x: x.ValuationRatios.BookValuePerShare, reverse=True)
sortedByfactor4 = sorted(filtered_fine, key=lambda x: x.ValuationRatios.FCFYield, reverse=True)

num_stocks = floor(len(filtered_fine)/self.num_portfolios)

stock_dict = {}

for i,ele in enumerate(sortedByfactor1):
rank1 = i
rank2 = sortedByfactor2.index(ele)
rank3 = sortedByfactor3.index(ele)
rank4 = sortedByfactor4.index(ele)
score = [ceil(rank1/num_stocks),
score = sum(score)
stock_dict[ele] = score
#self.Log("score" + str(score))
self.sorted_stock = sorted(stock_dict.items(), key=lambda d:d[1],reverse=True)
sorted_symbol = [self.sorted_stock[i][0] for i in range(len(self.sorted_stock))]
topFine = sorted_symbol[:self.numberOfSymbolsFine]

self.flag3 = self.flag3 + 1

return [i.Symbol for i in topFine]

return []

I am unsure of where the SMA Indicator is being used in this example, however, I can the provide information needed. In the QuantConnect Docs, the section on indicators provides plenty of information on how to instantiate SMAs. Reading this section is recommended to understand how indicators work in the QuantConnect environment.

A common practice for keeping track of SMAs during Universe Selection is the SymbolData class. This class is used to create a dictionary linking the securities to the relevant data needed in the algorithm. This sample code is an implementation which stores the EMA indicator for all added securities. 

For this specific case, a SymbolData class can be created for all added securities in the function OnSecuritiesChanged(self,changes). Here is a link on how the universe works in QuantConnect. 


Update Backtest


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.


This discussion is closed