=> Downloads historical Stock Prices for a company from Yahoo Finance website.
=> Calculates the 10 day moving average.
=> Generate the graph for the data
a) Necessary Imports:
1. Import Panda. Panda is a data analysis library of python. Pandas has tool to read and write between in-memory data structures and different file formats. It has efficient data frame object for data manipulation with better indexing support.
2. Import Datetime: Provides classes to manipulate date and time objects. Api to convert between different file formats.
3. Import matplotlib: Python 2D plotting library , simple to use and genrates graphs, plots etc with few lines of code.
4. Import Numpy : Scientific computing package for python,N-Dimensional array object ,Linear algebra related functions.
5. Import urlib: URL handling module for python
import pandas as pd import pandas.io.data from datetime import timedelta import datetime as dt from pandas import Series, DataFrame import matplotlib.pyplot as plt import matplotlib as mpl import urllib.request import numpy as numpy from datetime import datetime from matplotlib.pyplot import * import matplotlib.dates as mdates
b) Create a class called Stock
The main(Init) function should accept as parameter the company symbol, lookback period, window size,and the end date.
def __init__(self,symbol,lookback_period,window_size,end=datetime.date.today()):Suppose your lookback period is 100 then get prices for 100 days.
SO to get prices for 100 days subtract from the end date the lookback period.
But for that you need to use same format .
In this example I have used timedelta. Time delta helps you get the start date by specifying the number of days from given date. Like your end date is today and you want stoc prices for last 100 days ,we can use timedelta.
start = end - timedelta(days=lookback_period)c) Convert date into required format.
start_date = start.isoformat()d) get the required Url for data analysis.
url = "http://ichart.finance.yahoo.com/table.csv?s={0}".format(symbol)
url += "&a={0}&b={1}&c={2}".format(start_month,start_day,start_year)
url += "&d={0}&e={1}&f={2}".format(end_month,end_day,end_year)
e) Parse data
df = pd.read_csv(urllib.request.urlopen(url))f) Get the moving average
#get the adj close from csv
saved_column = df['Adj Close']
#get the matching date
y_data = df['Date']
def movingaverage(self,interval, window_size):
window = numpy.ones(int(window_size))/float(window_size)
return numpy.convolve(interval, window, 'same')
Moving Average smooths price fluctuations by removing the noise. It computes the averages of a subset of full data set.
Moving Average Wiki
g) Generate the graph
x_data = x_points[0:70]
#get the moving average
y_av = self.movingaverage(saved_column,window_size)
#generate graph
figure("Plot of stocks")
x = [dt.datetime.strptime(d,"%Y-%m-%d").date() for d in x_data]
gca().xaxis.set_major_formatter(mdates.DateFormatter("%Y-%m-%d"))
gca().xaxis.set_major_locator(mdates.DayLocator())
plot(x,y_av,'r')
plot(x,close,'g')
gcf().autofmt_xdate()
xlabel("Date")
ylabel("adjusted close")
show()
GIT
Oh my goodness! Impressive article dude! Many thanks, However I am
ReplyDeletegoing through troubles with your RSS. I don'tknow
why I cannot subscrobe to it. Is there anybody else ggetting the same
RSS problems? Anybody who knows thee solution can you kindly respond?
Thanks!!
My blog post - spanish jokes in english
I am in fact grateful to the owner of this web page who has shared this wonderful paragraph at at this place.
ReplyDeleteAlso visit my web blog :: silkn flash and go review