/// <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); }
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> /// Constructor /// </summary> public Balance(MainCryptos ccyBase, MainCryptos ccyPair) { this.cryptoTrader = new My_CryptoTrader.My_CryptoTrader(); this.ccyBase = ccyBase; this.ccyPair = ccyPair; this.amount = 0; this.totalValueBTC = 0; this.totalValueUSD = 0; this.ccyUSDPrice = 0; this.BTCUSDPrice = 0; }
/// <summary> /// Constructor /// </summary> public Balance(MainCryptos ccyBase, MainCryptos ccyPair, double amount) { this.cryptoTrader = new My_CryptoTrader.My_CryptoTrader(); this.ccyBase = ccyBase; this.ccyPair = ccyPair; this.amount = amount; this.ccyUSDPrice = this.cryptoTrader.GetSpotPrice(this.ccyBase.ToString()); this.BTCUSDPrice = this.cryptoTrader.GetSpotPrice(MainCryptos.BTC.ToString()); this.totalValueUSD = amount * this.ccyUSDPrice; this.totalValueBTC = (amount * this.ccyUSDPrice) / (this.BTCUSDPrice); }
/// <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 an order to sell <paramref name="amount"/> <paramref name="currency"/>, /// at a limit price of <paramref name="price"/>, to coinbase 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 Coinbase market /// </returns> override public Order Sell(string currency, double amount, double price) { Dictionary <string, string> body = new Dictionary <string, string>(); Models.OrderType oneOrderType = Models.OrderType.BUY; Models.OrderStyle oneOrderStyle = Models.OrderStyle.LIMIT; Exchanges oneExchange = Exchanges.COINBASE; 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 ccyId; if (System.Linq.Enumerable.Contains(this.GetAccountId().Keys, currency)) { ccyId = this.GetAccountId()[currency]; } else { ccyId = ""; } string api = "https://api.coinbase.com/"; string queryString = "v2/accounts/" + ccyId + "/sells"; body.Add("currency", currency); body.Add("amount", amount.ToString()); this.response = this.coinbase.SendRequest(api + queryString, body, RestSharp.Method.POST); if (this.response.Errors != null) { oneNewOrder.success = false; oneNewOrder.message = this.response.Errors[0].Message; } else { oneNewOrder.success = false; oneNewOrder.message = this.response.Data.ToString(); } return(oneNewOrder); }
/// <summary> /// Function that cancel an order placed on coinbase 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 ccyId = ""; string api = "https://api.coinbase.com/v2/accounts/:account_id/addresses"; Models.OrderType oneOrderType = Models.OrderType.CANCEL; Models.OrderStyle oneOrderStyle = Models.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); Dictionary <string, string> dicoCcyIds = this.GetAccountId(); foreach (KeyValuePair <string, string> row in dicoCcyIds) { if (row.Key.ToUpper() == currency.ToUpper()) { ccyId = row.Value; } } api = "https://api.coinbase.com/v2/accounts/" + ccyId + "/addresses"; this.response = this.coinbase.SendRequest(api, "", RestSharp.Method.POST); try { this.dicoResponse = JsonConvert.DeserializeObject <Dictionary <dynamic, dynamic> >(this.response.Data.ToString()); oneNewOrder.success = true; oneNewOrder.message = this.dicoResponse["success"]; } catch (Exception e) { Console.WriteLine(e.Message); oneNewOrder.success = false; oneNewOrder.message = "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 <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 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) { Models.OrderType oneOrderType = Models.OrderType.SEND; Models.OrderStyle oneOrderStyle = Models.OrderStyle.WITHDRAWAL; Exchanges oneExchange = Exchanges.COINBASE; 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 ccyId = ""; string api = "https://api.coinbase.com/v2/accounts/:account_id/transactions"; Dictionary <string, string> body = new Dictionary <string, string>(); Dictionary <string, string> accountId = this.GetAccountId(); foreach (KeyValuePair <string, string> row in accountId) { if (row.Key.ToUpper() == currency.ToString()) { ccyId = row.Value; } } api = "https://api.coinbase.com/v2/accounts/" + ccyId + "/transactions"; body.Add("type", "send"); body.Add("to", adress); body.Add("amount", amount.ToString()); body.Add("currency", currency); this.response = this.coinbase.SendRequest(api, body, RestSharp.Method.POST); try { if (this.response.Data.ToString() != "[]") { this.dicoResponse = JsonConvert.DeserializeObject <Dictionary <dynamic, dynamic> >(this.response.Data.ToString()); oneNewOrder.success = true; oneNewOrder.message = this.dicoResponse["message"]; } else { this.dicoResponse = new Dictionary <dynamic, dynamic>() { { "ERROR", "ERROR WHILE SENDING CRYPTOS" } }; oneNewOrder.success = false; oneNewOrder.message = "ERROR WHILE SENDING CRYPTOS"; } } catch (Exception e) { Console.WriteLine(e.Message); if (this.response != null) { this.dicoResponse = new Dictionary <dynamic, dynamic>() { { "ERROR", this.response.Errors[0].Message } }; oneNewOrder.success = false; oneNewOrder.message = this.response.Errors[0].Message; return(oneNewOrder); } else { this.dicoResponse = new Dictionary <dynamic, dynamic>() { { "ERROR", "This currency isn't handled on Coinbase" } }; oneNewOrder.success = false; oneNewOrder.message = "This crypto isn't handled on Coinbase"; return(oneNewOrder); } } 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 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 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); }
/// <summary> /// Method to add a <paramref name="ccyBase"/> <paramref name="ccyPair"/> <see cref="Balance"/> object, /// for a total amount of <paramref name="amount"/> to the <see cref="balanceList"/> of the portfolio /// </summary> public void AddBalance(MainCryptos ccyBase, MainCryptos ccyPair, double amount) { this.balanceList.Add(new Balance(ccyBase, ccyPair, amount)); }