rq下载的tick数据转换为dsb
<ol>
<li><a href="https://pan.baidu.com/s/1i7FkLbUJAR3ChKYctOAd0A?pwd=5tzc" title="下载提供的数据">下载提供的数据</a></li>
<li>运行代码如下:</li>
</ol>
<pre><code class="language-python">
# -*- coding: utf-8 -*-
import re
import pandas as pd
import time
from wtpy.wrapper import WtDataHelper
from wtpy.WtCoreDefs import WTSBarStruct, WTSTickStruct
from tqdm import tqdm
f = open('IF9999','r')
file_data = f.readlines() #读取所有行
f.close()
headername = ['price','ask_1','ask_qty_1','bid_1','bid_qty_1',
'volume','total_volume','open_interest','upper_limit','lower_limit',
'open','high','low','pre_close',
'pre_interest','pre_settle','update_millisec','time']
file_data1=[re.split(',', i) for i in file_data]
df=pd.DataFrame(file_data1,columns=headername)
# 过滤掉非交易时间收集的数据
df['high']=df['high'].apply(float)
df=df[df['high']<=10**10]
df=df.reset_index(drop=True)
df['exchg']='CFFEX'
df['code']='IF'
date='20220602'
df['action_date']=date
df['action_time']=df['time'].apply(lambda x:x.replace(':','').replace('\n','')).astype('int64')
df['action_date']=df['action_date'].astype('int')
df['action_time'] = df['action_time']*1000+df['update_millisec'].astype('int')
count = len(df)
BUFFER = WTSTickStruct * count
buffer = BUFFER()
for index, row in tqdm(df.iterrows()):
curTick = buffer[index]
curTick.exchg = bytes(row["exchg"],'utf-8')
curTick.code = bytes(row["code"],'utf-8')
curTick.action_date = int(row["action_date"])
curTick.action_time = int(row["action_time"])
curTick.upper_limit = float(row["upper_limit"])
curTick.lower_limit = float(row["lower_limit"])
curTick.price = float(row["price"])
curTick.open = float(row["open"])
curTick.high = float(row["high"])
curTick.low = float(row["low"])
curTick.total_volume = float(row["total_volume"])
curTick.volume = float(row["volume"])
curTick.open_interest = float(row["open_interest"])
curTick.trading_date = int(row["action_date"])
curTick.pre_close = float(row["pre_close"])
curTick.pre_settle = float(row["pre_settle"])
curTick.pre_interest =float(row["pre_interest"])
curTick.bid_prices[0] = float(row["bid_1" ])
curTick.bid_qty[0] = float(row["bid_qty_1" ])
curTick.ask_prices[0] = float(row["ask_1" ])
curTick.ask_qty[0] = float(row["ask_qty_1" ])
dtHelper = WtDataHelper()
dsbname="IF9999.dsb"
dtHelper.store_ticks(tickFile=dsbname, firstTick=buffer, count=len(buffer))
dsb_data_read=dtHelper.read_dsb_ticks(tickFile=dsbname)# 读取 </code></pre>