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