private async Task <ExternalTrade> ExecuteLimitOrderAsync(string assetPairId, decimal volume, decimal?price, Side side) { string instrument = await GetInstrumentAsync(assetPairId); var request = new RequestForQuoteRequest(instrument, side, volume); RequestForQuoteResponse response; Trade trade = await WrapAsync(async() => { _log.InfoWithDetails("Get quote request", request); response = await _client.RequestForQuoteAsync(request); _log.InfoWithDetails("Get quote response", response); if (price.HasValue) { if (side == Side.Sell && price.Value > response.Price) { throw new NotEnoughLiquidityException(); } if (side == Side.Buy && price.Value < response.Price) { throw new NotEnoughLiquidityException(); } } var tradeRequest = new TradeRequest(response); _log.InfoWithDetails("Execute trade request", tradeRequest); Trade tradeResponse = await _client.TradeAsync(tradeRequest); _log.InfoWithDetails("Execute trade response", tradeResponse); return(tradeResponse); }); return(new ExternalTrade { Id = trade.TradeId, LimitOrderId = trade.Order, AssetPairId = trade.Instrument, Type = trade.Side == Side.Sell ? TradeType.Sell : TradeType.Buy, Time = trade.Created, Price = trade.Price, Volume = trade.Quantity, RequestId = trade.RfqId }); }
//[Fact] public async void TradeTest() { var rfqRequest = new RequestForQuoteRequest("BTCUSD", Side.Buy, 1); var rfqResponse = await _restClient.RequestForQuoteAsync(rfqRequest); var result = await _restClient.TradeAsync(new TradeRequest(rfqResponse)); Assert.NotNull(result); Assert.NotEmpty(result.TradeId); Assert.Equal(result.RfqId, rfqResponse.Id); Assert.Equal(result.Instrument, rfqResponse.Instrument); Assert.Equal(result.Side, rfqResponse.Side); Assert.Equal(result.Price, rfqResponse.Price); Assert.Equal(result.Quantity, rfqResponse.Quantity); Assert.Null(result.Order); Assert.NotEqual(default(DateTime), result.Created); }