005:根据股票代码和起始日期获取K线数据

我们改进《001》中的部分,因为他他没法在可视化界面输入信息,这样太麻烦。我们设法在可视化界面输入股票代码和起始日期,这样可以灵活得多。这部分,我们仍旧只获取日K线的数据。

import tkinter as tk
from tkinter import messagebox
from tkcalendar import Calendar
import pandas as pd
import requests
from urllib.parse import urlencode

 
def gen_secid(rawcode: str) -> str:
    '''
    生成东方财富专用的secid
    Parameters
    ----------
    rawcode : 6 位股票代码
    Return
    ------
    str: 指定格式的字符串
    '''
    # 沪市指数
    if rawcode[:3] == '000':
        return f'1.{rawcode}'
    # 深证指数
    if rawcode[:3] == '399':
        return f'0.{rawcode}'
    # 沪市股票
    if rawcode[0] != '6':
        return f'0.{rawcode}'
    # 深市股票
    return f'1.{rawcode}'
 
 
def get_k_history(code: str, beg: str, end: str, klt: int = 101, fqt: int = 1) -> pd.DataFrame:
    '''
    功能获取k线数据
    -
    参数
        code : 6 位股票代码
        beg: 开始日期 例如 20200101
        end: 结束日期 例如 20200201
        klt: k线间距 默认为 101 即日k
            klt:1 1 分钟
            klt:5 5 分钟
            klt:101 日
            klt:102 周
        fqt: 复权方式
            不复权 : 0
            前复权 : 1
            后复权 : 2 
    '''
    EastmoneyKlines = {
        'f51': '日期',
        'f52': '开盘',
        'f53': '收盘',
        'f54': '最高',
        'f55': '最低',
        'f56': '成交量',
        'f57': '成交额',
        'f58': '振幅',
        'f59': '涨跌幅',
        'f60': '涨跌额',
        'f61': '换手率',
 
 
    }
    EastmoneyHeaders = {
  
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; Touch; rv:11.0) like Gecko',
        'Accept': '*/*',
        'Accept-Language': 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
        'Referer': 'http://quote.eastmoney.com/center/gridlist.html',
    }
    fields = list(EastmoneyKlines.keys())
    columns = list(EastmoneyKlines.values())
    fields2 = ",".join(fields)
    secid = gen_secid(code)
    params = (
        ('fields1', 'f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13'),
        ('fields2', fields2),
        ('beg', beg),
        ('end', end),
        ('rtntype', '6'),
        ('secid', secid),
        ('klt', f'{klt}'),
        ('fqt', f'{fqt}'),
    )
    params = dict(params)
    base_url = 'https://push2his.eastmoney.com/api/qt/stock/kline/get'
    url = base_url+'?'+urlencode(params)
    json_response: dict = requests.get(
        url, headers=EastmoneyHeaders).json()
 
    data = json_response.get('data')
    if data is None:
        if secid[0] == '0':
            secid = f'1.{code}'
        else:
            secid = f'0.{code}'
        params['secid'] = secid
        url = base_url+'?'+urlencode(params)
        json_response: dict = requests.get(
            url, headers=EastmoneyHeaders).json()
        data = json_response.get('data')
    if data is None:
        print('股票代码:', code, '可能有误')
        return pd.DataFrame(columns=columns)
 
    klines = data['klines']
 
    rows = []
    for _kline in klines:
 
        kline = _kline.split(',')
        rows.append(kline)
 
    df = pd.DataFrame(rows, columns=columns)
 
    return df

def select_start_date():
    def on_date_selected():
        selected_date = cal.selection_get()
        start_date_entry.delete(0, tk.END)
        start_date_entry.insert(0, selected_date.strftime('%Y%m%d'))
        top.destroy()

    top = tk.Toplevel(root)
    cal = Calendar(top, selectmode='day')
    cal.pack()
    confirm_button = tk.Button(top, text='确认', command=on_date_selected)
    confirm_button.pack()

def select_end_date():
    def on_date_selected():
        selected_date = cal.selection_get()
        end_date_entry.delete(0, tk.END)
        end_date_entry.insert(0, selected_date.strftime('%Y%m%d'))
        top.destroy()

    top = tk.Toplevel(root)
    cal = Calendar(top, selectmode='day')
    cal.pack()
    confirm_button = tk.Button(top, text='确认', command=on_date_selected)
    confirm_button.pack()

def get_kline_data():
    code = stock_code_entry.get()
    start_date = start_date_entry.get()
    end_date = end_date_entry.get()

    try:
        df = get_k_history(code, start_date, end_date)
        df.to_csv(f'{code}.csv', encoding='utf-8-sig', index=None)
        messagebox.showinfo('提示', f'股票代码:{code} 的 k线数据已保存到代码目录下的 {code}.csv 文件中')
    except:
        messagebox.showerror('错误', '获取K线数据失败')

root = tk.Tk()
root.title('股票数据获取')

stock_code_label = tk.Label(root, text='股票代码')
stock_code_label.pack()
stock_code_entry = tk.Entry(root)
stock_code_entry.pack()

start_date_label = tk.Label(root, text='起始日期')
start_date_label.pack()
start_date_entry = tk.Entry(root)
start_date_entry.pack()

select_start_date_button = tk.Button(root, text='选择日期', command=select_start_date)
select_start_date_button.pack()

end_date_label = tk.Label(root, text='结束日期')
end_date_label.pack()
end_date_entry = tk.Entry(root)
end_date_entry.pack()

select_end_date_button = tk.Button(root, text='选择日期', command=select_end_date)
select_end_date_button.pack()

get_data_button = tk.Button(root, text='获取K线数据', command=get_kline_data)
get_data_button.pack()

root.mainloop()

中间有一些库可能要安装,看着办。我们来看下效果:

005:根据股票代码和起始日期获取K线数据_第1张图片005:根据股票代码和起始日期获取K线数据_第2张图片

起始日期可以弹出日历窗口部件选择,比较方便。继续:

005:根据股票代码和起始日期获取K线数据_第3张图片

完成

你可能感兴趣的:(量化交易,IT量化,量化交易)