Hello All,
I have read the scheduling event documentation and took a look at the scheduling events example of GitHub (love that repo btw) but I am having trouble scheduling events for a specific day of the week.
My code, works if I schedule events for 2 days of the week but not 1 only. I want to schedule an event every Friday. Here is my example code:
import numpy as np
from System import *
### <summary>
### Basic template algorithm simply initializes the date range and cash. This is a skeleton
### framework you can use for designing an algorithm.
### </summary>
class FTSEFridays(QCAlgorithm):
'''Basic template algorithm simply initializes the date range and cash'''
def Initialize(self):
'''Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.'''
self.SetStartDate(2018,6, 1) #Set Start Date
self.SetEndDate(2018,7,1) #Set End Date
self.SetCash(10000) #Set Strategy Cash
self.Allocate = 1 # Percentage of holdings to risk
# Setup Oanda Broker simulation
self.SetBrokerageModel(BrokerageName.OandaBrokerage)
# Find more symbols here: http://quantconnect.com/data
# THIS NEEDS TO BE ADDED AFTER THE BROKERAGE MODEL!
self.AddCfd("UK100GBP", Resolution.Minute)
#Logging / Debugs
self.Logging_On = True
self.Debug_On = True
#Set Shcedule
# self.Schedule.On(self.DateRules.Every(DayOfWeek.Monday,DayOfWeek.Friday), self.TimeRules.At(6, 55), self.MorningBuy)
# self.Schedule.On(self.DateRules.Every(DayOfWeek.Monday,DayOfWeek.Friday), self.TimeRules.At(16, 29), self.AfternoonSell)
self.Schedule.On(self.DateRules.Every(DayOfWeek.Friday), self.TimeRules.At(6, 55), self.MorningBuy)
self.Schedule.On(self.DateRules.Every(DayOfWeek.Friday), self.TimeRules.At(16, 29), self.AfternoonSell)
def OnData(self, data):
'''OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
Arguments:
data: Slice object keyed by symbol containing the stock data
'''
pass
def MorningBuy(self):
if self.Debug_On:
self.Debug("MorningBuy: Fired at : {0}".format(self.Time))
self.SetHoldings("UK100GBP", self.Allocate)
def AfternoonSell(self):
if self.Debug_On:
self.Debug("AfternoonSell: Fired at : {0}".format(self.Time))
self.Liquidate
In the code, the two commented `self.Schedule.On` lines work, whereas the two uncommented lines do not work. I receive the following error:
During the algorithm initialization, the following exception has occurred: Trying to dynamically access a method that does not exist throws a TypeError exception. To prevent the exception, ensure each parameter type matches those required by the Every method. Please checkout the API documentation.
at Initialize in main.py:line 35
TypeError : No method matches given arguments for Every
Should I be using something other than `self.DateRules.Every()` ?
Gurumeher Sawhney
In order to execute a trade on the Friday of every week, use the method .WeekEnd(). So the code needed to run will look like this:
self.Schedule.On(self.DateRules.WeekEnd(), self.TimeRules.At(6, 55), self.MorningBuy)
self.Schedule.On(self.DateRules.WeekEnd(), self.TimeRules.At(16, 29), self.AfternoonSell)
The reason why self.DateRules.Every() was not a suitable method call is because it expects a list of DayOfWeek objects and only one object is being passed. While self.DateRules.Every([DayOfWeek.Friday]) would work, WeekEnd() will provide a better solution since it considers holidays like Good Friday.
ThatBlokeDave
Hi Gurumeher Sawhney
Thank you very much for the pointer. Much appreciated.
Your suggestion should work well. However, for the sake of completeness and my own learning, let's say I want to do the same but it has to be on the exact day (Friday or Thursday or whatever) and cannot dynamically move for holidays like Weekend() will. Is there a way to do this?
Or should I just specify two days in the Every() call and then just handle it in the handler (i.e ignore the day I don't want).
Thanks!
Xin Wei
Hi Islander,
`self.DateRules.Every()` accepts a list of DayOfWeek objects. So, the following code should work
self.DateRules.Every([DayOfWeek.Monday,DayOfWeek.Friday])
You can add other days to the list, too.
ThatBlokeDave
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!