Esempio n. 1
0
        public void StartOrderHandling()
        {
            buy       = buySymbolsDate.orderTrade;
            sell      = sellSymbolsDate.orderTrade;
            arbitrage = arbitrageSymbolsDate.orderTrade;

            buy.MinAmaunt  = minAmountTrade;
            sell.MinAmaunt = minAmountTrade;
            percent_1      = realPercentage;
        }
        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))
            }
        }
Esempio n. 3
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);
        }