Back

How can I schedule an event handler and have the data available?

I know how to register a scheduled event handler with the schedule.On method. But the Action handler doesn't have a parameter to provide the data. On the other hand, the OnData method have the data parameter, but I don't know how to trigger it only at specific timings.

How can I schedule an event handler which only get triggered at specified date/time rules?

Update Backtest








Looks like the equivalent of the Slice parameter for the the handler registered by schedule.On is the History method.

0

Can you explain what you are trying to accomplish by doing this? We may be able to provide a better answer. However with the information provided there are a couple of options:

 

1) cache the slice data and then use it with a schedule. As an example:


#declare a variable to cache with
Slice lastSlice = null;

public override void Initialize()
{
Schedule.On(DateRules.EveryDay(), TimeRules.Every(new TimeSpan(0, 5, 0)), () =>
{
if (lastSlice != null)
{
// use cached data here
}
});
}

public void OnData(Slice data)
{
lastSlice = data;
}

 

2) Use consolidators: see Consolidating Data to Build Bars

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.


In a simlar program I wrote in Quantopian's API:

def after_open(context, data):
# Do some calculations here and probably send orders

def initialize(context):
schedule_function(after_open,
date_rules.every_day(),
time_rules.market_open(minutes=1)
)

Like the above snippet, I was expecting there is a *data* parameter representing the view of data at the point of the time that I can use in the handler passed to schedule.On.

The 'caching' approach was a workaround I thought of too. But it's less optimal in terms of ergonmics and code smell, plus it would imply that the OnData callback needs to be triggered on every data entry which is proportional to the data resolution, rather than just the timings I scheduled.

 

0

Ah, sorry for spamming the thread as I cannot find a way to edit my comments.

I forgot mentioning I have to calculate the symbol of the rolling futures from the symbol of the continuous futures. I need the information in the scheduled event handlers.

The FuturesChain property can only be accessed from the Slice class passed to OnData AFAICT.

 

0

Mura1, 

Ray is correct in his answer: it depends on what you're trying to accomplish.

Slices are provided to OnData, so you can save a reference to the latest slice in a field of your QCAlgorithm-derivative class, and in your callback method (you pass a reference to that method as a parameter to Schedule.On) you check the field, and if it's not-null, you've got the latest data slice available.

They could have designed this differently with your method taking a Slice parameter, but that would have meant that other scheduled methods - ones that might not use or need a Slice - would have to take the parameter as well.

So the simplest thing:

- Create an OnData method to save off each Slice

- Create a callback method that uses the latest Slice

- Pass a callback method ref to Schedule.On

Hope this helps!

0

How about reversing the question: is it possible to create and store a schedule object against which one can perform a check inside OnData that "the time is right" to do something? A schedule object (created and stored inside self instance of the algoritm during Initialize call can simply be a dictionary of (timestamp, action) pairs where action can have any signature you want.  Then, while inside OnData(self,slice), you can simply do:

action=self.schedule_dict.get(slice.Time,None)

if action is not None:

       # build required inputs and call action

If it were possible to extract the schedule somehow from the algo after calling Schedule.On function it would considerably simplify the task, of course.

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