Skip to content

Commit 2126fe6

Browse files
Add files via upload
1 parent 78a9669 commit 2126fe6

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

Python_8_Plot_Bokeh_Candlestick.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Created on Fri Nov 27 08:09:11 2020
4+
5+
@author: Tin
6+
"""
7+
# Plot Candlestick
8+
import pandas as pd # Dataframe Library
9+
from math import pi
10+
from bokeh.plotting import figure, show, output_file
11+
12+
pd.set_option('max_columns', None) # To show all columns
13+
14+
import yfinance as yf
15+
yf.pdr_override()
16+
17+
18+
# input
19+
symbol = 'AAPL'
20+
start = '2019-12-01'
21+
end = '2020-01-01'
22+
23+
24+
# dataframe
25+
df = yf.download(symbol,start,end)
26+
27+
df["Date"] = pd.to_datetime(df.index)
28+
29+
mids = (df['Open'] + df['Adj Close'])/2
30+
spans = abs(df['Adj Close']-df['Open'])
31+
32+
inc = df['Adj Close'] > df['Open']
33+
dec = df['Open'] > df['Adj Close']
34+
w = 12*60*60*1000 # half day in ms
35+
36+
TOOLS = "pan,wheel_zoom,box_zoom,reset,save"
37+
38+
p = figure(x_axis_type="datetime", tools=TOOLS, plot_width=1000, title = symbol + " Candlestick")
39+
p.xaxis.major_label_orientation = pi/4
40+
p.grid.grid_line_alpha=0.3
41+
42+
p.segment(df.Date, df.High, df.Date, df.Low, color="black")
43+
p.vbar(df.Date[inc], w, df.Open[inc], df['Adj Close'][inc], fill_color="#D5E1DD", line_color="black")
44+
p.vbar(df.Date[dec], w, df.Open[dec], df['Adj Close'][dec], fill_color="#F2583E", line_color="black")
45+
46+
output_file("candlestick.html", title= symbol + " candlestick")
47+
48+
show(p) # open a browser

0 commit comments

Comments
 (0)