增加取中金所分钟的方法bar

This commit is contained in:
msincenselee 2017-05-11 11:37:35 +08:00
parent fe13e89ca9
commit fdea5a35af

View File

@ -224,6 +224,85 @@ class UtilSinaClient(object):
self.strategy.writeCtaLog(u'加载Sina历史分钟数据失败'+str(e)) self.strategy.writeCtaLog(u'加载Sina历史分钟数据失败'+str(e))
return False return False
def getMinBars2(self, symbol, minute, callback):
"""# 从sina加载最新的M5,M15,M30,M60数据(针对中金所)"""
if minute not in {5, 15, 30, 60}:
return False
sinaBars = []
try:
timestamp = (datetime.utcnow() - datetime(1970, 1, 1)).total_seconds()
url=u'http://stock2.finance.sina.com.cn/futures/api/jsonp.php/var%20_{1}_{0}_{2}=/InnerFuturesNewService.getFewMinLine?symbol={1}&type={0}'.format(minute,symbol,timestamp)
#url = u'http://stock2.finance.sina.com.cn/futures/api/json.php/InnerFuturesService.getInnerFutures{0}MinKLine?symbol={1}'.format(minute,symbol)
self.strategy.writeCtaLog(u'从sina下载{0}{1}分钟数据 {2}'.format(symbol,minute, url))
response_data = self.session.get(url).content
response_data = response_data.decode('gbk').split('=')[-1]
response_data = response_data.replace('(', '')
response_data = response_data.replace(');', '')
responses = execjs.eval(response_data)
dayVolume = 0
for item in responses:
bar = CtaBarData()
bar.vtSymbol = symbol
bar.symbol = symbol
# bar的close time
sinaDt = datetime.strptime(item['d'], '%Y-%m-%d %H:%M:00')
if minute in {5, 15} and sinaDt.hour == 10 and sinaDt.minute == 30:
# 这个是sina的bug它把10:15 ~10:30也包含进来了
continue
if minute == 60 and sinaDt.hour in {11,23,1,2} and sinaDt.minute == 30:
bar.datetime = sinaDt - timedelta(seconds=(minute /2)* 60)
else:
bar.datetime = sinaDt - timedelta(seconds=minute * 60)
bar.date = bar.datetime.strftime('%Y%m%d')
bar.tradingDay = bar.date # todo: 需要修改晚上21点后修改为next workingday
bar.time = bar.datetime.strftime('%H:%M:00')
bar.open = float(item['o'])
bar.high = float(item['h'])
bar.low = float(item['l'])
bar.close = float(item['c'])
bar.volume = int(item['v'])
# 计算dayvolume
if not sinaBars:
dayVolume = bar.volume
else:
if sinaBars[-1].datetime.hour == 14 and bar.datetime.hour !=14:
dayVolume = bar.volume
else:
dayVolume += bar.volume
bar.dayVolume = dayVolume
sinaBars.append(bar)
if len(sinaBars)>0:
self.strategy.writeCtaLog(u'从sina读取了{0}{1}分钟数据'.format(len(sinaBars),minute))
# 把sina的bar灌入回调函数
for bar in sinaBars:
callback(bar)
# 处理完毕,清空
sinaBars = []
return True
else:
self.strategy.writeCtaLog(u'从sina读取{0}分钟数据失败'.format(minute))
return False
except Exception as e:
self.strategy.writeCtaLog(u'加载Sina历史分钟数据失败'+str(e))
return False
def getDayBars(self, symbol, callback): def getDayBars(self, symbol, callback):
"""# 从sina加载最新的Day数据""" """# 从sina加载最新的Day数据"""
@ -301,4 +380,6 @@ if __name__ == '__main__':
#rt = sina.getTicks(symbol='RB1705', callback=t.addTick) #rt = sina.getTicks(symbol='RB1705', callback=t.addTick)
#rt = sina.getTicks2(symbol='TF1706', callback=t.addTick) #rt = sina.getTicks2(symbol='TF1706', callback=t.addTick)
rt = sina.getTicks3(symbol='TF1709', callback=t.addTick) #rt = sina.getTicks3(symbol='TF1709', callback=t.addTick)
rt = sina.getMinBars2(symbol='TF1709',minute=60, callback=t.addBar)