/// <summary> /// Function that cancel an order placed on bittrex markets, /// identified with its <paramref name="orderId"/> /// </summary> /// <param name="orderId">the ID of the order to cancel.</param> /// <returns> /// a <see cref="Order"/> object, representing the cancelled order /// </returns> override public Order CancelOrder(string currency, string orderId) { BittrexSharp.ResponseWrapper <object> responseCancelOrder; object canceledOrder; OrderType oneOrderType = OrderType.CANCEL; OrderStyle oneOrderStyle = OrderStyle.CANCELLATION; Exchanges oneExchange = Exchanges.BINANCE; MainCryptos ccyBase = (MainCryptos)Enum.Parse(typeof(MainCryptos), currency.ToUpper()); MainCryptos ccyPair = (MainCryptos)Enum.Parse(typeof(MainCryptos), currency.ToUpper());; Order oneNewOrder = new Order(oneExchange, oneOrderType, oneOrderStyle, false, ccyBase, ccyPair, 0, 0); try { responseCancelOrder = this.my_bittrex.CancelOrder(orderId).Result; canceledOrder = this.my_bittrex.CancelOrder(orderId).Result; oneNewOrder.message = responseCancelOrder.Message; oneNewOrder.success = responseCancelOrder.Success; return(oneNewOrder); } catch (Exception exc) { Console.WriteLine(exc.Message); oneNewOrder.message = "ERROR"; oneNewOrder.success = false; return(oneNewOrder); } }
/// <summary> /// Function that send an order to <paramref name="side"/>. /// The order is for <paramref name="amount"/> <paramref name="currency"/>, /// at a limit price of <paramref name="price"/>, to bitfinex market /// <paramref name="currency"/> have to be in <see cref="MainCryptos"/> /// <paramref name="amount"/> have to be positive /// <paramref name="price"/> have to be positive /// </summary> /// <param name="currency">the name of the cryptocurrency.</param> /// <param name="amount">amount to buy.</param> /// <param name="price">limit price of the order.</param> /// <returns> /// A <see cref="Order"/> object, representing the new order that has been send to Bitfinex market /// </returns> override public Order NewOrder(string currency, double amount, double price, string side) { OrderType oneOrderType = (OrderType)Enum.Parse(typeof(OrderType), side.ToUpper()); OrderStyle oneOrderStyle = OrderStyle.LIMIT; Exchanges oneExchange = Exchanges.BITFINEX; MainCryptos ccyBase = (MainCryptos)Enum.Parse(typeof(MainCryptos), currency.ToUpper()); MainCryptos ccyPair; Order oneNewOrder; string message = ""; string completeUrl; string market; this.nonce = DateTimeOffset.Now.ToUnixTimeSeconds().ToString(); this.endpoint = "order/new"; completeUrl = this.apiV1 + this.endpoint; this.request = new HttpRequestMessage(HttpMethod.Post, completeUrl); if (ccyBase != MainCryptos.BTC) { market = currency.ToUpper() + "BTC"; ccyPair = MainCryptos.BTC; } else { market = currency.ToUpper() + "USD"; ccyPair = MainCryptos.USDT; } oneNewOrder = new Order(oneExchange, oneOrderType, oneOrderStyle, false, ccyBase, ccyPair, amount, price); this.body.Clear(); this.body.Add("request", "/v1/order/new"); this.body.Add("nonce", nonce.ToString()); this.body.Add("symbol", market); this.body.Add("amount", oneNewOrder.amount.ToString()); this.body.Add("price", oneNewOrder.orderPrice.price.ToString("F5")); this.body.Add("exchange", oneNewOrder.exchange.ToString().ToLower()); this.body.Add("side", oneNewOrder.side.ToString().ToLower()); this.body.Add("type", "exchange market"); this.setRequestHeaders(this.body); this.response = this.client.SendAsync(this.request).Result; this.jStringResponse = this.response.Content.ReadAsStringAsync().Result; this.requestResponse = JsonConvert.DeserializeObject <Dictionary <dynamic, dynamic> >(this.jStringResponse); if (this.requestResponse.ContainsKey("message")) { message = this.requestResponse["message"]; oneNewOrder.success = false; oneNewOrder.message = message; } else { oneNewOrder.success = true; } return(oneNewOrder); }
/// <summary> /// Constructor /// </summary> public Order(bool error) { if (error == true) { cryptoTrader = new My_CryptoTrader.My_CryptoTrader(); this.exchange = Exchanges.BITTREX; this.side = OrderType.BUY; this.style = OrderStyle.LIMIT; this.success = false; this.ccyPair = MainCryptos.USDT; this.ccyBase = MainCryptos.BTC; this.amount = 0; this.orderPrice = new Price(error: true); this.message = "ERROR"; this.ID = "ERROR"; this.valueBTC = 0; this.valueUSD = 0; } else { cryptoTrader = new My_CryptoTrader.My_CryptoTrader(); this.exchange = Exchanges.BITTREX; this.side = OrderType.BUY; this.style = OrderStyle.LIMIT; this.success = false; this.ccyPair = MainCryptos.USDT; this.ccyBase = MainCryptos.BTC; this.amount = 0; this.orderPrice = new Price(error: true); this.message = ""; this.ID = ""; this.valueBTC = 0; this.valueUSD = 0; } }
/// <summary> /// Function that send <paramref name="amount"/> <paramref name="currency"/>, /// to the specified <paramref name="adress"/> /// <paramref name="currency"/> have to be in <see cref="MainCryptos"/> /// <paramref name="amount"/> have to be positive /// </summary> /// <param name="currency">the name of the cryptocurrency.</param> /// <param name="amount">amount to buy.</param> /// <param name="currency">the crypto currency to send.</param> /// <returns> /// A <see cref="Order"/> object representing the sending. /// </returns> override public Order SendCryptos(string adress, string currency, double amount) { OrderType oneOrderType = OrderType.SEND; OrderStyle oneOrderStyle = OrderStyle.WITHDRAWAL; Exchanges oneExchange = Exchanges.BINANCE; MainCryptos ccyBase = (MainCryptos)Enum.Parse(typeof(MainCryptos), currency.ToUpper()); MainCryptos ccyPair = (MainCryptos)Enum.Parse(typeof(MainCryptos), currency.ToUpper()); Order oneNewOrder; string stringResponse = ""; oneNewOrder = new Order(oneExchange, oneOrderType, oneOrderStyle, false, ccyBase, ccyPair, amount, 0); this.endpoint = "wapi/v3/withdraw.html"; this.queryString = ""; this.bodyRequest.Clear(); this.bodyRequest.Add("asset", currency); this.bodyRequest.Add("address", adress); this.bodyRequest.Add("amount", amount.ToString()); this.bodyRequest.Add("recvWindow", 1000000.ToString()); this.bodyRequest.Add("timestamp", (DateTimeOffset.Now.ToUnixTimeMilliseconds()).ToString()); foreach (KeyValuePair <string, string> row in this.bodyRequest) { this.queryString += row.Key + "=" + row.Value + "&"; } this.queryString = this.queryString.Substring(0, this.queryString.Length - 1); this.hexHash = Tools.byteToString(hasher.ComputeHash(System.Text.Encoding.UTF8.GetBytes(queryString))).ToLower(); this.queryString += "&signature=" + this.hexHash; this.httpRequest = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Post, this.api + this.endpoint + "?" + this.queryString); this.httpRequest.Headers.Add("X-MBX-APIKEY", this.api_key); this.httpResponse = this.httpClient.SendAsync(this.httpRequest).Result; stringResponse = this.httpResponse.Content.ReadAsStringAsync().Result; this.responseRequest = JsonConvert.DeserializeObject <Dictionary <dynamic, dynamic> >(stringResponse); if (this.responseRequest.ContainsKey("success")) { oneNewOrder.success = this.responseRequest["success"]; } else { oneNewOrder.success = false; } if (this.responseRequest.ContainsKey("msg")) { oneNewOrder.message = this.responseRequest["msg"]; } return(oneNewOrder); }
/// <summary> /// Function that cancel an order placed on cex.IO markets, /// identified with its <paramref name="orderId"/> /// </summary> /// <param name="orderId">the ID of the order to cancel.</param> /// <returns> /// a <see cref="Order"/> object, representing the cancelled order /// </returns> override public Order CancelOrder(string currency, string orderId) { string queryJson = ""; string stringResponse; OrderType oneOrderType = OrderType.CANCEL; OrderStyle oneOrderStyle = OrderStyle.CANCELLATION; Exchanges oneExchange = Exchanges.BINANCE; MainCryptos ccyBase = (MainCryptos)Enum.Parse(typeof(MainCryptos), currency.ToUpper()); MainCryptos ccyPair = (MainCryptos)Enum.Parse(typeof(MainCryptos), currency.ToUpper());; Order oneNewOrder = new Order(oneExchange, oneOrderType, oneOrderStyle, false, ccyBase, ccyPair, 0, 0); this.nonce = DateTimeOffset.Now.ToUnixTimeSeconds(); this.endpoint = "cancel_order/"; this.httpRequest = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Post, this.api + this.endpoint); this.hexHash = this.getSignature(this.nonce); this.bodyRequest.Clear(); this.bodyRequest.Add("key", this.apiKey); this.bodyRequest.Add("signature", this.hexHash); this.bodyRequest.Add("nonce", this.nonce.ToString()); this.bodyRequest.Add("id", orderId); queryJson = Newtonsoft.Json.JsonConvert.SerializeObject(this.bodyRequest); this.httpRequest.Content = new System.Net.Http.StringContent(queryJson, System.Text.Encoding.UTF8, "application/json"); this.httpRequest.Content.Headers.Add("key", this.apiKey); this.httpRequest.Content.Headers.Add("signature", this.hexHash); this.httpRequest.Content.Headers.Add("nonce", this.nonce.ToString()); this.httpRequest.Content.Headers.Add("currency", currency); this.httpResponse = this.httpClient.SendAsync(this.httpRequest).Result; stringResponse = this.httpResponse.Content.ReadAsStringAsync().Result; try { this.responseRequest = Newtonsoft.Json.JsonConvert.DeserializeObject <Dictionary <dynamic, dynamic> >(stringResponse); oneNewOrder.message = this.responseRequest["data"]; oneNewOrder.success = true; } catch (Exception e) { Console.WriteLine(e.Message); this.responseRequest = new Dictionary <dynamic, dynamic>() { { "ERROR", "ERROR" } }; oneNewOrder.message = "ERROR"; oneNewOrder.success = false; } return(oneNewOrder); }
/// <summary> /// Function returning all open orders (BUY or SELL) placed on any crypto currencies /// <paramref name="currency"/> have to be in <see cref="MainCryptos"/> /// </summary> /// <returns> /// a List of <see cref="Order"/>, representing all open orders /// </returns> override public List <Order> GetOpenOrders(string currency) { OrderType oneOrderType; OrderStyle oneOrderStyle = OrderStyle.LIMIT; Exchanges oneExchange = Exchanges.BITFINEX; MainCryptos ccyBase; MainCryptos ccyPair; double amount; double orderPrice; List <Order> orderList = new List <Order>(); Order oneNewOrder; Dictionary <dynamic, dynamic> newBody = new Dictionary <dynamic, dynamic>(); this.endpoint = "orders"; string completeUrl = this.apiV1 + this.endpoint; this.request = new HttpRequestMessage(HttpMethod.Post, completeUrl); this.nonce = DateTimeOffset.Now.ToUnixTimeSeconds().ToString(); this.request = new HttpRequestMessage(HttpMethod.Post, completeUrl); this.body.Clear(); this.body.Add("request", "/v1/" + this.endpoint); this.body.Add("nonce", nonce.ToString()); this.setRequestHeaders(this.body); this.response = this.client.SendAsync(this.request).Result; this.jStringResponse = this.response.Content.ReadAsStringAsync().Result; try { if (this.jStringResponse != "[]") { this.requestResponse = JsonConvert.DeserializeObject <Dictionary <dynamic, dynamic> >(this.jStringResponse); if (this.requestResponse.ContainsKey("message")) { oneNewOrder = new Order(error: true); oneNewOrder.message = this.requestResponse["message"]; } } else { oneNewOrder = new Order(error: true); oneNewOrder.message = "NO OPEN ORDERS"; } } catch (Exception e) { Console.WriteLine(e.Message); oneNewOrder = new Order(error: true); } return(orderList); }
/// <summary> /// Function returning all open orders (BUY or SELL) placed on any crypto currencies /// </summary> /// <returns> /// a List of <see cref="Order"/>, representing all open orders /// </returns> override public List <Order> GetOpenOrders(string currency) { OrderType oneOrderType; OrderStyle oneOrderStyle = OrderStyle.LIMIT; Exchanges oneExchange = Exchanges.BITTREX; MainCryptos ccyBase; MainCryptos ccyPair; double amount; double orderPrice; string ID; List <Order> orderList = new List <Order>(); Order oneNewOrder; IEnumerable <BittrexSharp.Domain.OpenOrder> openOrders; Dictionary <dynamic, dynamic> dicoAllOrders = new Dictionary <dynamic, dynamic>(); Dictionary <string, Dictionary <double, double> > dicoResult = new Dictionary <string, Dictionary <double, double> >(); Dictionary <double, double> dicoOrder; string market; if (currency.ToUpper() != MainCryptos.BTC.ToString()) { market = MainCryptos.BTC.ToString() + "-" + currency; } else { market = MainCryptos.USDT.ToString() + "-" + currency; } openOrders = this.my_bittrex.GetOpenOrders(market).Result.Result; foreach (BittrexSharp.Domain.OpenOrder oneOrder in openOrders) { dicoOrder = new Dictionary <double, double> { { (double)oneOrder.Quantity, (double)oneOrder.Limit } }; dicoResult = new Dictionary <string, Dictionary <double, double> > { { oneOrder.OrderType, dicoOrder } }; dicoAllOrders.Add(oneOrder.Exchange, dicoResult); oneOrderType = (OrderType)Enum.Parse(typeof(OrderType), oneOrder.OrderType.Split('_')[1]); ccyBase = (MainCryptos)Enum.Parse(typeof(MainCryptos), oneOrder.Exchange.Split('-')[1]); ccyPair = (MainCryptos)Enum.Parse(typeof(MainCryptos), oneOrder.Exchange.Split('-')[0]); amount = (double)oneOrder.Quantity; orderPrice = (double)oneOrder.Limit; ID = oneOrder.OrderUuid; oneNewOrder = new Order(oneExchange, oneOrderType, oneOrderStyle, true, ccyBase, ccyPair, amount, orderPrice, ID); orderList.Add(oneNewOrder); } return(orderList); }
public virtual Order NewOrder(string currency, double amount, double price, string side) { OrderType oneOrderType = (OrderType)Enum.Parse(typeof(OrderType), side.ToUpper()); OrderStyle oneOrderStyle = OrderStyle.LIMIT; Exchanges oneExchange = Exchanges.BINANCE; MainCryptos ccyBase = (MainCryptos)Enum.Parse(typeof(MainCryptos), currency.ToUpper()); MainCryptos ccyPair = MainCryptos.USDT; Order oneNewOrder = new Order(oneExchange, oneOrderType, oneOrderStyle, false, ccyBase, ccyPair, amount, price); return(oneNewOrder); }
/// <summary> /// Function that send <paramref name="amount"/> <paramref name="currency"/>, /// to the specified <paramref name="adress"/> /// <paramref name="currency"/> have to be in <see cref="MainCryptos"/> /// <paramref name="amount"/> have to be positive /// </summary> /// <param name="currency">the name of the cryptocurrency.</param> /// <param name="amount">amount to buy.</param> /// <param name="currency">the crypto currency to send.</param> /// <returns> /// A <see cref="Order"/> object representing the sending. /// </returns> override public Order SendCryptos(string adress, string currency, double amount) { string sparametersCcy, completeUri, shash, jStringResponse; byte[] uriBytes, secretBytes, hash; string BaseUrl = "https://bittrex.com/api/v1.1/"; string uri = BaseUrl + "account/withdraw"; long nonce = DateTime.Now.Ticks; OrderType oneOrderType = OrderType.SEND; OrderStyle oneOrderStyle = OrderStyle.WITHDRAWAL; Exchanges oneExchange = Exchanges.BITTREX; MainCryptos ccyBase = (MainCryptos)Enum.Parse(typeof(MainCryptos), currency.ToUpper()); MainCryptos ccyPair = (MainCryptos)Enum.Parse(typeof(MainCryptos), currency.ToUpper()); Order oneNewOrder = new Order(oneExchange, oneOrderType, oneOrderStyle, false, ccyBase, ccyPair, amount, 0); HttpClient client = new HttpClient(); HttpRequestMessage request = new HttpRequestMessage(); HttpResponseMessage response = new HttpResponseMessage(); System.Security.Cryptography.HMACSHA512 encryptedUri = new System.Security.Cryptography.HMACSHA512(); Dictionary <string, string> dicoResponse = new Dictionary <string, string>(); Dictionary <string, string> parametersCcy = new Dictionary <string, string> { { "apikey", this.api_key.ToString() }, { "currency", currency }, { "quantity", amount.ToString(System.Globalization.CultureInfo.InvariantCulture) }, { "address", adress }, { "nonce", nonce.ToString() } }; sparametersCcy = parametersCcy.Select(param => System.Net.WebUtility.UrlEncode(param.Key) + "=" + System.Net.WebUtility.UrlEncode(param.Value)).Aggregate((l, r) => l + "&" + r); completeUri = uri + "?" + sparametersCcy; uriBytes = System.Text.Encoding.UTF8.GetBytes(completeUri); secretBytes = System.Text.Encoding.UTF8.GetBytes(this.api_secret); encryptedUri = new System.Security.Cryptography.HMACSHA512(secretBytes); hash = encryptedUri.ComputeHash(uriBytes); shash = Tools.byteToString(hash); request = new HttpRequestMessage(HttpMethod.Get, completeUri); request.Headers.Add("apisign", shash); response = client.SendAsync(request).Result; jStringResponse = response.Content.ReadAsStringAsync().Result; Newtonsoft.Json.JsonConvert.PopulateObject(jStringResponse, dicoResponse); oneNewOrder.success = bool.Parse(dicoResponse["success"]); oneNewOrder.message = dicoResponse["message"]; return(oneNewOrder); }
/// <summary> /// Function that cancel an order placed on bitfinex markets, /// identified with its <paramref name="orderId"/> /// </summary> /// <param name="orderId">the ID of the order to cancel.</param> /// <returns> /// a <see cref="Order"/> object, representing the cancelled order /// </returns> override public Order CancelOrder(string currency, string orderId) { OrderType oneOrderType = OrderType.CANCEL; OrderStyle oneOrderStyle = OrderStyle.CANCELLATION; Exchanges oneExchange = Exchanges.BINANCE; MainCryptos ccyBase = (MainCryptos)Enum.Parse(typeof(MainCryptos), currency.ToUpper()); MainCryptos ccyPair = (MainCryptos)Enum.Parse(typeof(MainCryptos), currency.ToUpper());; Dictionary <dynamic, dynamic> newBody = new Dictionary <dynamic, dynamic>(); Order oneNewOrder = new Order(oneExchange, oneOrderType, oneOrderStyle, false, ccyBase, ccyPair, 0, 0); this.endpoint = "order/cancel"; string completeUrl = this.apiV1 + this.endpoint; this.request = new HttpRequestMessage(HttpMethod.Post, completeUrl); this.nonce = DateTimeOffset.Now.ToUnixTimeSeconds().ToString(); this.request = new HttpRequestMessage(HttpMethod.Post, completeUrl); this.body.Clear(); this.body.Add("request", "/v1/" + this.endpoint); this.body.Add("nonce", nonce.ToString()); this.body.Add("rid", orderId); this.setRequestHeaders(this.body); this.response = this.client.SendAsync(this.request).Result; this.jStringResponse = this.response.Content.ReadAsStringAsync().Result; try { this.requestResponse = JsonConvert.DeserializeObject <Dictionary <dynamic, dynamic> >(this.jStringResponse); oneNewOrder.message = this.requestResponse["success"]; oneNewOrder.success = true; } catch (Exception e) { Console.WriteLine(e.Message); this.requestResponse = new Dictionary <dynamic, dynamic>() { { "ERROR", "ERROR" } }; oneNewOrder.message = "ERROR"; oneNewOrder.success = false; } return(oneNewOrder); }
/// <summary> /// Constructor /// </summary> public Order(Exchanges exchange, OrderType side, OrderStyle style, bool success, MainCryptos ccyBase, MainCryptos ccyPair) { cryptoTrader = new My_CryptoTrader.My_CryptoTrader(); this.exchange = exchange; this.side = side; this.style = style; this.success = success; this.ccyPair = ccyPair; this.ccyBase = ccyBase; this.amount = 0; this.orderPrice = new Price(error: true); this.message = ""; this.ID = ""; this.valueBTC = 0; this.valueUSD = 0; }
/// <summary> /// Constructor /// </summary> public Order(Exchanges exchange, OrderType side) { cryptoTrader = new My_CryptoTrader.My_CryptoTrader(); this.exchange = exchange; this.side = side; this.style = OrderStyle.LIMIT; this.success = false; this.ccyPair = MainCryptos.USDT; this.ccyBase = MainCryptos.BTC; this.amount = 0; this.orderPrice = new Price(error: true); this.message = ""; this.ID = ""; this.valueBTC = 0; this.valueUSD = 0; }
/// <summary> /// Constructor /// </summary> public Order(Exchanges exchange, OrderType side, OrderStyle style, bool success, MainCryptos ccyBase, MainCryptos ccyPair, double amount) { cryptoTrader = new My_CryptoTrader.My_CryptoTrader(); this.exchange = exchange; this.side = side; this.style = style; this.success = success; this.ccyPair = ccyPair; this.ccyBase = ccyBase; this.amount = amount; this.orderPrice = new Price(this.exchange.ToString(), this.ccyBase.ToString(), this.ccyPair.ToString(), this.cryptoTrader.GetSpotPrice(this.ccyBase.ToString())); this.message = ""; this.ID = ""; this.valueBTC = 0; this.valueUSD = 0; }
/// <summary> /// Function that send an order to <paramref name="side"/>. /// The order is for <paramref name="amount"/> <paramref name="currency"/>, /// at a limit price of <paramref name="price"/>, to cex.IO market /// <paramref name="currency"/> have to be in <see cref="MainCryptos"/> /// <paramref name="amount"/> have to be positive /// <paramref name="price"/> have to be positive /// </summary> /// <param name="currency">the name of the cryptocurrency.</param> /// <param name="amount">amount to buy.</param> /// <param name="price">limit price of the order.</param> /// <returns> /// A <see cref="Order"/> object, representing the new order that has been send to CexIO market /// </returns> override public Order NewOrder(string currency, double amount, double price, string side) { OrderType oneOrderType = (OrderType)Enum.Parse(typeof(OrderType), side.ToUpper()); OrderStyle oneOrderStyle = OrderStyle.LIMIT; Exchanges oneExchange = Exchanges.CEXIO; MainCryptos ccyBase = (MainCryptos)Enum.Parse(typeof(MainCryptos), currency.ToUpper()); MainCryptos ccyPair = MainCryptos.USDT; Order oneNewOrder = new Order(oneExchange, oneOrderType, oneOrderStyle, false, ccyBase, ccyPair, amount, price);; string queryString = "place_order/" + currency + "/" + MainCurrency.USD.ToString() + "/"; string queryJson; Dictionary <string, string> ThreadAlias = new Dictionary <string, string>(); this.endpoint = "place_order/" + currency + "/" + MainCurrency.USD.ToString() + "/"; this.nonce = DateTimeOffset.Now.ToUnixTimeSeconds(); this.hexHash = this.getSignature(this.nonce); this.bodyRequest.Clear(); this.bodyRequest.Add("nonce", this.nonce.ToString()); this.bodyRequest.Add("key", this.apiKey); this.bodyRequest.Add("signature", this.hexHash); this.bodyRequest.Add("type", oneNewOrder.side.ToString().ToLower()); this.bodyRequest.Add("amount", oneNewOrder.amount.ToString().ToLower()); this.bodyRequest.Add("price", oneNewOrder.orderPrice.price.ToString().ToLower()); queryJson = Newtonsoft.Json.JsonConvert.SerializeObject(this.bodyRequest); this.httpRequest = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Post, this.api + this.endpoint); this.httpRequest.Content = new System.Net.Http.StringContent(queryJson, System.Text.Encoding.UTF8, "application/json"); this.httpRequest.Content.Headers.Add("key", this.apiKey); this.httpRequest.Content.Headers.Add("signature", this.hexHash); this.httpRequest.Content.Headers.Add("nonce", this.nonce.ToString()); this.httpResponse = this.httpClient.SendAsync(this.httpRequest).Result; var r = this.httpResponse.Content.ReadAsStringAsync().Result; var jsonResponse = Newtonsoft.Json.JsonConvert.DeserializeObject <Dictionary <string, string> >(r); oneNewOrder.success = false; oneNewOrder.message = jsonResponse["error"]; return(oneNewOrder); }
/// <summary> /// Function that send <paramref name="amount"/> <paramref name="currency"/>, /// to the specified <paramref name="adress"/> /// <paramref name="currency"/> have to be in <see cref="MainCryptos"/> /// <paramref name="amount"/> have to be positive /// </summary> /// <param name="currency">the name of the cryptocurrency.</param> /// <param name="amount">amount to buy.</param> /// <param name="currency">the crypto currency to send.</param> /// <returns> /// A <see cref="Order"/> object representing the sending. /// </returns> override public Order SendCryptos(string adress, string currency, double amount) { OrderType oneOrderType = OrderType.SEND; OrderStyle oneOrderStyle = OrderStyle.WITHDRAWAL; Exchanges oneExchange = Exchanges.CEXIO; MainCryptos ccyBase = (MainCryptos)Enum.Parse(typeof(MainCryptos), currency.ToUpper()); MainCryptos ccyPair = (MainCryptos)Enum.Parse(typeof(MainCryptos), currency.ToUpper()); Order oneNewOrder = new Order(oneExchange, oneOrderType, oneOrderStyle, false, ccyBase, ccyPair, amount, 0); string queryJson = ""; string stringResponse; Dictionary <dynamic, dynamic> dicoResponse; this.nonce = DateTimeOffset.Now.ToUnixTimeSeconds(); this.endpoint = "withdrawal"; this.httpRequest = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Post, this.api + this.endpoint); this.hexHash = this.getSignature(this.nonce); this.bodyRequest.Clear(); this.bodyRequest.Add("key", this.apiKey); this.bodyRequest.Add("signature", this.hexHash); this.bodyRequest.Add("nonce", this.nonce.ToString()); queryJson = Newtonsoft.Json.JsonConvert.SerializeObject(this.bodyRequest); this.httpRequest.Content = new System.Net.Http.StringContent(queryJson, System.Text.Encoding.UTF8, "application/json"); this.httpRequest.Content.Headers.Add("key", this.apiKey); this.httpRequest.Content.Headers.Add("signature", this.hexHash); this.httpRequest.Content.Headers.Add("nonce", this.nonce.ToString()); this.httpRequest.Content.Headers.Add("symbol", currency); this.httpRequest.Content.Headers.Add("msymbol", currency); this.httpResponse = this.httpClient.SendAsync(this.httpRequest).Result; stringResponse = this.httpResponse.Content.ReadAsStringAsync().Result; try { this.responseRequest = Newtonsoft.Json.JsonConvert.DeserializeObject <Dictionary <dynamic, dynamic> >(stringResponse); if (this.responseRequest["ok"].ToUpper() == "ERROR") { oneNewOrder.success = false; } else { oneNewOrder.success = true; } oneNewOrder.message = this.responseRequest["error"]; } catch (Exception e) { Console.WriteLine(e.Message); this.responseRequest = new Dictionary <dynamic, dynamic>() { { "ERROR", "NO OPEN ORDERS" } }; dicoResponse = new Dictionary <dynamic, dynamic>() { { "ERROR", "NO OPEN ORDERS" } }; oneNewOrder.success = false; oneNewOrder.message = this.responseRequest["error"]; } return(oneNewOrder); }
/// <summary> /// Function that send an order to <paramref name="side"/>. /// The order is for <paramref name="amount"/> <paramref name="currency"/>, /// at a limit price of <paramref name="price"/>, to binance market /// <paramref name="currency"/> have to be in <see cref="MainCryptos"/> /// <paramref name="amount"/> have to be positive /// <paramref name="price"/> have to be positive /// </summary> /// <param name="currency">the name of the cryptocurrency.</param> /// <param name="amount">amount to buy.</param> /// <param name="price">limit price of the order.</param> /// <returns> /// A <see cref="Order"/> object, representing the new order that has been send to Binance market /// </returns> override public Order NewOrder(string currency, double amount, double price, string side) { string market = ""; Dictionary <string, dynamic> jsonResponse = new Dictionary <string, dynamic>(); List <KeyValuePair <string, string> > jsonResponse2 = new List <KeyValuePair <string, string> >(); long yesterday = DateTimeOffset.Now.AddDays(-1).ToUnixTimeMilliseconds(); long now = DateTimeOffset.Now.ToUnixTimeMilliseconds(); OrderType oneOrderType = (OrderType)Enum.Parse(typeof(OrderType), side.ToUpper()); OrderStyle oneOrderStyle = OrderStyle.LIMIT; Exchanges oneExchange = Exchanges.BINANCE; MainCryptos ccyBase = (MainCryptos)Enum.Parse(typeof(MainCryptos), currency.ToUpper()); MainCryptos ccyPair; Order oneNewOrder; this.endpoint = "api/v3/order"; this.queryString = ""; if (ccyBase != MainCryptos.BTC) { market = (currency + "BTC").ToUpper(); ccyPair = MainCryptos.BTC; } else { market = (currency + "USDT").ToUpper(); ccyPair = MainCryptos.USDT; } oneNewOrder = new Order(oneExchange, oneOrderType, oneOrderStyle, false, ccyBase, ccyPair, amount, price); this.bodyRequest.Clear(); this.bodyRequest.Add("symbol", market); this.bodyRequest.Add("side", oneNewOrder.side.ToString()); this.bodyRequest.Add("type", oneNewOrder.style.ToString()); this.bodyRequest.Add("timeInForce", "gtc".ToUpper()); this.bodyRequest.Add("quantity", oneNewOrder.amount.ToString()); this.bodyRequest.Add("price", oneNewOrder.orderPrice.price.ToString("F5")); this.bodyRequest.Add("recvWindow", 1000000.ToString()); this.bodyRequest.Add("timestamp", (DateTimeOffset.Now.ToUnixTimeMilliseconds()).ToString()); foreach (KeyValuePair <string, string> row in this.bodyRequest) { this.queryString += row.Key + "=" + row.Value + "&"; } this.queryString = this.queryString.Substring(0, this.queryString.Length - 1); this.hexHash = Tools.byteToString(hasher.ComputeHash(System.Text.Encoding.UTF8.GetBytes(this.queryString))).ToLower(); this.queryString += "&signature=" + this.hexHash; this.httpRequest = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Post, this.api + this.endpoint + "?" + this.queryString); this.httpRequest.Headers.Add("X-MBX-APIKEY", this.api_key); this.httpResponse = this.httpClient.SendAsync(this.httpRequest).Result; string stringResponse = this.httpResponse.Content.ReadAsStringAsync().Result; try { jsonResponse = JsonConvert.DeserializeObject <Dictionary <string, dynamic> >(stringResponse); if (jsonResponse.ContainsKey("msg")) { oneNewOrder.success = false; oneNewOrder.message = jsonResponse["msg"]; } else if (jsonResponse.ContainsKey("symbol")) { oneNewOrder.success = true; oneNewOrder.message = "order executed at price " + jsonResponse["price"]; } else { oneNewOrder.success = false; oneNewOrder.message = jsonResponse["ERROR"]; } } catch (Exception e) { Console.Write(e.Message); try { this.listDico = JsonConvert.DeserializeObject <List <Dictionary <dynamic, dynamic> > >(stringResponse); oneNewOrder.success = false; oneNewOrder.message = jsonResponse["ERROR"]; } catch (Exception e2) { Console.Write(e2.Message); oneNewOrder.success = false; oneNewOrder.message = jsonResponse["ERROR"]; } } return(oneNewOrder); }
/// <summary> /// Function returning all open orders (BUY or SELL) placed on any crypto currencies /// <paramref name="currency"/> have to be in <see cref="MainCryptos"/> /// </summary> /// <returns> /// a List of <see cref="Order"/>, representing all open orders /// </returns> override public List <Order> GetOpenOrders(string currency) { OrderType oneOrderType; OrderStyle oneOrderStyle = OrderStyle.LIMIT; Exchanges oneExchange = Exchanges.CEXIO; MainCryptos ccyBase; MainCryptos ccyPair; double amount; double orderPrice; List <Order> orderList = new List <Order>(); Order oneNewOrder; string queryJson = ""; string stringResponse; string error; this.nonce = DateTimeOffset.Now.ToUnixTimeSeconds(); Dictionary <dynamic, dynamic> dicoResponse; this.endpoint = "open_position/" + currency + "/" + MainCurrency.USD.ToString(); this.httpRequest = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Post, this.api + this.endpoint); this.hexHash = this.getSignature(this.nonce); this.bodyRequest.Clear(); this.bodyRequest.Add("key", this.apiKey); this.bodyRequest.Add("signature", this.hexHash); this.bodyRequest.Add("nonce", this.nonce.ToString()); queryJson = Newtonsoft.Json.JsonConvert.SerializeObject(this.bodyRequest); this.httpRequest.Content = new System.Net.Http.StringContent(queryJson, System.Text.Encoding.UTF8, "application/json"); this.httpRequest.Content.Headers.Add("key", this.apiKey); this.httpRequest.Content.Headers.Add("signature", this.hexHash); this.httpRequest.Content.Headers.Add("nonce", this.nonce.ToString()); this.httpRequest.Content.Headers.Add("symbol", currency); this.httpRequest.Content.Headers.Add("msymbol", currency); this.httpResponse = this.httpClient.SendAsync(this.httpRequest).Result; stringResponse = this.httpResponse.Content.ReadAsStringAsync().Result; try { this.responseRequest = Newtonsoft.Json.JsonConvert.DeserializeObject <Dictionary <dynamic, dynamic> >(stringResponse); stringResponse = this.responseRequest["data"]; } catch (Exception e) { Console.WriteLine(e.Message); error = this.responseRequest["error"]; this.responseRequest = new Dictionary <dynamic, dynamic>() { { "ERROR", error } }; dicoResponse = new Dictionary <dynamic, dynamic>() { { "ERROR", error } }; stringResponse = "ERROR"; } return(orderList); }
/// <summary> /// Function that cancel an order placed on binance markets, /// identified with its <paramref name="orderId"/> /// </summary> /// <param name="orderId">the ID of the order to cancel.</param> /// <returns> /// a <see cref="Order"/> object, representing the cancelled order /// </returns> override public Order CancelOrder(string currency, string orderId) { OrderType oneOrderType = OrderType.CANCEL; OrderStyle oneOrderStyle = OrderStyle.CANCELLATION; Exchanges oneExchange = Exchanges.BINANCE; MainCryptos ccyBase = (MainCryptos)Enum.Parse(typeof(MainCryptos), currency.ToUpper()); MainCryptos ccyPair; Order oneNewOrder; string market, stringResponse; this.endpoint = "api/v3/order"; this.queryString = ""; if (currency != MainCryptos.BTC.ToString()) { market = (currency + "BTC").ToUpper(); ccyPair = MainCryptos.BTC; } else { market = (currency + "USDT").ToUpper(); ccyPair = MainCryptos.USDT; } oneNewOrder = new Order(oneExchange, oneOrderType, oneOrderStyle, false, ccyBase, ccyPair, 0, 0); this.queryString = ""; this.bodyRequest.Clear(); this.bodyRequest.Add("symbol", market.ToString()); this.bodyRequest.Add("orderId", orderId); this.bodyRequest.Add("recvWindow", 1000000.ToString()); this.bodyRequest.Add("timestamp", (DateTimeOffset.Now.ToUnixTimeMilliseconds()).ToString()); foreach (KeyValuePair <string, string> row in this.bodyRequest) { this.queryString += row.Key + "=" + row.Value + "&"; } this.queryString = this.queryString.Substring(0, this.queryString.Length - 1); this.hexHash = Tools.byteToString(hasher.ComputeHash(System.Text.Encoding.UTF8.GetBytes(queryString))).ToLower(); this.queryString += "&signature=" + this.hexHash; this.httpRequest = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Delete, this.api + this.endpoint + "?" + this.queryString); this.httpRequest.Headers.Add("X-MBX-APIKEY", this.api_key); this.httpResponse = this.httpClient.SendAsync(this.httpRequest).Result; stringResponse = this.httpResponse.Content.ReadAsStringAsync().Result; this.responseRequest = JsonConvert.DeserializeObject <Dictionary <dynamic, dynamic> >(stringResponse); if (this.responseRequest.ContainsKey("symbol")) { oneNewOrder.success = true; } else { oneNewOrder.success = false; } return(oneNewOrder); }
/// <summary> /// Function that send <paramref name="amount"/> <paramref name="currency"/>, /// to the specified <paramref name="adress"/> /// <paramref name="currency"/> have to be in <see cref="MainCryptos"/> /// <paramref name="amount"/> have to be positive /// </summary> /// <param name="currency">the name of the cryptocurrency.</param> /// <param name="amount">amount to buy.</param> /// <param name="currency">the crypto currency to send.</param> /// <returns> /// A <see cref="Order"/> object representing the sending. /// </returns> override public Order SendCryptos(string adress, string currency, double amount) { OrderType oneOrderType = OrderType.SEND; OrderStyle oneOrderStyle = OrderStyle.WITHDRAWAL; Exchanges oneExchange = Exchanges.BITFINEX; MainCryptos ccyBase = (MainCryptos)Enum.Parse(typeof(MainCryptos), currency.ToUpper()); MainCryptos ccyPair = (MainCryptos)Enum.Parse(typeof(MainCryptos), currency.ToUpper()); Order oneNewOrder = new Order(oneExchange, oneOrderType, oneOrderStyle, false, ccyBase, ccyPair, amount, 0); Dictionary <dynamic, dynamic> newBody = new Dictionary <dynamic, dynamic>(); string completeUrl; //string message = ""; //string sucess = ""; string cryptoName = MyConstants.MainCryptosDico[currency.ToUpper()]; this.endpoint = "withdraw"; completeUrl = this.apiV1 + this.endpoint; this.request = new HttpRequestMessage(HttpMethod.Post, completeUrl); this.nonce = DateTimeOffset.Now.ToUnixTimeSeconds().ToString(); this.body.Clear(); this.body.Add("request", "/v1/" + this.endpoint); this.body.Add("withdraw_type", cryptoName.ToLower()); this.body.Add("walletselected", "exchange"); this.body.Add("amount", amount.ToString()); this.body.Add("address", adress); this.body.Add("nonce", nonce.ToString()); this.setRequestHeaders(this.body); this.response = this.client.SendAsync(this.request).Result; this.jStringResponse = this.response.Content.ReadAsStringAsync().Result; try { if (this.jStringResponse != "[]" && this.response.ReasonPhrase != "Not Allowed") { if (this.jStringResponse[0] == '[') { this.jStringResponse = this.jStringResponse.Substring(1, this.jStringResponse.Length - 2); } this.requestResponse = JsonConvert.DeserializeObject <Dictionary <dynamic, dynamic> >(this.jStringResponse); if (this.requestResponse["status"].ToUpper() == "ERROR") { oneNewOrder.success = false; } oneNewOrder.message = this.requestResponse["message"]; } else { if (this.response.ReasonPhrase == "Not Allowed") { this.requestResponse = new Dictionary <dynamic, dynamic>() { { "ERROR", this.response.ReasonPhrase } }; oneNewOrder.success = false; oneNewOrder.message = this.response.ReasonPhrase; } else { this.requestResponse = new Dictionary <dynamic, dynamic>() { { "ERROR", "Error while sending crypto currency" } }; oneNewOrder.success = false; oneNewOrder.message = "Error while sending crypto currency"; } } } catch (Exception e) { Console.WriteLine(e.Message); this.requestResponse = new Dictionary <dynamic, dynamic>() { { "ERROR", "ERROR" } }; oneNewOrder.success = false; oneNewOrder.message = "ERROR"; } return(oneNewOrder); }
/// <summary> /// Function returning all open orders (BUY or SELL) placed on any crypto currencies /// <paramref name="currency"/> have to be in <see cref="MainCryptos"/> /// </summary> /// <returns> /// a List of <see cref="Order"/>, representing all open orders /// </returns> override public List <Order> GetOpenOrders(string currency) { OrderType oneOrderType; OrderStyle oneOrderStyle = OrderStyle.LIMIT; Exchanges oneExchange = Exchanges.BINANCE; MainCryptos ccyBase; MainCryptos ccyPair; double amount, orderPrice; List <Order> orderList = new List <Order>(); Order oneNewOrder; Price BTCPrice = this.GetPrice(MainCryptos.BTC.ToString()); double BTCSpot = BTCPrice.price; string market, message, aggregateMarket, ID; this.endpoint = "api/v3/openOrders"; this.queryString = ""; if (currency != MainCryptos.BTC.ToString()) { market = (currency + "BTC").ToUpper(); aggregateMarket = ("BTC-" + currency).ToUpper(); ccyPair = MainCryptos.BTC; ccyBase = (MainCryptos)Enum.Parse(typeof(MainCryptos), currency); } else { market = (currency + "USDT").ToUpper(); aggregateMarket = ("USDT-" + currency).ToUpper(); ccyPair = MainCryptos.USDT; ccyBase = (MainCryptos)Enum.Parse(typeof(MainCryptos), currency); } this.queryString = ""; this.bodyRequest.Clear(); this.bodyRequest.Add("symbol", market.ToString()); this.bodyRequest.Add("recvWindow", 1000000.ToString()); this.bodyRequest.Add("timestamp", (DateTimeOffset.Now.ToUnixTimeMilliseconds()).ToString()); foreach (KeyValuePair <string, string> row in this.bodyRequest) { this.queryString += row.Key + "=" + row.Value + "&"; } this.queryString = this.queryString.Substring(0, this.queryString.Length - 1); this.hexHash = Tools.byteToString(hasher.ComputeHash(System.Text.Encoding.UTF8.GetBytes(queryString))).ToLower(); this.queryString += "&signature=" + this.hexHash; this.httpRequest = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Get, this.api + this.endpoint + "?" + this.queryString); this.httpRequest.Headers.Add("X-MBX-APIKEY", this.api_key); this.httpResponse = this.httpClient.SendAsync(this.httpRequest).Result; message = this.httpResponse.Content.ReadAsStringAsync().Result; if (message != "[]") { try { this.responseRequest = JsonConvert.DeserializeObject <Dictionary <dynamic, dynamic> >(message); if (this.responseRequest.ContainsKey("msg")) { this.responseRequest = new Dictionary <dynamic, dynamic>() { { "ERROR", this.responseRequest["msg"] } }; } } catch (Exception exc) { Console.WriteLine(exc.Message); if (message.Substring(0, 1) == "[") { message = message.Substring(1, message.Length - 2); } this.responseRequest = JsonConvert.DeserializeObject <Dictionary <dynamic, dynamic> >(message); if (this.responseRequest.ContainsKey("symbol")) { oneOrderType = Enum.Parse(typeof(OrderType), this.responseRequest["side"]); oneOrderStyle = Enum.Parse(typeof(OrderStyle), this.responseRequest["type"]); amount = Double.Parse(this.responseRequest["origQty"], System.Globalization.CultureInfo.InvariantCulture); orderPrice = Double.Parse(this.responseRequest["price"], System.Globalization.CultureInfo.InvariantCulture); ID = this.responseRequest["orderId"].ToString(); oneNewOrder = new Order(oneExchange, oneOrderType, oneOrderStyle, true, ccyBase, ccyPair, amount, orderPrice, ID); orderList.Add(oneNewOrder); } } } return(orderList); }
/// <summary> /// Function that send an order to sell <paramref name="amount"/> <paramref name="currency"/>, /// at a limit price of <paramref name="price"/>, to bittrex market /// <paramref name="currency"/> have to be in <see cref="MainCryptos"/> /// <paramref name="amount"/> have to be positive /// <paramref name="price"/> have to be positive /// </summary> /// <param name="currency">the name of the cryptocurrency.</param> /// <param name="amount">amount to buy.</param> /// <param name="price">limit price of the order.</param> /// <returns> /// A <see cref="Order"/> object, representing the new Sell order that has been send to Bittrex market /// </returns> override public Order Sell(string currency, double amount, double price) { OrderType oneOrderType = OrderType.SELL; OrderStyle oneOrderStyle = OrderStyle.LIMIT; Exchanges oneExchange = Exchanges.BITTREX; MainCryptos ccyBase = (MainCryptos)Enum.Parse(typeof(MainCryptos), currency.ToUpper()); MainCryptos ccyPair; Order oneNewOrder; BittrexSharp.ResponseWrapper <BittrexSharp.Domain.AcceptedOrder> oneSell; string market; if (currency != MainCryptos.BTC.ToString()) { ccyPair = MainCryptos.BTC; oneNewOrder = new Order(oneExchange, oneOrderType, oneOrderStyle, false, ccyBase, ccyPair, amount, price); market = MainCryptos.BTC.ToString() + "-" + currency; if (currency == MainCryptos.BCH.ToString()) { currency = "BCC"; } try { oneSell = this.my_bittrex.SellLimit(market, (decimal)amount, (decimal)price).Result; oneNewOrder.success = oneSell.Success; if (oneSell.Message == "") { oneNewOrder.message = "Sell Order Succes at price " + price; } else { oneNewOrder.message = oneSell.Message; } } catch { Console.WriteLine("Sell Order wasn't completed or isn't permitted"); oneNewOrder.success = false; oneNewOrder.message = "Sell Order wasn't completed or isn't permitted"; } } else { ccyPair = MainCryptos.USDT; oneNewOrder = new Order(oneExchange, oneOrderType, oneOrderStyle, false, ccyBase, ccyPair, amount, price); market = MainCryptos.USDT.ToString() + "-" + currency; try { oneSell = this.my_bittrex.SellLimit(market, (decimal)amount, (decimal)price).Result; oneNewOrder.success = oneSell.Success; oneNewOrder.message = oneSell.Message; } catch { Console.WriteLine("Sell Order wasn't completed"); oneNewOrder.success = false; oneNewOrder.message = "Sell Order wasn't completed or isn't permitted"; } } return(oneNewOrder); }