Hello QC Support,
I am working on a Machine Learning Strategy for the Daily SPY ETF. Code below & attached. I am getting the following error:
Runtime Error: ValueError : Shape of passed values is (2, 1), indices imply (2, 2)
at construction_error
"Shape of passed values is {0} in managers.py:line 1717
ValueError : Shape of passed values is (2, 1), indices imply (2, 2) (Open Stacktrace)
I seem to be passing an incorrectly shaped array to the Neural Network predict function. I'm using a rolling window for the daily Close prices and a Momentum indicator with the same lookback period. I hope I am initializing & passing these correctly to the alpha model. Does the Close rolling window & MOM indicator have a Date / Time index that I need to drop? As you can see I have tried all sorts of data manipulations to try to get this work. Can you have a look pls and see if you can help me out.
Thanks / Sheikh
import numpy as np
import pandas as pd
from sklearn.neural_network import MLPClassifier
class MachineLearningSPY(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2016, 5, 2)
self.SetEndDate(2021, 6, 22)
self.SetCash(100000)
self.AddEquity("SPY", Resolution.Daily)
self.SetBenchmark("SPY")
self.SetBrokerageModel(BrokerageName.AlphaStreams)
self.SetExecution(ImmediateExecutionModel())
self.SetPortfolioConstruction(EqualWeightingPortfolioConstructionModel())
self.AddUniverseSelection(ManualUniverseSelectionModel("SPY"))
self.lookback = 30
self.spyClose = RollingWindow[float](self.lookback)
self.spyMomentum = self.MOMP("SPY", self.lookback, Resolution.Daily)
self.SetWarmup(self.lookback)
self.AddAlpha(MachineLearningSPYAlphaModel(self.spyClose, self.spyMomentum))
class MachineLearningSPYAlphaModel:
def __init__(self, close, spyMomentum):
self.period = timedelta(30)
self.spyClose = close
self.spyMomentum = spyMomentum
def GetMLModel(self):
self.MLModel = 0
self.MLModel = MLPClassifier(hidden_layer_sizes = (100, 100, 100, 100), max_iter = 1000)
def Update(self, algorithm, data):
insights = []
if data.Bars.ContainsKey("SPY"):
self.spyMomentum.Update(data["SPY"].EndTime, data["SPY"].Close)
self.spyClose.Add(data["SPY"].Close)
if self.spyMomentum.IsReady and self.spyClose.IsReady:
# features array
arr_features = np.array([self.spyMomentum, self.spyClose])
# features dataframe
self.df = pd.DataFrame(arr_features, columns=["MOM", "Close"]) # Does MOM and Close series have a date/time index?
# calculate daily SPY forward returns to be used to set Target / Signal
self.df['spyReturn'] = np.log(self.df['Close'].shift(-1)/self.df['Close'])
self.df = self.df.dropna()
# set Signal / Target
self.df['Signal'] = 0
self.df.loc[self.df['spyReturn'] > 0, 'Signal'] = 1
self.df.loc[self.df['spyReturn'] < 0, 'Signal'] = -1
# set training data
self.X = self.df.drop(['Close','Signal'], axis=1)
self.Y = self.df['Signal']
# align signals and features
self.Y, self.X = self.Y.align(self.X, axis=0, join='inner')
self.X_train = self.X[:-1]
self.Y_train = self.Y[:-1]
# fit / train ML model
self.GetMLModel()
self.MLModel.fit(self.X_train, self.Y_train)
# predict next day signal using today's values of feature set
self.X_today = self.X.iloc[-1]
# self.X_today is Series, so convert to numpy array
self.X_today = self.X_today.to_numpy()
# reshape self.X_today because it only has 1 day's sample
self.X_today = self.X_today.reshape(1,-1)
# Y_predict will take predicted signal
self.Y_predict = self.Y.iloc[-1]
self.Y_predict = self.MLModel.predict(self.X_today)
if self.Y_predict == 1:
insights.append(Insight("SPY", self.period, InsightType.Price, InsightDirection.Up, 1, None))
elif self.Y_predict == -1:
insights.append(Insight("SPY", self.period, InsightType.Price, InsightDirection.Down, 1, None))
return insights
def OnSecuritiesChanged(self, algorithm, changes):
self.changes = changes
Louis Szeto
Hi Sheikh
Indicator behaves like stack, you might use a rolling window to store indicator's value to avoid LIFO only. Also rolling windows cannot be converted to arrays but can be converted into dataframe.
Good luck with your data cleansing below
Cheers
Louis
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.
Louis Szeto
*Correction: Indicator behaves like queue, you might use a rolling window to store indicator's value to avoid FIFO only.
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.
Sheikh Pancham
Hi Louis, thanks for looking at this. The Momentum and Close seem to be overlapping. I am getting this when I use Debug to display df1 and self.df. Code below. Kindly have a look when you have a moment.
Thanks.
Sheikh
Louis Szeto
Hi Sheikh
Ohhhh I’m sorry you need to setup another new exactly same rolling window to store 2 variables coz mow they share same one. Separate the 2 variable from using same rolling window in line 25. You can do the same way as you did: initialize a new rolling window in your algo class’s initialize method, input into alpha class, reference it for the other variable.
BTW the prefer way of using rolling window with indicator is in this doc.
Cheers
Louis
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.
Louis Szeto
Hi Sheikh
I'll help you out this time, also with some data clenasing to drop rows with inf values. Of coz the cleansing is just one primitive example, plz adjust on your own need like ffill, bfill, mean fill, etc. Hope it helps.
The indicators shall work.
Happy researching!
Cheers
Louis
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.
Sheikh Pancham
Fantastico! Thanks Louis, I'll continue with this. There's a lot that you added, so greatly appreciated! And I'm sure the QC community will find it useful also.
Sheikh Pancham
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.
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!