[Mod] add support for bitfinex margin trading

This commit is contained in:
vn.py 2019-10-29 16:15:43 +08:00
parent af3e90c280
commit c1151dc8fc

View File

@ -53,6 +53,13 @@ ORDERTYPE_VT2BITFINEX = {
OrderType.LIMIT: "EXCHANGE LIMIT", OrderType.LIMIT: "EXCHANGE LIMIT",
OrderType.MARKET: "EXCHANGE MARKET", OrderType.MARKET: "EXCHANGE MARKET",
} }
ORDERTYPE_BITFINEX2VT = {
"EXCHANGE LIMIT": OrderType.LIMIT,
"EXCHANGE MARKET": OrderType.MARKET,
"LIMIT": OrderType.LIMIT,
"MARKET": OrderType.MARKET
}
DIRECTION_VT2BITFINEX = { DIRECTION_VT2BITFINEX = {
Direction.LONG: "Buy", Direction.LONG: "Buy",
Direction.SHORT: "Sell", Direction.SHORT: "Sell",
@ -86,6 +93,7 @@ class BitfinexGateway(BaseGateway):
"session": 3, "session": 3,
"proxy_host": "127.0.0.1", "proxy_host": "127.0.0.1",
"proxy_port": 1080, "proxy_port": 1080,
"margin": ["False", "True"]
} }
exchanges = [Exchange.BITFINEX] exchanges = [Exchange.BITFINEX]
@ -108,8 +116,13 @@ class BitfinexGateway(BaseGateway):
proxy_host = setting["proxy_host"] proxy_host = setting["proxy_host"]
proxy_port = setting["proxy_port"] proxy_port = setting["proxy_port"]
if setting["margin"] == "True":
margin = True
else:
margin = False
self.rest_api.connect(key, secret, session, proxy_host, proxy_port) self.rest_api.connect(key, secret, session, proxy_host, proxy_port)
self.ws_api.connect(key, secret, proxy_host, proxy_port) self.ws_api.connect(key, secret, proxy_host, proxy_port, margin)
self.event_engine.register(EVENT_TIMER, self.process_timer_event) self.event_engine.register(EVENT_TIMER, self.process_timer_event)
@ -212,7 +225,7 @@ class BitfinexRestApi(RestClient):
secret: str, secret: str,
session: int, session: int,
proxy_host: str, proxy_host: str,
proxy_port: int, proxy_port: int
): ):
""" """
Initialize connection to REST server. Initialize connection to REST server.
@ -383,11 +396,17 @@ class BitfinexWebsocketApi(WebsocketClient):
self.subscribed = {} self.subscribed = {}
def connect( def connect(
self, key: str, secret: str, proxy_host: str, proxy_port: int self,
key: str,
secret: str,
proxy_host: str,
proxy_port: int,
margin: bool
): ):
"""""" """"""
self.key = key self.key = key
self.secret = secret.encode() self.secret = secret.encode()
self.margin = margin
self.init(WEBSOCKET_HOST, proxy_host, proxy_port) self.init(WEBSOCKET_HOST, proxy_host, proxy_port)
self.start() self.start()
@ -432,9 +451,13 @@ class BitfinexWebsocketApi(WebsocketClient):
else: else:
amount = -req.volume amount = -req.volume
order_type = ORDERTYPE_VT2BITFINEX[req.type]
if self.margin:
order_type = order_type.replace("EXCHANGE ", "")
o = { o = {
"cid": orderid, "cid": orderid,
"type": ORDERTYPE_VT2BITFINEX[req.type], "type": order_type,
"symbol": "t" + req.symbol, "symbol": "t" + req.symbol,
"amount": str(amount), "amount": str(amount),
"price": str(req.price), "price": str(req.price),
@ -609,19 +632,26 @@ class BitfinexWebsocketApi(WebsocketClient):
def on_wallet(self, data): def on_wallet(self, data):
"""""" """"""
if str(data[0]) == "exchange": print(data)
accountid = str(data[1]) # Exchange Mode
account = self.accounts.get(accountid, None) if not self.margin and str(data[0]) != "exchange":
if not account: return
account = AccountData( # Margin Mode
accountid=accountid, elif self.margin and str(data[0]) != "margin":
gateway_name=self.gateway_name, return
)
account.balance = float(data[2]) accountid = str(data[1])
account.available = 0.0 account = self.accounts.get(accountid, None)
account.frozen = 0.0 if not account:
self.gateway.on_account(copy(account)) account = AccountData(
accountid=accountid,
gateway_name=self.gateway_name,
)
account.balance = float(data[2])
account.available = 0.0
account.frozen = 0.0
self.gateway.on_account(copy(account))
def on_trade_update(self, data): def on_trade_update(self, data):
"""""" """"""
@ -776,6 +806,7 @@ class BitfinexWebsocketApi(WebsocketClient):
order = OrderData( order = OrderData(
symbol=str(data[3].replace("t", "")), symbol=str(data[3].replace("t", "")),
exchange=Exchange.BITFINEX, exchange=Exchange.BITFINEX,
type=ORDERTYPE_BITFINEX2VT[data[8]],
orderid=orderid, orderid=orderid,
status=Status.REJECTED, status=Status.REJECTED,
direction=direction, direction=direction,
@ -808,6 +839,7 @@ class BitfinexWebsocketApi(WebsocketClient):
symbol=str(data[3].replace("t", "")), symbol=str(data[3].replace("t", "")),
exchange=Exchange.BITFINEX, exchange=Exchange.BITFINEX,
orderid=orderid, orderid=orderid,
type=ORDERTYPE_BITFINEX2VT[data[8]],
status=STATUS_BITFINEX2VT[order_status], status=STATUS_BITFINEX2VT[order_status],
direction=direction, direction=direction,
price=float(data[16]), price=float(data[16]),