コード例 #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>
        private PlaceOrderResult PlaceOrder(ref KrakenOrder order, bool wait)
        {
            PlaceOrderResult placeOrderResult = new PlaceOrderResult();

            try
            {
                JsonObject res = _kraken.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
ファイル: KrakenServer.cs プロジェクト: mvi1972/OsEngine
 /// <summary>
 /// send order
 /// исполнить ордер
 /// </summary>
 public void SendOrder(Order order)
 {
     KrakenApi.AddOrder(order);
 }