013:获取K线图,增加周期可选

改进《001:如何获取A股个股的前复权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()


klt_options = {'101': '日K', '102': '周K', '1': '1分钟K','5': '5分钟K'}
def get_kline_data():
    code = stock_code_entry.get()
    start_date = start_date_entry.get()
    end_date = end_date_entry.get()
    selected_klt = {k: v for k, v in klt_options.items() if klt_var[k].get()}

    if not selected_klt:
        messagebox.showerror('错误', '请选择至少一种K线周期')
        return

    try:
        for klt, name in selected_klt.items():
            df = get_k_history(code, start_date, end_date, int(klt))
            df.to_csv(f'{code}_{name}.csv', encoding='utf-8-sig', index=None)
        messagebox.showinfo(
            '提示', f'股票代码:{code} 的 K线数据已分别保存到代码目录下的 {", ".join(selected_klt.values())} CSV 文件中')

    except Exception as e:
        messagebox.showerror('错误', '获取K线数据失败:{}'.format(e))


if __name__ == '__main__':

    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()

    # 新增部分:创建并布局复选框

    klt_var = {}
    klt_frame = tk.Frame(root)
    klt_frame.pack(anchor=tk.W)
    for klt_value, klt_name in klt_options.items():
        klt_var[klt_value] = tk.BooleanVar(value=False)
        klt_checkbutton = tk.Checkbutton(
            klt_frame, text=klt_name, variable=klt_var[klt_value])
        klt_checkbutton.pack(side=tk.LEFT, anchor=tk.W, padx=5)
    get_data_button = tk.Button(root, text='获取K线数据', command=get_kline_data)
    get_data_button.pack()

    root.mainloop()

源码:获取K线+周期.py · 中锋/quantization - Gitee.com

效果:

013:获取K线图,增加周期可选_第1张图片生成的文件会增加周期的后缀。

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