public static void CheckAndCancel99(OrderHandling orderHandling) { if (!orderHandling.OrderToCare.Done) { orderHandling.OrderToCare.Result = TradeUseCase.OrderDetails(orderHandling.OrderToCare.Result); } int useCase = 0; decimal difPrecentge = 0; if (orderHandling.OrderToCare.Result.Result == ExchangeAPIOrderResult.FilledPartially) { difPrecentge = orderHandling.OrderToCare.Result.AmountFilled / orderHandling.OrderToCare.Result.Amount; if (difPrecentge > 0.99m) { useCase = 4; // Current Order Cancellation TradeUseCase.Start(useCase, orderHandling); } } }
public static bool HandlingOneOrderTrade(OrderHandling orderHandling, bool firstOrder) { orderHandling.OrderToCare.StartTrade(); orderHandling.OrderToCare.Result = TradeUseCase.Order(orderHandling.OrderToCare.Request); string resPrint = String.Format("Handling Order - {0}-{1}, ExtraPercent {2:P0}, Percentage(without ExtraPercent) - {3:P3}, realPercentage - {4:P3}", (orderHandling.OrderToCare.request.IsBuy ? "Buy" :"Sell"), orderHandling.OrderToCare.request.Symbol, orderHandling.OrderToCare.extraPercent.Percent, orderHandling.percent / 100, orderHandling.realPercentage / 100); PrintTable.PrintConsole(resPrint); int useCase = 0; decimal difPrecentge = 0; System.Threading.Thread.Sleep(orderHandling.OrderToCare.ExtraPercent.WaitingTimeForNextPriceUpdate); orderHandling.OrderToCare.Result = TradeUseCase.OrderDetails(orderHandling.OrderToCare.Result); DateTime startTimeSmallAmount = DateTime.Now; while (!orderHandling.OrderToCare.Done) { if (orderHandling.OrderToCare.itsCanAdded & orderHandling.OrderToCare.itsCanUpdate) { startTimeSmallAmount = DateTime.Now; if (orderHandling.OrderToCare.Result.Result == ExchangeAPIOrderResult.FilledPartially) { difPrecentge = orderHandling.OrderToCare.Result.AmountFilled / orderHandling.OrderToCare.Result.Amount; if (difPrecentge > 0.99m) { useCase = 4; // Current Order Cancellation } else { useCase = 3; // We will also update the price in addition to the quantity update } } else // if (orderHandling.OrderToCare.Result.Result == ExchangeAPIOrderResult.Pending) { useCase = 1; // Update price } TradeUseCase.Start(useCase, orderHandling); } else if (orderHandling.OrderToCare.Result.Result == ExchangeAPIOrderResult.Filled) { return(true); } else if (orderHandling.OrderToCare.Result.Result == ExchangeAPIOrderResult.FilledPartially) { if (firstOrder) // We will cancel an order only in the case of a first order, because in other orders we will want the order to remain until it is executed { DateTime timeSmallAmount = DateTime.Now; if (!(timeSmallAmount.Subtract(startTimeSmallAmount).TotalMinutes > 1)) // Limit of waiting for order to 1 minute { if ((orderHandling.OrderToCare.Result.AmountFilled + orderHandling.OrderToCare.AmountFilledDifferentOrderNumber) < orderHandling.minAmountTrade) // try fix bug of buying a small amount of coins, In use case of the quantity is less than the minimum for the trade, we will continue to wait until at least the required minimum is filled { continue; } } else { useCase = 4; // Current Order Cancellation TradeUseCase.Start(useCase, orderHandling); if (orderHandling.OrderToCare.amountFilled < orderHandling.minAmountTrade) // try fix bug of buying a small amount of coins, In use case of the quantity is less than the minimum for the trade, we will continue to wait until at least the required minimum is filled { ExchangeOrderRequest revertOrder = new ExchangeOrderRequest(); try { revertOrder.Amount = orderHandling.OrderToCare.amountFilled; revertOrder.IsBuy = false; revertOrder.OrderType = StaticVariables.orderType; revertOrder.Price = orderHandling.OrderToCare.Request.Price * (1 + (StaticVariables.FeeTrade * 2)); revertOrder.Symbol = orderHandling.OrderToCare.Request.Symbol; OrderTrade revertOrderTrade = new OrderTrade(revertOrder); revertOrderTrade.Result = StaticVariables.api.PlaceOrder(revertOrder); PrintTable.Start(StaticVariables.pathWithDate + "Small_Amount_Handling.csv", orderHandling.OrderToCare.Result.PrintSymbol(), "OrderResultSymbol"); PrintTable.Start(StaticVariables.pathWithDate + "Small_Amount_Handling.csv", revertOrderTrade.Result.PrintSymbol(), "OrderResultSymbol"); } catch (Exception ex) { StaticVariables.Wallet = WalletFunc.GetWallet(); // Wallet update. Because part of the trade was carried out. Apparently the amounts of coins have changed string warningMessage = String.Format("{0},\tamountFilled - {1},\tAmount - {2},\tminAmountTrade - {3},", orderHandling.OrderToCare.request.Symbol, orderHandling.OrderToCare.amountFilled, orderHandling.OrderToCare.request.Amount, orderHandling.minAmountTrade); PrintTable.PrintConsole(warningMessage); PrintFunc.AddLine(StaticVariables.pathWithDate + "Small_Amount_Left.txt", warningMessage); PrintTable.Start(StaticVariables.pathWithDate + "Small_Amount_Left.csv", orderHandling.OrderToCare.request.Print(), "OrderResult"); DateTime localDate = DateTime.Now; string printResult = String.Format("{0}\n{1}", localDate.ToString(), ex.ToString()); PrintException.Start("Small_Amount_Left", printResult); } } return(orderHandling.OrderToCare.succsseFirstOrder); } } else { orderHandling.OrderToCare.ItsOrderLeft = true; return(false); } } else if (orderHandling.OrderToCare.Result.Result == ExchangeAPIOrderResult.Pending) { if (firstOrder) // We will cancel an order only in the case of a first order, because in other orders we will want the order to remain until it is executed { useCase = 4; // Current Order Cancellation TradeUseCase.Start(useCase, orderHandling); return(orderHandling.OrderToCare.succsseFirstOrder); } else { orderHandling.OrderToCare.ItsOrderLeft = true; return(false); } } if (!orderHandling.OrderToCare.Done) { System.Threading.Thread.Sleep(orderHandling.OrderToCare.ExtraPercent.WaitingTimeForNextPriceUpdate); orderHandling.OrderToCare.Result = TradeUseCase.OrderDetails(orderHandling.OrderToCare.Result); if (orderHandling.OrderToCare.Result.Result == ExchangeAPIOrderResult.Canceled) // In case a cancellation was made by the stock exchange due to an order or quantity error { bool checkCancel = CancellationFunc.ReviewCancellationAndUpdateOrder(orderHandling); if (firstOrder) { return(orderHandling.OrderToCare.succsseFirstOrder); } else { // TODO Check what amount has not traded. Maybe by the wallet. Update amount and send request in the current function } } } } if (firstOrder) { return(orderHandling.OrderToCare.succsseFirstOrder); // amountFilled > minAmaunt } else { return(orderHandling.OrderToCare.succsseTrade); // ((amountFilled == amountStart) || (amountFilled >= amountFinish)) } }