Back

Unable to use self.Schedule.On in Framework Algorithms?

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)
Update Backtest







Please share an algorithm (use the attach backtest button below) that shows the issue you are experiencing.
This procedure helps us, the community, to answer your questions quickly and effectively.

I got a different error:

During the algorithm initialization, the following exception has occurred: AttributeError : 'BasicTemplateFrameworkAlgorithm' object has no attribute 'SelectSymbols'
at Initialize in main.py:line 36
AttributeError : 'BasicTemplateFrameworkAlgorithm' object has no attribute 'SelectSymbols'

It means we haven't defined SelectedSymbols callback method.

Ideally, a framework algorithm is alpha driven (or insight driven), so we shouldn't be placing orders in a scheduled event. We should be emitting insights at a given date/time that would trigger orders in the execution model.

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.


Hello Alexandre,

   Again, thank you for your assistance.  The issue was with my PreMarketClose function itself, there was some sort of error in it (maybe an unprintable ASCII character? couldn't tell).   I'll share backtests in the future, thanks.

0

In PreMarketClose, you are iterating a Portfolio object which is a key-value pair:

for kvp in self.Portfolio:
holdings = kvp.Value
if holdings.Invested:
ticket = self.MarketOnCloseOrder(holdings.Symbol, -holdings.Quantity,
asynchronous = True)
#self.Log(f'closing: {ticket}) # only uncomment for debug
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.


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

This discussion is closed