diff --git a/vnpy/api/rest/rest_client.py b/vnpy/api/rest/rest_client.py index 008b4c7f..df7b6ea7 100644 --- a/vnpy/api/rest/rest_client.py +++ b/vnpy/api/rest/rest_client.py @@ -374,7 +374,7 @@ class RestClient(object): request.response = response status_code = response.status_code - if not stream: # normal API: + if not stream: # normal API: # just call callback with all contents received. if status_code // 100 == 2: # 2xx codes are all successful if status_code == 204: @@ -388,7 +388,7 @@ class RestClient(object): request.on_failed(status_code, request) else: self.on_failed(status_code, request) - else: # streaming API: + else: # streaming API: if request.on_connected: request.on_connected(request) # split response by lines, and call one callback for each line. diff --git a/vnpy/gateway/oanda/oanda_api_base.py b/vnpy/gateway/oanda/oanda_api_base.py index 2182187b..c3fe192f 100644 --- a/vnpy/gateway/oanda/oanda_api_base.py +++ b/vnpy/gateway/oanda/oanda_api_base.py @@ -1,22 +1,11 @@ import json import sys -from dataclasses import dataclass -from datetime import datetime -from threading import Lock -from typing import Dict, List, Optional, TYPE_CHECKING, Tuple +from typing import TYPE_CHECKING -from requests import ConnectionError - -from vnpy.api.rest import Request -from vnpy.trader.constant import Direction, Exchange, Interval, Offset, Product, Status -from vnpy.trader.object import AccountData, BarData, CancelRequest, ContractData, OrderRequest, \ - PositionData, OrderData -from .oanda_common import (INTERVAL_VT2OANDA, ORDER_TYPE_VT2OANDA, - STATUS_OANDA2VT, parse_time, INTERVAL_VT2OANDA_DELTA, parse_datetime) +from vnpy.api.rest import Request, RestClient if TYPE_CHECKING: from vnpy.gateway.oanda import OandaGateway -from vnpy.api.rest import RestClient _ = lambda x: x # noqa @@ -25,6 +14,7 @@ class OandaApiBase(RestClient): """ Oanda Base API """ + def __init__(self, gateway: "OandaGateway"): super().__init__() self.gateway = gateway @@ -62,4 +52,3 @@ class OandaApiBase(RestClient): sys.stderr.write( self.exception_detail(exception_type, exception_value, tb, request) ) - diff --git a/vnpy/gateway/oanda/oanda_common.py b/vnpy/gateway/oanda/oanda_common.py index 59cbcabe..968063c7 100644 --- a/vnpy/gateway/oanda/oanda_common.py +++ b/vnpy/gateway/oanda/oanda_common.py @@ -6,7 +6,7 @@ from vnpy.trader.constant import Direction, Interval, OrderType, Status if TYPE_CHECKING: # noinspection PyUnresolvedReferences - from vnpy.gateway.oanda import OandaGateway + from vnpy.gateway.oanda import OandaGateway # noqa STATUS_OANDA2VT = { "PENDING": Status.NOTTRADED, diff --git a/vnpy/gateway/oanda/oanda_gateway.py b/vnpy/gateway/oanda/oanda_gateway.py index 9a602e3a..f099ac69 100644 --- a/vnpy/gateway/oanda/oanda_gateway.py +++ b/vnpy/gateway/oanda/oanda_gateway.py @@ -5,22 +5,19 @@ todo: """ from dataclasses import dataclass from datetime import datetime -from typing import Dict, List, Any, Tuple, Optional +from typing import Any, Dict, List, Optional, Tuple from vnpy.event import Event -from vnpy.trader.constant import (Exchange, OrderType, Interval, Direction, Offset, Status) +from vnpy.trader.constant import (Direction, Exchange, Interval, Offset, OrderType, Status) from vnpy.trader.event import EVENT_TIMER from vnpy.trader.gateway import BaseGateway -from vnpy.trader.object import (CancelRequest, HistoryRequest, OrderData, OrderRequest, - PositionData, SubscribeRequest, TickData, BarData) -from .oanda_common import DIRECTION_OANDA2VT, OPPOSITE_DIRECTION, ORDER_TYPE_OANDA2VT, \ - STOP_ORDER_STATUS_OANDA2VT, parse_datetime, STATUS_OANDA2VT, INTERVAL_VT2OANDA, local_tz, \ - utc_tz, INTERVAL_VT2OANDA_INT, parse_time, INTERVAL_VT2OANDA_DELTA -from .oanda_rest_api import OandaRestApi, HistoryDataNextInfo +from vnpy.trader.object import (BarData, CancelRequest, HistoryRequest, OrderData, OrderRequest, + SubscribeRequest) +from .oanda_common import INTERVAL_VT2OANDA_DELTA, ORDER_TYPE_OANDA2VT, local_tz, parse_time, utc_tz +from .oanda_rest_api import HistoryDataNextInfo, OandaRestApi from .oanda_stream_api import OandaStreamApi - -_ = lambda x:x # noqa +_ = lambda x: x # noqa @dataclass() @@ -99,10 +96,10 @@ class OandaGateway(BaseGateway): self.rest_api.query_positions() def query_first_history(self, - symbol: str, - interval: Interval, - start: datetime, - ) -> Tuple[List[BarData], "HistoryDataNextInfo"]: + symbol: str, + interval: Interval, + start: datetime, + ) -> Tuple[List[BarData], "HistoryDataNextInfo"]: # datetime for a bar is close_time # we got open_time from API. @@ -205,4 +202,3 @@ class OandaGateway(BaseGateway): ) self.orders[order_id] = order return order - diff --git a/vnpy/gateway/oanda/oanda_rest_api.py b/vnpy/gateway/oanda/oanda_rest_api.py index ceb4cf79..852931c8 100644 --- a/vnpy/gateway/oanda/oanda_rest_api.py +++ b/vnpy/gateway/oanda/oanda_rest_api.py @@ -1,5 +1,3 @@ -import json -import sys from dataclasses import dataclass from datetime import datetime from threading import Lock @@ -11,10 +9,9 @@ from vnpy.api.rest import Request from vnpy.gateway.oanda.oanda_api_base import OandaApiBase from vnpy.trader.constant import Direction, Exchange, Interval, Offset, Product, Status from vnpy.trader.object import AccountData, BarData, CancelRequest, ContractData, OrderRequest, \ - PositionData, OrderData -from .oanda_common import (INTERVAL_VT2OANDA, ORDER_TYPE_VT2OANDA, - STATUS_OANDA2VT, parse_time, INTERVAL_VT2OANDA_DELTA, parse_datetime, - utc_tz, local_tz) + PositionData +from .oanda_common import (INTERVAL_VT2OANDA, INTERVAL_VT2OANDA_DELTA, ORDER_TYPE_VT2OANDA, + STATUS_OANDA2VT, local_tz, parse_datetime, parse_time, utc_tz) if TYPE_CHECKING: from vnpy.gateway.oanda import OandaGateway @@ -122,7 +119,6 @@ class OandaRestApi(OandaApiBase): order.time = parse_time(datetime.now().isoformat()) # Only add price for limit order. - str_price = str(req.price) data["type"] = ORDER_TYPE_VT2OANDA[req.type] data["price"] = str(req.price) self.gateway.orders[order.orderid] = order @@ -240,7 +236,7 @@ class OandaRestApi(OandaApiBase): def on_send_order(self, raw_data: dict, request: Request): """""" - order: OrderData = request.extra + # order: OrderData = request.extra # creation = raw_data.get('orderCreateTransaction', None) # if creation is not None: # order.status = Status.NOTTRADED @@ -314,7 +310,7 @@ class OandaRestApi(OandaApiBase): gateway_name=self.gateway_name, accountid=account_id, balance=NAV - pnl, - frozen=0, # no frozen + frozen=0, # no frozen ) self.gateway.on_account(account) @@ -347,15 +343,16 @@ class OandaRestApi(OandaApiBase): def parse_position_data(self, pos_data) -> Tuple[PositionData, PositionData]: symbol = pos_data['instrument'] - pos_long, pos_short = [PositionData( - gateway_name=self.gateway_name, - symbol=symbol, - exchange=Exchange.OANDA, - direction=direction, - volume=int(data['units']), - price=float(data.get('averagePrice', 0.0)), - pnl=float(data.get('unrealizedPL', 0.0)), - ) + pos_long, pos_short = [ + PositionData( + gateway_name=self.gateway_name, + symbol=symbol, + exchange=Exchange.OANDA, + direction=direction, + volume=int(data['units']), + price=float(data.get('averagePrice', 0.0)), + pnl=float(data.get('unrealizedPL', 0.0)), + ) for direction, data in ( (Direction.LONG, pos_data['long']), (Direction.SHORT, pos_data['short']) diff --git a/vnpy/gateway/oanda/oanda_stream_api.py b/vnpy/gateway/oanda/oanda_stream_api.py index 5c3cab06..4a433d02 100644 --- a/vnpy/gateway/oanda/oanda_stream_api.py +++ b/vnpy/gateway/oanda/oanda_stream_api.py @@ -1,5 +1,4 @@ from copy import copy -from copy import copy from dataclasses import dataclass from functools import partial from http.client import IncompleteRead, RemoteDisconnected diff --git a/vnpy/trader/constant.py b/vnpy/trader/constant.py index 57b8e631..a7ce039a 100644 --- a/vnpy/trader/constant.py +++ b/vnpy/trader/constant.py @@ -119,6 +119,7 @@ class Exchange(Enum): BINANCE = "BINANCE" COINBASE = "COINBASE" + class Currency(Enum): """ Currency.