Hello everyone,
I'd like to schedule an event being triggered every minute but not like it is described in the documentation at the beginning of the minute but ten seconds after it. So at 09:30:10, 9:31:10, 9:32:10 etc. How do I achieve this?
Thank you in advance.
Kind regards,
Christian Lauer
Petter Hansson
I would like this too. Please make an issue at the Github project for Lean and I will comment there to "vote" on it.
Jared Broad
Probably best way is:
Schedule.On(DateRules.EveryDay(), TimeRules.Every(TimeSpan.FromSeconds(10)), () => { if (Securities["SPY"].Exchange.ExchangeOpen) { } })
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.
Christian Lauer
This would trigger every ten seconds, right? I need the event only once per minute though. The problem is that I can't use self.TimeRules.Every(timedelta(seconds=60) because if the event is triggered at 9:32:00 for example the bar from 9:31 to 9:32 at my rolling window isn't ready yet. That's why I want it to be triggered ten seconds after the beginning of the minute.
Kind regards,
Christian Lauer
Jared Broad
Got it -- I think that is possible you'll just need to define a custom ScheduleEvent class.
Schedule.Add(new CustomEvent);
class CustomEvent : ScheduledEvent
{
public new DateTime NextEventUtcTime {
get { // Any UTC time.
return new DateTime(14,31,10);
}
}
}
PS: Instead of doing that you can also just bind to the event handler for your data and do what you need as soon as the data is ready?
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.
Michael Manus
maybe tickresolution? and enter a method every tick?hmm
Christian Lauer
Thank you for the answer, Jared. I don't completely understand it though. Maybe this is because I'm not familiar with C#. Could you please give me a Pyhton code snippet too?
I don't think it would be possible to link the event to the data because there is always data available in my rolling windows but I don't get the most recent minute bar at xx:xx:00 as the minute is not completed yet.
Kind regards,
Christian Lauer
Christian Lauer
I've never worked with tick resolution and I'm not too familiar with it but I don't see why I should use it here. Then the method would run many, many times instead of one per minute, wouldn't it?
Petter Hansson
I don't recommend using tick resolution unless you need it, simply because it's slow
JayJayD
I’d add a security with second resultion with fill forward. Then, in the OnData method I’d add something like this:
public override void OnData(Slice slice) { if (Time.Second == 10) { // Do stuff. } }
The drawback is that second resolution is slow.
Christian Lauer
Thank you for your answer. I already use second resolution. Why is it slow? And what do you mean by 'fill forward'? I think your idea should work. But when I try if self.UtcTime.Second == 10: I get an error saying
'datetime.datetime' object has no attribute 'Second'
Kind regards,
Christian Lauer
JayJayD
Is slow simply because you have so many more points to process each day.
Forget the fill forward stuff, is turned on by default. Just in case, check the docs.
I’m not familiar with the Python Lean, but I think you should use
if self.Time.Second == 10: #Do stuff
Christian Lauer
Oh, that's what you mean. I've experienced that. My backtests always take ages.
I've tried it like that before and got the same error. It's strange why it's not working like that in Python.
Kind regards,
Christian Lauer
Jared Broad
Would recommend you share your algorithm so people can suggest the best solution =) handling the event from the consolidator will be fastest and "right" solution.
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.
Christian Lauer
Any of the algorithms I posted would do as the structure is always the same. Suppose in this one I wanted to run the Tensec function only once per minute es described above.
Kind regards,
Christian Lauer
Michael Handschuh
I'm a little late to the party here but it is possible to define your own ITimeRule for the scheduled event functions. Here's the interface code in LEAN:
The function you need to implement accepts an enumerable of dates provided via the IDateRule used when configuring the event. I'm no pythonista, but I've attached a C# version of what I think you're asking for.
That being said, if the reason you're wanting a 10 second delayis because the data isn't there yet then perhaps the code is better placed inside the consolidator's event handler. This would guarantee that it runs as soon as the data is received.
Christian Lauer
Thank you for your answer. This is exactly what I was looking for. Knowing nothing about C# and having limited programming knowledge in general I'm unable to transfer this into Python unfortunately. I would be very glad if anyone could do that for me.
Kind regards,
Christian Lauer
Christian Lauer
I've tried to simplify this by leaving out the part about the market hours and tried to create a class in Python but I think it's still far from working. I'm just not familiar with C# and don't know how to modify the commands so that they work in Python. Here's my try:
class TenSecondsAfterEveryMinute(ITimeRule): def __init__(self): self.IEnumerable<DateTime> CreateUtcEventTimes(IEnumerable<DateTime> dates) minutesPerDay = range(0, 1440) return from date in dates from minute in minutesPerDay let time = date.AddMinutes(minute).AddSeconds(10) select.time
It would be nice if someone showed me how this is done correctly.
Kind regards,
Christian Lauer
Sdoof
Hi Christian
Has anyone evr got back to you on this? I think this is a very interesting question and I too would love to know how to do this in Python.
Let me know.
AHARON ZBAIDA
Regarding Jared's earlier solution, I need an event fire at 09:34:30AM each trading day. I have jimmied the following code, but on back test it fires at 12:00. Can anyone here suggest what the problem is?
public override void Initialize()
{
...
Schedule.Add(new NineThirtyFourThirty());
{
Debug("Scheduled for 9:34:30, Fired: " + Time);
}
}
namespace QuantConnect {
class NineThirtyFourThirty : ScheduledEvent {
public NineThirtyFourThirty()
: base("NineThirtyFourThirty", new DateTime(), null){}
public new DateTime NextEventUtcTime {
get { // Any UTC time.
return new DateTime(9,34,30);
}
}
}
}
console output:
594 | 14:59:32:
Scheduled for 9:34:30, Fired: 6/3/2018 12:00:00 AM
AHARON ZBAIDA
sorry, never mind, I found the solution with:
Schedule.On(DateRules.EveryDay("SPY"), TimeRules.At(9, 34, 30), () =>
{
Debug("Scheduled for 9:34:30, Fired: " + Time);
});
Christian Lauer
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!