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
            }
        }
Example #2
0
        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;
        }