Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How do I get the ACTUAL Current Price for a Specific Stock

I have a list of specific stocks I am looking at and I need their price at that part of the day specified by my schedule_function.
To test I have been using

for sids in context.sids :
current_price = data.current(sids,'price')
close_price = data.current(sids,'close')
percent_change = ((current_price - close_price)/close_price)*100
print(sids)
print(percent_change)

My issue is that current_price is grossly misleading. I need the ACTUAL current price, not the previous close price.

3 responses

Not sure what you mean by 'actual current price'. Here are the definitions of the various prices available from the 'data.current' method (see docs https://www.quantopian.com/help#api-data-methods ). All of these will be as of the previous minute of trading which is the last known or current trade bar.

'price' returns the last known close price of the asset. If there is
no last known value (either because the asset has never traded, or
because it has delisted) NaN is returned. If a value is found, and we
had to cross an adjustment boundary (split, dividend, etc) to get it,
the value is adjusted before being returned. 'price' is always
forward-filled.

'open', 'high', 'low', and 'close' return the relevant information for
the current trade bar. If there is no current trade bar, NaN is
returned. These fields are never forward-filled.

Use the 'close' price to get the price as of the close of the previous minute of trading. This can be NaN if the security did not trade during the last minute. What you may be seeing (and why you feel that current_price is grossly misleading) is that the 'price' field is forward filled, so, could actually be a close price from many minutes ago?

If you are trying to calculate the percent change from the close of the previous day then you will want to use the 'data.history' method. The last row will be the last minutes close price and the second from last row will be yesterdays close price (note the frequency is '1d' and not minutes).

close_prices = data.history(sids,  'close', 2, '1d')  
previous_day_close = close_prices.iloc[-2]  
last_minutes_close = close_prices.iloc[-1]  
percent_change = ((last_minutes_close - previous_day_close)/previous_day_close)*100

The above will return a series indexed by security.
Perhaps a more concise way...

percent_change = data.history(sids,  'close', 2, '1d').pct_change() * 100

This returns a dataframe with a column for each security. The last row will be the percent changes.

Note that in the original example the values 'current_price' and 'close_price' will be identical if the security traded in the last minute. Otherwise 'close_price will be NaN.

current_price = data.current(sids,'price')  
close_price = data.current(sids,'close')

Oh I had a fundamental misunderstanding, thank you for clarifying. I assumed that the close price meant most recent DAY close price. Thank you for clearing that up for me. I'll use the data.history instead.

No problem. Good luck!