-
Notifications
You must be signed in to change notification settings - Fork 5
/
drawing_chart_with_annotation
49 lines (40 loc) · 1.46 KB
/
drawing_chart_with_annotation
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
#!/d/sysutils/conda/python
# 2020/07/22
#
# pip install --upgrade mpl_finance
# new version
# pip install --upgrade mplfinance
#
# download data.csv as well
import matplotlib.pyplot as plt
from mpl_finance import candlestick_ohlc
import pandas as pd
import matplotlib.dates as mpl_dates
plt.style.use('ggplot')
# Extracting Data for plotting
data = pd.read_csv('data.csv')
ohlc = data.loc[:, ['Date', 'Open', 'High', 'Low', 'Close']]
ohlc['Date'] = pd.to_datetime(ohlc['Date'])
ohlc['Date'] = ohlc['Date'].apply(mpl_dates.date2num)
ohlc = ohlc.astype(float)
print(ohlc)
# Creating Subplots
fig, ax = plt.subplots()
candlestick_ohlc(ax, ohlc.values, width=0.6, colorup='green', colordown='red', alpha=0.8)
# Setting labels & titles
ax.set_xlabel('Date')
ax.set_ylabel('Price')
fig.suptitle('Daily Candlestick Chart of NIFTY50')
ax.annotate('Enter',xy=(ohlc.values[3][0],ohlc.values[3][3]),
xytext=(ohlc.values[3][0]-0.5,ohlc.values[3][3]-30))
ax.annotate('Exit',xy=(ohlc.values[8][0],ohlc.values[8][3]),
xytext=(ohlc.values[8][0]-0.4,ohlc.values[8][2]+20))
ax.annotate('Enter',xy=(ohlc.values[1][0],ohlc.values[1][3]-10),
xytext=(ohlc.values[1][0]-0.5,ohlc.values[1][3]-100),
arrowprops=dict(facecolor='red', shrink=0.02))
# Formatting Date
date_format = mpl_dates.DateFormatter('%d-%m-%Y')
ax.xaxis.set_major_formatter(date_format)
fig.autofmt_xdate()
fig.tight_layout()
plt.show()# -*- coding: utf-8 -*-