-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdraw_candlestick_ohlc.py
65 lines (59 loc) · 2.42 KB
/
draw_candlestick_ohlc.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
"""
从excel中读取数据绘制蜡烛图
"""
from pandas import DataFrame, Series
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import dates as mdates
from matplotlib import ticker as mticker
from matplotlib.finance import candlestick_ohlc
from matplotlib.dates import DateFormatter, WeekdayLocator, DayLocator, MONDAY, YEARLY
from matplotlib.dates import MonthLocator, MONTHLY
import datetime
import pylab
import time
import os
def draw_candlestick_ohlc(file_path):
MA1 = 10 #10日均线
MA2 = 50 #50日均线
# 数据预处理
data = pd.DataFrame(pd.read_excel(file_path,sheet_name=1,index_col='日期'))
data = data.sort_index()
stdata = pd.DataFrame({'DateTime':data.index,'Open':data.开盘价,'High':data.最高价,
'Close':data.收盘价,'Low':data.最低价})
stdata['DateTime'] = mdates.date2num(stdata['DateTime'].astype(datetime.date)) #日期转化为天数
daysreshape = stdata.reset_index()
daysreshape = daysreshape.reindex(columns=['DateTime','Open','High','Low','Close'])
print(daysreshape)
Av1 = pd.rolling_mean(daysreshape.Close.values,MA1)
Av2 = pd.rolling_mean(daysreshape.Close.values,MA2)
SP = len(daysreshape.DateTime.values[MA2-1:])
fig = plt.figure(facecolor='#07000d',figsize=(15,10))
ax1 = plt.subplot2grid((6,4),(1,0),rowspan=4,colspan=4,axisbg='#07000d')
candlestick_ohlc(ax1,daysreshape.values[-SP:],width=.6,colorup="#ff1717",colordown='#53c156')
# 画出均线
label1 = str(MA1)+' SMA'
label2 = str(MA2)+' SMA'
ax1.plot(daysreshape.DateTime.values[-SP:],Av1[-SP:],'#e1edf9',label=label1,linewidth=1.5)
ax1.plot(daysreshape.DateTime.values[-SP:],Av2[-SP:],'#4ee6fd',label=label2,linewidth=1.5)
ax1.grid(True,color='w')
ax1.xaxis.set_major_locator(mticker.MaxNLocator(10))
ax1.xaxis.set_major_formatter(mdates.DateFormatter("%Y-%m-%d"))
ax1.yaxis.label.set_color('w')
ax1.spines['bottom'].set_color('#5998ff')
ax1.spines['top'].set_color('#5998ff')
ax1.spines['left'].set_color('#5998ff')
ax1.spines['right'].set_color('#5998ff')
ax1.tick_params(axis='y',colors='w')
plt.gca().yaxis.set_major_locator(mticker.MaxNLocator(prune='upper'))
ax1.tick_params(axis='x',colors='w')
plt.ylabel('Stock price and Volume')
plt.show()
def main():
file_folder = r''
file = r'000001_2019.xlsx'
file_path = os.path.join(file_folder,file)
draw_candlestick_ohlc(file_path)
if __name__ == '__main__':
main()