public Trade_package(decimal _precent, string _currency, SymbolsDate _buySymbolsDate, SymbolsDate _sellSymbolsDate) { api = StaticVariables.api; itsBuyArbitrage = true; percent = _precent; buySymbolsDate = _buySymbolsDate; sellSymbolsDate = _sellSymbolsDate; currency = _currency; buySymbol = buySymbolsDate.symbole; sellSymbol = sellSymbolsDate.symbole; minAmountTrade = Math.Max(buySymbolsDate.MinAmount, sellSymbolsDate.MinAmount); maxAmountTrade = WalletFunc.GetMaxAmountTrade(buySymbolsDate.orderTrade.request.Price, buySymbolsDate.payment); maxAmountTrade = StaticVariables.api.ClampOrderQuantity(buySymbol, maxAmountTrade); if (maxAmountTrade < minAmountTrade) { string warningMessage = String.Format("currency - {0}, buySymbol - {1}, buy.MinAmount - {2}, sellSymbols - {3}, sell.MinAmount- {4}, minAmountTrade - {5}, maxAmountTrade - {6}", currency, buySymbol, buySymbolsDate.MinAmount, sellSymbol, sellSymbolsDate.MinAmount, minAmountTrade, maxAmountTrade); PrintTable.PrintConsole(warningMessage); PrintFunc.AddLine(StaticVariables.pathWithDate + "WARNING_maxAmount.txt", warningMessage); maxAmountTrade = minAmountTrade; } }
public static decimal ConversionPrice(decimal price, string currency, string payment = null, string convertibleCurrency = null) { payment = (payment == null ? StaticVariables.paymentWeighted : payment); if (payment.Equals(currency)) { return(price); } string symbole = currency + "_" + payment; decimal result = price * StaticVariables.ConversionCurrencyPayment[symbole]; convertibleCurrency = (convertibleCurrency == null ? StaticVariables.paymentWeighted : convertibleCurrency); if (payment.Equals(convertibleCurrency)) { return(result); } string Finalsymbole = payment + "_" + convertibleCurrency; decimal FinalConversionRatio; if (!StaticVariables.ConversionCurrencyPayment.TryGetValue(Finalsymbole, out FinalConversionRatio)) { string warningMessage = String.Format("WARNING_Finalsymbole_{0}.txt", Finalsymbole); PrintTable.PrintConsole(warningMessage); PrintFunc.AddLine(StaticVariables.pathWithDate + warningMessage, warningMessage); } return(result * FinalConversionRatio); }
public static void ExceptionDeliberately(string printResult) { decimal result = 0; try { // The goal is to intentionally throw an exception decimal temp = 1 / result; } catch (Exception ex) { PrintTable.PrintConsole(printResult); Start("ExceptionDeliberately", printResult); throw ex; } }
public static void FindAndTrade(bool trade) { StaticVariables.Wallet = WalletFunc.GetWallet(); #if DEBUG PrintDataDebug(); #endif DateTime currentTime; string timeHouer; string pathSummaryFind; int numFind = 0; while (true) { numFind++; currentTime = DateTime.Now; List <OrderHandling> packageList = new List <OrderHandling>(); int i = 0; bool tradeSuccses; List <MagicNumber> magicNumbersToUpdate = new List <MagicNumber>(); foreach (var item in StaticVariables.symbolsDateList) { if (i % 5 == 0) { WalletFunc.ConversionPayment(); } i++; tradeSuccses = false; do { OrderHandling package; try { package = FindingSymbolsTrading.ArbitragePercent(item.Key, item.Value); } catch (Exception ex) { package = null; DateTime localDate = DateTime.Now; string printResult = String.Format("{0}\n{1}", localDate.ToString(), ex.ToString()); printResult += String.Format("\ncurrency - {0}", item.Key); PrintException.Start(MethodBase.GetCurrentMethod().Name, printResult); } if (package != null) { packageList.Add(package); PrintTable.Start(StaticVariables.pathFindFile + item.Key + ".csv", package.ToString(), "Trade_package"); // TODO Add Func TradeFast List <MagicNumber> magicNumbersTradeMagicToUpdate = new List <MagicNumber>(); if (package.percentPotential > StaticVariables.revnuTrade) { if (StaticVariables.rateGateLimit) { StaticVariables.api.RateLimit.OneOpportunity = true; } try { if (package.StartTradePackageMagic()) { tradeSuccses = TradeMagic.Start(package); } } catch (Exception ex) { StaticVariables.Wallet = WalletFunc.GetWallet(); // Wallet update. Because part of the trade was carried out. Apparently the amounts of coins have changed DateTime localDate = DateTime.Now; string printResult = String.Format("{0}\n{1}", localDate.ToString(), ex.ToString()); PrintException.Start(MethodBase.GetCurrentMethod().Name, printResult); try { PrintTable.Start(StaticVariables.pathWithDate + "Exception_TradeMagic_" + item.Key + ".csv", package.Buy.ToString(), "OrderTrade"); PrintTable.Start(StaticVariables.pathWithDate + "Exception_TradeMagic_" + item.Key + ".csv", package.Sell.ToString(), "OrderTrade"); PrintTable.Start(StaticVariables.pathWithDate + "Exception_TradeMagic_" + item.Key + ".csv", package.Arbitrage.ToString(), "OrderTrade"); } catch (Exception) { PrintTable.Start(StaticVariables.pathWithDate + "Exception_TradeMagic_Exception_" + item.Key + ".csv", package.ToString(), "Trade_package"); } } if (StaticVariables.rateGateLimit) { StaticVariables.api.RateLimit.OneOpportunity = false; } magicNumbersTradeMagicToUpdate.Add(package.buySymbolsDate.magicNumber); magicNumbersTradeMagicToUpdate.Add(package.sellSymbolsDate.magicNumber); magicNumbersTradeMagicToUpdate.Add(package.arbitrageSymbolsDate.magicNumber); SqlMagicNumber.UpdateAll(magicNumbersTradeMagicToUpdate); } else { magicNumbersToUpdate.Add(package.buySymbolsDate.magicNumber); } } } while (tradeSuccses); } SqlMagicNumber.UpdateAll(magicNumbersToUpdate); WaitingTimeML.Start(); // USE to ML_4 timeHouer = String.Format("{0}-{1}-{2}", currentTime.Hour, currentTime.Minute, currentTime.Second); PrintTable.PrintConsole(timeHouer + "\t" + numFind); pathSummaryFind = StaticVariables.pathSummaryFind + "SummaryFind_" + timeHouer + ".csv"; foreach (var item in packageList) { PrintTable.Start(pathSummaryFind, item.ToString(), "Trade_package"); if (item.percent > StaticVariables.revnuTrade || item.percentPotential > StaticVariables.revnuTrade) { PrintTable.PrintConsole(item.ToConsole()); PrintTable.Start(StaticVariables.pathWithDate + "SummaryFind" + ".csv", item.ToString(), "Trade_package"); } } #if DEBUG PrintFunc.PrintDictionary(StaticVariables.magicNumberList, nameof(StaticVariables.magicNumberList), StaticVariables.pathDataDebug); #endif } }
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)) } }