예제 #1
0
    static void makeOrder(string side)
    {
        bool execute = false;

        try
        {
            log(" wait 5s Make order " + side);


            if (side == "Sell" && statusShort == "enable" && Math.Abs(limiteOrder) > Math.Abs(bitMEXApi.GetOpenOrders(pair).Count))
            {
                double price = 0;

                String json = "";
                if (operation == "surf")
                {
                    price = Math.Abs(getPriceActual(side)) - 10;
                    json  = bitMEXApi.PostOrderPostOnly(pair, side, price, Math.Abs(getPosition()) + Math.Abs(qtdyContacts), true);
                }
                else
                {
                    price = Math.Abs(getPriceActual(side)) + 0;
                    json  = bitMEXApi.PostOrderPostOnly(pair, side, price, Math.Abs(qtdyContacts));
                }

                log(json);

                if (json.ToLower().IndexOf("error") >= 0)
                {
                    return;
                }

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

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

                        if (operation != "surf")
                        {
                            json = "error";
                            while (json.ToLower().IndexOf("error") >= 0)
                            {
                                json = bitMEXApi.PostOrderPostOnly(pair, "Buy", price, Math.Abs(qtdyContacts));
                                if (json.ToLower().IndexOf("error") >= 0)
                                {
                                    Thread.Sleep(800);
                                }
                            }
                        }



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



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

            if (side == "Buy" && statusLong == "enable" && Math.Abs(limiteOrder) > Math.Abs(bitMEXApi.GetOpenOrders(pair).Count))
            {
                double price = 0;
                String json  = "";
                if (operation == "surf")
                {
                    price = Math.Abs(getPriceActual(side)) + 10;
                    json  = bitMEXApi.PostOrderPostOnly(pair, side, price, Math.Abs(getPosition()) + Math.Abs(qtdyContacts), true);
                }
                else
                {
                    price = Math.Abs(getPriceActual(side)) - 0;
                    json  = bitMEXApi.PostOrderPostOnly(pair, side, price, Math.Abs(qtdyContacts));
                }

                log(json);
                if (json.ToLower().IndexOf("error") >= 0)
                {
                    return;
                }


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

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

                        if (operation != "surf")
                        {
                            json = "error";
                            while (json.ToLower().IndexOf("error") >= 0)
                            {
                                json = bitMEXApi.PostOrderPostOnly(pair, "Sell", price, Math.Abs(qtdyContacts));
                                if (json.ToLower().IndexOf("error") >= 0)
                                {
                                    Thread.Sleep(800);
                                }
                            }
                        }

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


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

        if (execute)
        {
            log("wait " + intervalOrder + "ms", ConsoleColor.Blue);
            Thread.Sleep(intervalOrder);
        }
    }
예제 #2
0
    static void makeOrder(string side)
    {
        bool execute = false;

        try
        {
            log("Make order " + side);

            if (side == "Sell" && statusShort == "enable" && Math.Abs(limiteOrder) > Math.Abs(bitMEXApi.GetOpenOrders(pair).Count))
            {
                double     price      = Math.Abs(getPriceActual(side) + 1);
                String     json       = bitMEXApi.PostOrderPostOnly(pair, side, price, qtdyContacts);
                JContainer jCointaner = (JContainer)JsonConvert.DeserializeObject(json, (typeof(JContainer)));
                log(json);
                log("wait total...");
                for (int i = 0; i < intervalCancelOrder; i++)
                {
                    if (!existsOrderOpenById(jCointaner["orderID"].ToString()))
                    {
                        price -= (price * profit) / 100;
                        price  = Math.Abs(Math.Floor(price));
                        json   = bitMEXApi.PostOrderPostOnly(pair, "Buy", price, qtdyContacts);
                        log(json);
                        execute = true;
                        break;
                    }
                    log("wait order limit 1s...");
                    Thread.Sleep(1000);
                }


                if (!execute)
                {
                    bitMEXApi.DeleteOrders(jCointaner["orderID"].ToString());
                }
            }
            if (side == "Buy" && statusLong == "enable" && Math.Abs(limiteOrder) > Math.Abs(bitMEXApi.GetOpenOrders(pair).Count))
            {
                double     price      = Math.Abs(getPriceActual(side) - 1);
                String     json       = bitMEXApi.PostOrderPostOnly(pair, side, price, qtdyContacts);
                JContainer jCointaner = (JContainer)JsonConvert.DeserializeObject(json, (typeof(JContainer)));
                log(json);
                log("wait total...");
                for (int i = 0; i < intervalCancelOrder; i++)
                {
                    if (!existsOrderOpenById(jCointaner["orderID"].ToString()))
                    {
                        price += (price * profit) / 100;
                        price  = Math.Abs(Math.Floor(price));
                        json   = bitMEXApi.PostOrderPostOnly(pair, "Sell", price, qtdyContacts);
                        log(json);
                        execute = true;
                        break;
                    }
                    log("wait order limit 1s...");
                    Thread.Sleep(1000);
                }


                if (!execute)
                {
                    bitMEXApi.DeleteOrders(jCointaner["orderID"].ToString());
                }
            }
        }
        catch (Exception ex)
        {
            log("makeOrder()::" + ex.Message + ex.StackTrace);
        }

        if (execute)
        {
            log("wait " + intervalOrder * 2 + "ms", ConsoleColor.Blue);
            Thread.Sleep(intervalOrder * 2);
        }
    }
예제 #3
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);
        }
    }