Runtime Error: Value cannot be null

Back

from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import train_test_split
import numpy as np
class AlphaFiveUSTreasuries(QCAlgorithm):

    def Initialize(self):

        #1. Required: Five years of backtest history
        self.SetStartDate(2014, 1, 1)
    
        #2. Required: Alpha Streams Models:
        self.SetBrokerageModel(BrokerageName.AlphaStreams)
    
        #3. Required: Significant AUM Capacity
        self.SetCash(100000)

        self.AddUniverse(self.CoarseSelectionFunction)
        self.UniverseSettings.Resolution = Resolution.Hour
    
        self.spy = self.AddEquity("SPY", Resolution.Hour).Symbol
        
        #4. Required: Benchmark to SPY
        self.SetBenchmark("SPY")
        
        self.SetPortfolioConstruction(InsightWeightingPortfolioConstructionModel())
        
        self.SetExecution(ImmediateExecutionModel())
    
        # self.assets = ["IEF", "SHY", "TLT", "IEI", "SHV", "TLH", "EDV", "BIL",
        #               "SPTL", "TBT", "TMF", "TMV", "TBF", "VGSH", "VGIT",
        #               "VGLT", "SCHO", "SCHR", "SPTS", "GOVT"]

        self.assets = []

        self.symbols = {}
        
        self.portfolioValue = RollingWindow[Decimal](500)
        
        self.SetWarmup(500)
        
        # Add Equity ------------------------------------------------ 
        # add equities from coarse universe to self.symbols
        # for i in range(len(self.assets)):
        #     self.symbols[self.assets[i]] = self.AddEquity(self.assets[i],Resolution.Hour).Symbol 
        
        # test different timings after market opens affects the returns
        self.Schedule.On(self.DateRules.Every(DayOfWeek.Monday), self.TimeRules.AfterMarketOpen("SPY", 0), self.EveryDayAfterMarketOpen)
    
    def CoarseSelectionFunction(self, coarse):
        # add the top 20 assets
        # Sort symbols by Dollar Volume and if fundamental data exists, descending
        sortedByDollarVolume = sorted([x for x in coarse if x.HasFundamentalData and x.Price > 10],
                key=lambda x: x.DollarVolume, reverse=True)

        # for y in range(20):
        #     self.Debug(sortedByDollarVolume[y])
        #     self.Debug(f"Test going into range")

        # if self.lastMonth == algorithm.Time.month:
        #     return Universe.Unchanged
        # self.lastMonth = algorithm.Time.month
        self.Debug(f"before dictionary completion")
        
        self.assets = sortedByDollarVolume[:20]
        
        for x in range(20):
            if self.assets[x].Value is None:
                self.assets.RemoveSecurity(self.assets[x])
        
        self.Debug(f"test went through assets list")
        
        # for asset in self.assets:
        for x in self.assets:
            self.Debug(x)
            # if not self.Securities.ContainsKey(asset.Symbol):
            # self.symbols[self.assets[x]] = self.AddEquity(self.assets[x],Resolution.Hour).Symbol 
                

        # add assets list 
        # for asset in self.assets:
        #     self.Debug(f"test going in")
        #     self.Debug(asset)
            # self.Debug("{}".format(type(self.assets[x])))
            # create symbols and tickers dictionary
            #self.symbols[self.assets[x]] = self.AddEquity(self.assets[x],Resolution.Hour).Symbol 

        
        # add tickers and symbols to dictionary
        # for i in range(len(self.assets)):
        #     self.symbols[self.assets[x]] = self.AddEquity(self.assets[x],Resolution.Hour).Symbol 
        
        self.Debug(f"test dictionary completion")
        # for ticker, symbol in self.symbols:
        #     self.Debug(symbol)
        #     self.Debug(ticker)
        
        # return self.symbols
        # return [x.Symbol for x in sortedByDollarVolume[:10]]
        
    def EveryDayAfterMarketOpen(self):
        for symbol, ticker in self.symbols.items():
            self.Debug(symbol, ticker)
            
        # for ticker, symbol in self.symbols:
        #     self.Debug(f"test going in")
        #     self.Debug(ticker, symbol)
        
        # training for model 
        # if not self.Portfolio.Invested:
        #     insights = []
        #     for ticker, symbol in self.symbols.items():
        #         insights.append( Insight.Price(symbol, timedelta(days=5), InsightDirection.Up, 0.01, None, None, 1/len(self.symbols)) )
        #     self.EmitInsights(insights)
        # else:
        #     qb = self 
        #     #==============================
        #     # Initialize instance of Random Forest Regressor
        #     regressor = RandomForestRegressor(n_estimators=100, min_samples_split=5, random_state = 1990)
    
        #     # Fetch history on our universe
        #     df = qb.History(qb.Securities.Keys, 500, Resolution.Hour)
            
        #     # Get train/test data
        #     returns = df.unstack(level=1).close.transpose().pct_change().dropna()
        #     X = returns
        #     y = [x for x in qb.portfolioValue][-X.shape[0]:]
        #     X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 1990)
            
        #     # Fit regressor
        #     regressor.fit(X_train, y_train)
            
        #     # Get long-only predictions
        #     weights = regressor.feature_importances_
        #     symbols = returns.columns[np.where(weights)]
        #     selected = zip(symbols, weights)
        #     # ==============================
            
        #     insights = []
        #     for symbol, weight in selected:
        #         insights.append( Insight.Price(symbol, timedelta(days=5), InsightDirection.Up, 0.01, None, None, weight) )
        #     self.EmitInsights(insights)
        
    def OnData(self, data):
        self.portfolioValue.Add(self.Portfolio.TotalPortfolioValue)

Hi guys, I'm trying to create a universe sorted by dollar volume. Subsequently, the ticker and corresponding symbols will be the key value pairs of a dictionary "self.symbols". However, I am not able to append all 20 symbols I want to from the sorted dollar volume list to self.assets. I am being thrown the error "System.ArgumentNullException: Value cannot be null.
Parameter name: source". 

I have tried removing values that are null from my self.assets list but I'm still getting the error. Any help is appreciated (new to QuantConnect), thank you!

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.


Hi Wei Li,

Our universe selection expects a list of  Symbols to be returned by the Coarse function, but currently it doesn't return anything. To address this, I've uncommented line 98. For future reference, I suggest trying out our debugger to step through the code to find the cause of the errors. I've shown the necessary change in the attached backtest.

Best,
Shile Wen

1

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

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!