Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
how to get yesterday's close?

Hey guys, loving this site by the way!

Newbie here, but curious as to how I can get yesterdays closing price, say I want to compare todays open vs yesterdays close, how can I do this? so if today's open is X.XX% above or below yesterdays close then do Y.

Thanks!!

6 responses

Hello Samuel,

Here's an example. Note, however, that the example is for daily data. If you are looking to submit a trade in the morning after the opening, then different code is needed, that will compare data from the last minute of the prior day to the first minute of the current day. With daily data, you can submit an order, but it won't be filled until the next day, at opening (after a whole day has passed).

The code for the minute-level trading is straightforward, too. If you elaborate on what you'd like to do, I (or someone else) can provide an example.

Grant

Grant,
This is an interesting code snippet. How can you trade a basket of say 100 stocks (nasdaq 100), using the following criteria
1. Buy next morning at open (day 1) when the previous days close (day 2) was below the 3 day MA of the lows (ie. ma of lows of days 3).
2. You hold it for 3 days or if it profit target of 10% or stop loss of 10%.
3. You trade only 1/100 of your capital per trade, but if the signal comes again when you are holding the stock, you buy again. But only 1 additional buy is allowed. you are thus risking a very small portion of your capital per stock.
4. You are allowed a margin of 100% only.
This is a variation of the dip buyer I am looking at.
Thank you.
Maji

Amritendu,

There is a subtlety that might matter, but I'd be inclined not to worry about it at this point. The issue is that to get the actual opening price when the order gets executed, you have to submit the order one tic prior, which would need to be the final minute of the prior trading day. If you can wait to submit the order at opening, then it will be filled (typically) during the second minute of the trading day.

Also, I think you are talking about tracking up to 100 stocks in parallel, making individual buy/sell decisions on each, right? I don't know how to do that in Quantopian, although I expect that it is doable. I'll think about it, and maybe post a separate question to the forum. Or maybe Peter has provided an example in https://www.quantopian.com/posts/a-sample-dip-buying-system-need-help-with-error?

Grant

Hey guys, thanks for the replies.

Grant this is what I want to code up:

Yesterday must have been a down day by 0.25%
If X opens DOWN more than 0.1%, go long and exit on the close.

Just a sample strategy off the net, but more interested in HOW to code something like that up rather than the actual strategy yet.

If you(or anyone) could help show me how, that'd be amazing, thanks!!

Hello Samuel,

I don't have time right now, but for starters, I suggest having a look at the datetime functionality described in the Quantopian help/API docs. You can also search the forum. Also, if you search the forum for info. on early closes/closings, you can see how to handle days when the market closes early.

Grant

Samuel - For that type of algorithm, you can only run it on minute data, since that is the only way to submit an order and get it filled in on the same day. For each day you'll just need to keep track of yesterday's change and today's open. However, there are some additional complexities with doing market open and market close orders. You'll be dealing with minute data, so you can hardcode trades to go through close to the first minute of the day and the last minute of the day. However, you'll also need to keep track of DST and early closing days. Search the forum a bit for solutions for those.