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)); } }
public override CMState ProcessMessage(Wrapper wrapper) { try { if (wrapper.GetAction() == Actions.REJECT) { DoLog("Processing REJECT @:" + Configuration.Name, Fwk.Main.Common.Util.Constants.MessageType.Information); Thread processNewOrderThread = new Thread(ProcessReject); processNewOrderThread.Start(wrapper); return(CMState.BuildSuccess()); } else if (wrapper.GetAction() == Actions.EXECUTION_REPORT) { DoLog("Processing EXECUTION_REPORT @:" + Configuration.Name, Fwk.Main.Common.Util.Constants.MessageType.Information); Thread processExecReportThread = new Thread(ProcessExecutionReport); processExecReportThread.Start(wrapper); return(CMState.BuildSuccess()); } else { return(CMState.BuildFail(new Exception(string.Format("Could not process action {0} for strategy {1}", wrapper.GetAction().ToString(), Configuration.Name)))); } } catch (Exception ex) { DoLog("Error processing message @" + Configuration.Name + ":" + ex.Message, Fwk.Main.Common.Util.Constants.MessageType.Error); return(CMState.BuildFail(ex)); } }
protected override CMState ProcessMarketDataOrderBookRequest(Wrapper wrapper) { try { MarketDataRequest mdr = MarketDataRequestConverter.GetMarketDataRequest(wrapper); if (mdr.SubscriptionRequestType == SubscriptionRequestType.Snapshot) { throw new Exception(string.Format("@{0}: Market Data Order Book snaphsot not implemented for symbol {1}", BitmexConfiguration.Name, mdr.Security.Symbol)); } else if (mdr.SubscriptionRequestType == SubscriptionRequestType.SnapshotAndUpdates) { return(ProcessMarketDataRequestOrderBook(wrapper)); } else if (mdr.SubscriptionRequestType == SubscriptionRequestType.Unsuscribe) { CancelMarketDataOrderBook(mdr.Security); return(CMState.BuildSuccess()); } else { throw new Exception(string.Format("@{0}: Value not recognized for subscription type {1} for symbol {2}", BitmexConfiguration.Name, mdr.SubscriptionRequestType.ToString(), mdr.Security.Symbol)); } } catch (Exception ex) { return(CMState.BuildFail(ex)); } }
public CMState ProcessMessage(Wrapper wrapper) { try { if (wrapper.GetAction() == Actions.NEW_ORDER) { DoLog(string.Format("@{1}:Routing to market for symbol {0}", wrapper.GetField(OrderFields.Symbol).ToString(), GetConfig().Name), Main.Common.Util.Constants.MessageType.Information); return(RouteNewOrder(wrapper)); } else if (wrapper.GetAction() == Actions.UPDATE_ORDER) { DoLog(string.Format("@{1}:Updating order for symbol {0}", wrapper.GetField(OrderFields.Symbol).ToString(), GetConfig().Name), Main.Common.Util.Constants.MessageType.Information); return(UpdateOrder(wrapper)); //return CMState.BuildSuccess(); } else if (wrapper.GetAction() == Actions.CANCEL_ALL_ORDERS) { DoLog(string.Format("@{0}:Cancelling all orders", GetConfig().Name), Main.Common.Util.Constants.MessageType.Information); return(CancelAllOrders(wrapper)); } else if (wrapper.GetAction() == Actions.CANCEL_ORDER) { DoLog(string.Format("@{0}:Cancelling order", GetConfig().Name), Main.Common.Util.Constants.MessageType.Information); return(CancelOrder(wrapper)); } else if (wrapper.GetAction() == Actions.CANCEL_ALL_POSITIONS) { DoLog(string.Format("@{0}:Cancelling all active orders", GetConfig().Name), Main.Common.Util.Constants.MessageType.Information); return(CancelAllActiveOrders()); } else if (wrapper.GetAction() == Actions.ORDER_LIST) { DoLog(string.Format("@{0}:Recovering full order list", GetConfig().Name), Main.Common.Util.Constants.MessageType.Information); return(GetOrders(wrapper)); } else if (wrapper.GetAction() == Actions.SECURITY_LIST) { DoLog(string.Format("@{0}:Receiving Security List", GetConfig().Name), Main.Common.Util.Constants.MessageType.Information); return(ProcessSecurityList(wrapper)); } else { DoLog(string.Format("@{1}:Could not process order routing for action {0} :", wrapper.GetAction().ToString(), GetConfig().Name), Main.Common.Util.Constants.MessageType.Error); return(CMState.BuildFail(new Exception(string.Format("@{1}:Could not process order routing for action {0}:", wrapper.GetAction().ToString(), GetConfig().Name)))); } } catch (Exception ex) { DoLog(string.Format("@{0}:Error @ProcessMessage:" + ex.Message, GetConfig().Name), Main.Common.Util.Constants.MessageType.Error); return(CMState.BuildFail(ex)); } }
public CMState ProcessMessageToOutgoing(Wrapper wrapper) { try { Log("ProcessMessageToOutgoing"); return(OutgoingModule.ProcessMessage(wrapper)); } catch (Exception ex) { Log(string.Format("ProcessMessageToOutgoing Error: {0}", ex.Message)); return(CMState.BuildFail(ex)); } }
protected override CMState ProcessSecurityList(Wrapper wrapper) { try { SecurityList secList = SecurityListConverter.GetSecurityList(wrapper, Config); Securities = secList.Securities; return(CMState.BuildSuccess()); } catch (Exception ex) { return(CMState.BuildFail(ex)); } }
protected override CMState CancelAllOrders(Wrapper wrapper) { try { OrderManager.CancelAll(); return(CMState.BuildSuccess()); } catch (Exception ex) { DoLog(string.Format("@{0}:Error cancelling all orders!:{1}", BitmexConfiguration.Name, ex.Message), Main.Common.Util.Constants.MessageType.Error); return(CMState.BuildFail(ex)); } }
protected override CMState ProcessSecurityListRequest(Wrapper wrapper) { try { BitmexSecurityListWrapper secListWrapper = new BitmexSecurityListWrapper(Securities, Config); OnMessageRcv(secListWrapper); return(CMState.BuildSuccess()); } catch (Exception ex) { return(CMState.BuildFail(ex)); } }
protected override CMState ProcessMarketDataTradeListRequest(Wrapper wrapper) { try { MarketDataRequest mdr = MarketDataRequestConverter.GetMarketDataRequest(wrapper); int i = 1; if (mdr.SubscriptionRequestType == SubscriptionRequestType.Snapshot) { List <zHFT.InstructionBasedMarketClient.BitMex.Common.DTO.Trade> trades = RESTMarketDataManager.GetTrades(mdr.Security.Symbol); foreach (zHFT.InstructionBasedMarketClient.BitMex.Common.DTO.Trade trade in trades.Where(x => x.symbol == mdr.Security.Symbol) .OrderByDescending(x => x.timestamp).Take(100).ToList()) { BitMexTradeWrapper tradeWrapper = new BitMexTradeWrapper(trade, i == trades.Count); OnMessageRcv(tradeWrapper); if (i % 20 == 0) { Thread.Sleep(1000); } i++; } return(CMState.BuildSuccess()); } else if (mdr.SubscriptionRequestType == SubscriptionRequestType.SnapshotAndUpdates) { throw new Exception(string.Format("@{0}: Market Data trade List SnapshotAndUpdates not implemented for symbol {1}", BitmexConfiguration.Name, mdr.Security.Symbol)); } else if (mdr.SubscriptionRequestType == SubscriptionRequestType.Unsuscribe) { throw new Exception(string.Format("@{0}: Market Data trade List Unsuscribe not implemented for symbol {1}", BitmexConfiguration.Name, mdr.Security.Symbol)); return(CMState.BuildSuccess()); } else { throw new Exception(string.Format("@{0}: Value not recognized for subscription type {1} for symbol {2}", BitmexConfiguration.Name, mdr.SubscriptionRequestType.ToString(), mdr.Security.Symbol)); } } catch (Exception ex) { return(CMState.BuildFail(ex)); } }
public override CMState ProcessMessage(Wrapper wrapper) { try { if (wrapper != null) { Actions action = wrapper.GetAction(); if (action == Actions.SECURITY_LIST_REQUEST) { return(ProcessSecurityListRequest(wrapper)); } else if (Actions.MARKET_DATA_QUOTES_REQUEST == action) { return(ProcessMarketDataQuotesRequest(wrapper)); } else if (Actions.MARKET_DATA_TRADES_REQUEST == action) { return(ProcessMarketDataTradesRequest(wrapper)); } else if (Actions.MARKET_DATA_TRADE_LIST_REQUEST == action) { return(ProcessMarketDataTradeListRequest(wrapper)); } else if (Actions.MARKET_DATA_ORDERBOOK_REQUEST == action) { return(ProcessMarketDataOrderBookRequest(wrapper)); } //else if (Actions.MARKET_DATA_REQUEST == action) //{ // return ProessMarketDataRequest(wrapper); //} else { DoLog("Sending message " + action + " not implemented", Main.Common.Util.Constants.MessageType.Information); return(CMState.BuildFail(new Exception("Sending message " + action + " not implemented"))); } } else { throw new Exception("Invalid Wrapper"); } } catch (Exception ex) { DoLog(ex.Message, Main.Common.Util.Constants.MessageType.Error); throw; } }
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 CancelOrderOnOrderId(string orderId, Wrapper wrapper) { try { lock (tLock) { Order order = BitMexActiveOrders.Values.Where(x => x.OrderId == orderId).FirstOrDefault(); if (order != null) { ExecutionReport exReport = RunCancelOrder(order, false); ProcessCancelationError(exReport, order, wrapper); } else { OrderCancelRejectWrapper reject = new OrderCancelRejectWrapper(orderId, wrapper.GetField(OrderFields.OrigClOrdID).ToString(), wrapper.GetField(OrderFields.ClOrdID).ToString(), OrdStatus.Rejected, Side.Unknown, null, 0, 0, DateTime.Now, CxlRejReason.UnknownOrder, string.Format("OrderId not found: {0}", orderId), wrapper.GetField(OrderFields.Symbol).ToString()); OnMessageRcv(reject); throw new Exception(string.Format("@{0}: Could not cancel order for OrderId {1} because it was not found", BitmexConfiguration.Name, orderId)); } } return(CMState.BuildSuccess()); } catch (Exception ex) { DoLog(string.Format("@{0}:Error cancelig order {1}!:{2}", BitmexConfiguration.Name, orderId, 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)); } }
protected override CMState ProcessIncoming(Wrapper wrapper) { //StrongLogIncoming("ProcessIncoming..." + wrapper.ToString(), Constants.MessageType.Information); try { if (OutgoingModule != null) { return(OutgoingModule.ProcessMessage(wrapper)); } else { return(CMState.BuildSuccess(false, "Outgoing module not set...")); } } catch (Exception ex) { LogIncoming("Error processing outgoing message: " + ex.Message, Constants.MessageType.Error); return(CMState.BuildFail(ex));; } }
public override CMState ProcessMessage(Fwk.Main.Common.Wrappers.Wrapper wrapper) { try { if (wrapper.GetAction() == Actions.NEW_ORDER) { DoLog("Processing NEW_ORDER:" + wrapper.ToString(), Fwk.Main.Common.Util.Constants.MessageType.Information); Thread processNewOrderThread = new Thread(ProcessNewOrder); processNewOrderThread.Start(wrapper); return(CMState.BuildSuccess()); } else { return(CMState.BuildFail(new Exception(string.Format("Could not process action {0} for strategy {1}", wrapper.GetAction().ToString(), Configuration.Name)))); } } catch (Exception ex) { DoLog("Error processing market data @" + Configuration.Name + ":" + ex.Message, Fwk.Main.Common.Util.Constants.MessageType.Error); return(CMState.BuildFail(ex)); } }
protected CMState CancelAllActiveOrders() { try { lock (tLock) { foreach (string uuid in ActiveOrders.Keys) { Order order = ActiveOrders[uuid]; DoLog(string.Format("@{0}:Cancelling active order for symbol {1}", GetConfig().Name, order.Security.Symbol), Main.Common.Util.Constants.MessageType.Information); RunCancelOrder(order, false); } return(CMState.BuildSuccess()); } } catch (Exception ex) { DoLog(string.Format("@{0}:Error cancelling all active orders!:{1}", GetConfig().Name, ex.Message), Main.Common.Util.Constants.MessageType.Error); return(CMState.BuildFail(ex)); } }
protected override CMState ProcessOutgoing(Wrapper wrapper) { //Este Communication Module no tiene modulos de Incoming o Outgoing return(CMState.BuildFail(new Exception("No outgoing module set for market order router!"))); }