In [1]:
%matplotlib inline
import math
import matplotlib.pyplot as plt
import requests
import datetime
import urllib
from pandas import DataFrame,Series
# Define function to fetch remote data # demonstartion only #
def TQDB(symbol='DEMO1',startDate='2014-6-30',endDate='2035-7-01',type='Series',server='127.0.0.1'):
querystr={'symbol':symbol, 'BEG':startDate, 'END': endDate}
url = "http://"+server+"/cgi-bin/q1min.py?"+urllib.urlencode(querystr)
r = requests.get(url)
lines = r.content.split('\n')
x = []
H=[]
L=[]
C=[]
O=[]
Vol=[]
i=0
for line in lines:
i=i+1
items=line.split(',')
if len(items) < 5:
continue
dt=datetime.datetime.strptime(items[0]+items[1], '%Y%m%d%H%M%S')
x.append(dt)
C.append(float(items[5]))
L.append(float(items[4]))
H.append(float(items[3]))
O.append(float(items[2]))
Vol.append(float(items[6]))
d = {'O' :O,'H':H,'L':L,'C':C,'Vol':Vol}
if len(O)==0:
print "no data available. Please select different date"
return
if type=="DataFrame":
return DataFrame(d,index=x, columns=['O','H','L','C','Vol'])
elif type=="Series":
return Series(C,index=x)
else:
print 'type is not defined'
In [2]:
s='2015-07-1'
e='2035-12-1'
a=TQDB("DEMO1",type='Series',startDate=s,endDate=e)
b=TQDB("DEMO2",type='Series',startDate=s,endDate=e)
c=TQDB("DEMO3",type='Series',startDate=s,endDate=e)
In [3]:
# (a)['C'].plot()#blue
ee="2015-10-23"
a[ee].plot()
b[ee].plot()
c[ee].plot()
#a.to_csv('SPY.csv')
# b.plot()#Green
# c.plot()#red
In [4]:
(a-b).plot()#blue
#b.plot()#Green
Out[4]:
In [5]:
s='2014-5-2'
e='2035-7-3'
stw=TQDB("DEMO1",type='Series',startDate=s,endDate=e)
tx=TQDB("DEMO2",type='Series',startDate=s,endDate=e)
In [6]:
p=1.5*30*stw-tx*2
In [7]:
p
Out[7]:
In [8]:
p.plot()
Out[8]:
In [ ]: