When I run a backtest for the following algortihm, I get the error: Build Error: File: n/a Line:0 Column:0 - return
I don't know why. Please help, I migrated from Quantopian.
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
# QuantConnect Basic Template:
# Fundamentals to using a QuantConnect algorithm.
#
# You can view the QCAlgorithm base class on Github:
# https://github.com/QuantConnect/Lean/tree/master/Algorithm
#
import numpy as np
class MovingAverageCrossAlgorithm(QCAlgorithm):
def Initialize(self):
# Set the cash we'd like to use for our backtest
# This is ignored in live trading
self.SetCash(100000)
# Start and end dates for the backtest.
# These are ignored in live trading.
self.SetStartDate(2014,1,1)
self.SetEndDate(2017,1,1)
self.UniverseSettings.Resolution = Resolution.Daily
# Add assets you'd like to see
self.aapl = self.AddEquity("AAPL")
self.tesla = self.AddEquity("TSLA")
self.boeing = self.AddEquity("BA")
self.fb = self.AddEquity("FB")
#20 Day MA Setup
self.fastAAPL = self.SMA("AAPL", 20, Resolution.Daily);
self.fastBA = self.SMA("BA", 20, Resolution.Daily);
self.fastTSLA = self.SMA("TSLA", 20, Resolution.Daily);
self.fastFB = self.SMA("FB", 20, Resolution.Daily);
#50 Day MA Setup
self.slowAAPL = self.SMA("AAPL", 50, Resolution.Daily);
self.slowBA = self.SMA("BA", 50, Resolution.Daily);
self.slowTSLA = self.SMA("TSLA", 50, Resolution.Daily);
self.slowFB = self.SMA("FB", 50, Resolution.Daily);
if not self.slow.IsReady:
return
if not self.fast.IsReady:
return
def OnData(self, slice):
# Simple buy and hold template
hold_AAPL = self.AAPL["AAPL"].Quantity
hold_TSLA = self.TSLA["TSLA"].Quantity
hold_BA = self.BA["BA"].Quantity
hold_FB = self.FB["FB"].Quantity
hist = data[AAPL]
tolerance = 0.00015;
#Go Long If MA20 > MA50
if hold_AAPL <= 0:
if self.fastAAPL.Current.Value > self.slowAAPL.Current.Value * d.Decimal(1 + tolerance):
self.Log("BUY >> {0}".format(self.Securities["TSLA"].Price))
self.SetHoldings("TSLA", 0.24)
if hold_TSLA <= 0:
if self.fastTSLA.Current.Value > self.slowTSLA.Current.Value * d.Decimal(1 + tolerance):
self.Log("BUY >> {0}".format(self.Securities["TSLA"].Price))
self.SetHoldings("TSLA", 0.24)
if hold_FB <= 0:
# if the fast is greater than the slow, we'll go long
if self.fastFB.Current.Value > self.slowFB.Current.Value * d.Decimal(1 + tolerance):
self.Log("BUY >> {0}".format(self.Securities["FB"].Price))
self.SetHoldings("FB", 0.24)
if hold_BA <= 0:
# if the fast is greater than the slow, we'll go long
if self.fastBA.Current.Value > self.slowBA.Current.Value * d.Decimal(1 + tolerance):
self.Log("BUY >> {0}".format(self.Securities["BA"].Price))
self.SetHoldings("BA", 0.24)
#Sell if stock is present and if MA50 > MA20
if hold_AAPL > 0 and self.fastAAPL.Current.Value < self.slowAAPL.Current.Value:
self.Log("SELL >> {0}".format(self.Securities["AAPL"].Price))
self.Liquidate("AAPL")
if hold_TSLA > 0 and self.fastTSLA.Current.Value < self.slowTSLA.Current.Value:
self.Log("SELL >> {0}".format(self.Securities["TSLA"].Price))
self.Liquidate("TSLA")
if hold_BA > 0 and self.fastBA.Current.Value < self.slowBA.Current.Value:
self.Log("SELL >> {0}".format(self.Securities["BA"].Price))
self.Liquidate("BA")
if hold_FB > 0 and self.fastFB.Current.Value < self.slowFB.Current.Value:
self.Log("SELL >> {0}".format(self.Securities["FB"].Price))
self.Liquidate("FB")
self.previous = self.Time