Exemplo n.º 1
0
        public void TestResponse(Authorization authorization)
        {
            Console.WriteLine();
            GetMyPosition getPosition = new GetMyPosition {
                symbol = symbol
            };
            string            getPositionRequest  = getPosition.CreateRequest(authorization, TimeValue);
            string            getPositionResponse = HTTP.Get(getPositionRequest);
            GetMyPositionRoot ResultGetPosition   = Makeclass <GetMyPositionRoot> .Get(getPositionResponse);

            Console.ReadLine();
            bool againplaceorder = true;
            int  Count           = 0;
            var  OrderStatus     = "";

            //Выставляем контракт по рынку
            placeorder = new PlaceOrder
            {
                api_key       = api,
                qty           = ContractQty.ToString(),
                side          = TempSide,
                symbol        = symbol,
                order_type    = "Market",
                time_in_force = "GoodTillCancel",
                sign          = sign
            };

            url = placeorder.CreateRequest(authorization, TimeValue);//задаем путь запроса
            //получаем ответ от выставленных контрактов
            var response = HTTP.Post(Makejson.Convert(placeorder), url);

            PlaceOrderRoot OrderRoot = Makeclass <PlaceOrderRoot> .Get(response);

            //Получаем цену ,по которой вошли в позицию
            //   double Price = OrderRoot.result.price;



            GetActiveOrderRealTime getOrder = new GetActiveOrderRealTime
            {
                api_key = api,
                //         order_id = temp_order_id,
                symbol = symbol,
                sign   = sign
            };

            var p = HTTP.Get(getOrder.CreateRequest(authorization, TimeValue));
            GetActiveOrderRealTimeRoot ResultGetOrder = Makeclass <GetActiveOrderRealTimeRoot> .Get(p);

            OrderStatus = ResultGetOrder.result.order_status;

            if (OrderStatus == "Cancelled")
            {
                Console.WriteLine("STOP");
            }
        }
Exemplo n.º 2
0
        public double PlaceActiveOrders(Authorization authorization, string position)//Выставить активный ордер
        {
            Console.WriteLine();
            SetPositionConstants(position);

            bool   againplaceorder = true;
            int    Count           = 0;
            var    OrderStatus     = "";
            string LastLimitPrice  = "";

            //Выставляем контракт по рынку
            placeorder = new PlaceOrder
            {
                api_key       = api,
                qty           = ContractQty.ToString(),
                side          = TempSide,
                symbol        = symbol,
                order_type    = "Market",
                time_in_force = "GoodTillCancel",
                sign          = sign
            };

            url = placeorder.CreateRequest(authorization, TimeValue);//задаем путь запроса
            //получаем ответ от выставленных контрактов
            var response = HTTP.Post(Makejson.Convert(placeorder), url);

            PlaceOrderRoot OrderRoot = Makeclass <PlaceOrderRoot> .Get(response);

            //Получаем цену ,по которой вошли в позицию
            double Price = OrderRoot.result.price;

            //Если выставляем лимитный ордер не в первый раз - отменяем сначала все активные ордера
            if (MAC_TEMP_COUNT_ORDER > 0)
            {
                CancelAllActiveOrder cancelorder = new CancelAllActiveOrder();
                url      = cancelorder.CreateRequest(authorization, TimeValue);//задаем путь запроса
                response = HTTP.Post(Makejson.Convert(cancelorder), url);
                Console.WriteLine("Отменили все ордера");
            }

            MAC_TEMP_COUNT_ORDER++;//Увеличили количество выставленных ордеров на 1, потому что сейчас выставим ордера


            Price = SetPrice(Price, FirstContract, position, 1);//Выставляем ордер от той цены позиции


            for (int i = 0; i < MAC_TEMP_COUNT_ORDER; i++) //Пытаемся выставить лимитные ордера
            {
                while (againplaceorder && Count < 20)      //Если цена пошла быстрее чем мы выставили ордер, то начинаем спамить с определенным шагом
                {
                    againplaceorder = false;
                    placeorder      = new PlaceOrder
                    {
                        api_key       = api,
                        qty           = (ContractQty).ToString(),
                        side          = TempSide == "Sell" ? "Buy" : "Sell",
                        symbol        = symbol,
                        price         = Price.ToString(),
                        order_type    = "Limit",
                        time_in_force = "PostOnly",
                        sign          = sign
                    };

                    url      = placeorder.CreateRequest(authorization, TimeValue);//задаем путь запроса
                    response = HTTP.Post(Makejson.Convert(placeorder), url);

                    PlaceOrderRoot placeOrderRoot = Makeclass <PlaceOrderRoot> .Get(response);


                    var temp_order_id = placeOrderRoot.result.order_id;

                    GetActiveOrderRealTime getOrder = new GetActiveOrderRealTime
                    {
                        api_key  = api,
                        order_id = temp_order_id,
                        symbol   = symbol,
                        sign     = sign
                    };
                    string getOrderjson     = getOrder.CreateRequest(authorization, TimeValue);
                    var    getOrderResponse = HTTP.Get(getOrderjson);
                    GetActiveOrderRealTimeRoot ResultGetOrder = Makeclass <GetActiveOrderRealTimeRoot> .Get(getOrderResponse);

                    if (Count != 0)
                    {
                        Console.WriteLine("Не успеели выставить ,идем еще ниже - " + Count);
                    }
                    if (ResultGetOrder.ret_code != 0)
                    {
                        againplaceorder = true;
                        throw new Exception("Что-то пошло не так при получении значения ActiveOrderRealTime. отправленный запрос - " + getOrderjson + " ; полученный запрос - " + getOrderResponse);
                    }

                    OrderStatus = ResultGetOrder.result.order_status;


                    Console.WriteLine("Цена ордера, который мы выставили - " + ResultGetOrder.result.price);
                    Console.WriteLine("Статус выставленного оордера - " + OrderStatus);
                    if (OrderStatus == "Cancelled")
                    {
                        Console.WriteLine("не успели выставить ордер, идем с шагом 0.5");
                        againplaceorder = true;
                        Price           = SetPrice(Price, 0.5, position, 2);
                        Count++;
                    }
                    LastLimitPrice = ResultGetOrder.result.price;
                }
                if (Count >= 20)
                {
                    ClosePosition(authorization);

                    Console.WriteLine("Больше 20 итераций");
                    //return Price - ContractStep;
                    return(SetPrice(Price, ContractStep, position, 2));
                }
                else
                {
                    Console.WriteLine("Могла бы быть такая цена - " + SetPrice(Price, ContractStep, position, 1));
                    Price = double.Parse(LastLimitPrice, CultureInfo.InvariantCulture);
                    Console.WriteLine("Но сейчас такая цена последняя выставленного ордера по запросу GET - " + Price);
                }

                // Price = SetPrice(Price, ContractStep, position, 1);
                Console.WriteLine("--итерация выставленного ордера:" + i + ";Всего нужно выставить:" + MAC_TEMP_COUNT_ORDER);
            }


            SetPositionConstants(position);
            Console.WriteLine();
            return(SetPrice(Price, ContractStep, position, 2));
        }