/// <summary> /// execute order in the exchange /// исполнить ордер на бирже /// </summary> public void ExecuteOrder(KrakenOrder order, Order osOrder, DateTime time) { if (_isConnected == false) { return; } if (_orders == null) { _orders = new List <KrakenOrder>(); } _orders.Add(order); _osEngineOrders.Add(osOrder); lock (_lockerListen) { try { PlaceOrderResult result = PlaceOrder(ref order, false); SendLogMessage(result.ResultType.ToString(), LogMessageType.System); if (order.TxId != null && result.ResultType == PlaceOrderResultType.success) { Order newOrder = new Order(); newOrder.SecurityNameCode = osOrder.SecurityNameCode; newOrder.NumberUser = osOrder.NumberUser; newOrder.NumberMarket = order.TxId; newOrder.PortfolioNumber = osOrder.PortfolioNumber; newOrder.Side = osOrder.Side; newOrder.State = OrderStateType.Activ; newOrder.TimeCallBack = time; if (NewOrderEvent != null) { NewOrderEvent(newOrder); } } else { Order newOrder = new Order(); newOrder.SecurityNameCode = osOrder.SecurityNameCode; newOrder.NumberUser = osOrder.NumberUser; newOrder.PortfolioNumber = osOrder.PortfolioNumber; newOrder.Side = osOrder.Side; newOrder.State = OrderStateType.Fail; if (NewOrderEvent != null) { NewOrderEvent(newOrder); } } } catch (Exception error) { SendLogMessage(error.ToString(), LogMessageType.Error); } } }
/// <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); } }