private async Task PlaceLimitOrdersTillFulfilled(List <string> soldAssetNames, List <string> boughtAssetNames) { Debug.WriteLine($"[{DateTime.UtcNow.ToString("dd-MM-yyyy HH:mm:ss")}]Cryptos revendues: " + string.Join(" / ", soldAssetNames)); Debug.WriteLine($"[{DateTime.UtcNow.ToString("dd-MM-yyyy HH:mm:ss")}]Cryptos achetées: " + string.Join(" / ", boughtAssetNames)); MinutesSinceLastPosition = 0; foreach (var asset in soldAssetNames.Concat(boughtAssetNames)) { if (OrdersToProcess.Any(o => o.Symbol.ToLower() == asset)) { continue; } bool isSold = soldAssetNames.Contains(asset); decimal price = 0.0m; if (!_parameters.DoPlaceMarketOrders) { price = isSold ? _binanceWsClient.UpdatedBookTickers[asset].BestAskPrice ?? 0.0m : _binanceWsClient.UpdatedBookTickers[asset].BestBidPrice ?? 0.0m; } decimal lastPrice = 0; lastPrice = (decimal)_binanceWsClient.WholeMarketKlines[asset].Last().Kline.ClosePrice; int minTakenRank = 1 + (int)Math.Floor(_parameters.MinTakenRank * (CurrRatios.Count(x => x.Value != null) - 1)); int maxTakenRank = 1 + (int)Math.Floor(_parameters.MaxTakenRank * (CurrRatios.Count(x => x.Value != null) - 1)); decimal qty = isSold ? CurrentHoldBaseAssetPos[asset].Qty : Math.Round(CurrentInvestedAmount / (lastPrice * (maxTakenRank - minTakenRank + 1)), ExchangeInfo.Symbols.FirstOrDefault(s => asset == $"{s.BaseAsset.ToLower()}{s.QuoteAsset.ToLower()}")?.QuantityPrecision ?? 1, MidpointRounding.ToZero); OrdersToProcess.Add(new BinanceBotOrder { //OrderId = Guid.NewGuid().ToString(), Price = price, Qty = qty, Side = (sbyte)(isSold ? -1 : 1), Symbol = asset, LastModifed = DateTime.UtcNow, IsClosingPosition = _parameters.DoPlaceMarketOrders && isSold }); if (isSold) { CurrentHoldBaseAssetPos[asset].IsBeingLiquidated = true; } if (_parameters.DoPlaceMarketOrders) { if (isSold) { CurrentHoldBaseAssetPos[asset] = new SingleAssetPosition { Qty = 0.0m, AvgEntryPrice = 0.0m, Side = 0, IsBeingLiquidated = true } } ; else { CurrentHoldBaseAssetPos[asset] = new SingleAssetPosition { Qty = qty, AvgEntryPrice = price, Side = 1, IsBeingLiquidated = false } }; } } Debug.WriteLine($"[{DateTime.UtcNow.ToString("dd - MM - yyyy hh: mm:ss")}] Avant envoi ordre: " + string.Join(" / ", OrdersToProcess.Select(o => $"{o.Symbol}: qty = " + (o.Side == -1 ? "-" : "") + $"{o.Qty} "))); List <BinanceFuturesPlacedOrderResponse> responses = new List <BinanceFuturesPlacedOrderResponse>(); #region Process orders till all filled if (_parameters.DoPlaceMarketOrders) { List <string> liquidatedSymbols = CurrentHoldBaseAssetPos.Where(kvp => kvp.Value.IsBeingLiquidated).Select(kvp => kvp.Key.ToLower()).ToList(); foreach (var symbol in liquidatedSymbols) { var cancelResponse = await _binanceWsClient.CancelAllOpenOrders(symbol); Debug.WriteLine($"[{DateTime.UtcNow.ToString("dd - MM - yyyy hh: mm:ss")}] Annulation de l'asset liquidé {symbol}: {JsonConvert.SerializeObject(cancelResponse, new JsonSerializerSettings { Formatting = Formatting.None })}"); //await Task.Delay(DELAY_BETWEEN_ORDERS_IN_MILLISECS); } var marketOrders = OrdersToProcess.Select(o => new BinanceFuturesPlacedOrder { Symbol = o.Symbol, Side = o.Side == 1 ? "BUY" : "SELL", Quantity = o.Qty, Type = "MARKET" }); responses = await _binanceWsClient.PlaceBatchOrder(marketOrders.ToList()); OrdersToProcess.Clear(); } else { responses = await _binanceWsClient.PlaceBatchOrder(OrdersToProcess.Select(o => new BinanceFuturesPlacedOrder { Symbol = o.Symbol, Side = o.Side == 1 ? "BUY" : "SELL", Price = o.Price, Quantity = o.Qty, //NewClientOrderId = o.OrderId, TimeInForce = "GTX", Type = "LIMIT" }) .ToList()); } #endregion foreach (var response in responses) { if (response.IsSuccess && response.Code == null) { Debug.WriteLine($"[{DateTime.UtcNow.ToString("dd-MM-yyyy HH:mm:ss")}]Vente/achat envoyé: {JsonConvert.SerializeObject(response, new JsonSerializerSettings { Formatting = Formatting.None })}"); if (!_parameters.DoPlaceMarketOrders) { var order = OrdersToProcess.FirstOrDefault(o => o.Symbol.ToLower() == response.Symbol.ToLower()); order.OrderId = response.OrderId; order.LastModifed = DateTime.UtcNow; } } else { Debug.WriteLine($"[{DateTime.UtcNow.ToString("dd-MM-yyyy HH:mm:ss")}]Vente/achat non-envoyé: {JsonConvert.SerializeObject(response, new JsonSerializerSettings { Formatting = Formatting.None })}"); } } if (_parameters.DoPlaceMarketOrders) { foreach (var symbol in CurrentHoldBaseAssetPos.Keys) { CurrentHoldBaseAssetPos[symbol].IsBeingLiquidated = false; } } }