Friday, July 19, 2013

Downloading Yahoo IEOD charts

The following python script downloads yahoo 1 day charts and saves them for you.  I personally use this as IEOD data is available only at a price. The scripts are stored with a directory for each day. This might not provide continuity for most users but I found it useful to see the correlation between the stocks on a particular day. This script has been tested on Python 2.7 and requires no extra libraries. The code of getNIFTY.py is given below.
import urllib
import csv
import os
from datetime import date
fi = open("symbolList.csv", "rb")
symList = csv.reader(fi)
dt = date.today()
if not os.path.exists("./Intraday_YAHOO"):
    os.makedirs("./Intraday_YAHOO")
direc = "./Intraday_YAHOO/"+str(dt.year)+str(dt.month).zfill(2)+ \
str(dt.day).zfill(2)
if not os.path.exists(direc):
    os.makedirs(direc)

for row in symList:
 print row[0],row[1]
 url_chart = "http://chart.finance.yahoo.com/z?s=" + row[0] + "&t=1d&q=&l=off&z=m&a=v&p=s&lang=en-US&region=US"
 file_chart = direc+"/"+row[1]+".jpg"

 urllib.urlretrieve(url_chart, file_chart)
Create symbolList.csv with the format (<Yahoo symbol>, <image_finename>). An example is given below.
^NSEI,NSEI
ACC.NS,ACC
AMBUJACEM-EQ.NS,AMBUJACEM-EQ
AXISBANK.NS,AXISBANK
The folder structure that is generated should look somewhat like this.
~\
|   getNifty.py
\---Intraday_YAHOO
\---20130716
    |   ACC.jpg
    |   AMBUJACEM-EQ.jpg
    |   AXISBANK.jpg
\---20130717
\---20130718
\---20130719

No comments:

Post a Comment