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.
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!