Exemple #1
0
    public static void makeOrder(string side, bool nothing = false, string text = "botmex", bool force = false)
    {
        bool execute = false;

        try
        {
            log(" wait 5s Make order " + side);
            double price = 0;
            String json  = "";

            if (nothing)
            {
                price = Math.Abs(getPriceActual(side));
                json  = bitMEXApi.PostOrderPostOnly(pair, side, price, Math.Abs(qtdyContacts), force, marketTaker, text);
                return;
            }

            if (side == "Sell" && statusShort == "enable" && Math.Abs(limiteOrder) > Math.Abs(bitMEXApi.GetOpenOrders(pair).Count) && getPosition() >= 0)
            {
                if (tendencyBook)
                {
                    if (getTendencyOrderBook() != Tendency.low)
                    {
                        return;
                    }
                }


                if (operation == "surf" || operation == "normal")
                {
                    price = Math.Abs(getPriceActual(side));
                    json  = bitMEXApi.PostOrderPostOnly(pair, side, price + obDiff, Math.Abs(getPosition()) + Math.Abs(qtdyContacts), true, marketTaker, text);
                }

                log(json);

                if (json.ToLower().IndexOf("error") >= 0 || json.ToLower().IndexOf("canceled") >= 0)
                {
                    if (json.ToLower().IndexOf("overload") >= 0)
                    {/* In case of overload, close the position to prevent losses*/
                        log("System is on overload, trying to close position to prevent losses");
                        log(bitMEXApi.MarketClose(pair, side));
                    }
                    return;
                }


                log("Short Order at: " + price);

                JContainer config = (JContainer)JsonConvert.DeserializeObject(json, (typeof(JContainer)));


                for (int i = 0; i < intervalCancelOrder; i++)
                {
                    if (!existsOrderOpenById(config["orderID"].ToString()) && !existsOrderOpenById(config["orderID"].ToString()))
                    {
                        if (operation == "scalper")
                        {
                            price = price - stepValue;
                        }
                        else
                        {
                            price -= (price * profit) / 100;
                            price  = Math.Abs(Math.Floor(price));
                        }

                        traillingProfit = -1;
                        traillingStop   = -1;

                        log(json);
                        execute = true;
                        break;
                    }
                    log("wait order limit " + i + " of " + intervalCancelOrder + "...");
                    if (operation != "scalper")
                    {
                        Thread.Sleep(800);
                    }
                }

                if (!execute)
                {
                    bitMEXApi.DeleteOrders(config["orderID"].ToString());
                    while (existsOrderOpenById(config["orderID"].ToString()))
                    {
                        bitMEXApi.DeleteOrders(config["orderID"].ToString());
                    }
                    log("Cancel order ID " + config["orderID"].ToString());
                }
            }

            if (side == "Buy" && statusLong == "enable" && Math.Abs(limiteOrder) > Math.Abs(bitMEXApi.GetOpenOrders(pair).Count) && getPosition() <= 0)
            {
                if (tendencyBook)
                {
                    if (getTendencyOrderBook() != Tendency.high)
                    {
                        return;
                    }
                }


                price = 0;
                json  = "";
                if (operation == "surf" || operation == "normal")
                {
                    price = Math.Abs(getPriceActual(side));
                    json  = bitMEXApi.PostOrderPostOnly(pair, side, price - obDiff, Math.Abs(getPosition()) + Math.Abs(qtdyContacts), true, marketTaker, text);
                }


                log(json);
                if (json.ToLower().IndexOf("error") >= 0 || json.ToLower().IndexOf("canceled") >= 0)
                {
                    if (json.ToLower().IndexOf("overload") >= 0)
                    {/* In case of overload, close the position to prevent losses*/
                        log("System is on overload, trying to close position to prevent losses");
                        log(bitMEXApi.MarketClose(pair, side));
                    }
                    return;
                }


                log("Long Order at: " + price);
                JContainer config = (JContainer)JsonConvert.DeserializeObject(json, (typeof(JContainer)));

                log("wait total...");
                for (int i = 0; i < intervalCancelOrder; i++)
                {
                    if (!existsOrderOpenById(config["orderID"].ToString()) && !existsOrderOpenById(config["orderID"].ToString()))
                    {
                        if (operation == "scalper")
                        {
                            price = price + stepValue;
                        }
                        else
                        {
                            price += (price * profit) / 100;
                            price  = Math.Abs(Math.Floor(price));
                        }

                        traillingProfit = -1;
                        traillingStop   = -1;
                        log(json);
                        execute = true;
                        break;
                    }
                    log("wait order limit " + i + " of " + intervalCancelOrder + "...");
                    if (operation != "scalper")
                    {
                        Thread.Sleep(1000);
                    }
                }


                if (!execute)
                {
                    bitMEXApi.DeleteOrders(config["orderID"].ToString());
                    while (existsOrderOpenById(config["orderID"].ToString()))
                    {
                        bitMEXApi.DeleteOrders(config["orderID"].ToString());
                    }
                    log("Cancel order ID " + config["orderID"].ToString());
                }
            }
        }
        catch (Exception ex)
        {
            log("makeOrder()::" + ex.Message + ex.StackTrace);
        }

        if (execute)
        {
            log("wait " + intervalOrder + "ms", ConsoleColor.Blue);
            Thread.Sleep(intervalOrder);
        }
    }