量化交易之回测篇 - 记载csv的tick数据并开生成TickData(处理字符串毫秒数据为datetime类型)

import datetime
import re

from tqsdk import TqApi, TqAuth
from tqsdk.tools import DataDownloader
from contextlib import closing

from public_module.tqz_extern.tools.pandas_operator.pandas_operator import pandas  # noqa
from public_module.tqz_extern.tools.file_path_operator.file_path_operator import TQZFilePathOperator

from server_api.tqz_object import TickData, BarData, Exchange
import csv

TIME_GAP = 8 * 60 * 60 * 1000000000

class TQZTianQinClient:
    
    """
    略...
    """    

    @staticmethod
    def query_history_ticks_from_csv(tq_symbol: str, csv_path: str) -> list:
        csv_read = csv.reader(open(csv_path))

        tq_sym = tq_symbol.split('@')[1]

        history_ticks = []
        index = 0
        for line in csv_read:
            index += 1
            if line[0] == 'datetime':
                continue
            if index == 20:  # for test
                break

            std_datetime_string = re.match('(\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+\.[\d]{4})', line[0]).group()
            history_ticks.append(
                TickData(
                    symbol=f'{tq_sym.split(".")[1]}888',
                    exchange=Exchange(tq_sym.split('.')[0]),
                    name=tq_symbol,
                    datetime=datetime.datetime.strptime(std_datetime_string, "%Y-%m-%d %H:%M:%S.%f"),
                    last_price=float(line[2]),
                    high_price=float(line[3]),
                    low_price=float(line[4]),
                    volume=float(line[5]),
                    open_interest=float(line[7]),
                    bid_price_1=float(line[8]),
                    bid_volume_1=float(line[9]),
                    ask_price_1=float(line[10]),
                    ask_volume_1=float(line[11]),
                    gateway_name="csv_ticks"
                )  # noqa
            )

        for tick in history_ticks[:20]:
            print(tick)

        return history_ticks
class TQZTianQinDataManager:
    """
    小时线: 一天6根
    1493个交易日

    30分钟线: 一天12根
    746个交易日

    10分钟线: 一天36根
    248个交易日
    """
    @classmethod
    def load_main_history_ticks_from_csv(cls, tq_m_symbol: str, csv_path: str):
        return TQZTianQinClient.query_history_ticks_from_csv(tq_symbol=tq_m_symbol, csv_path=csv_path)
if __name__ == '__main__':
    cu_file = TQZFilePathOperator.grandfather_path(source_path=__file__) + f'/history_ticks_csv/KQ_m@SHFE_cu.csv'
    
    ticks_content = TQZTianQinDataManager.load_main_history_ticks_from_csv(tq_m_symbol="KQ.m@SHFE.cu", csv_path=cu_file)

    print("here...")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值