2017-10-20 00:00:00 : Runtime Error: ValueError : shape mismatch: value array of shape (4,) could not be broadcast to indexing result of shape (2,) ValueError : shape mismatch: value array of shape (4,) could not be broadcast to indexing result of shape (2,)
Getting this error only on this particular date for UncorrelatedUniverseSelectionModel. There are history intervals, where this error is not thrown for years. It is also never thrown for Forex universes. Have checked Alpha methods with try/except - the problem is not in alpha. The error is raised only if a certain stock is traded - not sure which. However, the day is persistand. There is also one such date in 2019. Any ideas how to fix it?
Jack Simonson
Hi Artur,
Could you share an example with this error so that we could reproduce and address it in detail?
Artur Sokolovsky
380 | 10:55:22:
Build Error: File: n/a Line:0 Column:0 - Unhandled Exception:
381 | 10:55:22:
Build Error: File: n/a Line:0 Column:0 - System.Reflection.ReflectionTypeLoadException: Exception of type 'System.Reflection.ReflectionTypeLoadException' was thrown.
382 | 10:55:22:
Build Error: File: n/a Line:0 Column:0 - at (wrapper managed-to-native) System.Reflection.Assembly.GetTypes(System.Reflection.Assembly,bool)
383 | 10:55:22:
Build Error: File: n/a Line:0 Column:0 - at System.Reflection.Assembly.GetTypes () [0x00000] in :0
384 | 10:55:22:
Build Error: File: n/a Line:0 Column:0 - at Python.Runtime.AssemblyManager.GetNames (System.String nsname) [0x00072] in :0
I have tried to remove trading logic from my algo to shre it - the exception disappeared. Here is a new one - I cannot add an implemented Portfolio Construction Model even the official implementations - when I add it to main.py, I get this error above.
Have not created a new thread to avoid spamming. Hope it is ok.
Artur Sokolovsky
I have tried to remove trading logic from my algo to shre it - the exception disappeared.
Here is a new one - I cannot add an implemented Portfolio Construction Model even the official implementations - when I add it to main.py, I get this error:
380 | 10:55:22:
Build Error: File: n/a Line:0 Column:0 - Unhandled Exception:
381 | 10:55:22:
Build Error: File: n/a Line:0 Column:0 - System.Reflection.ReflectionTypeLoadException: Exception of type 'System.Reflection.ReflectionTypeLoadException' was thrown.
382 | 10:55:22:
Build Error: File: n/a Line:0 Column:0 - at (wrapper managed-to-native) System.Reflection.Assembly.GetTypes(System.Reflection.Assembly,bool)
383 | 10:55:22:
Build Error: File: n/a Line:0 Column:0 - at System.Reflection.Assembly.GetTypes () [0x00000] in :0
384 | 10:55:22:
Build Error: File: n/a Line:0 Column:0 - at Python.Runtime.AssemblyManager.GetNames (System.String nsname) [0x00072] in :0
Have not created a new thread to avoid spamming. Hope it is ok. Not adding the backtest - it does not get to that stage.
main.py:
from Alphas.PearsonCorrelationPairsTradingAlphaModel import PearsonCorrelationPairsTradingAlphaModel from Execution.ImmediateExecutionModel import ImmediateExecutionModel from Portfolio.EqualWeightingPortfolioConstructionModel import EqualWeightingPortfolioConstructionModel from Risk.MaximumDrawdownPercentPerSecurity import MaximumDrawdownPercentPerSecurity from Selection.UncorrelatedUniverseSelectionModel import UncorrelatedUniverseSelectionModel from alpha_template1 import template_AlphaModel from Execution.StandardDeviationExecutionModel import StandardDeviationExecutionModel from Risk.TrailingStopRiskManagementModel import TrailingStopRiskManagementModel from Selection.QC500UniverseSelectionModel import QC500UniverseSelectionModel from temp import MeanVarianceOptimizationPortfolioConstructionModel class CalibratedNadionSplitter(QCAlgorithm): def Initialize(self): #self.SetStartDate(2019, 1, 5) self.SetStartDate(2017, 10, 15) self.SetCash(100000) self.AddAlpha(template_AlphaModel()) self.SetExecution(StandardDeviationExecutionModel()) self.SetPortfolioConstruction(MeanVarianceOptimizationPortfolioConstructionModel()) self.SetRiskManagement(MaximumDrawdownPercentPerSecurity(0.01)) self.SetUniverseSelection(UncorrelatedUniverseSelectionModel())
As You see, I import the implementation of Portfolio Construction Model from temp.py file. The implementation is taken from github:
https://github.com/QuantConnect/Lean/blob/master/Algorithm.Framework/Portfolio/MeanVarianceOptimizationPortfolioConstructionModel.pyalpha_template.py:
from clr import AddReference AddReference("QuantConnect.Algorithm") AddReference("QuantConnect.Algorithm.Framework") AddReference("QuantConnect.Indicators") AddReference("QuantConnect.Logging") AddReference("QuantConnect.Common") from QuantConnect import * from QuantConnect.Indicators import * from QuantConnect.Logging import Log from QuantConnect.Algorithm import * from QuantConnect.Algorithm.Framework import * from QuantConnect.Algorithm.Framework.Alphas import * from datetime import timedelta from enum import Enum import pandas as pd import datetime import numpy as np class template_AlphaModel(AlphaModel): def __init__(self, period = 14, resolution = Resolution.Daily): self.period = period self.resolution = resolution self.insight_period = 5 self.insightPeriod = Time.Multiply(Extensions.ToTimeSpan(resolution), self.insight_period) self.symbolDataBySymbol ={} self.symbols = [] resolutionString = Extensions.GetEnumString(resolution, Resolution) self.Name = '{}({},{})'.format(self.__class__.__name__, period, resolutionString) self.magic_date = '1999-01-01' def Update(self, algorithm, data): insights = [] for symbol in list(self.symbols): prediction = np.random.choice(a=[1,-1],size=1) if prediction > 0: insights.append( Insight.Price(symbol, self.insightPeriod, InsightDirection.Up, 0.05, None) ) if prediction < 0: insights.append( Insight.Price(symbol, self.insightPeriod, InsightDirection.Down, -0.05, None) ) return insights def OnSecuritiesChanged(self, algorithm, changes): # clean up data for removed securities removedSymbols = [ x.Symbol for x in changes.RemovedSecurities ] if len(removedSymbols) > 0: for subscription in algorithm.SubscriptionManager.Subscriptions: if subscription.Symbol in removedSymbols: try: self.symbolDataBySymbol.pop(subscription.Symbol, None) subscription.Consolidators.Clear() except: continue addedSymbols = [ x.Symbol for x in changes.AddedSecurities if x.Symbol not in self.symbolDataBySymbol] for i in range(len(addedSymbols)): try: self.symbols = list(set(self.symbols + [addedSymbols[i]])) except Exception as e: algorithm.Debug(str(e)) continue for i in range(len(removedSymbols)): try: self.symbols = list(set(self.symbols) - set([removedSymbols[i]])) except Exception as e: algorithm.Debug(str(e)) continue return
Artur Sokolovsky
UPDATE: tried running this code multiple times - at certain point it started working. Not getting it...:/
Artur Sokolovsky
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!