protected override CMState RouteNewOrder(Wrapper wrapper) { try { Order order = GetOrder(wrapper); try { lock (tLock) { //RunUpperTick(order); ExecutionReport exRep = OrderManager.PlaceOrder(order); BitMexActiveOrders.Add(order.ClOrdId, order); if (exRep.OrderID != null) { OrderIdMappers.Add(exRep.OrderID, order.ClOrdId); } //The new ER will arrive through the websockets //ExecutionReportWrapper erWrapper = new ExecutionReportWrapper(exRep, order); //OnMessageRcv(erWrapper); } } catch (Exception ex) { zHFT.OrderRouters.Bitmex.Common.DTO.ErrorMessage errMsg = JsonConvert.DeserializeObject <zHFT.OrderRouters.Bitmex.Common.DTO.ErrorMessage>(ex.Message); ExecutionReportWrapper erWrapper = new ExecutionReportWrapper(GetRejectedExecutionReport(order, errMsg.error.message != null ? errMsg.error.message : ex.Message), order); OnMessageRcv(erWrapper); } return(CMState.BuildSuccess()); } catch (Exception ex) { return(CMState.BuildFail(ex)); } }
protected void ProcessExecutionReports(WebSocketSubscriptionEvent subscrEvent) { WebSocketExecutionReportEvent reports = (WebSocketExecutionReportEvent)subscrEvent; foreach (zHFT.OrderRouters.Bitmex.Common.DTO.ExecutionReport execReportDTO in reports.data) { try { string clOrdId = ""; if (OrderIdMappers.ContainsKey(execReportDTO.OrderID)) { clOrdId = OrderIdMappers[execReportDTO.OrderID]; if (BitMexActiveOrders.ContainsKey(clOrdId)) { Order order = BitMexActiveOrders[clOrdId]; lock (tLock) { if (execReportDTO.ExecType == ExecType.New.ToString()) { order.OrderId = execReportDTO.OrderID; } if (execReportDTO.ExecType == ExecType.Replaced.ToString()) { BitMexActiveOrders.Remove(clOrdId); order.ClOrdId = order.PendingClOrdId; order.PendingClOrdId = null; BitMexActiveOrders.Add(order.ClOrdId, order); OrderIdMappers[execReportDTO.OrderID] = order.ClOrdId; } if (execReportDTO.ExecType == ExecType.Canceled.ToString() || execReportDTO.ExecType == ExecType.Stopped.ToString() || execReportDTO.ExecType == ExecType.Rejected.ToString() || execReportDTO.ExecType == ExecType.Suspended.ToString() || execReportDTO.ExecType == ExecType.Expired.ToString()) { BitMexActiveOrders.Remove(order.ClOrdId); OrderIdMappers.Remove(order.OrderId); } } WSExecutionReportWrapper wrapper = new WSExecutionReportWrapper(execReportDTO, order); OnMessageRcv(wrapper); } } else { DoLog(string.Format("Unknown order processing execution report for OrderId {0} ", execReportDTO.OrderID), Main.Common.Util.Constants.MessageType.Information); } } catch (Exception ex) { DoLog(string.Format("Error processing execution report for ClOrdId {0}:{1} ", execReportDTO.ClOrdID, ex.Message), Main.Common.Util.Constants.MessageType.Error); } } }
protected override CMState UpdateOrder(Wrapper wrapper) { string origClOrderId = wrapper.GetField(OrderFields.OrigClOrdID).ToString(); string clOrderId = wrapper.GetField(OrderFields.ClOrdID).ToString(); double?price = (double?)wrapper.GetField(OrderFields.Price); try { if (wrapper.GetField(OrderFields.OrigClOrdID) == null) { throw new Exception("Could not find OrigClOrdID for order updated"); } if (wrapper.GetField(OrderFields.ClOrdID) == null) { throw new Exception("Could not find ClOrdId for new order"); } lock (tLock) { if (BitMexActiveOrders.ContainsKey(origClOrderId) && price.HasValue) { Order order = BitMexActiveOrders[origClOrderId]; order.Price = Convert.ToDecimal(price.Value); order.PendingClOrdId = clOrderId; ExecutionReport exRep = OrderManager.UpdateOrder(order); //ExecutionReportWrapper exWrapper = new ExecutionReportWrapper(exRep, order); //OnMessageRcv(exWrapper); } else { DoLog(string.Format("@{0}:Could not find order for origClOrderId {1}!", BitmexConfiguration.Name, origClOrderId), Main.Common.Util.Constants.MessageType.Error); } } return(CMState.BuildSuccess()); } catch (Exception ex) { DoLog(string.Format("@{0}:Error updating order {1}!:{2}", BitmexConfiguration.Name, origClOrderId, ex.Message), Main.Common.Util.Constants.MessageType.Error); return(CMState.BuildFail(ex)); } }
protected CMState CancelOrderOnClOrderId(string origClOrderId, Wrapper wrapper) { try { //New order id string clOrderId = wrapper.GetField(OrderFields.ClOrdID).ToString(); lock (tLock) { if (BitMexActiveOrders.ContainsKey(origClOrderId)) { Order order = BitMexActiveOrders[origClOrderId]; ExecutionReport exReport = RunCancelOrder(order, false); ProcessCancelationError(exReport, order, wrapper); } else { string orderId = wrapper.GetField(OrderFields.OrderId) != OrderFields.NULL ? wrapper.GetField(OrderFields.OrderId).ToString() : ""; OrderCancelRejectWrapper reject = new OrderCancelRejectWrapper(orderId, origClOrderId, clOrderId, OrdStatus.Rejected, Side.Unknown, null, 0, 0, DateTime.Now, CxlRejReason.UnknownOrder, string.Format("ClOrdId not found: {0}", origClOrderId), wrapper.GetField(OrderFields.Symbol).ToString()); OnMessageRcv(reject); throw new Exception(string.Format("@{0}: Could not cancel order for OrigClOrdId {1} because it was not found", BitmexConfiguration.Name, origClOrderId)); } } return(CMState.BuildSuccess()); } catch (Exception ex) { DoLog(string.Format("@{0}:Error cancelig order {1}!:{2}", BitmexConfiguration.Name, origClOrderId, ex.Message), Main.Common.Util.Constants.MessageType.Error); return(CMState.BuildFail(ex)); } }