Search code examples
pythonpython-2.7yahoo-finance

Python: Loop through a list of variables


I need to loop through multiple ticker symbols into my URL that I am constructing. I am not sure if this is the best way to go about this.

YahooFinance url builder

from datetime import datetime

def constructYFURL(ticker,start_date,end_date,freq): start_date = datetime.strptime(start_date,"%Y-%m-%d").date() end_date = datetime.strptime(end_date, "%Y-%m-%d").date()

s=ticker.replace("^","%5E")
if start_date.month-1<10:
    a="0"+str(start_date.month-1)
else:
    a=str(start_date.month-1)

b=str(start_date.day)

c=str(start_date.year)

if end_date.month-1<10:
    d="0"+str(end_date.month-1)
else:
    d=str(end_date.month-1)


e=str(end_date.day)

f=str(end_date.year)

g=freq

yfURL = "http://real-chart.finance.yahoo.com/table.csv?s="+s+"&a="+a+"&b="+b+"&c="+c+"&d="+d+"&e="+e+"&f="+f+"&g="+g+"&ignore=.csv"
return yfURL


webRequest = urllib2.Request(urlOfFile)

try:
    page=urllib2.urlopen(webRequest)

    content=page.read()

    with open(filePath,"wb") as output:
        output.write(bytearray(content))



except urllib2.HTTPError, e:
    print e.fp.read()

ticker = ["FB","C"]

for x in ticker:
    start_date= "2015-01-01"
    end_date= "2017-02-23"
    freq = "d"
    yfURL = constructYFURL(ticker,start_date,end_date,freq)
    fileName="YF_"+str(ticker)+".csv"
    print yfURL
    localFilePath="/Users/student/Downloads/datalibrary/"
    download(localFilePath+fileName,yfURL)

Solution

  • This should help get you closer. urlOfFile is not defined and download() is not a python module so you will have to fix that. It now builds the URL correctly if you comment out those pieces.

    import urllib2
    from datetime import datetime
    
    def constructYFURL(x, start_date, end_date, freq): 
        start_date = datetime.strptime(start_date, '%Y-%m-%d').date()
        end_date = datetime.strptime(end_date, '%Y-%m-%d').date()
        s = x.replace('^', '%5E')
    
        if (start_date.month - 1) < 10:
            a = '0' + str(start_date.month - 1)
        else:
            a = str(start_date.month - 1)
    
        b = str(start_date.day)
        c = str(start_date.year)
    
        if (end_date.month - 1) < 10:
            d = '0' + str(end_date.month - 1)
        else:
            d = str(end_date.month - 1)
    
        e = str(end_date.day)
        f = str(end_date.year)
        g = freq
    
        yfURL = 'http://real-chart.finance.yahoo.com/table.csv?s='+s+'&a='+a+'&b='+b+'&c='+c+'&d='+d+'&e='+e+'&f='+f+'&g='+g+'&ignore=.csv'
        return yfURL
    
    def main():
        webRequest = urllib2.Request(urlOfFile)
        try:
            page = urllib2.urlopen(webRequest)
            content = page.read()
            with open(filePath,'wb') as output:
                output.write(bytearray(content))   
        except urllib2.HTTPError as e:
            print e.fp.read()
    
        ticker = ['fb', 'c', 'goog']
        start_date = '2015-01-01'
        end_date = '2017-02-23'
        freq = 'd'
        localFilePath = '/Users/student/Downloads/datalibrary/'
    
        for x in ticker:
            yfURL = constructYFURL(x, start_date, end_date, freq)
            fileName = 'YF_' + x + '.csv'
            print yfURL    
            download(localFilePath + fileName, yfURL)
    
    if __name__ == '__main__':
        main()