不知道图能否看清
就是我数据库里没有IH99 9:25的数据,但是通过datamanager方法读取来的数据时间戳有9:25,应该是9:31才对
不知道图能否看清
就是我数据库里没有IH99 9:25的数据,但是通过datamanager方法读取来的数据时间戳有9:25,应该是9:31才对
gateway_name symbol exchange datetime interval volume open_interest open high low close
0 DB IH99 Exchange.CFFEX 2019-05-31 09:25:00+08:00 Interval.MINUTE 767.0 54422.0 2713.5703 2716.4436 2709.8341 2712.8189
1 DB IH99 Exchange.CFFEX 2019-05-31 09:26:00+08:00 Interval.MINUTE 359.0 54205.0 2712.7911 2716.2056 2712.6688 2713.1206
2 DB IH99 Exchange.CFFEX 2019-05-31 09:27:00+08:00 Interval.MINUTE 323.0 53981.0 2713.1068 2713.5549 2709.9648 2709.9710
3 DB IH99 Exchange.CFFEX 2019-05-31 09:28:00+08:00 Interval.MINUTE 422.0 53717.0 2710.1046 2712.3135 2708.4665 2710.7099
4 DB IH99 Exchange.CFFEX 2019-05-31 09:29:00+08:00 Interval.MINUTE 291.0 53536.0 2711.1412 2714.8780 2710.6063 2713.2508
5 DB IH99 Exchange.CFFEX 2019-05-31 09:30:00+08:00 Interval.MINUTE 626.0 53167.0 2713.3754 2714.0023 2709.8716 2711.3979
6 DB IH99 Exchange.CFFEX 2019-05-31 09:31:00+08:00 Interval.MINUTE 354.0 52976.0 2711.2408 2713.0316 2709.2843 2712.6573
7 DB IH99 Exchange.CFFEX 2019-05-31 09:32:00+08:00 Interval.MINUTE 416.0 52736.0 2712.7938 2717.9527 2711.2315 2717.3653
8 DB IH99 Exchange.CFFEX 2019-05-31 09:33:00+08:00 Interval.MINUTE 506.0 52480.0 2717.7916 2721.5836 2715.4050 2720.7167
9 DB IH99 Exchange.CFFEX 2019-05-31 09:34:00+08:00 Interval.MINUTE 519.0 52240.0 2720.8500 2725.9267 2719.0185 2724.5785
相当于整体往前移动了6分钟,很费解,读取出来的每天都是往前移6分钟
发现应该是时区问题 但不知道如何解决
首先在CSV导入数据库的时候我对时间戳做了如下设置
中国时区是+8,对应参数hours=8
utc_8 = timezone(timedelta(hours=8))
bar = BarData(
symbol=row.symbol,
exchange=row.exchange,
datetime=row.datetime.replace(tzinfo=utc_8),#20201129发现vnpy需要时区数据
interval=row.interval,
volume=row.volume,
open_price=row.open,
high_price=row.high,
low_price=row.low,
close_price=row.close,
open_interest=row.open_interest,
gateway_name="DB",
)
尽管我做了如上设置,在回测是打印每分钟的时间戳 ,打印信息如下:|
C:\vnstudio\python.exe D:/vnpy-2.1.7/examples/cta_backtesting/BACKTESTING20201129.py
tzkeyname= China
2020-11-30 00:33:26.563880 开始加载历史数据
2020-11-30 00:33:26.950880 历史数据加载完成,数据量:4560
--策略接受到的分钟bar时间戳为= 2019-06-03 09:31:00+08:06
open high ... open_interest volume
datetime ...
2019-05-31 09:25:00+08:00 2713.5703 2716.4436 ... 54422.0 767.0
2019-05-31 14:54:00+08:00 2702.3911 2704.0818 ... 55943.0 432.0
[2 rows x 6 columns]
--策略接受到的分钟bar时间戳为= 2019-06-03 09:32:00+08:06
open high ... open_interest volume
datetime ...
2019-05-31 09:25:00+08:00 2713.5703 2716.4436 ... 54422.0 767.0
2019-05-31 14:54:00+08:00 2702.3911 2704.0818 ... 55943.0 432.0
可以看到“策略接受到的分钟bar时间戳为= 2019-06-03 09:31:00+08:06 ”
+08:06,应该是这个问题导致的上述6分钟,所以我现在该如何解决呢,是那个导入数据时设置的时区的参数不对是么,该怎么改呢,请教各位大佬
中国时区是+8,对应参数hours=8
utc_8 = timezone(timedelta(hours=8)) 是这里要明确分钟也是0么
输出的时候请用replace(tzinfo=None)处理一下时间戳,2.1.3以后vnpy支持了全球时间戳,在数据后加了时区信息,中国地区是+08:06。但是你输出的时候可能被本地转成UTC时间了(+08:00)
不是存入数据的时间设置问题么?
我不会改输出时候的时区设置。。。。
我感觉我的浏览器时区 都是错的 现在是凌晨,却显示上午,看着贼别扭
主要是tclocal这个模块老报错
我就把他强行设置为 China
不知道是不是这个深层原因
def get_localzone_name():
# Windows is special. It has unique time zone names (in several
# meanings of the word) available, but unfortunately, they can be
# translated to the language of the operating system, so we need to
# do a backwards lookup, by going through all time zones and see which
# one matches.
handle = winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE)
TZLOCALKEYNAME = r"SYSTEM\CurrentControlSet\Control\TimeZoneInformation"
localtz = winreg.OpenKey(handle, TZLOCALKEYNAME)
keyvalues = valuestodict(localtz)
localtz.Close()
print ('keyvalues=',keyvalues)
if 'TimeZoneKeyName' in keyvalues:
# Windows 7 (and Vista?)
# For some reason this returns a string with loads of NUL bytes at
# least on some systems. I don't know if this is a bug somewhere, I
# just work around it.
tzkeyname = keyvalues['TimeZoneKeyName'].split('\x00', 1)[0]
else:
# Windows 2000 or XP
# This is the localized name:
tzwin = keyvalues['StandardName']
# Open the list of timezones to look up the real name:
TZKEYNAME = r"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones"
tzkey = winreg.OpenKey(handle, TZKEYNAME)
print ('tzkey=',tzkey)
# Now, match this value to Time Zone information
tzkeyname = None
for i in range(winreg.QueryInfoKey(tzkey)[0]):
subkey = winreg.EnumKey(tzkey, i)
sub = winreg.OpenKey(tzkey, subkey)
data = valuestodict(sub)
sub.Close()
try:
if data['Std'] == tzwin:
tzkeyname = subkey
break
except KeyError:
# This timezone didn't have proper configuration.
# Ignore it.
pass
tzkey.Close()
handle.Close()
if tzkeyname is None:
raise LookupError('Can not find Windows timezone configuration')
# print ('tzkeyname=',tzkeyname)
# print('type(tzkeyname)=', type(tzkeyname))
# tzkeyname= "China"
# print('tzkeyname=', tzkeyname)
# print('type(tzkeyname)=', type(tzkeyname))
print('tzkeyname=', tzkeyname)
timezone = win_tz.get(tzkeyname)
if timezone is None:
# Nope, that didn't work. Try adding "Standard Time",
# it seems to work a lot of times:
timezone = win_tz.get(tzkeyname + " Standard Time")
# Return what we have.
if timezone is None:
raise pytz.UnknownTimeZoneError('Can not find timezone 123456' + tzkeyname)
return timezone
另一个帖子里回答你了,另外这是用的什么云服务器?Windows没有时区配置,可能引发很多奇怪的问题,这种云服务商建议不要用
用Python的交易员 wrote:
另一个帖子里回答你了,另外这是用的什么云服务器?Windows没有时区配置,可能引发很多奇怪的问题,这种云服务商建议不要用
感谢大佬指导!!确实是时区配置没设置,目前没用服务器,是个人电脑没有设置时区。。。
xiaohe wrote:
输出的时候请用replace(tzinfo=None)处理一下时间戳,2.1.3以后vnpy支持了全球时间戳,在数据后加了时区信息,中国地区是+08:06。但是你输出的时候可能被本地转成UTC时间了(+08:00)
+
+
++
不会在输出的时候请用replace(tzinfo=None)处理一下时间戳。。
我把变通方法贴在这个帖子了,时区搞得头痛
https://www.vnpy.com/forum/topic/5171
shunyuzhiqian wrote:
xiaohe wrote:
输出的时候请用replace(tzinfo=None)处理一下时间戳,2.1.3以后vnpy支持了全球时间戳,在数据后加了时区信息,中国地区是+08:06。但是你输出的时候可能被本地转成UTC时间了(+08:00)
+
+
++不会在输出的时候请用replace(tzinfo=None)处理一下时间戳。。
我把变通方法贴在这个帖子了,时区搞得头痛
https://www.vnpy.com/forum/topic/5171
xiaohe wrote:
shunyuzhiqian wrote:
xiaohe wrote:
输出的时候请用replace(tzinfo=None)处理一下时间戳,2.1.3以后vnpy支持了全球时间戳,在数据后加了时区信息,中国地区是+08:06。但是你输出的时候可能被本地转成UTC时间了(+08:00)
+
+
++不会在输出的时候请用replace(tzinfo=None)处理一下时间戳。。
我把变通方法贴在这个帖子了,时区搞得头痛
https://www.vnpy.com/forum/topic/5171
+
+
+
+
+
+
+
大佬,单独的一个bardata的时区信息我会改,我想一次性转化dataframe的那一列,如果在取数据那里嵌套一个for循环,我担心取1万个1分钟bardata是否会降低效率
def load_select_bar_data_from_mongodb(
# self,
symbol: str,
exchange: Exchange,
interval: Interval,
start: datetime,
end: datetime,
collection_name: str = None):
false_tzinsfo_bar_data_list=database_manager.load_bar_data(symbol, exchange, interval, start, end, collection_name)
new_tzinsfo_bar_list = []#正确时区bardata的list
for data in false_tzinsfo_bar_data_list:
new_tzinsfo_bar_list.append(data.datetime.replace(tzinfo=None))
df = pd.DataFrame(new_tzinsfo_bar_list)
df = df.rename(index=str, columns={"open_price": "open",
"high_price": "high",
"low_price": "low",
"close_price": "close",
})
data_select_columns = ["open", "high", "low", "close", "open_interest", "volume"]
df = df.set_index('datetime')[data_select_columns]
print('//',df.head(1).append(df.tail(1)),"//")
return df
new_tzinsfo_bar_list = []#正确时区bardata的list
for data in false_tzinsfo_bar_data_list:
data.datetime.replace(tzinfo=None)
new_tzinsfo_bar_list.append(data)
```
不对这段代码应该这样写,但是经过测试还是错了,并没有改变bar对象的属性,
如果用data.datetime.replace(tzinfo=None)代码
只是获得了一个时间类型,没有获得一个正确时区的bardata对象
取一年的1分钟bardata,用for循环成功修改每个bardata的时区属性(还没成功),这样内置的循环,取一次数据将耗时20s左右,性能降低了,而且还没写成功
+
+
+
+
现在问题是我存入的数据时区是正确的的,但是我按照自定义的load—bar——data去数据的时候,时区又被识别为一个错误的偏离上海时区+6分钟的时区,导致我生成的pandas dataframe 索引有问题
要=才能改变属性吧,直接去掉时区信息就可以了呀
xiaohe wrote:
要=才能改变属性吧,直接去掉时区信息就可以了呀
+
+
+
+
+
+
++
大佬 问题的核心点在于不会在pandas中统一修改一整列的时区,for循环很费时
问题在于从数据库读取出来的时候 时间就错了,我觉得这算个小bug了
数据库存储的是+8:00,读取出来是+8:06
+
+
+
https://www.cnblogs.com/foxracle/p/3258034.html
也许这是MongoDB的问题