Hi - I am new to QuantConnect, and I am trying to calculate the Simple Moving Average of a stock in the last 1 hour period, however constantly get 0.0 when I set the period to 60 and the resolution to Minute. Am I missing something here?
Any help would be greatly appreciated for this newbie!
Thanks,
Gus
import numpy as np
import pandas as pd
### <summary>
### Basic template algorithm simply initializes the date range and cash. This is a skeleton
### framework you can use for designing an algorithm.
### </summary>
class MyAlgo(QCAlgorithm):
def Initialize(self):
# Reference to AAPL
self.SetStartDate(2017,10,2)
self.SetEndDate(2017,10,6)
self.SetCash(10000)
self.secs = ["AAPL", "IBM", "CAT"]
for stock in self.secs:
self.stock = self.AddEquity(stock)
self.Log("hello")
self.secs_df = pd.DataFrame(index=self.secs)
def OnData(self, data):
''' Runs every tick defined by resolution in initialize under equity'''
# Position 100% of our portfolio to be long in AAPL
len_sec = len(self.secs_df)
self.Log("total securities to trade " + str(len_sec))
for stock in self.secs_df.index:
self.SetHoldings(stock, 1.0/len_sec)
#self.SetHoldings("IBM", 0.5)
shares_held = self.Portfolio[stock].AbsoluteQuantity
self.BuyPrice = self.Portfolio[stock].AveragePrice
self.sma = self.SMA(stock, 60, Resolution.Minute)
self.Log(str(stock) + ' ' + str(shares_held) + ' ' + str(self.BuyPrice) + ' SMA: ' + str(self.sma))
Alexandre Catarino
When you create an indicator with a method helper, e.g. self.SMA, it creates an indicator object that is updated under the hood. Therefore you just need to define it once (normally in Initialize). Each time you call these methods, you will redefine the indicator and set it to start, thus returning zero.
Since you are using multiple stocks, you will need to create an dictionary that will hold the indicator for each one of them:
# In Initialize self.sma = {} self.secs = ["AAPL", "IBM", "CAT"] for stock in self.secs: equity = self.AddEquity(stock) self.sma[equity.Symbol] = self.SMA(equity.Symbol, 60, Resolution.Minute)
In OnData, you will be able to access the SMA for a given symbol like this:
# In OnData for stock in self.secs_df.index: sma = self.sma[stock] self.Log(str(stock) + ' SMA: ' + str(sma))
Gustavo Rincon
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!