Merge branch 'dev' of https://github.com/vnpy/vnpy into okex.websocket

This commit is contained in:
nanoric 2018-10-16 22:01:05 -04:00
commit 04068256d3
32 changed files with 188 additions and 17 deletions

View File

@ -0,0 +1,10 @@
{
"MONGO_HOST": "localhost",
"MONGO_PORT": 27017,
"SYMBOLS": [
"BTC/USDT.OKEX",
"BTC/USDT.HUOBIPRO",
"BTC/USDT.BINANCE"
]
}

View File

@ -0,0 +1,106 @@
# encoding: UTF-8
from __future__ import print_function
import json
import time
import datetime
import requests
from pymongo import MongoClient, ASCENDING
from vnpy.trader.vtObject import VtBarData
from vnpy.trader.app.ctaStrategy.ctaBase import MINUTE_DB_NAME
# 加载配置
config = open('config.json')
setting = json.load(config)
MONGO_HOST = setting['MONGO_HOST']
MONGO_PORT = setting['MONGO_PORT']
SYMBOLS = setting['SYMBOLS']
mc = MongoClient(MONGO_HOST, MONGO_PORT) # Mongo连接
db = mc[MINUTE_DB_NAME] # 数据库
#----------------------------------------------------------------------
def generateVtBar(vtSymbol, d):
"""生成K线"""
bar = VtBarData()
bar.vtSymbol = vtSymbol
bar.symbol, bar.exchange = bar.vtSymbol.split('.')
bar.datetime = datetime.datetime.fromtimestamp(d['time'])
bar.date = bar.datetime.strftime('%Y%m%d')
bar.time = bar.datetime.strftime('%H:%M:%S')
bar.open = d['open']
bar.high = d['high']
bar.low = d['low']
bar.close = d['close']
bar.volume = d['volumeto']
return bar
#----------------------------------------------------------------------
def downloadMinuteBarBySymbol(vtSymbol, end):
"""下载某一合约的分钟线数据"""
end = datetime.datetime.strptime(end, '%Y%m%d')
startTime = time.time()
cl = db[vtSymbol]
cl.ensure_index([('datetime', ASCENDING)], unique=True)
symbol, exchange = vtSymbol.split('.')
fsym, tsym = symbol.split('/')
url = 'https://min-api.cryptocompare.com/data/histominute'
params = {
'fsym': fsym,
'tsym': tsym,
'e': exchange,
'toTs': int(time.mktime(end.timetuple()))
}
resp = requests.get(url, headers={}, params=params)
if resp.status_code != 200:
print(u'%s数据下载失败' %vtSymbol)
return
j = resp.json()
l = j['Data']
for d in l:
bar = generateVtBar(vtSymbol, d)
d = bar.__dict__
flt = {'datetime': bar.datetime}
cl.replace_one(flt, d, True)
endTime = time.time()
cost = (endTime - startTime) * 1000
print(u'合约%s数据下载完成%s - %s,耗时%s毫秒' %(vtSymbol,
datetime.datetime.fromtimestamp(l[0]['time']),
datetime.datetime.fromtimestamp(l[-1]['time']),
cost))
#----------------------------------------------------------------------
def downloadAllMinuteBar(end):
"""下载所有配置中的合约的分钟线数据"""
print('-' * 50)
print(u'开始下载合约分钟线数据')
print('-' * 50)
for symbol in SYMBOLS:
downloadMinuteBarBySymbol(symbol, end)
time.sleep(1)
print('-' * 50)
print(u'合约分钟线数据下载完成')
print('-' * 50)

View File

@ -0,0 +1,14 @@
# encoding: UTF-8
"""
立即下载数据到数据库中用于手动执行更新操作
"""
from dataService import *
if __name__ == '__main__':
#downMinuteBarBySymbol('BTC/USDT.OKEX', '20181012')
#downMinuteBarBySymbol('BTC/USDT.HUOBIPRO', '20181012')
#downMinuteBarBySymbol('BTC/USDT.BINANCE', '20181012')
downloadAllMinuteBar('20181012')

View File

@ -31,10 +31,11 @@ headers = {'X-CoinAPI-Key': APIKEY}
#----------------------------------------------------------------------
def generateVtBar(symbol, d):
"""生成K线"""
l = symbol.split('_')
bar = VtBarData()
bar.symbol = symbol
bar.vtSymbol = symbol
bar.symbol = l[-2] + l[-1]
bar.exchange = l[0]
bar.vtSymbol = '/'.join([bar.symbol, bar.exchange])
bar.datetime = datetime.datetime.strptime(d['time_open'], '%Y-%m-%dT%H:%M:%S.%f0Z')
bar.date = bar.datetime.strftime('%Y%m%d')
bar.time = bar.datetime.strftime('%H:%M:%S')

View File

@ -0,0 +1,33 @@
# encoding: UTF-8
"""
定时服务可无人值守运行实现每日自动下载更新历史行情数据到数据库中
"""
from __future__ import print_function
from dataService import *
if __name__ == '__main__':
taskCompletedDate = None
taskTime = datetime.time(hour=22, minute=0)
# 进入主循环
while True:
t = datetime.datetime.now()
# 每天到达任务下载时间后,执行数据下载的操作
if t.time() > taskTime and (taskCompletedDate is None or t.date() != taskCompletedDate):
end = t.strftime('%Y%m%d')
start = (t - datetime.timedelta(1)).strftime('%Y%m%d')
# 下载过去24小时的K线数据
downloadAllMinuteBar(start, end)
# 更新任务完成的日期
taskCompletedDate = t.date()
else:
print(u'当前时间%s,任务定时%s' %(t, taskTime))
time.sleep(60)

View File

@ -0,0 +1,11 @@
# DataService说明
* ShcifcoDataService上海中期历史行情服务期货
* TqDataService天勤历史行情服务期货
* TushareDataServiceTuShare历史行情服务A股
* FutuDataService富途证券历史行情服务美股、港股
* CoinapiDataServiceCoinAPI.io历史行情服务数字货币

View File

@ -20,12 +20,4 @@
* ServerClient服务端业务逻辑和客户端GUI界面分离的VnTrader
* ShcifcoDataService上海中期历史行情服务期货
* TqDataService天勤历史行情服务期货
* TushareDataServiceTuShare历史行情服务A股
* FutuDataService富途证券历史行情服务美股、港股
* CoinapiDataServiceCoinAPI.io历史行情服务数字货币
* DataService用于下载历史行情数据以及每日数据自动更新的数据服务

View File

@ -111,7 +111,8 @@ class OkexApi(object):
if not self.reconnecting:
self.reconnecting = True
self.closeWebsocket() # 首先关闭之前的连接
self.closeWebsocket() # 首先关闭之前的连接
self.heartbeatReceived = True # 将心跳状态设为正常
self.initWebsocket()
self.reconnecting = False

View File

@ -7,6 +7,7 @@ from vnpy.trader.vtConstant import (DIRECTION_LONG, DIRECTION_SHORT,
OFFSET_OPEN, OFFSET_CLOSE,
STATUS_ALLTRADED, STATUS_CANCELLED,
STATUS_REJECTED)
from vnpy.trader.uiQt import QtGui
from vnpy.trader.app.algoTrading.algoTemplate import AlgoTemplate
@ -157,7 +158,7 @@ class ArbitrageAlgo(AlgoTemplate):
tick.bidPrice5,
volume)
elif self.netPos < 0:
self.passiveOrderID = self.buy(self.activeVtSymbol,
self.passiveOrderID = self.buy(self.passiveVtSymbol,
tick.askPrice5,
volume)

View File

@ -198,6 +198,11 @@ class RestApi(BitmexRestApi):
'orderQty': orderReq.volume,
'clOrdID': str(orderId)
}
# 市价单不能有price字段
if orderReq.priceType == PRICETYPE_MARKETPRICE:
req.pop('price')
self.addReq('POST', '/order', self.onSendOrder, postdict=req)
return vtOrderID

View File

@ -113,9 +113,6 @@ class MainEngine(object):
if gateway:
gateway.connect()
# 接口连接后自动执行数据库连接的任务
self.dbConnect()
#----------------------------------------------------------------------
def subscribe(self, subscribeReq, gatewayName):