Пример #1
0
        /// <summary>
        /// Submit an order to Kraken. The order passed by reference will be updated with info set by Kraken.
        /// </summary>
        /// <param name="order">Order to submit.</param>
        /// <param name="wait">If set to true, the function will wait until the order is closed or canceled.</param>
        /// <returns>PlaceOrderResult containing info about eventual success or failure of the request</returns>
        public PlaceOrderResult PlaceOrder(ref KrakenOrder order, bool wait)
        {
            PlaceOrderResult placeOrderResult = new PlaceOrderResult();

            try
            {
                JsonObject res = client.AddOrder(order);

                JsonArray error = (JsonArray)res["error"];
                if (error.Count() > 0)
                {
                    placeOrderResult.ResultType = PlaceOrderResultType.error;
                    List <string> errorList = new List <string>();
                    foreach (var item in error)
                    {
                        errorList.Add(item.ToString());
                    }
                    placeOrderResult.Errors = errorList;
                    return(placeOrderResult);
                }
                else
                {
                    JsonObject result = (JsonObject)res["result"];
                    JsonObject descr  = (JsonObject)result["descr"];
                    JsonArray  txid   = (JsonArray)result["txid"];

                    if (txid == null)
                    {
                        placeOrderResult.ResultType = PlaceOrderResultType.txid_null;
                        return(placeOrderResult);
                    }
                    else
                    {
                        string transactionIds = "";

                        foreach (var item in txid)
                        {
                            transactionIds += item.ToString() + ",";
                        }
                        transactionIds = transactionIds.TrimEnd(',');

                        order.TxId = transactionIds;

                        if (wait)
                        {
                            #region Repeatedly check order status by calling RefreshOrder
                            bool keepSpinning = true;
                            while (keepSpinning)
                            {
                                RefreshOrderResult refreshOrderResult = RefreshOrder(ref order);
                                switch (refreshOrderResult.ResultType)
                                {
                                case RefreshOrderResultType.success:
                                    switch (order.Status)
                                    {
                                    case "closed":
                                        placeOrderResult.ResultType = PlaceOrderResultType.success;
                                        return(placeOrderResult);

                                    case "pending":
                                        break;

                                    case "open":
                                        break;

                                    case "canceled":
                                        if (order.VolumeExecuted > 0)
                                        {
                                            placeOrderResult.ResultType = PlaceOrderResultType.partial;
                                            return(placeOrderResult);
                                        }
                                        else
                                        {
                                            placeOrderResult.ResultType = PlaceOrderResultType.canceled_not_partial;
                                            return(placeOrderResult);
                                        }

                                    default:
                                        throw new Exception(string.Format("Unknown type of order status: {0}", order.Status));
                                    }
                                    break;

                                case RefreshOrderResultType.error:
                                    throw new Exception(string.Format("An error occured while trying to refresh the order.\nError List: {0}", refreshOrderResult.Errors.ToString()));

                                case RefreshOrderResultType.order_not_found:
                                    throw new Exception("An error occured while trying to refresh the order.\nOrder not found");

                                case RefreshOrderResultType.exception:
                                    throw new Exception("An unexpected exception occured while trying to refresh the order.", refreshOrderResult.Exception);

                                default:
                                    keepSpinning = false;
                                    break;
                                }
                                Thread.Sleep(5000);
                            }
                            #endregion
                        }

                        placeOrderResult.ResultType = PlaceOrderResultType.success;
                        return(placeOrderResult);
                    }
                }
            }
            catch (Exception ex)
            {
                placeOrderResult.ResultType = PlaceOrderResultType.exception;
                placeOrderResult.Exception  = ex;
                return(placeOrderResult);
            }
        }
Пример #2
0
        /// <summary>
        /// Call Kraken to update info about order execution.
        /// </summary>
        /// <param name="order">Order to update</param>
        /// <returns>RefreshOrderResult containing info about eventual success or failure of the request</returns>
        public RefreshOrderResult RefreshOrder(ref KrakenOrder order)
        {
            RefreshOrderResult refreshOrderResult = new RefreshOrderResult();

            try
            {
                JsonObject res = client.QueryOrders(order.TxId);

                JsonArray error = (JsonArray)res["error"];
                if (error.Count() > 0)
                {
                    refreshOrderResult.ResultType = RefreshOrderResultType.error;
                    List <string> errorList = new List <string>();
                    foreach (var item in error)
                    {
                        errorList.Add(item.ToString());
                    }
                    refreshOrderResult.Errors = errorList;
                    return(refreshOrderResult);
                }
                else
                {
                    JsonObject result       = (JsonObject)res["result"];
                    JsonObject orderDetails = (JsonObject)result[order.TxId];

                    if (orderDetails == null)
                    {
                        refreshOrderResult.ResultType = RefreshOrderResultType.order_not_found;
                        return(refreshOrderResult);
                    }
                    else
                    {
                        string    status      = (orderDetails["status"] != null) ? orderDetails["status"].ToString() : null;
                        string    reason      = (orderDetails["reason"] != null) ? orderDetails["reason"].ToString() : null;
                        string    openTime    = (orderDetails["opentm"] != null) ? orderDetails["opentm"].ToString() : null;
                        string    closeTime   = (orderDetails["closetm"] != null) ? orderDetails["closetm"].ToString() : null;
                        string    vol_exec    = (orderDetails["vol_exec"] != null) ? orderDetails["vol_exec"].ToString() : null;
                        string    cost        = (orderDetails["cost"] != null) ? orderDetails["cost"].ToString() : null;
                        string    fee         = (orderDetails["fee"] != null) ? orderDetails["fee"].ToString() : null;
                        string    price       = (orderDetails["price"] != null) ? orderDetails["price"].ToString() : null;
                        string    misc        = (orderDetails["misc"] != null) ? orderDetails["misc"].ToString() : null;
                        string    oflags      = (orderDetails["oflags"] != null) ? orderDetails["oflags"].ToString() : null;
                        JsonArray tradesArray = (JsonArray)orderDetails["trades"];
                        string    trades      = null;
                        if (tradesArray != null)
                        {
                            foreach (var item in tradesArray)
                            {
                                trades += item.ToString() + ",";
                            }
                            trades = trades.TrimEnd(',');
                        }

                        order.Status         = status;
                        order.Reason         = reason;
                        order.OpenTime       = openTime;
                        order.CloseTime      = closeTime;
                        order.VolumeExecuted = double.Parse(vol_exec);
                        order.Cost           = decimal.Parse(cost);
                        order.Fee            = decimal.Parse(fee);
                        order.AveragePrice   = decimal.Parse(price);
                        order.Info           = misc;
                        order.OFlags         = oflags;
                        order.Trades         = trades;

                        refreshOrderResult.ResultType = RefreshOrderResultType.success;
                        return(refreshOrderResult);
                    }
                }
            }
            catch (Exception ex)
            {
                refreshOrderResult.ResultType = RefreshOrderResultType.exception;
                refreshOrderResult.Exception  = ex;
                return(refreshOrderResult);
            }
        }