/// <summary> /// Função principal de loop /// </summary> /// <param name="obj"></param> private static void triangularBinanceDetail(object obj) { Logger.log("START " + obj.ToString()); String[] pairs = obj.ToString().Split(':'); while (true) { try { ArbTriangle ret = verifyOrderBook(pairs, initialValue); bool insert = true; for (int x = 0; x < dsSearch.Tables["Symbol"].Rows.Count; x++) { if (dsSearch.Tables["Symbol"].Rows[x][0].ToString().ToLower().Trim() == obj.ToString().ToLower().Trim()) { insert = false; } } if (insert) { dsSearch.Tables["Symbol"].Rows.Add(obj.ToString()); Console.Title = dsSearch.Tables[0].Rows.Count.ToString() + " PARES RASTREADOS"; Logger.trade(obj.ToString()); } if (ret.perc > percValue) { Console.BackgroundColor = ConsoleColor.Blue; Console.WriteLine(" -- -- BAITING THE FISH!! -- -- "); lock (objLockOrders) { if (pairs[2] == "ETHBTC" || pairs[2] == "BNBBTC") { WebCallResult <BinancePlacedOrder> resultBuy; using (var bClient = new BinanceClient(options: new BinanceClientOptions() { RequestTimeout = TimeSpan.FromSeconds(3) })) { bClient.SetApiCredentials(Environment.GetEnvironmentVariable("binanceApiKey"), Environment.GetEnvironmentVariable("binanceApiSecret")); Console.WriteLine($" VOU EXECUTAR O BCLIENT PLACE ORDER VALOR :{ret.amount1}"); resultBuy = bClient.PlaceOrder(pairs[0], OrderSide.Buy, OrderType.Market, ret.amount1); } //var resultBuy = binanceClient.PlaceOrder(pairs[0], OrderSide.Buy, OrderType.Market, ret.amount1); Logger.log($"Tentativa de Primeira compra {pairs[0]}, {ret.amount1}"); if (resultBuy.Success) { Console.WriteLine("|| -- -- FIRST ACT COMPLETE!!! -- -- >>"); Logger.log($"Primeira compra com sucesso!"); var resultSell = binanceClient.PlaceOrder(pairs[1], OrderSide.Sell, OrderType.Market, ret.amount1); Logger.log($"Tentativa de Primeira Venda {pairs[1]}, {ret.amount1}"); if (resultSell.Success) { Console.WriteLine("|| -- -- SECOND ACT COMPLETE!!! -- -- >>"); Logger.log($"Segunda venda com sucesso!"); var resultSell2 = binanceClient.PlaceOrder(pairs[2], OrderSide.Sell, OrderType.Market, ret.amount2); Logger.log($"Tentativa de Segunda Venda {pairs[2]}, {ret.amount2}"); if (resultSell2.Success) { String obs = pairs[2].Replace("BTC", "") + " | " + ret.perc + Environment.NewLine + "Buy " + pairs[0] + " " + ret.amount1 + " " + Environment.NewLine + " Change " + pairs[1] + " " + ret.amount2 + " " + Environment.NewLine + " Sell " + pairs[2] + " " + ret.amount2 + Environment.NewLine + " Initial " + initialValue + " Final " + ret.finalvalue + " perc " + Math.Round(ret.perc, 8) + Environment.NewLine; Logger.triangle(obs); Console.WriteLine("<< -- -- FISHING COMPLETE!!! -- -- ||"); } else { Console.WriteLine("|| -- -- FINAL FISHING FAIL!!! -- -- >>"); break; } } else { Console.WriteLine("|| -- -- SECOND ACT FAIL!!! -- -- >>"); break; } } else { Logger.log($"Error ao tentar comprar 1x: {resultBuy.Error}"); Console.WriteLine("|| -- -- FIRST ACT FAIL!!! -- -- \\"); Console.WriteLine($"Error: {resultBuy.Error}"); break; } } else if (pairs[2] == "BTCUSDT" || pairs[2] == "BTCPAX") { WebCallResult <BinancePlacedOrder> resultBuy; using (var bClient = new BinanceClient(options: new BinanceClientOptions() { RequestTimeout = TimeSpan.FromSeconds(3) })) { bClient.SetApiCredentials(Environment.GetEnvironmentVariable("binanceApiKey"), Environment.GetEnvironmentVariable("binanceApiSecret")); Console.WriteLine($" VOU EXECUTAR O BCLIENT PLACE ORDER VALOR :{ret.amount1}"); resultBuy = bClient.PlaceOrder(pairs[0], OrderSide.Buy, OrderType.Market, ret.amount1); } Logger.log($"Tentativa de Primeira compra {pairs[0]}, {ret.amount1}"); if (resultBuy.Success) { Console.WriteLine("|| -- -- FIRST ACT COMPLETE!!! -- -- >>"); Logger.log($"Primeira compra com sucesso!"); var resultSell = binanceClient.PlaceOrder(pairs[1], OrderSide.Sell, OrderType.Market, ret.amount1); Logger.log($"Tentativa de Primeira Venda {pairs[1]}, {ret.amount1}"); if (resultSell.Success) { Console.WriteLine("|| -- -- SECOND ACT COMPLETE!!! -- -- >>"); Logger.log($"Segunda venda com sucesso!"); var resultSell2 = binanceClient.PlaceOrder(pairs[2], OrderSide.Sell, OrderType.Market, ret.amount2); Logger.log($"Tentativa de Segunda Venda {pairs[2]}, {ret.amount2}"); if (resultSell2.Success) { String obs = pairs[2].Replace("BTC", "") + " | " + ret.perc + Environment.NewLine + "Buy " + pairs[0] + " " + ret.amount1 + " " + Environment.NewLine + " Change " + pairs[1] + " " + ret.amount2 + " " + Environment.NewLine + " Sell " + pairs[2] + " " + ret.amount2 + Environment.NewLine + " Initial " + initialValue + " Final " + ret.finalvalue + " perc " + Math.Round(ret.perc, 8) + Environment.NewLine; Logger.triangle(obs); Console.WriteLine("<< -- -- FISHING COMPLETE!!! -- -- ||"); } else { Logger.log($"Error To Final Sell: {resultSell2.Error}"); Console.WriteLine($"Error: {resultSell2.Error}"); Console.WriteLine("|| -- -- FINAL FISHING FAIL!!! -- -- >>"); break; } } else { Logger.log($"Error ao tentar Vender 1x: {resultSell.Error}"); Console.WriteLine("|| -- -- SECOND ACT FAIL!!! -- -- \\"); Console.WriteLine($"Error: {resultSell.Error}"); break; } } else { Logger.log($"Error ao tentar comprar 1x: {resultBuy.Error}"); Console.WriteLine("|| -- -- FIRST ACT FAIL!!! -- -- \\"); Console.WriteLine($"Error: {resultBuy.Error}"); break; } } else { WebCallResult <BinancePlacedOrder> resultBuy; using (var bClient = new BinanceClient(options: new BinanceClientOptions() { RequestTimeout = TimeSpan.FromSeconds(3) })) { Console.WriteLine($" VOU EXECUTAR O BCLIENT PLACE ORDER VALOR :{ret.amount1}"); bClient.SetApiCredentials(Environment.GetEnvironmentVariable("binanceApiKey"), Environment.GetEnvironmentVariable("binanceApiSecret")); resultBuy = bClient.PlaceOrder(pairs[0], OrderSide.Buy, OrderType.Market, ret.amount1); } Logger.log($"Tentativa de Primeira compra {pairs[0]}, {ret.amount1}"); if (resultBuy.Success) { Console.WriteLine("|| -- -- FIRST ACT COMPLETE!!! -- -- >>"); Logger.log($"Primeira compra com sucesso!"); var resultSell = binanceClient.PlaceOrder(pairs[1], OrderSide.Sell, OrderType.Market, ret.amount1); Logger.log($"Tentativa de Primeira Venda {pairs[1]}, {ret.amount1}"); if (resultSell.Success) { Console.WriteLine("|| -- -- SECOND ACT COMPLETE!!! -- -- >>"); Logger.log($"Segunda venda com sucesso!"); var resultSell2 = binanceClient.PlaceOrder(pairs[2], OrderSide.Sell, OrderType.Market, ret.amount2); Logger.log($"Tentativa de Segunda Venda {pairs[2]}, {ret.amount2}"); if (resultSell2.Success) { String obs = pairs[2].Replace("BTC", "") + " | " + ret.perc + Environment.NewLine + "Buy " + pairs[0] + " " + ret.amount1 + " " + Environment.NewLine + " Change " + pairs[1] + " " + ret.amount2 + " " + Environment.NewLine + " Sell " + pairs[2] + " " + ret.amount2 + Environment.NewLine + " Initial " + initialValue + " Final " + ret.finalvalue + " perc " + Math.Round(ret.perc, 8) + Environment.NewLine; Logger.triangle(obs); Console.WriteLine("<< -- -- FISHING COMPLETE!!! -- -- ||"); } else { Logger.log($"Error ao tentar vender 2x: {resultSell2.Error}"); Console.WriteLine("|| -- -- FINAL ACT FAIL!!! -- -- \\"); Console.WriteLine($"Error: {resultSell2.Error}"); break; } } else { Logger.log($"Error ao tentar vender 1x: {resultSell.Error}"); Console.WriteLine("|| -- -- SECOND ACT FAIL!!! -- -- \\"); Console.WriteLine($"Error: {resultSell.Error}"); break; } } else { Logger.log($"Error ao tentar comprar 1x: {resultBuy.Error}"); Console.WriteLine("|| -- -- FIRST ACT FAIL!!! -- -- \\"); Console.WriteLine($"Error: {resultBuy.Error}"); break; } } } } } catch (Exception ex) { System.Threading.Thread.Sleep(1000); //Logger.log(ex.Message + ex.StackTrace + "||" + obj.ToString()); } } }
/// <summary> /// Calculo com o Order book /// </summary> /// <param name="pairs"></param> /// <param name="initialValue"></param> /// <returns></returns> private static ArbTriangle verifyOrderBook(string[] pairs, decimal initialValue) { try { ArbTriangle arbTriangle = new ArbTriangle(); arbTriangle.pair1 = pairs[0]; arbTriangle.pair2 = pairs[1]; arbTriangle.pair3 = pairs[2]; if (pairs[2] == "ETHBTC" || pairs[2] == "BNBBTC") { //EOSBTC BUY //EOSETH SELL //EOSBTC SELL //BUY arbTriangle.amount1 = Math.Round(exchangeBinance.getBook(pairs[0], initialValue, "asks", "buy"), 4); //CHANGE arbTriangle.amount2 = Math.Round(exchangeBinance.getBook(pairs[1], arbTriangle.amount1, "bids", "sell", false), 4); //SELL arbTriangle.finalvalue = Math.Round(exchangeBinance.getBook(pairs[2], arbTriangle.amount2, "bids", "sell", false), 4); //Report decimal perc = Math.Round((((arbTriangle.finalvalue * 100) / initialValue) - 100), 5); arbTriangle.perc = perc; //if (perc > -1) //{ // Console.WriteLine(Math.Round(perc, 2) + "% | " + pairs[0].ToString() + "(" + arbTriangle.amount1 + ") - " + pairs[1].ToString() + "(" + arbTriangle.amount2 + ") - " + pairs[2].ToString() + "(" + arbTriangle.finalvalue + ")"); //} return(arbTriangle); } else if (pairs[2] == "BTCUSDT" || pairs[2] == "BTCPAX" || pairs[2] == "BTCTUSD" || pairs[2] == "BTCUSDC") { //XRPBTC BUY //XRPUSDT SELL //BTCUSDT BUY //BUY arbTriangle.amount1 = Math.Round(exchangeBinance.getBook(pairs[0], initialValue, "asks", "buy"), 4); //CHANGE arbTriangle.amount2 = Math.Round(exchangeBinance.getBook(pairs[1], arbTriangle.amount1, "bids", "sell", false), 4); //SELL arbTriangle.finalvalue = Math.Round(exchangeBinance.getBook(pairs[2], arbTriangle.amount2, "asks", "buy"), 4); //Report decimal perc = Math.Round((((arbTriangle.finalvalue * 100) / initialValue) - 100), 5); arbTriangle.perc = perc; //if (perc > -1) //{ // Console.WriteLine(Math.Round(perc, 2) + "% | " + pairs[0].ToString() + "(" + arbTriangle.amount1 + ") - " + pairs[1].ToString() + "(" + arbTriangle.amount2 + ") - " + pairs[2].ToString() + "(" + arbTriangle.finalvalue + ")"); //} return(arbTriangle); } else { //ETHBTC BUY //XRPETH BUY //XRPBTC SELL //BUY arbTriangle.amount1 = Math.Round(exchangeBinance.getBook(pairs[0], initialValue, "asks", "buy"), 4); //CHANGE arbTriangle.amount2 = Math.Round(exchangeBinance.getBook(pairs[1], arbTriangle.amount1, "asks", "buy"), 4); //SELL arbTriangle.finalvalue = Math.Round(exchangeBinance.getBook(pairs[2], arbTriangle.amount2, "bids", "sell", false), 4); //Report decimal perc = Math.Round((((arbTriangle.finalvalue * 100) / initialValue) - 100), 5); arbTriangle.perc = perc; //if (perc > -1) //{ // Console.WriteLine(Math.Round(perc, 2) + "% | " + pairs[0].ToString() + "(" + arbTriangle.amount1 + ") - " + pairs[1].ToString() + "(" + arbTriangle.amount2 + ") - " + pairs[2].ToString() + "(" + arbTriangle.finalvalue + ")"); //} return(arbTriangle); } } catch { //Console.WriteLine("ERROR BOOK!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); throw new Exception("Erro book"); } }
/// <summary> /// Função principal de loop /// </summary> /// <param name="obj"></param> static void triangularBinanceDetail(object obj) { Logger.log("START " + obj.ToString()); String[] pairs = obj.ToString().Split(':'); while (true) { try { ArbTriangle ret = verifyOrderBook(pairs, initialValue); if (ret.perc > 0) { Logger.log("************ || " + obj.ToString() + "||" + ret.perc + "||" + DateTime.Now.ToString()); } bool insert = true; for (int x = 0; x < dsSearch.Tables["Symbol"].Rows.Count; x++) { if (dsSearch.Tables["Symbol"].Rows[x][0].ToString().ToLower().Trim() == obj.ToString().ToLower().Trim()) { insert = false; } } if (insert) { dsSearch.Tables["Symbol"].Rows.Add(obj.ToString()); Console.Title = dsSearch.Tables[0].Rows.Count.ToString() + " search pairs"; Logger.trade(obj.ToString()); } if (ret.perc > percValue && ok) { Console.BackgroundColor = ConsoleColor.Green; //System.Threading.Thread.Sleep(1000); //decimal[] ret = verifyOrderBook(pairs, initialValue); //if (ret[0] > ret[0] && ok) { //ok = false; //lock (objLockOrders) { //if (ok) { bool t1 = false, t2 = false, t3 = false; if (pairs[2] == "ETHBTC" || pairs[2] == "BNBBTC") { string json = ""; //new Thread(() => //{ // binance.orderMarket("buy", pairs[0], ret.amount1, false, true); //}).Start(); //Thread t = new Thread(() => //{ // json = binance.orderMarket("sell", pairs[1], ret.amount1, false, true); //}); //t.Start(); //new Thread(() => //{ // binance.orderMarket("sell", pairs[2], ret.amount2); //}).Start(); t1 = true; t2 = true; t3 = true; } else if (pairs[2] == "BTCUSDT" || pairs[2] == "BTCPAX") { //new Thread(() => //{ // string json = binance.orderMarket("buy", pairs[0], ret.amount1,false,true); // t1 = true; //}).Start(); //new Thread(() => //{ // string json = binance.orderMarket("sell", pairs[1], ret.amount2Fee, true); // t2 = true; //}).Start(); //new Thread(() => //{ // string json = binance.orderMarket("buy", pairs[2], ret.amount2Fee, true); // t3 = true; //}).Start(); } else { // //new Thread(() => //{ // binance.orderMarket("buy", pairs[0], ret.amount1, false, true); //}).Start(); //Thread t = new Thread(() => //{ // binance.orderMarket("buy", pairs[1], ret.amount2); //}); //t.Start(); //Thread at = new Thread(() => //{ // binance.orderMarket("sell", pairs[2], ret.amount2); //}); //at.Start(); t1 = true; t2 = true; t3 = true; // new Thread(() => //{ // string json = binance.orderMarket("buy", pairs[0], ret.amount1,false,true); // t1 = true; //}).Start(); // Thread.Sleep(500); // new Thread(() => // { // string json = binance.orderMarket("buy", pairs[1], ret.amount2Fee, true); // t2 = true; // }).Start(); // Thread.Sleep(500); // new Thread(() => // { // string json = binance.orderMarket("sell", pairs[2], ret.amount2Fee, true); // t3 = true; // }).Start(); } //string json = binance.order("buy", pairs[0], amountBuy, buy[0], true, "FOK"); //Task taskAa = Task.Run(() => binance.orderMarket("buy", pairs[0], amountBuy)); //Newtonsoft.Json.Linq.JContainer jContainer = (Newtonsoft.Json.Linq.JContainer)JsonConvert.DeserializeObject(json); //Logger.triangle(json); //if (jContainer["status"].ToString().Trim().ToUpper() == "FILLED") { //jContainer = (Newtonsoft.Json.Linq.JContainer)JsonConvert.DeserializeObject(json); //amountBuy = decimal.Parse(jContainer["executedQty"].ToString().Replace(".", ",")); //json = binance.orderMarket("buy", pairs[1], amountBuy / change[0]); //Task taskA = Task.Run(() => binance.orderMarket("buy", pairs[1], amountChange)); //Logger.triangle(json); //jContainer = (Newtonsoft.Json.Linq.JContainer)JsonConvert.DeserializeObject(json); //if (json.IndexOf("FILLED") >= 0) { //jContainer = (Newtonsoft.Json.Linq.JContainer)JsonConvert.DeserializeObject(json); //amountChange = decimal.Parse(jContainer["executedQty"].ToString().Replace(".", ",")); //json = binance.orderMarket("sell", pairs[2], amountChange); //Task taskB = Task.Run(() => binance.orderMarket("sell", pairs[2], amountChange)); //Logger.triangle(json); bool wait = true; while (wait) { if (t1) { if (t2) { if (t3) { wait = false; } } } System.Threading.Thread.Sleep(500); } String obs = pairs[2].Replace("BTC", "") + " | " + ret.perc + Environment.NewLine + "Buy " + pairs[0] + " " + ret.amount1 + " " + Environment.NewLine + " Change " + pairs[1] + " " + ret.amount2 + " " + Environment.NewLine + " Sell " + pairs[2] + " " + ret.amount2 + Environment.NewLine + " Initial " + initialValue + " Final " + ret.finalvalue + " perc " + Math.Round(ret.perc, 8) + Environment.NewLine; Logger.triangle(obs); for (int i = 0; i < 50; i++) { System.Threading.Thread.Sleep(500); } //System.Diagnostics.Process.Start(@"C:\programas\RobotArbitrage\bin\Debug\RobotArbitrage.exe"); //Environment.Exit(0); //return; System.Threading.Thread.Sleep(120000); } } } } System.Threading.Thread.Sleep(60000); } } } catch (Exception ex) { System.Threading.Thread.Sleep(1000); //Logger.log(ex.Message + ex.StackTrace + "||" + obj.ToString()); } System.Threading.Thread.Sleep(10); } }