[Mod]change function name and complete test of OkexfGateway

This commit is contained in:
vn.py 2019-04-21 23:08:06 +08:00
parent 723efbcf1e
commit 62397fafc0
2 changed files with 15 additions and 10 deletions

View File

@ -14,6 +14,7 @@ from vnpy.gateway.okex import OkexGateway
from vnpy.gateway.huobi import HuobiGateway from vnpy.gateway.huobi import HuobiGateway
from vnpy.gateway.bitfinex import BitfinexGateway from vnpy.gateway.bitfinex import BitfinexGateway
from vnpy.gateway.onetoken import OnetokenGateway from vnpy.gateway.onetoken import OnetokenGateway
from vnpy.gateway.okexf import OkexfGateway
from vnpy.app.cta_strategy import CtaStrategyApp from vnpy.app.cta_strategy import CtaStrategyApp
from vnpy.app.csv_loader import CsvLoaderApp from vnpy.app.csv_loader import CsvLoaderApp
@ -38,6 +39,7 @@ def main():
main_engine.add_gateway(HuobiGateway) main_engine.add_gateway(HuobiGateway)
main_engine.add_gateway(BitfinexGateway) main_engine.add_gateway(BitfinexGateway)
main_engine.add_gateway(OnetokenGateway) main_engine.add_gateway(OnetokenGateway)
main_engine.add_gateway(OkexfGateway)
main_engine.add_app(CtaStrategyApp) main_engine.add_app(CtaStrategyApp)
main_engine.add_app(CtaBacktesterApp) main_engine.add_app(CtaBacktesterApp)

View File

@ -11,7 +11,7 @@ import json
import base64 import base64
import zlib import zlib
from copy import copy from copy import copy
from datetime import datetime from datetime import datetime, timedelta
from threading import Lock from threading import Lock
from urllib.parse import urlencode from urllib.parse import urlencode
@ -228,8 +228,12 @@ class OkexfRestApi(RestClient):
def send_order(self, req: OrderRequest): def send_order(self, req: OrderRequest):
"""""" """"""
orderid = f"a{self.connect_time}{self._new_order_id()}" # Need both offset and direction for sending order.
if (req.offset, req.direction) not in TYPE_VT2OKEXF:
return ""
orderid = f"a{self.connect_time}{self._new_order_id()}"
data = { data = {
"client_oid": orderid, "client_oid": orderid,
"type": TYPE_VT2OKEXF[(req.offset, req.direction)], "type": TYPE_VT2OKEXF[(req.offset, req.direction)],
@ -407,7 +411,7 @@ class OkexfRestApi(RestClient):
traded=int(order_data["filled_qty"]), traded=int(order_data["filled_qty"]),
price=float(order_data["price"]), price=float(order_data["price"]),
volume=float(order_data["size"]), volume=float(order_data["size"]),
time=UTC2LOCAL(order_data["timestamp"]).strftime("%H:%M:%S"), time=utc_to_local(order_data["timestamp"]).strftime("%H:%M:%S"),
status=STATUS_OKEXF2VT[order_data["status"]], status=STATUS_OKEXF2VT[order_data["status"]],
gateway_name=self.gateway_name, gateway_name=self.gateway_name,
) )
@ -698,7 +702,7 @@ class OkexfWebsocketApi(WebsocketClient):
tick.high_price = d["high_24h"] tick.high_price = d["high_24h"]
tick.low_price = d["low_24h"] tick.low_price = d["low_24h"]
tick.volume = d["volume_24h"] tick.volume = d["volume_24h"]
tick.datetime = UTC2LOCAL(d["timestamp"]) tick.datetime = utc_to_local(d["timestamp"])
self.gateway.on_tick(copy(tick)) self.gateway.on_tick(copy(tick))
@ -722,7 +726,7 @@ class OkexfWebsocketApi(WebsocketClient):
tick.__setattr__("ask_price_%s" % (n + 1), price) tick.__setattr__("ask_price_%s" % (n + 1), price)
tick.__setattr__("ask_volume_%s" % (n + 1), volume) tick.__setattr__("ask_volume_%s" % (n + 1), volume)
tick.datetime = UTC2LOCAL(d["timestamp"]) tick.datetime = utc_to_local(d["timestamp"])
self.gateway.on_tick(copy(tick)) self.gateway.on_tick(copy(tick))
def on_order(self, d): def on_order(self, d):
@ -738,7 +742,7 @@ class OkexfWebsocketApi(WebsocketClient):
price=float(d["price"]), price=float(d["price"]),
volume=float(d["size"]), volume=float(d["size"]),
traded=float(d["filled_qty"]), traded=float(d["filled_qty"]),
time=UTC2LOCAL(d["timestamp"]).strftime("%H:%M:%S"), time=utc_to_local(d["timestamp"]).strftime("%H:%M:%S"),
status=STATUS_OKEXF2VT[d["status"]], status=STATUS_OKEXF2VT[d["status"]],
gateway_name=self.gateway_name, gateway_name=self.gateway_name,
) )
@ -816,8 +820,7 @@ def get_timestamp():
return timestamp + "Z" return timestamp + "Z"
def UTC2LOCAL(timestamp): def utc_to_local(timestamp):
import datetime time = datetime.strptime(timestamp, "%Y-%m-%dT%H:%M:%S.%fZ")
time = datetime.datetime.strptime(timestamp, "%Y-%m-%dT%H:%M:%S.%fZ") utc_time = time + timedelta(hours=8)
utc_time = time + datetime.timedelta(hours=8)
return utc_time return utc_time