法
Posted on 2021-11-21 19:38 心默默言 閱讀(112) 評論(0) 編輯 收藏 舉報(bào)
https://blog.csdn.net/Wilburzzz/article/details/109667926
復(fù)制代碼
# -*- coding: utf-8 -*-
# @Time : 2021/11/7 20:40
# @Author : zhaozhuang
# 導(dǎo)入 efinance 如果沒有安裝則需要通過執(zhí)行命令: pip install efinance 來安裝
import efinance as ef
import mplfinance as mpf
import pandas as pd
# import matplotlib.pyplot as plt
pd.set_option('display.max_rows', 50000)
pd.set_option('display.max_columns', 50000)
pd.set_option('display.width', 2000)
# plt.rcParams['font.sans-serif'] = ['SimHei']
# plt.rcParams['axes.unicode_minus'] = False
# 解決mplfinance繪制輸出中文亂碼
# s = mpf.make_mpf_style(rc={'font.family': 'SimHei'})
def get_kchar(df: pd.DataFrame, code: str, name: str):
date_index = df['日期']
date_index = pd.to_datetime(date_index)
data = df[['開盤', '最高', '最低', '收盤', '成交量']]
data = data.rename(columns={'開盤': 'Open', '收盤': 'Close', '最高': 'High', '最低': 'Low', '成交量': 'Volume'})
data.index = date_index
my_color = mpf.make_marketcolors(up='red', down='green')
my_style = mpf.make_mpf_style(marketcolors=my_color, rc={'font.family': 'SimHei'})
add_plot = [
mpf.make_addplot(turnover_rate, scatter=True, marker='^', color='red'),
mpf.make_addplot(turnover_rate, color='red'),
mpf.make_addplot(ma_turnover_rate, color='blue'),
]
mpf.plot(data, type='candle', style=my_style, figscale=2, addplot=add_plot, mav=(5, 20, 30), ylabel='price',
volume=True,
title=f'\n\n\n {code} {name} K_line')
# mpf.plot(data, type='candle', style=my_style, figscale=2, addplot=add_plot, mav=(5, 20, 30), volume=True,
# title=f'\n\n\n {code} {name} K_line')
paras = {
'stock_code': '002626',
'begin': '20210130',
'end': '20251105',
'freq': 101
}
stock_code = paras['stock_code']
beg = paras['begin']
end = paras['end']
freq = paras['freq']
# 獲取最新一個(gè)交易日的分鐘級別股票行情數(shù)據(jù)
df = ef.stock.get_quote_history(stock_codes=stock_code, beg=beg, end=end, klt=freq)
# 將數(shù)據(jù)存儲(chǔ)到 csv 文件中
# df.to_excel(f'{stock_code}_{freq}.xlsx', encoding='utf-8-sig', index=None)
print(f'股票: {stock_code} 的行情數(shù)據(jù)已存儲(chǔ)到文件: {stock_code}_{freq}.xlsx 中!')
# 獲取一支股票的量比
name = df['股票名稱'].values[0]
vol_ma = df['成交量'].rolling(window=5).mean().values
vol_raito = df['成交量'].values / df['成交量'].rolling(window=5).mean().values
turnover_rate = df['換手率'].values
ma_turnover_rate = df['換手率'].rolling(window=21).mean().values
# print(vol_ma)
# print(vol_raito)
get_kchar(df, paras['stock_code'], name)
聯(lián)系客服