Ejemplo n.º 1
0
        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));
            }
        }
Ejemplo n.º 2
0
        protected override CMState GetOrders(Wrapper wrapper)
        {
            string symbol = (string)wrapper.GetField(OrderFields.Symbol);

            ExecutionReport[] reports = OrderManager.GetOrders(symbol);

            int count = 0;

            foreach (ExecutionReport report in reports.OrderByDescending(x => x.Timestamp).Take(100))
            {
                count++;
                Order orderRetrieved = new Order();

                orderRetrieved.OrderId    = report.OrderID;
                orderRetrieved.ClOrdId    = report.ClOrdID;
                orderRetrieved.SymbolPair = report.Symbol;
                orderRetrieved.OrderQty   = report.OrderQty;
                orderRetrieved.OrdType    = report.Price.HasValue ? OrdType.Limit : OrdType.Market;
                orderRetrieved.Price      = report.Price.HasValue ? (decimal?)report.Price.Value : null;
                orderRetrieved.Currency   = report.Currency;
                orderRetrieved.Side       = report.Side == "Buy" ? Side.Buy : Side.Sell;

                ExecutionReportInitialListWrapper erWrapper = new ExecutionReportInitialListWrapper(report, orderRetrieved, count == reports.Length);
                OnMessageRcv(erWrapper);

                if (count % 20 == 0)
                {
                    Thread.Sleep(1000);
                }
            }

            return(CMState.BuildSuccess());
        }
Ejemplo n.º 3
0
        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));
            }
        }
        public override Main.Common.DTO.CMState ProcessMessage(Main.Common.Wrappers.Wrapper wrapper)
        {
            try
            {
                CMState state = SingletonHandler.ProcessMessage(wrapper);

                if (state.Success)
                {
                    DoLog(string.Format("@{0}:Publishing Wrapper for action {1}: {2} ", SingletonModuleConfiguration.Name, wrapper.GetAction(),
                                        wrapper.ToString()), Main.Common.Util.Constants.MessageType.Debug);
                }
                else
                {
                    DoLog(string.Format("@{0}:Error Publishing Wrapper. Error={1} ",
                                        state.Exception != null ? state.Exception.Message : "",
                                        SingletonModuleConfiguration.Name),
                          Main.Common.Util.Constants.MessageType.Error);
                }

                return(state);
            }
            catch (Exception ex)
            {
                DoLog(string.Format("@{0}:Error Publishing Wrapper. Error={1} ",
                                    ex != null ? ex.Message : "",
                                    SingletonModuleConfiguration.Name),
                      Main.Common.Util.Constants.MessageType.Error);
                throw;
            }
        }
Ejemplo n.º 6
0
 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));
     }
 }
Ejemplo n.º 7
0
 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));
     }
 }
Ejemplo n.º 8
0
        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));
            }
        }
Ejemplo n.º 9
0
        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));
            }
        }
Ejemplo n.º 10
0
        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));
            }
        }
Ejemplo n.º 11
0
        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));
            }
        }
Ejemplo n.º 12
0
 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;
     }
 }
Ejemplo n.º 13
0
        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));
            }
        }
Ejemplo n.º 14
0
        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));
            }
        }
Ejemplo n.º 15
0
        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));
            }
        }
Ejemplo n.º 16
0
        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));;
            }
        }
Ejemplo n.º 17
0
        protected CMState ProcessMarketDataRequestOrderBook(Wrapper wrapper)
        {
            string symbol  = (string)wrapper.GetField(MarketDataRequestField.Symbol);
            int    mdReqId = (int)wrapper.GetField(MarketDataRequestField.MDReqId);

            Security sec = new Security()
            {
                Symbol = symbol
            };

            lock (ActiveSecuritiesOrderBook)
            {
                ActiveSecuritiesOrderBook.Add(mdReqId, sec);
            }


            Thread RequestMarketDataThread = new Thread(DoRequestMarketDataOrderBook);

            RequestMarketDataThread.Start(symbol);

            return(CMState.BuildSuccess());
        }
Ejemplo n.º 18
0
 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));
     }
 }
Ejemplo n.º 19
0
        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));
            }
        }
Ejemplo n.º 20
0
 protected virtual CMState ProcessSecurityListRequest(Wrapper wrapper)
 {
     return(CMState.BuildSuccess());
 }
Ejemplo n.º 21
0
 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!")));
 }