In [1]:
import urllib
base_url = "http://ichart.finance.yahoo.com/table.csv?s="
def make_url(ticker_symbol):
return base_url + ticker_symbol
In [2]:
output_path = "/Users/vakilp/Documents/StockInvestingExperiments"
def make_filename(ticker_symbol, directory="S&P"):
return output_path + "/" + directory + "/" + ticker_symbol + ".csv"
def pull_historical_data(ticker_symbol, directory="S&P"):
try:
urllib.urlretrieve(make_url(ticker_symbol), make_filename(ticker_symbol, directory))
except urllib.ContentTooShortError as e:
outfile = open(make_filename(ticker_symbol, directory), "w")
outfile.write(e.content)
outfile.close()
In [3]:
pull_historical_data('ATEN')
In [4]:
from pandas.io.data import DataReader
from datetime import datetime
In [5]:
DataReader?
In [6]:
goog = DataReader("GOOG","yahoo")
In [7]:
goog
Out[7]:
In [8]:
import matplotlib.pyplot as plt
import numpy as np
In [9]:
plt.plot(goog.High)
Out[9]:
In [10]:
%matplotlib inline
In [11]:
goog.T
Out[11]:
In [12]:
goog.at
Out[12]:
In [13]:
goog.axes
Out[13]:
All the above commands are using Pandas module for python....still dont know how to plot against time yet. Another option to look at is Yahoo's finance package
In [14]:
goog?
In [15]:
goog.T
Out[15]:
In [16]:
goog.High
Out[16]:
In [17]:
goog
Out[17]:
In [18]:
goog.columns
Out[18]:
In [19]:
goog.plot.im_self
Out[19]:
In [20]:
plt.plot(goog.index,goog['High']);
In [21]:
%matplotlib qt
In [22]:
plt.plot(goog.index,goog['High']);
In [23]:
plt.plot(goog.index,goog['Low']);
In [24]:
%matplotlib inline
plt.plot(goog.index,goog['High'],goog.index,goog['Low'])
plt.grid()
In [25]:
goog.High[0]
Out[25]:
In [26]:
a=[];
for i in range(1,len(goog)):
if(goog.High[i]<goog.High[i-1]):
a.append(i)
In [27]:
plt.plot(a)
Out[27]:
In [28]:
a[0:10]
Out[28]:
In [29]:
b=[];
for i in range(1,len(goog)):
if(goog.High[i]<=goog.High[i-1]):
b.append(i)
In [30]:
b[0:10]
Out[30]:
In [31]:
len(a)
len(b)
Out[31]:
In [32]:
len(a)
Out[32]:
In [33]:
len(b)
Out[33]:
In [34]:
a=np.array(a)
b=np.array(b)
In [35]:
plt.plot(a-b)
Out[35]:
So both the outputs wheter I have <= or < are the same for this set of data
In [36]:
c=[];
for i in range(1,len(goog)):
if((goog.High[i]<=goog.High[i-1]) and (goog.Low[i]>=goog.Low[i-1])):
c.append(i)
In [37]:
len(c)
Out[37]:
In [38]:
a_diff = np.diff(a)
In [39]:
%matplotlib qt
In [40]:
plt.plot(a_diff)
Out[40]:
In [41]:
idx = 0;
d = []
for i in range(1,len(goog)):
if(goog.High[i]<=goog.High[i-1]):
idx = idx + 1;
else:
idx = 0;
if idx==3:
d.append(i)
In [42]:
len(d)
Out[42]:
In [43]:
np.transpose(b)
Out[43]:
In [44]:
goog.High[4:7]
Out[44]:
In [45]:
np.transpose(d)
Out[45]:
In [46]:
goog.High[17:20]
Out[46]:
In [47]:
goog.High[8]
Out[47]:
In [48]:
goog.Low[4:7]
Out[48]:
In [49]:
class box(object):
high = []
low = []
box1 = box()
In [50]:
box1.high.append(300)
In [51]:
box1.high
Out[51]:
In [52]:
xx = goog.High[4:7]
xx.max()
Out[52]:
In [53]:
class box(object):
high = []
low = []
box1 = box()
idx = 0;
d = []
index = 0;
for i in range(1,len(goog)):
if(goog.High[i]<=goog.High[i-1]):
idx = idx + 1;
else:
idx = 0;
if idx==3:
d.append(i)
high_vals = goog.High[i-3:i]
low_vals = goog.Low[i-3:i]
box1.high.append(high_vals.max())
box1.low.append(low_vals.min())
In [54]:
box1
Out[54]:
In [55]:
box1.high
Out[55]:
In [56]:
len(box1.high)
Out[56]:
So, the good thing here is that number of elements in box1.high is same as we expect
In [57]:
len(box1.low)
Out[57]:
In [58]:
np.argmin(goog.Low[4:7])
Out[58]:
In [59]:
goog.Low[4:7]
Out[59]:
In [60]:
class box(object):
high = []
low = []
date_high = []
date_low = []
box1 = box()
idx = 0;
d = []
index = 0;
for i in range(1,len(goog)):
if(goog.High[i]<=goog.High[i-1]):
idx = idx + 1;
else:
idx = 0;
if idx==3:
d.append(i)
high_vals = goog.High[i-3:i]
low_vals = goog.Low[i-3:i]
box1.high.append(high_vals.max())
box1.low.append(low_vals.min())
box1.date_high.append(goog.index[i-3])
box1.date_low.append(goog.index[i-3+np.argmin(low_vals)])
high = np.array(box1.high)
low = np.array(box1.low)
date_high = np.array(box1.date_high)
date_low = np.array(box1.date_low)
In [61]:
plt.plot(date_high,high,'o',goog.index,goog.High,date_low,low,'ro',goog.index,goog.Low);
In [62]:
import matplotlib.patches as patches
So, clearly the rectangle is not working yet. Need to work that out
In [63]:
from matplotlib.patches import FancyBboxPatch
def draw_bbox(ax, bb):
# boxstyle=square with pad=0, i.e. bbox itself.
p_bbox = FancyBboxPatch((bb.xmin, bb.ymin),
abs(bb.width), abs(bb.height),
boxstyle="square,pad=0.",
ec="k", fc="none", zorder=10.,
)
ax.add_patch(p_bbox)
In [64]:
import matplotlib.transforms as mtransforms
bb = mtransforms.Bbox([[0.3, 0.4], [0.7, 0.6]])
ax = plt.subplot(111)
In [65]:
draw_bbox(ax,bb)
In [66]:
plt.draw()
In [67]:
plt.plot(date_high,high,'o',goog.index,goog.High,date_low,low,'ro',goog.index,goog.Low);
In [68]:
plt.clf()
plt.plot(date_high,high,'o',goog.index,goog.High,date_low,low,'ro',goog.index,goog.Low);
In [69]:
plt.close()
plt.plot(date_high,high,'o',goog.index,goog.High,date_low,low,'ro',goog.index,goog.Low);
In [70]:
plt.subplot(111)
plt.plot(date_high,high,'o',goog.index,goog.High,date_low,low,'ro',goog.index,goog.Low);
plt.draw()
In [71]:
box1
Out[71]:
Things to figure out about plotting:
In [72]:
%matplotlib inline
In [73]:
bb = mtransforms.Bbox([[0.3, 0.4], [0.7, 0.6]])
ax = plt.subplot(111)
draw_bbox(ax,bb)
In [74]:
bb.corners
Out[74]:
In [75]:
ax = plt.subplot(111)
plt.plot(date_high,high,'o',goog.index,goog.High,date_low,low,'ro',goog.index,goog.Low);
draw_bbox(ax,bb)
In [76]:
%matplotlib qt
In [77]:
ax = plt.subplot(111)
plt.plot(date_high,high,'o',goog.index,goog.High,date_low,low,'ro',goog.index,goog.Low);
draw_bbox(ax,bb)
In [78]:
bb = mtransforms.Bbox([[box1.date_high[1], box1.high[1]], [box1.date_low[1], box1.low[1]]])
In [79]:
box1.date_high[1]
Out[79]:
In [80]:
datetime.now()
Out[80]:
In [81]:
datetime.ctime?
In [82]:
datetime.ctime(datetime.now())
Out[82]:
In [83]:
datetime.ctime(box1.date_high[1])
Out[83]:
In [84]:
import matplotlib.dates as mdates
In [85]:
mdates.date2num(datetime.fromtimestamp(box1.high[1]))
Out[85]:
In [86]:
mdates.num2date(719162.7978846341)
Out[86]:
In [87]:
datetime.timetuple(box1.date_high[1])
Out[87]:
In [88]:
datetime.fromtimestamp(box1.high[1])
Out[88]:
In [89]:
datetime.fromtimestamp(1)
Out[89]:
In [90]:
mdates.datestr2num(datetime.ctime(box1.date_high[1]))
Out[90]:
In [91]:
mdates.num2date(735345)
Out[91]:
In [92]:
mdates.datestr2num(datetime.ctime(box1.date_high[]))
In [93]:
x = box1.date_high[1]
In [94]:
x.to_datetime()
Out[94]:
In [95]:
mdates.date2num(x.to_datetime())
Out[95]:
In [96]:
mdates.num2date(mdates.date2num(x.to_datetime()))
Out[96]:
The output of the read from finance.yahoo.com is timestamp class. To work with rectangles, I need x values that indicate time. The rectangles and bboxes work only with numbers. So, I need to convert the timestamp I have into a number. The way to do is as follows:
In [97]:
x_high = box1.date_high[1]
In [98]:
x_high = x_high.to_datetime()
In [99]:
x_high
Out[99]:
In [100]:
mdates.date2num(x_high)
Out[100]:
In [101]:
x_low = box1.date_low[1]
In [102]:
x_low = x_low.to_datetime()
In [103]:
mdates.date2num(x_low)
Out[103]:
In [104]:
box1.low[1]
box1.high[1]
Out[104]:
In [105]:
bb = mtransforms.Bbox([[np.float(x_high), np.float(box1.low[1])], [np.float(x_low), np.float(box1.high[1])]])
ax = plt.subplot(111)
draw_bbox(ax,bb)
In [106]:
type(box1.low[1])
Out[106]:
In [107]:
type(np.float(box1.low[1]))
Out[107]:
In [108]:
type(x_high)
Out[108]:
In [109]:
x_low = mdates.date2num(x_low)
x_high = mdates.date2num(x_high)
In [ ]:
bb = mtransforms.Bbox([[np.float(x_high), np.float(box1.low[1])], [np.float(x_low), np.float(box1.high[1])]])
ax = plt.subplot(111)
draw_bbox(ax,bb)
In [ ]:
plt.show()
In [ ]:
%matplotlib inline
draw_bbox(ax,bb)
In [ ]:
bb
In [ ]:
plt.show()
In [134]:
ax=plt.subplot(111)
bb = mtransforms.Bbox([[0.3,1.4],[0.4,1.5]])
bb = mtransforms.Bbox([[np.float(x_high), np.float(box1.low[1])], [np.float(x_low), np.float(box1.high[1])]])
draw_bbox(ax,bb)
ax.autoscale()
In [ ]: