Back

More complex event scheduling

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

Update Backtest








I would like this too. Please make an issue at the Github project for Lean and I will comment there to "vote" on it.

0

Probably best way is:

Schedule.On(DateRules.EveryDay(), TimeRules.Every(TimeSpan.FromSeconds(10)), () => {

if (Securities["SPY"].Exchange.ExchangeOpen) {

}

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


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

0

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?

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.


maybe tickresolution? and enter a method every tick?hmm

0

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

1

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?

0

I don't recommend using tick resolution unless you need it, simply because it's slow

0

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.

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.


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

0

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

1

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.


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

0

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.

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.


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

1


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: 

https://github.com/QuantConnect/Lean/blob/master/Common/Scheduling/ITimeRule.cs

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. 

1

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.


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

0

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

2

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. 

2

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

0

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);
            });

0

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