[增强功能] 加固
This commit is contained in:
parent
0d0a7818e1
commit
bb9ec7ba98
@ -14,6 +14,7 @@ from queue import Empty
|
|||||||
from threading import Thread, Condition
|
from threading import Thread, Condition
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
import shelve
|
import shelve
|
||||||
|
import chardet
|
||||||
|
|
||||||
from ibapi import comm
|
from ibapi import comm
|
||||||
from ibapi.client import EClient
|
from ibapi.client import EClient
|
||||||
@ -79,7 +80,8 @@ EXCHANGE_VT2IB = {
|
|||||||
Exchange.HKFE: "HKFE",
|
Exchange.HKFE: "HKFE",
|
||||||
Exchange.CFE: "CFE",
|
Exchange.CFE: "CFE",
|
||||||
Exchange.NYSE: "NYSE",
|
Exchange.NYSE: "NYSE",
|
||||||
Exchange.NASDAQ: "NASDAQ"
|
Exchange.NASDAQ: "NASDAQ",
|
||||||
|
Exchange.AMEX: "AMEX"
|
||||||
}
|
}
|
||||||
EXCHANGE_IB2VT = {v: k for k, v in EXCHANGE_VT2IB.items()}
|
EXCHANGE_IB2VT = {v: k for k, v in EXCHANGE_VT2IB.items()}
|
||||||
|
|
||||||
@ -176,7 +178,7 @@ class IbGateway(BaseGateway):
|
|||||||
port = setting["TWS端口"]
|
port = setting["TWS端口"]
|
||||||
clientid = setting["客户号"]
|
clientid = setting["客户号"]
|
||||||
account = setting["交易账户"]
|
account = setting["交易账户"]
|
||||||
|
self.write_log(f'开始连接{account}')
|
||||||
self.api.connect(host, port, clientid, account)
|
self.api.connect(host, port, clientid, account)
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
@ -184,11 +186,13 @@ class IbGateway(BaseGateway):
|
|||||||
Close gateway connection.
|
Close gateway connection.
|
||||||
"""
|
"""
|
||||||
self.api.close()
|
self.api.close()
|
||||||
|
self.write_log(f'{self.gateway_name}断开')
|
||||||
|
|
||||||
def subscribe(self, req: SubscribeRequest):
|
def subscribe(self, req: SubscribeRequest):
|
||||||
"""
|
"""
|
||||||
Subscribe tick data update.
|
Subscribe tick data update.
|
||||||
"""
|
"""
|
||||||
|
self.write_log(f'订阅行情:{req.__dict__}')
|
||||||
self.api.subscribe(req)
|
self.api.subscribe(req)
|
||||||
|
|
||||||
def send_order(self, req: OrderRequest):
|
def send_order(self, req: OrderRequest):
|
||||||
@ -623,6 +627,7 @@ class IbApi(EWrapper):
|
|||||||
Connect to TWS.
|
Connect to TWS.
|
||||||
"""
|
"""
|
||||||
if self.status:
|
if self.status:
|
||||||
|
self.gateway.write_log(f'已连接,不再重连')
|
||||||
return
|
return
|
||||||
|
|
||||||
self.clientid = clientid
|
self.clientid = clientid
|
||||||
@ -728,10 +733,12 @@ class IbApi(EWrapper):
|
|||||||
|
|
||||||
def query_history(self, req: HistoryRequest):
|
def query_history(self, req: HistoryRequest):
|
||||||
""""""
|
""""""
|
||||||
|
self.gateway.write_log(f'查询历史数据:{req.__dict__}')
|
||||||
self.history_req = req
|
self.history_req = req
|
||||||
|
|
||||||
self.reqid += 1
|
self.reqid += 1
|
||||||
|
|
||||||
|
# 转换为ib的合约
|
||||||
ib_contract = generate_ib_contract(req.symbol, req.exchange)
|
ib_contract = generate_ib_contract(req.symbol, req.exchange)
|
||||||
|
|
||||||
if req.end:
|
if req.end:
|
||||||
@ -822,9 +829,9 @@ class IbClient(EClient):
|
|||||||
def generate_ib_contract(symbol: str, exchange: Exchange) -> Optional[Contract]:
|
def generate_ib_contract(symbol: str, exchange: Exchange) -> Optional[Contract]:
|
||||||
"""生成ib合约"""
|
"""生成ib合约"""
|
||||||
try:
|
try:
|
||||||
fields = symbol.split(JOIN_SYMBOL)
|
|
||||||
|
|
||||||
ib_contract = Contract()
|
ib_contract = Contract()
|
||||||
|
if JOIN_SYMBOL in symbol:
|
||||||
|
fields = symbol.split(JOIN_SYMBOL)
|
||||||
ib_contract.exchange = EXCHANGE_VT2IB[exchange]
|
ib_contract.exchange = EXCHANGE_VT2IB[exchange]
|
||||||
ib_contract.secType = fields[-1]
|
ib_contract.secType = fields[-1]
|
||||||
ib_contract.currency = fields[-2]
|
ib_contract.currency = fields[-2]
|
||||||
@ -837,6 +844,11 @@ def generate_ib_contract(symbol: str, exchange: Exchange) -> Optional[Contract]:
|
|||||||
ib_contract.right = fields[2]
|
ib_contract.right = fields[2]
|
||||||
ib_contract.strike = float(fields[3])
|
ib_contract.strike = float(fields[3])
|
||||||
ib_contract.multiplier = int(fields[4])
|
ib_contract.multiplier = int(fields[4])
|
||||||
|
else:
|
||||||
|
ib_contract.symbol = symbol
|
||||||
|
ib_contract.exchange = EXCHANGE_VT2IB[exchange]
|
||||||
|
ib_contract.secType = "STK"
|
||||||
|
|
||||||
except IndexError:
|
except IndexError:
|
||||||
ib_contract = None
|
ib_contract = None
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user