Hi!
I am trying to implement a Schedule Event into my algo. Following documentation and other users' posts, I wrote this code:
# region imports
from AlgorithmImports import *
# endregion
class CustomIndexStrategy(QCAlgorithm):
def Initialize(self):
self.Pair_1 = "USDJPY"
self.holdingDays = 1
self.SetStartDate (2020, 1, 1)
self.SetEndDate(2022,7,1)
self.SetCash(10000)
self.SetBrokerageModel(BrokerageName.OandaBrokerage)
self.EURSEK = self.AddForex(self.Pair_1, Resolution.Daily, Market.Oanda)
self.symbols = [self.Pair_1]
self.prevPrices = { symbol : RollingWindow[QuoteBar](7) for symbol in self.symbols }
self.ticketPair1 = None
self.ticketPair2 = None
self.maximumholdingdays = 4
self.volume = 2
self.Schedule.On(self.DateRules.Every(DayOfWeek.Monday, DayOfWeek.Friday), self.TimeRules.At(12, 0), self.MondayTrade)
def OnData(self,data):
self.quantity_1 = self.CalculateOrderQuantity(self.Pair_1 , self.volume)
for symbol in self.symbols:
if data.ContainsKey(symbol):
self.prevPrices[symbol].Add( data[symbol] )
if not all([ window.IsReady for window in self.prevPrices.values() ]):
return
Pair1_window = self.prevPrices[self.Pair_1]
Pair1_6D = Pair1_window[6].Close
Pair1_5D = Pair1_window[5].Close
Pair1_4D = Pair1_window[4].Close
Pair1_3D = Pair1_window[3].Close
Pair1_2D = Pair1_window[2].Close
Pair1_1D = Pair1_window[1].Close
Pair1_0D = Pair1_window[0].Close
if self.ticketPair2 is not None and self.Securities[self.Pair_1].Exchange.ExchangeOpen is True and self.UtcTime >= self.ticketPair2.Time + timedelta(days = self.maximumholdingdays):
self.Liquidate()
self.ticketPair2 = None
if self.ticketPair1 is not None and self.Securities[self.Pair_1].Exchange.ExchangeOpen is True and self.UtcTime >= self.ticketPair1.Time + timedelta(days = self.maximumholdingdays):
self.Liquidate()
self.ticketPair1 = None
def MondayTrade(self):
if self.ticketPair2 is None and self.Securities[self.Pair_1].Exchange.ExchangeOpen is True and Pair1_0D < Pair1_1D < Pair1_2D < Pair1_3D < Pair1_4D :
self.ticketPair2 = self.MarketOrder(self.Pair_1, self.quantity_1 )
self.priceLong= self.ticketPair2.AverageFillPrice
self.Log(self.priceLong)
if self.ticketPair1 is None and self.Securities[self.Pair_1].Exchange.ExchangeOpen is True and Pair1_0D > Pair1_1D > Pair1_2D > Pair1_3D > Pair1_4D :
self.ticketPair1 = self.MarketOrder(self.Pair_1, -self.quantity_1)
self.priceShort = self.ticketPair1.AverageFillPrice
self.Log(self.priceShort)
I receive this error:
name 'Pair1_0D' is not defined
at MondayTrade
if self.ticketPair2 is None and self.Securities[self.Pair_1].Exchange.ExchangeOpen is True and Pair1_0D < Pair1_1D < Pair1_2D < Pair1_3D < Pair1_4D :
at Python.Runtime.PythonException.ThrowLastAsClrException()
at Python.Runtime.PyObject.Invoke(PyObject[] args)
at QuantConnect.Scheduling.ScheduleManager.<>c__DisplayClass15_0.<On>b__0(String name in main.py: line 53
If I understand correctly, in the “OnData" event, each new point of data will be saved.
By adding “ScheduleOn" event, data from “OnData” should be moved there and available to use. Therefore, I am not sure what could be causing this error.
But to solve the issue I copied and pasted “Pair1” objects into the “ScheduleOn” event.
# region imports
from AlgorithmImports import *
# endregion
class CustomIndexStrategy(QCAlgorithm):
def Initialize(self):
self.Pair_1 = "USDJPY"
self.holdingDays = 1
self.SetStartDate (2020, 1, 1)
self.SetEndDate(2022,7,1)
self.SetCash(10000)
self.SetBrokerageModel(BrokerageName.OandaBrokerage)
self.EURSEK = self.AddForex(self.Pair_1, Resolution.Daily, Market.Oanda)
self.symbols = [self.Pair_1]
self.prevPrices = { symbol : RollingWindow[QuoteBar](7) for symbol in self.symbols }
self.ticketPair1 = None
self.ticketPair2 = None
self.maximumholdingdays = 4
self.volume = 2
self.Schedule.On(self.DateRules.Every(DayOfWeek.Monday, DayOfWeek.Friday), self.TimeRules.At(12, 0), self.MondayTrade)
def OnData(self,data):
self.quantity_1 = self.CalculateOrderQuantity(self.Pair_1 , self.volume)
for symbol in self.symbols:
if data.ContainsKey(symbol):
self.prevPrices[symbol].Add( data[symbol] )
if not all([ window.IsReady for window in self.prevPrices.values() ]):
return
Pair1_window = self.prevPrices[self.Pair_1]
Pair1_6D = Pair1_window[6].Close
Pair1_5D = Pair1_window[5].Close
Pair1_4D = Pair1_window[4].Close
Pair1_3D = Pair1_window[3].Close
Pair1_2D = Pair1_window[2].Close
Pair1_1D = Pair1_window[1].Close
Pair1_0D = Pair1_window[0].Close
if self.ticketPair2 is not None and self.Securities[self.Pair_1].Exchange.ExchangeOpen is True and self.UtcTime >= self.ticketPair2.Time + timedelta(days = self.maximumholdingdays):
self.Liquidate()
self.ticketPair2 = None
if self.ticketPair1 is not None and self.Securities[self.Pair_1].Exchange.ExchangeOpen is True and self.UtcTime >= self.ticketPair1.Time + timedelta(days = self.maximumholdingdays):
self.Liquidate()
self.ticketPair1 = None
def MondayTrade(self):
for symbol in self.symbols:
if data.ContainsKey(symbol):
self.prevPrices[symbol].Add( data[symbol] )
if not all([ window.IsReady for window in self.prevPrices.values() ]):
return
Pair1_window = self.prevPrices[self.Pair_1]
Pair1_6D = Pair1_window[6].Close
Pair1_5D = Pair1_window[5].Close
Pair1_4D = Pair1_window[4].Close
Pair1_3D = Pair1_window[3].Close
Pair1_2D = Pair1_window[2].Close
Pair1_1D = Pair1_window[1].Close
Pair1_0D = Pair1_window[0].Close
if self.ticketPair2 is None and self.Securities[self.Pair_1].Exchange.ExchangeOpen is True and Pair1_0D < Pair1_1D < Pair1_2D < Pair1_3D < Pair1_4D :
self.ticketPair2 = self.MarketOrder(self.Pair_1, self.quantity_1 )
self.priceLong= self.ticketPair2.AverageFillPrice
self.Log(self.priceLong)
if self.ticketPair1 is None and self.Securities[self.Pair_1].Exchange.ExchangeOpen is True and Pair1_0D > Pair1_1D > Pair1_2D > Pair1_3D > Pair1_4D :
self.ticketPair1 = self.MarketOrder(self.Pair_1, -self.quantity_1)
self.priceShort = self.ticketPair1.AverageFillPrice
self.Log(self.priceShort)
And this error pups up:
name 'data' is not defined
at MondayTrade
if data.ContainsKey(symbol):
at Python.Runtime.PythonException.ThrowLastAsClrException()
at Python.Runtime.PyObject.Invoke(PyObject[] args)
at QuantConnect.Scheduling.ScheduleManager.<>c__DisplayClass15_0.<On>b__0(String name in main.py: line 54
Could you please provide me with some guidance on how to approach this issue? It's my first time using the Schedule Event as so far I have been working only with “OnData” and I am slightly confused.
Thank you!