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!
Nico Xenox
Hey sebul,
you're trying to access a variable that doesnt exist outside of OnData. For that you have to add ‘self’ to the variables that should also be accesible outside of OnData.
Hope it helps ;)
Nico Xenox
Sebul
Nico Xenox
Thank you for such a quick response!
Typical rookie mistake but as always lesson learnt :)
Sebul
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!