股票信息获取基础

import pandas as pd
import tushare as ts
import baostock as bs

def data_recon_ts(ts_code, start_date, end_date, token):
    # 获取股票日线行情数据并规整化。接口:Tushare Pro;时间单位:日
    ts_pro = ts.pro_api(token)
    data = ts_pro.daily(ts_code=ts_code, start_date=start_date, end_date=end_date)
    data.trade_date = pd.DatetimeIndex(data.trade_date)
    data.set_index("trade_date", drop=True, inplace=True)
    data.sort_index(inplace=True)
    data.index = data.index.set_names('Date')

    recon_data = {'High': data.high, 'Low': data.low,
                  'Open': data.open, 'Close': data.close,
                  'Volume': data.vol}
    data_recon = pd.DataFrame(recon_data)

    return data_recon


def data_recon_bs(ticker, start_data, end_data, frequency='d', adjustflag='3'):
    # frequency="d"取日k线,adjustflag="3"默认不复权,1:后复权;2:前复权
    lg = bs.login()

    fields= "date,open,high,low,close,volume"
    date = bs.query_history_k_data(ticker, fields, start_date=start_data, end_date=end_data,
                                    frequency=frequency, adjustflag=adjustflag)

    data_list = []
    while (date.error_code == '0') & date.next():
        data_list.append(date.get_row_data())
    result = pd.DataFrame(data_list, columns=date.fields)

    result.close = result.close.astype('float64')
    result.open = result.open.astype('float64')
    result.low = result.low.astype('float64')
    result.high = result.high.astype('float64')
    result.volume = result.volume.astype('float64')
    result.volume = result.volume/100 # 单位转换:股-手
    result.date = pd.DatetimeIndex(result.date)
    result.set_index("date", drop=True, inplace=True)
    result.index = result.index.set_names('Date')

    recon_data = {'High': result.high, 'Low': result.low,
                  'Open': result.open, 'Close': result.close,
                  'Volume': result.volume}
    data_recon = pd.DataFrame(recon_data)

    bs.logout()
    return data_recon

token='*****your own token!*****'
zc_ts=data_recon_ts('002891.sz','20211101','20211103',token)
print(zc_ts)
zc_bs=data_recon_bs('002891.sz','2021-11-01','2021-11-03')
print(zc_bs)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
{ 更新日志 2010.10.27 增加采用注册回调函数方式获取数据,详细函数声明请参考 uFunction.pas 相关数据结构声明请参考 TDXGrobal.pas 以S_打头的均为注册回调函数的函数 procedure S_InitMarketData (TDXManager: longword; CallBack :TOnDecodePacket_INITMARKET); stdcall; procedure S_GetPK (TDXManager: longword; CallBack :TOnDecodePacket_PKDAT); stdcall; procedure S_GetTestRealPK (TDXManager: longword; CallBack :TOnDecodePacket_REALPK); stdcall; procedure S_GetKDays (TDXManager: longword; CallBack :TOnDecodePacket_DAYS); stdcall; procedure S_GetDeals (TDXManager: longword; CallBack :TOnDecodePacket_DEALS); stdcall; procedure S_GetMins (TDXManager: longword; CallBack :TOnDecodePacket_MINS); stdcall; procedure S_Get_QH_KDays (TDXManager: longword; CallBack :TOnDecodePacket_FUTURES_DAYS); stdcall; procedure S_Get_QH_TestRealPK (TDXManager: longword; CallBack :TOnDecodePacket_FUTURES_PKDAT); stdcall; procedure S_Get_QH_Mins (TDXManager: longword; CallBack :TOnDecodePacket_FUTURES_MINS); stdcall; procedure S_Connected (TDXManager: longword; CallBack :TRNotifyEvent); stdcall; procedure S_DisConnected (TDXManager: longword; CallBack :TRNotifyEvent); stdcall; 2010.10.26 //期货,函数用法基本同普通股票函数,在使用 R_Connect 时,请指定期货服务器地址及端口(7721) procedure R_Get_QH_KDays (TDXManager: longword; StkCode :PChar; startcount, count: integer); stdcall; procedure R_Get_QH_TestRealPK (TDXManager: longword; StkCode :PChar); stdcall; } { ****************************************** 以下为Delphi版的数据结构和相关调用函数声明 使用其他开发语言的,只要转换成自己语言的相应格式就可以了 目前在 RSRStock.dll 中,提供了如下几个导出函数: DLLVER, R_Open, R_Close, R_Connect, R_DisConnect, R_InitMarketData, R_GetPK, R_GetTestRealPK, R_GetKDays, R_GetDeals, R_GetMins, R_GetMarket, R_GetMarketByStockCode, R_GetMarketByStockName,
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值