4.21K 浏览
0

把历史数据补为一个CSV文件.

quotesT= datetime.datetime.fromtimestamp(ks[s][i][“datetime”]/1e9)
OSError: [Errno 22] Invalid argument

就是上面这个错误.但多运行几次就会对一次.而且如果SYMBOL代码多,这个错误就会多一些.有何方法处理吗

Traceback (most recent call last):
File “C:\Users\CarreyXu\AppData\Local\Programs\Python\Python36-32\Lib\site-packages\tqsdk\demo\DDE era Single backfill.py”, line 71, in <module>
quotesT= datetime.datetime.fromtimestamp(ks[s][i][“datetime”]/1e9)
OSError: [Errno 22] Invalid argument
>>>
RESTART: C:\Users\CarreyXu\AppData\Local\Programs\Python\Python36-32\Lib\site-packages\tqsdk\demo\DDE era Single backfill.py
Traceback (most recent call last):
File “C:\Users\CarreyXu\AppData\Local\Programs\Python\Python36-32\Lib\site-packages\tqsdk\demo\DDE era Single backfill.py”, line 71, in <module>
quotesT= datetime.datetime.fromtimestamp(ks[s][i][“datetime”]/1e9)
OSError: [Errno 22] Invalid argument
>>>
RESTART: C:\Users\CarreyXu\AppData\Local\Programs\Python\Python36-32\Lib\site-packages\tqsdk\demo\DDE era Single backfill.py
Traceback (most recent call last):
File “C:\Users\CarreyXu\AppData\Local\Programs\Python\Python36-32\Lib\site-packages\tqsdk\demo\DDE era Single backfill.py”, line 71, in <module>
quotesT= datetime.datetime.fromtimestamp(ks[s][i][“datetime”]/1e9)
OSError: [Errno 22] Invalid argument

import datetime
import csv
 #!/usr/bin/env python
#  -*- coding: utf-8 -*-
from tqsdk import TqApi, TqSim
import datetime
api = TqApi(TqSim())
SYMS = {
      'R1': 'KQ.m@SHFE.ag',
     'R5': 'KQ.m@SHFE.ni',
  'R6': 'KQ.m@DCE.j',
   'R7': 'KQ.m@DCE.pp',
  'R8': 'CZCE.AP001',
   'R9': 'CZCE.TA001',
  'R11': 'KQ.m@SHFE.ru',    
 'R12': 'KQ.m@DCE.eg',
  'R13': 'CZCE.MA001',
   'R14': 'KQ.m@CZCE.CF',
    'R15': 'KQ.m@CZCE.CJ',
   'R17': 'DCE.i2001',
  'R18': 'DCE.y2001',
    'R19': 'DCE.p2001',
     'R21': 'KQ.m@DCE.jd',
   'R23': 'KQ.m@CZCE.SR',
   'R27': 'KQ.m@CZCE.OI',
  'R28': 'DCE.jm2001',
     }
def get_key (dict, value):
    return [k for k, v in dict.items() if v == value]
 i=-8000
ks = {}
 first_time = True
 for s in SYMS:
    ks[s] = api.get_kline_serial( SYMS[s],60,8199)
 while not all([ks[s].is_ready() for s in ks]):
            api.wait_update()
    if api.is_changing(ks[s][i], 'close'):
                             while i<=-1:
                    for s in SYMS:
                        quotesC=ks[s][i]["close"]
                        quotesO=ks[s][i]["open"]
                        quotesH=ks[s][i]["high"]
                        quotesL=ks[s][i]["low"]
                        quotesV=ks[s][i]["volume"]
                        quotesT= datetime.datetime.fromtimestamp(ks[s][i]["datetime"]/1e9)
                         with open("C:\\Docu Dontdel\\ChinaFut.csv","a",newline='') as csvfile: 
                                    writer = csv.writer(csvfile, dialect='excel')
                                    writer.writerow([str(get_key (SYMS, SYMS[s])).replace(' ', '')[2:-2].upper(),str(quotesT).replace(' ', '')[0:-8],str(quotesT).replace(' ', '')[10:],quotesO,quotesH,quotesL,quotesC,quotesV])
                                    csvfile.close()

jacket chueng 已回答的问题 2020年4月17日
0

不知道你解决问题没有,我刚开始使用tqsdk,也遇到了这个问题,这里很明显是数据格式不对的问题,tq返回的是pandas的float64,而datetime对象不能直接使用这个,你需要先手动转换成python对象,再除以1000000000变成python中的标准tiemstamp

jacket chueng 已回答的问题 2020年4月17日
您正在查看2个答案中的1个,单击此处查看所有答案。