python读取通达信期货本地下载数据
2023-12-13 05:10:16
1.函数:
def kline(cls, file)
用途:自动读取本地文件日线或分钟数据
返回:str,pd.DataFrame??
参数:?
? ? --file : str 类似'\lday\28#APL9.day' 或'\minline\28#AP2401.lc1'
? ?
# -*- coding: utf-8 -*-
"""
@Project:TcyQuant
@File: tdx.py
@Auth: tcy
@Date: 2023/12/12 13:36
@Desc:
@Ver : 0.0.1
@Emial: 3615693665@qq.com
@City: China Shanghai Songjiang Yexie
"""
import re
import os
import struct
import numpy as np
import pandas as pd
from tool.file.file import File
class Tdx(object):
daypath = r'C:\new_tdx\vipdoc\ds\lday' # 28#APL9.day
minpath = r'C:\new_tdx\vipdoc\ds\minline' # 28#AP2401.lc1
dstpath = r'D:\futuresdata_cn\data\day\tdxday'
@classmethod
def _get_codemon_(cls, file):
if file.endswith('L9.day'):
file = file.replace('L9.day', '')
s = re.findall(r'[\d+#]+([\da-z]+)\.?', file, re.I)[0]
if s.isalpha():
s = s + '99'
s = s.upper()
var = re.sub(r'\d+', '', s)
return var, s
@classmethod
def kline(cls, file): # 日线分钟读取
"""
读取通达信日线分钟本地下载数据;返回var,df
df cols= date,codemon,open,high,low,close,position,volume,settle
"""
def _op1(a):
v = str(a[0])
ts = '%s-%s-%s' % (v[:4], v[4:6], v[6:8])
return [ts] + list(a[1:])
def _op2(a):
d = a[0] + 25412
h = a[1] // 60
m = a[1] % 60
delta = pd.Timedelta(days=d, hours=h, minutes=m)
ts = (pd.Timestamp('2023-12-12') + delta).strftime('%Y-%m-%d')
return [ts] + list(a[2:])
fmt = 'lffffllf' if file.endswith('.day') else 'hhffffllf'
op = _op1 if file.endswith('.day') else _op2
with open(file, "rb") as f:
buf = f.read()
row = int(len(buf) / 32)
cols = "date,open,high,low,close,position,volume,settle"
datas = []
for i in range(row):
a = struct.unpack(fmt, buf[i * 32:(i + 1) * 32])
datas.append(op(a))
df = pd.DataFrame(datas, columns=cols.split(','))
var, val = cls._get_codemon_(file)
df.insert(loc=1, column='codemon', value=val)
return var, df
@classmethod
def rwdata(cls):
cols2 = ['varmon', 'date']
okcols = ['date', 'codemon', 'varmon', 'open', 'high', 'low',
'close', 'volume', 'amount', 'position', 'settle',
'前结算', 'main', 'var']
files = File.listdir2(cls.daypath, 'L9.day')
for file in files:
var, df = cls.kline(file)
df['varmon'] = df['codemon']
df['amount'] = np.nan
df['前结算'] = np.nan
df['main'] = 'index'
df['var'] = var
df = df[okcols]
newfile = r'%s\%s.csv' % (cls.dstpath, var)
if File.exist_file(newfile):
dfold = pd.read_csv(newfile)
df = pd.concat([dfold, df])
df = df.drop_duplicates(cols2)
df = df.sort_values(cols2)
df.to_csv(newfile, index=False)
if __name__ == '__main__':
file = r'C:\new_tdx\vipdoc\ds\lday\28#APL9.day'
file1 = r'C:\new_tdx\vipdoc\ds\minline\28#AP2401.lc1'
var, df = Tdx.kline(file)
print('var=',var,'\n',df.tail(2))
var, df = Tdx.kline(file1)
print('var=',var,'\n',df.tail(2))
# Tdx.rwdata()
var= AP
date codemon open high ... close position volume settle
1447 2023-12-11 AP99 8620.0 8620.0 ... 8516.0 107892 64517 0.0
1448 2023-12-12 AP99 8515.0 8575.0 ... 8527.0 105098 36468 0.0
[2 rows x 9 columns]
var= AP
date codemon open ... position volume settle
45673 2023-12-12 AP2401 9097.000977 ... 20493 50 0.0
45674 2023-12-12 AP2401 9102.000977 ... 20470 67 0.0
[2 rows x 9 columns]
文章来源:https://blog.csdn.net/tcy23456/article/details/117334305
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!