예제 #1
0
        // call by this.StartTradePackage
        public void Amounts()
        {
            minAmountFromExchange = Math.Min(buySymbolsDate.orderTrade.request.Amount, sellSymbolsDate.orderTrade.request.Amount);
            minAmountFromWallet   = StaticVariables.WalletAvailableAmount[currencyForWallet] / buySymbolsDate.orderTrade.maxOrMinPrice;
            originalAmountTrade   = Math.Min(minAmountFromExchange, minAmountFromWallet);

            decimal originalAmountTrade1 = Math.Max(originalAmountTrade, minAmountTrade);
            decimal originalAmountTrade2 = Math.Min(originalAmountTrade1, maxAmountTrade);

            AmountTrade = originalAmountTrade2;

#if DEBUG
            DateTime timeOrder = DateTime.Now;
            amountsPrint  = String.Format("{0}\n", timeOrder);
            amountsPrint += String.Format("buy.Amount - {0},\tsell.Amount - {1}\n", buySymbolsDate.orderTrade.request.Amount, sellSymbolsDate.orderTrade.request.Amount);
            amountsPrint += String.Format("minAmountFromExchange = Min(buy.Amount, sell.Amount) - {0}\n", minAmountFromExchange);
            amountsPrint += String.Format("minAmountFromWallet - {0}\n", minAmountFromWallet);
            amountsPrint += String.Format("originalAmountTrade = Min(minAmountFromExchange, minAmountFromWallet) - {0}\n", originalAmountTrade);

            amountsPrint += String.Format("minAmountTrade - {0},\tmaxAmountTrade - {1},\n", minAmountTrade, maxAmountTrade);

            amountsPrint += String.Format("originalAmountTrade1 = Max(originalAmountTrade, minAmountTrade) - {0},\n", originalAmountTrade1);
            amountsPrint += String.Format("originalAmountTrade2 = Min(originalAmountTrade1, maxAmountTrade) - {0},\n", originalAmountTrade2);
            amountsPrint += String.Format("AmountTrade = originalAmountTrade2 - {0},\n\n", AmountTrade);

            Directory.CreateDirectory(StaticVariables.pathDebug + currency);
            PrintFunc.AddLine(StaticVariables.pathDebug + currency + "/Amounts_" + currency + ".txt", amountsPrint);
#endif

            buySymbolsDate.orderTrade.request.Amount  = AmountTrade;
            sellSymbolsDate.orderTrade.request.Amount = AmountTrade;
        }
예제 #2
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;
            }
        }
예제 #3
0
        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);
        }
예제 #4
0
        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);
        }
예제 #5
0
 public static void StartTable(string PathFileName, string TableType)
 {
     if (!(File.Exists(PathFileName)))
     {
         string choosingTable = ChoosingTable(TableType);
         PrintFunc.AddLine(PathFileName, choosingTable);
     }
 }
예제 #6
0
        // call by Start.FindAndTrade
        public bool StartTradePackageMagic()
        {
            StartTradePackage();

            buySymbolsDate.orderTrade.itsFirstOrder = true;
            revnuCalculation(percent);
            percent_0 = realPercentage;
            percent_1 = realPercentage;
            ChangeMaxOrMinPriceBuy((StaticVariables.revnuTrade / 100));

#if DEBUG
            PrintFunc.AddLine(StaticVariables.pathDebug + currency + "/buy_" + currency + ".csv", debug);
#endif

            return(result);
        }
        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))
            }
        }
예제 #9
0
 public static void Start(string PathFileName, string printResult, string TableType)
 {
     StartTable(PathFileName, TableType);
     PrintFunc.AddLine(PathFileName, printResult);
 }
예제 #10
0
 public static void PrintConsole(string resPrint)
 {
     Console.WriteLine(resPrint);
     PrintFunc.AddLine(StaticVariables.pathWithDate + "Console.txt", resPrint);
 }
예제 #11
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);
        }