Repository navigation
Expand file tree
/
Copy pathplotly_ohlc_plotter.py
More file actions
95 lines (82 loc) · 3.01 KB
/
Copy pathplotly_ohlc_plotter.py
File metadata and controls
95 lines (82 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import csv,datetime,requests,time
import numpy as np
import plotly.plotly as py
import plotly.graph_objs as go
def numpy_stock(exchange,ticker,interval,days):
url = 'https://finance.google.com/finance/getprices?q=%s&i=%d&p=%dd&x=%s&f=d,o,h,l,c,v' %\
(ticker,interval,days,exchange)
with requests.Session() as ses:
file = ses.get(url)
csv_reader = csv.reader(file.text.splitlines(),delimiter=',')
header_pass = 0
for row in csv_reader:
if row[0].split('=')[0]=='COLUMNS':
column_names = [row[0].split('=')[1]]+row[1:]
data = [[] for ii in range(0,len(column_names))]
if row[0].split('=')[0]=='TIMEZONE_OFFSET':
t_offset = float(row[0].split('=')[1])
header_pass = 1
continue
if header_pass==1:
if row[0][0]=='a':
new_day = datetime.datetime.fromtimestamp(int(row[0][1:]))
curr_datetime = new_day
else:
curr_datetime = new_day+datetime.timedelta(seconds=period*int(row[0]))
for ii in range(0,len(column_names)):
if ii==0:
data[ii].extend([curr_datetime])
else:
data[ii].extend([np.double(row[ii])])
return url,data,column_names
def plotter(ticker,names,data):
for ii,jj in zip(range(0,len(names)),names):
if jj=='DATE':
datevec = data[ii]
elif jj=='OPEN':
data_open = data[ii]
elif jj=='HIGH':
data_high = data[ii]
elif jj=='LOW':
data_low = data[ii]
elif jj=='CLOSE':
data_close = data[ii]
elif jj=='VOLUME':
data_volume = data[ii]
hovertext=[]
for kk in range(len(data[0])):
hovertext.append(ticker+\
'<br>'+str(data[0][kk])+\
'<br>----------------------------'+\
'<br>Open: '+str(data_open[kk])+\
'<br>Close: '+str(data_close[kk])+\
'<br>High: '+str(data_high[kk])+\
'<br>Low: '+str(data_low[kk])+\
'<br>Volume: '+str(data_volume[kk]))
trace = go.Ohlc(x=datevec,
open=data_open,
high=data_high,
low=data_low,
close=data_close,
text = hovertext,
hoverinfo='text')
layout = go.Layout(
title='Open-High-Low-Close Plot for '+ticker,
xaxis = dict(
rangeslider = dict(
visible = False
)
),
yaxis=dict(
title='U.S. Dollar'
)
)
data = [trace]
fig = go.Figure(data=data, layout=layout)
py.plot(fig, filename='OHLC_'+ticker)
ticker = 'NFLX'
period = 60
days = 5
exchange = 'NASD'
url,data,names = numpy_stock(exchange,ticker,period,days)
plotter(ticker,names,data)