[Mod] improve code readability
This commit is contained in:
parent
af94e1e186
commit
1b139f34a1
@ -3,14 +3,9 @@
|
|||||||
Author: vigarbuaa
|
Author: vigarbuaa
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import hashlib
|
|
||||||
import hmac
|
|
||||||
import sys
|
import sys
|
||||||
import time
|
|
||||||
from copy import copy
|
|
||||||
from threading import Lock
|
from threading import Lock
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from urllib.parse import urlencode
|
|
||||||
from vnpy.api.rest import Request, RestClient
|
from vnpy.api.rest import Request, RestClient
|
||||||
from vnpy.api.websocket import WebsocketClient
|
from vnpy.api.websocket import WebsocketClient
|
||||||
from vnpy.event import Event
|
from vnpy.event import Event
|
||||||
@ -21,16 +16,15 @@ from vnpy.trader.constant import (
|
|||||||
Exchange,
|
Exchange,
|
||||||
OrderType,
|
OrderType,
|
||||||
Product,
|
Product,
|
||||||
Status,
|
Status
|
||||||
Offset,
|
|
||||||
Interval
|
|
||||||
)
|
)
|
||||||
from vnpy.trader.gateway import BaseGateway
|
from vnpy.trader.gateway import BaseGateway
|
||||||
from vnpy.trader.object import (
|
from vnpy.trader.object import (
|
||||||
TickData,
|
TickData,
|
||||||
OrderData,
|
OrderData,
|
||||||
TradeData,
|
TradeData,
|
||||||
PositionData, AccountData,
|
PositionData,
|
||||||
|
AccountData,
|
||||||
ContractData,
|
ContractData,
|
||||||
OrderRequest,
|
OrderRequest,
|
||||||
CancelRequest,
|
CancelRequest,
|
||||||
@ -74,7 +68,7 @@ class AlpacaGateway(BaseGateway):
|
|||||||
"key": "",
|
"key": "",
|
||||||
"secret": "",
|
"secret": "",
|
||||||
"session": 3,
|
"session": 3,
|
||||||
"服务器": ["REAL", "PAPER"],
|
"server": ["REAL", "PAPER"],
|
||||||
"proxy_host": "127.0.0.1",
|
"proxy_host": "127.0.0.1",
|
||||||
"proxy_port": 1080,
|
"proxy_port": 1080,
|
||||||
}
|
}
|
||||||
@ -94,7 +88,7 @@ class AlpacaGateway(BaseGateway):
|
|||||||
session = setting["session"]
|
session = setting["session"]
|
||||||
proxy_host = setting["proxy_host"]
|
proxy_host = setting["proxy_host"]
|
||||||
proxy_port = setting["proxy_port"]
|
proxy_port = setting["proxy_port"]
|
||||||
env = setting['服务器']
|
env = setting['server']
|
||||||
rest_url = REST_HOST if env == "REAL" else PAPER_REST_HOST
|
rest_url = REST_HOST if env == "REAL" else PAPER_REST_HOST
|
||||||
websocket_url = WEBSOCKET_HOST if env == "REAL" else PAPER_WEBSOCKET_HOST
|
websocket_url = WEBSOCKET_HOST if env == "REAL" else PAPER_WEBSOCKET_HOST
|
||||||
self.rest_api.connect(key, secret, session,
|
self.rest_api.connect(key, secret, session,
|
||||||
@ -127,15 +121,6 @@ class AlpacaGateway(BaseGateway):
|
|||||||
self.rest_api.stop()
|
self.rest_api.stop()
|
||||||
self.ws_api.stop()
|
self.ws_api.stop()
|
||||||
|
|
||||||
def process_timer_event(self, event: Event):
|
|
||||||
""""""
|
|
||||||
self.count += 1
|
|
||||||
if self.count < 5:
|
|
||||||
return
|
|
||||||
|
|
||||||
self.query_account()
|
|
||||||
self.query_position()
|
|
||||||
|
|
||||||
def init_query(self):
|
def init_query(self):
|
||||||
""""""
|
""""""
|
||||||
self.count = 0
|
self.count = 0
|
||||||
@ -146,8 +131,7 @@ class AlpacaGateway(BaseGateway):
|
|||||||
self.count += 1
|
self.count += 1
|
||||||
if self.count < 5:
|
if self.count < 5:
|
||||||
return
|
return
|
||||||
else:
|
self.count = 0
|
||||||
self.count = 0
|
|
||||||
|
|
||||||
self.query_account()
|
self.query_account()
|
||||||
self.query_position()
|
self.query_position()
|
||||||
@ -441,14 +425,6 @@ class AlpacaWebsocketApi(WebsocketClient):
|
|||||||
}}
|
}}
|
||||||
self.send_packet(params)
|
self.send_packet(params)
|
||||||
|
|
||||||
def send_order(self, req: OrderRequest):
|
|
||||||
pass
|
|
||||||
|
|
||||||
# ----------------------------------------------------------------------
|
|
||||||
def cancel_order(self, req: CancelRequest):
|
|
||||||
""""""
|
|
||||||
pass
|
|
||||||
|
|
||||||
def on_connected(self):
|
def on_connected(self):
|
||||||
""""""
|
""""""
|
||||||
self.gateway.write_log("Websocket API连接成功")
|
self.gateway.write_log("Websocket API连接成功")
|
||||||
@ -473,17 +449,17 @@ class AlpacaWebsocketApi(WebsocketClient):
|
|||||||
else:
|
else:
|
||||||
print("unrecognize msg", packet)
|
print("unrecognize msg", packet)
|
||||||
|
|
||||||
# ----------------------------------------------------------------------
|
|
||||||
def on_data(self, data):
|
def on_data(self, data):
|
||||||
print("on_data is {}".format(data))
|
print("on_data is {}".format(data))
|
||||||
stream_ret = data['stream']
|
stream_ret = data['stream']
|
||||||
data_ret = data['data']
|
data_ret = data['data']
|
||||||
if(stream_ret == "account_updates"):
|
if(stream_ret == "account_updates"):
|
||||||
|
frozen = float(data_ret['cash']) - float(data_ret['cash_withdrawable'])
|
||||||
|
|
||||||
account = AccountData(
|
account = AccountData(
|
||||||
accountid=data_ret['id'],
|
accountid=data_ret['id'],
|
||||||
balance=float(data_ret['cash']),
|
balance=float(data_ret['cash']),
|
||||||
frozen=float(data_ret['cash']) -
|
frozen=frozen,
|
||||||
float(data_ret['cash_withdrawable']),
|
|
||||||
gateway_name=self.gateway_name
|
gateway_name=self.gateway_name
|
||||||
)
|
)
|
||||||
self.gateway.on_account(account)
|
self.gateway.on_account(account)
|
||||||
@ -510,9 +486,8 @@ class AlpacaWebsocketApi(WebsocketClient):
|
|||||||
else:
|
else:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
# ----------------------------------------------------------------------
|
|
||||||
def handle_auth(self, data):
|
def handle_auth(self, data):
|
||||||
stream_ret = data['stream']
|
# stream_ret = data['stream']
|
||||||
data_ret = data['data']
|
data_ret = data['data']
|
||||||
if (data_ret['status'] == "authorized"):
|
if (data_ret['status'] == "authorized"):
|
||||||
print("authorization success!!!")
|
print("authorization success!!!")
|
||||||
@ -524,18 +499,6 @@ class AlpacaWebsocketApi(WebsocketClient):
|
|||||||
else:
|
else:
|
||||||
print("??unhandled status: ", data)
|
print("??unhandled status: ", data)
|
||||||
|
|
||||||
# ----------------------------------------------------------------------
|
|
||||||
def on_response(self, data):
|
|
||||||
""""""
|
|
||||||
pass
|
|
||||||
# ----------------------------------------------------------------------
|
|
||||||
|
|
||||||
def on_update(self, data):
|
|
||||||
""""""
|
|
||||||
pass
|
|
||||||
|
|
||||||
# ----------------------------------------------------------------------
|
|
||||||
|
|
||||||
def on_error(self, exception_type: type, exception_value: Exception, tb):
|
def on_error(self, exception_type: type, exception_value: Exception, tb):
|
||||||
""""""
|
""""""
|
||||||
print("on_error: ", type, Exception, tb)
|
print("on_error: ", type, Exception, tb)
|
||||||
|
Loading…
Reference in New Issue
Block a user