This​​​​​​ is my code below.import clr
clr.AddReference("System")
clr.AddReference("QuantConnect.Algorithm")
clr.AddReference("QuantConnect.Indicators")
clr.AddReference("QuantConnect.Common")
from System import *
from QuantConnect import *
from QuantConnect.Algorithm import *
from QuantConnect.Indicators import *
import decimal as d
from datetime import datetime
class FirstAlgo(QCAlgorithm):
def Initialize(self):
'''Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.'''
self.SetCash(100000) #Set Strategy Cash
self.SetStartDate(2007, 1, 1) #Set Start Date
self.SetEndDate(2011, 1, 1) #Set End Date
self.stock = "spy"
self.AddEquity(self.stock, Resolution.Daily)
### SMA ###
# create a 200 day moving average
self.SMA200 = self.SMA(self.stock, 200, Resolution.Daily);
def OnData(self, data):
'''OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.'''
# a couple things to notice in this method:
# 1. We never need to 'update' our indicators with the data, the engine takes care of this for us
# 2. We can use indicators directly in math expressions
# 3. We can easily plot many indicators at the same time
### SMA setups ###
# wait for our slow ema to fully initialize
if not self.SMA200.IsReady:
return
### When to buy/sell ###
holdings = self.Portfolio[self.stock].Quantity
# buy when close price is above SMA200
if holdings <= 0:
#Got an error on the line below...
if self.SMA200[self.stock].Current.Value < [self.stock].Ask.Close:
self.Log("BUY >> {0}".format(self.Equity[self.stock].Price))
self.SetHoldings(self.stock, 1)
# sell when close price is under SMA200
if self.SMA200[self.stock].Current.Value > [self.stock].Ask.Close:
self.Log("SELL >> {0}".format(self.Equity[self.stock].Price))
self.Liquidate(self.stock)

