public static Instrument ConvertInstrument(BinanceSymbol instrument) { return new Instrument(instrument.Name, Convert.ToDecimal((double)(instrument.LotSizeFilter.StepSize)), ExchangeName.Binance, active: true); }
private static void ExecuteOrder(string symbol, decimal quantity, decimal takeProfitRate, decimal stopLossRate, decimal limitPriceRate) { string pair = symbol.ToUpper() + "BTC"; WebCallResult <BinanceBookPrice> priceResult = client.Spot.Market.GetBookPrice(pair); if (priceResult.Success) { Utilities.Write(ConsoleColor.Green, $"Price for {pair} is {priceResult.Data.BestAskPrice}"); BinanceSymbol symbolInfo = exchangeInfo.Data.Symbols.FirstOrDefault(s => s.QuoteAsset == "BTC" && s.BaseAsset == symbol.ToUpper()); if (symbolInfo == null) { Utilities.Write(ConsoleColor.Red, $"ERROR! Could not get symbol informations."); return; } int symbolPrecision = symbolInfo.BaseAssetPrecision; Utilities.Write(ConsoleColor.Green, $"Asset precision: {symbolPrecision}"); //Place Market Order WebCallResult <BinancePlacedOrder> order = client.Spot.Order.PlaceOrder(pair, OrderSide.Buy, OrderType.Market, null, quantity); if (!order.Success) { Utilities.Write(ConsoleColor.Red, $"ERROR! Could not place the Market order. Error code: " + order.Error?.Message); return; } //Get the filled order average price decimal paidPrice = 0; if (order.Data.Fills != null) { paidPrice = order.Data.Fills.Average(trade => trade.Price); } decimal orderQuantity = order.Data.QuantityFilled; Utilities.Write(ConsoleColor.Green, $"Order submitted, Got: {orderQuantity} coins from {pair} at {paidPrice}"); decimal sellPrice = Math.Round(paidPrice * stopLossRate, symbolPrecision); decimal triggerPrice = Math.Round(paidPrice * limitPriceRate, symbolPrecision); decimal limit = Math.Round(paidPrice * takeProfitRate, symbolPrecision); WebCallResult <BinanceOrderOcoList> ocoOrder = client.Spot.Order.PlaceOcoOrder(pair, OrderSide.Sell, orderQuantity, limit, triggerPrice, sellPrice, stopLimitTimeInForce: TimeInForce.GoodTillCancel); if (!ocoOrder.Success) { Utilities.Write(ConsoleColor.Red, $"OCO order failed, Error code: {ocoOrder.Error?.Message}"); return; } Utilities.Write(ConsoleColor.Green, $"OCO Order submitted, sell price: {limit}, stop price: {triggerPrice}, stop limit price: {sellPrice}"); } else { Utilities.Write(ConsoleColor.Red, $"ERROR! Could not get price for pair: {pair}. Error code: {priceResult.Error?.Message}"); } }
private void InitRequiredLP() { _requiredLP = new List <LiquidityProvider>(); if (!SoftFxSymbol.Equals("")) { _requiredLP.Add(LiquidityProvider.SoftFx); } if (!LivecoinSymbol.Equals("")) { _requiredLP.Add(LiquidityProvider.Livecoin); } if (!TidexSymbol.Equals("")) { _requiredLP.Add(LiquidityProvider.Tidex); } if (!OkexSymbol.Equals("")) { _requiredLP.Add(LiquidityProvider.Okex); } if (!BinanceSymbol.Equals("")) { _requiredLP.Add(LiquidityProvider.Binance); } if (!BitfinexSymbol.Equals("")) { _requiredLP.Add(LiquidityProvider.Bitfinex); } if (!HitBtcSymbol.Equals("")) { _requiredLP.Add(LiquidityProvider.HitBtc); } if (!KrakenSymbol.Equals("")) { _requiredLP.Add(LiquidityProvider.Kraken); } if (!KucoinSymbol.Equals("")) { _requiredLP.Add(LiquidityProvider.Kucoin); } if (!HuobiSymbol.Equals("")) { _requiredLP.Add(LiquidityProvider.Huobi); } }
private static void ExecuteOrder(string symbol, decimal quantity, decimal takeProfitRate, decimal stopLossRate, decimal limitPriceRate) { string pair = symbol.ToUpper() + "BTC"; Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); WebCallResult <BinancePlacedOrder> order = client.Spot.Order.PlaceOrder(pair, OrderSide.Buy, OrderType.Market, null, quantity); stopWatch.Stop(); var timestamp = DateTime.Now.ToFileTime(); TimeSpan ts = stopWatch.Elapsed; decimal orderQuantity = order.Data.QuantityFilled; decimal paidPrice = 0; if (order.Data.Fills != null) { paidPrice = order.Data.Fills.Average(trade => trade.Price); } string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:000}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds); Utilities.Write(ConsoleColor.Green, $"Order submitted and accepted, Got: {orderQuantity} coins from {pair} at {paidPrice}, time: + {elapsedTime} ms"); BinanceSymbol symbolInfo = exchangeInfo.Data.Symbols.FirstOrDefault(s => s.QuoteAsset == "BTC" && s.BaseAsset == symbol); if (symbolInfo == null) { Utilities.Write(ConsoleColor.Red, $"ERROR! Could not get symbol informations."); return; } int symbolPrecision = 1; decimal ticksize = symbolInfo.PriceFilter.TickSize; while ((ticksize = ticksize * 10) < 1) { ++symbolPrecision; } decimal sellPrice = Math.Round(paidPrice * stopLossRate, symbolPrecision); decimal triggerPrice = Math.Round(paidPrice * limitPriceRate, symbolPrecision); decimal limit = Math.Round(paidPrice * takeProfitRate, symbolPrecision); WebCallResult <BinanceOrderOcoList> ocoOrder = client.Spot.Order.PlaceOcoOrder(pair, OrderSide.Sell, orderQuantity, limit, triggerPrice, sellPrice, stopLimitTimeInForce: TimeInForce.GoodTillCancel); if (!ocoOrder.Success) { Utilities.Write(ConsoleColor.Red, $"OCO order failed, Error code: {ocoOrder.Error?.Message}"); return; } Utilities.Write(ConsoleColor.Green, $"OCO Order submitted, sell price: {limit}, stop price: {triggerPrice}, stop limit price: {sellPrice}"); }
public Trade(BinanceStreamOrderUpdate binanceOrder, BinanceSymbol binanceSymbol) { Timestamp = binanceOrder.UpdateTime; ReferenceID = binanceOrder.OrderId.ToString(); if (binanceOrder.Side == Binance.Net.Enums.OrderSide.Buy) { BoughtAsset = binanceSymbol.BaseAsset; BoughtQuantity = binanceOrder.QuantityFilled; SoldAsset = binanceSymbol.QuoteAsset; SoldQuantity = binanceOrder.QuoteQuantityFilled; } else { SoldAsset = binanceSymbol.BaseAsset; SoldQuantity = binanceOrder.QuantityFilled; BoughtAsset = binanceSymbol.QuoteAsset; BoughtQuantity = binanceOrder.QuoteQuantityFilled; } }
public Trade(BinanceTrade binanceTrade, BinanceSymbol binanceSymbol) { Timestamp = binanceTrade.TradeTime; ReferenceID = binanceTrade.Id.ToString(); if (binanceTrade.IsBuyer) { BoughtAsset = binanceSymbol.BaseAsset; BoughtQuantity = binanceTrade.Quantity; SoldAsset = binanceSymbol.QuoteAsset; SoldQuantity = binanceTrade.QuoteQuantity; } else { SoldAsset = binanceSymbol.BaseAsset; SoldQuantity = binanceTrade.Quantity; BoughtAsset = binanceSymbol.QuoteAsset; BoughtQuantity = binanceTrade.QuoteQuantity; } }
private void DownloadTrades(List <BinanceTrade> trades, BinanceSymbol market) { try { Logger.LogDebug($"Getting History from {market.Name}"); var tradeResponse = Client.Spot.Order.GetMyTrades(market.Name); if (tradeResponse.Success) { trades.AddRange(tradeResponse.Data); } else { Logger.LogError(tradeResponse.Error?.ToString()); Thread.Sleep(3000); DownloadTrades(trades, market); } } catch (Exception ex) { Logger.LogDebug($"Getting History From {market.Name} Failed {ex.Message}"); } }
private static void ExecuteOrder( string symbol, Decimal quantity, Decimal strategyrisk, Decimal sellStrategy, Decimal maxsecondsbeforesell) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); using (BinanceClient client = new BinanceClient()) { string pair = symbol.ToUpper() + "BTC"; WebCallResult <BinancePlacedOrder> webCallResult1 = client.Spot.Order.PlaceOrder(pair, OrderSide.Buy, OrderType.Market, quoteOrderQuantity: new Decimal?(quantity)); if (!webCallResult1.Success) { Utilities.Write(ConsoleColor.Red, "ERROR! Could not place the Market order. Error code: " + webCallResult1.Error?.Message); } else { stopwatch.Stop(); long timestamp = DateTime.Now.ToFileTime(); TimeSpan elapsed = stopwatch.Elapsed; Console.WriteLine("RunTime " + string.Format("{0:00}:{1:00}:{2:00}.{3:00}", (object)elapsed.Hours, (object)elapsed.Minutes, (object)elapsed.Seconds, (object)(elapsed.Milliseconds / 10))); WebCallResult <BinanceExchangeInfo> exchangeInfo = client.Spot.System.GetExchangeInfo(); if (!exchangeInfo.Success) { Utilities.Write(ConsoleColor.Red, "ERROR! Could not exchange informations. Error code: " + exchangeInfo.Error?.Message); } else { BinanceSymbol binanceSymbol = exchangeInfo.Data.Symbols.FirstOrDefault <BinanceSymbol>((Func <BinanceSymbol, bool>)(s => s.QuoteAsset == "BTC" && s.BaseAsset == symbol.ToUpper())); if (binanceSymbol == null) { Utilities.Write(ConsoleColor.Red, "ERROR! Could not get symbol informations."); } else { int symbolPrecision = 1; Decimal tickSize = binanceSymbol.PriceFilter.TickSize; while ((tickSize *= 10M) < 1M) { ++symbolPrecision; } Decimal num1 = 0M; if (webCallResult1.Data.Fills != null) { num1 = webCallResult1.Data.Fills.Average <BinanceOrderTrade>((Func <BinanceOrderTrade, Decimal>)(trade => trade.Price)); } Decimal OrderQuantity = webCallResult1.Data.QuantityFilled; Utilities.Write(ConsoleColor.Green, string.Format("Order submitted, Got: {0} coins from {1} at {2}", (object)OrderQuantity, (object)pair, (object)num1)); Decimal sellPriceRiskRatio = 0.95M; Decimal StartSellStrategy = sellStrategy; Decimal MaxSellStrategy = 1M - (1M - sellStrategy) / 5M; Decimal volasellmax = 1M; Decimal currentstoploss = 0M; List <Decimal> tab = new List <Decimal>(); int count = -1; int x = 0; int usainsell = 0; int imincharge = 0; new Thread(new ThreadStart(NewThread)).Start(); WebCallResult <BinanceBookPrice> priceResult3 = client.Spot.Market.GetBookPrice(pair); new Thread(new ThreadStart(NewThread2)).Start(); WebCallResult <BinanceBookPrice> priceResult2 = client.Spot.Market.GetBookPrice(pair); while (sellStrategy <= MaxSellStrategy && sellStrategy >= StartSellStrategy && usainsell == 0) { if ((Decimal)timestamp + 10000000M * maxsecondsbeforesell > (Decimal)DateTime.Now.ToFileTime()) { priceResult2 = client.Spot.Market.GetBookPrice(pair); Decimal num2 = Math.Round(priceResult2.Data.BestBidPrice * sellStrategy, symbolPrecision); if (num2 > currentstoploss) { currentstoploss = num2; Decimal num3 = Math.Round(num2 * sellPriceRiskRatio, symbolPrecision); WebCallResult <IEnumerable <BinanceCancelledId> > webCallResult2 = client.Spot.Order.CancelAllOpenOrders(pair); if (!webCallResult2.Success) { if (x == 0) { x = 1; Utilities.Write(ConsoleColor.Red, "ERROR! Could not remove orders. Error code: " + webCallResult2.Error?.Message); } else { x = 2; Utilities.Write(ConsoleColor.Red, "StopLoss executed : " + webCallResult2.Error?.Message); break; } } else { Utilities.Write(ConsoleColor.Cyan, "Orders successfully removed."); } if (usainsell == 0) { WebCallResult <BinancePlacedOrder> webCallResult3 = client.Spot.Order.PlaceOrder(pair, OrderSide.Sell, OrderType.StopLossLimit, new Decimal?(OrderQuantity), price: new Decimal?(num3), timeInForce: new TimeInForce?(TimeInForce.GoodTillCancel), stopPrice: new Decimal?(num2)); if (!webCallResult3.Success) { Utilities.Write(ConsoleColor.Red, "ERROR! Could not place the StopLimit order. Error code: " + webCallResult3.Error?.Message); break; } Utilities.Write(ConsoleColor.DarkYellow, string.Format("StopLimit Order submitted, stop limit price: {0}, sell price: {1}", (object)num2, (object)num3)); } } } else { imincharge = 1; client.Spot.Order.CancelAllOpenOrders(pair); WebCallResult <BinancePlacedOrder> webCallResult2; for (webCallResult2 = client.Spot.Order.PlaceOrder(pair, OrderSide.Sell, OrderType.Limit, new Decimal?(OrderQuantity), price: new Decimal?(Math.Round(priceResult3.Data.BestBidPrice * sellPriceRiskRatio, symbolPrecision)), timeInForce: new TimeInForce?(TimeInForce.GoodTillCancel)); !webCallResult2.Success; webCallResult2 = client.Spot.Order.PlaceOrder(pair, OrderSide.Sell, OrderType.Limit, new Decimal?(OrderQuantity), price: new Decimal?(Math.Round(priceResult3.Data.BestBidPrice * sellPriceRiskRatio, symbolPrecision)), timeInForce: new TimeInForce?(TimeInForce.GoodTillCancel))) { Utilities.Write(ConsoleColor.Red, "ERROR! Could not place the Market order sell, trying another time. Error code: " + webCallResult2.Error?.Message); } usainsell = 1; Utilities.Write(ConsoleColor.Green, "UsainBot TIME SOLD successfully " + OrderQuantity.ToString() + " " + webCallResult2.Data.Symbol + " sold at " + priceResult3.Data.BestBidPrice.ToString()); break; } } void NewThread() { while ((Decimal)timestamp + maxsecondsbeforesell * 10000000M > (Decimal)DateTime.Now.ToFileTime() && x != 2) { ++count; priceResult2 = client.Spot.Market.GetBookPrice(pair); if (!priceResult2.Success) { break; } tab.Add(priceResult2.Data.BestBidPrice); Decimal num1; if (count % 10 == 0) { int index = count; Decimal num2 = 0M; int num3 = -1; while (--index > 0 && ++num3 < 10) { num2 += (tab[index] - tab[index - 1]) / 10M; } Decimal num4 = num2 / 3M; while (--index > 0 && ++num3 < 30) { num4 += (tab[index] - tab[index - 1]) / 30M; } Utilities.Write(ConsoleColor.Yellow, string.Format(" {0}", (object)Math.Round(num2 / priceResult2.Data.BestBidPrice * 100000M, 2))); Utilities.Write(ConsoleColor.Magenta, string.Format(" {0}", (object)Math.Round(num4 / priceResult2.Data.BestBidPrice * 100000M, 2))); Decimal d = (num4 - num2) / priceResult2.Data.BestBidPrice * 100000M; if (d > strategyrisk / 4M) { Utilities.Write(ConsoleColor.Red, string.Format(" negative volatility detected at a {0} ratio", (object)Math.Round(d, 2))); if (d < strategyrisk) { if (d > volasellmax) { volasellmax = d; sellStrategy = Math.Round(StartSellStrategy + (Decimal)Math.Pow((double)d / (double)strategyrisk, 1.5) * (MaxSellStrategy - StartSellStrategy), 3); } } else { if (usainsell != 0 || imincharge != 0) { break; } imincharge = 1; client.Spot.Order.CancelAllOpenOrders(pair); WebCallResult <BinancePlacedOrder> webCallResult; for (webCallResult = client.Spot.Order.PlaceOrder(pair, OrderSide.Sell, OrderType.Limit, new Decimal?(OrderQuantity), price: new Decimal?(Math.Round(priceResult2.Data.BestBidPrice * sellPriceRiskRatio, symbolPrecision)), timeInForce: new TimeInForce?(TimeInForce.GoodTillCancel)); !webCallResult.Success; webCallResult = client.Spot.Order.PlaceOrder(pair, OrderSide.Sell, OrderType.Limit, new Decimal?(OrderQuantity), price: new Decimal?(Math.Round(priceResult2.Data.BestBidPrice * sellPriceRiskRatio, symbolPrecision)), timeInForce: new TimeInForce?(TimeInForce.GoodTillCancel))) { Utilities.Write(ConsoleColor.Red, "ERROR! Could not place the Market order sell, trying another time. Error code: " + webCallResult.Error?.Message); } usainsell = 1; string[] strArray = new string[6] { "UsainBot PANIC SOLD successfully ", OrderQuantity.ToString(), " ", webCallResult.Data.Symbol, " sold at ", null }; num1 = priceResult2.Data.BestBidPrice; strArray[5] = num1.ToString(); Utilities.Write(ConsoleColor.Green, string.Concat(strArray)); break; } } } string[] strArray1 = new string[6] { string.Format("Price for {0} is {1} to {2} in iteration ", (object)pair, (object)priceResult2.Data.BestBidPrice, (object)priceResult2.Data.BestAskPrice), count.ToString(), " negative volatility ratio is ", null, null, null }; num1 = Math.Round(volasellmax, 2); strArray1[3] = num1.ToString(); strArray1[4] = " stop limit is placed at "; strArray1[5] = currentstoploss.ToString(); Console.Title = string.Concat(strArray1); } } void NewThread2() { while ((Decimal)timestamp + maxsecondsbeforesell * 10000000M > (Decimal)DateTime.Now.ToFileTime() && x != 2) { ++count; try { priceResult3 = client.Spot.Market.GetBookPrice(pair); tab.Add(priceResult3.Data.BestBidPrice); } catch { tab.Add(tab[count - 1]); } string[] strArray1 = new string[6] { string.Format("Price for {0} is {1} to {2} in iteration ", (object)pair, (object)priceResult3.Data.BestBidPrice, (object)priceResult3.Data.BestAskPrice), count.ToString(), " negative volatility ratio is ", null, null, null }; Decimal num1 = Math.Round(volasellmax, 2); strArray1[3] = num1.ToString(); strArray1[4] = " stop limit is placed at "; strArray1[5] = currentstoploss.ToString(); Console.Title = string.Concat(strArray1); if (count % 10 == 0) { int index = count; Decimal num2 = 0M; int num3 = -1; while (--index > 0 && ++num3 < 10) { num2 += (tab[index] - tab[index - 1]) / 10M; } Decimal num4 = num2 / 3M; while (--index > 0 && ++num3 < 30) { num4 += (tab[index] - tab[index - 1]) / 30M; } Utilities.Write(ConsoleColor.Green, string.Format(" {0}", (object)Math.Round(num2 / priceResult3.Data.BestBidPrice * 100000M, 2))); Utilities.Write(ConsoleColor.Red, string.Format(" {0}", (object)Math.Round(num4 / priceResult3.Data.BestBidPrice * 100000M, 2))); Decimal d = (num4 - num2) / priceResult3.Data.BestBidPrice * 100000M; if (d > strategyrisk / 4M) { Utilities.Write(ConsoleColor.Red, string.Format(" negative volatility detected at a {0} ratio", (object)Math.Round(d, 2))); if (d < strategyrisk) { if (d > volasellmax) { volasellmax = d; sellStrategy = Math.Round(StartSellStrategy + (Decimal)Math.Pow((double)d / (double)strategyrisk, 1.5) * (MaxSellStrategy - StartSellStrategy), 3); } } else { if (usainsell != 0 || imincharge != 0) { break; } imincharge = 1; client.Spot.Order.CancelAllOpenOrders(pair); WebCallResult <BinancePlacedOrder> webCallResult; for (webCallResult = client.Spot.Order.PlaceOrder(pair, OrderSide.Sell, OrderType.Limit, new Decimal?(OrderQuantity), price: new Decimal?(Math.Round(priceResult3.Data.BestBidPrice * sellPriceRiskRatio, symbolPrecision)), timeInForce: new TimeInForce?(TimeInForce.GoodTillCancel)); !webCallResult.Success; webCallResult = client.Spot.Order.PlaceOrder(pair, OrderSide.Sell, OrderType.Limit, new Decimal?(OrderQuantity), price: new Decimal?(Math.Round(priceResult3.Data.BestBidPrice * sellPriceRiskRatio, symbolPrecision)), timeInForce: new TimeInForce?(TimeInForce.GoodTillCancel))) { Utilities.Write(ConsoleColor.Red, "ERROR! Could not place the Market order sell, trying another time. Error code: " + webCallResult.Error?.Message); } usainsell = 1; string[] strArray2 = new string[6] { "UsainBot PANIC SOLD successfully ", OrderQuantity.ToString(), " ", webCallResult.Data.Symbol, " sold at ", null }; num1 = priceResult3.Data.BestBidPrice; strArray2[5] = num1.ToString(); Utilities.Write(ConsoleColor.Green, string.Concat(strArray2)); break; } } } } } } } } } }