Back

Closing Price for First Bar of Day

Somebody please tell me how to capture the closing price of the first bar of the day (using minute resolution) and remember that price all day, then replace it with the closing price of the first bar for each successive day. data[symbol].Close will capture it, but that gets replaced with each successive bar of the day. There has to be a way of doing it (simple way would be better :), but I haven't found it. Help please, and thank you.
Update Backtest








Or maybe I have found it :( Scheduled event maybe?
0

Hey Oran, you're on the right track. I think the cleanest way to do it is probably via a scheduled event set to run at (assuming minute data) 9:31:01. Note the extra second here, this is to be sure we've received the 9:30-9:31 minute bar. Typically your algorithm will receive the bars within 10ms, but if you're doing minute data it's safe to run it a full second after 9:31, then you could just access the Securities["SPY"].Close property for the closing value of the first minute! Post back with your scheduled event solution, or if you run into any issues!
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.


Issues? No, never :)

Thanks, Michael. As always - you're the man with the answers! Now let me see if I can make it work.
0

I'm not familiar with scheduled events, but another simple way to consider is to use the end of day method.

private decimal YesterdaysClose { get; set; }

public override void OnEndOfDay()
{
this.YesterdaysClose = Securities["SPY"].Close;
}


The only down fall to this is you'll have to ensure a full day has passed before your algorithm can get access to this.

To get around this you could

1. Set YesterdaysClose in Initialize maybe.
2. Or, Warmup 1 day. This is probably more reliable.

public override void Initialize()
{
this.SetWarmUp(TimeSpan.FromDays(1));
...
}


And ensure your OnData doesn't process while warming up:

public override void OnData(Slice data)
{
if (this.IsWarmingUp) return;
1

@Michael - You wanted me to post back with my solution, so here it is. I have been playing with Scheduled Event and it seems to be working (mostly). See if I am doing it right. I say "mostly" because when I ran a backtest and checked the log, everything looks as expected except for 1/13/2016 and 1/14/2016. Both days captured a price of $193.95 which is way off from what it should be for either day - at least from the Thinkorswim chart I looked at. I can't see where that figure came from! Other than that, I "think" it is working. The buys and sells are as expected, and the captured price is correct for the previous days.

Don't fall out of your chair laughing at the strategy. It was not meant to be the greatest. I just wanted to do "something" with the price I had captured. As I said, this was just a learning exercise. Your thoughts?
0


After re-reading your post above, I saw that you suggested using Securities["SPY"] to capture the closing price. I used data[symbol].Close where the symbol was "SPY". I don't know what the difference is but I changed that one word and ran it again. It changed all the numbers - buys, sells and captured dailyLevels. But it still captured the $193.95 for the 13th and 14th. Any clues?
0

A couple things going on here. Each time you call the scheduler (Schedule.On(...)) it schedules a new series of events, so it's best to place these calls inside the Initialize method. The way it's coded above will create a new series of events on every bar. The issue you encountered using the 'data' variable has to deal with closures. Here's a link I found giving a pretty good explanation of closures. The short part of the story is that the () => { ... } syntax creates a new function which captures the 'data' variable, so each time it is called it will reuse the 'data' variable from when the function was created. Read through the link and please feel free to post back with any questions (especially surrounding the closure concept).

As for the data on the 13th and 14th, it looks like the data just didn't make it to the backtesting box yet. Anytime you see a 'flat-line' at the end of an algorithm, it's typically just because fill forward is on but there's actually no new data remaining, so when you ran it looks like the data was current up until 1/12/16. I just ran and had data up to and including 1/14/16.
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.


Thanks Michael, I'm still learning that it's not just learning what to type, you have to learn where to type it as well. I had originally typed it in the initialize section, which (in my mind anyway, correct me if I'm wrong) starts at public override void Initialize (), and goes to public void OnData(TradeBars data). . It didn't work. I see now, my mistake was that I put it in the right section, but before the "AddSecurity(...)" line. I tried again with it after "AddSecurity(...)" and it works.

I read the link you gave above (probably 10 times) and it's still all mud. It uses a lot of words and terms that I don't know what they mean, so I am now trying to learn those meanings so that I can understand the article. This is going to take some time!

In the meantime, can you (or someone) please explain to me the difference between "Securities[symbol].Close" and "data[symbol].Close" and when each should be used? I think I hear you saying that it "has to deal with closures", but "closures" is one of those words I am learning the meaning of. For now, can I get a plain English lesson on the two?

Thanks to everyone for your patience and help. I hate being a dumb newbie, but we all have to start somewhere!
0

The Securities object is a dictionary of all of your securities, indexed by the symbol. This is available from anywhere within the algorithm as Securities is a property of the algorithm. It has properties such as the current Price, even the Open/High/Low/Close of the most recent bar. The data object is a parameter that is passed into your algorithm on each time step. This includes all the data for the current point in time.

Now things become slightly more complicated in the scheduled events case. When you schedule an event, you're scheduling for a specific thing (function/method/action) to run given some conditions (time of day). In the example you posted, the specified thing to run was defined using the data parameter, which means each time it runs it will use the same data parameter instead of seeing the new value. This is a fairly advanced topic in C# and can be kind of hard to conceptually understand, so the short answer is that Securities[symbol].Close will always give you the most recent closing price no matter what, but data[symbol].Close will give you the closing price from that data collection, which may or may not be the most recent.

One last thought, you're clearly not dumb since you're on this platform and figuring stuff out! The smartest of people are just smart enough to know what they do not know and to where find what they do not know!
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.


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