Example #1
0
    static void makeOrder(string side)
    {
        try
        {
            log("Make order " + side);
            if (side == "Sell" && statusShort == "enable")
            {
                String json = bitMEXApi.MarketOrder(pair, "Sell", qtdyContacts);
                log(json);
                JContainer jCointaner = (JContainer)JsonConvert.DeserializeObject(json, (typeof(JContainer)));
                double     price      = double.Parse(jCointaner["price"].ToString().Replace(".", "."));
                price -= (price * (fee + profit)) / 100;
                price  = Math.Floor(price);
                json   = bitMEXApi.PostOrderPostOnly(pair, "Buy", price, qtdyContacts);
                log(json);
            }
            if (side == "Buy" && statusLong == "enable")
            {
                String json = bitMEXApi.MarketOrder(pair, "Buy", qtdyContacts);
                log(json);
                JContainer jCointaner = (JContainer)JsonConvert.DeserializeObject(json, (typeof(JContainer)));
                double     price      = double.Parse(jCointaner["price"].ToString().Replace(".", "."));
                price += (price * (fee + profit)) / 100;
                price  = Math.Ceiling(price);
                json   = bitMEXApi.PostOrderPostOnly(pair, "Sell", price, qtdyContacts);
                log(json);
            }
        }
        catch (Exception ex)
        {
            log("ERRO MAKEORDER " + ex.Message + ex.StackTrace);
        }

        log("wait " + intervalOrder + "ms");
        Thread.Sleep(intervalOrder);
    }
Example #2
0
    public static void Main(string[] args)
    {
        try
        {
            //Config
            Console.Title = "Loading...";

            Console.ForegroundColor = ConsoleColor.White;

            log("Deleron - Back to the future - v" + version + " - Bitmex version");
            log("by Matheus Grijo ", ConsoleColor.Green);
            log(" ======= HALL OF FAME BOTMEX  ======= ");
            log(" - Lucas Sousa", ConsoleColor.Magenta);
            log(" - Carlos Morato", ConsoleColor.Magenta);
            log(" - Luis Felipe Alves", ConsoleColor.Magenta);
            log(" ======= END HALL OF FAME BOTMEX  ======= ");

            log("http://botmex.ninja/");
            log("GITHUB http://github.com/matheusgrijo", ConsoleColor.Blue);
            log(" ******* DONATE ********* ");
            log("BTC 39DWjHHGXJh9q82ZrxkA8fiZoE37wL8jgh");
            log("BCH qqzwkd4klrfafwvl7ru7p7wpyt5z3sjk6y909xq0qk");
            log("ETH 0x3017E79f460023435ccD285Ff30Bd10834D20777");
            log("ETC 0x088E7E67af94293DB55D61c7B55E2B098d2258D9");
            log("LTC MVT8fxU4WBzdfH5XgvRPWkp7pE4UyzG9G5");
            log("Load config...");
            log("Considere DOAR para o projeto!", ConsoleColor.Green);
            log("Vamos aguardar 10 min para voce doar ;) ... ", ConsoleColor.Blue);
            log("ATENCAO, PARA FACILITAR A DOACAO DAQUI A 30 SEGUNDOS VAMOS ABRIR UMA PAGINA PARA VOCE!", ConsoleColor.Green);
            System.Threading.Thread.Sleep(30000);
            System.Diagnostics.Process.Start("https://www.blockchain.com/btc/payment_request?address=1AnttTLGhzJsX7T96SutWS4N9wPYuBThu8&amount_local=30&currency=USD&nosavecurrency=true");
            log("Perfeito, agora aguarde os 9 minutos e 30 segundos restantes para iniciar o BOTMEX, enquanto isto estamos carregando as suas configuracoes...", ConsoleColor.Magenta);
            String     jsonConfig = System.IO.File.ReadAllText(location + "key.txt");
            JContainer jCointaner = (JContainer)JsonConvert.DeserializeObject(jsonConfig, (typeof(JContainer)));

            System.Threading.Thread.Sleep(60000 * 10);



            bitmexKey           = jCointaner["key"].ToString();
            bitmexSecret        = jCointaner["secret"].ToString();
            bitmexKeyWeb        = jCointaner["webserverKey"].ToString();
            bitmexSecretWeb     = jCointaner["webserverSecret"].ToString();
            bitmexDomain        = jCointaner["domain"].ToString();
            statusShort         = jCointaner["short"].ToString();
            statusLong          = jCointaner["long"].ToString();
            pair                = jCointaner["pair"].ToString();
            timeGraph           = jCointaner["timeGraph"].ToString();
            qtdyContacts        = int.Parse(jCointaner["contract"].ToString());
            interval            = int.Parse(jCointaner["interval"].ToString());
            intervalCancelOrder = int.Parse(jCointaner["intervalCancelOrder"].ToString());
            intervalOrder       = int.Parse(jCointaner["intervalOrder"].ToString());
            intervalCapture     = int.Parse(jCointaner["webserverIntervalCapture"].ToString());
            profit              = double.Parse(jCointaner["profit"].ToString());
            fee          = double.Parse(jCointaner["fee"].ToString());
            stoploss     = double.Parse(jCointaner["stoploss"].ToString());
            stopgain     = double.Parse(jCointaner["stopgain"].ToString());
            roeAutomatic = jCointaner["roe"].ToString() == "automatic";
            limiteOrder  = int.Parse(jCointaner["limiteOrder"].ToString());

            bitMEXApi = new BitMEX.BitMEXApi(bitmexKey, bitmexSecret, bitmexDomain);



            //TESTS HERE

            //makeOrder("Buy");

            //FINAL

            if (jCointaner["webserver"].ToString() == "enable")
            {
                WebServer ws = new WebServer(WebServer.SendResponse, jCointaner["webserverConfig"].ToString());
                ws.Run();
                System.Threading.Thread tCapture = new Thread(Database.captureDataJob);
                tCapture.Start();
                System.Threading.Thread.Sleep(1000);
            }


            log("wait 1s...");
            System.Threading.Thread.Sleep(1000);
            log("Total open orders: " + bitMEXApi.GetOpenOrders(pair).Count);
            log("");
            log("Wallet: " + bitMEXApi.GetWallet());

            lstIndicatorsAll.Add(new IndicatorADX());
            lstIndicatorsAll.Add(new IndicatorMFI());
            lstIndicatorsAll.Add(new IndicatorBBANDS());
            lstIndicatorsAll.Add(new IndicatorCCI());
            lstIndicatorsAll.Add(new IndicatorCMO());
            lstIndicatorsAll.Add(new IndicatorDI());
            lstIndicatorsAll.Add(new IndicatorDM());
            lstIndicatorsAll.Add(new IndicatorMA());
            lstIndicatorsAll.Add(new IndicatorMACD());
            lstIndicatorsAll.Add(new IndicatorMOM());
            lstIndicatorsAll.Add(new IndicatorPPO());
            lstIndicatorsAll.Add(new IndicatorROC());
            lstIndicatorsAll.Add(new IndicatorRSI());
            lstIndicatorsAll.Add(new IndicatorSAR());
            lstIndicatorsAll.Add(new IndicatorSTOCH());
            lstIndicatorsAll.Add(new IndicatorSTOCHRSI());
            lstIndicatorsAll.Add(new IndicatorTRIX());
            lstIndicatorsAll.Add(new IndicatorULTOSC());
            lstIndicatorsAll.Add(new IndicatorWILLR());

            foreach (var item in jCointaner["indicatorsEntry"])
            {
                foreach (var item2 in lstIndicatorsAll)
                {
                    if (item["name"].ToString().Trim().ToUpper() == item2.getName().Trim().ToUpper())
                    {
                        item2.setPeriod(int.Parse((item["period"].ToString().Trim().ToUpper())));
                        lstIndicatorsEntry.Add(item2);
                    }
                }
            }

            foreach (var item in jCointaner["indicatorsEntryCross"])
            {
                foreach (var item2 in lstIndicatorsAll)
                {
                    if (item["name"].ToString().Trim().ToUpper() == item2.getName().Trim().ToUpper())
                    {
                        item2.setPeriod(int.Parse((item["period"].ToString().Trim().ToUpper())));
                        lstIndicatorsEntryCross.Add(item2);
                    }
                }
            }

            foreach (var item in jCointaner["indicatorsEntryDecision"])
            {
                foreach (var item2 in lstIndicatorsAll)
                {
                    if (item["name"].ToString().Trim().ToUpper() == item2.getName().Trim().ToUpper())
                    {
                        item2.setPeriod(int.Parse((item["period"].ToString().Trim().ToUpper())));
                        lstIndicatorsEntryDecision.Add(item2);
                    }
                }
            }


            if (jCointaner["webserver"].ToString() == "enable")
            {
                System.Diagnostics.Process.Start(jCointaner["webserverConfig"].ToString());
            }


            bool automaticTendency = statusLong == "automatic";

            //LOOP
            while (true)
            {
                positionContracts = getPosition(); // FIX CARLOS MORATO
                roe = getRoe();


                //Stop Loss
                if (roe < 0)
                {
                    if ((roe * (-1)) >= stoploss)
                    {
                        //Stop loss
                        bitMEXApi.CancelAllOpenOrders(pair);
                        String side = "Buy";
                        if (positionContracts > 0)
                        {
                            side = "Sell";
                        }
                        bitMEXApi.MarketOrder(pair, side, positionContracts);
                    }
                }

                //Stop Gain
                if (roe > 0)
                {
                    if (roe >= stopgain)
                    {
                        //Stop loss
                        bitMEXApi.CancelAllOpenOrders(pair);
                        String side = "Buy";
                        if (positionContracts > 0)
                        {
                            side = "Sell";
                        }
                        bitMEXApi.MarketOrder(pair, side, positionContracts);
                    }
                }

                //SEARCH POSITION AND MAKE ORDER
                //By Carlos Morato
                if (roeAutomatic && (Math.Abs(getOpenOrderQty()) < Math.Abs(positionContracts)))
                {
                    log("Get Position " + positionContracts);

                    int qntContacts = (Math.Abs(positionContracts) - Math.Abs(getOpenOrderQty()));


                    if (positionContracts > 0)
                    {
                        string side                = "Sell";
                        double priceContacts       = Math.Abs(getPositionPrice());
                        double actualPrice         = Math.Abs(getPriceActual(side));
                        double priceContactsProfit = Math.Abs(Math.Floor(priceContacts + (priceContacts * (profit + fee) / 100)));

                        if (actualPrice > priceContactsProfit)
                        {
                            double     price       = actualPrice + 1;
                            String     json        = bitMEXApi.PostOrderPostOnly(pair, side, price, qntContacts);
                            JContainer jCointaner2 = (JContainer)JsonConvert.DeserializeObject(json, (typeof(JContainer)));
                            log(json);
                        }
                        else
                        {
                            double     price       = priceContactsProfit;
                            String     json        = bitMEXApi.PostOrderPostOnly(pair, side, price, qntContacts);
                            JContainer jCointaner2 = (JContainer)JsonConvert.DeserializeObject(json, (typeof(JContainer)));
                            log(json);
                        }
                    }

                    if (positionContracts < 0)
                    {
                        string side                = "Buy";
                        double priceContacts       = Math.Abs(getPositionPrice());
                        double actualPrice         = Math.Abs(getPriceActual(side));
                        double priceContactsProfit = Math.Abs(Math.Floor(priceContacts - (priceContacts * (profit + fee) / 100)));

                        if (actualPrice < priceContactsProfit)
                        {
                            double     price       = actualPrice - 1;
                            String     json        = bitMEXApi.PostOrderPostOnly(pair, side, price, qntContacts);
                            JContainer jCointaner2 = (JContainer)JsonConvert.DeserializeObject(json, (typeof(JContainer)));
                            log(json);
                        }
                        else
                        {
                            double     price       = priceContactsProfit;
                            String     json        = bitMEXApi.PostOrderPostOnly(pair, side, price, qntContacts);
                            JContainer jCointaner2 = (JContainer)JsonConvert.DeserializeObject(json, (typeof(JContainer)));
                            log(json);
                        }
                    }
                }



                //CANCEL ORDER WITHOUT POSITION
                //By Carlos Morato

                if (roeAutomatic && Math.Abs(positionContracts) != Math.Abs(getOpenOrderQty()))
                {
                    bitMEXApi.CancelAllOpenOrders(pair);
                }

                if (automaticTendency)
                {
                    verifyTendency();
                }
                //GET CANDLES
                if (getCandles())
                {
                    if (statusLong == "enable")
                    {
                        log("");
                        log("==========================================================");
                        log(" ==================== Verify LONG OPERATION =============", ConsoleColor.Green);
                        log("==========================================================");
                        /////VERIFY OPERATION LONG
                        string operation = "buy";
                        //VERIFY INDICATORS ENTRY
                        foreach (var item in lstIndicatorsEntry)
                        {
                            Operation operationBuy = item.GetOperation(arrayPriceOpen, arrayPriceClose, arrayPriceLow, arrayPriceHigh, arrayPriceVolume);
                            log("Indicator: " + item.getName());
                            log("Result1: " + item.getResult());
                            log("Result2: " + item.getResult2());
                            log("Operation: " + operationBuy.ToString());
                            log("");
                            if (operationBuy != Operation.buy)
                            {
                                operation = "nothing";
                                break;
                            }
                        }

                        //VERIFY INDICATORS CROSS
                        if (operation == "buy")
                        {
                            //Prepare to long
                            while (true)
                            {
                                log("wait operation long...");
                                getCandles();
                                foreach (var item in lstIndicatorsEntryCross)
                                {
                                    Operation operationBuy = item.GetOperation(arrayPriceOpen, arrayPriceClose, arrayPriceLow, arrayPriceHigh, arrayPriceVolume);
                                    log("Indicator Cross: " + item.getName());
                                    log("Result1: " + item.getResult());
                                    log("Result2: " + item.getResult2());
                                    log("Operation: " + operationBuy.ToString());
                                    log("");

                                    if (item.getTypeIndicator() == TypeIndicator.Cross)
                                    {
                                        if (operationBuy == Operation.buy)
                                        {
                                            operation = "long";
                                            break;
                                        }
                                    }
                                    else if (operationBuy != Operation.buy)
                                    {
                                        operation = "long";
                                        break;
                                    }
                                }
                                if (lstIndicatorsEntryCross.Count == 0)
                                {
                                    operation = "long";
                                }
                                if (operation != "buy")
                                {
                                    break;
                                }
                                log("wait " + interval + "ms");
                                Thread.Sleep(interval);
                            }
                        }

                        //VERIFY INDICATORS DECISION
                        if (operation == "long" && lstIndicatorsEntryDecision.Count > 0)
                        {
                            operation = "decision";
                            foreach (var item in lstIndicatorsEntryDecision)
                            {
                                Operation operationBuy = item.GetOperation(arrayPriceOpen, arrayPriceClose, arrayPriceLow, arrayPriceHigh, arrayPriceVolume);
                                log("Indicator Decision: " + item.getName());
                                log("Result1: " + item.getResult());
                                log("Result2: " + item.getResult2());
                                log("Operation: " + operationBuy.ToString());
                                log("");


                                if (getValue("indicatorsEntryDecision", item.getName(), "decision") == "enable" && getValue("indicatorsEntryDecision", item.getName(), "tendency") == "enable")
                                {
                                    int decisionPoint = int.Parse(getValue("indicatorsEntryDecision", item.getName(), "decisionPoint"));
                                    if (item.getResult() >= decisionPoint && item.getTendency() == Tendency.high)
                                    {
                                        operation = "long";
                                        break;
                                    }
                                }

                                if (getValue("indicatorsEntryDecision", item.getName(), "decision") == "enable")
                                {
                                    int decisionPoint = int.Parse(getValue("indicatorsEntryDecision", item.getName(), "decisionPoint"));
                                    if (item.getResult() >= decisionPoint)
                                    {
                                        operation = "long";
                                        break;
                                    }
                                }
                                if (getValue("indicatorsEntryDecision", item.getName(), "tendency") == "enable")
                                {
                                    if (item.getTendency() == Tendency.high)
                                    {
                                        operation = "long";
                                        break;
                                    }
                                }
                            }
                        }


                        //EXECUTE OPERATION
                        if (operation == "long")
                        {
                            makeOrder("Buy");
                        }

                        ////////////FINAL VERIFY OPERATION LONG//////////////////
                    }


                    if (statusShort == "enable")
                    {
                        //////////////////////////////////////////////////////////////
                        log("");
                        log("==========================================================");
                        log(" ==================== Verify SHORT OPERATION =============", ConsoleColor.Red);
                        log("==========================================================");
                        /////VERIFY OPERATION LONG
                        string operation = "sell";
                        //VERIFY INDICATORS ENTRY
                        foreach (var item in lstIndicatorsEntry)
                        {
                            Operation operationBuy = item.GetOperation(arrayPriceOpen, arrayPriceClose, arrayPriceLow, arrayPriceHigh, arrayPriceVolume);
                            log("Indicator: " + item.getName());
                            log("Result1: " + item.getResult());
                            log("Result2: " + item.getResult2());
                            log("Operation: " + operationBuy.ToString());
                            log("");
                            if (operationBuy != Operation.sell)
                            {
                                operation = "nothing";
                                break;
                            }
                        }

                        //VERIFY INDICATORS CROSS
                        if (operation == "sell")
                        {
                            //Prepare to long
                            while (true)
                            {
                                log("wait operation short...");
                                getCandles();
                                foreach (var item in lstIndicatorsEntryCross)
                                {
                                    Operation operationBuy = item.GetOperation(arrayPriceOpen, arrayPriceClose, arrayPriceLow, arrayPriceHigh, arrayPriceVolume);
                                    log("Indicator Cross: " + item.getName());
                                    log("Result1: " + item.getResult());
                                    log("Result2: " + item.getResult2());
                                    log("Operation: " + operationBuy.ToString());
                                    log("");

                                    if (item.getTypeIndicator() == TypeIndicator.Cross)
                                    {
                                        if (operationBuy == Operation.sell)
                                        {
                                            operation = "short";
                                            break;
                                        }
                                    }
                                    else if (operationBuy != Operation.sell)
                                    {
                                        operation = "short";
                                        break;
                                    }
                                }
                                if (lstIndicatorsEntryCross.Count == 0)
                                {
                                    operation = "short";
                                }
                                if (operation != "sell")
                                {
                                    break;
                                }
                                log("wait " + interval + "ms");
                                Thread.Sleep(interval);
                            }
                        }

                        //VERIFY INDICATORS DECISION
                        if (operation == "short" && lstIndicatorsEntryDecision.Count > 0)
                        {
                            operation = "decision";
                            foreach (var item in lstIndicatorsEntryDecision)
                            {
                                Operation operationBuy = item.GetOperation(arrayPriceOpen, arrayPriceClose, arrayPriceLow, arrayPriceHigh, arrayPriceVolume);
                                log("Indicator Decision: " + item.getName());
                                log("Result1: " + item.getResult());
                                log("Result2: " + item.getResult2());
                                log("Operation: " + operationBuy.ToString());
                                log("");


                                if (getValue("indicatorsEntryDecision", item.getName(), "decision") == "enable" && getValue("indicatorsEntryDecision", item.getName(), "tendency") == "enable")
                                {
                                    int decisionPoint = int.Parse(getValue("indicatorsEntryDecision", item.getName(), "decisionPoint"));
                                    if (item.getResult() <= decisionPoint && item.getTendency() == Tendency.low)
                                    {
                                        operation = "short";
                                        break;
                                    }
                                }

                                if (getValue("indicatorsEntryDecision", item.getName(), "decision") == "enable")
                                {
                                    int decisionPoint = int.Parse(getValue("indicatorsEntryDecision", item.getName(), "decisionPoint"));
                                    if (item.getResult() <= decisionPoint)
                                    {
                                        operation = "short";
                                        break;
                                    }
                                }
                                if (getValue("indicatorsEntryDecision", item.getName(), "tendency") == "enable")
                                {
                                    if (item.getTendency() == Tendency.low)
                                    {
                                        operation = "short";
                                        break;
                                    }
                                }
                            }
                        }


                        //EXECUTE OPERATION
                        if (operation == "short")
                        {
                            makeOrder("Sell");
                        }

                        ////////////FINAL VERIFY OPERATION LONG//////////////////
                    }
                }
                log("wait " + interval + "ms", ConsoleColor.Blue);
                Thread.Sleep(interval);
            }
        }
        catch (Exception ex)
        {
            log("ERROR FATAL::" + ex.Message + ex.StackTrace);
        }
    }
Example #3
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);
        }
    }
Example #4
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);
        }
    }
Example #5
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);
        }
    }