///Review the cancellation results and update the order accordingly public static bool ReviewCancellationAndUpdateOrder(OrderHandling orderHandling) { bool result = true; try { if (orderHandling.OrderToCare.Cancel.Success == ExchangeAPIOrderResult.Canceled) // Ostensibly this situation does not belong only in the case of cancellation due to non-execution of the order at all (and not in the case of cancellation due to incomplete filling) because this only happens in details.AmountFilled == 0 { result = true; } else if (orderHandling.OrderToCare.Cancel.Success == ExchangeAPIOrderResult.Filled) { result = false; } else if (orderHandling.OrderToCare.Cancel.Success == ExchangeAPIOrderResult.FilledPartially) { result = false; // We will continue on the other side for traded quantity } } catch (Exception ex) { DateTime localDate = DateTime.Now; string printResult = String.Format("{0}\n{1}", localDate.ToString(), ex.ToString()); printResult += String.Format("\nCan not get details about cancellation. Apparently he did not have to check why"); printResult += String.Format("\nsymbole - {0}, itsBuy - {1}", orderHandling.OrderToCare.request.Symbol, orderHandling.OrderToCare.request.IsBuy); PrintException.Start(MethodBase.GetCurrentMethod().Name, printResult); } orderHandling.OrderToCare.Result = orderHandling.OrderToCare.Cancel.Details; // For a goal that will receive its own value and update the value of the variable Done & SuccsseExtraPercent & amountFilled += result.AmountFilled; return(result); }
public static void Cancellation99(OrderHandling orderHandling) { if (orderHandling.Done) { return; } orderHandling.OrderToCare = orderHandling.Buy; CheckAndCancel99(orderHandling); orderHandling.OrderToCare = orderHandling.Sell; CheckAndCancel99(orderHandling); orderHandling.OrderToCare = orderHandling.Arbitrage; CheckAndCancel99(orderHandling); orderHandling.OrderToCare = orderHandling.Buy; // Only to be the result of OrderHandling.Done }
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 Start(OrderHandling orderHandling) { bool result = false; bool orderIsLeft = false; decimal cutAmountFee = 1; orderHandling.StartOrderHandling(); /// buy orderHandling.OrderToCare = orderHandling.Buy; if (!HandlingOneOrderTrade(orderHandling, true)) { return(false); } if (orderHandling.OrderToCare.amountFilled > orderHandling.AmountTrade) { string warningMessage = String.Format("orderHandling.OrderToCare.amountFilled - {0}, orderHandling.AmountTrade - {1},", orderHandling.OrderToCare.amountFilled, orderHandling.AmountTrade); PrintTable.PrintConsole(warningMessage); PrintFunc.AddLine(StaticVariables.pathWithDate + "WARNING_amountFilled Greater than AmountTrade.txt", warningMessage); PrintTable.Start(StaticVariables.pathWithDate + "WARNING_amountFilledOrderHandlingData.csv", orderHandling.PrintResult_2(), "OrderHandling"); } else if (orderHandling.OrderToCare.amountFilled <= orderHandling.AmountTrade) { if (StaticVariables.CurrencyTradingFeeReduction) { cutAmountFee = StaticVariables.api.FeeTrade(orderHandling.OrderToCare.originalMaxOrMinPrice == orderHandling.OrderToCare.request.Price); orderHandling.AmountTrade = orderHandling.OrderToCare.amountFilled * cutAmountFee; } else { orderHandling.AmountTrade = orderHandling.OrderToCare.amountFilled; } orderHandling.Sell.Request.Amount = orderHandling.AmountTrade; if (orderHandling.itsBuyArbitrage) { orderHandling.Arbitrage.Request.Amount = orderHandling.AmountTrade * orderHandling.Buy.request.Price; } else { orderHandling.Arbitrage.Request.Amount = orderHandling.AmountTrade * orderHandling.Sell.request.Price; } if (orderHandling.Arbitrage.request.Amount < orderHandling.arbitrageSymbolsDate.MinAmount) { orderHandling.Arbitrage.request.Amount = orderHandling.arbitrageSymbolsDate.MinAmount; } } if (orderHandling.AmountTrade < orderHandling.minAmountTrade) { return(false); } // sell orderHandling.percent_2 = orderHandling.revnuCalculation(orderHandling.percent_1); orderHandling.ChangeMaxOrMinPriceSell((StaticVariables.revnuTrade / 100)); orderHandling.OrderToCare = orderHandling.Sell; orderHandling.OrderToCare.UpExtraPercent(); if (!HandlingOneOrderTrade(orderHandling, false)) { orderIsLeft = true; orderHandling.buySymbolsDate.magicNumber.Buy.WaitingTimeForNextPriceUpdate -= 300; // USE to ML_4 orderHandling.sellSymbolsDate.magicNumber.Sell.WaitingTimeForNextPriceUpdate -= 300; // USE to ML_4 } result = true; #if DEBUG orderHandling.debug += String.Format("\n\n{0},cutAmountFee,\n{1},Buy.extraPercent.Percent,\n{2},AmountTrade,\n{3},Sell.request.Amount,", cutAmountFee, orderHandling.Buy.extraPercent.Percent, orderHandling.AmountTrade, orderHandling.OrderToCare.request.Amount); PrintFunc.AddLine(StaticVariables.pathDebug + orderHandling.currency + "/sell_" + orderHandling.currency + ".csv", orderHandling.debug); #endif if (StaticVariables.CurrencyTradingFeeReduction) { cutAmountFee = StaticVariables.api.FeeTrade(orderHandling.OrderToCare.originalMaxOrMinPrice == orderHandling.OrderToCare.request.Price); orderHandling.AmountTrade = orderHandling.AmountTrade * cutAmountFee; } if (orderHandling.itsBuyArbitrage) { orderHandling.Arbitrage.Request.Amount = orderHandling.AmountTrade * orderHandling.Buy.request.Price; } else { orderHandling.Arbitrage.Request.Amount = orderHandling.AmountTrade * orderHandling.Sell.request.Price; } if (orderHandling.Arbitrage.request.Amount < orderHandling.arbitrageSymbolsDate.MinAmount) { orderHandling.Arbitrage.request.Amount = orderHandling.arbitrageSymbolsDate.MinAmount; } // arbitrage orderHandling.percent_3 = orderHandling.revnuCalculation(orderHandling.percent_2); orderHandling.ChangeMaxOrMinPriceArbitrage((StaticVariables.revnuTrade / 100)); orderHandling.OrderToCare = orderHandling.Arbitrage; orderHandling.OrderToCare.UpExtraPercent(); try { if (!HandlingOneOrderTrade(orderHandling, false)) { orderIsLeft = true; if (orderHandling.itsBuyArbitrage) { orderHandling.arbitrageSymbolsDate.magicNumber.Buy.WaitingTimeForNextPriceUpdate -= 300; // USE to ML_4 } else { orderHandling.arbitrageSymbolsDate.magicNumber.Sell.WaitingTimeForNextPriceUpdate -= 300; // USE to ML_4 } } CancellationFunc.Cancellation99(orderHandling); } catch (Exception ex) { DateTime localDate = DateTime.Now; string printResult = String.Format("{0}\n{1}", localDate.ToString(), ex.ToString()); string fileName = String.Format("ExceptionArbitrage_{0}_{1}", (orderHandling.OrderToCare.request.IsBuy ? "Buy" : "Sell"), orderHandling.OrderToCare.request.Symbol); PrintException.Start(fileName, printResult); // TODU Send to a function to check if step b (sell) has been performed. And resubmit arbitrage order. The function should use a database to restore if the boot is shut down and restarted } #if DEBUG orderHandling.debug += String.Format("\n\n{0},cutAmountFee,\n{1},Sell.extraPercent.Percent,\n{2},AmountTrade,\n{3},Sell.request.Amount,", cutAmountFee, orderHandling.Sell.extraPercent.Percent, orderHandling.AmountTrade, orderHandling.OrderToCare.request.Amount); PrintFunc.AddLine(StaticVariables.pathDebug + orderHandling.currency + "/Arbitrage_" + orderHandling.currency + ".csv", orderHandling.debug); #endif // TODO Add a unique id variable to OrderHandling and send to a function that handles incomplete orders (orderLeft) if (!orderIsLeft) { PrintTable.PrintConsole("yes"); orderHandling.buySymbolsDate.magicNumber.Buy.WaitingTimeForNextPriceUpdate += 500; // USE to ML_4 orderHandling.sellSymbolsDate.magicNumber.Sell.WaitingTimeForNextPriceUpdate += 500; // USE to ML_4 if (orderHandling.itsBuyArbitrage) { orderHandling.arbitrageSymbolsDate.magicNumber.Buy.WaitingTimeForNextPriceUpdate += 500; // USE to ML_4 } else { orderHandling.arbitrageSymbolsDate.magicNumber.Sell.WaitingTimeForNextPriceUpdate += 500; // USE to ML_4 } } orderHandling.percent_end = orderHandling.revnuCalculation(orderHandling.percent_3); orderHandling.WalletResultEnd(); PrintTable.Start(StaticVariables.pathWithDate + "WalletResultReal.csv", orderHandling.summaryTradeReal, "WalletResultReal"); PrintTable.Start(StaticVariables.pathWithDate + "WalletResult.csv", orderHandling.summaryTrade, "WalletResult"); PrintFunc.AddLine(StaticVariables.pathWithDate + "main.txt", orderHandling.PrintResult()); PrintTable.Start(StaticVariables.pathWithDate + "OrderHandlingData.csv", orderHandling.PrintResult_2(), "OrderHandling"); #if DEBUG PrintFunc.AddLine(StaticVariables.pathDebug + orderHandling.currency + "/end_" + orderHandling.currency + ".csv", orderHandling.debug); #endif result = orderHandling.succsseTrade; return(result); }
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)) } }
public static OrderHandling ArbitragePercent(string currency, List <SymbolsDate> list) { #if DEBUG DateTime timeOrder = DateTime.Now; FindDebug = String.Format("{0}\n", timeOrder); #endif OrderHandling package = null; int sumListToCheck = ListToCheck(list); if (sumListToCheck < 2) { return(package); } Dictionary <string, decimal> BuyPriceList = new Dictionary <string, decimal>(); Dictionary <string, decimal> SellPriceList = new Dictionary <string, decimal>(); ExchangeOrderBook book; string symbole; decimal priceBuy; decimal priceSell; for (int i = 0; i < list.Count; i++) { if (!list[i].itsAvalible) { continue; } symbole = list[i].symbole; book = StaticVariables.api.GetOrderBook(symbole, StaticVariables.maxCount); priceBuy = GetPrice(book, list[i], true); if (priceBuy != 0) { BuyPriceList.Add(symbole, priceBuy); } priceSell = GetPrice(book, list[i], false); if (priceSell != 0) { SellPriceList.Add(symbole, priceSell); } } string BuyKey = ""; string SellKey = ""; decimal BuyPrice = 0; decimal SellPrice = 0; while (BuyKey == SellKey) { // Get the lowest price. So sorting Descending BuyKey = BuyPriceList.OrderByDescending(x => x.Value).Select(y => y.Key).ToList().Last(); BuyPrice = BuyPriceList[BuyKey]; // Getting the highest price. That's why sorting is normal SellKey = SellPriceList.OrderBy(x => x.Value).Select(y => y.Key).ToList().Last(); SellPrice = SellPriceList[SellKey]; #if DEBUG FindDebug += String.Format("BuyPriceList\n{0}\n", PrintFunc.PrintDictionary(BuyPriceList)); FindDebug += String.Format("SellPriceList\n{0}\n", PrintFunc.PrintDictionary(SellPriceList)); FindDebug += String.Format("BuyKey - {0}\tBuyPrice - {1}\n", BuyKey, BuyPrice); FindDebug += String.Format("SellKey - {0}\tSellPrice - {1}\n", SellKey, SellPrice); #endif if (BuyKey != SellKey) { break; } // Handling in case of buying and selling from the same currency int indexBuyPriceList = BuyPriceList.Count; int indexSellPriceList = SellPriceList.Count; if (indexBuyPriceList < 2 || indexSellPriceList < 2) { return(package); } // indexBuyPriceList-2 -> This is the index of the next proposal decimal BuyNextOffer = BuyPriceList.ElementAt(indexBuyPriceList - 2).Value; decimal BuyNextOfferDifrent = BuyNextOffer - BuyPrice; decimal SellNextOffer = SellPriceList.ElementAt(indexSellPriceList - 2).Value; decimal SellNextOfferDifrent = SellPrice - SellNextOffer; if (BuyNextOfferDifrent > SellNextOfferDifrent) { SellPriceList.Remove(SellKey); } else { BuyPriceList.Remove(BuyKey); } #if DEBUG FindDebug += String.Format("BuyNextOffer - {0}\tBuyNextOfferDifrent - {1}\n", BuyNextOffer, BuyNextOfferDifrent); FindDebug += String.Format("SellNextOffer - {0}\tSellNextOfferDifrent - {1}\n", SellNextOffer, SellNextOfferDifrent); FindDebug += String.Format("(BuyNextOfferDifrent > SellNextOfferDifrent) - {0}\n", (BuyNextOfferDifrent > SellNextOfferDifrent)); #endif } decimal precent = 0; try { precent = ((SellPrice - BuyPrice) / BuyPrice) * 100; } catch (Exception) { // Division by zero return(package); } SymbolsDate buy = (from item in list where item.Symbole == BuyKey select item).FirstOrDefault(); buy.ItsBuy = true; SymbolsDate sell = (from item in list where item.Symbole == SellKey select item).FirstOrDefault(); sell.ItsBuy = false; // After activating the magic number by buying.ItsBuy / buy.ItsBuy we will check the prices and the percentage of potential profit decimal buyPricePotential = WalletFunc.ConversionPrice(buy.orderTrade.request.Price, buy.payment); decimal sellPricePotential = WalletFunc.ConversionPrice(sell.orderTrade.request.Price, sell.payment); decimal percentPotential = 0; try { percentPotential = ((sellPricePotential - buyPricePotential) / buyPricePotential) * 100; } catch (Exception) { // Division by zero return(package); } #if DEBUG FindDebug += String.Format("precent - {0:P3}\n", precent / 100); FindDebug += String.Format("after implemation ExtraPercent\textraPercent.Percent - {0:P2}\n", buy.orderTrade.extraPercent.Percent); FindDebug += String.Format("buy.Price - {0:N8}\tbuyPricePotential (ConversionPrice) - {1:N8}\n", buy.orderTrade.request.Price, buyPricePotential); FindDebug += String.Format("after implemation ExtraPercent\textraPercent.Percent - {0:P2}\n", sell.orderTrade.extraPercent.Percent); FindDebug += String.Format("sell.Price - {0:N8}\tsellPricePotential (ConversionPrice) - {1:N8}\n", sell.orderTrade.request.Price, sellPricePotential); FindDebug += String.Format("percentPotential - {0:P3}\n\n\n", percentPotential / 100); PrintFunc.AddLine(StaticVariables.pathFindDebug + "Find_" + currency + ".txt", FindDebug); #endif package = new OrderHandling(precent, currency, buy, sell); package.buyPrice = WalletFunc.ConversionPrice(buy.orderTrade.maxOrMinPrice, buy.payment); package.sellPrice = WalletFunc.ConversionPrice(sell.orderTrade.maxOrMinPrice, sell.payment); package.buyPricePotential = buyPricePotential; package.sellPricePotential = sellPricePotential; package.percentPotential = percentPotential; return(package); }
public static bool Start(int useCase, OrderHandling orderHandling) { bool result = true; decimal amountToFilled; switch (useCase) { case 0: break; case 1: // Update price and send if (Start(5, orderHandling)) { Start(7, orderHandling); } else { return(false); } break; case 2: // Update quantity and send if (Start(6, orderHandling)) { Start(7, orderHandling); } else { return(false); } break; case 3: // Update quantity and price and send if (Start(5, orderHandling)) { if (Start(6, orderHandling)) { Start(7, orderHandling); } else { return(false); } } else { return(false); } break; case 4: // Send Cancel Order orderHandling.OrderToCare.Cancel = CancelOrder(orderHandling.OrderToCare.Result); break; case 5: // Price update action if (!orderHandling.OrderToCare.DownExtraPercent()) { result = false; } break; case 6: // Quantity update operation amountToFilled = orderHandling.OrderToCare.Result.Amount - orderHandling.OrderToCare.Result.AmountFilled; amountToFilled = StaticVariables.api.ClampOrderQuantity(orderHandling.OrderToCare.Request.Symbol, amountToFilled); if (amountToFilled < orderHandling.OrderToCare.MinAmaunt) { orderHandling.OrderToCare.itsCanUpdate = false; return(false); } orderHandling.OrderToCare.Request.Amount = amountToFilled; break; case 7: // Submit an update request to the Exchange if (!orderHandling.OrderToCare.itsCanUpdate) { return(false); } else { orderHandling.OrderToCare.Update = UpdateOrder(orderHandling.OrderToCare.Result, orderHandling.OrderToCare.Request); } break; default: break; } return(result); }