public static void PrintDataDebug() { PrintFunc.PrintList(StaticVariables.ListPaymentDontTrade, nameof(StaticVariables.ListPaymentDontTrade), StaticVariables.pathDataDebug); PrintFunc.PrintList(StaticVariables.PaymentListByWeight, nameof(StaticVariables.PaymentListByWeight), StaticVariables.pathDataDebug); PrintFunc.PrintDictionary(StaticVariables.Wallet, nameof(StaticVariables.Wallet), StaticVariables.pathDataDebug); PrintFunc.PrintDictionary(StaticVariables.WalletAvailableAmount, nameof(StaticVariables.WalletAvailableAmount), StaticVariables.pathDataDebug); PrintFunc.PrintDictionary(StaticVariables.WalletAvailable, nameof(StaticVariables.WalletAvailable), StaticVariables.pathDataDebug); PrintFunc.PrintDictionary(StaticVariables.ConversionCurrencyPayment, nameof(StaticVariables.ConversionCurrencyPayment), StaticVariables.pathDataDebug); PrintFunc.PrintDictionary(StaticVariables.listArbitrageSymbolsDate, nameof(StaticVariables.listArbitrageSymbolsDate), StaticVariables.pathDataDebug); PrintFunc.PrintDictionary(StaticVariables.magicNumberList, nameof(StaticVariables.magicNumberList), StaticVariables.pathDataDebug); PrintFunc.PrintDictionaryList(StaticVariables.symbolsDateList, nameof(StaticVariables.symbolsDateList), StaticVariables.pathDataDebug); string main1Var = String.Format("paymentWeighted - {0}\n", StaticVariables.paymentWeighted); main1Var += String.Format("usdName - {0}\n", StaticVariables.usdName); main1Var += String.Format("roundingPrice - {0}\n", StaticVariables.roundingPrice); main1Var += String.Format("eachAddPercentage - {0}\n", StaticVariables.eachAddPercentage); main1Var += String.Format("orderType - {0}\n", StaticVariables.orderType); main1Var += String.Format("FeeTrade - {0:P4}\n", StaticVariables.FeeTrade); main1Var += String.Format("maxTradeInUsdt - {0}\n", StaticVariables.maxTradeInUsdt); main1Var += String.Format("maxTradeInPaymentWeighted - {0}\n", StaticVariables.maxTradeInPaymentWeighted); main1Var += String.Format("revnuTrade - {0:P4}\n", StaticVariables.revnuTrade / 100); main1Var += String.Format("maxCount - {0}\n", StaticVariables.maxCount); main1Var += String.Format("CurrencyTradingFeeReduction - {0}\n", StaticVariables.CurrencyTradingFeeReduction); main1Var += String.Format("rateGateLimit - {0}\n", StaticVariables.rateGateLimit); PrintFunc.AddLine(StaticVariables.pathDataDebug + "Main1_var.txt", main1Var); }
public static void GetWalletAvailable() { decimal amaount; string currency; string symbole; string payment = StaticVariables.paymentWeighted; for (int i = 0; i < StaticVariables.PaymentListByWeight.Count; i++) { currency = StaticVariables.PaymentListByWeight[i]; payment = StaticVariables.paymentWeighted; if (currency.Equals(payment)) { payment = StaticVariables.PaymentListByWeight[1]; } symbole = currency + "_" + payment; SymbolsDate arbitrageSymbolsDate; if (StaticVariables.listArbitrageSymbolsDate.TryGetValue(symbole, out arbitrageSymbolsDate)) { amaount = arbitrageSymbolsDate.MinAmount; StaticVariables.WalletAvailable[currency] = (StaticVariables.WalletAvailableAmount[currency] > amaount); } else { symbole = payment + "_" + currency; if (StaticVariables.listArbitrageSymbolsDate.TryGetValue(symbole, out arbitrageSymbolsDate)) { amaount = arbitrageSymbolsDate.MinAmount; amaount = ConversionPrice(amaount, payment); StaticVariables.WalletAvailable[currency] = (StaticVariables.WalletAvailableAmount[currency] > amaount); } else { PrintException.ExceptionDeliberately("Missing symbol for mainMinAmount"); } } } #if DEBUG PrintFunc.PrintDictionary(StaticVariables.WalletAvailableAmount, nameof(StaticVariables.WalletAvailableAmount), StaticVariables.pathDataDebug); PrintFunc.PrintDictionary(StaticVariables.WalletAvailable, nameof(StaticVariables.WalletAvailable), StaticVariables.pathDataDebug); #endif }
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 void Start(bool fullSymbol = false) { List <string> SymbolsList = StaticVariables.api.GetSymbolsNormalize(); SymbolsList.Sort(); #if DEBUG PrintFunc.PrintList(SymbolsList, "SymbolsList_beforeRemove", StaticVariables.pathDataDebug); List <string> SymbolsListRemove = new List <string>(); #endif List <string> currencyList = new List <string>(); Dictionary <string, int> paymentList = new Dictionary <string, int>(); string[] currency_payment; string currency; string payment; for (int g = 0; g < SymbolsList.Count; g++) { currency_payment = SymbolsList[g].Split('_'); currency = currency_payment[0]; payment = currency_payment[1]; if (Remove(payment)) { #if DEBUG SymbolsListRemove.Add(SymbolsList[g]); #endif SymbolsList.Remove(SymbolsList[g]); // For the purpose of saving running time in the following loops g--; // Because we removed the value in the current index, then the next loop should use the current index that contains the following value } else { currencyList.Add(currency); if (paymentList.Keys.Contains(payment)) { paymentList[payment] = paymentList[payment] + 1; } else { paymentList.Add(payment, 1); } } } #if DEBUG PrintFunc.PrintList(SymbolsList, "SymbolsList_afterRemove", StaticVariables.pathDataDebug); PrintFunc.PrintList(SymbolsListRemove, "SymbolsListRemove", StaticVariables.pathDataDebug); #endif StaticVariables.PaymentListByWeight = paymentList.OrderByDescending(x => x.Value).Select(y => y.Key).ToList(); currencyList = currencyList.Distinct().ToList(); WalletFunc.InitializationStaticLists(SymbolsList); WalletFunc.ConversionPayment(); Dictionary <string, ExchangeTicker> allTickers = StaticVariables.api.GetTickers(); StaticVariables.maxTradeInPaymentWeighted = WalletFunc.GetMaxAmount(allTickers); //DataTable symboleDB = GetDB(GetExtraPercentFromDB); Dictionary <string, List <string> > listCurrenciesAndPayment = new Dictionary <string, List <string> >(); StaticVariables.symbolsDateList = new Dictionary <string, List <SymbolsDate> >(); // Use a reference. For the purpose of machine learning and the use of databases StaticVariables.magicNumberList = DBfunc.GetMagicNumberTable(); if (StaticVariables.magicNumberList.Count > 0) { WaitingTimeML.Start(); // USE to ML_4 } #if DEBUG PrintFunc.PrintList(SymbolsList, "SymbolsList_afterDistinct", StaticVariables.pathDataDebug); PrintFunc.PrintDictionary(allTickers, "allTickers", StaticVariables.pathDataDebug); #endif for (int i = 0; i < currencyList.Count; i++) { currency = currencyList[i]; List <string> paymentCurrencyList = new List <string>(); List <SymbolsDate> tempSymbolsDateList = new List <SymbolsDate>(); SymbolsDate tempSymbolsDate; ExchangeTicker tempTicker; MagicNumber magicNumber; string symbole; for (int j = 0; j < SymbolsList.Count; j++) { symbole = SymbolsList[j]; currency_payment = symbole.Split('_'); if (currency_payment[0].Equals(currency)) { paymentCurrencyList.Add((fullSymbol ? SymbolsList[j] : currency_payment[1])); if (!allTickers.TryGetValue(symbole, out tempTicker)) { SymbolsList.Remove(symbole); j--; continue; } magicNumber = DBfunc.GetMagicNumberItem(symbole, currency); tempSymbolsDate = new SymbolsDate(symbole, tempTicker, magicNumber); tempSymbolsDateList.Add(tempSymbolsDate); if (StaticVariables.PaymentListByWeight.Contains(currency)) { StaticVariables.listArbitrageSymbolsDate[symbole] = tempSymbolsDate; } SymbolsList.Remove(symbole); // For the purpose of saving running time in the following loops j--; // Because we removed the value in the current index, then the next loop should use the current index that contains the following value } } if (paymentCurrencyList.Count > 1) { paymentCurrencyList.Sort(); listCurrenciesAndPayment.Add(currency, paymentCurrencyList); StaticVariables.symbolsDateList.Add(currency, tempSymbolsDateList); } } DBfunc.AddMagicNumberTable(StaticVariables.magicNumberList); return; }
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); }