Question

I'm trying to fetch data from the Yahoo finance API via Joe C's method described here: Download history stock prices automatically from yahoo finance in python

However, when I try to pass additional parameters about the date, Yahoo finance seems to ignore these parameters and returns a list of prices from the beginning of the stock's existence.

Is there an easy way to get the data for a certain date range or should I just process the results manually?

Thanks for your help.

Was it helpful?

Solution

To get data for a particular date range, you need to modify the make_url as shown below,

def make_url(ticker_symbol,start_date, end_date):
    print ticker_symbol
    a = start_date
    b = end_date
    dt_url = '%s&a=%d&b=%d&c=%d&d=%d&e=%d&f=%d&g=d&ignore=.csv'% (ticker_symbol, a.month-1, a.day, a.year, b.month-1, b.day,b.year)
    return base_url + dt_url

To use this function, you need to do the following,

import datetime
s = datetime.date(2012,1,1)
e = datetime.date(2013,1,1)
u =  make_url('csco',s,e)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top