Ejemplo n.º 1
0
 public BitMexPriceHarvester(
     BitMexExchangeConfiguration configuration,
     IBitmexSocketSubscriber socketSubscriber,
     ILog log, IHandler <TickPrice> tickPriceHandler)
 {
     _socketSubscriber = socketSubscriber;
     _log = log;
     _tickPriceHandler = tickPriceHandler;
     _mapper           = new BitMexModelConverter(configuration.SupportedCurrencySymbols);
 }
 public BitMexOrderHarvester(
     BitMexExchangeConfiguration configuration,
     IBitmexSocketSubscriber socketSubscriber,
     IHandler <ExecutionReport> tradeHandler,
     ILog log)
 {
     _socketSubscriber = socketSubscriber;
     _log          = log;
     _tradeHandler = tradeHandler;
     _mapper       = new BitMexModelConverter(configuration.SupportedCurrencySymbols);
 }
Ejemplo n.º 3
0
        public override async Task <ExecutionReport> GetOrder(string id, Instrument instrument, TimeSpan timeout)
        {
            var filterObj = new { orderID = id };
            var filterArg = JsonConvert.SerializeObject(filterObj);
            var cts       = new CancellationTokenSource(timeout);
            var response  = await _exchangeApi.OrdergetOrdersAsync(filter : filterArg, cancellationToken : cts.Token);

            var res = EnsureCorrectResponse(id, response);

            return(BitMexModelConverter.OrderToTrade(res[0]));
        }
Ejemplo n.º 4
0
        public override async Task <IReadOnlyCollection <TradeBalanceModel> > GetTradeBalances(TimeSpan timeout)
        {
            var cts      = new CancellationTokenSource(timeout);
            var response = await _exchangeApi.UsergetMarginWithHttpMessagesAsync(cancellationToken : cts.Token);

            var bitmexMargin = response.Body;

            var model = BitMexModelConverter.ExchangeBalanceToModel(bitmexMargin);

            return(new[] { model });
        }
Ejemplo n.º 5
0
        public override async Task <ExecutionReport> CancelOrderAndWaitExecution(TradingSignal signal, TranslatedSignalTableEntity translatedSignal, TimeSpan timeout)
        {
            var ct       = new CancellationTokenSource(timeout);
            var id       = signal.OrderId;
            var response = await _exchangeApi.OrdercancelAsync(cancellationToken : ct.Token, orderID : id);

            if (response is Error error)
            {
                throw new ApiException(error.ErrorProperty.Message);
            }
            var res = EnsureCorrectResponse(id, response);

            return(BitMexModelConverter.OrderToTrade(res[0]));
        }
Ejemplo n.º 6
0
        public override async Task <ExecutionReport> AddOrderAndWaitExecution(TradingSignal signal, TranslatedSignalTableEntity translatedSignal, TimeSpan timeout)
        {
            var symbol    = "XBTUSD"; //HACK Hard code!
            var volume    = BitMexModelConverter.ConvertVolume(signal.Volume);
            var orderType = BitMexModelConverter.ConvertOrderType(signal.OrderType);
            var side      = BitMexModelConverter.ConvertTradeType(signal.TradeType);
            var price     = (double?)signal.Price;
            var ct        = new CancellationTokenSource(timeout);

            var response = await _exchangeApi.OrdernewAsync(symbol, orderQty : volume, price : price, clOrdID : signal.OrderId, ordType : orderType, side : side, cancellationToken : ct.Token);

            if (response is Error error)
            {
                throw new ApiException(error.ErrorProperty.Message);
            }

            var order = (Order)response;

            return(BitMexModelConverter.OrderToTrade(order));
        }
Ejemplo n.º 7
0
        private async Task HandleResponseAsync(TableResponse table)
        {
            if (_tradeHandler == null)
            {
                throw new InvalidOperationException("Acknowledgement handler or executed trader is not set.");
            }

            if (!ValidateOrder(table))
            {
                await _log.WriteWarningAsync(nameof(BitMexOrderHarvester), nameof(HandleResponseAsync),
                                             $"Ignoring invalid 'order' message: '{JsonConvert.SerializeObject(table)}'");

                return;
            }

            switch (table.Action)
            {
            case Action.Insert:
            case Action.Update:
                foreach (var row in table.Data)
                {
                    var trade = BitMexModelConverter.OrderToTrade(row);

                    if (trade.ExecutionStatus == OrderExecutionStatus.Unknown)
                    {
                        await _log.WriteWarningAsync(nameof(BitMexOrderHarvester), nameof(HandleResponseAsync),
                                                     $"Can't convert trade status {row.OrdStatus} into ExecutionStatus. Converted item: {trade}. Don't call handlers.");
                    }
                    else
                    {
                        await _tradeHandler.Handle(trade);
                    }
                }
                break;
            }
        }