From fa6c9e9975af736f9be269f44a8fbeae3d60a6ed Mon Sep 17 00:00:00 2001 From: "vn.py" Date: Mon, 2 Sep 2019 16:44:48 +0800 Subject: [PATCH] [Mod] complete function test of da gateway --- vnpy/api/da/vndafuture.pyd | Bin 893440 -> 893440 bytes vnpy/gateway/da/da_gateway.py | 458 +++++++++++++++++----------------- 2 files changed, 231 insertions(+), 227 deletions(-) diff --git a/vnpy/api/da/vndafuture.pyd b/vnpy/api/da/vndafuture.pyd index 0bae339f131dbee2ddae467284044459280b9b0f..285eee8732e63dc65852cc080b030c7344d81bfb 100644 GIT binary patch delta 153 zcmZoTV%BiPY=Z$KbH=rtW@E;7V@42W0%B$$W&vVWAZ7z%b|B^eVoo6D+HTCqJu%Q4 zs412K2$+D_35ZkXb2Fs0a)X2zU~)V_wg?cf0LraE$W8wr#4QQ3b-Q#hcdQX3S9@hE OHxTn|uWaRQmjM6+ 0: + order.status = Status.PARTTRADED + else: + order.status = Status.NOTTRADED + + self.gateway.on_order(copy(order)) + + def onRtnTrade(self, data: dict, error: dict, reqid: int, last: bool): """ Callback of trade status update. """ - symbol = data["InstrumentID"] - exchange = symbol_exchange_map.get(symbol, "") - if not exchange: - self.trade_data.append(data) - return + self.update_trade(data) + + def onRtnCapital(self, data: dict, error: dict, reqid: int, last: bool): + """ + Callback of capital status update. + """ + currency = account_currency_map[data["AccountNo"]] - orderid = self.sysid_orderid_map[data["OrderSysID"]] - - trade = TradeData( - symbol=symbol, - exchange=exchange, - orderid=orderid, - tradeid=data["TradeID"], - direction=DIRECTION_DA2VT[data["Direction"]], - offset=OFFSET_DA2VT[data["OffsetFlag"]], - price=data["Price"], - volume=data["Volume"], - time=data["TradeTime"], + account = AccountData( + accountid=currency, + balance=data["TodayTotal"], + frozen=data["FrozenDeposit"], gateway_name=self.gateway_name ) - self.gateway.on_trade(trade) - def update_trade(self, data: dict): - """""" - pass + self.gateway.on_account(account) - def update_account(self, data: dict): - """""" - pass + def onRtnPosition(self, data: dict, error: dict, reqid: int, last: bool): + """ + Callback of position status update. + """ + long_position = PositionData( + symbol=data["TreatyCode"], + exchange=EXCHANGE_DA2VT[data["ExchangeNo"]], + direction=Direction.LONG, + volume=data["BuyHoldNumber"], + price=data["BuyHoldOpenPrice"], + gateway_name=self.gateway_name + ) + self.gateway.on_position(long_position) - def update_position(self, data: dict): - """""" - pass + short_position = PositionData( + symbol=data["TreatyCode"], + exchange=EXCHANGE_DA2VT[data["ExchangeNo"]], + direction=Direction.SHORT, + volume=data["SaleHoldNumber"], + price=data["SaleHoldOpenPrice"], + gateway_name=self.gateway_name + ) + self.gateway.on_position(short_position) def connect( self, @@ -689,50 +681,36 @@ class DaFutureApi(FutureApi): } self.reqid += 1 - n = self.reqUserLogin(req, self.reqid) - print("login", n) + self.reqUserLogin(req, self.reqid) def send_order(self, req: OrderRequest): """ Send new order. """ - self.order_ref += 1 + self.local_no += 1 + currency = symbol_currency_map[req.symbol] + account_no = currency_account_map[currency] + da_req = { - "InstrumentID": req.symbol, - "ExchangeID": req.exchange.value, - "LimitPrice": req.price, - "VolumeTotalOriginal": int(req.volume), - "OrderPriceType": ORDERTYPE_VT2DA.get(req.type, ""), - "Direction": DIRECTION_VT2DA.get(req.direction, ""), - "CombOffsetFlag": OFFSET_VT2DA.get(req.offset, ""), - "OrderRef": str(self.order_ref), - "InvestorID": self.userid, - "UserID": self.userid, - "BrokerID": self.brokerid, - "CombHedgeFlag": THOST_FTDC_HF_Speculation, - "ContingentCondition": THOST_FTDC_CC_Immediately, - "ForceCloseReason": THOST_FTDC_FCC_NotForceClose, - "IsAutoSuspend": 0, - "TimeCondition": THOST_FTDC_TC_GFD, - "VolumeCondition": THOST_FTDC_VC_AV, - "MinVolume": 1 + "UserId": self.userid, + "AccountNo": account_no, + "LocalNo": str(self.local_no), + "TradePwd": self.password, + "ExchangeCode": EXCHANGE_VT2DA[req.exchange], + "TreatyCode": req.symbol, + "BuySale": DIRECTION_VT2DA[req.direction], + "OrderPrice": str(req.price), + "OrderNumber": str(int(req.volume)), + "PriceType": ORDERTYPE_VT2DA[req.type] } - if req.type == OrderType.FAK: - da_req["OrderPriceType"] = THOST_FTDC_OPT_LimitPrice - da_req["TimeCondition"] = THOST_FTDC_TC_IOC - da_req["VolumeCondition"] = THOST_FTDC_VC_AV - elif req.type == OrderType.FOK: - da_req["OrderPriceType"] = THOST_FTDC_OPT_LimitPrice - da_req["TimeCondition"] = THOST_FTDC_TC_IOC - da_req["VolumeCondition"] = THOST_FTDC_VC_CV - self.reqid += 1 self.reqOrderInsert(da_req, self.reqid) - orderid = f"{self.frontid}_{self.sessionid}_{self.order_ref}" - order = req.create_order_data(orderid, self.gateway_name) + order = req.create_order_data(str(self.local_no), self.gateway_name) + + self.orders[order.orderid] = order self.gateway.on_order(order) return order.vt_orderid @@ -741,21 +719,26 @@ class DaFutureApi(FutureApi): """ Cancel existing order. """ - frontid, sessionid, order_ref = req.orderid.split("_") + order = self.orders[req.orderid] + + currency = symbol_currency_map[req.symbol] + account_no = currency_account_map[currency] + order_no, system_no = self.order_info[order.orderid] da_req = { - "InstrumentID": req.symbol, - "ExchangeID": req.exchange.value, - "OrderRef": order_ref, - "FrontID": int(frontid), - "SessionID": int(sessionid), - "ActionFlag": THOST_FTDC_AF_Delete, - "BrokerID": self.brokerid, - "InvestorID": self.userid + "UserId": self.userid, + "LocalNo": req.orderid, + "AccountNo": account_no, + "TradePwd": self.password, + "ExchangeCode": EXCHANGE_VT2DA[req.exchange], + "TreatyCode": req.symbol, + "BuySale": DIRECTION_VT2DA[order.direction], + "OrderNo": order_no, + "SystemNo": system_no } self.reqid += 1 - self.reqOrderAction(da_req, self.reqid) + self.reqOrderCancel(da_req, self.reqid) def query_account(self): """ @@ -775,15 +758,19 @@ class DaFutureApi(FutureApi): """ Query account balance data. """ + da_req = {"UserId": self.userid} + self.reqid += 1 - self.reqQryTrade({}, self.reqid) + self.reqQryTrade(da_req, self.reqid) def query_position(self): """ Query position holding data. """ + da_req = {"AccountNo": self.userid} + self.reqid += 1 - self.reqQryPosition({}, self.reqid) + self.reqQryTotalPosition(da_req, self.reqid) def query_contract(self, exchange, page=1): """ @@ -827,4 +814,21 @@ def get_mac_address(): if not interface: return "" - return interface.MACAddress \ No newline at end of file + return interface.MACAddress + + +def to_int(data: str) -> int: + """""" + if not data: + return 0 + else: + return int(data) + + +def to_float(data: str) -> float: + """""" + if not data: + return 0.0 + else: + return float(data) + \ No newline at end of file