Hi all. I've been trying to get the SMA to use a weekly timeframe and am struggling. I figured out how to create the trade bar for a weekly bar from the help docs and BootCamp, but I'm having trouble with the SMA part. Line 21 gives an error
from System import *
from QuantConnect import *
from QuantConnect.Algorithm import *
from QuantConnect.Indicators import *
from QuantConnect.Data.Market import TradeBar
class LeverageForTheLongRun(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2009,6, 1) #Set Start Date
self.SetEndDate(2020,2,2) #Set End Date
self.SetCash(100000) #Set Strategy Cash
self.spy = self.AddEquity("SPY", Resolution.Daily)
self.upro = self.AddEquity("UPRO", Resolution.Daily)
self.agg = self.AddEquity("AGG", Resolution.Daily)
self.SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage)
self.sma = self.SMA("SPY", 40)
# Register the SMA to use Weekly timeframe bars.
self.RegisterIndicator("SPY", self.sma, CalendarType.Weekly)
# Create the Weekly timeframe bar.
self.Consolidate("SPY", CalendarType.Weekly, self.OnDataConsolidated)
# warm up the indicators
self.SetWarmUp(40)
def OnData(self, data):
if data.ContainsKey("SPY") == False: return
if data.ContainsKey("UPRO") == False: return
if data.ContainsKey("AGG") == False: return
# define the Weekly Trade bar for SPY from Initialize
def OnDataConsolidated(self, bar):
self.CurrentBar = bar
# Free cash buffer, increase in case buy orders are rejected due to insufficient funds
self.Settings.FreePortfolioValuePercentage = 0.05
if self.CurrentBar.Close>self.sma.Current.Value:
self.SetHoldings("AGG", 0)
self.SetHoldings("UPRO", 1)
if self.CurrentBar.Close<self.sma.Current.Value:
self.SetHoldings("UPRO", 0)
self.SetHoldings("AGG", 1)
thanks for the help.
Rahul Chowdhury
Hey Mark,
We need to first create a weekly consolidator.
consolidator = self.Consolidate("SPY", self.CalenderType.Weekly, self.OnDataConsolidated)
Then we can register our indicator to that consolidator.
self.RegisterIndicator("SPY", self.sma, consolidator)
Just as a side note, please try to post your code as either a backtest if possible or as a code snippet. It makes code much easier to read and understand.
Mark hatlan
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!