diff --git a/vn.trader/utilSinaClient.py b/vn.trader/utilSinaClient.py index a995b50d..8b92de2d 100644 --- a/vn.trader/utilSinaClient.py +++ b/vn.trader/utilSinaClient.py @@ -224,6 +224,85 @@ class UtilSinaClient(object): self.strategy.writeCtaLog(u'加载Sina历史分钟数据失败:'+str(e)) 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): """# 从sina加载最新的Day数据""" @@ -301,4 +380,6 @@ if __name__ == '__main__': #rt = sina.getTicks(symbol='RB1705', callback=t.addTick) #rt = sina.getTicks2(symbol='TF1706', callback=t.addTick) - rt = sina.getTicks3(symbol='TF1709', callback=t.addTick) \ No newline at end of file + #rt = sina.getTicks3(symbol='TF1709', callback=t.addTick) + + rt = sina.getMinBars2(symbol='TF1709',minute=60, callback=t.addBar) \ No newline at end of file