Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas yahoo finance real time data

Is it possible to get real time stock data with pandas from yahoo finance?

For historical data i would do the following:

import pandas.io.data as web
aapl = web.get_data_yahoo('AAPL')

Is there a way to get the latest aapl price?

like image 491
Uninvited Avatar asked Jan 17 '26 15:01

Uninvited


2 Answers

EDIT:

Yahoo has ended their free finance API so this answer is no longer relevant. Below is my answer for pre-2019 purposes.

Archival:

There's plenty of libraries available for this. Pandas doesn't explicitly do this though.

Most simply, I would suggest you just use a web library to download yahoo data. I like using requests, but you could also use urllib. You can coerce the response into a data frame after you get it.

import requests
requests.get("http://download.finance.yahoo.com/d/quotes.csv?s=AAPL&e=.csv&f=nsl1op").text

The nsl1op format var is from the docs:

I will use here the tags of name(n), symbol(s), the latest value(l1), open(o) and the close value of the last trading day(p)

Your response data should look something like

u'"Apple Inc.","AAPL",114.63,113.50,115.07\r\n'

You can just split up the string using the csv library and throw it into a data frame from there

like image 199
Greg Avatar answered Jan 20 '26 05:01

Greg


To answer your question about using Pandas specifically, you can pull stock data from yahoo using pandas like so:

from pandas.io.data import DataReader
from datetime import datetime

aapl = DataReader('AAPL',  'yahoo', datetime(2015,7,1), datetime(2015,7,1))
print(aapl['Adj Close'][0])

This code results in:

126.599998

The other keys you can use are Open, Close, High, Low, and Volume.

Keep in mind that the returned dataset is an array. You need to enumerate said array to get the data, either by specifying your index, or with a for loop.

like image 43
DevOops Avatar answered Jan 20 '26 05:01

DevOops



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!