-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathFinancial_Analysis.py
68 lines (43 loc) · 1.77 KB
/
Financial_Analysis.py
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
# coding: utf-8
# In[71]:
from pandas_datareader import data
import datetime
from bokeh.plotting import figure,show,output_file
from bokeh.embed import components
from bokeh.resources import CDN
#the start and end parameter should be in "datetime object" format
s = datetime.datetime(2017,7,1)
e = datetime.datetime(2018,1,10)
df = data.DataReader(name="GOOG", data_source="yahoo", start= s, end= e)
p = figure(x_axis_type="datetime", width=1000, height=300, responsive = True)
p.title.text = "Candlestick Chart(Google Stock Prices)"
#a function that determines whether the close price is higher than the open price
def inc_dec(c, o):
if c > o:
value = "Rise"
elif c < o:
value = "Drop"
else:
value = "Tie"
return value
df["Status"] = [inc_dec(c,o) for c,o in zip(df.Close, df.Open)] #list comprehension
df["Mid"] = (df.Open + df.Close)/2
df["Height"] = abs(df.Open - df.Close)
#we build two types of rectangles-- one where the close price is higher, another one where the close price is lower
hours_12 = 12*60*60*1000 #convert the width, which is 12 hours, to miliseconds
p.segment(df.index, df.High, df.index, df.Low, color="black")
p.rect(df.index[df.Status == "Rise"], df.Mid[df.Status == "Rise"],hours_12, df.Height[df.Status == "Rise"],
fill_color = "red", line_color = "black" )
p.rect(df.index[df.Status == "Drop"], df.Mid[df.Status == "Drop"],hours_12, df.Height[df.Status == "Drop"],
fill_color = "#228B22", line_color = "black" )
#load the html elements and JS script that containing all data with "embed" library
script1, div1, = components(p)
#load the bokeh JS and CSS files with "resources" library
cdn_js = CDN.js_files
cdn_css = CDN.css_files
# output_file("Graph.html")
# show(p)
# In[72]:
cdn_js
# In[73]:
cdn_css