Esempio n. 1
0
        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;
            }
        }
Esempio n. 2
0
 public OrderHandling(decimal _precent, string _currency, SymbolsDate _buySymbolsDate, SymbolsDate _sellSymbolsDate)
     : base(_precent, _currency, _buySymbolsDate, _sellSymbolsDate)
 {
     done         = false;
     succsseTrade = false;
     itsCanAdded  = true;
     itsCanUpdate = true;
     intervalRevnuPercentageTake = 0.1m;
     TimeSummary          = new DateTime[10];
     TimeSummary[4]       = DateTime.Now;
     itsOrderHandlingLeft = false;
     itsTradeMagic        = true;
     ItsTradeFaster       = false;
 }
Esempio n. 3
0
        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);
        }
Esempio n. 4
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;
        }
Esempio n. 5
0
        public static decimal GetPrice(ExchangeOrderBook book, SymbolsDate item, bool buy)
        {
            decimal price  = 0;
            decimal amaunt = 0;
            int     j      = 0;

            do
            {
                if (buy)
                {
                    if (book.Asks.Count <= j)
                    {
                        return(0);
                    }

                    price  = book.Asks[j].Price;
                    amaunt = book.Asks[j].Amount;
                }
                else
                {
                    if (book.Bids.Count <= j)
                    {
                        return(0);
                    }

                    price  = book.Bids[j].Price;
                    amaunt = book.Bids[j].Amount;
                }

                j++;

                if (j == StaticVariables.maxCount) //  In case the first 5 orders in the bookOrder are below the minimum required quantity
                {
                    StaticVariables.maxCount = 10;
                    return(0);
                }

                if (!buy)
                {
                    item.MinAmount = price;
                }
            } while (amaunt < item.MinAmount);

            ExchangeOrderRequest request = new ExchangeOrderRequest();

            request.Amount    = amaunt;
            request.IsBuy     = buy;
            request.OrderType = StaticVariables.orderType;
            request.Price     = price;
            request.Symbol    = item.symbole;

            OrderTrade orderTrade = new OrderTrade(request);

            if (buy)
            {
                item.buyOrderTrade = orderTrade;
            }
            else
            {
                item.sellOrderTrade = orderTrade;
            }

            price = WalletFunc.ConversionPrice(price, item.payment);

#if DEBUG
            FindDebug += String.Format("{0}\tSymbol - {1}\n", (request.IsBuy ? "Buy" : "Sell"), request.Symbol);
            FindDebug += String.Format("while (amaunt < item.MinAmount) count - {0}\n", j);
            FindDebug += String.Format("MinAmount - {0}\n", item.MinAmount);
            FindDebug += String.Format("request - {0}\n", request.Print());
            FindDebug += String.Format("price - {0}\n\n", price);
#endif

            return(price);
        }