示例#1
0
        /// <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);
        }
示例#2
0
        /// <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);
        }
示例#3
0
        public Order openOrder(string volume, Models.OrderSide side, Models.OrderType type, string price)
        {
            var mySide = side == Models.OrderSide.Buy
                ? LiquidQuoine.Net.Objects.OrderSide.Buy
                : LiquidQuoine.Net.Objects.OrderSide.Sell;

            var myType = type == Models.OrderType.Limit
                ? LiquidQuoine.Net.Objects.OrderType.Limit
                : LiquidQuoine.Net.Objects.OrderType.Market;

            price = price.Replace(',', '.');
            CallResult <LiquidQuoinePlacedOrder> response;

            if (price == "")
            {
                response = _api.PlaceOrder(
                    productId: _productId,
                    orderSide: mySide,
                    orderType: myType,
                    quantity: decimal.Parse(volume, CultureInfo.InvariantCulture)
                    );
            }
            else
            {
                response = _api.PlaceOrder(
                    productId: _productId,
                    orderSide: mySide,
                    orderType: myType,
                    price: decimal.Parse(price, CultureInfo.InvariantCulture),
                    quantity: decimal.Parse(volume, CultureInfo.InvariantCulture)
                    );
            }


            if (response == null)
            {
                throw new Exception($"[{DateTime.UtcNow.TimeOfDay}] Критическая ошибка: ордербук вообще не вернулся с сервера Liquid. Возможно, не подходит pairName.");
            }

            if (response.Error != null)
            {
                if (response.Error.Message.Contains("nonce"))
                {
                    throw new Exception("nonce");
                }
                if (response.Error.Message.Contains("order"))
                {
                    throw new Exception("");
                }
                throw new Exception(response.Error.ToString());
            }

            if (!response.Success) //TODO: хз правильно ли
            {
                throw new Exception($"[{DateTime.UtcNow.TimeOfDay}] Сервер вернул неуспех.");
            }

            return(new Order(
                       response.Data.Id.ToString(),
                       response.Data.CurrencyPairCode,
                       "", //liquid doesn't supply clientOrderId field
                       nameof(response.Data.Status),
                       nameof(response.Data.OrderType),
                       "", //time in force
                       response.Data.CreatedAt.ToString(),
                       response.Data.UpdatedAt.ToString(),
                       response.Data.CreatedAt.ToString(), //timestamp
                       (float)response.Data.Quantity,
                       (float)response.Data.Price
                       ));
        }
示例#4
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)
        {
            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);
        }