Whenever I try scheduling an event in a QCFrameworkAlgorithm, I get a NullReference exception. Is the Action somehow failing to be created in Initialize()? Am I doing something wrong, or is there possibly a bug here? Error printout:
Runtime Error: In Scheduled Event 'SPY: EveryDay: SPY: 16 min before MarketClose', NullReferenceException : Object reference not set to an instance of an object
at Python.Runtime.Dispatcher.Dispatch (System.Collections.ArrayList args) [0x00018] in <7ada479175184ff388929ece541bbdb4>:0
at __System_ActionDispatcher.Invoke () [0x00006] in :0
at (wrapper managed-to-native) System.Reflection.MonoMethod.InternalInvoke(System.Reflection.MonoMethod,object,object[],System.Exception&)
at System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00032] in <2e7c1c96edae44d496118948ca617c11>:0 NullReferenceException : Object reference not set to an instance of an object
at Python.Runtime.Dispatcher.Dispatch (System.Collections.ArrayList args) [0x00018] in <7ada479175184ff388929ece541bbdb4>:0
at __System_ActionDispatcher.Invoke () [0x00006] in :0
at (wrapper managed-to-native) System.Reflection.MonoMethod.InternalInvoke(System.Reflection.MonoMethod,object,object[],System.Exception&)
at System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00032] in <2e7c1c96edae44d496118948ca617c11>:0
Code attached:
from clr import AddReference
AddReference("System")
AddReference("QuantConnect.Algorithm")
AddReference("QuantConnect.Common")
from System import *
from QuantConnect import *
from QuantConnect.Algorithm import *
from QuantConnect.Data import *
from Alphas.NullAlphaModel import NullAlphaModel
from Portfolio.EqualWeightingPortfolioConstructionModel import EqualWeightingPortfolioConstructionModel
from Execution.VolumeWeightedAveragePriceExecutionModel import VolumeWeightedAveragePriceExecutionModel
from Risk.TrailingStopRiskManagementModel import TrailingStopRiskManagementModel
from datetime import datetime, timedelta
class BasicTemplateFrameworkAlgorithm(QCAlgorithmFramework):
def Initialize(self):
# Set requested data resolution
self.UniverseSettings.Resolution = Resolution.Minute
self.SetStartDate(2016, 7, 29)
self.SetEndDate(2016, 8, 2)
self.SetCash(1000000)
self.SetUniverseSelection(ScheduledUniverseSelectionModel(
self.DateRules.Every(DayOfWeek.Monday, DayOfWeek.Tuesday, DayOfWeek.Wednesday, DayOfWeek.Thursday, DayOfWeek.Friday),
self.TimeRules.At(4, 0),
self.SelectSymbols))
self.SetAlpha(NullAlphaModel(self))
self.SetPortfolioConstruction(EqualWeightingPortfolioConstructionModel())
self.SetExecution(ImmediateExecutionModel())
self.SetRiskManagement(TrailingStopRiskManagementModel(0.03))
# QCAlgorithm
self.AddEquity('SPY', Resolution.Minute)
self.Schedule.On(self.DateRules.EveryDay('SPY'), self.TimeRules.BeforeMarketClose('SPY', 16), Action(self.PreMarketClose))
def PreMarketClose(self):
self.Log('market closing')
for instrument in self.Portfolio:
if self.Portfolio[instrument].Invested:
self.Log('closing: ' + str(instrument))
self.MarketOnCloseOrder(instrument, -self.Portfolio[instrument].Quantity, asynchronous = True)