[Fix]use abs for trade volume

This commit is contained in:
vn.py 2019-04-17 09:20:29 +08:00
parent fdd4094d33
commit 9de35bfd43
2 changed files with 18 additions and 9 deletions

View File

@ -12,6 +12,7 @@ from vnpy.gateway.tiger import TigerGateway
from vnpy.gateway.oes import OesGateway from vnpy.gateway.oes import OesGateway
from vnpy.gateway.okex import OkexGateway 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.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
@ -34,6 +35,7 @@ def main():
main_engine.add_gateway(OesGateway) main_engine.add_gateway(OesGateway)
main_engine.add_gateway(OkexGateway) main_engine.add_gateway(OkexGateway)
main_engine.add_gateway(HuobiGateway) main_engine.add_gateway(HuobiGateway)
main_engine.add_gateway(BitfinexGateway)
main_engine.add_app(CtaStrategyApp) main_engine.add_app(CtaStrategyApp)
main_engine.add_app(CtaBacktesterApp) main_engine.add_app(CtaBacktesterApp)

View File

@ -50,11 +50,11 @@ ORDERTYPE_VT2BITFINEX = {
OrderType.MARKET: "EXCHANGE MARKET", OrderType.MARKET: "EXCHANGE MARKET",
} }
DIRECTION_VT2BITFINEX = { DIRECTION_VT2BITFINEX = {
Direction.LONG: "Buy", Direction.LONG: "Buy",
Direction.SHORT: "Sell", Direction.SHORT: "Sell",
} }
DIRECTION_BITFINEX2VT = { DIRECTION_BITFINEX2VT = {
"Buy": Direction.LONG, "Buy": Direction.LONG,
"Sell": Direction.SHORT, "Sell": Direction.SHORT,
} }
@ -204,7 +204,7 @@ class BitfinexRestApi(RestClient):
def on_query_contract(self, data, request): def on_query_contract(self, data, request):
"""""" """"""
for d in data: for d in data:
contract = ContractData( contract = ContractData(
symbol=d["pair"].upper(), symbol=d["pair"].upper(),
exchange=Exchange.BITFINEX, exchange=Exchange.BITFINEX,
@ -215,7 +215,7 @@ class BitfinexRestApi(RestClient):
gateway_name=self.gateway_name, gateway_name=self.gateway_name,
) )
self.gateway.on_contract(contract) self.gateway.on_contract(contract)
self.gateway.write_log("账户资金查询成功") self.gateway.write_log("账户资金查询成功")
def on_failed(self, status_code: int, request: Request): def on_failed(self, status_code: int, request: Request):
@ -357,7 +357,7 @@ class BitfinexWebsocketApi(WebsocketClient):
def on_packet(self, packet: dict): def on_packet(self, packet: dict):
"""""" """"""
if isinstance(packet, dict): if isinstance(packet, dict):
self.on_response(packet) self.on_response(packet)
else: else:
self.on_update(packet) self.on_update(packet)
@ -412,7 +412,7 @@ class BitfinexWebsocketApi(WebsocketClient):
tick.low_price = float(l_data1[-1]) tick.low_price = float(l_data1[-1])
tick.last_price = float(l_data1[-4]) tick.last_price = float(l_data1[-4])
tick.open_price = float(tick.last_price - l_data1[4]) tick.open_price = float(tick.last_price - l_data1[4])
# Update deep quote # Update deep quote
elif channel == "book": elif channel == "book":
bid = self.bidDict.setdefault(symbol, {}) bid = self.bidDict.setdefault(symbol, {})
@ -490,6 +490,7 @@ class BitfinexWebsocketApi(WebsocketClient):
def on_wallet(self, data): def on_wallet(self, data):
"""""" """"""
if str(data[0]) == "exchange": if str(data[0]) == "exchange":
print("wallet", data)
accountid = str(data[1]) accountid = str(data[1])
account = self.accounts.get(accountid, None) account = self.accounts.get(accountid, None)
if not account: if not account:
@ -602,7 +603,8 @@ class BitfinexWebsocketApi(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 = datetime.strptime(d["timestamp"], "%Y-%m-%dT%H:%M:%S.%fZ") tick.datetime = datetime.strptime(
d["timestamp"], "%Y-%m-%dT%H:%M:%S.%fZ")
self.gateway.on_tick(copy(tick)) self.gateway.on_tick(copy(tick))
def on_trade(self, data): def on_trade(self, data):
@ -619,7 +621,7 @@ class BitfinexWebsocketApi(WebsocketClient):
exchange=Exchange.BITFINEX, exchange=Exchange.BITFINEX,
orderid=data[-1], orderid=data[-1],
direction=direction, direction=direction,
volume=data[4], volume=abs(data[4]),
price=data[5], price=data[5],
tradeid=str(self.trade_id), tradeid=str(self.trade_id),
time=self.generate_date_time(data[2]), time=self.generate_date_time(data[2]),
@ -641,7 +643,12 @@ class BitfinexWebsocketApi(WebsocketClient):
data = d[4] data = d[4]
error_info = d[-1] error_info = d[-1]
# Filter cancel of non-existing order
orderid = str(data[2]) orderid = str(data[2])
if orderid == "None":
self.gateway.write_log("撤单失败,委托不存在")
return
if data[6] > 0: if data[6] > 0:
direction = Direction.LONG direction = Direction.LONG
else: else:
@ -666,7 +673,7 @@ class BitfinexWebsocketApi(WebsocketClient):
def on_order(self, data): def on_order(self, data):
"""""" """"""
orderid = str(data[2]) orderid = str(data[2])
if data[7] > 0: if data[7] > 0:
direction = Direction.LONG direction = Direction.LONG
else: else: