/// <summary>
        /// 
        /// </summary>
        /// <param name="Command"></param>
        public void UpdateCommand(OpenTrade Command)
        {
            string content = string.Empty;
            string comment = "[modify order]";
            string mode = TradingServer.Facade.FacadeGetTypeNameByTypeID(Command.Type.ID).ToLower();

            string size = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Command.Size.ToString(), 2);
            string openPrice = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Command.OpenPrice.ToString(), Command.Symbol.Digit);
            string stopLoss = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Command.StopLoss.ToString(), Command.Symbol.Digit);
            string takeProfit = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Command.TakeProfit.ToString(), Command.Symbol.Digit);
            string bid = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Command.Symbol.TickValue.Bid.ToString(), Command.Symbol.Digit);
            string ask = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Command.Symbol.TickValue.Ask.ToString(), Command.Symbol.Digit);

            #region NEW INSTANCE OPEN TRADE
            Business.OpenTrade newOpenTrade = new OpenTrade();
            newOpenTrade.ClientCode = Command.ClientCode;
            newOpenTrade.ClosePrice = Command.ClosePrice;
            newOpenTrade.CloseTime = Command.CloseTime;
            newOpenTrade.CommandCode = Command.CommandCode;
            newOpenTrade.Commission = Command.Commission;
            newOpenTrade.ExpTime = Command.ExpTime;
            newOpenTrade.ID = Command.ID;
            newOpenTrade.StopLoss = Command.StopLoss;
            newOpenTrade.TakeProfit = Command.TakeProfit;
            newOpenTrade.Symbol = Command.Symbol;
            newOpenTrade.Investor = Command.Investor;
            newOpenTrade.Type = Command.Type;
            newOpenTrade.Size = Command.Size;
            newOpenTrade.IGroupSecurity = Command.IGroupSecurity;
            newOpenTrade.AgentCommission = Command.AgentCommission;
            newOpenTrade.Taxes = Command.Taxes;
            newOpenTrade.OpenPrice = Command.OpenPrice;
            newOpenTrade.Comment = Command.Comment;
            #endregion

            double TakeProfit = 0;
            double StopLoss = 0;
            TakeProfit = Command.TakeProfit;
            StopLoss = Command.StopLoss;

            #region CONNECT MT4 UPDATE COMMAND
            if (Business.Market.IsConnectMT4)
            {
                if (!Business.Market.StatusConnect)
                {
                    Model.Helper.Instance.SendCommandToClient(Command, EnumET5.CommandName.UpdateCommand, EnumET5.ET5Message.DISCONNECT_FROM_MT4, false, Command.ID, 2);

                    //string Message = "UpdateCommand$False,DISCONNECT FROM MT4," + Command.ID + "," + Command.Investor.InvestorID + "," + Command.Symbol.Name + "," +
                    //                        Command.Size + "," + false + "," + Command.OpenTime + "," + Command.OpenPrice + "," + Command.StopLoss + "," + Command.TakeProfit + "," +
                    //                        Command.ClosePrice + "," + Command.Commission + "," + Command.Swap + "," + Command.Profit + "," + "Comment," + Command.ID + "," + Command.Type.Name + "," +
                    //                        1 + "," + Command.ExpTime + "," + Command.ClientCode + "," + Command.CommandCode + "," + Command.IsHedged + "," + Command.Type.ID + "," + Command.Margin + ",Update";

                    //if (Command.Investor.ClientCommandQueue == null)
                    //    Command.Investor.ClientCommandQueue = new List<string>();

                    //Command.Investor.ClientCommandQueue.Add(Message);

                    return;
                }

                string executionType = Business.Market.marketInstance.GetExecutionType(Command.IGroupSecurity, "B03");

                if (executionType == "manual only- no automation" ||
                    executionType == "manual- but automatic if no dealer online" ||
                    executionType == "automatic only")
                {
                    #region Set Property IsBuy And CommandType Send To Client
                    bool IsBuy = Model.Helper.Instance.IsBuy(Command.Type.ID);

                    string CommandType = Model.Helper.Instance.convertCommandTypeIDToString(Command.Type.ID);
                    #endregion                    

                    #region CONNECT MT4 USING NJ4X
                    string cmd = NJ4XConnectSocket.MapNJ4X.Instance.MapOrderModify(Command.ID, Command.OpenPrice, Command.StopLoss, Command.TakeProfit,
                                                                                    Command.Investor.Code, Command.Investor.UnZipPwd);

                    NJ4XConnectSocket.NJ4XTicket newNJ4XTicket = new NJ4XConnectSocket.NJ4XTicket();
                    newNJ4XTicket.Ask = 0;
                    newNJ4XTicket.Bid = 0;
                    newNJ4XTicket.Code = Command.Investor.Code;
                    newNJ4XTicket.IsDisable = false;
                    newNJ4XTicket.IsReQuote = false;
                    newNJ4XTicket.IsUpdate = true;
                    newNJ4XTicket.IsRequest = false;
                    newNJ4XTicket.OpenPrice = Command.OpenPrice;
                    newNJ4XTicket.Symbol = Command.Symbol.Name;
                    newNJ4XTicket.Ticket = -1;
                    newNJ4XTicket.Execution = Command.Symbol.ExecutionTrade;
                    newNJ4XTicket.OpenPrice = Command.OpenPrice;
                    newNJ4XTicket.ClosePrices = Command.ClosePrice;
                    newNJ4XTicket.Digit = Command.Symbol.Digit;
                    newNJ4XTicket.Command = Command;

                    Business.Market.NJ4XTickets.Add(newNJ4XTicket);

                    string result = NJ4XConnectSocket.NJ4XConnectSocketAsync.Instance.SendNJ4X(cmd);

                    if (!string.IsNullOrEmpty(result))
                    {
                        string[] subResult = result.Split('$');
                        if (subResult[0] == "OrderModify")
                        {
                            string[] subnj4x = subResult[1].Split('{');
                            bool _isClose = bool.Parse(subnj4x[0]);
                            int ticket = int.Parse(subnj4x[1]);

                            if (!_isClose)
                            {
                                if (ticket != 135 && ticket != 138)
                                {
                                    EnumET5.ET5Message eMessage = this.GetEnumWithTicket(ticket);
                                    Model.Helper.Instance.SendCommandToClient(Command, EnumET5.CommandName.UpdateCommand, eMessage, false, Command.ID, 2);

                                    //string strError = this.GetErrorWithCode(ticket);
                                    //string Message = "UpdateCommand$False," + strError + "," + Command.ID + "," +
                                    //                                            Command.Investor.InvestorID + "," +
                                    //                                            Command.Symbol.Name + "," +
                                    //                                            Command.Size + "," + IsBuy + "," +
                                    //                                            Command.OpenTime + "," +
                                    //                                            Command.OpenPrice + "," +
                                    //                                            Command.StopLoss + "," +
                                    //                                            Command.TakeProfit + "," +
                                    //                                            Command.ClosePrice + "," +
                                    //                                            Command.Commission + "," +
                                    //                                            Command.Swap + "," +
                                    //                                            Command.Profit + "," +
                                    //                                            Command.Comment + "," +
                                    //                                            Command.ID + "," +
                                    //                                            Command.Type.Name + "," +
                                    //                                            1 + "," + Command.ExpTime + "," +
                                    //                                            Command.ClientCode + "," +
                                    //                                            Command.CommandCode + "," +
                                    //                                            Command.IsHedged + "," +
                                    //                                            Command.Type.ID + "," +
                                    //                                            Command.Margin + ",Update";

                                    //if (Command.Investor.ClientCommandQueue == null)
                                    //    Command.Investor.ClientCommandQueue = new List<string>();

                                    //Command.Investor.ClientCommandQueue.Add(Message);

                                    lock (Business.Market.nj4xObject)
                                        Business.Market.NJ4XTickets.Remove(newNJ4XTicket);

                                    string Content = string.Empty;
                                    Content = "'" + Command.Investor.Code + "': modified #" + Command.CommandCode + " " + mode + " " + size + " " + Command.Symbol.Name + " at " +
                                        openPrice + " sl: " + stopLoss + " tp: " + takeProfit + " (" + bid + "/" + ask + ") - [Failed - " + Model.Helper.Instance.GetMessage(eMessage) + "]";

                                    TradingServer.Facade.FacadeAddNewSystemLog(5, Content, comment, Command.Investor.IpAddress, Command.Investor.Code);

                                }
                            }
                            else
                            {
                                if (executionType == "manual only- no automation" ||
                                    executionType == "manual- but automatic if no dealer online" ||
                                    executionType == "automatic only")
                                {
                                    Business.Market.NJ4XTickets.Remove(newNJ4XTicket);
                                }   
                            }
                        }
                    }
                    else
                    {
                        Model.Helper.Instance.SendCommandToClient(Command, EnumET5.CommandName.UpdateCommand, EnumET5.ET5Message.TIME_OUT, false, -1, 2);

                        ////unknown error
                        //string Message = "UpdateCommand$False,timeout," + -1 + "," + Command.Investor.InvestorID + "," +
                        //                       Command.Symbol.Name + "," + Command.Size + "," + IsBuy + "," + Command.OpenTime + "," + Command.OpenPrice + "," + Command.StopLoss + "," +
                        //                           Command.TakeProfit + "," + Command.ClosePrice + "," + Command.Commission + "," + Command.Swap + "," + Command.Profit + "," + "Comment," + Command.ID + "," +
                        //                           CommandType + "," + 1 + "," + Command.ExpTime + "," + Command.ClientCode + "," + Command.CommandCode + "," + Command.IsHedged + "," + Command.Type.ID + "," + Command.Margin + ",Open";

                        //if (Command.Investor.ClientCommandQueue == null)
                        //    Command.Investor.ClientCommandQueue = new List<string>();

                        //Command.Investor.ClientCommandQueue.Add(Message);

                        lock (Business.Market.nj4xObject)
                            Business.Market.NJ4XTickets.Remove(newNJ4XTicket);

                        string Content = string.Empty;
                        Content = "'" + Command.Investor.Code + "': modified #" + Command.CommandCode + " " + mode + " " + size + " " + Command.Symbol.Name + " at " +
                            openPrice + " sl: " + stopLoss + " tp: " + takeProfit + " (" + bid + "/" + ask + ") - [Failed - time out]";

                        TradingServer.Facade.FacadeAddNewSystemLog(5, Content, comment, Command.Investor.IpAddress, Command.Investor.Code);
                    }
                    #endregion
                }
                else
                {
                    #region MODE AUTOMATIC
                    string cmd = BuildCommandElement5ConnectMT4.Mode.BuildCommand.Instance.ConvertUpdateOnlineCommandToString(Command.RefCommandID, Command.OpenPrice,
                        Command.StopLoss, Command.TakeProfit, Command.Comment);

                    string resultUpdate = Element5SocketConnectMT4.Business.SocketConnect.Instance.SendSocket(cmd);

                    bool isUpdateSuccess = false;
                    string strError = string.Empty;

                    bool IsBuy = false;
                    if (Command.Type.ID == 1 || Command.Type.ID == 7 || Command.Type.ID == 9)
                        IsBuy = true;

                    if (!string.IsNullOrEmpty(resultUpdate))
                    {
                        string[] subValue = resultUpdate.Split('$');
                        if (subValue.Length == 2)
                        {
                            string[] subParameter = subValue[1].Split('{');
                            if (int.Parse(subParameter[0]) == 1)
                                isUpdateSuccess = true;

                            if (!isUpdateSuccess)
                                strError = subParameter[1];
                        }

                        if (!isUpdateSuccess)
                        {
                            Model.Helper.Instance.SendCommandToClient(Command, EnumET5.CommandName.UpdateCommand, EnumET5.ET5Message.CAN_NOT_UPDATE_COMMAND_FROM_MT4, false, Command.ID, 2);

                            //string Message = "UpdateCommand$False,CAN'T UPDATE COMMAND FROM MT4," + Command.ID + "," +
                            //                Command.Investor.InvestorID + "," +
                            //                Command.Symbol.Name + "," +
                            //                Command.Size + "," + false + "," +
                            //                Command.OpenTime + "," +
                            //                Command.OpenPrice + "," +
                            //                Command.StopLoss + "," +
                            //                Command.TakeProfit + "," +
                            //                Command.ClosePrice + "," +
                            //                Command.Commission + "," +
                            //                Command.Swap + "," +
                            //                Command.Profit + "," + "Comment," +
                            //                Command.ID + "," +
                            //                Command.Type.Name + "," +
                            //                1 + "," + Command.ExpTime + "," +
                            //                Command.ClientCode + "," +
                            //                Command.CommandCode + "," +
                            //                Command.IsHedged + "," +
                            //                Command.Type.ID + "," +
                            //                Command.Margin + ",Update";

                            //if (Command.Investor.ClientCommandQueue == null)
                            //    Command.Investor.ClientCommandQueue = new List<string>();

                            //Command.Investor.ClientCommandQueue.Add(Message);
                        }
                    }
                    else
                    {
                        Model.Helper.Instance.SendCommandToClient(Command, EnumET5.CommandName.UpdateCommand, EnumET5.ET5Message.CAN_NOT_UPDATE_COMMAND_FROM_MT4, false, Command.ID, 2);

                        //string Message = "UpdateCommand$False,CAN'T UPDATE COMMAND FROM MT4," + Command.ID + "," +
                        //                    Command.Investor.InvestorID + "," +
                        //                    Command.Symbol.Name + "," +
                        //                    Command.Size + "," + false + "," +
                        //                    Command.OpenTime + "," +
                        //                    Command.OpenPrice + "," +
                        //                    Command.StopLoss + "," +
                        //                    Command.TakeProfit + "," +
                        //                    Command.ClosePrice + "," +
                        //                    Command.Commission + "," +
                        //                    Command.Swap + "," +
                        //                    Command.Profit + "," + "Comment," +
                        //                    Command.ID + "," +
                        //                    Command.Type.Name + "," +
                        //                    1 + "," + Command.ExpTime + "," +
                        //                    Command.ClientCode + "," +
                        //                    Command.CommandCode + "," +
                        //                    Command.IsHedged + "," +
                        //                    Command.Type.ID + "," +
                        //                    Command.Margin + ",Update";

                        //if (Command.Investor.ClientCommandQueue == null)
                        //    Command.Investor.ClientCommandQueue = new List<string>();

                        //Command.Investor.ClientCommandQueue.Add(Message);
                    }
                    #endregion
                }
            }
            #endregion
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Command"></param>
        private void OrderSendNj4x(OpenTrade Command)
        {
            #region GET EXECUTION TYPE
            string executionType = string.Empty;
            //CHECK TRADE TYPE MANUAL OR AUTOMATICK
            executionType = Business.Market.marketInstance.GetExecutionType(Command.IGroupSecurity, "B03");
            #endregion

            #region Set Property IsBuy And CommandType Send To Client
            bool IsBuy = Model.Helper.Instance.IsBuy(Command.Type.ID);

            string CommandType = Model.Helper.Instance.convertCommandTypeIDToString(Command.Type.ID);
            #endregion                    
            
            string CommandCode = string.Empty;

            Command.Comment = "Element5 OrderSend";
            string cmd = string.Empty;
            if (Command.Symbol.ExecutionTrade == EnumMT4.Execution.REQUEST)
            {
                cmd = NJ4XConnectSocket.MapNJ4X.Instance.MapOrderSend(Command.Investor.Code, Command.Symbol.Name, 1,
                                                Command.Size, Command.OpenPrice, 0, Command.StopLoss, Command.TakeProfit, Command.Comment);
            }
            else
            {
                cmd = NJ4XConnectSocket.MapNJ4X.Instance.MapOrderSend(Command.Investor.Code, Command.Symbol.Name, Command.Type.ID,
                                                Command.Size, Command.OpenPrice, 0, Command.StopLoss, Command.TakeProfit, Command.Comment);
            }

            NJ4XConnectSocket.NJ4XTicket newNJ4XTicket = new NJ4XConnectSocket.NJ4XTicket();
            newNJ4XTicket.Ask = 0;
            newNJ4XTicket.Bid = 0;
            newNJ4XTicket.Code = Command.Investor.Code;
            newNJ4XTicket.IsDisable = false;
            newNJ4XTicket.IsReQuote = false;
            newNJ4XTicket.IsUpdate = false;
            newNJ4XTicket.OpenPrice = Command.OpenPrice;
            newNJ4XTicket.Symbol = Command.Symbol.Name;
            newNJ4XTicket.Ticket = -1;
            newNJ4XTicket.Execution = Command.Symbol.ExecutionTrade;
            newNJ4XTicket.IsRequest = false;
            newNJ4XTicket.OpenPrice = Command.OpenPrice;
            newNJ4XTicket.ClosePrices = Command.ClosePrice;
            newNJ4XTicket.Digit = Command.Symbol.Digit;
            newNJ4XTicket.Command = Command;
            newNJ4XTicket.ClientCode = Command.ClientCode;

            Business.Market.NJ4XTickets.Add(newNJ4XTicket);

            string result = NJ4XConnectSocket.NJ4XConnectSocketAsync.Instance.SendNJ4X(cmd);

            if (!string.IsNullOrEmpty(result))
            {
                string[] subResult = result.Split('$');
                if (subResult[0] == "OrderSend")
                {
                    int ticket = int.Parse(subResult[1]);
                    if (Model.Helper.Instance.IsMT4ErrorCode(ticket))
                    {
                        if (ticket != 135 && ticket != 138)
                        {
                            lock (Business.Market.nj4xObject)
                                Business.Market.NJ4XTickets.Remove(newNJ4XTicket);

                            this.CheckTicket(ticket, Command);

                            #region INSERT SYSTEM LOG EVENT MAKE COMMAND
                            string size = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Command.Size.ToString(), 2);
                            string openPrice = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Command.OpenPrice.ToString(), Command.Symbol.Digit);
                            string takeProfit = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Command.TakeProfit.ToString(), Command.Symbol.Digit);
                            string stopLoss = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Command.StopLoss.ToString(), Command.Symbol.Digit);
                            string bid = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Command.Symbol.TickValue.Bid.ToString(), Command.Symbol.Digit);
                            string ask = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Command.Symbol.TickValue.Ask.ToString(), Command.Symbol.Digit);
                            string mode = TradingServer.Model.TradingCalculate.Instance.ConvertTypeIDToString(Command.Type.ID);

                            string content = string.Empty;
                            string comment = string.Empty;

                            content = "'" + Command.Investor.Code + "': " + mode + " " + size + " " + Command.Symbol.Name + " at " + openPrice +
                                            " sl: " + stopLoss + " tp: " + takeProfit + " (" + bid + "/" + ask + ")";
                            comment = "[" + mode + " instance]";
                            string strError = this.GetErrorWithCode(ticket);
                            content += " - failed [" + strError + "]";

                            TradingServer.Facade.FacadeAddNewSystemLog(5, content, comment, Command.IpAddress, Command.Investor.Code);
                            #endregion
                        }
                    }
                    else
                    {
                        if (executionType == "manual- but automatic if no dealer online" ||
                            executionType == "automatic only" ||
                            executionType == "manual only- no automation")
                        {
                            if (Business.Market.NJ4XTickets != null && Business.Market.NJ4XTickets.Count > 0)
                            {
                                lock (Business.Market.nj4xObject)
                                {
                                    //if (Business.Market.NJ4XTickets != null)
                                    //{
                                    //    int countNJ4X = Business.Market.NJ4XTickets.Count;
                                    //    for (int n = 0; n < countNJ4X; n++)
                                    //    {
                                    //        if (Business.Market.NJ4XTickets[n].Code == newNJ4XTicket.Code)
                                    //        {
                                    //            Business.Market.NJ4XTickets.RemoveAt(n);

                                    //            break;
                                    //        }
                                    //    }
                                    //}

                                    #region comment code
                                    int countNJ4X = Business.Market.NJ4XTickets.Count;
                                    for (int n = 0; n < countNJ4X; n++)
                                    {
                                        if (Business.Market.NJ4XTickets[n].Code == newNJ4XTicket.Code)
                                        {
                                            lock (Business.Market.nj4xObject)
                                                Business.Market.NJ4XTickets.RemoveAt(n);

                                            newNJ4XTicket.Ticket = ticket;

                                            string cmdOpenTrade = BuildCommandElement5ConnectMT4.Mode.BuildCommand.Instance.ConvertGetCommandHistoryInfo(ticket);
                                            string cmdResult = Element5SocketConnectMT4.Business.SocketConnect.Instance.SendSocket(cmdOpenTrade);

                                            BuildCommandElement5ConnectMT4.Business.OnlineTrade _newOpenTrade = BuildCommandElement5ConnectMT4.Mode.ReceiveCommand.Instance.ConvertHistoryInfo(cmdResult);

                                            Business.OpenTrade resultOpenTrade = Command;

                                            resultOpenTrade.Commission = _newOpenTrade.Commission;
                                            resultOpenTrade.Swap = _newOpenTrade.Swap;
                                            resultOpenTrade.OpenPrice = _newOpenTrade.OpenPrice;
                                            resultOpenTrade.StopLoss = _newOpenTrade.StopLoss;
                                            resultOpenTrade.TakeProfit = _newOpenTrade.TakeProfit;

                                            resultOpenTrade.RefCommandID = ticket;
                                            resultOpenTrade.CommandCode = ticket.ToString();

                                            #region process add new command
                                            if (resultOpenTrade.RefCommandID > 0)
                                            {
                                                Business.OpenTrade newOpenTradeExe = new OpenTrade();
                                                Business.OpenTrade newOpenTradeSymbol = new OpenTrade();
                                                Business.OpenTrade newOpenTradeInvestor = new OpenTrade();

                                                #region SET INSTANCE IGROUPSECURITY, SYMBOL, TYPE, INVESTOR
                                                //set igroupsecurity
                                                newOpenTradeExe.IGroupSecurity = resultOpenTrade.IGroupSecurity;
                                                newOpenTradeInvestor.IGroupSecurity = resultOpenTrade.IGroupSecurity;
                                                newOpenTradeSymbol.IGroupSecurity = resultOpenTrade.IGroupSecurity;

                                                //set symbol
                                                newOpenTradeExe.Symbol = resultOpenTrade.Symbol;
                                                newOpenTradeInvestor.Symbol = resultOpenTrade.Symbol;
                                                newOpenTradeSymbol.Symbol = resultOpenTrade.Symbol;

                                                //set type
                                                newOpenTradeExe.Type = resultOpenTrade.Type;
                                                newOpenTradeInvestor.Type = resultOpenTrade.Type;
                                                newOpenTradeSymbol.Type = resultOpenTrade.Type;

                                                //set investor
                                                newOpenTradeExe.Investor = resultOpenTrade.Investor;
                                                newOpenTradeInvestor.Investor = resultOpenTrade.Investor;
                                                newOpenTradeSymbol.Investor = resultOpenTrade.Investor;
                                                #endregion

                                                #region GET SPREAD DIFFIRENCE FOR COMMAND
                                                //GET SPREAD DIFFRENCE OF OPEN TRADE
                                                double spreadDifference = 0;
                                                if (resultOpenTrade.IGroupSecurity != null)
                                                {
                                                    if (resultOpenTrade.IGroupSecurity.IGroupSecurityConfig != null)
                                                    {
                                                        int countIGroupSecurity = resultOpenTrade.IGroupSecurity.IGroupSecurityConfig.Count;
                                                        for (int j = 0; j < countIGroupSecurity; j++)
                                                        {
                                                            if (resultOpenTrade.IGroupSecurity.IGroupSecurityConfig[j].Code == "B11")
                                                            {
                                                                double.TryParse(resultOpenTrade.IGroupSecurity.IGroupSecurityConfig[j].NumValue, out spreadDifference);
                                                                break;
                                                            }
                                                        }
                                                    }
                                                }

                                                newOpenTradeExe.SpreaDifferenceInOpenTrade = spreadDifference;
                                                newOpenTradeInvestor.SpreaDifferenceInOpenTrade = spreadDifference;
                                                newOpenTradeSymbol.SpreaDifferenceInOpenTrade = spreadDifference;
                                                #endregion

                                                double tempClosePrice = 0;
                                                if (IsBuy)
                                                    tempClosePrice = resultOpenTrade.Symbol.TickValue.Bid;
                                                else
                                                    tempClosePrice = resultOpenTrade.Symbol.TickValue.Ask;

                                                resultOpenTrade.ClosePrice = tempClosePrice;

                                                #region NEW INSTANCES FOR COMMAND EXECUTOR
                                                newOpenTradeExe.AgentCommission = 0;
                                                newOpenTradeExe.ClientCode = resultOpenTrade.ClientCode;
                                                newOpenTradeExe.CloseTime = resultOpenTrade.OpenTime;
                                                //newOpenTradeExe.CommandCode = resultOpenTrade.CommandCode;
                                                newOpenTradeExe.Comment = resultOpenTrade.Comment;
                                                newOpenTradeExe.Swap = resultOpenTrade.Swap;
                                                newOpenTradeExe.Commission = resultOpenTrade.Commission;
                                                newOpenTradeExe.ExpTime = resultOpenTrade.ExpTime;
                                                newOpenTradeExe.FreezeMargin = 0;
                                                //newOpenTradeExe.ID = listOnlineCommand[i].OnlineCommandID;
                                                newOpenTradeExe.IsClose = false;
                                                newOpenTradeExe.OpenPrice = resultOpenTrade.OpenPrice;
                                                newOpenTradeExe.OpenTime = resultOpenTrade.OpenTime;
                                                newOpenTradeExe.Profit = resultOpenTrade.Profit;
                                                newOpenTradeExe.Size = resultOpenTrade.Size;
                                                newOpenTradeExe.StopLoss = resultOpenTrade.StopLoss;
                                                newOpenTradeExe.Swap = resultOpenTrade.Swap;
                                                newOpenTradeExe.TakeProfit = resultOpenTrade.TakeProfit;
                                                newOpenTradeExe.Taxes = 0;
                                                newOpenTradeExe.TotalSwap = 0;
                                                newOpenTradeExe.RefCommandID = resultOpenTrade.RefCommandID;
                                                newOpenTradeExe.ClosePrice = tempClosePrice;
                                                #endregion

                                                #region NEW INSTANCE FOR SYMBOL LIST
                                                newOpenTradeSymbol.AgentCommission = 0;
                                                newOpenTradeSymbol.ClientCode = resultOpenTrade.ClientCode;
                                                newOpenTradeSymbol.CloseTime = resultOpenTrade.OpenTime;
                                                //newOpenTradeSymbol.CommandCode = listOnlineCommand[i].CommandCode;
                                                newOpenTradeSymbol.Comment = resultOpenTrade.Comment;
                                                newOpenTradeInvestor.Swap = resultOpenTrade.Swap;
                                                newOpenTradeSymbol.Commission = resultOpenTrade.Commission;
                                                newOpenTradeSymbol.ExpTime = resultOpenTrade.ExpTime;
                                                newOpenTradeSymbol.FreezeMargin = 0;
                                                //newOpenTradeSymbol.ID = listOnlineCommand[i].OnlineCommandID;
                                                newOpenTradeSymbol.IsClose = true;
                                                newOpenTradeSymbol.OpenPrice = resultOpenTrade.OpenPrice;
                                                newOpenTradeSymbol.OpenTime = resultOpenTrade.OpenTime;
                                                newOpenTradeSymbol.Profit = resultOpenTrade.Profit;
                                                newOpenTradeSymbol.Size = resultOpenTrade.Size;
                                                newOpenTradeSymbol.StopLoss = resultOpenTrade.StopLoss;
                                                newOpenTradeSymbol.Swap = resultOpenTrade.Swap;
                                                newOpenTradeSymbol.TakeProfit = resultOpenTrade.TakeProfit;
                                                newOpenTradeSymbol.Taxes = 0;
                                                newOpenTradeSymbol.TotalSwap = 0;
                                                newOpenTradeSymbol.InsExe = newOpenTradeExe;
                                                newOpenTradeSymbol.RefCommandID = resultOpenTrade.RefCommandID;
                                                newOpenTradeSymbol.ClosePrice = tempClosePrice;
                                                #endregion

                                                #region NEW INSTANCE FOR INVESTOR LIST
                                                newOpenTradeInvestor.AgentCommission = 0;
                                                newOpenTradeInvestor.ClientCode = resultOpenTrade.ClientCode;
                                                newOpenTradeInvestor.CloseTime = resultOpenTrade.OpenTime;
                                                //newOpenTradeInvestor.CommandCode = listOnlineCommand[i].CommandCode;
                                                newOpenTradeInvestor.Comment = resultOpenTrade.Comment;
                                                newOpenTradeInvestor.Commission = resultOpenTrade.Commission;
                                                newOpenTradeInvestor.Swap = resultOpenTrade.Swap;
                                                newOpenTradeInvestor.ExpTime = resultOpenTrade.ExpTime;
                                                newOpenTradeInvestor.FreezeMargin = 0;
                                                //newOpenTradeInvestor.ID = listOnlineCommand[i].OnlineCommandID;
                                                newOpenTradeInvestor.IsClose = false;
                                                newOpenTradeInvestor.OpenPrice = resultOpenTrade.OpenPrice;
                                                newOpenTradeInvestor.OpenTime = resultOpenTrade.OpenTime;
                                                newOpenTradeInvestor.Profit = resultOpenTrade.Profit;
                                                newOpenTradeInvestor.Size = resultOpenTrade.Size;
                                                newOpenTradeInvestor.StopLoss = resultOpenTrade.StopLoss;
                                                newOpenTradeInvestor.Swap = resultOpenTrade.Swap;
                                                newOpenTradeInvestor.TakeProfit = resultOpenTrade.TakeProfit;
                                                newOpenTradeInvestor.Taxes = 0;
                                                newOpenTradeInvestor.TotalSwap = 0;
                                                newOpenTradeInvestor.InsExe = newOpenTradeExe;
                                                newOpenTradeInvestor.RefCommandID = resultOpenTrade.RefCommandID;
                                                newOpenTradeInvestor.ClosePrice = tempClosePrice;
                                                #endregion

                                                #region BUILD CLIENT CODE
                                                string clientCode = string.Empty;
                                                clientCode = resultOpenTrade.Investor.Code + "_" + DateTime.Now.Ticks;
                                                #endregion

                                                newOpenTradeExe.ClientCode = clientCode;
                                                newOpenTradeInvestor.ClientCode = clientCode;
                                                newOpenTradeSymbol.ClientCode = clientCode;

                                                newOpenTradeExe.CommandCode = resultOpenTrade.CommandCode;
                                                newOpenTradeInvestor.CommandCode = resultOpenTrade.CommandCode;
                                                newOpenTradeSymbol.CommandCode = resultOpenTrade.CommandCode;

                                                string commandCode = string.Empty;

                                                //set id and command code
                                                newOpenTradeExe.ID = resultOpenTrade.RefCommandID;
                                                newOpenTradeInvestor.ID = resultOpenTrade.RefCommandID;
                                                newOpenTradeSymbol.ID = resultOpenTrade.RefCommandID;

                                                commandCode = resultOpenTrade.CommandCode;

                                                //======================================
                                                #region ADD COMMAND TO COMMAND EXECUTOR
                                                if (Business.Market.CommandExecutor == null)
                                                    Business.Market.CommandExecutor = new List<Business.OpenTrade>();

                                                Business.Market.CommandExecutor.Add(newOpenTradeExe);
                                                #endregion

                                                #region ADD COMMAND TO SYMBOL LIST
                                                resultOpenTrade.Symbol.CommandList.Add(newOpenTradeSymbol);
                                                #endregion

                                                #region ADD COMMAND TO INVESTOR LIST
                                                resultOpenTrade.Investor.CommandList.Add(newOpenTradeInvestor);
                                                #endregion

                                                string msg = string.Empty;

                                                #region Map Command Server To Client
                                                if (resultOpenTrade.IsServer)
                                                {
                                                    #region BUILD COMMAND SEND TO CLIENT
                                                    StringBuilder Message = new StringBuilder();
                                                    Message.Append("AddCommandByManager$True,Add New Command Complete,");
                                                    Message.Append(resultOpenTrade.RefCommandID);
                                                    Message.Append(",");
                                                    Message.Append(resultOpenTrade.Investor.InvestorID);
                                                    Message.Append(",");
                                                    Message.Append(resultOpenTrade.Symbol.Name);
                                                    Message.Append(",");
                                                    Message.Append(resultOpenTrade.Size);
                                                    Message.Append(",");
                                                    Message.Append(IsBuy);
                                                    Message.Append(",");
                                                    Message.Append(resultOpenTrade.OpenTime);
                                                    Message.Append(",");
                                                    Message.Append(resultOpenTrade.OpenPrice);
                                                    Message.Append(",");
                                                    Message.Append(resultOpenTrade.StopLoss);
                                                    Message.Append(",");
                                                    Message.Append(resultOpenTrade.TakeProfit);
                                                    Message.Append(",");
                                                    Message.Append(resultOpenTrade.ClosePrice);
                                                    Message.Append(",");
                                                    Message.Append(resultOpenTrade.Commission);
                                                    Message.Append(",");
                                                    Message.Append(resultOpenTrade.Swap);
                                                    Message.Append(",");
                                                    Message.Append(resultOpenTrade.Profit);
                                                    Message.Append(",");
                                                    Message.Append(resultOpenTrade.Comment);
                                                    Message.Append(",");
                                                    Message.Append(resultOpenTrade.ID);
                                                    Message.Append(",");
                                                    Message.Append(CommandType);
                                                    Message.Append(",");
                                                    Message.Append(1);
                                                    Message.Append(",");
                                                    Message.Append(resultOpenTrade.ExpTime);
                                                    Message.Append(",");
                                                    Message.Append(resultOpenTrade.ClientCode);
                                                    Message.Append(",");
                                                    Message.Append(commandCode);
                                                    Message.Append(",");
                                                    Message.Append(resultOpenTrade.IsHedged);
                                                    Message.Append(",");
                                                    Message.Append(resultOpenTrade.Type.ID);
                                                    Message.Append(",");
                                                    Message.Append(resultOpenTrade.Margin);
                                                    Message.Append(",Open");

                                                    if (resultOpenTrade.Investor.ClientCommandQueue == null)
                                                        resultOpenTrade.Investor.ClientCommandQueue = new List<string>();

                                                    resultOpenTrade.Investor.ClientCommandQueue.Add(Message.ToString());

                                                    msg = Message.ToString();
                                                    #endregion
                                                }
                                                else
                                                {
                                                    #region BUILD COMMAND SEND TO CLIENT
                                                    StringBuilder Message = new StringBuilder();
                                                    Message.Append("AddCommand$True,Add New Command Complete,");
                                                    Message.Append(resultOpenTrade.RefCommandID);
                                                    Message.Append(",");
                                                    Message.Append(resultOpenTrade.Investor.InvestorID);
                                                    Message.Append(",");
                                                    Message.Append(resultOpenTrade.Symbol.Name);
                                                    Message.Append(",");
                                                    Message.Append(resultOpenTrade.Size);
                                                    Message.Append(",");
                                                    Message.Append(IsBuy);
                                                    Message.Append(",");
                                                    Message.Append(resultOpenTrade.OpenTime);
                                                    Message.Append(",");
                                                    Message.Append(resultOpenTrade.OpenPrice);
                                                    Message.Append(",");
                                                    Message.Append(resultOpenTrade.StopLoss);
                                                    Message.Append(",");
                                                    Message.Append(resultOpenTrade.TakeProfit);
                                                    Message.Append(",");
                                                    Message.Append(resultOpenTrade.ClosePrice);
                                                    Message.Append(",");
                                                    Message.Append(resultOpenTrade.Commission);
                                                    Message.Append(",");
                                                    Message.Append(resultOpenTrade.Swap);
                                                    Message.Append(",");
                                                    Message.Append(resultOpenTrade.Profit);
                                                    Message.Append(",");
                                                    Message.Append(resultOpenTrade.Comment);
                                                    Message.Append(",");
                                                    Message.Append(resultOpenTrade.ID);
                                                    Message.Append(",");
                                                    Message.Append(CommandType);
                                                    Message.Append(",");
                                                    Message.Append(1);
                                                    Message.Append(",");
                                                    Message.Append(resultOpenTrade.ExpTime);
                                                    Message.Append(",");
                                                    Message.Append(resultOpenTrade.ClientCode);
                                                    Message.Append(",");
                                                    Message.Append(commandCode);
                                                    Message.Append(",");
                                                    Message.Append(resultOpenTrade.IsHedged);
                                                    Message.Append(",");
                                                    Message.Append(resultOpenTrade.Type.ID);
                                                    Message.Append(",");
                                                    Message.Append(resultOpenTrade.Margin);
                                                    Message.Append(",Open");

                                                    if (resultOpenTrade.Investor.ClientCommandQueue == null)
                                                        resultOpenTrade.Investor.ClientCommandQueue = new List<string>();

                                                    resultOpenTrade.Investor.ClientCommandQueue.Add(Message.ToString());

                                                    msg = Message.ToString();
                                                    #endregion
                                                }
                                                #endregion

                                                #region LOG MAKE COMMAND SUCCESS
                                                string mode = TradingServer.Model.TradingCalculate.Instance.ConvertTypeIDToString(resultOpenTrade.Type.ID);
                                                string size = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(resultOpenTrade.Size.ToString(), 2);
                                                string openPrice = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(resultOpenTrade.OpenPrice.ToString(), resultOpenTrade.Symbol.Digit);
                                                string takeProfit = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(resultOpenTrade.TakeProfit.ToString(), resultOpenTrade.Symbol.Digit);
                                                string stopLoss = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(resultOpenTrade.StopLoss.ToString(), resultOpenTrade.Symbol.Digit);
                                                string bid = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(resultOpenTrade.Symbol.TickValue.Bid.ToString(), resultOpenTrade.Symbol.Digit);
                                                string ask = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(resultOpenTrade.Symbol.TickValue.Ask.ToString(), resultOpenTrade.Symbol.Digit);

                                                string content = "'" + resultOpenTrade.Investor.Code + "': order #" + resultOpenTrade.RefCommandID + " " + mode + " " + size + " " +
                                                                            resultOpenTrade.Symbol.Name + " at " + openPrice + " commission: " + resultOpenTrade.Commission + " [Success]";

                                                TradingServer.Facade.FacadeAddNewSystemLog(5, content, mode, resultOpenTrade.IpAddress, resultOpenTrade.Investor.Code);
                                                #endregion
                                            }
                                            else
                                            {
                                                TradingServer.Facade.FacadeAddNewSystemLog(6, result, "[MakeCommandNotify]", "", "");
                                            }
                                            #endregion

                                            break;
                                        }
                                    }
                                    #endregion
                                }
                            }
                            
                        }
                    }
                }
                else
                {
                    lock (Business.Market.nj4xObject)
                        Business.Market.NJ4XTickets.Remove(newNJ4XTicket);
                }
            }
            else
            {
                #region TIME OUT
                Model.Helper.Instance.SendCommandToClient(Command, EnumET5.CommandName.AddCommand, EnumET5.ET5Message.TIME_OUT, false, -1, 0);
                
                lock (Business.Market.nj4xObject)
                    Business.Market.NJ4XTickets.Remove(newNJ4XTicket);
                #endregion

                #region INSERT SYSTEM LOG EVENT MAKE COMMAND
                string size = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Command.Size.ToString(), 2);
                string openPrice = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Command.OpenPrice.ToString(), Command.Symbol.Digit);
                string takeProfit = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Command.TakeProfit.ToString(), Command.Symbol.Digit);
                string stopLoss = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Command.StopLoss.ToString(), Command.Symbol.Digit);
                string bid = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Command.Symbol.TickValue.Bid.ToString(), Command.Symbol.Digit);
                string ask = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Command.Symbol.TickValue.Ask.ToString(), Command.Symbol.Digit);
                string mode = TradingServer.Model.TradingCalculate.Instance.ConvertTypeIDToString(Command.Type.ID);

                string content = string.Empty;
                string comment = string.Empty;

                content = "'" + Command.Investor.Code + "': " + mode + " " + size + " " + Command.Symbol.Name + " at " + openPrice +
                                " sl: " + stopLoss + " tp: " + takeProfit + " (" + bid + "/" + ask + ")";
                comment = mode;
                content += " failed [time out]";

                TradingServer.Facade.FacadeAddNewSystemLog(5, content, comment, Command.IpAddress, Command.Investor.Code);
                #endregion
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Command"></param>
        /// <param name="executionType"></param>
        private void OrderCloseNj4x(Business.OpenTrade Command)
        {
            #region CHECK MANUAL DEALERS OR AUTOMATIC
            string executionType = Business.Market.marketInstance.GetExecutionType(Command.IGroupSecurity, "B03");
            #endregion

            //CONNECT TO NJ4X
            bool isPending = TradingServer.Model.TradingCalculate.Instance.CheckIsPendingPosition(Command.Type.ID);
            string nj4xOrderClose = string.Empty;

            if (isPending)
                nj4xOrderClose = NJ4XConnectSocket.MapNJ4X.Instance.MapOrderDelete(Command.ID, Command.Investor.Code, Command.Investor.UnZipPwd);
            else
            {
                if (Command.Symbol.ExecutionTrade == EnumMT4.Execution.REQUEST)
                {
                    nj4xOrderClose = NJ4XConnectSocket.MapNJ4X.Instance.MapOrderClose(Command.ID, Command.Size, Command.OpenPrice, Command.Investor.Code,
                                                                                    Command.Symbol.Name);
                }
                else
                {
                    nj4xOrderClose = NJ4XConnectSocket.MapNJ4X.Instance.MapOrderClose(Command.ID, Command.Size, Command.ClosePrice, Command.Investor.Code,
                                                                                    Command.Symbol.Name);
                }
            }   

            NJ4XConnectSocket.NJ4XTicket newNJ4XTicket = new NJ4XConnectSocket.NJ4XTicket();
            newNJ4XTicket.Ticket = Command.ID;
            newNJ4XTicket.IsClose = true;
            newNJ4XTicket.Ask = 0;
            newNJ4XTicket.Bid = 0;
            newNJ4XTicket.Code = Command.Investor.Code;
            newNJ4XTicket.IsDisable = false;
            newNJ4XTicket.IsReQuote = false;
            newNJ4XTicket.IsRequest = false;
            newNJ4XTicket.IsUpdate = false;
            newNJ4XTicket.OpenPrice = Command.OpenPrice;
            newNJ4XTicket.Symbol = Command.Symbol.Name;
            newNJ4XTicket.Execution = Command.Symbol.ExecutionTrade;
            newNJ4XTicket.Command = Command;

            Business.Market.NJ4XTickets.Add(newNJ4XTicket);

            string nj4xOrderCloseResult = NJ4XConnectSocket.NJ4XConnectSocketAsync.Instance.SendNJ4X(nj4xOrderClose);

            if (!string.IsNullOrEmpty(nj4xOrderCloseResult))
            {
                #region ORDER CLOSE
                string[] subnj4xResult = nj4xOrderCloseResult.Split('$');
                if (subnj4xResult[0] == "OrderClose")
                {
                    string[] subnj4x = subnj4xResult[1].Split('{');
                    bool _isClose = bool.Parse(subnj4x[0]);
                    int ticket = int.Parse(subnj4x[1]);
                    if (ticket == 138 || ticket == 4051 || ticket == 4055 || ticket == 4062 || ticket == 4063 ||
                        ticket == 4106 || ticket == 4107 || ticket == 4109 || ticket == 4110 || ticket == 4111 ||
                        ticket == 2 || ticket == 3 || ticket == 4 || ticket == 5 || ticket == 6 || ticket == 8 ||
                        ticket == 64 || ticket == 65 || ticket == 128 || ticket == 129 || ticket == 134 || ticket == 136 ||
                        ticket == 135 || ticket == 2004 || ticket == 9999 || ticket == 130 || ticket == 131 || ticket == 4108 ||
                        ticket == 133)
                    {
                        if (ticket != 135 && ticket != 138)
                        {
                            EnumET5.ET5Message eMessage = this.GetEnumWithTicket(ticket);
                            Model.Helper.Instance.SendCommandToClient(Command, EnumET5.CommandName.CloseCommand, eMessage, false, Command.ID, 1);

                            string strError = this.GetErrorWithCode(ticket);
                            //if (!string.IsNullOrEmpty(strError))
                            //{
                            //    string Message = "CloseCommand$False," + strError + "," + Command.ID + "," + Command.Investor.InvestorID + "," + Command.Symbol.Name + "," +
                            //       Command.Size + "," + true + "," + Command.OpenTime + "," + Command.OpenPrice + "," + Command.StopLoss + "," + Command.TakeProfit + "," +
                            //       Command.ClosePrice + "," + Command.Commission + "," + Command.Swap + "," + Command.Profit + "," + "Comment," + Command.ID + "," + Command.Type.Name + "," +
                            //       1 + "," + Command.ExpTime + "," + Command.ClientCode + "," + Command.CommandCode + "," + Command.IsHedged + "," + Command.Type.ID + "," + Command.Margin +
                            //       ",Close," + Command.CloseTime;

                            //    if (Command.Investor.ClientCommandQueue == null)
                            //        Command.Investor.ClientCommandQueue = new List<string>();

                            //    Command.Investor.ClientCommandQueue.Add(Message);
                            //}

                            lock (Business.Market.nj4xObject)
                                Business.Market.NJ4XTickets.Remove(newNJ4XTicket);

                            #region INSERT SYSTEM LOG EVENT CLOSE SPOT COMMAND ORDER
                            string mode = TradingServer.Model.TradingCalculate.Instance.ConvertTypeIDToString(Command.Type.ID);
                            string size = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Command.Size.ToString(), 2);
                            string openPrice = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Command.OpenPrice.ToString(), Command.Symbol.Digit);
                            string strClosePrice = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Command.ClosePrice.ToString(), Command.Symbol.Digit);
                            string stopLoss = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Command.StopLoss.ToString(), Command.Symbol.Digit);
                            string takeProfit = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Command.TakeProfit.ToString(), Command.Symbol.Digit);
                            string bid = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Command.Symbol.TickValue.Bid.ToString(), Command.Symbol.Digit);
                            string ask = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Command.Symbol.TickValue.Ask.ToString(), Command.Symbol.Digit);

                            string contentServer = "'" + Command.Investor.Code + "': close order #" + Command.CommandCode + " (" + mode + " " + size + " " +
                                Command.Symbol.Name + " at " + openPrice + ") at " + strClosePrice + " [Failed - " + Model.Helper.Instance.GetMessage(eMessage) + "]"; 

                            TradingServer.Facade.FacadeAddNewSystemLog(5, contentServer, "[Close position]", Command.Investor.IpAddress, Command.Investor.Code);
                            #endregion
                        }
                    }
                    else
                    {
                        if (executionType == "manual- but automatic if no dealer online" ||
                            executionType == "automatic only" ||
                            executionType == "manual only- no automation")
                        {
                            if (Business.Market.NJ4XTickets != null)
                            {
                                lock (Business.Market.nj4xObject)
                                {
                                    //if (Business.Market.NJ4XTickets != null)
                                    //{
                                    //    int countNJ4x = Business.Market.NJ4XTickets.Count;
                                    //    for (int n = 0; n < countNJ4x; n++)
                                    //    {
                                    //        if (Business.Market.NJ4XTickets[n].Code == newNJ4XTicket.Code)
                                    //        {
                                    //            Business.Market.NJ4XTickets.RemoveAt(n);
                                    //            break;
                                    //        }
                                    //    }
                                    //}

                                    int countNJ4X = Business.Market.NJ4XTickets.Count;
                                    for (int m = 0; m < countNJ4X; m++)
                                    {
                                        if (Business.Market.NJ4XTickets[m].Ticket == Command.RefCommandID)
                                        {
                                            lock (Business.Market.nj4xObject)
                                                Business.Market.NJ4XTickets.RemoveAt(m);

                                            List<Business.OpenTrade> commands = Command.Investor.CommandList;
                                            bool IsBuy = Model.Helper.Instance.IsBuy(Command.Type.ID);

                                            string cmd = BuildCommandElement5ConnectMT4.Mode.BuildCommand.Instance.ConvertGetCommandHistoryInfo(Command.RefCommandID);
                                            string cmdResult = Element5SocketConnectMT4.Business.SocketConnect.Instance.SendSocket(cmd);

                                            BuildCommandElement5ConnectMT4.Business.OnlineTrade listHistory = BuildCommandElement5ConnectMT4.Mode.ReceiveCommand.Instance.ConvertHistoryInfo(cmdResult);

                                            #region convert online trade to open trade
                                            Business.OpenTrade newOpenTrade = new Business.OpenTrade();
                                            if (listHistory != null && listHistory.CommandID > 0)
                                            {
                                                //newOpenTrade.ClientCode = tbCommandHistory[i].ClientCode;
                                                newOpenTrade.ClosePrice = listHistory.ClosePrice;
                                                newOpenTrade.CloseTime = listHistory.CloseTime;
                                                newOpenTrade.CommandCode = listHistory.CommandCode;
                                                newOpenTrade.Commission = listHistory.Commission;
                                                newOpenTrade.ExpTime = listHistory.TimeExpire;
                                                newOpenTrade.ID = listHistory.CommandID;
                                                //Investor

                                                #region FILL COMMAND TYPE
                                                switch (listHistory.CommandType)
                                                {
                                                    case "0":
                                                        {
                                                            Business.TradeType resultType = Business.Market.marketInstance.GetTradeType(1);
                                                            newOpenTrade.Type = resultType;
                                                        }
                                                        break;

                                                    case "1":
                                                        {
                                                            Business.TradeType resultType = Business.Market.marketInstance.GetTradeType(2);
                                                            newOpenTrade.Type = resultType;
                                                        }
                                                        break;

                                                    case "2":
                                                        {
                                                            Business.TradeType resultType = Business.Market.marketInstance.GetTradeType(7);
                                                            newOpenTrade.Type = resultType;
                                                        }
                                                        break;

                                                    case "3":
                                                        {
                                                            Business.TradeType resultType = Business.Market.marketInstance.GetTradeType(8);
                                                            newOpenTrade.Type = resultType;
                                                        }
                                                        break;

                                                    case "4":
                                                        {
                                                            Business.TradeType resultType = Business.Market.marketInstance.GetTradeType(9);
                                                            newOpenTrade.Type = resultType;
                                                        }
                                                        break;

                                                    case "5":
                                                        {
                                                            Business.TradeType resultType = Business.Market.marketInstance.GetTradeType(10);
                                                            newOpenTrade.Type = resultType;
                                                        }
                                                        break;

                                                    case "6":
                                                        {
                                                            if (listHistory.Profit >= 0)
                                                            {
                                                                Business.TradeType resultType = new Business.TradeType();
                                                                resultType.ID = 13;
                                                                resultType.Name = "Deposit";
                                                                newOpenTrade.Type = resultType;
                                                            }
                                                            else
                                                            {
                                                                Business.TradeType resultType = new Business.TradeType();
                                                                resultType.ID = 14;

                                                                resultType.Name = "Withdraw";
                                                                newOpenTrade.Type = resultType;
                                                            }
                                                        }
                                                        break;

                                                    case "7":
                                                        {
                                                            if (listHistory.Profit >= 0)
                                                            {
                                                                Business.TradeType resultType = new Business.TradeType();
                                                                resultType.ID = 15;
                                                                resultType.Name = "CreditIn";
                                                                newOpenTrade.Type = resultType;
                                                            }
                                                            else
                                                            {
                                                                Business.TradeType resultType = new Business.TradeType();
                                                                resultType.ID = 16;
                                                                resultType.Name = "CreditOut";
                                                                newOpenTrade.Type = resultType;
                                                            }
                                                        }
                                                        break;
                                                }
                                                #endregion

                                                #region Find Investor In Investor List
                                                if (Business.Market.InvestorList != null)
                                                {
                                                    int countInvestor = Business.Market.InvestorList.Count;
                                                    for (int k = 0; k < countInvestor; k++)
                                                    {
                                                        if (Business.Market.InvestorList[k].Code.ToUpper().Trim() == listHistory.InvestorCode)
                                                        {
                                                            newOpenTrade.Investor = Business.Market.InvestorList[k];
                                                            break;
                                                        }
                                                    }
                                                }
                                                #endregion

                                                newOpenTrade.OpenPrice = listHistory.OpenPrice;
                                                newOpenTrade.OpenTime = listHistory.OpenTime;

                                                if (newOpenTrade.Type.ID == 13 || newOpenTrade.Type.ID == 14 ||
                                                    newOpenTrade.Type.ID == 15 || newOpenTrade.Type.ID == 16)
                                                {
                                                    newOpenTrade.CloseTime = newOpenTrade.OpenTime;
                                                }

                                                newOpenTrade.Profit = listHistory.Profit;
                                                newOpenTrade.Size = listHistory.Size;
                                                newOpenTrade.StopLoss = listHistory.StopLoss;
                                                newOpenTrade.Swap = listHistory.Swap;
                                                //newOpenTrade.Taxes = listHistory[j].Taxes;
                                                newOpenTrade.Comment = listHistory.Comment;
                                                //newOpenTrade.AgentCommission = listHistory[j].AgentCommission;
                                                newOpenTrade.TakeProfit = listHistory.TakeProfit;

                                                #region Find Symbol In Symbol List
                                                if (Business.Market.SymbolList != null)
                                                {
                                                    bool Flag = false;
                                                    int countSymbol = Business.Market.SymbolList.Count;
                                                    for (int k = 0; k < countSymbol; k++)
                                                    {
                                                        if (Flag == true)
                                                            break;

                                                        if (Business.Market.SymbolList[k].Name.ToUpper().Trim() == listHistory.SymbolName.ToUpper().Trim())
                                                        {
                                                            newOpenTrade.Symbol = Business.Market.SymbolList[k];

                                                            Flag = true;
                                                            break;
                                                        }
                                                    }
                                                }
                                                #endregion
                                            }
                                            #endregion

                                            Command = newOpenTrade;

                                            #region For Command List
                                            for (int j = 0; j < commands.Count; j++)
                                            {
                                                if (commands[j].ID == Command.ID)
                                                {
                                                    int commandId = Command.ID;
                                                    Command.CommandCode = commands[j].CommandCode;

                                                    //NEW SOLUTION ADD COMMAND TO REMOVE LIST
                                                    Business.OpenRemove newOpenRemove = new OpenRemove();
                                                    newOpenRemove.InvestorID = Command.Investor.InvestorID;
                                                    newOpenRemove.OpenTradeID = commandId;
                                                    newOpenRemove.SymbolName = commands[j].Symbol.Name;
                                                    newOpenRemove.IsExecutor = true;
                                                    newOpenRemove.IsSymbol = true;
                                                    newOpenRemove.IsInvestor = false;
                                                    Business.Market.AddCommandToRemoveList(newOpenRemove);

                                                    //Close Command Complete Add Message To Client
                                                    if (Command.Investor.ClientCommandQueue == null)
                                                        Command.Investor.ClientCommandQueue = new List<string>();

                                                    #region Map Command Server To Client
                                                    if (Command.IsServer)
                                                    {
                                                        string Message = "CloseCommandByManager$True,Close Command Complete," + Command.ID + "," + Command.Investor.InvestorID + "," + Command.Symbol.Name + "," +
                                                            Command.Size + "," + IsBuy + "," + Command.OpenTime + "," + Command.OpenPrice + "," + Command.StopLoss + "," + Command.TakeProfit + "," +
                                                            Command.ClosePrice + "," + Command.Commission + "," + Command.Swap + "," + Command.Profit + "," + "Comment," + Command.ID + "," + Command.Type.Name + "," +
                                                            1 + "," + Command.ExpTime + "," + Command.ClientCode + "," + Command.CommandCode + "," + Command.IsHedged + "," + Command.Type.ID + "," + Command.Margin +
                                                            ",Close," + Command.CloseTime;

                                                        Command.Investor.ClientCommandQueue.Add(Message);

                                                        TradingServer.Facade.FacadeAddNewSystemLog(5, Message, "  ", Command.IpAddress, Command.Investor.Code);
                                                    }
                                                    else
                                                    {
                                                        string Message = "CloseCommand$True,Close Command Complete," + Command.ID + "," + Command.Investor.InvestorID + "," + Command.Symbol.Name + "," +
                                                            Command.Size + "," + IsBuy + "," + Command.OpenTime + "," + Command.OpenPrice + "," + Command.StopLoss + "," + Command.TakeProfit + "," +
                                                            Command.ClosePrice + "," + Command.Commission + "," + Command.Swap + "," + Command.Profit + "," + "Comment," + Command.ID + "," + Command.Type.Name + "," +
                                                            1 + "," + Command.ExpTime + "," + Command.ClientCode + "," + Command.CommandCode + "," + Command.IsHedged + "," + Command.Type.ID + "," + Command.Margin +
                                                            ",Close," + Command.CloseTime;

                                                        commands[j].Investor.ClientCommandQueue.Add(Message);

                                                        TradingServer.Facade.FacadeAddNewSystemLog(5, Message, "  ", Command.IpAddress, Command.Investor.Code);
                                                    }
                                                    #endregion

                                                    if (Business.Market.marketInstance.MQLCommands != null)
                                                    {
                                                        int countMQL = Business.Market.marketInstance.MQLCommands.Count;
                                                        for (int n = 0; n < countMQL; n++)
                                                        {
                                                            if (Business.Market.marketInstance.MQLCommands[n].InvestorCode == Command.Investor.Code)
                                                            {
                                                                Command.IpAddress = Business.Market.marketInstance.MQLCommands[n].IpAddress;
                                                                Business.Market.marketInstance.MQLCommands.Remove(Business.Market.marketInstance.MQLCommands[n]);
                                                                break;
                                                            }
                                                        }
                                                    }

                                                    #region INSERT SYSTEM LOG EVENT CLOSE SPOT COMMAND ORDER
                                                    string mode = "sell";
                                                    if (commands[j].Type.ID == 1)
                                                        mode = "buy";

                                                    string size = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Command.Size.ToString(), 2);
                                                    string openPrice = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Command.OpenPrice.ToString(), commands[j].Symbol.Digit);
                                                    string strClosePrice = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Command.ClosePrice.ToString(), commands[j].Symbol.Digit);
                                                    string stopLoss = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Command.StopLoss.ToString(), commands[j].Symbol.Digit);
                                                    string takeProfit = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Command.TakeProfit.ToString(), commands[j].Symbol.Digit);
                                                    string bid = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Command.Symbol.TickValue.Bid.ToString(), commands[j].Symbol.Digit);
                                                    string ask = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Command.Symbol.TickValue.Ask.ToString(), commands[j].Symbol.Digit);

                                                    string contentServer = "'" + commands[j].Investor.Code + "': close order #" + Command.CommandCode + " (" + mode + " " + size + " " +
                                                        Command.Symbol.Name + " at " + openPrice + ") at " + strClosePrice + " completed";

                                                    TradingServer.Facade.FacadeAddNewSystemLog(5, contentServer, "  ", Command.IpAddress, Command.Investor.Code);
                                                    #endregion

                                                    lock (Business.Market.syncObject)
                                                    {
                                                        bool deleteCommandInvestor = commands.Remove(commands[j]);
                                                    }

                                                    break;
                                                }
                                            }
                                            #endregion

                                            break;
                                        }
                                    }
                                        
                                }
                            }
                            
                        }
                    }
                }
                #endregion

                #region ORDER DELETE
                if (subnj4xResult[0] == "OrderDelete")
                {
                    string[] subnj4x = subnj4xResult[1].Split('{');
                    bool _isClose = bool.Parse(subnj4x[0]);
                    int ticket = int.Parse(subnj4x[1]);

                    if (ticket == 138 || ticket == 4051 || ticket == 4055 || ticket == 4062 || ticket == 4063 ||
                        ticket == 4106 || ticket == 4107 || ticket == 4109 || ticket == 4110 || ticket == 4111 ||
                        ticket == 2 || ticket == 3 || ticket == 4 || ticket == 5 || ticket == 6 || ticket == 8 ||
                        ticket == 64 || ticket == 65 || ticket == 128 || ticket == 129 || ticket == 134 || ticket == 136 ||
                        ticket == 135 || ticket == 2004 || ticket == 9999 || ticket == 130 || ticket == 4108 ||
                        ticket == 133)
                    {
                        if (ticket != 135 && ticket != 138)
                        {
                            EnumET5.ET5Message eMessage = this.GetEnumWithTicket(ticket);
                            Model.Helper.Instance.SendCommandToClient(Command, EnumET5.CommandName.CloseCommand, eMessage, false, Command.ID, 1);

                            //string strError = this.GetErrorWithCode(ticket);
                            //if (!string.IsNullOrEmpty(strError))
                            //{
                            //    string Message = "CloseCommand$False," + strError + "," + Command.ID + "," + Command.Investor.InvestorID + "," + Command.Symbol.Name + "," +
                            //       Command.Size + "," + true + "," + Command.OpenTime + "," + Command.OpenPrice + "," + Command.StopLoss + "," + Command.TakeProfit + "," +
                            //       Command.ClosePrice + "," + Command.Commission + "," + Command.Swap + "," + Command.Profit + "," + "Comment," + Command.ID + "," + Command.Type.Name + "," +
                            //       1 + "," + Command.ExpTime + "," + Command.ClientCode + "," + Command.CommandCode + "," + Command.IsHedged + "," + Command.Type.ID + "," + Command.Margin +
                            //       ",Close," + Command.CloseTime;

                            //    if (Command.Investor.ClientCommandQueue == null)
                            //        Command.Investor.ClientCommandQueue = new List<string>();

                            //    Command.Investor.ClientCommandQueue.Add(Message);
                            //}

                            lock (Business.Market.nj4xObject)
                                Business.Market.NJ4XTickets.Remove(newNJ4XTicket);

                            #region INSERT SYSTEM LOG EVENT CLOSE SPOT COMMAND ORDER
                            string mode = TradingServer.Model.TradingCalculate.Instance.ConvertTypeIDToString(Command.Type.ID);
                            string size = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Command.Size.ToString(), 2);
                            string openPrice = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Command.OpenPrice.ToString(), Command.Symbol.Digit);
                            string strClosePrice = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Command.ClosePrice.ToString(), Command.Symbol.Digit);
                            string stopLoss = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Command.StopLoss.ToString(), Command.Symbol.Digit);
                            string takeProfit = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Command.TakeProfit.ToString(), Command.Symbol.Digit);
                            string bid = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Command.Symbol.TickValue.Bid.ToString(), Command.Symbol.Digit);
                            string ask = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Command.Symbol.TickValue.Ask.ToString(), Command.Symbol.Digit);

                            string contentServer = "'" + Command.Investor.Code + "': close order #" + Command.CommandCode + " (" + mode + " " + size + " " +
                                Command.Symbol.Name + " at " + openPrice + ") at " + strClosePrice + " [Failed - " + Model.Helper.Instance.GetMessage(eMessage) + "]";

                            TradingServer.Facade.FacadeAddNewSystemLog(5, contentServer, "  ", Command.Investor.IpAddress, Command.Investor.Code);
                            #endregion
                        }
                    }
                    else
                    {
                        if (executionType == "manual- but automatic if no dealer online" ||
                            executionType == "automatic only" ||
                            executionType == "manual only- no automation")
                        {
                            lock (Business.Market.nj4xObject)
                                Business.Market.NJ4XTickets.Remove(newNJ4XTicket);
                        }
                    }
                }
                #endregion
            }
            else
            {
                #region TIME OUT
                Model.Helper.Instance.SendCommandToClient(Command, EnumET5.CommandName.CloseCommand, EnumET5.ET5Message.TRADE_TIMEOUT, false, Command.ID, 1);

                //unknown error
                //string Message = "CloseCommand$False,timeout," + Command.ID + "," + Command.Investor.InvestorID + "," + Command.Symbol.Name + "," +
                //                   Command.Size + "," + true + "," + Command.OpenTime + "," + Command.OpenPrice + "," + Command.StopLoss + "," + Command.TakeProfit + "," +
                //                   Command.ClosePrice + "," + Command.Commission + "," + Command.Swap + "," + Command.Profit + "," + "Comment," + Command.ID + "," + Command.Type.Name + "," +
                //                   1 + "," + Command.ExpTime + "," + Command.ClientCode + "," + Command.CommandCode + "," + Command.IsHedged + "," + Command.Type.ID + "," + Command.Margin +
                //                   ",Close," + Command.CloseTime;

                //if (Command.Investor.ClientCommandQueue == null)
                //    Command.Investor.ClientCommandQueue = new List<string>();

                //Command.Investor.ClientCommandQueue.Add(Message);

                lock (Business.Market.nj4xObject)
                    Business.Market.NJ4XTickets.Remove(newNJ4XTicket);
                #endregion

                #region INSERT SYSTEM LOG EVENT CLOSE SPOT COMMAND ORDER
                string mode = TradingServer.Model.TradingCalculate.Instance.ConvertTypeIDToString(Command.Type.ID);
                string size = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Command.Size.ToString(), 2);
                string openPrice = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Command.OpenPrice.ToString(), Command.Symbol.Digit);
                string strClosePrice = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Command.ClosePrice.ToString(), Command.Symbol.Digit);
                string stopLoss = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Command.StopLoss.ToString(), Command.Symbol.Digit);
                string takeProfit = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Command.TakeProfit.ToString(), Command.Symbol.Digit);
                string bid = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Command.Symbol.TickValue.Bid.ToString(), Command.Symbol.Digit);
                string ask = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Command.Symbol.TickValue.Ask.ToString(), Command.Symbol.Digit);

                string contentServer = "'" + Command.Investor.Code + "': close order #" + Command.CommandCode + " (" + mode + " " + size + " " +
                    Command.Symbol.Name + " at " + openPrice + ") at " + strClosePrice + " [Failed - time out]";

                TradingServer.Facade.FacadeAddNewSystemLog(5, contentServer, "  ", Command.Investor.IpAddress, Command.Investor.Code);
                #endregion
            }
        }