private void EnableKlineStreams() { var successKlineStream = Socket.SubscribeToKlineStream( Configuration.Instance.EnabledAlgorithm.AlgorithmConfiguration.TradingPairs.First().ToString(), BinanceUtilities.ToInternalKline(Configuration.Instance.CandleWidth), candle => { if (candle.Data.Final) { _candleDispenserImplementation.Publish(BinanceUtilities.ToInternal(candle)); } }); if (!successKlineStream.Success) { _logger.LogError(successKlineStream.Error.Message); Program.ExitProgramWithCode(ExitCode.BinanceCommunicationStartupFailure); } successKlineStream.Data.ConnectionLost += () => { _logger.LogCritical($"Kline stream got closed at {DateTime.UtcNow}, attempting reconnect..."); }; successKlineStream.Data.ConnectionRestored += t => { _logger.LogCritical($"Kline stream was restored after {t}"); }; }
/// <inheritdoc /> public override ResponseObject <OrderUpdate> PlaceLimitOrder(TradingPair pair, OrderSide side, decimal quantity, decimal price, long tradeId) { var client = _communications.Client; var realQuantity = pair.RoundToTradable(quantity); var realPrice = pair.RoundToPriceable(price); var query = client.PlaceOrder( symbol: pair.ToString(), side: BinanceUtilities.ToExternal(side), type: OrderType.Limit, quantity: realQuantity, newClientOrderId: null, price: realPrice, timeInForce: TimeInForce.GoodTillCancel, stopPrice: null, icebergQty: null, orderResponseType: null, receiveWindow: (int)_communications.ReceiveWindow); return(query.Success ? new ResponseObject <OrderUpdate>( ResponseCode.Success, new OrderUpdate( query.Data.OrderId, tradeId, OrderUpdate.OrderStatus.New, OrderUpdate.OrderTypes.Limit, DateTimeOffset.Now.ToUnixTimeMilliseconds(), realPrice, side, pair, realQuantity)) : ResponseObject.OrderPlacementFailed(BinanceUtilities.ToInternalError(query.Error.Code), query.Error.Message)); }
private void EnableOrderStreams() { // Obtain listenKey var response = _listenKeyManager.Obtain(); if (!response.Success) { _logger.LogError("Unable to obtain listenKey"); Program.ExitProgramWithCode(ExitCode.BinanceCommunicationStartupFailure); } var listenKey = response.Data; // Start socket connection // TODO: Is this correct? // TODO: TradeId is not correct. var successOrderBook = Socket.SubscribeToUserStream( listenKey, accountInfoUpdate => { // TODO: Implement AccountInfoUpdate callback }, orderInfoUpdate => { // ########################################################################## // ####### WARNING ########################################################## // ### Any exception will cause this method to shutdown without warning, // ### causing the observers to hear nothing. This is completely shitty behavior, // ### do not make the mistake I made and waste your time. // ########################################################################## try { _logger.LogDebug(JsonConvert.SerializeObject(orderInfoUpdate)); OrderUpdateDispenserImplementation.Publish(BinanceUtilities.ToInternal(orderInfoUpdate)); } catch (Exception e) { _logger.LogError($"Error parsing a BinanceOrderInfoUpdate: {e.Message} \n {JsonConvert.SerializeObject(orderInfoUpdate)}"); } }); if (!successOrderBook.Success) { _logger.LogError(successOrderBook.Error.Message); Program.ExitProgramWithCode(ExitCode.BinanceCommunicationStartupFailure); } // Set error handler successOrderBook.Data.ConnectionLost += () => { _logger.LogCritical($"Order stream got closed at {DateTime.UtcNow}, attempting reconnect..."); }; successOrderBook.Data.ConnectionRestored += t => _logger.LogCritical($"Order stream was restored after {t}"); }
/// <inheritdoc /> public override ResponseObject CancelOrder(TradingPair pair, long orderId) { var client = _communications.Client; var query = client.CancelOrder( symbol: pair.ToString(), orderId: orderId, origClientOrderId: null, newClientOrderId: null, receiveWindow: _communications.ReceiveWindow); return(query.Success ? new ResponseObject(ResponseCode.Success) : new ResponseObject(BinanceUtilities.ToInternalError(query.Error.Code), query.Error.Message)); }
/// <inheritdoc /> public override ResponseObject <OrderUpdate> ExecuteMarketOrder(TradingPair pair, OrderSide side, decimal quantity, long tradeId) { var client = _communications.Client; var realQuantity = pair.RoundToTradable(quantity); // Attempt to place the order on Binance var query = client.PlaceOrder( symbol: pair.ToString(), side: BinanceUtilities.ToExternal(side), type: OrderType.Market, quantity: realQuantity, newClientOrderId: null, price: null, timeInForce: null, stopPrice: null, icebergQty: null, orderResponseType: null, (int)_communications.ReceiveWindow); // Report failure of placing market order if (!query.Success) { Logger.LogError($"Placing market order {side} {realQuantity} {pair.Left} failed! --> {query.Error.Message}"); return(new ResponseObject <OrderUpdate>(ResponseCode.Error, query.Error.Message)); } var order = query.Data; // Create an order update with known information OrderUpdate result = new OrderUpdate( orderId: order.OrderId, tradeId: tradeId, orderStatus: OrderUpdate.OrderStatus.Filled, orderType: BinanceUtilities.ToInternal(order.Type), createdTimestamp: DateTimeOffset.Now.ToUnixTimeMilliseconds(), setPrice: 0, // This information is unknown for market orders side: side, pair: pair, setQuantity: realQuantity) { FilledQuantity = order.ExecutedQuantity, FilledTimestamp = DateTimeOffset.Now.ToUnixTimeMilliseconds(), AverageFilledPrice = HelperMethods.SafeDiv(order.CummulativeQuoteQuantity, order.ExecutedQuantity), }; return(new ResponseObject <OrderUpdate>(ResponseCode.Success, result)); }
/// <inheritdoc /> public override ResponseObject <OrderUpdate> PlaceStoplossOrder(TradingPair pair, OrderSide side, decimal quantity, decimal price, long tradeId) { var client = _communications.Client; decimal limitPrice; if (side == OrderSide.Sell) { // Set the limit price extremely low -> sell immediately for the best price. // 5% is an arbitrary number that is probably more than the spread, but is not // rejected by Binance for deviating too much from the current price. limitPrice = price * 0.95M; } else { // Skew the quantity and the price -> buy immediately for the best price. // Quantity must scale inverse because (quantity * price) is the amount that needs to // be locked. You cannot lock more assets than you have. // 2% is hardcoded on purpose because it is unlikely to change. limitPrice = price * 1.02M; quantity /= 1.02M; } var realQuantity = pair.RoundToTradable(quantity); var realLimitPrice = pair.RoundToPriceable(limitPrice); var realStopPrice = pair.RoundToPriceable(price); lock (_orderCache) { var query = client.PlaceOrder( symbol: pair.ToString(), side: BinanceUtilities.ToExternal(side), type: OrderType.StopLossLimit, quantity: realQuantity, newClientOrderId: null, price: realLimitPrice, timeInForce: TimeInForce.GoodTillCancel, stopPrice: realStopPrice, icebergQty: null, orderResponseType: null, receiveWindow: (int)_communications.ReceiveWindow); if (query.Success) { var order = new OrderUpdate( query.Data.OrderId, tradeId, OrderUpdate.OrderStatus.New, OrderUpdate.OrderTypes.StopLoss, DateTimeOffset.Now.ToUnixTimeMilliseconds(), realLimitPrice, side, pair, realQuantity) { StopPrice = realStopPrice, }; // Enter middleware instance to make sure this order is // also converted to a stoploss order when the exchange reports updates. _transformMiddleWare.Add(order.OrderId, x => x.OrderType = OrderUpdate.OrderTypes.StopLoss); return(new ResponseObject <OrderUpdate>(order)); } return(ResponseObject.OrderPlacementFailed(BinanceUtilities.ToInternalError(query.Error.Code), query.Error.Message)); } }