private bool CancelOpeningOrder() { bool ok = false; try { if (BrokerPosition == null) { Log(LogEntryImportance.Info, "Position is null. No opening order to cancel", true); ok = true; } else { Order currentOpeningOrder = BrokerPosition.OpeningOrder; if (currentOpeningOrder != null && (currentOpeningOrder.Status == "open" || currentOpeningOrder.Status == "pending" || currentOpeningOrder.Status == "not yet submitted")) { Log(LogEntryImportance.Info, "Canceling current opening order...", true); bool keepSpinning = true; while (keepSpinning) { CancelOrderResult cancelOrderResult = _client.CancelOrder(currentOpeningOrder); switch (cancelOrderResult.ResultType) { case CancelOrderResultType.error: Log(LogEntryImportance.Error, string.Format("An error occured while trying to cancel the opening order.\nError List: {0}", String.Join(",\n", cancelOrderResult.Errors)), true); //if the error is handled, keep spinning _client.HandleErrors(cancelOrderResult.Errors); break; case CancelOrderResultType.success: Log(LogEntryImportance.Info, "Succesfully canceled opening order", true); UpdateOpeningOrder(cancelOrderResult.Order); UpdateEmergencyOrder(null); keepSpinning = false; ok = true; break; case CancelOrderResultType.exception: Log(LogEntryImportance.Error, string.Format("An error occured while attempting to cancel the current opening order: {0}", cancelOrderResult.Exception.Message), true); return(ok); } } } else { Log(LogEntryImportance.Info, "No opening order to cancel.", true); ok = true; } } } catch (Exception ex) { Log(LogEntryImportance.Error, string.Format("An unhandled exception occured in CancelOpeningOrder: {0} {1}", ex.Message, ((ex.InnerException != null) ? ex.InnerException.Message : "")), true); } return(ok); }
public JsonResult cancelOrder([FromBody] CancelOrder order) { OrderResult oResult = new OrderResult(); if (order.orderId != null && order.orderId != "") { using (var connection = ConnectionMultiplexer.Connect($"127.0.0.1:6379")){ var redisDb = connection.GetDatabase(); string result = (string)redisDb.Execute("cancelorder", new object[] { order.orderId, "2", "0" }); if (result.Contains("Error")) { return(Json(oResult)); } else { CancelOrderResult cResult = JsonConvert.DeserializeObject <CancelOrderResult>(result); double price = double.Parse(cResult.Order.Price); if (cResult.Order.BuySell == "0") { if (price > (cResult.CurrentBid - 10) || price > (cResult.CurrentAsk - 10)) { oResult.bidask = (string)redisDb.Execute( "queryfirstbidask", new object[] { "BTCUSD", "0", "5" }); } else { oResult.bidask = "{}"; } } else { if (price < (cResult.CurrentBid - 10) || price < (cResult.CurrentAsk - 10)) { oResult.bidask = (string)redisDb.Execute( "queryfirstbidask", new object[] { "BTCUSD", "0", "5" }); } else { oResult.bidask = "{}"; } } oResult.executedPrice = "{}"; } } } return(Json(oResult)); }
public void Parse(HttpStatusCode status, string s) { // We need to differentiate between two types of errors: // 1. Order can't be cancelled because it's already done (filled or cancelled). // 2. All other errors. // // Coinbase gives us HTTP 400 plus a JSON object with a specific message string // in case of (1) but the error strings aren't documented. We can either rely on the // exact undocumented strings or assume that HTTP 400 always means invalid order. // In the absence of bugs the latter assumption should hold true, so we go with it. if (status == HttpStatusCode.BadRequest) { Result = CancelOrderResult.InvalidOrder; return; } status.EnsureSuccess(); Result = CancelOrderResult.Success; }