654 浏览
0

代码是引用“例子”代码
希望1、解决显示异常问题
希望2,原来显示的很长,我只需要显示到秒,怎么办?
 from tqsdk import TqApi, TqAuth,  TqKq
import datetime, csv, os
order_cols = ["order_id", "exchange_order_id", "exchange_id", "instrument_id", "direction", "offset", "status", "volume_orign", "volume_left", "limit_price", "price_type", "volume_condition", "time_condition", "insert_date_time", "last_msg"]
trade_cols = [str("trade_date_time"), "exchange_id", "instrument_id", "direction", "offset", "price", "volume"]
 def write_csv(file_name, cols, datas):
    file_exists = os.path.exists(file_name) and os.path.getsize(file_name) > 0
    with open(file_name, 'a', newline='', encoding='utf-8-sig') as csvfile:
        csv_writer = csv.writer(csvfile, dialect='excel')
        if not file_exists:
            csv_writer.writerow(['datetime'] + cols)
        for item in datas.values():
            if 'insert_date_time' in cols:
                dt = datetime.datetime.now()
            elif 'trade_date_time' in cols:
                dt = datetime.datetime.now()
            else:
                dt = None
            row = [dt] + [item[k] for k in cols]
            csv_writer.writerow(row)
with TqApi(TqKq(), auth=TqAuth("19965208464", "mima")) as api:
    # 将当前账户下全部委托单、成交信息写入 csv 文件中
    write_csv("orders.csv", order_cols, api.get_order())
    write_csv("trades.csv", trade_cols, api.get_trade())

liudaliuda668 发表新评论 2022年8月21日

自己修改部分代码有错误,现在已经找到问题,问题已经解决了。