Beispiel #1
0
        public SymbolsDate(string _symbole, ExchangeTicker ticker, MagicNumber _magicNumber)
        {
            Symbole   = _symbole;
            lastPrice = ticker.Last;
            valume    = WalletFunc.ConversionPrice((ticker.Volume.PriceAmount * lastPrice), payment);

            if (StaticVariables.roundingPrice)
            {
                currencyRound = StaticVariables.api.GetRoundAmount(symbole, lastPrice);
            }

            MinAmount   = lastPrice;
            magicNumber = _magicNumber;
        }
Beispiel #2
0
        public string PrintResult()
        {
            string res = String.Format("\nTime: {4},\toriginal Percentage: {8:P3},\tEnd Percentage: {2:P3} " +
                                       "\nusdTrade - {18},\tusdRevnuCalculation - {21}  usdRevnu - {19:N3}," +
                                       "\nAmount Trade: {0:N3},\tbtcTrade - {17},\tbtcRevnu - {20}," +
                                       "\nprice To Buy = {1},\tprice To Sell = {5},\trevnue: {3:P3}," +
                                       "\npercent - {8:P3}   percentPotential - {9:P3}   percent_real - {10:P3}   percent_beforeBuy - {11:P3}   " +
                                       "\npercent_beforeSell - {12:P3}   percent_beforeArbitrage - {13:P3}  percentTrade - {22:P3}    percent_End - {14:P3}" +
                                       "\nbuy-{15} & sell-{16},\ttrade case-{6},\tSuccess- {7},",
                                       AmountTrade,
                                       WalletFunc.ConversionPrice(Buy.request.Price, buySymbolsDate.payment),
                                       percent_end / 100,
                                       StaticVariables.revnuTrade / 100,
                                       buy.startTimeOrder,
                                       WalletFunc.ConversionPrice(Sell.request.Price, sellSymbolsDate.payment),
                                       (itsTradeFaster ? "Faster" : "Magic"),
                                       (succsseTrade ? "YES" : "NO"),
                                       percent / 100,
                                       percentPotential / 100,
                                       percent_0 / 100,
                                       percent_1 / 100,
                                       percent_2 / 100,
                                       percent_3 / 100,
                                       percent_end / 100,
                                       buySymbol,
                                       sellSymbol,
                                       btcTrade,
                                       usdTrade,
                                       usdRevnu,
                                       btcRevnu,
                                       usdRevnuCalculation,
                                       percentTrade
                                       );

            if (!succsseTrade)
            {
                res += String.Format("\nbuy - {0:P2},    sell - {1:P2},   arbitrage - {2:P2},",
                                     (buy.amountFilled / AmountTrade),
                                     (sell.amountFilled / AmountTrade),
                                     (arbitrage.amountFilled / arbitrage.Result.Amount));
            }

            return(res);
        }
Beispiel #3
0
        public void WalletResultEnd()
        {
            succsseTrade = (buy.succsseTrade & sell.succsseTrade & arbitrage.succsseTrade);

            oldWalletAvailableAmount = new Dictionary <string, decimal>();
            foreach (var item in StaticVariables.WalletAvailableAmount)
            {
                oldWalletAvailableAmount.Add(item.Key, item.Value);
            }
            StaticVariables.Wallet   = WalletFunc.GetWallet();
            newWalletAvailableAmount = new Dictionary <string, decimal>();
            newWalletAvailableAmount = StaticVariables.WalletAvailableAmount;
            walletResult            += WalletFunc.WalletResult(newWalletAvailableAmount); // -after

            summaryTradeReal = String.Format("{0},,", succsseTrade);
            walletResultReal = walletResult + WalletFunc.WalletResultCompare(oldWalletAvailableAmount, newWalletAvailableAmount, out decimal sumStart, out decimal sumAfter, out decimal sumRevnu);   // -dif

            btcRevnu     = sumRevnu;
            usdRevnu     = WalletFunc.ConversionPrice(btcRevnu, StaticVariables.paymentWeighted, StaticVariables.usdName, StaticVariables.usdName);
            btcTrade     = WalletFunc.ConversionPrice(Expense, buySymbolsDate.payment);
            usdTrade     = Math.Round(WalletFunc.ConversionPrice(btcTrade, StaticVariables.paymentWeighted, StaticVariables.usdName, StaticVariables.usdName), 3);
            percentTrade = btcRevnu / btcTrade;


#if DEBUG
            bool DifferentCalculation = percentTrade == percent_end;
            summaryTradeReal += String.Format("{0},{1},,", DifferentCalculation, percent_end);
#endif

            summaryTradeReal += String.Format("{0},{1},{2},{3},{4},{5},,{6},{7},,{8}", buySymbol, usdRevnu, btcRevnu, percentTrade, usdTrade, btcTrade, sumStart, sumAfter, walletResultReal);

            if (!buy.succsseTrade)
            {
                newWalletAvailableAmount[buySymbolsDate.payment] -= (buy.Result.Amount - buy.Result.AmountFilled) * buy.request.Price;
                numFee++;
            }


            if (!sell.succsseTrade)
            {
                newWalletAvailableAmount[sellSymbolsDate.payment] += (sell.Result.Amount - sell.Result.AmountFilled) * sell.request.Price;
                numFee++;
            }

            if (!arbitrage.succsseTrade)
            {
                if (itsBuyArbitrage)
                {
                    newWalletAvailableAmount[arbitrageSymbolsDate.currency] += (arbitrage.Result.Amount - arbitrage.Result.AmountFilled) * arbitrage.request.Price;
                }
                else
                {
                    newWalletAvailableAmount[arbitrageSymbolsDate.payment] += (arbitrage.Result.Amount - arbitrage.Result.AmountFilled) * arbitrage.request.Price;
                }

                numFee++;
            }

            walletResult += WalletFunc.WalletResult(newWalletAvailableAmount);                                                                            // -addOrderLeft

            walletResult += WalletFunc.WalletResultCompare(oldWalletAvailableAmount, newWalletAvailableAmount, out sumStart, out sumAfter, out sumRevnu); // -dif

            btcRevnu            = sumRevnu;
            usdRevnu            = WalletFunc.ConversionPrice(btcRevnu, StaticVariables.paymentWeighted, StaticVariables.usdName, StaticVariables.usdName);
            btcTrade            = WalletFunc.ConversionPrice(Expense, buySymbolsDate.payment);
            usdTrade            = Math.Round(WalletFunc.ConversionPrice(btcTrade, StaticVariables.paymentWeighted, StaticVariables.usdName, StaticVariables.usdName), 3);
            percentTrade        = (btcRevnu / btcTrade) + (StaticVariables.FeeTrade * numFee);
            usdRevnuCalculation = Math.Round(usdTrade * ((percent_end / 100) - (StaticVariables.FeeTrade * 3)), 3);


#if DEBUG
            DifferentCalculation = percentTrade == percent_end;
            summaryTrade         = String.Format("{0},{1},{2},,", DifferentCalculation, percent_end, percentTrade);
#endif

            summaryTrade += String.Format("{0},{1},{2},{3},{4},{5},,{6},{7},,{8}", buySymbol, usdRevnu, btcRevnu, percentTrade, usdTrade, btcTrade, sumStart, sumAfter, walletResult);
        }
Beispiel #4
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);
        }
Beispiel #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);
        }