Back

I don't know why I'm getting this error:Build Error: File: n/a Line:0 Column:0 - return

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

 

Update Backtest








We could reproduce the build error, but there are some issue in your algorithm.
First, we don't need to check whether the indicator are ready in the Initialize method. 
In order to get the quantity for each security in the portfolio, we use the Portfolio object: Portfolio[Symbol].Quantity.

Since you apply the same trading logic to all tickers, why not loop through the symbols in the Slice object?
Please checkout the attached backtest.

 

0

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.


Update Backtest





0

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.


Loading...

This discussion is closed