Skip to content

Commit 57e127f

Browse files
authored
stock_analysis.py
Using pandas_datareader for graphing stock data
1 parent 85f47c8 commit 57e127f

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

Diff for: stock_analysis.py

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
from pandas_datareader import data
2+
import datetime
3+
# you can use Shift-J/K to select multiple cells and Shift-M will merge all the selected cells.
4+
from bokeh.plotting import figure, show, output_file
5+
6+
7+
start=datetime.datetime(2015,11,1)
8+
end=datetime.datetime(2016,3,10)
9+
# name parameter is for the company's ticker... like AAPL for Apple Inc...
10+
# data used to build candlestick chart: High, Low, Open Close Date...
11+
df=data.DataReader(name="GOOG",data_source="yahoo",start=start,end=end)
12+
df
13+
14+
date_increase=df.index[df.Close > df.Open]
15+
date_decrease=df.index[df.Close < df.Open]
16+
17+
df.Open
18+
19+
# function definition...the return values of this function are used to set the coordinates for the rectangle glyphs...
20+
def inc_dec(c, o):
21+
if c > o:
22+
value="Increase"
23+
elif c < o:
24+
value="Decrease"
25+
else:
26+
value="Equal"
27+
return value
28+
29+
# creates new status column with list of data returned by function inc_dec(c,o)...
30+
df["Status"]=[inc_dec(c,o) for c, o in zip(df.Close,df.Open)]
31+
32+
df["Middle"]=(df.Open+df.Close)/2 # y coordinate of rectangle...center point...
33+
df["Height"]=abs(df.Close-df.Open) # bottom and top of rectangle...
34+
35+
36+
p=figure(x_axis_type='datetime', width=1000, height=300, sizing_mode="scale_width")
37+
p.title.text="Candlestick Chart"
38+
#-------------------------------------------------------------------------------------------------------------------
39+
# // NEW CODE//
40+
# Candlestick segments
41+
# Section 20 Lecture 216 TIME: 1:36 / 5:02
42+
# alpha factor for plot/figure object (p=figure)...how transparent you want the grid lines to appear...
43+
# a value of zero gives you 100% transparency on the grid... (the range is between 0-1...floating point values)
44+
#-------------------------------------------------------------------------------------------------------------------
45+
p.grid.grid_line_alpha=0.3
46+
#p.grid.grid_line_alpha=1
47+
p.grid.grid_line_alpha=0
48+
#p.grid.grid_line_alpha=1
49+
50+
hours_12=12*60*60*1000 # width of rectangle glyphs expressed in milliseconds along the x axis...
51+
52+
#-------------------------------------------------------------------------------------------------------------------
53+
# rect() takes 4 mandatory parameters...
54+
# x coordinate, y coordinate, 12 hour width, height
55+
# Displaying the data using the Rectangle glyphs...
56+
#-------------------------------------------------------------------------------------------------------------------
57+
58+
# segment() takes 4 mandatory parameters...
59+
p.segment(df.index, df.High, df.index, df.Low, color="Black") # (df.index, df.High) x, y coords...
60+
# df.index is the date column...
61+
p.rect(df.index[df.Status=="Increase"],df.Middle[df.Status=="Increase"], hours_12,
62+
df.Height[df.Status=="Increase"], fill_color="#CCFFFF", line_color="black")
63+
64+
p.rect(df.index[df.Status=="Decrease"],df.Middle[df.Status=="Decrease"], hours_12,
65+
df.Height[df.Status=="Decrease"], fill_color="#FF3333", line_color="black")
66+
67+
output_file("cs.html")
68+
show(p)

0 commit comments

Comments
 (0)