/// <summary>
        /// 
        /// </summary>
        /// <param name="listRollBack"></param>
        internal bool MultipleCloseCommand(Business.OpenTrade command)
        {
            bool result = false;
            if (this.CommandList != null && this.CommandList.Count > 0)
            {
                int count = this.CommandList.Count;
                for (int i = 0; i < this.CommandList.Count; i++)
                {
                    if (this.CommandList[i].ID == command.ID)
                    {
                        bool IsBuy = false;
                        if (this.CommandList[i].Type.ID == 1 || this.CommandList[i].Type.ID == 7 ||
                            this.CommandList[i].Type.ID == 9 || this.CommandList[i].Type.ID == 11 ||
                            this.CommandList[i].Type.ID == 17 || this.CommandList[i].Type.ID == 19)
                        {
                            IsBuy = true;
                        }

                        #region SET NEW DATA
                        this.CommandList[i].CloseTime = DateTime.Now;
                        this.CommandList[i].ClosePrice = command.ClosePrice;

                        //this.CommandList[i].CalculatorProfitCommand(this.CommandList[i]);
                        //this.CommandList[i].Profit = this.CommandList[i].Symbol.ConvertCurrencyToUSD(this.CommandList[i].Symbol.Currency, this.CommandList[i].Profit, false,
                        //    this.CommandList[i].SpreaDifferenceInOpenTrade, this.CommandList[i].Symbol.Digit);
                        #endregion

                        #region CLOSE COMMAND

                        #region COMMAND CODE(REMOVE COMMAND IN COMMAND EXECUTOR AND SYMBOL LIST
                        ////Remove Command In Symbol List
                        //bool temp = TradingServer.Facade.FacadeRemoveOpenTradeInCommandList(this.CommandList[i].ID);

                        ////Remove Command In Command Executor
                        //bool deleteCommandExe = TradingServer.Facade.FacadeRemoveOpenTradeInCommandExecutor(this.CommandList[i].ID);
                        #endregion

                        //NEW SOLUTION ADD COMMAND TO REMOVE LIST
                        //Business.OpenTrade newOpenTrade = this.CommandList[i];
                        Business.OpenRemove newOpenRemove = new OpenRemove();
                        newOpenRemove.InvestorID = this.InvestorID;
                        newOpenRemove.OpenTradeID = this.CommandList[i].ID;
                        newOpenRemove.SymbolName = this.CommandList[i].Symbol.Name;
                        newOpenRemove.IsExecutor = true;
                        newOpenRemove.IsSymbol = true;
                        newOpenRemove.IsInvestor = false;
                        Business.Market.AddCommandToRemoveList(newOpenRemove);

                        //if (temp && deleteCommandExe)
                        //{
                            double totalProfit = Math.Round(this.CommandList[i].Profit + this.CommandList[i].Commission + this.CommandList[i].Swap, 2);

                            //Update Balance Of Investor Account
                            bool updateBalance = this.UpdateBalance(this.CommandList[i].Investor.InvestorID, this.Balance);

                            if (updateBalance)
                            {
                                //'00001140': close command #00148535 balance : 123456 command profit : 10000
                                string content = "'" + this.Code + "': multiple close command #" + this.CommandList[i].CommandCode + " balance : " + this.Balance + " command profit: " + totalProfit +
                                    " Commission: " + this.CommandList[i].Commission + " Swap: " + this.CommandList[i].Swap;

                                TradingServer.Facade.FacadeAddNewSystemLog(5, content, "[Close Command]", "", this.Code);

                                this.Balance += totalProfit;

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

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

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

                                    this.ClientCommandQueue.Add(Message);
                                }
                                else
                                {
                                    string Message = "CloseCommand$True,Close Command Complete," + this.CommandList[i].ID + "," + this.CommandList[i].Investor.InvestorID + "," +
                                        this.CommandList[i].Symbol.Name + "," + this.CommandList[i].Size + "," + IsBuy + "," + this.CommandList[i].OpenTime + "," +
                                        this.CommandList[i].OpenPrice + "," + this.CommandList[i].StopLoss + "," + this.CommandList[i].TakeProfit + "," +
                                        this.CommandList[i].ClosePrice + "," + this.CommandList[i].Commission + "," + this.CommandList[i].Swap + "," +
                                        this.CommandList[i].Profit + "," + "Comment," + this.CommandList[i].ID + "," + this.CommandList[i].Type.Name + "," +
                                        1 + "," + this.CommandList[i].ExpTime + "," + this.CommandList[i].ClientCode + "," + this.CommandList[i].CommandCode + "," +
                                        this.CommandList[i].IsHedged + "," + this.CommandList[i].Type.ID + "," + this.CommandList[i].Margin +
                                        ",Close," + this.CommandList[i].CloseTime;

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

                                    this.ClientCommandQueue.Add(Message);
                                }
                                #endregion

                                #region INSERT SYSTEM LOG EVENT CLOSE SPOT COMMAND ORDER
                                string mode = TradingServer.Facade.FacadeGetTypeNameByTypeID(this.CommandList[i].Type.ID).ToLower();
                                string size = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(this.CommandList[i].Size.ToString(), 2);
                                string openPrice = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(this.CommandList[i].OpenPrice.ToString(), this.CommandList[i].Symbol.Digit);
                                string stopLoss = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(this.CommandList[i].StopLoss.ToString(), this.CommandList[i].Symbol.Digit);
                                string takeProfit = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(this.CommandList[i].TakeProfit.ToString(), this.CommandList[i].Symbol.Digit);
                                string bid = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(this.CommandList[i].Symbol.TickValue.Bid.ToString(), this.CommandList[i].Symbol.Digit);
                                string ask = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(this.CommandList[i].Symbol.TickValue.Ask.ToString(), this.CommandList[i].Symbol.Digit);

                                string contentServer = "'" + this.Code + "': multiple close order #" + this.CommandList[i].CommandCode + " (" + mode + " " + size + " " +
                                    this.CommandList[i].Symbol.Name + " at " + this.CommandList[i].OpenPrice + ") at " + command.ClosePrice + " completed";

                                TradingServer.Facade.FacadeAddNewSystemLog(5, contentServer, "[multiple close]", this.CommandList[i].Investor.IpAddress, this.CommandList[i].Investor.Code);
                                #endregion

                                bool removeOpenTrade = false;
                                lock (Business.Market.syncObject)
                                {
                                    //Remove Command In Investor List
                                    removeOpenTrade = this.CommandList.Remove(this.CommandList[i]);
                                }

                                if (removeOpenTrade)
                                {
                                    if (this.CommandList.Count > 0)
                                    {
                                        //RECACULATION TOTAL MARGIN OF INVESTOR
                                        Business.Margin newMargin = new Margin();
                                        newMargin = this.CommandList[0].Symbol.CalculationTotalMargin(this.CommandList);
                                        this.Margin = newMargin.TotalMargin;
                                        this.FreezeMargin = newMargin.TotalFreezeMargin;
                                    }
                                    else
                                    {
                                        this.Margin = 0;
                                    }

                                    //SEND NOTIFY TO MANAGER THEN CLOSE COMMAND
                                    TradingServer.Facade.FacadeSendNotifyManagerRequest(3, this);
                                }
                                else
                                {
                                    return false;
                                }
                            }
                            else
                            {
                                return false;
                            }
                        //}
                        //else
                        //{
                        //    return false;
                        //}

                        #endregion

                        result = true;
                        break;
                    }
                }
            }

            return result;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Mont"></param>
        /// <param name="InvestorID"></param>
        /// <returns></returns>
        internal bool SubCredit(double Money, int InvestorID, string Comment)
        {
            bool Result = false;

            Money = Math.Round(Money, 2);

            if (Money <= 0)
                return false;

            if (Business.Market.InvestorList != null)
            {
                int count = Business.Market.InvestorList.Count;
                for (int i = 0; i < count; i++)
                {
                    if (Business.Market.InvestorList[i].InvestorID == InvestorID)
                    {
                        double Credit = 0;
                        double tempCredit = 0;
                        double creditBefore = Business.Market.InvestorList[i].Credit;
                        tempCredit = Math.Round(Business.Market.InvestorList[i].Credit, 2) - Money;

                        Credit = (Business.Market.InvestorList[i].Balance + tempCredit);

                        if (tempCredit >= 0)
                        {
                            if (this.InvestorGroupInstance != null)
                            {
                                //RECALCULATION ACCOUNT
                                this.ReCalculationAccount();
                            }
                            else
                            {
                                #region RECALCULATION INVESTOR ACCOUNT
                                if (Business.Market.InvestorList[i].CommandList != null && Business.Market.InvestorList[i].CommandList.Count > 0)
                                {
                                    //Call function Total Margin Of Investor
                                    Business.Margin newMargin = new Business.Margin();
                                    newMargin = Business.Market.InvestorList[i].CommandList[0].Symbol.CalculationTotalMargin(Business.Market.InvestorList[i].CommandList);
                                    Business.Market.InvestorList[i].Margin = newMargin.TotalMargin;
                                    Business.Market.InvestorList[i].FreezeMargin = newMargin.TotalFreezeMargin;

                                    Business.Market.InvestorList[i].Profit = Business.Market.InvestorList[i].InvestorGroupInstance.CalculationTotalProfit(Business.Market.InvestorList[i].CommandList);
                                    Business.Market.InvestorList[i].Equity = Credit + Business.Market.InvestorList[i].Profit;
                                    double Loss = Business.Market.InvestorList[i].InvestorGroupInstance.CalculationTotalLoss(Business.Market.InvestorList[i].CommandList);
                                    double Profit = Business.Market.InvestorList[i].InvestorGroupInstance.CalculationTotalProfitPositive(Business.Market.InvestorList[i].CommandList);
                                    int Method = -1;
                                    switch (Business.Market.InvestorList[i].InvestorGroupInstance.FreeMargin)
                                    {
                                        case "do not use unrealized profit/loss":
                                            Method = 0;
                                            break;
                                        case "use unrealized profit/loss":
                                            Method = 1;
                                            break;
                                        case "use unrealized profit only":
                                            Method = 2;
                                            break;
                                        case "use unrealized loss only":
                                            Method = 3;
                                            break;
                                    }

                                    double totalMargin = Business.Market.InvestorList[i].Margin + Business.Market.InvestorList[i].FreezeMargin;
                                    Business.Market.InvestorList[i].FreeMargin = Business.Market.InvestorList[i].InvestorGroupInstance.CalculationTotalFreeMargin(totalMargin, Credit, Business.Market.InvestorList[i].Equity, Profit, Loss, Method);
                                    Business.Market.InvestorList[i].MarginLevel = (Business.Market.InvestorList[i].Equity * 100) / (Business.Market.InvestorList[i].Margin + Business.Market.InvestorList[i].FreezeMargin);
                                }
                                else
                                {
                                    Business.Market.InvestorList[i].Margin = 0;
                                    Business.Market.InvestorList[i].FreeMargin = 0;
                                    Business.Market.InvestorList[i].MarginLevel = 0;
                                    Business.Market.InvestorList[i].Profit = 0;
                                }

                                //Check Stop Out Level Of Account
                                if (Business.Market.InvestorList[i].MarginLevel <= Business.Market.InvestorList[i].InvestorGroupInstance.MarginStopOut)
                                {
                                    string comment = "so: " + Math.Round(Business.Market.InvestorList[i].MarginLevel, 2) + "%/" + Business.Market.InvestorList[i].Equity + "/" + Business.Market.InvestorList[i].Margin;

                                    //Call Function Close Command
                                    if (Business.Market.InvestorList[i].CommandList != null && Business.Market.InvestorList[i].CommandList.Count > 0)
                                    {
                                        for (int j = 0; j < Business.Market.InvestorList[i].CommandList.Count; j++)
                                        {
                                            bool isTrade = TradingServer.Facade.FacadeCheckStatusSymbol(Business.Market.InvestorList[i].CommandList[j].Symbol.Name.Trim());
                                            if (isTrade == true)
                                            {
                                                bool isPending = TradingServer.Model.TradingCalculate.Instance.CheckIsPendingPosition(Business.Market.InvestorList[i].CommandList[j].Type.ID);
                                                if (isPending)
                                                    Business.Market.InvestorList[i].CommandList[j].Profit = 0;

                                                #region Command Is Close
                                                int ResultHistory = -1;
                                                //Add Command To Command History
                                                Business.Market.InvestorList[i].CommandList[j].Comment = comment;

                                                if (isPending)
                                                {
                                                    ResultHistory = TradingServer.Facade.FacadeAddNewCommandHistory(Business.Market.InvestorList[i].CommandList[j].Investor.InvestorID,
                                                    Business.Market.InvestorList[i].CommandList[j].Type.ID, Business.Market.InvestorList[i].CommandList[j].CommandCode,
                                                    Business.Market.InvestorList[i].CommandList[j].OpenTime, Business.Market.InvestorList[i].CommandList[j].OpenPrice,
                                                    Business.Market.InvestorList[i].CommandList[j].CloseTime, Business.Market.InvestorList[i].CommandList[j].ClosePrice,
                                                    0, Business.Market.InvestorList[i].CommandList[j].Swap,
                                                    Business.Market.InvestorList[i].CommandList[j].Commission, Business.Market.InvestorList[i].CommandList[j].ExpTime,
                                                    Business.Market.InvestorList[i].CommandList[j].Size, Business.Market.InvestorList[i].CommandList[j].StopLoss,
                                                    Business.Market.InvestorList[i].CommandList[j].TakeProfit, Business.Market.InvestorList[i].CommandList[j].ClientCode,
                                                    Business.Market.InvestorList[i].CommandList[j].Symbol.SymbolID,
                                                    Business.Market.InvestorList[i].CommandList[j].Taxes,
                                                    Business.Market.InvestorList[i].CommandList[j].AgentCommission, /*Business.Market.InvestorList[i].CommandList[j].Comment*/comment, "12",
                                                    Business.Market.InvestorList[i].CommandList[j].TotalSwap,
                                                    Business.Market.InvestorList[i].CommandList[j].RefCommandID,
                                                    Business.Market.InvestorList[i].CommandList[j].AgentRefConfig,
                                                    Business.Market.InvestorList[i].CommandList[j].IsActivePending,
                                                    Business.Market.InvestorList[i].CommandList[j].IsStopLossAndTakeProfit);
                                                }
                                                else
                                                {
                                                    ResultHistory = TradingServer.Facade.FacadeAddNewCommandHistory(Business.Market.InvestorList[i].CommandList[j].Investor.InvestorID,
                                                    Business.Market.InvestorList[i].CommandList[j].Type.ID, Business.Market.InvestorList[i].CommandList[j].CommandCode,
                                                    Business.Market.InvestorList[i].CommandList[j].OpenTime, Business.Market.InvestorList[i].CommandList[j].OpenPrice,
                                                    Business.Market.InvestorList[i].CommandList[j].CloseTime, Business.Market.InvestorList[i].CommandList[j].ClosePrice,
                                                    Business.Market.InvestorList[i].CommandList[j].Profit, Business.Market.InvestorList[i].CommandList[j].Swap,
                                                    Business.Market.InvestorList[i].CommandList[j].Commission, Business.Market.InvestorList[i].CommandList[j].ExpTime,
                                                    Business.Market.InvestorList[i].CommandList[j].Size, Business.Market.InvestorList[i].CommandList[j].StopLoss,
                                                    Business.Market.InvestorList[i].CommandList[j].TakeProfit, Business.Market.InvestorList[i].CommandList[j].ClientCode,
                                                    Business.Market.InvestorList[i].CommandList[j].Symbol.SymbolID,
                                                    Business.Market.InvestorList[i].CommandList[j].Taxes,
                                                    Business.Market.InvestorList[i].CommandList[j].AgentCommission, /*Business.Market.InvestorList[i].CommandList[j].Comment*/comment, "12",
                                                    Business.Market.InvestorList[i].CommandList[j].TotalSwap,
                                                    Business.Market.InvestorList[i].CommandList[j].RefCommandID,
                                                    Business.Market.InvestorList[i].CommandList[j].AgentRefConfig,
                                                    Business.Market.InvestorList[i].CommandList[j].IsActivePending,
                                                    Business.Market.InvestorList[i].CommandList[j].IsStopLossAndTakeProfit);
                                                }

                                                if (ResultHistory > 0)
                                                {
                                                    //Log Stop Out
                                                    string content = string.Empty;
                                                    string mode = TradingServer.Facade.FacadeGetTypeNameByTypeID(Business.Market.InvestorList[i].CommandList[j].Type.ID).ToLower();
                                                    string size = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Business.Market.InvestorList[i].CommandList[j].Size.ToString(), 2);
                                                    string openPrice = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Business.Market.InvestorList[i].CommandList[j].OpenPrice.ToString(),
                                                        Business.Market.InvestorList[i].CommandList[j].Symbol.Digit);

                                                    content = "'" + Business.Market.InvestorList[i].CommandList[j].Investor.Code + "': stop out #" +
                                                        Business.Market.InvestorList[i].CommandList[j].CommandCode + " " + mode + " " + size + " " +
                                                        Business.Market.InvestorList[i].CommandList[j].Symbol.Name + " " + openPrice + " (" +
                                                        Business.Market.InvestorList[i].CommandList[j].Symbol.TickValue.Bid + "/" +
                                                        Business.Market.InvestorList[i].CommandList[j].Symbol.TickValue.Ask + ")";

                                                    TradingServer.Facade.FacadeAddNewSystemLog(5, content, "[stop out]", "", Business.Market.InvestorList[i].Code);

                                                    #region COMMAND CODE(REMOVE COMAMND IN COMMAND EXECUTOR AND SYMBOL LIST
                                                    ////Remove Command In Symbol List
                                                    //bool resultRemoveCommandSymbol = TradingServer.Facade.FacadeRemoveOpenTradeInCommandList(Business.Market.InvestorList[i].CommandList[j].ID);

                                                    ////Remove Command In Command Executor
                                                    //bool deleteCommandExe = TradingServer.Facade.FacadeRemoveOpenTradeInCommandExecutor(Business.Market.InvestorList[i].CommandList[j].ID);
                                                    #endregion

                                                    //NEW SOLUTION ADD COMMAND TO REMOVE LIST
                                                    //Business.OpenTrade newOpenTrade = this.CommandList[i];
                                                    Business.OpenRemove newOpenRemove = new OpenRemove();
                                                    newOpenRemove.InvestorID = this.InvestorID;
                                                    newOpenRemove.OpenTradeID = Business.Market.InvestorList[i].CommandList[j].ID;
                                                    newOpenRemove.SymbolName = Business.Market.InvestorList[i].CommandList[j].Symbol.Name;
                                                    newOpenRemove.IsExecutor = true;
                                                    newOpenRemove.IsSymbol = true;
                                                    newOpenRemove.IsInvestor = false;
                                                    Business.Market.AddCommandToRemoveList(newOpenRemove);

                                                    if (!isPending)
                                                    {
                                                        double totalProfit = Math.Round(Business.Market.InvestorList[i].CommandList[j].Profit + Business.Market.InvestorList[i].CommandList[j].Commission - Business.Market.InvestorList[i].CommandList[j].Swap, 2);
                                                        Business.Market.InvestorList[i].Balance += totalProfit;
                                                        //Update Balance Of Investor Account
                                                        this.UpdateBalance(Business.Market.InvestorList[i].CommandList[j].Investor.InvestorID, Business.Market.InvestorList[i].Balance);
                                                    }

                                                    //Update Command In Database
                                                    TradingServer.Facade.FacadeDeleteOpenTradeByID(Business.Market.InvestorList[i].CommandList[j].ID);

                                                    //Close Command Complete Add Message To Client
                                                    if (Business.Market.InvestorList[i].ClientCommandQueue == null)
                                                        Business.Market.InvestorList[i].ClientCommandQueue = new List<string>();

                                                    #region Map Command Server To Client
                                                    string Message = "StopOut$True,Close Command Complete," + Business.Market.InvestorList[i].CommandList[j].ID + "," +
                                                        Business.Market.InvestorList[i].CommandList[j].Investor.InvestorID + "," +
                                                        Business.Market.InvestorList[i].CommandList[j].Symbol.Name + "," +
                                                        Business.Market.InvestorList[i].CommandList[j].Size + "," + false + "," +
                                                        Business.Market.InvestorList[i].CommandList[j].OpenTime + "," +
                                                        Business.Market.InvestorList[i].CommandList[j].OpenPrice + "," +
                                                        Business.Market.InvestorList[i].CommandList[j].StopLoss + "," +
                                                        Business.Market.InvestorList[i].CommandList[j].TakeProfit + "," +
                                                        Business.Market.InvestorList[i].CommandList[j].ClosePrice + "," +
                                                        Business.Market.InvestorList[i].CommandList[j].Commission + "," +
                                                        Business.Market.InvestorList[i].CommandList[j].Swap + "," +
                                                        Business.Market.InvestorList[i].CommandList[j].Profit + "," +
                                                        Business.Market.InvestorList[i].CommandList[j].Comment + "," +
                                                        Business.Market.InvestorList[i].CommandList[j].ID + "," +
                                                        Business.Market.InvestorList[i].CommandList[j].Type.Name + "," +
                                                        1 + "," + Business.Market.InvestorList[i].CommandList[j].ExpTime + "," +
                                                        Business.Market.InvestorList[i].CommandList[j].ClientCode + "," +
                                                        Business.Market.InvestorList[i].CommandList[j].CommandCode + "," +
                                                        Business.Market.InvestorList[i].CommandList[j].IsHedged + "," +
                                                        Business.Market.InvestorList[i].CommandList[j].Type.ID + "," +
                                                        Business.Market.InvestorList[i].CommandList[j].Margin + ",Close";

                                                    if (Business.Market.InvestorList[i].ClientCommandQueue == null)
                                                        Business.Market.InvestorList[i].ClientCommandQueue = new List<string>();

                                                    //int countInvestorOnline = Business.Market.InvestorList[i].CountInvestorOnline(Business.Market.InvestorList[i].InvestorID);
                                                    //if (countInvestorOnline > 0)
                                                    Business.Market.InvestorList[i].ClientCommandQueue.Add(Message);

                                                    //SEND COMMAND TO AGENT SERVER

                                                    string msg = "StopOut$True,Close Command Complete," + Business.Market.InvestorList[i].CommandList[j].ID + "," +
                                                            Business.Market.InvestorList[i].CommandList[j].Investor.InvestorID + "," +
                                                            Business.Market.InvestorList[i].CommandList[j].Symbol.Name + "," +
                                                            Business.Market.InvestorList[i].CommandList[j].Size + "," + false + "," +
                                                            Business.Market.InvestorList[i].CommandList[j].OpenTime + "," +
                                                            Business.Market.InvestorList[i].CommandList[j].OpenPrice + "," +
                                                            Business.Market.InvestorList[i].CommandList[j].StopLoss + "," +
                                                            Business.Market.InvestorList[i].CommandList[j].TakeProfit + "," +
                                                            Business.Market.InvestorList[i].CommandList[j].ClosePrice + "," +
                                                            Business.Market.InvestorList[i].CommandList[j].Commission + "," +
                                                            Business.Market.InvestorList[i].CommandList[j].Swap + "," +
                                                            Business.Market.InvestorList[i].CommandList[j].Profit + "," + "Comment," +
                                                            Business.Market.InvestorList[i].CommandList[j].ID + "," +
                                                            Business.Market.InvestorList[i].CommandList[j].Type.Name + "," +
                                                            1 + "," + Business.Market.InvestorList[i].CommandList[j].ExpTime + "," +
                                                            Business.Market.InvestorList[i].CommandList[j].ClientCode + "," +
                                                            Business.Market.InvestorList[i].CommandList[j].CommandCode + "," +
                                                            Business.Market.InvestorList[i].CommandList[j].IsHedged + "," +
                                                            Business.Market.InvestorList[i].CommandList[j].Type.ID + "," +
                                                            Business.Market.InvestorList[i].CommandList[j].Margin +
                                                            ",Close," + Business.Market.InvestorList[i].CommandList[j].CloseTime;

                                                    msg += Business.Market.InvestorList[i].CommandList[j].AgentRefConfig;

                                                    Business.AgentNotify newAgentNotify = new AgentNotify();
                                                    newAgentNotify.NotifyMessage = msg;
                                                    TradingServer.Agent.AgentConfig.Instance.AddNotifyToAgent(newAgentNotify, Business.Market.InvestorList[i].InvestorGroupInstance);

                                                    #endregion

                                                    //SEND NOTIFY TO MANAGER
                                                    TradingServer.Facade.FacadeSendNoticeManagerRequest(2, Business.Market.InvestorList[i].CommandList[j]);

                                                    lock (Business.Market.syncObject)
                                                    {
                                                        //Remove Command In Investor List
                                                        Business.Market.InvestorList[i].CommandList.Remove(Business.Market.InvestorList[i].CommandList[j]);
                                                    }

                                                    if (Business.Market.InvestorList[i].CommandList.Count > 0 && Business.Market.InvestorList[i].CommandList != null)
                                                    {
                                                        //Call function Total Margin Of Investor
                                                        Business.Margin totalMargin = new Business.Margin();
                                                        totalMargin = Business.Market.InvestorList[i].CommandList[0].Symbol.CalculationTotalMargin(Business.Market.InvestorList[i].CommandList);
                                                        Business.Market.InvestorList[i].Margin = totalMargin.TotalMargin;
                                                        Business.Market.InvestorList[i].FreezeMargin = totalMargin.TotalFreezeMargin;

                                                        Business.Market.InvestorList[i].Profit = Business.Market.InvestorList[i].InvestorGroupInstance.CalculationTotalProfit(Business.Market.InvestorList[i].CommandList);
                                                        Business.Market.InvestorList[i].Equity = Business.Market.InvestorList[i].Balance +
                                                            Business.Market.InvestorList[i].Credit + Business.Market.InvestorList[i].Profit;
                                                    }
                                                    else
                                                    {
                                                        Business.Market.InvestorList[i].Margin = 0;
                                                        Business.Market.InvestorList[i].Profit = 0;
                                                        Business.Market.InvestorList[i].Equity = 0;
                                                    }

                                                    TradingServer.Facade.FacadeSendNotifyManagerRequest(3, Business.Market.InvestorList[i]);
                                                }
                                                else
                                                {
                                                    #region Map Command Server To Client
                                                    string Message = "StopOut$False,Can't Add Command To Database," +
                                                        Business.Market.InvestorList[i].CommandList[j].ID + "," +
                                                        Business.Market.InvestorList[i].CommandList[j].Investor.InvestorID + "," +
                                                        Business.Market.InvestorList[i].CommandList[j].Symbol.Name + "," +
                                                        Business.Market.InvestorList[i].CommandList[j].Size + "," + false + "," +
                                                        Business.Market.InvestorList[i].CommandList[j].OpenTime + "," +
                                                        Business.Market.InvestorList[i].CommandList[j].OpenPrice + "," +
                                                        Business.Market.InvestorList[i].CommandList[j].StopLoss + "," +
                                                        Business.Market.InvestorList[i].CommandList[j].TakeProfit + "," +
                                                        Business.Market.InvestorList[i].CommandList[j].ClosePrice + "," +
                                                        Business.Market.InvestorList[i].CommandList[j].Commission + "," +
                                                        Business.Market.InvestorList[i].CommandList[j].Swap + "," +
                                                        Business.Market.InvestorList[i].CommandList[j].Profit + "," + "Comment," +
                                                        Business.Market.InvestorList[i].CommandList[j].ID + "," +
                                                        Business.Market.InvestorList[i].CommandList[j].Type.Name + "," + 1 + "," +
                                                        Business.Market.InvestorList[i].CommandList[j].ExpTime + "," +
                                                        Business.Market.InvestorList[i].CommandList[j].ClientCode + "," +
                                                        Business.Market.InvestorList[i].CommandList[j].CommandCode + "," +
                                                        Business.Market.InvestorList[i].CommandList[j].IsHedged + "," +
                                                        Business.Market.InvestorList[i].CommandList[j].Type.ID + "," +
                                                        Business.Market.InvestorList[i].CommandList[j].Margin + ",Close";

                                                    if (Business.Market.InvestorList[i].ClientCommandQueue == null)
                                                        Business.Market.InvestorList[i].ClientCommandQueue = new List<string>();

                                                    Business.Market.InvestorList[i].ClientCommandQueue.Add(Message);
                                                    #endregion
                                                }
                                                #endregion

                                                j--;
                                            }
                                        }
                                    }
                                }
                                #endregion
                            }

                            //Call Function Update Credit In Database
                            bool UpdateCredit = false;
                            UpdateCredit = this.UpdateCredit(InvestorID, tempCredit);

                            if (UpdateCredit)
                            {
                                //Update Credit In Class Market
                                Business.Market.InvestorList[i].Credit = tempCredit;

                                if (Business.Market.InvestorList[i].ClientCommandQueue == null)
                                    Business.Market.InvestorList[i].ClientCommandQueue = new List<string>();

                                string Message = "SubCredit$True,Add Credit Complete";
                                //int countOnline = Business.Market.InvestorList[i].CountInvestorOnline(Business.Market.InvestorList[i].InvestorID);
                                //if (countOnline > 0)
                                Business.Market.InvestorList[i].ClientCommandQueue.Add(Message);

                                //SEND COMMAND TO AGENT SERVER
                                string msg = Message + "," + Money + "," + InvestorID;
                                Business.AgentNotify newAgentNotify = new AgentNotify();
                                newAgentNotify.NotifyMessage = msg;
                                TradingServer.Agent.AgentConfig.Instance.AddNotifyToAgent(newAgentNotify);

                                Business.InvestorAccountLog newInvestorAccountLog = new InvestorAccountLog();
                                newInvestorAccountLog.Name = Business.Market.InvestorList[i].NickName;
                                newInvestorAccountLog.InvestorID = Business.Market.InvestorList[i].InvestorID;
                                newInvestorAccountLog.Date = DateTime.Now;
                                newInvestorAccountLog.Comment = Comment;
                                newInvestorAccountLog.Amount = Money;
                                newInvestorAccountLog.Code = "CRD01";

                                //int ResultLog = TradingServer.Facade.FacadeAddInvestorAccountLog(newInvestorAccountLog);

                                //string CommandCode = TradingServer.Model.TradingCalculate.Instance.BuildCommandCode(ResultLog.ToString());

                                //TradingServer.Facade.FacadeUpdateDealID(ResultLog, CommandCode);

                                //SEND NOTIFY TO MANAGER THEN SUB CREDIT
                                TradingServer.Facade.FacadeSendNotifyManagerRequest(3, Business.Market.InvestorList[i]);
                            }

                            int resultAddCommandHistory = TradingServer.Facade.FacadeAddNewCommandHistory(InvestorID, 16, "", DateTime.Now, 0, DateTime.Now, 0, Money, 0, 0, DateTime.Now, 0, 0, 0, "", -1, 0, 0, Comment, "13", 0, -1, "", false, false);
                            if (resultAddCommandHistory > 0)
                            {
                                string commandCode = TradingServer.Model.TradingCalculate.Instance.BuildCommandCode(resultAddCommandHistory.ToString());
                                TradingServer.Facade.FacadeUpdateCommandCodeHistory(commandCode, resultAddCommandHistory);
                            }

                            string strContent = string.Empty;
                            string strBalance = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(creditBefore.ToString(), 2);
                            string strMoney = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Money.ToString(), 2);
                            string strCreditAfter = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Business.Market.InvestorList[i].Credit.ToString(), 2);
                            strContent = "'" + Business.Market.InvestorList[i].Code + "': credit " + strBalance + " credit out " + strMoney + " -> " + strCreditAfter;
                            TradingServer.Facade.FacadeAddNewSystemLog(3, strContent, "[credit out account]", "", Business.Market.InvestorList[i].Code);

                            return true;
                        }
                        else
                        {
                            string strContent = string.Empty;
                            string strBalance = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(creditBefore.ToString(), 2);
                            string strMoney = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Money.ToString(), 2);
                            string strCreditAfter = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Business.Market.InvestorList[i].Credit.ToString(), 2);
                            strContent = "'" + Business.Market.InvestorList[i].Code + "': credit " + strBalance + " credit out " + strMoney + " -> " + strCreditAfter;
                            TradingServer.Facade.FacadeAddNewSystemLog(3, strContent, "[credit out account]", "", Business.Market.InvestorList[i].Code);

                            return false;
                        }
                    }
                }
            }

            return Result;
        }
        /// <summary>
        /// PROCESS NOTIFY FROM MT4(CASE CONNECT MT4)
        /// </summary>
        internal static void ProcessNotifyMessage()
        {
            string result = string.Empty;
            while (Business.Market.IsProcessNotifyMessage)
            {
                result = Business.Market.GetNotifyMessage();

                while (!string.IsNullOrEmpty(result))
                {
                    //Process Notify Message
                    string[] subCommand = result.Split('¬');

                    if (subCommand.Length > 0)
                    {
                        int count = subCommand.Length;
                        for (int i = 0; i < count; i++)
                        {
                            string[] subValue = subCommand[i].Split('$');

                            if (subValue.Length == 2)
                            {
                                switch (subValue[0])
                                {
                                    #region NOTIFY MAKE COMMAND
                                    case "MakeCommandNotify":
                                        {
                                            try
                                            {
                                                string[] subParameter = subValue[1].Split('{');
                                                TradingServer.Facade.FacadeAddNewSystemLog(6, result, "[MakeCommandNotify]", "", subParameter[4]);

                                                string commandType = string.Empty;
                                                Business.OpenTrade resultOpenTrade = Business.Market.MapNotifyMakeCommand(subCommand[i]);

                                                if (resultOpenTrade.Investor == null || resultOpenTrade.Symbol == null || resultOpenTrade.Type == null)
                                                    continue;

                                                #region comment code
                                                if (Business.Market.NJ4XTickets != null)
                                                {
                                                    lock (Business.Market.nj4xObject)
                                                    {
                                                        bool isExitst = false;
                                                        int countNJ4XTicket = Business.Market.NJ4XTickets.Count;
                                                        for (int n = 0; n < countNJ4XTicket; n++)
                                                        {
                                                            if (Business.Market.NJ4XTickets[n].Code == resultOpenTrade.Investor.Code)
                                                            {
                                                                lock (Business.Market.nj4xObject)
                                                                    Business.Market.NJ4XTickets.RemoveAt(n);

                                                                #region get mql commands (log ipaddress)
                                                                if (Business.Market.marketInstance.MQLCommands != null)
                                                                {
                                                                    int countMQL = Business.Market.marketInstance.MQLCommands.Count;
                                                                    for (int j = 0; j < countMQL; j++)
                                                                    {
                                                                        if (Business.Market.marketInstance.MQLCommands[j] != null)
                                                                        {
                                                                            if (Business.Market.marketInstance.MQLCommands[j].InvestorCode == resultOpenTrade.Investor.Code)
                                                                            {
                                                                                resultOpenTrade.IpAddress = Business.Market.marketInstance.MQLCommands[j].IpAddress;
                                                                                Business.Market.marketInstance.MQLCommands.Remove(Business.Market.marketInstance.MQLCommands[j]);
                                                                                break;
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                                #endregion

                                                                #region process add new command
                                                                if (resultOpenTrade.RefCommandID > 0)
                                                                {
                                                                    bool IsBuy = Model.Helper.Instance.IsBuy(resultOpenTrade.Type.ID);
                                                                    commandType = Model.Helper.Instance.convertCommandTypeIDToString(resultOpenTrade.Type.ID);

                                                                    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

                                                                isExitst = true;
                                                                break;
                                                            }
                                                        }

                                                        if (!isExitst)
                                                        {
                                                            bool isExitsCommand = false;
                                                            if (resultOpenTrade.Investor.CommandList != null)
                                                            {

                                                                int countCommand = resultOpenTrade.Investor.CommandList.Count;
                                                                for (int n = 0; n < countCommand; n++)
                                                                {
                                                                    if (resultOpenTrade.Investor.CommandList[n].CommandCode == resultOpenTrade.CommandCode)
                                                                    {
                                                                        isExitsCommand = true;
                                                                        break;
                                                                    }
                                                                }
                                                            }

                                                            #region add command when make command from mt4
                                                            if (!isExitsCommand)
                                                            {
                                                                #region get mql commands (log ipaddress)
                                                                if (Business.Market.marketInstance.MQLCommands != null)
                                                                {
                                                                    int countMQL = Business.Market.marketInstance.MQLCommands.Count;
                                                                    for (int j = 0; j < countMQL; j++)
                                                                    {
                                                                        if (Business.Market.marketInstance.MQLCommands[j] != null)
                                                                        {
                                                                            if (Business.Market.marketInstance.MQLCommands[j].InvestorCode == resultOpenTrade.Investor.Code)
                                                                            {
                                                                                resultOpenTrade.IpAddress = Business.Market.marketInstance.MQLCommands[j].IpAddress;
                                                                                Business.Market.marketInstance.MQLCommands.Remove(Business.Market.marketInstance.MQLCommands[j]);
                                                                                break;
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                                #endregion

                                                                #region process add new command
                                                                if (resultOpenTrade.RefCommandID > 0)
                                                                {
                                                                    bool IsBuy = Model.Helper.Instance.IsBuy(resultOpenTrade.Type.ID);
                                                                    commandType = Model.Helper.Instance.convertCommandTypeIDToString(resultOpenTrade.Type.ID);

                                                                    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
                                                            }
                                                            #endregion
                                                        }
                                                    }
                                                }
                                                #endregion
                                            }
                                            catch (Exception ex)
                                            {
                                                string _temp = ex.Message + " - " + subValue[1];
                                                TradingServer.Facade.FacadeAddNewSystemLog(6, _temp, "[Exception]", "", subValue[0]);
                                            }
                                        }
                                        break;
                                    #endregion

                                    #region NOTIFY CLOSE COMMAND
                                    case "NotifyCloseCommand":
                                        {
                                            try
                                            {
                                                string[] subParameter = subValue[1].Split('{');

                                                TradingServer.Facade.FacadeAddNewSystemLog(6, result, "[NotifyCloseCommand]", "", subParameter[4]);

                                                Business.OpenTrade resultNotify = Business.Market.MapNotifyCloseCommand(subCommand[i]);

                                                Business.OpenTrade Command = TradingServer.Facade.FacadeFindOpenTradeInSymbolListByRefID(resultNotify.RefCommandID);

                                                if (Command.Investor == null || Command.Symbol == null || Command.Type == null)
                                                {
                                                    continue;
                                                }

                                                #region comment code
                                                if (Business.Market.NJ4XTickets != null)
                                                {
                                                    lock (Business.Market.nj4xObject)
                                                    {
                                                        bool isExits = false;
                                                        int countNJ4X = Business.Market.NJ4XTickets.Count;
                                                        for (int m = 0; m < countNJ4X; m++)
                                                        {
                                                            if (Business.Market.NJ4XTickets[m].Code == Command.CommandCode)
                                                            {
                                                                lock (Business.Market.nj4xObject)
                                                                    Business.Market.NJ4XTickets.RemoveAt(m);

                                                                if (Command.ID > 0)
                                                                {
                                                                    Command.ClosePrice = resultNotify.ClosePrice;
                                                                    Command.Size = resultNotify.Size;
                                                                    Command.Profit = resultNotify.Profit;
                                                                    Command.Swap = resultNotify.Swap;
                                                                    Command.Commission = resultNotify.Commission;
                                                                    Command.CloseTime = resultNotify.CloseTime;
                                                                    Command.IsClose = true;

                                                                    //Command.Investor.UpdateCommand(Command);

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

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

                                                                            //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]);
                                                                            }

                                                                            #region COMMAND CLOSE
                                                                            int commandRefID = commands[i].ID;
                                                                            bool isPending = TradingServer.Model.TradingCalculate.Instance.CheckIsPendingPosition(Command.Type.ID);
                                                                            #endregion
                                                                            break;
                                                                        }
                                                                    }
                                                                    #endregion
                                                                }
                                                                else
                                                                {
                                                                    //insert system log
                                                                    TradingServer.Facade.FacadeAddNewSystemLog(6, result, "[NotifyCloseCommandFalse]", "", "");
                                                                }

                                                                isExits = true;

                                                                break;
                                                            }
                                                        }

                                                        if (!isExits)
                                                        {
                                                            bool isExitsCommand = false;
                                                            if (Command.Investor.CommandList != null)
                                                            {
                                                                int countCommand = Command.Investor.CommandList.Count;
                                                                for (int n = 0; n < countCommand; n++)
                                                                {
                                                                    if (Command.Investor.CommandList[n].CommandCode == Command.CommandCode)
                                                                    {
                                                                        isExitsCommand = true;

                                                                        break;
                                                                    }
                                                                }
                                                            }

                                                            #region remove command when close
                                                            if (isExitsCommand)
                                                            {
                                                                if (Command.ID > 0)
                                                                {
                                                                    Command.ClosePrice = resultNotify.ClosePrice;
                                                                    Command.Size = resultNotify.Size;
                                                                    Command.Profit = resultNotify.Profit;
                                                                    Command.Swap = resultNotify.Swap;
                                                                    Command.Commission = resultNotify.Commission;
                                                                    Command.CloseTime = resultNotify.CloseTime;
                                                                    Command.IsClose = true;

                                                                    //Command.Investor.UpdateCommand(Command);

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

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

                                                                            //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]);
                                                                            }

                                                                            #region COMMAND CLOSE
                                                                            int commandRefID = commands[i].ID;
                                                                            bool isPending = TradingServer.Model.TradingCalculate.Instance.CheckIsPendingPosition(Command.Type.ID);
                                                                            #endregion

                                                                            break;
                                                                        }
                                                                    }
                                                                    #endregion
                                                                }
                                                                else
                                                                {
                                                                    //insert system log
                                                                    TradingServer.Facade.FacadeAddNewSystemLog(6, result, "[NotifyCloseCommandFalse]", "", "");
                                                                }
                                                            }
                                                            #endregion
                                                        }
                                                    }
                                                }
                                                #endregion
                                            }
                                            catch (Exception ex)
                                            {
                                                string _temp = ex.Message + " - " + subValue[1];
                                                TradingServer.Facade.FacadeAddNewSystemLog(6, _temp, "[Exception]", "", subValue[0]);
                                            }
                                        }
                                        break;
                                    #endregion

                                    #region NOTIFY UPDATE ACCOUNT
                                    case "NotifyUpdateAccount":
                                        {
                                            try
                                            {
                                                Model.TradingCalculate.Instance.StreamManagerNotify(result + " (" + DateTime.Now + ")");

                                                Business.Investor resultNotify = Business.Market.MapNotifyUpdateAccount(subCommand[i]);

                                                if (resultNotify.InvestorID <= 0)
                                                    continue;

                                                //TradingServer.Model.TradingCalculate.Instance.StreamFile("[Receive Notify] - " + subCommand[i]);

                                                string message = "IAC04332451";
                                                resultNotify.ClientCommandQueue.Add(message);
                                            }
                                            catch (Exception ex)
                                            {
                                                string _temp = ex.Message + " - " + subValue[1];
                                                TradingServer.Facade.FacadeAddNewSystemLog(6, _temp, "[Exception]", "", subValue[0]);
                                            }
                                        }
                                        break;
                                    #endregion

                                    #region NOTIFY UPDATE INFO ACCOUNT
                                    case "NotifyInfoAccount":
                                        {
                                            try
                                            {
                                                Model.TradingCalculate.Instance.StreamManagerNotify(result + " (" + DateTime.Now + ")");

                                                Business.Investor resultNotify = Business.Market.MapNotifyUpdateInfoAccount(subCommand[i]);

                                                if (resultNotify.InvestorID <= 0)
                                                    continue;

                                                //TradingServer.Model.TradingCalculate.Instance.StreamFile("[Receive Notify] - " + subCommand[i]);

                                                string message = "IAC04332451";

                                                resultNotify.ClientCommandQueue.Add(message);
                                            }
                                            catch (Exception ex)
                                            {
                                                string _temp = ex.Message + " - " + subValue[1];
                                                TradingServer.Facade.FacadeAddNewSystemLog(6, _temp, "[Exception]", "", subValue[0]);
                                            }
                                        }
                                        break;
                                    #endregion

                                    #region NOTIFY DELETE COMMAND
                                    case "NotifyDeleteCommand":
                                        {
                                            try
                                            {
                                                Model.TradingCalculate.Instance.StreamManagerNotify(result + " (" + DateTime.Now + ")");

                                                int resultNotify = Business.Market.MapNotifyDeleteCommand(subCommand[i]);
                                                if (resultNotify > 0)
                                                {
                                                    Business.OpenTrade newOpenTrade = TradingServer.Facade.FacadeFindOpenTradeInSymbolListByRefID(resultNotify);

                                                    if (newOpenTrade.Investor == null || newOpenTrade.Symbol == null || newOpenTrade.Type == null)
                                                        continue;

                                                    //TradingServer.Model.TradingCalculate.Instance.StreamFile("[Receive Notify] - " + subCommand[i]);

                                                    if (newOpenTrade.RefCommandID > 0)
                                                    {
                                                        List<Business.OpenTrade> commands = newOpenTrade.Investor.CommandList;
                                                        bool IsBuy = Model.Helper.Instance.IsBuy(newOpenTrade.Type.ID);

                                                        for (int j = 0; j < commands.Count; j++)
                                                        {
                                                            if (commands[j].ID == newOpenTrade.ID)
                                                            {
                                                                #region CLOSE PENDING ORDER
                                                                //ADD PENDING ORDER TO DATABASE
                                                                int addHistory = TradingServer.Facade.FacadeAddNewCommandHistory(newOpenTrade.Investor.InvestorID, newOpenTrade.Type.ID, newOpenTrade.CommandCode,
                                                                    newOpenTrade.OpenTime, newOpenTrade.OpenPrice, newOpenTrade.CloseTime, newOpenTrade.ClosePrice, 0, 0, 0, newOpenTrade.ExpTime, newOpenTrade.Size, newOpenTrade.StopLoss,
                                                                    newOpenTrade.TakeProfit, newOpenTrade.ClientCode, newOpenTrade.Symbol.SymbolID, newOpenTrade.Taxes, newOpenTrade.AgentCommission, newOpenTrade.Comment, "3",
                                                                    newOpenTrade.TotalSwap, newOpenTrade.RefCommandID, newOpenTrade.AgentRefConfig, newOpenTrade.IsActivePending, newOpenTrade.IsStopLossAndTakeProfit);

                                                                //NEW SOLUTION ADD COMMAND TO REMOVE LIST
                                                                Business.OpenRemove newOpenRemove = new OpenRemove();
                                                                newOpenRemove.InvestorID = commands[j].Investor.InvestorID;
                                                                newOpenRemove.OpenTradeID = commands[j].ID;
                                                                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 (newOpenTrade.Investor.ClientCommandQueue == null)
                                                                    newOpenTrade.Investor.ClientCommandQueue = new List<string>();
                                                                #endregion

                                                                #region MAP STRING SEND TO CLIENT
                                                                string Message = string.Empty;
                                                                if (newOpenTrade.IsServer)
                                                                {
                                                                    Message = "CloseCommandByManager$True,Close Command Complete," + newOpenTrade.ID + "," + newOpenTrade.Investor.InvestorID + "," + newOpenTrade.Symbol.Name + "," +
                                                                        newOpenTrade.Size + "," + IsBuy + "," + newOpenTrade.OpenTime + "," + newOpenTrade.OpenPrice + "," + newOpenTrade.StopLoss + "," + newOpenTrade.TakeProfit + "," +
                                                                        newOpenTrade.ClosePrice + "," + newOpenTrade.Commission + "," + newOpenTrade.Swap + "," + newOpenTrade.Profit + "," + "Comment," + newOpenTrade.ID + "," + newOpenTrade.Type.Name + "," +
                                                                        1 + "," + newOpenTrade.ExpTime + "," + newOpenTrade.ClientCode + "," + newOpenTrade.CommandCode + "," + newOpenTrade.IsHedged + "," + newOpenTrade.Type.ID + "," + newOpenTrade.Margin +
                                                                        ",Close," + newOpenTrade.CloseTime;
                                                                }
                                                                else
                                                                {
                                                                    Message = "CloseCommand$True,Close Command Complete," + newOpenTrade.ID + "," + newOpenTrade.Investor.InvestorID + "," + newOpenTrade.Symbol.Name + "," +
                                                                        newOpenTrade.Size + "," + IsBuy + "," + newOpenTrade.OpenTime + "," + newOpenTrade.OpenPrice + "," + newOpenTrade.StopLoss + "," + newOpenTrade.TakeProfit + "," +
                                                                        newOpenTrade.ClosePrice + "," + newOpenTrade.Commission + "," + newOpenTrade.Swap + "," + newOpenTrade.Profit + "," + "Comment," + newOpenTrade.ID + "," + newOpenTrade.Type.Name + "," +
                                                                        1 + "," + newOpenTrade.ExpTime + "," + newOpenTrade.ClientCode + "," + newOpenTrade.CommandCode + "," + newOpenTrade.IsHedged + "," + newOpenTrade.Type.ID + "," + newOpenTrade.Margin +
                                                                        ",Close," + newOpenTrade.CloseTime;
                                                                }

                                                                newOpenTrade.Investor.ClientCommandQueue.Add(Message);

                                                                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 == newOpenTrade.Investor.Code)
                                                                        {
                                                                            newOpenTrade.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[i].Type.ID == 1)
                                                                    mode = "buy";

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

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

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

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

                                                                break;
                                                            }
                                                        }

                                                        //bool isPending = TradingServer.Model.TradingCalculate.Instance.CheckIsPendingPosition(newOpenTrade.Type.ID);

                                                        //if (isPending)
                                                        //{
                                                        //    newOpenTrade.IsClose = true;

                                                        //    newOpenTrade.Investor.UpdateCommand(newOpenTrade);
                                                        //}
                                                        //else
                                                        //{
                                                        //    TradingServer.Facade.FacadeDeleteOpenTradeByManagerWithRefID(newOpenTrade.RefCommandID);
                                                        //}
                                                    }
                                                }
                                            }
                                            catch (Exception ex)
                                            {
                                                string _temp = ex.Message + " - " + subValue[1];
                                                TradingServer.Facade.FacadeAddNewSystemLog(6, _temp, "[Exception]", "", subValue[0]);
                                            }
                                        }
                                        break;
                                    #endregion

                                    #region NOTIFY UPDATE COMMAND
                                    case "NotifyUpdateCommand":
                                        {
                                            try
                                            {
                                                Model.TradingCalculate.Instance.StreamManagerNotify(result + " (" + DateTime.Now + ")");

                                                Business.OpenTrade resultNotify = Business.Market.MapNotifyUpdateCommand(subCommand[i]);
                                                resultNotify.ID = resultNotify.RefCommandID;
                                                if (resultNotify.Investor == null || resultNotify.Symbol == null || resultNotify.Type == null)
                                                    continue;

                                                double profit = 0;

                                                #region UPDATE ONLINE COMMAND IN INVESTOR LIST
                                                if (resultNotify.Investor.CommandList != null)
                                                {
                                                    int countCommand = resultNotify.Investor.CommandList.Count;
                                                    for (int k = 0; k < countCommand; k++)
                                                    {
                                                        if (resultNotify.Investor.CommandList[k].ID == resultNotify.RefCommandID)
                                                        {
                                                            Business.OpenTrade temp = resultNotify.Investor.CommandList[k];

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

                                                            bool isManager = false;
                                                            if (temp.OpenPrice != resultNotify.OpenPrice || temp.Size != resultNotify.Size)
                                                                isManager = true;

                                                            //SET NEW VALUE FOR ONLINE COMMAND CURRENT
                                                            temp.Commission = resultNotify.Commission;
                                                            temp.ExpTime = resultNotify.ExpTime;
                                                            temp.OpenPrice = resultNotify.OpenPrice;
                                                            temp.OpenTime = resultNotify.OpenTime;
                                                            temp.StopLoss = resultNotify.StopLoss;
                                                            temp.Swap = resultNotify.Swap;
                                                            temp.TakeProfit = resultNotify.TakeProfit;
                                                            temp.Comment = resultNotify.Comment;
                                                            temp.Size = resultNotify.Size;
                                                            temp.Profit = profit;

                                                            //Update Command In Command Execute
                                                            temp.InsExe.Commission = resultNotify.Commission;
                                                            temp.InsExe.ExpTime = resultNotify.ExpTime;
                                                            temp.InsExe.OpenPrice = resultNotify.OpenPrice;
                                                            temp.InsExe.OpenTime = resultNotify.OpenTime;
                                                            temp.InsExe.StopLoss = resultNotify.StopLoss;
                                                            temp.InsExe.Swap = resultNotify.Swap;
                                                            temp.InsExe.TakeProfit = resultNotify.TakeProfit;
                                                            temp.InsExe.Comment = resultNotify.Comment;
                                                            temp.InsExe.Size = resultNotify.Size;
                                                            temp.InsExe.Profit = profit;

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

                                                            #region MAP COMMAND TO CLIENT
                                                            #region BUILD COMMAND SEND TO CLIENT
                                                            StringBuilder Message = new StringBuilder();
                                                            Message.Append("UpdateCommand$True,UPDATE COMMAND BY MANAGER COMPLETE,");
                                                            Message.Append(temp.ID);
                                                            Message.Append(",");
                                                            Message.Append(temp.Investor.InvestorID);
                                                            Message.Append(",");
                                                            Message.Append(temp.Symbol.Name);
                                                            Message.Append(",");
                                                            Message.Append(temp.Size);
                                                            Message.Append(",");
                                                            Message.Append(IsBuy);
                                                            Message.Append(",");
                                                            Message.Append(temp.OpenTime);
                                                            Message.Append(",");
                                                            Message.Append(temp.OpenPrice);
                                                            Message.Append(",");
                                                            Message.Append(temp.StopLoss);
                                                            Message.Append(",");
                                                            Message.Append(temp.TakeProfit);
                                                            Message.Append(",");
                                                            Message.Append(temp.ClosePrice);
                                                            Message.Append(",");
                                                            Message.Append(temp.Commission);
                                                            Message.Append(",");
                                                            Message.Append(temp.Swap);
                                                            Message.Append(",");
                                                            Message.Append(temp.Profit);
                                                            Message.Append(",");
                                                            Message.Append(temp.Comment);
                                                            Message.Append(",");
                                                            Message.Append(temp.ID);
                                                            Message.Append(",");
                                                            Message.Append(temp.Type.Name);
                                                            Message.Append(",");
                                                            Message.Append(1);
                                                            Message.Append(",");
                                                            Message.Append(temp.ExpTime);
                                                            Message.Append(",");
                                                            Message.Append(temp.ClientCode);
                                                            Message.Append(",");
                                                            Message.Append(temp.CommandCode);
                                                            Message.Append(",");
                                                            Message.Append(temp.IsHedged);
                                                            Message.Append(",");
                                                            Message.Append(temp.Type.ID);
                                                            Message.Append(",");
                                                            Message.Append(temp.Margin);
                                                            Message.Append(",Update");

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

                                                            temp.Investor.ClientCommandQueue.Add(Message.ToString());
                                                            #endregion

                                                            #region insert system log
                                                            string content = string.Empty;
                                                            string comment = "[modify order]";
                                                            string mode = TradingServer.Facade.FacadeGetTypeNameByTypeID(temp.Type.ID).ToLower();

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

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

                                                            TradingServer.Facade.FacadeAddNewSystemLog(5, Content, comment, temp.IpAddress, temp.Investor.Code);
                                                            #endregion

                                                            //if (!isManager)
                                                            //{

                                                            //}
                                                            //else
                                                            //{
                                                            //    #region BUILD COMMAND SEND TO CLIENT
                                                            //    StringBuilder Message = new StringBuilder();
                                                            //    Message.Append("UpdateCommandByManager$True,UPDATE COMMAND BY MANAGER COMPLETE,");
                                                            //    Message.Append(temp.ID);
                                                            //    Message.Append(",");
                                                            //    Message.Append(temp.Investor.InvestorID);
                                                            //    Message.Append(",");
                                                            //    Message.Append(temp.Symbol.Name);
                                                            //    Message.Append(",");
                                                            //    Message.Append(temp.Size);
                                                            //    Message.Append(",");
                                                            //    Message.Append(IsBuy);
                                                            //    Message.Append(",");
                                                            //    Message.Append(temp.OpenTime);
                                                            //    Message.Append(",");
                                                            //    Message.Append(temp.OpenPrice);
                                                            //    Message.Append(",");
                                                            //    Message.Append(temp.StopLoss);
                                                            //    Message.Append(",");
                                                            //    Message.Append(temp.TakeProfit);
                                                            //    Message.Append(",");
                                                            //    Message.Append(temp.ClosePrice);
                                                            //    Message.Append(",");
                                                            //    Message.Append(temp.Commission);
                                                            //    Message.Append(",");
                                                            //    Message.Append(temp.Swap);
                                                            //    Message.Append(",");
                                                            //    Message.Append(temp.Profit);
                                                            //    Message.Append(",");
                                                            //    Message.Append(temp.Comment);
                                                            //    Message.Append(",");
                                                            //    Message.Append(temp.ID);
                                                            //    Message.Append(",");
                                                            //    Message.Append(temp.Type.Name);
                                                            //    Message.Append(",");
                                                            //    Message.Append(1);
                                                            //    Message.Append(",");
                                                            //    Message.Append(temp.ExpTime);
                                                            //    Message.Append(",");
                                                            //    Message.Append(temp.ClientCode);
                                                            //    Message.Append(",");
                                                            //    Message.Append(temp.CommandCode);
                                                            //    Message.Append(",");
                                                            //    Message.Append(temp.IsHedged);
                                                            //    Message.Append(",");
                                                            //    Message.Append(temp.Type.ID);
                                                            //    Message.Append(",");
                                                            //    Message.Append(temp.Margin);
                                                            //    Message.Append(",Update");

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

                                                            //    temp.Investor.ClientCommandQueue.Add(Message.ToString());
                                                            //    #endregion
                                                            //}

                                                            #endregion

                                                            break;
                                                        }
                                                    }
                                                }
                                                #endregion

                                                #region UPDATE ONLINE COMMAND IN SYMBOL LIST
                                                if (resultNotify.Symbol.CommandList != null)
                                                {
                                                    int countCommand = resultNotify.Symbol.CommandList.Count;
                                                    for (int k = 0; k < countCommand; k++)
                                                    {
                                                        if (resultNotify.Symbol.CommandList[k].ID == resultNotify.RefCommandID)
                                                        {
                                                            if (resultNotify.Symbol.CommandList[k].Type.ID == resultNotify.Type.ID)
                                                            {
                                                                //SET NEW VALUE FOR ONLINE COMMAND CURRENT
                                                                resultNotify.Symbol.CommandList[k].Commission = resultNotify.Commission;
                                                                resultNotify.Symbol.CommandList[k].ExpTime = resultNotify.ExpTime;
                                                                resultNotify.Symbol.CommandList[k].OpenPrice = resultNotify.OpenPrice;
                                                                resultNotify.Symbol.CommandList[k].OpenTime = resultNotify.OpenTime;
                                                                resultNotify.Symbol.CommandList[k].StopLoss = resultNotify.StopLoss;
                                                                resultNotify.Symbol.CommandList[k].Swap = resultNotify.Swap;
                                                                resultNotify.Symbol.CommandList[k].TakeProfit = resultNotify.TakeProfit;
                                                                resultNotify.Symbol.CommandList[k].Comment = resultNotify.Comment;
                                                                resultNotify.Symbol.CommandList[k].Size = resultNotify.Size;
                                                                //Business.Market.SymbolList[i].CommandList[j].Profit = profit;
                                                            }
                                                            else
                                                            {
                                                                resultNotify.Symbol.CommandList[k].Type = resultNotify.Type;
                                                                resultNotify.Symbol.CommandList[k].IsClose = false;
                                                                resultNotify.Symbol.CommandList[k].Investor.UpdateCommand(resultNotify);
                                                            }

                                                            break;
                                                        }
                                                    }
                                                }
                                                #endregion
                                            }
                                            catch (Exception ex)
                                            {
                                                string _temp = ex.Message + " - " + subValue[1];
                                                TradingServer.Facade.FacadeAddNewSystemLog(6, _temp, "[Exception]", "", subValue[0]);
                                            }
                                        }
                                        break;
                                    #endregion

                                    #region NOTIFY CONNECT MT4
                                    case "NotifyConnectMT4":
                                        {
                                            try
                                            {
                                                Business.Market.StatusConnect = true;
                                            }
                                            catch (Exception ex)
                                            {
                                                string _temp = ex.Message + " - " + subValue[1];
                                                TradingServer.Facade.FacadeAddNewSystemLog(6, _temp, "[Exception]", "", subValue[0]);
                                            }
                                        }
                                        break;
                                    #endregion

                                    #region NOTIFY DISCONNECT MT4
                                    case "NotifyDisconnectMT4":
                                        {
                                            try
                                            {
                                                Model.TradingCalculate.Instance.StreamManagerNotify(result + " (" + DateTime.Now + ")");

                                                Business.Market.StatusConnect = false;

                                                if (Business.Market.InvestorOnline != null)
                                                {
                                                    int countInvestor = Business.Market.InvestorOnline.Count;
                                                    for (int j = 0; j < countInvestor; j++)
                                                    {
                                                        if (Business.Market.InvestorOnline[j].IsOnline)
                                                        {
                                                            string message = "OLOFF14790251";

                                                            Business.Market.InvestorOnline[j].ClientCommandQueue.Add(message);
                                                        }

                                                        Business.Market.InvestorOnline[j].IsFirstLogin = true;
                                                    }
                                                }
                                            }
                                            catch (Exception ex)
                                            {
                                                string _temp = ex.Message + " - " + subValue[1];
                                                TradingServer.Facade.FacadeAddNewSystemLog(6, _temp, "[Exception]", "", subValue[0]);
                                            }
                                        }
                                        break;
                                    #endregion

                                    #region NOTIFY INFO CLIENT SEND REQUEST
                                    //NotifyInfoClientSendRequest$status{idSession{timeRequest{managerCode{clientCode{group{priceBid{
                                    //priceAsk{tradeType{commandID{reserved{commandCode{symbol{volume{price{stoploss{takeprofit{ie_deviation{expiration
                                    //"NotifyInfoClientSendRequest$1{353{03/08/1996 22:01:00{0{91122838{test-vietnam{1.3669{1.3672{@{1{{0{EURUSD{100{1.3669{0{0{0{01/01/1970 02:00:00¬"
                                    case "NotifyInfoClientSendRequest":
                                        {
                                            string[] subParameter = subValue[1].Split('{');

                                            TradingServer.Facade.FacadeAddNewSystemLog(6, result, "[NotifyInfoClientSendRequest]", subParameter[8], subParameter[4]);

                                            if (subParameter[0] == "3")
                                            {
                                                try
                                                {
                                                    if (subParameter[8] == "@")
                                                        break;

                                                    double bid = double.Parse(subParameter[6]);
                                                    double ask = double.Parse(subParameter[7]);

                                                    bool isFlag = false;
                                                    if (bid > 0 && ask > 0)
                                                    {
                                                        if (Business.Market.NJ4XTickets != null)
                                                        {
                                                            //int countTicket = Business.Market.NJ4XTickets.Count;
                                                            for (int j = 0; j < Business.Market.NJ4XTickets.Count; j++)
                                                            {
                                                                if (Business.Market.NJ4XTickets[j].IsClose)
                                                                {
                                                                    NJ4XConnectSocket.NJ4XTicket temp;
                                                                    temp = Business.Market.NJ4XTickets[j];

                                                                    #region NOTIFY CLOSE
                                                                    if (temp.Code.Trim() == subParameter[4].Trim() && temp.IsDisable == false &&
                                                                        temp.IsReQuote == false)
                                                                    {
                                                                        if (temp.Execution == EnumMT4.Execution.REQUEST)
                                                                        {
                                                                            bool IsBuy = false;
                                                                            if (temp.Command.Type.ID == 1 || temp.Command.Type.ID == 7 || temp.Command.Type.ID == 9)
                                                                                IsBuy = true;

                                                                            #region CLOSE COMMAND WITH MODE REQUEST
                                                                            temp.Ask = double.Parse(subParameter[7]);
                                                                            temp.Bid = double.Parse(subParameter[6]);
                                                                            temp.IsRequest = true;
                                                                            temp.IsDisable = true;
                                                                            temp.IsReQuote = true;

                                                                            if (temp.Command.Type.ID == 1)
                                                                                temp.Command.ClosePrice = temp.Bid;
                                                                            else
                                                                                temp.Command.ClosePrice = temp.Ask;

                                                                            StringBuilder Message = new StringBuilder();

                                                                            Message.Append("CloseCommand$False,RD28,");
                                                                            Message.Append(temp.Command.ID);
                                                                            Message.Append(",");
                                                                            Message.Append(temp.Command.Investor.InvestorID);
                                                                            Message.Append(",");
                                                                            Message.Append(temp.Command.Symbol.Name);
                                                                            Message.Append(",");
                                                                            Message.Append(temp.Command.Size);
                                                                            Message.Append(",");
                                                                            Message.Append(IsBuy);
                                                                            Message.Append(",");
                                                                            Message.Append(temp.Command.OpenTime);
                                                                            Message.Append(",");
                                                                            Message.Append(temp.Command.OpenPrice);
                                                                            Message.Append(",");
                                                                            Message.Append(temp.Command.StopLoss);
                                                                            Message.Append(",");
                                                                            Message.Append(temp.Command.TakeProfit);
                                                                            Message.Append(",");
                                                                            Message.Append(temp.Command.ClosePrice);
                                                                            Message.Append(",");
                                                                            Message.Append(temp.Command.Commission);
                                                                            Message.Append(",");
                                                                            Message.Append(temp.Command.Swap);
                                                                            Message.Append(",");
                                                                            Message.Append(temp.Command.Profit);
                                                                            Message.Append(",");
                                                                            Message.Append("Comment,");
                                                                            Message.Append(temp.Command.ID);
                                                                            Message.Append(",");
                                                                            Message.Append(temp.Command.Type.Name);
                                                                            Message.Append(",");
                                                                            Message.Append(1);
                                                                            Message.Append(",");
                                                                            Message.Append(temp.Command.ExpTime);
                                                                            Message.Append(",");
                                                                            Message.Append(temp.Command.ClientCode);
                                                                            Message.Append(",");
                                                                            Message.Append(temp.Command.CommandCode);
                                                                            Message.Append(",");
                                                                            Message.Append(temp.Command.IsHedged);
                                                                            Message.Append(",");
                                                                            Message.Append(temp.Command.Type.ID);
                                                                            Message.Append(",");
                                                                            Message.Append(temp.Command.Margin);
                                                                            Message.Append(",Close,");
                                                                            Message.Append(temp.Command.CloseTime);
                                                                            Message.Append(",");
                                                                            Message.Append(temp.Bid);
                                                                            Message.Append(",");
                                                                            Message.Append(temp.Ask);

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

                                                                            temp.Command.Investor.ClientCommandQueue.Add(Message.ToString());

                                                                            //TradingServer.Model.TradingCalculate.Instance.StreamFileNJ4X("[Receive Notify] - " + Message);
                                                                            #endregion
                                                                        }
                                                                        else
                                                                        {
                                                                            if (temp.Ticket == int.Parse(subParameter[11]))
                                                                            {
                                                                                bool IsBuy = false;
                                                                                if (temp.Command.Type.ID == 1 || temp.Command.Type.ID == 7 || temp.Command.Type.ID == 9)
                                                                                    IsBuy = true;

                                                                                #region CLOSE COMMAND WITH MODE INSTANCE
                                                                                temp.Ask = double.Parse(subParameter[7]);
                                                                                temp.Bid = double.Parse(subParameter[6]);
                                                                                temp.Ticket = int.Parse(subParameter[9]);
                                                                                temp.IsDisable = true;
                                                                                temp.IsReQuote = true;

                                                                                if (temp.Command.Type.ID == 1)
                                                                                    temp.Command.ClosePrice = temp.Bid;
                                                                                else
                                                                                    temp.Command.ClosePrice = temp.Ask;

                                                                                StringBuilder Message = new StringBuilder();
                                                                                Message.Append("CloseCommand$False,RD02,");
                                                                                Message.Append(temp.Command.ID);
                                                                                Message.Append(",");
                                                                                Message.Append(temp.Command.Investor.InvestorID);
                                                                                Message.Append(",");
                                                                                Message.Append(temp.Command.Symbol.Name);
                                                                                Message.Append(",");
                                                                                Message.Append(temp.Command.Size);
                                                                                Message.Append(",");
                                                                                Message.Append(IsBuy);
                                                                                Message.Append(",");
                                                                                Message.Append(temp.Command.OpenTime);
                                                                                Message.Append(",");
                                                                                Message.Append(temp.Command.OpenPrice);
                                                                                Message.Append(",");
                                                                                Message.Append(temp.Command.StopLoss);
                                                                                Message.Append(",");
                                                                                Message.Append(temp.Command.TakeProfit);
                                                                                Message.Append(",");
                                                                                Message.Append(temp.Command.ClosePrice);
                                                                                Message.Append(",");
                                                                                Message.Append(temp.Command.Commission);
                                                                                Message.Append(",");
                                                                                Message.Append(temp.Command.Swap);
                                                                                Message.Append(",");
                                                                                Message.Append(temp.Command.Profit);
                                                                                Message.Append(",");
                                                                                Message.Append("Comment,");
                                                                                Message.Append(temp.Command.ID);
                                                                                Message.Append(",");
                                                                                Message.Append(temp.Command.Type.Name);
                                                                                Message.Append(",");
                                                                                Message.Append(1);
                                                                                Message.Append(",");
                                                                                Message.Append(temp.Command.ExpTime);
                                                                                Message.Append(",");
                                                                                Message.Append(temp.Command.ClientCode);
                                                                                Message.Append(",");
                                                                                Message.Append(temp.Command.CommandCode);
                                                                                Message.Append(",");
                                                                                Message.Append(temp.Command.IsHedged);
                                                                                Message.Append(",");
                                                                                Message.Append(temp.Command.Type.ID);
                                                                                Message.Append(",");
                                                                                Message.Append(temp.Command.Margin);
                                                                                Message.Append(",Close,");
                                                                                Message.Append(temp.Command.CloseTime);
                                                                                Message.Append(",");
                                                                                Message.Append(temp.Bid);
                                                                                Message.Append(",");
                                                                                Message.Append(temp.Ask);

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

                                                                                temp.Command.Investor.ClientCommandQueue.Add(Message.ToString());
                                                                                #endregion
                                                                            }

                                                                            isFlag = true;
                                                                            lock (Business.Market.nj4xObject)
                                                                                Business.Market.NJ4XTickets.RemoveAt(j);
                                                                        }
                                                                    }
                                                                    #endregion
                                                                }
                                                                else
                                                                {
                                                                    NJ4XConnectSocket.NJ4XTicket temp;
                                                                    temp = Business.Market.NJ4XTickets[j];

                                                                    #region NOTIFY OPEN
                                                                    if (temp.Code.Trim() == subParameter[4].Trim() &&
                                                                        temp.IsDisable == false &&
                                                                        temp.IsReQuote == false)
                                                                    {
                                                                        if (temp.Execution == EnumMT4.Execution.REQUEST)
                                                                        {
                                                                            #region OPEN WITH MODE REQUEST
                                                                            bool IsBuy = false;
                                                                            if (temp.Command.Type.ID == 1 ||
                                                                                temp.Command.Type.ID == 7 ||
                                                                                temp.Command.Type.ID == 9)
                                                                                IsBuy = true;

                                                                            string CommandType = string.Empty;
                                                                            if (temp.Command.Type.ID == 1 ||
                                                                                temp.Command.Type.ID == 2)
                                                                            {
                                                                                CommandType = "Open";
                                                                            }
                                                                            else
                                                                            {
                                                                                if (temp.Command.Type.ID == 7)
                                                                                {
                                                                                    CommandType = "BuyLimit";
                                                                                }

                                                                                if (temp.Command.Type.ID == 8)
                                                                                {
                                                                                    CommandType = "SellLimit";
                                                                                }

                                                                                if (temp.Command.Type.ID == 9)
                                                                                {
                                                                                    CommandType = "BuyStop";
                                                                                }

                                                                                if (temp.Command.Type.ID == 10)
                                                                                {
                                                                                    CommandType = "SellStop";
                                                                                }
                                                                            }

                                                                            temp.Ask = double.Parse(subParameter[7]);
                                                                            temp.Bid = double.Parse(subParameter[6]);
                                                                            temp.IsDisable = true;
                                                                            temp.IsRequest = true;
                                                                            temp.IsReQuote = true;

                                                                            if (temp.Command.Type.ID == 1)
                                                                                temp.Command.OpenPrice = temp.Ask;
                                                                            else
                                                                                temp.Command.OpenPrice = temp.Bid;

                                                                            StringBuilder Message = new StringBuilder();
                                                                            Message.Append("AddCommand$False,RD28,");
                                                                            Message.Append(temp.Command.ID);
                                                                            Message.Append(",");
                                                                            Message.Append(temp.Command.Investor.InvestorID);
                                                                            Message.Append(",");
                                                                            Message.Append(temp.Command.Symbol.Name);
                                                                            Message.Append(",");
                                                                            Message.Append(temp.Command.Size);
                                                                            Message.Append(",");
                                                                            Message.Append(IsBuy);
                                                                            Message.Append(",");
                                                                            Message.Append(temp.Command.OpenTime);
                                                                            Message.Append(",");
                                                                            Message.Append(temp.Command.OpenPrice);
                                                                            Message.Append(",");
                                                                            Message.Append(temp.Command.StopLoss);
                                                                            Message.Append(",");
                                                                            Message.Append(temp.Command.TakeProfit);
                                                                            Message.Append(",");
                                                                            Message.Append(temp.Command.ClosePrice);
                                                                            Message.Append(",");
                                                                            Message.Append(temp.Command.Commission);
                                                                            Message.Append(",");
                                                                            Message.Append(temp.Command.Swap);
                                                                            Message.Append(",");
                                                                            Message.Append(temp.Command.Profit);
                                                                            Message.Append(",");
                                                                            Message.Append("Comment,");
                                                                            Message.Append(temp.Command.ID);
                                                                            Message.Append(",");
                                                                            Message.Append(CommandType);
                                                                            Message.Append(",");
                                                                            Message.Append(1);
                                                                            Message.Append(",");
                                                                            Message.Append(temp.Command.ExpTime);
                                                                            Message.Append(",");
                                                                            Message.Append(temp.Command.ClientCode);
                                                                            Message.Append(",");
                                                                            Message.Append(temp.Command.CommandCode);
                                                                            Message.Append(",");
                                                                            Message.Append(temp.Command.IsHedged);
                                                                            Message.Append(",");
                                                                            Message.Append(temp.Command.Type.ID);
                                                                            Message.Append(",");
                                                                            Message.Append(temp.Command.Margin);
                                                                            Message.Append(",Open");
                                                                            Message.Append(",");
                                                                            Message.Append(temp.Command.CloseTime);
                                                                            Message.Append(",");
                                                                            Message.Append(temp.Bid);
                                                                            Message.Append(",");
                                                                            Message.Append(temp.Ask);

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

                                                                            temp.Command.Investor.ClientCommandQueue.Add(Message.ToString());

                                                                            //TradingServer.Model.TradingCalculate.Instance.StreamFileNJ4X("[Receive Notify] - " + Message);
                                                                            #endregion
                                                                        }
                                                                        else
                                                                        {
                                                                            #region OPEN WITH MODE INSTANCE
                                                                            if (temp.Symbol.Trim().ToUpper() == subParameter[12].Trim().ToUpper())
                                                                            {
                                                                                temp.Ask = double.Parse(subParameter[7]);
                                                                                temp.Bid = double.Parse(subParameter[6]);
                                                                                temp.Ticket = int.Parse(subParameter[9]);
                                                                                temp.IsDisable = true;
                                                                                temp.IsReQuote = true;

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

                                                                                if (temp.Command.Type.ID == 1)
                                                                                    temp.Command.OpenPrice = temp.Ask;
                                                                                else
                                                                                    temp.Command.OpenPrice = temp.Bid;

                                                                                StringBuilder Message = new StringBuilder();
                                                                                Message.Append("AddCommand$False,RD28,");
                                                                                Message.Append(temp.Command.ID);
                                                                                Message.Append(",");
                                                                                Message.Append(temp.Command.Investor.InvestorID);
                                                                                Message.Append(",");
                                                                                Message.Append(temp.Command.Symbol.Name);
                                                                                Message.Append(",");
                                                                                Message.Append(temp.Command.Size);
                                                                                Message.Append(",");
                                                                                Message.Append(IsBuy);
                                                                                Message.Append(",");
                                                                                Message.Append(temp.Command.OpenTime);
                                                                                Message.Append(",");
                                                                                Message.Append(temp.Command.OpenPrice);
                                                                                Message.Append(",");
                                                                                Message.Append(temp.Command.StopLoss);
                                                                                Message.Append(",");
                                                                                Message.Append(temp.Command.TakeProfit);
                                                                                Message.Append(",");
                                                                                Message.Append(temp.Command.ClosePrice);
                                                                                Message.Append(",");
                                                                                Message.Append(temp.Command.Commission);
                                                                                Message.Append(",");
                                                                                Message.Append(temp.Command.Swap);
                                                                                Message.Append(",");
                                                                                Message.Append(temp.Command.Profit);
                                                                                Message.Append(",");
                                                                                Message.Append("Comment,");
                                                                                Message.Append(temp.Command.ID);
                                                                                Message.Append(",");
                                                                                Message.Append(temp.Command.Type.Name);
                                                                                Message.Append(",");
                                                                                Message.Append(1);
                                                                                Message.Append(",");
                                                                                Message.Append(temp.Command.ExpTime);
                                                                                Message.Append(",");
                                                                                Message.Append(temp.Command.ClientCode);
                                                                                Message.Append(",");
                                                                                Message.Append(temp.Command.CommandCode);
                                                                                Message.Append(",");
                                                                                Message.Append(temp.Command.IsHedged);
                                                                                Message.Append(",");
                                                                                Message.Append(temp.Command.Type.ID);
                                                                                Message.Append(",");
                                                                                Message.Append(temp.Command.Margin);
                                                                                Message.Append(",Close,");
                                                                                Message.Append(temp.Command.CloseTime);
                                                                                Message.Append(",");
                                                                                Message.Append(temp.Bid);
                                                                                Message.Append(",");
                                                                                Message.Append(temp.Ask);

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

                                                                                temp.Command.Investor.ClientCommandQueue.Add(Message.ToString());

                                                                                //TradingServer.Model.TradingCalculate.Instance.StreamFileNJ4X("[Receive Notify] - " + Message);
                                                                            }
                                                                            #endregion

                                                                            isFlag = true;
                                                                            lock (Business.Market.nj4xObject)
                                                                                Business.Market.NJ4XTickets.RemoveAt(0);
                                                                        }
                                                                    }
                                                                    #endregion
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                                catch (Exception ex)
                                                {
                                                    string _temp = ex.Message + " - " + subValue[1];
                                                    TradingServer.Facade.FacadeAddNewSystemLog(6, _temp, "[Exception]", "", subValue[0]);
                                                }
                                            }
                                        }
                                        break;
                                    #endregion

                                    #region NOTIFY CHANGE GROUP
                                    case "NotifyChangeGroup":
                                        {
                                            #region COMMENT CODE BECAUSE NEED TEST
                                            try
                                            {
                                                Business.Market.IsOpen = false;

                                                if (Business.Market.InvestorList != null)
                                                {
                                                    int countInvestor = Business.Market.InvestorList.Count;
                                                    for (int j = 0; j < countInvestor; j++)
                                                    {
                                                        if (Business.Market.InvestorList[j].IsOnline)
                                                        {
                                                            string message = "OLOFF14790251";
                                                            //int countInvestorOnline = Business.Market.InvestorList[j].CountInvestorOnline(Business.Market.InvestorList[j].InvestorID);
                                                            //if (countInvestorOnline > 0)
                                                            Business.Market.InvestorList[j].ClientCommandQueue.Add(message);
                                                        }

                                                        Business.Market.InvestorList[j].IsFirstLogin = true;
                                                    }
                                                }

                                                int countSymbol = Business.Market.SymbolList.Count;
                                                for (int j = 0; j < countSymbol;j++ )
                                                {
                                                    Business.Market.SymbolList[j].CommandList.Clear();
                                                }

                                                Business.Market.CommandExecutor.Clear();

                                                Business.Market.marketInstance.MapGroup(subValue[1]);

                                                Business.Market.IsOpen = true;
                                            }
                                            catch (Exception ex)
                                            {
                                                string _temp = ex.Message + " - " + subValue[1];
                                                TradingServer.Facade.FacadeAddNewSystemLog(6, _temp, "[Exception]", "", subValue[0]);
                                            }
                                            #endregion
                                        }
                                        break;
                                    #endregion

                                    #region NOTIFY CHANGE SYMBOL
                                    case "NotifyChangeSymbol":
                                        {
                                            #region COMMENT CODE BECAUSE NEED TEST
                                            try
                                            {
                                                Business.Market.IsOpen = false;

                                                if (Business.Market.InvestorList != null)
                                                {
                                                    int countInvestor = Business.Market.InvestorList.Count;
                                                    for (int j = 0; j < countInvestor; j++)
                                                    {
                                                        if (Business.Market.InvestorList[j].IsOnline)
                                                        {
                                                            string message = "OLOFF14790251";
                                                            //int countInvestorOnline = Business.Market.InvestorList[j].CountInvestorOnline(Business.Market.InvestorList[j].InvestorID);
                                                            //if (countInvestorOnline > 0)
                                                            Business.Market.InvestorList[j].ClientCommandQueue.Add(message);
                                                        }

                                                        Business.Market.InvestorList[j].IsFirstLogin = true;
                                                    }
                                                }

                                                //List<string> symbols = Element5SocketConnectMT4.MT4Connect.MT4ConnectPort.Instance.InitSymbolMT4();
                                                //Business.Market.marketInstance.ReceiveSymbolNotify(symbols);

                                                int countSymbol = Business.Market.SymbolList.Count;
                                                for (int j = 0; j < countSymbol; j++)
                                                {
                                                    Business.Market.SymbolList[j].CommandList.Clear();
                                                }

                                                Business.Market.CommandExecutor.Clear();

                                                Business.Market.marketInstance.MapSymbol(subValue[1]);

                                                Business.Market.IsOpen = true;
                                            }
                                            catch (Exception ex)
                                            {
                                                string _temp = ex.Message + " - " + subValue[1];
                                                TradingServer.Facade.FacadeAddNewSystemLog(6, _temp, "[Exception]", "", subValue[0]);
                                            }
                                            #endregion
                                        }
                                        break;
                                    #endregion
                                }
                            }
                        }
                    }

                    result = Business.Market.GetNotifyMessage();
                }

                System.Threading.Thread.Sleep(1000);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="InvestorID"></param>
        /// <param name="Money"></param>
        /// <returns></returns>
        public bool WithDrawals(int InvestorID, double Money, string Comment)
        {
            bool Result = false;

            Money = Math.Round(Money, 2);

            if (Money <= 0)
                return false;

            if (Business.Market.InvestorList != null)
            {
                int count = Business.Market.InvestorList.Count;
                for (int i = 0; i < count; i++)
                {
                    if (Business.Market.InvestorList[i].InvestorID == InvestorID)
                    {
                        double Balance = 0;
                        double balanceBefore = Business.Market.InvestorList[i].Balance;
                        double tempBalance = Math.Round(Business.Market.InvestorList[i].Balance, 2) - Money;
                        Balance = (tempBalance + Business.Market.InvestorList[i].Credit);

                        if (tempBalance >= 0)
                        {
                            #region WITHDRAWALS MONEY > BALANCE
                            if (Business.Market.InvestorList[i].CommandList != null && Business.Market.InvestorList[i].CommandList.Count > 0)
                            {
                                #region RECALCULATION ACCOUNT
                                //ReCalculation Account If Account Valid Then WithRaw Complete Else Then WithRaw False
                                Business.Margin newMargin = new Business.Margin();
                                double Margin = 0;
                                newMargin = Business.Market.InvestorList[i].CommandList[0].Symbol.CalculationTotalMargin(Business.Market.InvestorList[i].CommandList);
                                Margin = newMargin.TotalMargin;

                                double Profit = Business.Market.InvestorList[i].InvestorGroupInstance.CalculationTotalProfit(Business.Market.InvestorList[i].CommandList);
                                double Equity = Balance + Profit;
                                double Loss = Business.Market.InvestorList[i].InvestorGroupInstance.CalculationTotalLoss(Business.Market.InvestorList[i].CommandList);
                                double ProfitPositive = Business.Market.InvestorList[i].InvestorGroupInstance.CalculationTotalProfitPositive(Business.Market.InvestorList[i].CommandList);
                                int Method = -1;
                                switch (Business.Market.InvestorList[i].InvestorGroupInstance.FreeMargin)
                                {
                                    case "do not use unrealized profit/loss":
                                        Method = 0;
                                        break;
                                    case "use unrealized profit/loss":
                                        Method = 1;
                                        break;
                                    case "use unrealized profit only":
                                        Method = 2;
                                        break;
                                    case "use unrealized loss only":
                                        Method = 3;
                                        break;
                                }

                                double totalMargin = Business.Market.InvestorList[i].Margin + Business.Market.InvestorList[i].FreezeMargin;

                                double FreeMargin = Business.Market.InvestorList[i].InvestorGroupInstance.CalculationTotalFreeMargin(totalMargin, Balance, Equity, ProfitPositive, Loss, Method);
                                double MarginLevel = (Equity * 100) / (Margin + Business.Market.InvestorList[i].FreezeMargin);
                                #endregion

                                if (FreeMargin > 0)
                                {
                                    #region Valid Account Will WithRawals Complete
                                    bool UpdateBalance = false;
                                    //Update Balance In Database
                                    UpdateBalance = TradingServer.Facade.FacadeUpdateBalance(InvestorID, tempBalance);

                                    if (UpdateBalance == true)
                                    {
                                        //Set Balance Of Investor
                                        Business.Market.InvestorList[i].Balance = tempBalance;

                                        if (this.InvestorGroupInstance != null)
                                        {
                                            //ReCalculation Account Of Investor
                                            Business.Market.InvestorList[i].ReCalculationAccount();
                                        }
                                        else
                                        {
                                            if (MarginLevel <= Business.Market.InvestorList[i].InvestorGroupInstance.MarginStopOut)
                                            {
                                                for (int j = 0; j < Business.Market.InvestorList[i].CommandList.Count; j++)
                                                {
                                                    #region Command Is Close
                                                    int ResultHistory = -1;
                                                    //set time close
                                                    Business.Market.InvestorList[i].CommandList[j].CloseTime = DateTime.Now;

                                                    //Add Command To Command History

                                                    ResultHistory = TradingServer.Facade.FacadeAddNewCommandHistory(Business.Market.InvestorList[i].CommandList[j].Investor.InvestorID,
                                                        Business.Market.InvestorList[i].CommandList[j].Type.ID,
                                                        Business.Market.InvestorList[i].CommandList[j].CommandCode,
                                                        Business.Market.InvestorList[i].CommandList[j].OpenTime,
                                                        Business.Market.InvestorList[i].CommandList[j].OpenPrice,
                                                        Business.Market.InvestorList[i].CommandList[j].CloseTime,
                                                        Business.Market.InvestorList[i].CommandList[j].ClosePrice,
                                                        Business.Market.InvestorList[i].CommandList[j].Profit,
                                                        Business.Market.InvestorList[i].CommandList[j].Swap,
                                                        Business.Market.InvestorList[i].CommandList[j].Commission,
                                                        Business.Market.InvestorList[i].CommandList[j].ExpTime,
                                                        Business.Market.InvestorList[i].CommandList[j].Size,
                                                        Business.Market.InvestorList[i].CommandList[j].StopLoss,
                                                        Business.Market.InvestorList[i].CommandList[j].TakeProfit,
                                                        Business.Market.InvestorList[i].CommandList[j].ClientCode,
                                                        Business.Market.InvestorList[i].CommandList[j].Symbol.SymbolID,
                                                        Business.Market.InvestorList[i].CommandList[j].Taxes,
                                                        Business.Market.InvestorList[i].CommandList[j].AgentCommission,
                                                        Business.Market.InvestorList[i].CommandList[j].Comment, "16",
                                                        Business.Market.InvestorList[i].CommandList[j].TotalSwap,
                                                        Business.Market.InvestorList[i].CommandList[j].RefCommandID,
                                                        Business.Market.InvestorList[i].CommandList[j].AgentRefConfig,
                                                        Business.Market.InvestorList[i].CommandList[j].IsActivePending,
                                                        Business.Market.InvestorList[i].CommandList[j].IsStopLossAndTakeProfit);

                                                    if (ResultHistory > 0)
                                                    {
                                                        //Log Stop Out
                                                        string content = string.Empty;
                                                        string mode = TradingServer.Facade.FacadeGetTypeNameByTypeID(Business.Market.InvestorList[i].CommandList[j].Type.ID).ToLower();
                                                        string size = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Business.Market.InvestorList[i].CommandList[j].Size.ToString(), 2);
                                                        string openPrice = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Business.Market.InvestorList[i].CommandList[j].OpenPrice.ToString(),
                                                            Business.Market.InvestorList[i].CommandList[j].Symbol.Digit);
                                                        content = "'" + Business.Market.InvestorList[i].CommandList[j].Investor.Code + "': stop out #" +
                                                            Business.Market.InvestorList[i].CommandList[j].CommandCode + " " + mode + " " + size + " " +
                                                            Business.Market.InvestorList[i].CommandList[j].Symbol.Name + " " + openPrice + " (" +
                                                            Business.Market.InvestorList[i].CommandList[j].Symbol.TickValue.Bid + "/" +
                                                            Business.Market.InvestorList[i].CommandList[j].Symbol.TickValue.Ask + ")";

                                                        TradingServer.Facade.FacadeAddNewSystemLog(5, content, "[stop out]", "", this.Code);

                                                        #region COMMENT CODE(REMOVE COMMAND IN COMMAND EXECUTOR AND SYMBOL LIST
                                                        ////Remove Command In Symbol List
                                                        //bool resultRemoveCommandSymbol = TradingServer.Facade.FacadeRemoveOpenTradeInCommandList(Business.Market.InvestorList[i].CommandList[j].ID);

                                                        ////Remove Command In Command Executor
                                                        //bool deleteCommandExe = TradingServer.Facade.FacadeRemoveOpenTradeInCommandExecutor(Business.Market.InvestorList[i].CommandList[j].ID);
                                                        #endregion

                                                        //NEW SOLUTION ADD COMMAND TO REMOVE LIST
                                                        //Business.OpenTrade newOpenTrade = this.CommandList[i];
                                                        Business.OpenRemove newOpenRemove = new OpenRemove();
                                                        newOpenRemove.InvestorID = this.InvestorID;
                                                        newOpenRemove.OpenTradeID = this.CommandList[i].ID;
                                                        newOpenRemove.SymbolName = this.CommandList[i].Symbol.Name;
                                                        newOpenRemove.IsExecutor = true;
                                                        newOpenRemove.IsSymbol = true;
                                                        newOpenRemove.IsInvestor = false;
                                                        Business.Market.AddCommandToRemoveList(newOpenRemove);

                                                        double totalProfit = Math.Round(Business.Market.InvestorList[i].CommandList[j].Profit + Business.Market.InvestorList[i].CommandList[j].Commission - Business.Market.InvestorList[i].CommandList[j].Swap, 2);
                                                        Business.Market.InvestorList[i].Balance += totalProfit;

                                                        //Update Balance Of Investor Account
                                                        this.UpdateBalance(Business.Market.InvestorList[i].CommandList[j].Investor.InvestorID, Business.Market.InvestorList[j].Balance);

                                                        //Update Command In Database
                                                        TradingServer.Facade.FacadeDeleteOpenTradeByID(Business.Market.InvestorList[i].CommandList[j].ID);

                                                        //Close Command Complete Add Message To Client
                                                        if (Business.Market.InvestorList[i].ClientCommandQueue == null)
                                                            Business.Market.InvestorList[i].ClientCommandQueue = new List<string>();

                                                        #region Map Command Server To Client
                                                        string Message = "StopOut$True,Close Command Complete," +
                                                            Business.Market.InvestorList[i].CommandList[j].ID + "," +
                                                            Business.Market.InvestorList[i].CommandList[j].Investor.InvestorID + "," +
                                                            Business.Market.InvestorList[i].CommandList[j].Symbol.Name + "," +
                                                            Business.Market.InvestorList[i].CommandList[j].Size + "," + false + "," +
                                                            Business.Market.InvestorList[i].CommandList[j].OpenTime + "," +
                                                            Business.Market.InvestorList[i].CommandList[j].OpenPrice + "," +
                                                            Business.Market.InvestorList[i].CommandList[j].StopLoss + "," +
                                                            Business.Market.InvestorList[i].CommandList[j].TakeProfit + "," +
                                                            Business.Market.InvestorList[i].CommandList[j].ClosePrice + "," +
                                                            Business.Market.InvestorList[i].CommandList[j].Commission + "," +
                                                            Business.Market.InvestorList[i].CommandList[j].Swap + "," +
                                                            Business.Market.InvestorList[i].CommandList[j].Profit + "," + "Comment," +
                                                            Business.Market.InvestorList[i].CommandList[j].ID + "," +
                                                            Business.Market.InvestorList[i].CommandList[j].Type.Name + "," + 1 + "," +
                                                            Business.Market.InvestorList[i].CommandList[j].ExpTime + "," +
                                                            Business.Market.InvestorList[i].CommandList[j].ClientCode + "," +
                                                            Business.Market.InvestorList[i].CommandList[j].CommandCode + "," +
                                                            Business.Market.InvestorList[i].CommandList[j].IsHedged + "," +
                                                            Business.Market.InvestorList[i].CommandList[j].Type.ID + "," +
                                                            Business.Market.InvestorList[i].CommandList[j].Margin + ",Close," +
                                                            Business.Market.InvestorList[i].CommandList[j].CloseTime;

                                                        if (Business.Market.InvestorList[i].ClientCommandQueue == null)
                                                            Business.Market.InvestorList[i].ClientCommandQueue = new List<string>();

                                                        Business.Market.InvestorList[i].ClientCommandQueue.Add(Message);
                                                        #endregion

                                                        //SEND NOTIFY TO MANAGER
                                                        TradingServer.Facade.FacadeSendNoticeManagerRequest(2, Business.Market.InvestorList[i].CommandList[j]);

                                                        lock (Business.Market.syncObject)
                                                        {
                                                            //Remove Command In Investor List
                                                            Business.Market.InvestorList[i].CommandList.Remove(Business.Market.InvestorList[i].CommandList[j]);
                                                        }

                                                        if (Business.Market.InvestorList[i].CommandList.Count > 0 && Business.Market.InvestorList[i].CommandList != null)
                                                        {
                                                            //Call function Total Margin Of Investor
                                                            Business.Margin totalMarginAccount = new Business.Margin();
                                                            totalMarginAccount = Business.Market.InvestorList[i].CommandList[0].Symbol.CalculationTotalMargin(Business.Market.InvestorList[i].CommandList);
                                                            Business.Market.InvestorList[i].Margin = totalMarginAccount.TotalMargin;
                                                            Business.Market.InvestorList[i].FreezeMargin = totalMarginAccount.TotalFreezeMargin;

                                                            Business.Market.InvestorList[i].Profit = Business.Market.InvestorList[i].InvestorGroupInstance.CalculationTotalProfit(Business.Market.InvestorList[i].CommandList);
                                                            Business.Market.InvestorList[i].Equity = Business.Market.InvestorList[i].Balance +
                                                                Business.Market.InvestorList[i].Credit + Business.Market.InvestorList[i].Profit;
                                                        }
                                                        else
                                                        {
                                                            Business.Market.InvestorList[i].Margin = 0;
                                                            Business.Market.InvestorList[i].Profit = 0;
                                                            Business.Market.InvestorList[i].Equity = 0;
                                                        }

                                                        TradingServer.Facade.FacadeSendNotifyManagerRequest(3, Business.Market.InvestorList[i]);
                                                    }
                                                    else
                                                    {
                                                        #region Map Command Server To Client
                                                        string Message = "CloseCommand$False,Can't Add Command To Database," + Business.Market.InvestorList[i].CommandList[j].ID + "," +
                                                            Business.Market.InvestorList[i].CommandList[j].Investor.InvestorID + "," +
                                                            Business.Market.InvestorList[i].CommandList[j].Symbol.Name + "," +
                                                            Business.Market.InvestorList[i].CommandList[j].Size + "," + false + "," +
                                                            Business.Market.InvestorList[i].CommandList[j].OpenTime + "," +
                                                            Business.Market.InvestorList[i].CommandList[j].OpenPrice + "," +
                                                            Business.Market.InvestorList[i].CommandList[j].StopLoss + "," +
                                                            Business.Market.InvestorList[i].CommandList[j].TakeProfit + "," +
                                                            Business.Market.InvestorList[i].CommandList[j].ClosePrice + "," +
                                                            Business.Market.InvestorList[i].CommandList[j].Commission + "," +
                                                            Business.Market.InvestorList[i].CommandList[j].Swap + "," +
                                                            Business.Market.InvestorList[i].CommandList[j].Profit + "," + "Comment," +
                                                            Business.Market.InvestorList[i].CommandList[j].ID + "," +
                                                            Business.Market.InvestorList[i].CommandList[j].Type.Name + "," + 1 + "," +
                                                            Business.Market.InvestorList[i].CommandList[j].ExpTime + "," +
                                                            Business.Market.InvestorList[i].CommandList[j].ClientCode + "," +
                                                            Business.Market.InvestorList[i].CommandList[j].CommandCode + "," +
                                                            Business.Market.InvestorList[i].CommandList[j].IsHedged + "," +
                                                            Business.Market.InvestorList[i].CommandList[j].Type.ID + "," +
                                                            Business.Market.InvestorList[i].CommandList[j].Margin + ",Close," +
                                                            Business.Market.InvestorList[i].CommandList[j].CloseTime;

                                                        if (Business.Market.InvestorList[i].ClientCommandQueue == null)
                                                            Business.Market.InvestorList[i].ClientCommandQueue = new List<string>();

                                                        Business.Market.InvestorList[i].ClientCommandQueue.Add(Message);
                                                        #endregion
                                                    }
                                                    #endregion

                                                    j--;
                                                }
                                            }
                                        }

                                        //Send Command To Client
                                        if (Business.Market.InvestorList[i].ClientCommandQueue == null)
                                            Business.Market.InvestorList[i].ClientCommandQueue = new List<string>();

                                        string SmS = "WithDrawals$True,WithDrawals Complete";

                                        if (Business.Market.InvestorList[i].ClientCommandQueue == null)
                                            Business.Market.InvestorList[i].ClientCommandQueue = new List<string>();

                                        //int countOnline = Business.Market.InvestorList[i].CountInvestorOnline(Business.Market.InvestorList[i].InvestorID);
                                        //if (countOnline > 0)
                                        Business.Market.InvestorList[i].ClientCommandQueue.Add(SmS);

                                        //SEND COMMAND TO AGENT SERVER
                                        string msg = SmS + "," + Money + "," + InvestorID;
                                        Business.AgentNotify newAgentNotify = new AgentNotify();
                                        newAgentNotify.NotifyMessage = msg;
                                        TradingServer.Agent.AgentConfig.Instance.AddNotifyToAgent(newAgentNotify);

                                        Business.InvestorAccountLog newInvestorAccountLog = new InvestorAccountLog();
                                        newInvestorAccountLog.Name = Business.Market.InvestorList[i].NickName;
                                        newInvestorAccountLog.InvestorID = Business.Market.InvestorList[i].InvestorID;
                                        newInvestorAccountLog.Date = DateTime.Now;
                                        newInvestorAccountLog.Comment = Comment;
                                        newInvestorAccountLog.Amount = Money;
                                        newInvestorAccountLog.Code = "WRD01";

                                        //int ResultLog = TradingServer.Facade.FacadeAddInvestorAccountLog(newInvestorAccountLog);

                                        //string CommandCode = TradingServer.Model.TradingCalculate.Instance.BuildCommandCode(ResultLog.ToString());

                                        //TradingServer.Facade.FacadeUpdateDealID(ResultLog, CommandCode);

                                        Result = true;

                                        //SEND NOTIFY TO MANAGER THEN WITHDRAWALS
                                        TradingServer.Facade.FacadeSendNotifyManagerRequest(3, Business.Market.InvestorList[i]);
                                    }
                                    else
                                    {
                                        Result = false;
                                    }
                                    #endregion
                                }
                                else
                                {
                                    Result = false;
                                }
                            }
                            else
                            {
                                #region Valid Account Will WithRawals Complete
                                bool UpdateBalance = false;
                                //Update Balance In Database
                                UpdateBalance = TradingServer.Facade.FacadeUpdateBalance(InvestorID, tempBalance);

                                if (UpdateBalance == true)
                                {
                                    //Set Balance Of Investor
                                    Business.Market.InvestorList[i].Balance = tempBalance;

                                    //ReCalculation Account Of Investor
                                    Business.Market.InvestorList[i].ReCalculationAccount();

                                    //Send Command To Client
                                    if (Business.Market.InvestorList[i].ClientCommandQueue == null)
                                        Business.Market.InvestorList[i].ClientCommandQueue = new List<string>();

                                    string Message = "WithDrawals$True,WithDrawals Complete";

                                    //int countOnline = Business.Market.InvestorList[i].CountInvestorOnline(Business.Market.InvestorList[i].InvestorID);
                                    //if (countOnline > 0)
                                    Business.Market.InvestorList[i].ClientCommandQueue.Add(Message);

                                    //SEND COMMAND TO AGENT SERVER
                                    string msg = Message + "," + Money + "," + InvestorID;
                                    Business.AgentNotify newAgentNotify = new AgentNotify();
                                    newAgentNotify.NotifyMessage = msg;
                                    TradingServer.Agent.AgentConfig.Instance.AddNotifyToAgent(newAgentNotify);

                                    Business.InvestorAccountLog newInvestorAccountLog = new InvestorAccountLog();
                                    newInvestorAccountLog.Name = Business.Market.InvestorList[i].NickName;
                                    newInvestorAccountLog.InvestorID = Business.Market.InvestorList[i].InvestorID;
                                    newInvestorAccountLog.Date = DateTime.Now;
                                    newInvestorAccountLog.Comment = Comment;
                                    newInvestorAccountLog.Amount = Money;
                                    newInvestorAccountLog.Code = "WRD01";

                                    //int ResultLog = TradingServer.Facade.FacadeAddInvestorAccountLog(newInvestorAccountLog);

                                    //string CommandCode = TradingServer.Model.TradingCalculate.Instance.BuildCommandCode(ResultLog.ToString());

                                    //TradingServer.Facade.FacadeUpdateDealID(ResultLog, CommandCode);

                                    Result = true;

                                    //SEND NOTIFY TO MANAGER THEN WITHDRAWALS
                                    TradingServer.Facade.FacadeSendNotifyManagerRequest(3, Business.Market.InvestorList[i]);
                                }
                                else
                                {
                                    Result = false;
                                }
                                #endregion
                            }
                            #endregion
                        }
                        else
                        {
                            Result = false;
                        }

                        //Check If Result = true Then Insert To Database In Table Command History
                        if (Result)
                        {
                            int resultAddCommandHistory = TradingServer.Facade.FacadeAddNewCommandHistory(InvestorID, 14, "", DateTime.Now, 0, DateTime.Now, 0, Money, 0, 0, DateTime.Now, 0, 0, 0, "", -1, 0, 0, Comment, "14", 0, -1, "", false, false);
                            if (resultAddCommandHistory > 0)
                            {
                                string commandCodeHistory = TradingServer.Model.TradingCalculate.Instance.BuildCommandCode(resultAddCommandHistory.ToString());
                                TradingServer.Facade.FacadeUpdateCommandCodeHistory(commandCodeHistory, resultAddCommandHistory);
                            }
                        }

                        string strContent = string.Empty;
                        string strBalance = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(balanceBefore.ToString(), 2);
                        string strMoney = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Money.ToString(), 2);
                        string strCreditAfter = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Business.Market.InvestorList[i].Balance.ToString(), 2);
                        strContent = "'" + Business.Market.InvestorList[i].Code + "': balance " + strBalance + " withdrawals " + strMoney + " -> " + strCreditAfter;
                        TradingServer.Facade.FacadeAddNewSystemLog(3, strContent, "[credit in account]", "", Business.Market.InvestorList[i].Code);

                        break;
                    }
                }
            }

            return Result;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Command"></param>
        private void ProcessSetTassMT4()
        {
            Investor.IsInProcess = true;
            this.isInTask = true;

            while (this.UpdateCommands.Count > 0)
            {
                #region WHILE
                try
                {
                    Business.OpenTrade Command = this.UpdateCommands[0];

                    this.UpdateCommands.RemoveAt(0);

                    if (Command == null)
                        continue;

                    bool IsBuy = Model.Helper.Instance.IsBuy(Command.Type.ID);

                    #region PROCESS MARKET AREA
                    if (Command.Symbol.MarketAreaRef.IMarketAreaName.Trim() == "SpotCommand")
                    {
                        #region Spot Command
                        if (this.CommandList != null)
                        {
                            #region For Command List
                            for (int i = 0; i < this.CommandList.Count; i++)
                            {
                                if (this.CommandList[i].ID == Command.ID)
                                {
                                    if (Command.IsClose == true)
                                    {
                                        #region COMMAND CLOSE
                                        int commandRefID = this.CommandList[i].ID;
                                        bool isPending = TradingServer.Model.TradingCalculate.Instance.CheckIsPendingPosition(Command.Type.ID);

                                        if (isPending)
                                        {
                                            #region CLOSE PENDING ORDER
                                            //ADD PENDING ORDER TO DATABASE
                                            int addHistory = TradingServer.Facade.FacadeAddNewCommandHistory(Command.Investor.InvestorID, Command.Type.ID, Command.CommandCode,
                                                Command.OpenTime, Command.OpenPrice, Command.CloseTime, Command.ClosePrice, 0, 0, 0, Command.ExpTime, Command.Size, Command.StopLoss,
                                                Command.TakeProfit, Command.ClientCode, Command.Symbol.SymbolID, Command.Taxes, Command.AgentCommission, Command.Comment, "3",
                                                Command.TotalSwap, Command.RefCommandID, Command.AgentRefConfig, Command.IsActivePending, Command.IsStopLossAndTakeProfit);

                                            //NEW SOLUTION ADD COMMAND TO REMOVE LIST
                                            Business.OpenRemove newOpenRemove = new OpenRemove();
                                            newOpenRemove.InvestorID = this.InvestorID;
                                            newOpenRemove.OpenTradeID = this.CommandList[i].ID;
                                            newOpenRemove.SymbolName = this.CommandList[i].Symbol.Name;
                                            newOpenRemove.IsExecutor = true;
                                            newOpenRemove.IsSymbol = true;
                                            newOpenRemove.IsInvestor = false;
                                            Business.Market.AddCommandToRemoveList(newOpenRemove);

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

                                            #region MAP STRING SEND TO CLIENT
                                            string Message = string.Empty;
                                            if (Command.IsServer)
                                            {
                                                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;
                                            }
                                            else
                                            {
                                                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;
                                            }

                                            this.ClientCommandQueue.Add(Message);

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

                                            #region INSERT SYSTEM LOG EVENT CLOSE SPOT COMMAND ORDER
                                            string mode = "sell";
                                            if (this.CommandList[i].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(), this.CommandList[i].Symbol.Digit);
                                            string strClosePrice = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Command.ClosePrice.ToString(), this.CommandList[i].Symbol.Digit);
                                            string stopLoss = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Command.StopLoss.ToString(), this.CommandList[i].Symbol.Digit);
                                            string takeProfit = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Command.TakeProfit.ToString(), this.CommandList[i].Symbol.Digit);
                                            string bid = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Command.Symbol.TickValue.Bid.ToString(), this.CommandList[i].Symbol.Digit);
                                            string ask = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Command.Symbol.TickValue.Ask.ToString(), this.CommandList[i].Symbol.Digit);

                                            string contentServer = "'" + this.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 = this.CommandList.Remove(this.CommandList[i]);
                                            }
                                            #endregion
                                        }
                                        else
                                        {
                                            int commandId = Command.ID;

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

                                            //Close Command Complete Add Message To Client
                                            if (this.ClientCommandQueue == null)
                                                this.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;

                                                this.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;

                                                this.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 j = 0; j < countMQL; j++)
                                                {
                                                    if (Business.Market.marketInstance.MQLCommands[j].InvestorCode == Command.Investor.Code)
                                                    {
                                                        Command.IpAddress = Business.Market.marketInstance.MQLCommands[j].IpAddress;
                                                        Business.Market.marketInstance.MQLCommands.Remove(Business.Market.marketInstance.MQLCommands[j]);
                                                        break;
                                                    }
                                                }
                                            }

                                            #region INSERT SYSTEM LOG EVENT CLOSE SPOT COMMAND ORDER
                                            string mode = "sell";
                                            if (this.CommandList[i].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(), this.CommandList[i].Symbol.Digit);
                                            string strClosePrice = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Command.ClosePrice.ToString(), this.CommandList[i].Symbol.Digit);
                                            string stopLoss = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Command.StopLoss.ToString(), this.CommandList[i].Symbol.Digit);
                                            string takeProfit = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Command.TakeProfit.ToString(), this.CommandList[i].Symbol.Digit);
                                            string bid = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Command.Symbol.TickValue.Bid.ToString(), this.CommandList[i].Symbol.Digit);
                                            string ask = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Command.Symbol.TickValue.Ask.ToString(), this.CommandList[i].Symbol.Digit);

                                            string contentServer = "'" + this.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 = this.CommandList.Remove(this.CommandList[i]);
                                            }
                                        }
                                        #endregion
                                    }
                                    else
                                    {
                                        #region UPDATE PENDING ORDER(PENDING ORDER ACTIVE)
                                        if (this.CommandList[i].Type.ID != Command.Type.ID)
                                        {
                                            if (this.CommandList[i].Type.ID > Command.Type.ID)
                                            {
                                                //SET OPEN TIME FOR COMMAND
                                                this.CommandList[i].OpenTime = DateTime.Now;
                                                Command.OpenTime = DateTime.Now;
                                                this.CommandList[i].OpenPrice = Command.OpenPrice;
                                                this.CommandList[i].IsActivePending = false;
                                                this.CommandList[i].IsStopLossAndTakeProfit = false;
                                                this.CommandList[i].Type = Command.Type;

                                                //CALCULATION MARGIN FOR COMMAND PENDING ORDER
                                                this.CommandList[i].CalculatorMarginCommand(this.CommandList[i]);

                                                //SET MARGIN FOR COMMAND
                                                Command.Margin = this.CommandList[i].Margin;

                                                #region FIND COMMAND IN COMMAND EXECUTOR AND UPDATE TYPE
                                                if (this.CommandList[i].InsExe != null && this.CommandList[i].InsExe.ID == Command.ID)
                                                {
                                                    this.CommandList[i].InsExe.Type = Command.Type;
                                                    this.CommandList[i].InsExe.Commission = Command.Commission;

                                                    this.CommandList[i].InsExe.Margin = Command.Margin;
                                                    this.CommandList[i].InsExe.OpenTime = Command.OpenTime;
                                                    this.CommandList[i].InsExe.OpenPrice = Command.OpenPrice;
                                                    this.CommandList[i].InsExe.IsActivePending = false;
                                                    this.CommandList[i].InsExe.IsStopLossAndTakeProfit = false;
                                                }

                                                //if (Business.Market.CommandExecutor != null)
                                                //{
                                                //    int countExe = Business.Market.CommandExecutor.Count;
                                                //    for (int m = 0; m < countExe; m++)
                                                //    {
                                                //        if (Business.Market.CommandExecutor[m].ID == Command.ID)
                                                //        {
                                                //            Business.Market.CommandExecutor[m].Type = Command.Type;
                                                //            Business.Market.CommandExecutor[m].Commission = Command.Commission;

                                                //            Business.Market.CommandExecutor[m].Margin = Command.Margin;
                                                //            Business.Market.CommandExecutor[m].OpenTime = Command.OpenTime;
                                                //            Business.Market.CommandExecutor[m].OpenPrice = Command.OpenPrice;
                                                //            Business.Market.CommandExecutor[m].IsActivePending = false;
                                                //            Business.Market.CommandExecutor[m].IsStopLossAndTakeProfit = false;

                                                //            break;
                                                //        }
                                                //    }
                                                //}
                                                #endregion

                                                #region UPDATE COMMAND TYPE IN SYMBOL LIST
                                                if (this.CommandList[i].Symbol != null && this.CommandList[i].Symbol.CommandList != null)
                                                {
                                                    int countCommand = this.CommandList[i].Symbol.CommandList.Count;
                                                    for (int j = 0; j < countCommand; j++)
                                                    {
                                                        if (this.CommandList[i].Symbol.CommandList[j].ID == Command.ID)
                                                        {
                                                            this.CommandList[i].Symbol.CommandList[j].Type = Command.Type;
                                                            this.CommandList[i].Symbol.CommandList[j].Commission = Command.Commission;

                                                            this.CommandList[i].Symbol.CommandList[j].Margin = Command.Margin;
                                                            this.CommandList[i].Symbol.CommandList[j].OpenTime = Command.OpenTime;
                                                            this.CommandList[i].Symbol.CommandList[j].OpenPrice = Command.OpenPrice;
                                                            this.CommandList[i].Symbol.CommandList[j].IsActivePending = false;
                                                            this.CommandList[i].Symbol.CommandList[j].IsStopLossAndTakeProfit = false;

                                                            break;
                                                        }
                                                    }
                                                }
                                                #endregion

                                                //Update Command Type In Database
                                                //TradingServer.Facade.FacadeUpdateOpenTrade(Command);

                                                #region Map Command Server To Client
                                                if (this.ClientCommandQueue == null)
                                                    this.ClientCommandQueue = new List<string>();

                                                string Message = "UpdatePendingOrder$True,Update Pending Order Command To Spot Command," + this.CommandList[i].ID + "," +
                                                    this.CommandList[i].Investor.InvestorID + "," + this.CommandList[i].Symbol.Name + "," + this.CommandList[i].Size + "," +
                                                    IsBuy + "," + this.CommandList[i].OpenTime + "," + this.CommandList[i].OpenPrice + "," + this.CommandList[i].StopLoss + "," +
                                                    this.CommandList[i].TakeProfit + "," + Command.ClosePrice + "," + Command.Commission + "," + Command.Swap + "," +
                                                    Command.Profit + "," + "Comment," + Command.ID + "," + "Open" + "," + 1 + "," + this.CommandList[i].ExpTime + "," +
                                                    this.CommandList[i].ClientCode + "," + this.CommandList[i].CommandCode + "," + this.CommandList[i].IsHedged + "," +
                                                    this.CommandList[i].Type.ID + "," + this.CommandList[i].Margin + ",UpdatePendingOrder";

                                                //int countInvestor = this.CountInvestorOnline(this.InvestorID);
                                                //if (countInvestor > 0)
                                                this.ClientCommandQueue.Add(Message);
                                                #endregion

                                                this.CommandList[i].IsProcess = false;
                                            }
                                        }
                                        #endregion

                                        #region UPDATE STOP LOSS AND TAKE PROFIT
                                        //Update Database If Command Is Update StopLoss Or Update Take Profit
                                        if (this.CommandList[i].StopLoss != Command.StopLoss ||
                                            this.CommandList[i].TakeProfit != Command.TakeProfit ||
                                            this.CommandList[i].OpenPrice != Command.OpenPrice)
                                        {
                                            this.CommandList[i].TakeProfit = Command.TakeProfit;
                                            this.CommandList[i].StopLoss = Command.StopLoss;

                                            bool checkIsPending = TradingServer.Model.TradingCalculate.Instance.CheckIsPendingPosition(this.CommandList[i].Type.ID);

                                            if (checkIsPending)
                                            {
                                                if (this.CommandList[i].OpenPrice != Command.OpenPrice)
                                                {
                                                    this.CommandList[i].OpenPrice = Command.OpenPrice;
                                                }
                                            }

                                            //If Client Update Command Then Add Message To Client Message
                                            if (this.ClientCommandQueue == null)
                                                this.ClientCommandQueue = new List<string>();

                                            //SEND NOTIFY TO MANAGER
                                            TradingServer.Facade.FacadeSendNoticeManagerRequest(1, this.CommandList[i]);
                                        }
                                        #endregion
                                    }
                                    break;
                                }
                            }
                            #endregion
                        }
                        else
                        {
                            #region Init Command List Of Investor
                            this.CommandList = new List<OpenTrade>();
                            this.CommandList.Add(Command);
                            #endregion
                        }
                        #endregion
                    }
                    #endregion

                    //Call Function ReCalculation Account Of Investor
                    this.ReCalculationAccount();

                    Command = null;
                }
                catch (Exception ex)
                {
                    TradingServer.Facade.FacadeAddNewSystemLog(1, ex.Message, "[Process Close Open Position]", "", "");
                }
                #endregion
            }

            this.isInTask = false;
            Investor.IsInProcess = false;
        }
        /// <summary>
        /// If Margin Level Of Account < Stop Out Level Setting In Admin Then Close All Command Of Investor
        /// </summary>
        internal void StopOutLevel()
        {
            if (this.CommandList != null && this.CommandList.Count > 0)
            {
                int count = this.CommandList.Count;

                string comment = "so: " + Math.Round(this.MarginLevel, 2) + "%/" + Math.Round(this.Equity, 2) + "/" + this.Margin;

                for (int i = 0; i < this.CommandList.Count; i++)
                {
                    bool isTrade = TradingServer.Facade.FacadeCheckStatusSymbol(this.CommandList[i].Symbol.Name.Trim());
                    //TradingServer.Facade.FacadeAddNewSystemLog(6, "Status Trade Symbol: " + this.CommandList[i].Symbol.Name + " is: " + isTrade,
                    //                                            "[Check Status Symbol]", "", this.Code);
                    if (isTrade == true)
                    {
                        bool isPending = TradingServer.Model.TradingCalculate.Instance.CheckIsPendingPosition(this.CommandList[i].Type.ID);
                        if (isPending)
                            this.CommandList[i].Profit = 0;

                        #region Command Is Close
                        int ResultHistory = -1;
                        //Add Command To Command History
                        this.CommandList[i].ExpTime = DateTime.Now;
                        this.CommandList[i].CloseTime = DateTime.Now;
                        this.CommandList[i].Comment = comment;

                        //double totalSwap = this.CommandList[i].Swap + this.CommandList[i].TotalSwap;
                        //this.CommandList[i].TotalSwap = 0;
                        //this.CommandList[i].Swap = totalSwap;

                        if (isPending)
                        {
                            ResultHistory = TradingServer.Facade.FacadeAddNewCommandHistory(this.CommandList[i].Investor.InvestorID, this.CommandList[i].Type.ID,
                                this.CommandList[i].CommandCode, this.CommandList[i].OpenTime, this.CommandList[i].OpenPrice, this.CommandList[i].CloseTime,
                                this.CommandList[i].ClosePrice, 0, this.CommandList[i].Swap, this.CommandList[i].Commission,
                                this.CommandList[i].ExpTime, this.CommandList[i].Size, this.CommandList[i].StopLoss, this.CommandList[i].TakeProfit,
                                this.CommandList[i].ClientCode, this.CommandList[i].Symbol.SymbolID, this.CommandList[i].Taxes, this.CommandList[i].AgentCommission,
                                this.CommandList[i].Comment, "15", this.CommandList[i].TotalSwap, this.CommandList[i].RefCommandID,
                                this.CommandList[i].AgentRefConfig, this.CommandList[i].IsActivePending, this.CommandList[i].IsStopLossAndTakeProfit);
                        }
                        else
                        {
                            ResultHistory = TradingServer.Facade.FacadeAddNewCommandHistory(this.CommandList[i].Investor.InvestorID, this.CommandList[i].Type.ID,
                                this.CommandList[i].CommandCode, this.CommandList[i].OpenTime, this.CommandList[i].OpenPrice, this.CommandList[i].CloseTime,
                                this.CommandList[i].ClosePrice, this.CommandList[i].Profit, this.CommandList[i].Swap, this.CommandList[i].Commission,
                                this.CommandList[i].ExpTime, this.CommandList[i].Size, this.CommandList[i].StopLoss, this.CommandList[i].TakeProfit,
                                this.CommandList[i].ClientCode, this.CommandList[i].Symbol.SymbolID, this.CommandList[i].Taxes, this.CommandList[i].AgentCommission,
                                this.CommandList[i].Comment, "15", this.CommandList[i].TotalSwap, this.CommandList[i].RefCommandID,
                                this.CommandList[i].AgentRefConfig, this.CommandList[i].IsActivePending, this.CommandList[i].IsStopLossAndTakeProfit);
                        }

                        if (ResultHistory > 0)
                        {
                            //Log Stop Out
                            string content = string.Empty;
                            string mode = TradingServer.Facade.FacadeGetTypeNameByTypeID(this.CommandList[i].Type.ID).ToLower();
                            string size = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(this.CommandList[i].Size.ToString(), 2);
                            string openPrice = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(this.CommandList[i].OpenPrice.ToString(), this.CommandList[i].Symbol.Digit);
                            content = "'" + this.CommandList[i].Investor.Code + "': stop out #" + this.CommandList[i].CommandCode + " " + mode + " " + size + " " + this.CommandList[i].Symbol.Name + " at " + openPrice +
                                " (" + this.CommandList[i].Symbol.TickValue.Bid + "/" + this.CommandList[i].Symbol.TickValue.Ask + ")";

                            TradingServer.Facade.FacadeAddNewSystemLog(5, content, "[stop out]", "", this.Code);

                            //NEW SOLUTION ADD COMMAND TO REMOVE LIST
                            //Business.OpenTrade newOpenTrade = this.CommandList[i];
                            Business.OpenRemove newOpenRemove = new OpenRemove();
                            newOpenRemove.InvestorID = this.InvestorID;
                            newOpenRemove.OpenTradeID = this.CommandList[i].ID;
                            newOpenRemove.SymbolName = this.CommandList[i].Symbol.Name;
                            newOpenRemove.IsExecutor = true;
                            newOpenRemove.IsSymbol = true;
                            newOpenRemove.IsInvestor = false;
                            Business.Market.AddCommandToRemoveList(newOpenRemove);

                            double totalProfit = Math.Round(this.CommandList[i].Profit + this.CommandList[i].Commission + this.CommandList[i].Swap, 2);
                            if (!isPending)
                            {
                                this.Balance += totalProfit;

                                //Update Balance Of Investor Account
                                this.UpdateBalance(this.CommandList[i].Investor.InvestorID, this.Balance);
                            }

                            //Update Command In Database
                            TradingServer.Facade.FacadeDeleteOpenTradeByID(this.CommandList[i].ID);

                            this.Profit = this.InvestorGroupInstance.CalculationTotalProfit(this.CommandList);
                            this.Equity = this.Balance + this.Credit + this.Profit;
                            double Loss = this.InvestorGroupInstance.CalculationTotalLoss(this.CommandList);
                            double Profit = this.InvestorGroupInstance.CalculationTotalProfitPositive(this.CommandList);

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

                            #region Map Command Server To Client
                            string Message = "StopOut$True,Close Command Complete," + this.CommandList[i].ID + "," + this.CommandList[i].Investor.InvestorID + "," +
                                this.CommandList[i].Symbol.Name + "," + this.CommandList[i].Size + "," + false + "," + this.CommandList[i].OpenTime + "," +
                                this.CommandList[i].OpenPrice + "," + this.CommandList[i].StopLoss + "," + this.CommandList[i].TakeProfit + "," +
                                this.CommandList[i].ClosePrice + "," + this.CommandList[i].Commission + "," + this.CommandList[i].Swap + "," +
                                this.CommandList[i].Profit + "," + "Comment," + this.CommandList[i].ID + "," + this.CommandList[i].Type.Name + "," +
                                1 + "," + this.CommandList[i].ExpTime + "," + this.CommandList[i].ClientCode + "," + this.CommandList[i].CommandCode + "," +
                                this.CommandList[i].IsHedged + "," + this.CommandList[i].Type.ID + "," + this.CommandList[i].Margin + ",Close" + "," +
                                this.CommandList[i].CloseTime;

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

                            this.ClientCommandQueue.Add(Message);

                            //SEND COMMAND TO AGENT SERVER

                            string msg = "StopOut$True,Close Command Complete," + this.CommandList[i].ID + "," + this.CommandList[i].Investor.InvestorID + "," +
                                                        this.CommandList[i].Symbol.Name + "," + this.CommandList[i].Size + "," + false + "," +
                                                        this.CommandList[i].OpenTime + "," + this.CommandList[i].OpenPrice + "," +
                                                        this.CommandList[i].StopLoss + "," + this.CommandList[i].TakeProfit + "," +
                                                        this.CommandList[i].ClosePrice + "," + this.CommandList[i].Commission + "," +
                                                        this.CommandList[i].Swap + "," + this.CommandList[i].Profit + "," + "Comment," +
                                                        this.CommandList[i].ID + "," + this.CommandList[i].Type.Name + "," +
                                                        1 + "," + this.CommandList[i].ExpTime + "," + this.CommandList[i].ClientCode + "," +
                                                        this.CommandList[i].CommandCode + "," + this.CommandList[i].IsHedged + "," +
                                                        this.CommandList[i].Type.ID + "," + this.CommandList[i].Margin +
                                                        ",Close," + this.CommandList[i].CloseTime;

                            msg += this.CommandList[i].AgentRefConfig;
                            Business.AgentNotify newAgentNotify = new AgentNotify();
                            newAgentNotify.NotifyMessage = msg;
                            TradingServer.Agent.AgentConfig.Instance.AddNotifyToAgent(newAgentNotify, this.CommandList[i].Investor.InvestorGroupInstance);

                            #endregion

                            //SEND NOTIFY TO MANAGER
                            TradingServer.Facade.FacadeSendNoticeManagerRequest(2, this.CommandList[i]);

                            lock (Business.Market.syncObject)
                            {
                                //Remove Command In Investor List
                                this.CommandList.RemoveAt(i);
                            }
                        }
                        else
                        {
                            TradingServer.Facade.FacadeAddNewSystemLog(1, "can not delete command", "[stop out account] " + this.Code, "", this.Code);
                        }
                        #endregion

                        i--;
                    }
                }
            }

            if (this.CommandList.Count > 0 && this.CommandList != null)
            {
                //Call function Total Margin Of Investor
                Business.Margin totalMargin = new Business.Margin();
                totalMargin = this.CommandList[0].Symbol.CalculationTotalMargin(this.CommandList);
                this.Margin = totalMargin.TotalMargin;
                this.FreezeMargin = totalMargin.TotalFreezeMargin;

                this.Profit = this.InvestorGroupInstance.CalculationTotalProfit(this.CommandList);
                this.Equity = this.Balance + this.Credit + this.Profit;
                this.MarginLevel = (this.Equity * 100) / (this.Margin + this.FreezeMargin);
            }
            else
            {
                this.Margin = 0;
                this.FreezeMargin = 0;
                this.Profit = 0;
                this.Equity = 0;
                this.MarginLevel = 0;
            }

            //send notify to manager update account investor
            TradingServer.Facade.FacadeSendNotifyManagerRequest(3, this);
        }
        /// <summary>
        /// 
        /// </summary>
        private void CleanDemoAccount(int dayDemo)
        {
            return;
            if (Business.Market.InvestorList != null)
            {
                int count = Business.Market.InvestorList.Count;
                for (int i = 0; i < count; i++)
                {
                    TimeSpan timeDemo = DateTime.Now - Business.Market.InvestorList[i].RegisterDay;
                    if (timeDemo.TotalDays >= 14)
                    {
                        if (Business.Market.InvestorList[i].InvestorGroupInstance.Name.IndexOf("Demo") != -1)
                        {
                            if (Business.Market.InvestorList[i].CommandList != null)
                            {
                                int countCommand = Business.Market.InvestorList[i].CommandList.Count;
                                for (int j = countCommand - 1; j >= 0; j--)
                                {
                                    int commandID = Business.Market.InvestorList[i].CommandList[j].ID;

                                    #region COMMENT CODE(REMOVE COMMAND IN COMMAND EXECUTOR AND SYMBOL LIST
                                    //TradingServer.Facade.FacadeRemoveOpenTradeInCommandExecutor(commandID);
                                    //TradingServer.Facade.FacadeRemoveOpenTradeInCommandList(commandID);
                                    #endregion

                                    //NEW SOLUTION ADD COMMAND TO REMOVE LIST
                                    //Business.OpenTrade newOpenTrade = Business.Market.InvestorList[i].CommandList[j];
                                    Business.OpenRemove newOpenRemove = new OpenRemove();
                                    newOpenRemove.InvestorID = Business.Market.InvestorList[i].InvestorID;
                                    newOpenRemove.OpenTradeID = Business.Market.InvestorList[i].CommandList[j].ID;
                                    newOpenRemove.SymbolName = Business.Market.InvestorList[i].CommandList[j].Symbol.Name;
                                    newOpenRemove.IsExecutor = true;
                                    newOpenRemove.IsSymbol = true;
                                    newOpenRemove.IsInvestor = false;
                                    Business.Market.AddCommandToRemoveList(newOpenRemove);

                                    lock (Business.Market.syncObject)
                                    {
                                        Business.Market.InvestorList[i].CommandList.RemoveAt(j);
                                    }
                                }
                            }

                            Business.Market.InvestorList[i].IsDisable = true;
                            TradingServer.Facade.FacadeUpdateInvestor(Business.Market.InvestorList[i]);
                        }
                    }
                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Command"></param>
        private void ProcessSetTask()
        {
            Investor.IsInProcess = true;
            this.isInTask = true;

            while (this.UpdateCommands.Count > 0)
            {
                #region WHILE
                try
                {
                    DateTime timeStart = DateTime.Now;

                    NumCheck++;
                    if (NumCheck == 100)
                        NumCheck = 0;

                    Business.OpenTrade Command = this.UpdateCommands[0];

                    this.UpdateCommands.RemoveAt(0);

                    if (Command == null)
                        continue;

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

                    #region PROCESS MARKET AREA
                    if (Command.Symbol.MarketAreaRef.IMarketAreaName.Trim() == "SpotCommand")
                    {
                        #region Spot Command
                        if (this.CommandList != null)
                        {
                            if (Command.IsClose && Command.IsMultiClose)
                            {
                                this.MultiCloseCommand(Command);
                            }
                            else
                            {
                                #region For Command List
                                bool FlagCommand = false;
                                for (int i = 0; i < this.CommandList.Count; i++)
                                {
                                    if (this.CommandList[i].ID == Command.ID)
                                    {
                                        if (Command.IsClose == true)
                                        {
                                            int commandRefID = this.CommandList[i].ID;
                                            bool isPending = TradingServer.Model.TradingCalculate.Instance.CheckIsPendingPosition(Command.Type.ID);

                                            //if (Command.Type.ID == 7 || Command.Type.ID == 8 || Command.Type.ID == 9 || Command.Type.ID == 10)
                                            if(isPending)
                                            {
                                                #region CLOSE PENDING ORDER
                                                //ADD PENDING ORDER TO DATABASE
                                                //int addHistory = TradingServer.Facade.FacadeAddNewCommandHistory(Command.Investor.InvestorID, Command.Type.ID, Command.CommandCode,
                                                //    Command.OpenTime, Command.OpenPrice, Command.CloseTime, Command.ClosePrice, 0, 0, 0, Command.ExpTime, Command.Size, Command.StopLoss,
                                                //    Command.TakeProfit, Command.ClientCode, Command.Symbol.SymbolID, Command.Taxes, Command.AgentCommission, Command.Comment, "3",
                                                //    Command.TotalSwap, Command.RefCommandID, Command.AgentRefConfig, Command.IsActivePending, Command.IsStopLossAndTakeProfit);

                                                //NEW SOLUTION ADD COMMAND TO REMOVE LIST
                                                Business.OpenRemove newOpenRemove = new OpenRemove();
                                                newOpenRemove.InvestorID = this.InvestorID;
                                                newOpenRemove.OpenTradeID = this.CommandList[i].ID;
                                                newOpenRemove.SymbolName = this.CommandList[i].Symbol.Name;
                                                newOpenRemove.IsExecutor = true;
                                                newOpenRemove.IsSymbol = true;
                                                newOpenRemove.IsInvestor = false;
                                                Business.Market.AddCommandToRemoveList(newOpenRemove);

                                                //Delete Command In Database
                                                //bool deleteDB = TradingServer.Facade.FacadeDeleteOpenTradeByID(Command.ID);

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

                                                #region MAP STRING SEND TO CLIENT
                                                string Message = string.Empty;
                                                if (Command.IsServer)
                                                {
                                                    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;

                                                    string msg = "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";

                                                    string msgNotify = "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.AgentRefConfig + "," + Command.SpreaDifferenceInOpenTrade;

                                                    //SEND COMMAND TO AGENT SERVER
                                                    Business.AgentNotify newAgentNotify = new AgentNotify();
                                                    newAgentNotify.NotifyMessage = msgNotify;
                                                    TradingServer.Agent.AgentConfig.Instance.AddNotifyToAgent(newAgentNotify, Command.Investor.InvestorGroupInstance);
                                                }
                                                else
                                                {
                                                    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;

                                                    string msg = "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";

                                                    string msgNotify = "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.AgentRefConfig + "," + Command.SpreaDifferenceInOpenTrade;

                                                    //SEND COMMAND TO AGENT SERVER
                                                    Business.AgentNotify newAgentNotify = new AgentNotify();
                                                    newAgentNotify.NotifyMessage = msgNotify;
                                                    TradingServer.Agent.AgentConfig.Instance.AddNotifyToAgent(newAgentNotify, Command.Investor.InvestorGroupInstance);
                                                }

                                                this.ClientCommandQueue.Add(Message);
                                                #endregion

                                                #region INSTER SYSTEM LOG(EVENT DELETE PENDING ORDER)
                                                string mode = TradingServer.Facade.FacadeGetTypeNameByTypeID(this.CommandList[i].Type.ID).ToLower();
                                                string size = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(this.CommandList[i].Size.ToString(), 2);
                                                string openPrice = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(this.CommandList[i].OpenPrice.ToString(), this.CommandList[i].Symbol.Digit);

                                                string content = "'" + this.CommandList[i].Investor.Code + "': delete order #" + this.CommandList[i].CommandCode + " " + mode + " " +
                                                    size + this.CommandList[i].Symbol.Name + " at " + openPrice;

                                                TradingServer.Facade.FacadeAddNewSystemLog(5, content, "[delete order]", Command.Investor.IpAddress, Command.Investor.Code);
                                                #endregion

                                                lock (Business.Market.syncObject)
                                                {
                                                    //Delete Command In Investor
                                                    bool deleteCommandInvestor = this.CommandList.Remove(this.CommandList[i]);
                                                }
                                                #endregion
                                            }
                                            else
                                            {
                                                #region CLOSE SPOT COMMAND
                                                #region GET STEP LOTS
                                                double step = -1;
                                                int strStep = -1;
                                                if (Command.IGroupSecurity != null)
                                                {
                                                    if (Command.IGroupSecurity.IGroupSecurityConfig != null)
                                                    {
                                                        int count = Command.IGroupSecurity.IGroupSecurityConfig.Count;
                                                        for (int j = 0; j < count; j++)
                                                        {
                                                            if (Command.IGroupSecurity.IGroupSecurityConfig[j].Code == "B13")
                                                                double.TryParse(Command.IGroupSecurity.IGroupSecurityConfig[j].NumValue, out step);
                                                        }
                                                    }
                                                }

                                                if (step > 0)
                                                {
                                                    string[] temp = step.ToString().Split('.');
                                                    if (temp.Length > 1)
                                                    {
                                                        strStep = temp[1].Length;
                                                    }
                                                }
                                                #endregion

                                                double tempSize = -1;
                                                if (strStep > 0)
                                                    tempSize = Math.Round((this.CommandList[i].Size - Command.Size), strStep);
                                                else
                                                    tempSize = Math.Round((this.CommandList[i].Size - Command.Size), 2);

                                                if (tempSize > 0)
                                                    Command.Commission = Model.CalculationFormular.Instance.CalculationCommission(Command);

                                                #region CALCLUATION COMMISSION FOR AGENT(COMMENT 03-02-2012)
                                                //double commissionAgent = Model.CalculationFormular.Instance.CalculationAgentCommission(Command);

                                                ////set Agent Commission To Open Trade
                                                //Command.AgentCommission = commissionAgent;

                                                //Business.Investor newAgent = TradingServer.Facade.FacadeSelectInvestorByCode(Command.Investor.AgentID);

                                                //if (newAgent.InvestorID > 0)
                                                //{
                                                //    //Update balance of agent
                                                //    double BalanceAgent = newAgent.Balance + commissionAgent;
                                                //    TradingServer.Facade.FacadeUpdateBalance(newAgent.InvestorID, BalanceAgent);

                                                //    //search in investor online if agent online then send message get balance of agent
                                                //    if (Business.Market.InvestorList != null)
                                                //    {
                                                //        int countInvestorOnline = Business.Market.InvestorList.Count;
                                                //        for (int m = 0; m < countInvestorOnline; m++)
                                                //        {
                                                //            if (Business.Market.InvestorList[m].InvestorID == newAgent.InvestorID)
                                                //            {
                                                //                if (Business.Market.InvestorList[m].IsOnline)
                                                //                {
                                                //                    string Message = "GetNewBalance";
                                                //                    Business.Market.InvestorList[m].ClientCommandQueue.Add(Message);
                                                //                }

                                                //                Business.Market.InvestorList[m].Balance += commissionAgent;

                                                //                //SEND NOTIFY TO MANAGER with type =3 then balance and credit
                                                //                TradingServer.Facade.FacadeSendNotifyManagerRequest(3, Business.Market.InvestorList[m]);

                                                //                break;
                                                //            }
                                                //        }
                                                //    }
                                                //}
                                                //else
                                                //{
                                                //    Command.AgentCommission = 0;
                                                //}
                                                #endregion

                                                #region CALCULATION COMMISSION FOR AGENT
                                                double commissionAgent = Model.CalculationFormular.Instance.CalculationAgentCommission(Command);

                                                //search in investor online if agent online then send message get balance of agent
                                                if (Business.Market.InvestorList != null)
                                                {
                                                    int countInvestorOnline = Business.Market.InvestorList.Count;
                                                    for (int m = 0; m < countInvestorOnline; m++)
                                                    {
                                                        if (Business.Market.InvestorList[m].Code == Command.Investor.AgentID)
                                                        {
                                                            double BalanceAgent = Business.Market.InvestorList[m].Balance + commissionAgent;

                                                            Business.Market.InvestorList[m].Balance += commissionAgent;

                                                            //Update balance of agent
                                                            TradingServer.Facade.FacadeUpdateBalance(Business.Market.InvestorList[m].InvestorID, BalanceAgent);

                                                            //set Agent Commission To Open Trade
                                                            Command.AgentCommission = commissionAgent;

                                                            //SEND NOTIFY TO MANAGER with type =3 then balance and credit
                                                            TradingServer.Facade.FacadeSendNotifyManagerRequest(3, Business.Market.InvestorList[m]);

                                                            if (Business.Market.InvestorList[m].IsOnline)
                                                            {
                                                                string Message = "GetNewBalance";

                                                                Business.Market.InvestorList[m].ClientCommandQueue.Add(Message);
                                                            }

                                                            break;
                                                        }
                                                    }
                                                }
                                                #endregion

                                                int commandId = Command.ID;

                                                int ResultHistory = -1;
                                                //Add Command To Command History
                                                ResultHistory = TradingServer.Facade.FacadeAddNewCommandHistory(Command.Investor.InvestorID, Command.Type.ID, Command.CommandCode, Command.OpenTime,
                                                    Command.OpenPrice, Command.CloseTime, Command.ClosePrice, Command.Profit, this.CommandList[i].Swap, Command.Commission, Command.ExpTime,
                                                    Command.Size, Command.StopLoss, Command.TakeProfit, Command.ClientCode, Command.Symbol.SymbolID, Command.Taxes, Command.AgentCommission,
                                                    Command.Comment, "4", this.CommandList[i].TotalSwap, this.CommandList[i].RefCommandID,
                                                    this.CommandList[i].AgentRefConfig, this.CommandList[i].IsActivePending, this.CommandList[i].IsStopLossAndTakeProfit);

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

                                                double totalProfit = Math.Round(Command.Profit + Command.Commission + Command.Swap, 2);
                                                //CHANGE FORMAT CALCULATION SWAP(20-02-2012)
                                                //Update Balance Of Investor Account
                                                double totalBalance = this.Balance + totalProfit;

                                                bool updateBalance = false;
                                                if (!Business.Market.IsConnectMT4)
                                                    updateBalance = this.UpdateBalance(Command.Investor.InvestorID, totalBalance);

                                                if (updateBalance)
                                                {
                                                    string strBalance = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(this.Balance.ToString(), 2);
                                                    string strProfit = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(totalProfit.ToString(), 2);
                                                    string strCommission = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Command.Commission.ToString(), 2);
                                                    string strSwap = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(this.CommandList[i].Swap.ToString(), 2);
                                                    string strTotalSwap = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(this.CommandList[i].TotalSwap.ToString(), 2);
                                                    string strTotalBalance = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(totalBalance.ToString(), 2);

                                                    //'00001140': close command #00148535 balance : 123456 command profit : 10000
                                                    string content = "'" + this.Code + "': close command #" + this.CommandList[i].CommandCode + " balance : " + strBalance + " command profit: " + strProfit +
                                                        " Commission: " + strCommission + " Swap: " + strSwap + " Total Swap: " + strTotalSwap +
                                                        " -> total balance: " + strTotalBalance;

                                                    TradingServer.Facade.FacadeAddNewSystemLog(5, content, "[Close Command]", "", this.Code);

                                                    if (!Business.Market.IsConnectMT4)
                                                    {
                                                        this.Balance += totalProfit;
                                                    }
                                                }

                                                //Update Command In Database
                                                bool deleteCommandDB = TradingServer.Facade.FacadeDeleteOpenTradeByID(Command.ID);

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

                                                string msg = string.Empty;

                                                #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;

                                                    //int countOnline = this.CountInvestorOnline(this.InvestorID);
                                                    //if (countOnline > 0)
                                                    this.ClientCommandQueue.Add(Message);

                                                    string msgNotify = "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.AgentRefConfig + "," + Command.SpreaDifferenceInOpenTrade;

                                                    //SEND COMMAND TO AGENT SERVER
                                                    Business.AgentNotify newAgentNotify = new AgentNotify();
                                                    newAgentNotify.NotifyMessage = msgNotify;
                                                    TradingServer.Agent.AgentConfig.Instance.AddNotifyToAgent(newAgentNotify, Command.Investor.InvestorGroupInstance);

                                                    msg = Message;
                                                }
                                                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;

                                                    //int countOnline = this.CountInvestorOnline(this.InvestorID);
                                                    //if (countOnline > 0)
                                                    this.ClientCommandQueue.Add(Message);

                                                    string msgNotify = "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.AgentRefConfig + "," + Command.SpreaDifferenceInOpenTrade;

                                                    //SEND COMMAND TO AGENT SERVER
                                                    Business.AgentNotify newAgentNotify = new AgentNotify();
                                                    newAgentNotify.NotifyMessage = msgNotify;
                                                    TradingServer.Agent.AgentConfig.Instance.AddNotifyToAgent(newAgentNotify, Command.Investor.InvestorGroupInstance);

                                                    msg = Message;
                                                }
                                                #endregion

                                                #region INSERT SYSTEM LOG EVENT CLOSE SPOT COMMAND ORDER
                                                string mode = "sell";
                                                if (this.CommandList[i].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(), this.CommandList[i].Symbol.Digit);
                                                string strClosePrice = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Command.ClosePrice.ToString(), this.CommandList[i].Symbol.Digit);
                                                string stopLoss = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Command.StopLoss.ToString(), this.CommandList[i].Symbol.Digit);
                                                string takeProfit = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Command.TakeProfit.ToString(), this.CommandList[i].Symbol.Digit);
                                                string bid = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Command.Symbol.TickValue.Bid.ToString(), this.CommandList[i].Symbol.Digit);
                                                string ask = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Command.Symbol.TickValue.Ask.ToString(), this.CommandList[i].Symbol.Digit);

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

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

                                                lock (Business.Market.syncObject)
                                                {
                                                    //Remove Command In Investor List
                                                    //this.CommandList.Remove(this.CommandList[i]);
                                                    bool deleteCommandInvestor = this.CommandList.Remove(this.CommandList[i]);
                                                }

                                                #region RECALCULATION TOTAL MARGIN OF INVESTOR
                                                //RECACULATION TOTAL MARGIN OF INVESTOR
                                                if (this.CommandList.Count > 0)
                                                {
                                                    Business.Margin newMargin = new Margin();
                                                    newMargin = Command.Symbol.CalculationTotalMargin(this.CommandList);
                                                    this.Margin = newMargin.TotalMargin;
                                                    this.FreezeMargin = newMargin.TotalFreezeMargin;
                                                }
                                                else
                                                {
                                                    this.Margin = 0;
                                                    this.FreezeMargin = 0;
                                                }
                                                #endregion

                                                #region MAKE NEW COMMAND WITH DELTA SIZE
                                                if (!Business.Market.IsConnectMT4)
                                                {
                                                    if (tempSize > 0)
                                                    {
                                                        //SET MARGIN OF NEW COMMAND
                                                        Command.Margin = 0;

                                                        //SET NEW SIZE AND NEW PRICES
                                                        Command.Size = tempSize;
                                                        Command.Swap = 0;
                                                        Command.IsReNewOpen = true;
                                                        Command.RefCommandID = commandRefID;
                                                        switch (Command.Type.ID)
                                                        {
                                                            case 1:
                                                                {
                                                                    Command.ClosePrice = Command.Symbol.TickValue.Bid;
                                                                    Command.OpenPrice = Command.OpenPrice;
                                                                }
                                                                break;
                                                            case 2:
                                                                {
                                                                    Command.ClosePrice = Command.Symbol.TickValue.Ask;
                                                                    Command.OpenPrice = Command.OpenPrice;
                                                                }
                                                                break;
                                                        }

                                                        Command.IsServer = true;
                                                        Command.Symbol.MarketAreaRef.AddCommand(Command);
                                                    }
                                                }
                                                #endregion

                                                //SEND NOTIFY TO MANAGER THEN CLOSE COMMAND
                                                TradingServer.Facade.FacadeSendNotifyManagerRequest(3, this);
                                                #endregion

                                                //CHECK NEW RULE CLOSE POSITION
                                                Command.CheckRuleCloseOpenPosition(this.CommandList, tempSize);
                                            }
                                        }
                                        else
                                        {
                                            #region Command Is Open
                                            //Update Command Of Investor
                                            //this.CommandList[i].ClientCode = Command.ClientCode;

                                            #region SET CLOSE PRICE FOR COMMAND
                                            if (Command.ClosePrice > 0)

                                            {
                                                this.CommandList[i].ClosePrice = Command.ClosePrice;
                                            }
                                            else
                                            {
                                                switch (this.CommandList[i].Type.ID)
                                                {
                                                    case 1:
                                                        if (this.CommandList[i].Symbol.TickValue != null && this.CommandList[i].Symbol.TickValue.Bid > 0)
                                                            this.CommandList[i].ClosePrice = this.CommandList[i].Symbol.TickValue.Bid;
                                                        break;

                                                    case 2:
                                                        if (this.CommandList[i].Symbol.TickValue != null && this.CommandList[i].Symbol.TickValue.Ask > 0)
                                                        {
                                                            double Ask = 0;
                                                            Ask = (Symbol.ConvertNumberPip(this.CommandList[i].Symbol.Digit, this.CommandList[i].SpreaDifferenceInOpenTrade) +
                                                                this.CommandList[i].Symbol.TickValue.Ask);
                                                            this.CommandList[i].ClosePrice = Ask;
                                                        }
                                                        break;
                                                }
                                            }
                                            #endregion

                                            bool isPending = TradingServer.Model.TradingCalculate.Instance.CheckIsPendingPosition(this.CommandList[i].Type.ID);
                                            if (!isPending)
                                            {
                                                this.CommandList[i].CalculatorProfitCommand(this.CommandList[i]);
                                                this.CommandList[i].Profit = this.CommandList[i].Symbol.ConvertCurrencyToUSD(this.CommandList[i].Symbol.Currency, this.CommandList[i].Profit,
                                                    false, this.CommandList[i].SpreaDifferenceInOpenTrade, this.CommandList[i].Symbol.Digit);

                                                #region UPDATE CLOSE PRICES IN COMMAND EXECUTOR
                                                //if (Business.Market.CommandExecutor != null)
                                                //{
                                                //    int countCommandExe = Business.Market.CommandExecutor.Count;
                                                //    for (int j = 0; j < countCommandExe; j++)
                                                //    {
                                                //        if (Business.Market.CommandExecutor[j].ID == Command.ID)
                                                //        {
                                                //            Business.Market.CommandExecutor[j].ClosePrice = Command.ClosePrice;
                                                //            Business.Market.CommandExecutor[j].Profit = this.CommandList[i].Profit;

                                                //            break;
                                                //        }
                                                //    }
                                                //}
                                                this.CommandList[i].InsExe.Profit = this.CommandList[i].Profit;
                                                #endregion

                                                #region UPDATE PROFIT IN SYMBOL LIST
                                                if (this.CommandList[i].Symbol != null && this.CommandList[i].Symbol.CommandList != null)
                                                {
                                                    int countCommand = this.CommandList[i].Symbol.CommandList.Count;
                                                    for (int j = 0; j < countCommand; j++)
                                                    {
                                                        if (this.CommandList[i].Symbol.CommandList[j].ID == Command.ID)
                                                        {
                                                            this.CommandList[i].Symbol.CommandList[j].Profit = this.CommandList[i].Profit;

                                                            break;
                                                        }
                                                    }
                                                }
                                                #endregion
                                            }
                                            else
                                            {
                                                #region UPDATE PENDING ORDER(PENDING ORDER ACTIVE)
                                                if (this.CommandList[i].Type.ID != Command.Type.ID)
                                                {
                                                    if (this.CommandList[i].Type.ID > Command.Type.ID)
                                                    {
                                                        //CHECK VALID ACCOUNT IF TRUE THEN UPDATE TYPE ELSE
                                                        //THEN CANCEL PENDING ORDER AND INSERT DATABASE
                                                        bool checkValidAccount = Command.CheckValidAccountInvestor(Command);

                                                        if (checkValidAccount)
                                                        {
                                                            //SET OPEN TIME FOR COMMAND
                                                            this.CommandList[i].OpenTime = DateTime.Now;
                                                            Command.OpenTime = DateTime.Now;
                                                            this.CommandList[i].OpenPrice = Command.OpenPrice;
                                                            this.CommandList[i].IsActivePending = false;
                                                            this.CommandList[i].IsStopLossAndTakeProfit = false;

                                                            //CALCULATION COMMISSION(COMMENT BECAUSE DON"T NEED, COMMISSION GET FROM MT4)
                                                            //double commission = Model.CalculationFormular.Instance.CalculationCommission(Command);
                                                            //Command.Commission = commission;

                                                            #region CALCULATION AGENT COMMISSION
                                                            //CALCULATION AGENT COMMISSION
                                                            double agentCommission = Model.CalculationFormular.Instance.CalculationAgentCommission(Command);

                                                            Business.Investor newAgent = TradingServer.Facade.FacadeSelectInvestorByCode(Command.Investor.AgentID);

                                                            //set commission and agent commission to command list in investor
                                                            this.CommandList[i].AgentCommission = agentCommission;
                                                            //this.CommandList[i].Commission = commission;
                                                            this.CommandList[i].Commission = Command.Commission;

                                                            #region FIND AGENT AND REQUEST BALANCE IF AGENT ONLINE
                                                            if (newAgent != null && newAgent.InvestorID > 0)
                                                            {
                                                                //Update balance of agent
                                                                double BalanceAgent = newAgent.Balance + agentCommission;
                                                                TradingServer.Facade.FacadeUpdateBalance(newAgent.InvestorID, BalanceAgent);

                                                                //search in investor online if agent online then send message get balance of agent
                                                                if (Business.Market.InvestorList != null)
                                                                {
                                                                    int countInvestorOnline = Business.Market.InvestorList.Count;
                                                                    for (int m = 0; m < countInvestorOnline; m++)
                                                                    {
                                                                        if (Business.Market.InvestorList[m].InvestorID == newAgent.InvestorID)
                                                                        {
                                                                            if (Business.Market.InvestorList[m].IsOnline)
                                                                            {
                                                                                Business.Market.InvestorList[m].Balance += agentCommission;
                                                                                string messageAgent = "GetNewBalance";

                                                                                Business.Market.InvestorList[m].ClientCommandQueue.Add(messageAgent);

                                                                                //SEND NOTIFY TO MANAGER
                                                                                TradingServer.Facade.FacadeSendNotifyManagerRequest(3, Business.Market.InvestorList[m]);
                                                                            }

                                                                            break;
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                            #endregion
                                                            #endregion

                                                            this.CommandList[i].Type = Command.Type;

                                                            //CALCULATION MARGIN FOR COMMAND PENDING ORDER
                                                            this.CommandList[i].CalculatorMarginCommand(this.CommandList[i]);

                                                            //SET MARGIN FOR COMMAND
                                                            Command.Margin = this.CommandList[i].Margin;

                                                            #region FIND COMMAND IN COMMAND EXECUTOR AND UPDATE TYPE
                                                            if (Business.Market.CommandExecutor != null)
                                                            {
                                                                int countExe = Business.Market.CommandExecutor.Count;
                                                                for (int m = 0; m < countExe; m++)
                                                                {
                                                                    if (Business.Market.CommandExecutor[m].ID == Command.ID)
                                                                    {
                                                                        Business.Market.CommandExecutor[m].Type = Command.Type;
                                                                        Business.Market.CommandExecutor[m].Commission = Command.Commission;
                                                                        Business.Market.CommandExecutor[m].AgentCommission = agentCommission;
                                                                        Business.Market.CommandExecutor[m].Margin = Command.Margin;
                                                                        Business.Market.CommandExecutor[m].OpenTime = Command.OpenTime;
                                                                        Business.Market.CommandExecutor[m].OpenPrice = Command.OpenPrice;
                                                                        Business.Market.CommandExecutor[m].IsActivePending = false;
                                                                        Business.Market.CommandExecutor[m].IsStopLossAndTakeProfit = false;

                                                                        break;
                                                                    }
                                                                }
                                                            }
                                                            #endregion

                                                            #region UPDATE COMMAND TYPE IN SYMBOL LIST
                                                            if (this.CommandList[i].Symbol != null && this.CommandList[i].Symbol.CommandList != null)
                                                            {
                                                                int countCommand = this.CommandList[i].Symbol.CommandList.Count;
                                                                for (int j = 0; j < countCommand; j++)
                                                                {
                                                                    if (this.CommandList[i].Symbol.CommandList[j].ID == Command.ID)
                                                                    {
                                                                        this.CommandList[i].Symbol.CommandList[j].Type = Command.Type;
                                                                        this.CommandList[i].Symbol.CommandList[j].Commission = Command.Commission;
                                                                        this.CommandList[i].Symbol.CommandList[j].AgentCommission = agentCommission;
                                                                        this.CommandList[i].Symbol.CommandList[j].Margin = Command.Margin;
                                                                        this.CommandList[i].Symbol.CommandList[j].OpenTime = Command.OpenTime;
                                                                        this.CommandList[i].Symbol.CommandList[j].OpenPrice = Command.OpenPrice;
                                                                        this.CommandList[i].Symbol.CommandList[j].IsActivePending = false;
                                                                        this.CommandList[i].Symbol.CommandList[j].IsStopLossAndTakeProfit = false;

                                                                        break;
                                                                    }
                                                                }
                                                            }
                                                            #endregion

                                                            //Update Command Type In Database
                                                            TradingServer.Facade.FacadeUpdateOpenTrade(Command);

                                                            #region Map Command Server To Client
                                                            if (this.ClientCommandQueue == null)
                                                                this.ClientCommandQueue = new List<string>();

                                                            string Message = "UpdatePendingOrder$True,Update Pending Order Command To Spot Command," + this.CommandList[i].ID + "," +
                                                                this.CommandList[i].Investor.InvestorID + "," + this.CommandList[i].Symbol.Name + "," + this.CommandList[i].Size + "," +
                                                                IsBuy + "," + this.CommandList[i].OpenTime + "," + this.CommandList[i].OpenPrice + "," + this.CommandList[i].StopLoss + "," +
                                                                this.CommandList[i].TakeProfit + "," + Command.ClosePrice + "," + Command.Commission + "," + Command.Swap + "," +
                                                                Command.Profit + "," + "Comment," + Command.ID + "," + "Open" + "," + 1 + "," + this.CommandList[i].ExpTime + "," +
                                                                this.CommandList[i].ClientCode + "," + this.CommandList[i].CommandCode + "," + this.CommandList[i].IsHedged + "," +
                                                                this.CommandList[i].Type.ID + "," + this.CommandList[i].Margin + ",UpdatePendingOrder";

                                                            //int countInvestor = this.CountInvestorOnline(this.InvestorID);
                                                            //if (countInvestor > 0)
                                                            this.ClientCommandQueue.Add(Message);

                                                            //SEND NOTIFY TO AGENT SERVER
                                                            Message += "," + this.CommandList[i].AgentRefConfig;
                                                            Business.AgentNotify newAgentNotify = new AgentNotify();
                                                            newAgentNotify.NotifyMessage = Message;
                                                            TradingServer.Agent.AgentConfig.Instance.AddNotifyToAgent(newAgentNotify, this.CommandList[i].Investor.InvestorGroupInstance);
                                                            #endregion

                                                            #region CALCULATION TOTAL MARGIN
                                                            if (this.CommandList.Count > 0)
                                                            {
                                                                Business.Margin newMargin = new Margin();
                                                                newMargin = this.CommandList[0].Symbol.CalculationTotalMargin(this.CommandList);
                                                                this.Margin = newMargin.TotalMargin;
                                                                this.FreezeMargin = newMargin.TotalFreezeMargin;
                                                            }
                                                            else
                                                            {
                                                                this.Margin = 0;
                                                            }
                                                            #endregion

                                                            //SENT COMMAND REMOVE COMMAND TO MANAGER
                                                            TradingServer.Facade.FacadeSendNoticeManagerRequest(2, this.CommandList[i]);

                                                            //SENT NOTIFY ADD COMMAND TO MANAGER
                                                            TradingServer.Facade.FacadeSendNoticeManagerRequest(1, this.CommandList[i]);

                                                            //SEND NOTIFY TO MANAGE CHANGE ACCOUNT
                                                            TradingServer.Facade.FacadeSendNotifyManagerRequest(3, this);

                                                            //SEND COMMAND TO BROKER SERVER(CLOSE COMMAND)
                                                            //try
                                                            //{
                                                            //    string cmdBroker = Command.CommandCode + "¬" + Command.Size + "¬" + Command.Symbol.Name + "¬" +
                                                            //        Command.Investor.Code;

                                                            //    TradingServer.Business.Market.clientBroker.NotifyBroker(cmdBroker);
                                                            //}
                                                            //catch (Exception ex)
                                                            //{

                                                            //}

                                                            this.CommandList[i].IsProcess = false;

                                                            //UPDATE DATABASE
                                                            this.CommandList[i].UpdateIsActivePending(false, false, this.CommandList[i].ID, this.InvestorID);

                                                            #region LOG ACTIVATE PENDING ORDER
                                                            if (this.CommandList[i].Symbol != null)
                                                            {
                                                                string content = string.Empty;
                                                                string mode = string.Empty;
                                                                string size = string.Empty;
                                                                string openPrice = string.Empty;
                                                                string bid = string.Empty;
                                                                string ask = string.Empty;

                                                                bid = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(this.CommandList[i].Symbol.TickValue.Bid.ToString(), this.CommandList[i].Symbol.Digit);
                                                                ask = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(this.CommandList[i].Symbol.TickValue.Ask.ToString(), this.CommandList[i].Symbol.Digit);

                                                                TradingServer.Facade.FacadeConvertTypeIDToName(this.CommandList[i].Type.ID);
                                                                size = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(this.CommandList[i].Size.ToString(), 2);
                                                                openPrice = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(this.CommandList[i].OpenPrice.ToString(), this.CommandList[i].Symbol.Digit);
                                                                content = "order #" + this.CommandList[i].CommandCode + " " + mode + " " + size + " " + this.CommandList[i].Symbol.Name + " is opened at " +
                                                                    openPrice + " (" + bid + "/" + ask + ")";

                                                                TradingServer.Facade.FacadeAddNewSystemLog(5, content, "[pending order triggered]", "", this.Code);
                                                            }
                                                            else
                                                            {
                                                                TradingServer.Facade.FacadeAddNewSystemLog(5, "pending order triggerd [symbol empty]", "[pending order triggered]", "", this.Code);
                                                            }
                                                            #endregion
                                                        }
                                                        else
                                                        {
                                                            #region CLOSE PENDING ORDER IF CHECK ACCOUNT INVALID
                                                            //SET PROPERTY FOR CLOSE COMMAND
                                                            Command.CloseTime = DateTime.Now;
                                                            Command.Comment = "Cancel pending order #" + Command.CommandCode + " [not enough money]";
                                                            Command.Type = this.CommandList[i].Type;

                                                            //ADD PENDING ORDER TO DATABASE
                                                            TradingServer.Facade.FacadeAddNewCommandHistory(Command.Investor.InvestorID, Command.Type.ID, Command.CommandCode,
                                                                Command.OpenTime, Command.OpenPrice, Command.CloseTime, Command.ClosePrice, 0, 0, 0, Command.ExpTime, Command.Size, Command.StopLoss,
                                                                Command.TakeProfit, Command.ClientCode, Command.Symbol.SymbolID, Command.Taxes, Command.AgentCommission,
                                                                Command.Comment, "5", Command.TotalSwap,
                                                                Command.RefCommandID, Command.AgentRefConfig, Command.IsActivePending, Command.IsStopLossAndTakeProfit);

                                                            #region COMMENT CODE(REMOVE COMMAND IN COMMAND EXECUTOR AND SYMBOL LIST
                                                            ////Remove Command In Symbol List
                                                            //bool resultRemoveCommandSymbol = TradingServer.Facade.FacadeRemoveOpenTradeInCommandList(Command.ID);

                                                            ////Remove Command In Command Executor
                                                            //bool deleteCommandExe = TradingServer.Facade.FacadeRemoveOpenTradeInCommandExecutor(Command.ID);
                                                            #endregion

                                                            //NEW SOLUTION ADD COMMAND TO REMOVE LIST
                                                            //Business.OpenTrade newOpenTrade = this.CommandList[i];
                                                            Business.OpenRemove newOpenRemove = new OpenRemove();
                                                            newOpenRemove.InvestorID = this.InvestorID;
                                                            newOpenRemove.OpenTradeID = this.CommandList[i].ID;
                                                            newOpenRemove.SymbolName = this.CommandList[i].Symbol.Name;
                                                            newOpenRemove.IsExecutor = true;
                                                            newOpenRemove.IsSymbol = true;
                                                            newOpenRemove.IsInvestor = false;
                                                            Business.Market.AddCommandToRemoveList(newOpenRemove);

                                                            //Delete Command In Database
                                                            TradingServer.Facade.FacadeDeleteOpenTradeByID(Command.ID);

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

                                                            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," +
                                                                DateTime.Now;

                                                            this.ClientCommandQueue.Add(Message);
                                                            #endregion

                                                            #region LOG ACTIVATE PENDING ORDER
                                                            if (this.CommandList[i].Symbol != null)
                                                            {
                                                                string content = string.Empty;
                                                                string mode = string.Empty;
                                                                string size = string.Empty;
                                                                string openPrice = string.Empty;
                                                                string bid = string.Empty;
                                                                string ask = string.Empty;

                                                                bid = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(this.CommandList[i].Symbol.TickValue.Bid.ToString(), this.CommandList[i].Symbol.Digit);
                                                                ask = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(this.CommandList[i].Symbol.TickValue.Ask.ToString(), this.CommandList[i].Symbol.Digit);

                                                                TradingServer.Facade.FacadeConvertTypeIDToName(this.CommandList[i].Type.ID);
                                                                size = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(this.CommandList[i].Size.ToString(), 2);
                                                                openPrice = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(this.CommandList[i].OpenPrice.ToString(), this.CommandList[i].Symbol.Digit);
                                                                content = "order #" + this.CommandList[i].CommandCode + " " + mode + " " + size + " " + this.CommandList[i].Symbol.Name + " is opened at " +
                                                                    openPrice + " (" + bid + "/" + ask + ")" + " [unsuccessful - not enough money]";

                                                                TradingServer.Facade.FacadeAddNewSystemLog(5, content, "[unsuccessful - not enough money]", "", this.Code);
                                                            }
                                                            else
                                                            {
                                                                TradingServer.Facade.FacadeAddNewSystemLog(5, "pending order triggered [symbol empty]", "[unsuccessful - not enough money]", "", this.Code);
                                                            }
                                                            #endregion

                                                            //SENT NOTIFY TO MANAGER
                                                            TradingServer.Facade.FacadeSendNoticeManagerRequest(2, this.CommandList[i]);

                                                            lock (Business.Market.syncObject)
                                                            {
                                                                this.CommandList.Remove(this.CommandList[i]);
                                                            }
                                                        }
                                                    }
                                                }
                                                #endregion
                                            }

                                            #region UPDATE STOP LOSS AND TAKE PROFIT
                                            //Update Database If Command Is Update StopLoss Or Update Take Profit
                                            if (this.CommandList[i].StopLoss != Command.StopLoss ||
                                                this.CommandList[i].TakeProfit != Command.TakeProfit ||
                                                this.CommandList[i].OpenPrice != Command.OpenPrice)
                                            {
                                                //TradingServer.Facade.FacadeUpdateOnlineCommandWithTakeProfit(Command.TakeProfit, Command.StopLoss, Command.ID);

                                                this.CommandList[i].TakeProfit = Command.TakeProfit;
                                                this.CommandList[i].StopLoss = Command.StopLoss;

                                                bool checkIsPending = TradingServer.Model.TradingCalculate.Instance.CheckIsPendingPosition(this.CommandList[i].Type.ID);
                                                //if (this.CommandList[i].Type.ID == 7 || this.CommandList[i].Type.ID == 8 ||
                                                //    this.CommandList[i].Type.ID == 9 || this.CommandList[i].Type.ID == 10)
                                                if(checkIsPending)
                                                {
                                                    if (this.CommandList[i].OpenPrice != Command.OpenPrice)
                                                    {
                                                        this.CommandList[i].OpenPrice = Command.OpenPrice;
                                                    }
                                                }

                                                //If Client Update Command Then Add Message To Client Message
                                                if (this.ClientCommandQueue == null)
                                                    this.ClientCommandQueue = new List<string>();

                                                //SEND NOTIFY TO MANAGER
                                                TradingServer.Facade.FacadeSendNoticeManagerRequest(1, this.CommandList[i]);
                                            }
                                            #endregion

                                            #endregion
                                        }

                                        FlagCommand = true;
                                        break;
                                    }
                                }
                                #endregion
                            }
                        }
                        else
                        {
                            #region Init Command List Of Investor
                            this.CommandList = new List<OpenTrade>();
                            this.CommandList.Add(Command);
                            #endregion
                        }
                        #endregion
                    }
                    #endregion

                    //Call Function ReCalculation Account Of Investor
                    this.ReCalculationAccount();

                    DateTime timeStop = DateTime.Now;
                    TimeSpan timeProcess = timeStop - timeStart;

                    if (this.IsMonitor)
                    {
                        StringBuilder monitor = new StringBuilder();
                        monitor.Append("Time Process " + timeProcess.TotalMilliseconds + " millisecond <=> Command Code: " + Command.CommandCode + "<=>");
                        monitor.Append("Balance: " + this.Balance + "<=> Margin Level: " + Math.Round(this.MarginLevel,2) + "<=> Margin: " + this.Margin);

                        if (this.ListMonitor == null)
                            this.ListMonitor = new List<string>();

                        this.ListMonitor.Insert(0, monitor.ToString());

                        if (this.ListMonitor.Count > 10)
                        {
                            this.ListMonitor.RemoveAt(10);
                        }
                    }

                    Command = null;
                }
                catch (Exception ex)
                {

                }
                #endregion
            }

            this.isInTask = false;
            Investor.IsInProcess = false;
        }
        /// <summary>
        /// EVENT WEEK CLOSE TRADE SYMBOL
        /// </summary>
        internal void CloseTradeSymbol(string TargetName, Business.TimeEvent timeEvent)
        {
            return;

            if (Business.Market.SymbolList != null)
            {
                int count = Business.Market.SymbolList.Count;
                for (int i = 0; i < count; i++)
                {
                    if (Business.Market.SymbolList[i].Name == TargetName)
                    {
                        //=============CLOSE TRADE SESSION=========================
                        if (Business.Market.SymbolList[i].IsTrade)
                        {
                            Business.Market.SymbolList[i].IsTrade = false;

                            //TradingServer.Facade.FacadeAddNewSystemLog(6, "Close Trade Symbol " + TargetName + " is: " +
                            //                                            Business.Market.SymbolList[i].IsTrade,
                            //                                            "[Check Event Close Trade]", "", "");

                            //SEND NOTIFY TO CLIENT
                            string content = "OTS53427640$" + TargetName + "{" + Business.Market.SymbolList[i].IsTrade;
                            TradingServer.Business.Market.SendNotifyToClient(content, 2, 0);
                        }

                        //==============FREEZE MARGIN============================
                        #region FIND SETTING ORDER OF SYMBOL
                        string MethodOrders = string.Empty;
                        if (Business.Market.SymbolList[i].ParameterItems != null)
                        {
                            int countParameter = Business.Market.SymbolList[i].ParameterItems.Count;
                            for (int n = 0; n < countParameter; n++)
                            {
                                if (Business.Market.SymbolList[i].ParameterItems[n].Code == "S012")
                                {
                                    MethodOrders = Business.Market.SymbolList[i].ParameterItems[n].StringValue;
                                    break;
                                }
                            }
                        }
                        #endregion

                        #region PROCESS GOOD TILL CANCEL
                        if (Business.Market.SymbolList[i].CommandList != null)
                        {
                            for (int j = 0; j < Business.Market.SymbolList[i].CommandList.Count; j++)
                            {
                                bool flagUpdateSLTP = false;
                                switch (MethodOrders)
                                {
                                    #region GOOD TILL TODAY INCLUDING SL/TP
                                    case "Good till today including SL/TP":
                                        {
                                            bool isPending = TradingServer.Model.TradingCalculate.Instance.CheckIsPendingPosition(Business.Market.SymbolList[i].CommandList[j].Type.ID);
                                            if (!isPending)
                                            {
                                                #region RESET STOP LOSS AND TAKE PROFIT OF ONLINE COMMAND
                                                if (Business.Market.SymbolList[i].CommandList[j].StopLoss > 0 ||
                                                  Business.Market.SymbolList[i].CommandList[j].TakeProfit > 0)
                                                {
                                                    Business.Market.SymbolList[i].CommandList[j].StopLoss = 0;
                                                    Business.Market.SymbolList[i].CommandList[j].TakeProfit = 0;
                                                    flagUpdateSLTP = true;
                                                }
                                                #endregion

                                                if (flagUpdateSLTP)
                                                {
                                                    #region UPDATE STOP LOST AND TAKE PROFIT OF COMMAND IN COMMAND EXECUTOR
                                                    //UPDATE STOP LOST AND TAKE PROFIT OF COMMAND IN COMMAND EXECUTOR
                                                    if (Business.Market.CommandExecutor != null && Business.Market.CommandExecutor.Count > 0)
                                                    {
                                                        for (int n = 0; n < Business.Market.CommandExecutor.Count; n++)
                                                        {
                                                            if (Business.Market.CommandExecutor[n].ID == Business.Market.SymbolList[i].CommandList[j].ID)
                                                            {
                                                                if (Business.Market.CommandExecutor[n].StopLoss > 0 || Business.Market.CommandExecutor[n].TakeProfit > 0)
                                                                {
                                                                    Business.Market.CommandExecutor[n].StopLoss = 0;
                                                                    Business.Market.CommandExecutor[n].TakeProfit = 0;
                                                                }
                                                                break;
                                                            }
                                                        }
                                                    }
                                                    #endregion

                                                    #region UPDATE STOP LOSS AND TAKE PROFIT OF COMMAND IN INVESTOR COMMAND
                                                    if (Business.Market.InvestorList != null)
                                                    {
                                                        for (int n = 0; n < Business.Market.InvestorList.Count; n++)
                                                        {
                                                            if (Business.Market.InvestorList[n].InvestorCommand == Business.Market.SymbolList[i].CommandList[j].Investor.InvestorCommand)
                                                            {
                                                                if (Business.Market.InvestorList[n].CommandList != null)
                                                                {
                                                                    for (int m = 0; m < Business.Market.InvestorList[n].CommandList.Count; m++)
                                                                    {
                                                                        if (Business.Market.InvestorList[n].CommandList[m].ID == Business.Market.SymbolList[i].CommandList[j].ID)
                                                                        {
                                                                            if (Business.Market.InvestorList[n].CommandList[m].StopLoss > 0 || Business.Market.InvestorList[n].CommandList[m].TakeProfit > 0)
                                                                            {
                                                                                Business.Market.InvestorList[n].CommandList[m].StopLoss = 0;
                                                                                Business.Market.InvestorList[n].CommandList[m].TakeProfit = 0;

                                                                                break;
                                                                            }
                                                                        }
                                                                    }
                                                                }

                                                                break;
                                                            }
                                                        }
                                                    }
                                                    #endregion

                                                    //SENT NOTIFY TO CLIENT
                                                    string message = "STO8546";
                                                    Business.Market.SymbolList[i].CommandList[j].Investor.ClientCommandQueue.Add(message);

                                                    //SEND NOTIFY TO CLIENT UPDATE STOP LOSS AND TAKE PROFIT
                                                    TradingServer.Facade.FacadeSendNoticeManagerRequest(1, Business.Market.SymbolList[i].CommandList[j]);

                                                    //UPDATE STOP LOST AND TAKE PROFIT OF COMMAND IN DATABASE
                                                    TradingServer.Facade.FacadeUpdateOnlineCommandWithTakeProfit(0, 0, Business.Market.SymbolList[i].CommandList[j].ID,
                                                        Business.Market.SymbolList[i].CommandList[j].Comment, Business.Market.SymbolList[i].CommandList[j].OpenPrice);
                                                }
                                            }
                                            else
                                            {
                                                #region REMOVE PENDING ORDER OF INVESTOR
                                                Business.OpenRemove newOpenRemove = new OpenRemove();
                                                newOpenRemove.InvestorID = Business.Market.SymbolList[i].CommandList[j].Investor.InvestorID;
                                                newOpenRemove.IsExecutor = true;
                                                newOpenRemove.IsInvestor = true;
                                                newOpenRemove.IsSymbol = false;
                                                newOpenRemove.OpenTradeID = Business.Market.SymbolList[i].CommandList[j].ID;
                                                newOpenRemove.SymbolName = Business.Market.SymbolList[i].Name;
                                                Business.Market.AddCommandToRemoveList(newOpenRemove);

                                                //DELETE COMMAND IN DATABASE
                                                bool deleteCommand = TradingServer.Facade.FacadeDeleteOpenTradeByID(Business.Market.SymbolList[i].CommandList[j].ID);

                                                //INSERT DATABASE THEN CANCEL PENDING ORDER
                                                TradingServer.Facade.FacadeAddNewCommandHistory(Business.Market.SymbolList[i].CommandList[j].Investor.InvestorID,
                                                    Business.Market.SymbolList[i].CommandList[j].Type.ID, Business.Market.SymbolList[i].CommandList[j].CommandCode,
                                                    Business.Market.SymbolList[i].CommandList[j].OpenTime, Business.Market.SymbolList[i].CommandList[j].OpenPrice,
                                                    DateTime.Now, 0, 0, 0, 0, Business.Market.SymbolList[i].CommandList[j].ExpTime, Business.Market.SymbolList[i].CommandList[j].Size,
                                                    Business.Market.SymbolList[i].CommandList[j].StopLoss, Business.Market.SymbolList[i].CommandList[j].TakeProfit,
                                                    Business.Market.SymbolList[i].CommandList[j].ClientCode, Business.Market.SymbolList[i].SymbolID,
                                                    Business.Market.SymbolList[i].CommandList[j].Taxes, 0, Business.Market.SymbolList[i].CommandList[j].Comment, "8",
                                                    Business.Market.SymbolList[i].CommandList[j].TotalSwap,
                                                    Business.Market.SymbolList[i].CommandList[j].RefCommandID,
                                                    Business.Market.SymbolList[i].CommandList[j].AgentRefConfig,
                                                    Business.Market.SymbolList[i].CommandList[j].IsActivePending,
                                                    Business.Market.SymbolList[i].CommandList[j].IsStopLossAndTakeProfit);

                                                //SENT NOTIFY TO CLIENT
                                                string message = "STO8546";
                                                if (Business.Market.SymbolList[i].CommandList[j].Investor.ClientCommandQueue == null)
                                                    Business.Market.SymbolList[i].CommandList[j].Investor.ClientCommandQueue = new List<string>();

                                                Business.Market.SymbolList[i].CommandList[j].Investor.ClientCommandQueue.Add(message);

                                                //NOTIFY TO MANAGER DELETE PENDING ORDER COMMAND
                                                TradingServer.Facade.FacadeSendNoticeManagerRequest(2, Business.Market.SymbolList[i].CommandList[j]);

                                                //'1205': close sell stop order #00275799 sell stop 0.10 XAUUSD at 1401.30 sl: 0.00 tp: 0.00 (1410.40/1410.90) exp 6/7/2013 10:48:46 AM
                                                //'1205': delete order #00275799 sell stop 0.10XAUUSD at 1401.30
                                                string strLog = "'System': close " +
                                                    Business.Market.SymbolList[i].CommandList[j].Type.Name + " order #" + Business.Market.SymbolList[i].CommandList[j].CommandCode +
                                                    " " + Business.Market.SymbolList[i].CommandList[j].Type.Name + " " +
                                                    Model.TradingCalculate.Instance.BuildStringWithDigit(Business.Market.SymbolList[i].CommandList[j].Size.ToString(), 2) + " " +
                                                    Business.Market.SymbolList[i].CommandList[j].Symbol.Name + " at " +
                                                    Model.TradingCalculate.Instance.BuildStringWithDigit(Business.Market.SymbolList[i].CommandList[j].OpenPrice.ToString(), Business.Market.SymbolList[i].CommandList[j].Symbol.Digit) +
                                                    " sl: " + Model.TradingCalculate.Instance.BuildStringWithDigit(Business.Market.SymbolList[i].CommandList[j].StopLoss.ToString(), 2) +
                                                    " tp: " + Model.TradingCalculate.Instance.BuildStringWithDigit(Business.Market.SymbolList[i].CommandList[j].TakeProfit.ToString(), 2) +
                                                    " (" + Business.Market.SymbolList[i].CommandList[j].Symbol.TickValue.Bid + "/" + Business.Market.SymbolList[i].CommandList[j].Symbol.TickValue.Ask + ")" +
                                                    " [Good till today including SL/TP]";

                                                TradingServer.Facade.FacadeAddNewSystemLog(3, strLog, "[Good till today including SL/TP]", "", "");

                                                string stContent = "'System': delete order #" + Business.Market.SymbolList[i].CommandList[j].CommandCode +
                                                    " " + Business.Market.SymbolList[i].CommandList[j].Type.Name + " " +
                                                    Model.TradingCalculate.Instance.BuildStringWithDigit(Business.Market.SymbolList[i].CommandList[j].Size.ToString(), 2) +
                                                    " " + Business.Market.SymbolList[i].CommandList[j].Symbol.Name + " at " +
                                                    Model.TradingCalculate.Instance.BuildStringWithDigit(Business.Market.SymbolList[i].CommandList[j].OpenPrice.ToString(), Business.Market.SymbolList[i].CommandList[j].Symbol.Digit) +
                                                    " [Good till today including SL/TP]";

                                                TradingServer.Facade.FacadeAddNewSystemLog(3, stContent, "[Good till today including SL/TP]", "", "");

                                                //REMOVE COMMAND IN SYMBOL LIST
                                                Business.Market.SymbolList[i].CommandList.RemoveAt(j);

                                                j--;
                                                #endregion
                                            }
                                        }
                                        break;
                                    #endregion

                                    #region GOOL TILL TODAY EXCLUDING SL/TP
                                    case "Good till today excluding SL/TP":
                                        {
                                            bool isPending = TradingServer.Model.TradingCalculate.Instance.CheckIsPendingPosition(Business.Market.SymbolList[i].CommandList[j].Type.ID);
                                            if (isPending)
                                            {
                                                #region REMOVE PENDING ORDER OF INVESTOR
                                                Business.OpenRemove newOpenRemove = new OpenRemove();
                                                newOpenRemove.InvestorID = Business.Market.SymbolList[i].CommandList[j].Investor.InvestorID;
                                                newOpenRemove.IsExecutor = true;
                                                newOpenRemove.IsInvestor = true;
                                                newOpenRemove.IsSymbol = false;
                                                newOpenRemove.OpenTradeID = Business.Market.SymbolList[i].CommandList[j].ID;
                                                newOpenRemove.SymbolName = Business.Market.SymbolList[i].Name;
                                                Business.Market.AddCommandToRemoveList(newOpenRemove);

                                                //DELETE COMMAND IN DATABASE
                                                TradingServer.Facade.FacadeDeleteOpenTradeByID(Business.Market.SymbolList[i].CommandList[j].ID);

                                                //INSERT DATABASE THEN CANCEL PENDING ORDER
                                                TradingServer.Facade.FacadeAddNewCommandHistory(Business.Market.SymbolList[i].CommandList[j].Investor.InvestorID,
                                                    Business.Market.SymbolList[i].CommandList[j].Type.ID, Business.Market.SymbolList[i].CommandList[j].CommandCode,
                                                    Business.Market.SymbolList[i].CommandList[j].OpenTime, Business.Market.SymbolList[i].CommandList[j].OpenPrice,
                                                    DateTime.Now, 0, 0, 0, 0, Business.Market.SymbolList[i].CommandList[j].ExpTime, Business.Market.SymbolList[i].CommandList[j].Size,
                                                    Business.Market.SymbolList[i].CommandList[j].StopLoss, Business.Market.SymbolList[i].CommandList[j].TakeProfit,
                                                    Business.Market.SymbolList[i].CommandList[j].ClientCode, Business.Market.SymbolList[i].SymbolID,
                                                    Business.Market.SymbolList[i].CommandList[j].Taxes, 0, Business.Market.SymbolList[i].CommandList[j].Comment, "11",
                                                    Business.Market.SymbolList[i].CommandList[j].TotalSwap,
                                                    Business.Market.SymbolList[i].CommandList[j].RefCommandID,
                                                    Business.Market.SymbolList[i].CommandList[j].AgentRefConfig,
                                                    Business.Market.SymbolList[i].CommandList[j].IsActivePending,
                                                    Business.Market.SymbolList[i].CommandList[j].IsStopLossAndTakeProfit);

                                                //SENT NOTIFY TO CLIENT
                                                string message = "STO8546";
                                                if (Business.Market.SymbolList[i].CommandList[j].Investor.ClientCommandQueue == null)
                                                    Business.Market.SymbolList[i].CommandList[j].Investor.ClientCommandQueue = new List<string>();

                                                Business.Market.SymbolList[i].CommandList[j].Investor.ClientCommandQueue.Add(message);

                                                //SEND NOTIFY DELETE PENDING ORDER TO MANAGER
                                                TradingServer.Facade.FacadeSendNoticeManagerRequest(2, Business.Market.SymbolList[i].CommandList[j]);

                                                //'1205': close sell stop order #00275799 sell stop 0.10 XAUUSD at 1401.30 sl: 0.00 tp: 0.00 (1410.40/1410.90) exp 6/7/2013 10:48:46 AM
                                                //'1205': delete order #00275799 sell stop 0.10XAUUSD at 1401.30
                                                string strLog = "'System': close " +
                                                    Business.Market.SymbolList[i].CommandList[j].Type.Name + " order #" + Business.Market.SymbolList[i].CommandList[j].CommandCode +
                                                    " " + Business.Market.SymbolList[i].CommandList[j].Type.Name + " " +
                                                    Model.TradingCalculate.Instance.BuildStringWithDigit(Business.Market.SymbolList[i].CommandList[j].Size.ToString(), 2) + " " +
                                                    Business.Market.SymbolList[i].CommandList[j].Symbol.Name + " at " +
                                                    Model.TradingCalculate.Instance.BuildStringWithDigit(Business.Market.SymbolList[i].CommandList[j].OpenPrice.ToString(), Business.Market.SymbolList[i].CommandList[j].Symbol.Digit) +
                                                    " sl: " + Model.TradingCalculate.Instance.BuildStringWithDigit(Business.Market.SymbolList[i].CommandList[j].StopLoss.ToString(), 2) +
                                                    " tp: " + Model.TradingCalculate.Instance.BuildStringWithDigit(Business.Market.SymbolList[i].CommandList[j].TakeProfit.ToString(), 2) +
                                                    " (" + Business.Market.SymbolList[i].CommandList[j].Symbol.TickValue.Bid + "/" + Business.Market.SymbolList[i].CommandList[j].Symbol.TickValue.Ask + ")" +
                                                    " [Good till today including SL/TP]";

                                                TradingServer.Facade.FacadeAddNewSystemLog(3, strLog, "[Good till today including SL/TP]", "", "");

                                                string stContent = "'System': delete order #" + Business.Market.SymbolList[i].CommandList[j].CommandCode +
                                                    " " + Business.Market.SymbolList[i].CommandList[j].Type.Name + " " +
                                                    Model.TradingCalculate.Instance.BuildStringWithDigit(Business.Market.SymbolList[i].CommandList[j].Size.ToString(), 2) +
                                                    " " + Business.Market.SymbolList[i].CommandList[j].Symbol.Name + " at " +
                                                    Model.TradingCalculate.Instance.BuildStringWithDigit(Business.Market.SymbolList[i].CommandList[j].OpenPrice.ToString(), Business.Market.SymbolList[i].CommandList[j].Symbol.Digit) +
                                                    " [Good till today including SL/TP]";

                                                TradingServer.Facade.FacadeAddNewSystemLog(3, stContent, "[Good till today including SL/TP]", "", "");

                                                //REMOVE COMMAND IN SYMBOL LIST
                                                Business.Market.SymbolList[i].CommandList.RemoveAt(j);

                                                j--;

                                                #endregion
                                            }
                                        }
                                        break;
                                    #endregion
                                }
                            }
                        }
                        #endregion                        

                        if (!Business.Market.SymbolList[i].IsEnableFreezeMargin)
                            return;

                        List<Business.Investor> listTempInvestor = new List<Investor>();

                        #region CHANGE VALUE MARGIN AND SET FREEZE MARGIN
                        if (Business.Market.SymbolList[i].CommandList != null)
                        {                            
                            int countCommand = Business.Market.SymbolList[i].CommandList.Count;
                            for (int j = 0; j < countCommand; j++)
                            {
                                bool flagInvestor = false;

                                Business.Market.SymbolList[i].CommandList[j].Symbol.UseFreezeMargin = true;
                                //Business.Market.SymbolList[i].CommandList[j].Margin = Business.Market.SymbolList[i].CommandList[j].CalculationFreezeMarginCommand(Business.Market.SymbolList[i].CommandList[j]);
                                Business.Market.SymbolList[i].CommandList[j].CalculatorMarginCommand(Business.Market.SymbolList[i].CommandList[j]);

                                if (listTempInvestor != null)
                                {
                                    int countInvestor = listTempInvestor.Count;
                                    for (int n = 0; n < countInvestor; n++)
                                    {
                                        if (listTempInvestor[n].Code == Business.Market.SymbolList[i].CommandList[j].Investor.Code)
                                        {
                                            flagInvestor = true;
                                            break;
                                        }
                                    }
                                }

                                if (!flagInvestor)
                                {
                                    listTempInvestor.Add(Business.Market.SymbolList[i].CommandList[j].Investor);
                                }
                            }
                        }
                        #endregion     

                        #region SEND COMMAND TO INVESTOR ONLINE
                        if (listTempInvestor != null)
                        {
                            int countInvestor = listTempInvestor.Count;
                            for (int j = 0; j < countInvestor; j++)
                            {
                                if (listTempInvestor[j].CommandList != null && listTempInvestor[j].CommandList.Count > 0)
                                {
                                    int countCommand = listTempInvestor[j].CommandList.Count;
                                    for (int n = 0; n < countCommand; n++)
                                    {
                                        if (listTempInvestor[j].CommandList[n].Symbol.Name.Trim().ToUpper() == TargetName.Trim().ToUpper())
                                        {
                                            listTempInvestor[j].CommandList[n].Symbol.UseFreezeMargin = true;
                                            //listTempInvestor[j].CommandList[n].Margin = listTempInvestor[j].CommandList[n].CalculationFreezeMarginCommand(listTempInvestor[j].CommandList[n]);
                                            listTempInvestor[j].CommandList[n].CalculatorMarginCommand(listTempInvestor[j].CommandList[n]);
                                        }
                                    }
                                }

                                #region RECALCULATION ACCOUNT OF INVESTOR
                                //Recalculation total margin of investor
                                if (listTempInvestor[j].CommandList.Count > 0)
                                {
                                    Business.Margin newMargin = new Margin();
                                    newMargin = listTempInvestor[j].CommandList[0].Symbol.CalculationTotalMargin(listTempInvestor[j].CommandList);
                                    listTempInvestor[j].Margin = newMargin.TotalMargin;
                                    listTempInvestor[j].FreezeMargin = newMargin.TotalFreezeMargin;
                                }
                                else
                                {
                                    listTempInvestor[j].Margin = 0;
                                    listTempInvestor[j].FreezeMargin = 0;
                                }
                                #endregion                                

                                //SEND COMMAND TO AGENT SERVER
                                Business.AgentNotify newAgentNotify = new AgentNotify();
                                newAgentNotify.NotifyMessage = "UpdateInvestorAccount$" + listTempInvestor[j].InvestorID;
                                TradingServer.Agent.AgentConfig.Instance.AddNotifyToAgent(newAgentNotify, listTempInvestor[j].InvestorGroupInstance);

                                //SEND NOTIFY TO MANAGE RESET ACCOUNT
                                TradingServer.Facade.FacadeSendNotifyManagerRequest(3, listTempInvestor[j]);

                                #region SEND COMMAND TO INVESTOR IF INVESTOR ONLINE
                                if (listTempInvestor[j].IsOnline)
                                {
                                    string message = "FRZM14785213";
                                    if (listTempInvestor[j].ClientCommandQueue == null)
                                        listTempInvestor[j].ClientCommandQueue = new List<string>();

                                        listTempInvestor[j].ClientCommandQueue.Add(message);
                                }
                                #endregion                                
                            }
                        }
                        #endregion                        

                        break;
                    }
                }
            }
        }
        /// <summary>
        /// EVENT DAY CHECK SETTING ORDER IN ADMIN
        /// </summary>
        /// <param name="TargetName"></param>
        internal void ProcessSettingOrder(string TargetName, Business.TimeEvent timeEvent)
        {
            return;
            if (Business.Market.SymbolList != null)
            {
                int count = Business.Market.SymbolList.Count;
                for (int i = 0; i < Business.Market.SymbolList.Count; i++)
                {
                    #region Find Setting Order Of Symbol
                    string MethodOrders = string.Empty;
                    if (Business.Market.SymbolList[i].ParameterItems != null)
                    {
                        int countParameter = Business.Market.SymbolList[i].ParameterItems.Count;
                        for (int n = 0; n < countParameter; n++)
                        {
                            if (Business.Market.SymbolList[i].ParameterItems[n].Code == "S012")
                            {
                                MethodOrders = Business.Market.SymbolList[i].ParameterItems[n].StringValue;
                                break;
                            }
                        }
                    }
                    #endregion

                    #region FIND COMMAND IN SYMBOL LIST
                    if (Business.Market.SymbolList[i].CommandList != null && Business.Market.SymbolList[i].CommandList.Count > 0)
                    {                     
                        for (int j = 0; j < Business.Market.SymbolList[i].CommandList.Count; j++)
                        {
                            bool flagResetSLTP = false;
                            switch (MethodOrders)
                            {
                                #region GOOD TILL TODAY INCLUDING SL/TP
                                case "Good till today including SL/TP":
                                    {
                                        bool isPending = TradingServer.Model.TradingCalculate.Instance.CheckIsPendingPosition(Business.Market.SymbolList[i].CommandList[j].Type.ID);
                                        if (!isPending)
                                        {
                                            string content = string.Empty;
                                            string strBeforeStopLoss = string.Empty;
                                            string strBeforeTakeProfit = string.Empty;
                                            string strSize = string.Empty;

                                            #region RESET STOP LOSS AND TAKE PROFIT OF ONLINE COMMAND
                                            if (Business.Market.SymbolList[i].CommandList[j].StopLoss > 0 ||
                                              Business.Market.SymbolList[i].CommandList[j].TakeProfit > 0)
                                            {
                                                strBeforeStopLoss = Business.Market.SymbolList[i].CommandList[j].StopLoss.ToString();
                                                strBeforeTakeProfit = Business.Market.SymbolList[i].CommandList[j].TakeProfit.ToString();
                                                strSize = Business.Market.SymbolList[i].CommandList[j].Size.ToString();

                                                Business.Market.SymbolList[i].CommandList[j].StopLoss = 0;
                                                Business.Market.SymbolList[i].CommandList[j].TakeProfit = 0;
                                                flagResetSLTP = true;
                                            }
                                            #endregion

                                            if (flagResetSLTP)
                                            {
                                                #region UPDATE STOP LOST AND TAKE PROFIT OF COMMAND IN COMMAND EXECUTOR
                                                //UPDATE STOP LOST AND TAKE PROFIT OF COMMAND IN COMMAND EXECUTOR
                                                if (Business.Market.CommandExecutor != null && Business.Market.CommandExecutor.Count > 0)
                                                {
                                                    for (int n = 0; n < Business.Market.CommandExecutor.Count; n++)
                                                    {
                                                        if (Business.Market.CommandExecutor[n].ID == Business.Market.SymbolList[i].CommandList[j].ID)
                                                        {
                                                            if (Business.Market.CommandExecutor[n].StopLoss > 0 || Business.Market.CommandExecutor[n].TakeProfit > 0)
                                                            {
                                                                Business.Market.CommandExecutor[n].StopLoss = 0;
                                                                Business.Market.CommandExecutor[n].TakeProfit = 0;
                                                            }

                                                            break;
                                                        }
                                                    }
                                                }
                                                #endregion

                                                #region UPDATE STOP LOSS AND TAKE PROFIT OF COMMAND IN INVESTOR LIST
                                                //if (Business.Market.SymbolList[i].CommandList[j].Investor.CommandList != null)
                                                //{
                                                //    int countInvestorCommand = Business.Market.SymbolList[i].CommandList[j].Investor.CommandList.Count;
                                                //    for (int n = 0; n < countInvestorCommand; n++)
                                                //    {
                                                //        if (Business.Market.SymbolList[i].CommandList[j].Investor.CommandList[n].ID == Business.Market.SymbolList[i].CommandList[j].ID)
                                                //        {
                                                //            if (Business.Market.SymbolList[i].CommandList[j].Investor.CommandList[n].StopLoss > 0 ||
                                                //                Business.Market.SymbolList[i].CommandList[j].Investor.CommandList[n].TakeProfit > 0)
                                                //            {
                                                //                Business.Market.SymbolList[i].CommandList[j].Investor.CommandList[n].StopLoss = 0;
                                                //                Business.Market.SymbolList[i].CommandList[j].Investor.CommandList[n].TakeProfit = 0;
                                                //            }

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

                                                if (Business.Market.InvestorList != null)
                                                {
                                                    for (int n = 0; n < Business.Market.InvestorList.Count; n++)
                                                    {
                                                        if (Business.Market.InvestorList[n].InvestorID == Business.Market.SymbolList[i].CommandList[j].Investor.InvestorID)
                                                        {
                                                            if (Business.Market.InvestorList[n].CommandList != null)
                                                            {
                                                                for (int m = 0; m < Business.Market.InvestorList[n].CommandList.Count; m++)
                                                                {
                                                                    if (Business.Market.InvestorList[n].CommandList[m].ID == Business.Market.SymbolList[i].CommandList[j].ID)
                                                                    {
                                                                        if (Business.Market.InvestorList[n].CommandList[m].StopLoss > 0 || Business.Market.InvestorList[n].CommandList[m].TakeProfit > 0)
                                                                        {
                                                                            Business.Market.InvestorList[n].CommandList[m].StopLoss = 0;
                                                                            Business.Market.InvestorList[n].CommandList[m].TakeProfit = 0;
                                                                        }

                                                                        break;
                                                                    }
                                                                }
                                                            }

                                                            break;
                                                        }
                                                    }
                                                }
                                                #endregion

                                                //SENT NOTIFY TO CLIENT
                                                string message = "STO8546";

                                                Business.Market.SymbolList[i].CommandList[j].Investor.ClientCommandQueue.Add(message);

                                                //SEND NOTIFY TO CLIENT UPDATE STOP LOSS AND TAKE PROFIT
                                                TradingServer.Facade.FacadeSendNoticeManagerRequest(1, Business.Market.SymbolList[i].CommandList[j]);

                                                //UPDATE STOP LOST AND TAKE PROFIT OF COMMAND IN DATABASE
                                                TradingServer.Facade.FacadeUpdateOnlineCommandWithTakeProfit(0, 0, Business.Market.SymbolList[i].CommandList[j].ID,
                                                    Business.Market.SymbolList[i].CommandList[j].Comment, Business.Market.SymbolList[i].CommandList[j].OpenPrice);

                                                //INSERT SYSTEM LOG

                                                //'00001140: calculation swap order #004432443 - current balance: 40000 - old swap: 5.00'
                                                content = "'System': reset order #" +
                                                    Business.Market.SymbolList[i].CommandList[j].CommandCode + " name: " + Business.Market.CommandExecutor[i].Symbol.Name +
                                                    " size: " + strSize + " s/l: " + strBeforeStopLoss + " t/p: " + strBeforeTakeProfit + " -> s/l: 0.00 t/p: 0.00 [Good till today including SL/TP]";

                                                TradingServer.Facade.FacadeAddNewSystemLog(5, content, "[Good till today including SL/TP]", "", "");
                                            }
                                        }
                                        else
                                        {
                                            string content = string.Empty;
                                            #region REMOVE PENDING ORDER OF INVESTOR

                                            #region COMMENT CODE(REMOVE COMMAND IN COMMAND EXECUTOR AND INVESTOR LIST
                                            ////REMOVE COMMAND IN COMMAND EXECUTOR
                                            //if (Business.Market.CommandExecutor != null)
                                            //{
                                            //    int countCommand = Business.Market.CommandExecutor.Count;
                                            //    for (int n = 0; n < countCommand; n++)
                                            //    {
                                            //        if (Business.Market.CommandExecutor[n].ID == Business.Market.SymbolList[i].CommandList[j].ID)
                                            //        {
                                            //            Business.Market.CommandExecutor.Remove(Business.Market.CommandExecutor[n]);
                                            //            break;
                                            //        }
                                            //    }
                                            //}

                                            ////DELETE COMMAND IN INVESTOR COMMAND LIST
                                            //TradingServer.Facade.FacadeRemoveOpenTradeInInvestorList(Business.Market.SymbolList[i].CommandList[j].ID,
                                            //    Business.Market.SymbolList[i].CommandList[j].Investor.InvestorID);
                                            #endregion                                            

                                            Business.OpenRemove newOpenRemove = new OpenRemove();
                                            newOpenRemove.InvestorID = Business.Market.SymbolList[i].CommandList[j].Investor.InvestorID;
                                            newOpenRemove.IsExecutor = true;
                                            newOpenRemove.IsInvestor = true;
                                            newOpenRemove.IsSymbol = false;
                                            newOpenRemove.OpenTradeID = Business.Market.SymbolList[i].CommandList[j].ID;
                                            newOpenRemove.SymbolName = Business.Market.SymbolList[i].Name;
                                            Business.Market.AddCommandToRemoveList(newOpenRemove);

                                            //DELETE COMMAND IN DATABASE
                                            TradingServer.Facade.FacadeDeleteOpenTradeByID(Business.Market.SymbolList[i].CommandList[j].ID);

                                            //INSERT DATABASE THEN CANCEL PENDING ORDER
                                            TradingServer.Facade.FacadeAddNewCommandHistory(Business.Market.SymbolList[i].CommandList[j].Investor.InvestorID,
                                                Business.Market.SymbolList[i].CommandList[j].Type.ID, Business.Market.SymbolList[i].CommandList[j].CommandCode,
                                                Business.Market.SymbolList[i].CommandList[j].OpenTime, Business.Market.SymbolList[i].CommandList[j].OpenPrice,
                                                DateTime.Now, 0, 0, 0, 0, Business.Market.SymbolList[i].CommandList[j].ExpTime, Business.Market.SymbolList[i].CommandList[j].Size,
                                                Business.Market.SymbolList[i].CommandList[j].StopLoss, Business.Market.SymbolList[i].CommandList[j].TakeProfit,
                                                Business.Market.SymbolList[i].CommandList[j].ClientCode, Business.Market.SymbolList[i].SymbolID,
                                                Business.Market.SymbolList[i].CommandList[j].Taxes, 0, Business.Market.SymbolList[i].CommandList[j].Comment, "8",
                                                Business.Market.SymbolList[i].CommandList[j].TotalSwap,
                                                Business.Market.SymbolList[i].CommandList[j].RefCommandID,
                                                Business.Market.SymbolList[i].CommandList[j].AgentRefConfig,
                                                Business.Market.SymbolList[i].CommandList[j].IsActivePending,
                                                Business.Market.SymbolList[i].CommandList[j].IsStopLossAndTakeProfit);

                                            //SENT NOTIFY TO CLIENT
                                            string message = "STO8546";
                                            if (Business.Market.SymbolList[i].CommandList[j].Investor.ClientCommandQueue == null)
                                                Business.Market.SymbolList[i].CommandList[j].Investor.ClientCommandQueue = new List<string>();

                                            Business.Market.SymbolList[i].CommandList[j].Investor.ClientCommandQueue.Add(message);

                                            //NOTIFY TO MANAGER DELETE PENDING ORDER COMMAND
                                            TradingServer.Facade.FacadeSendNoticeManagerRequest(2, Business.Market.SymbolList[i].CommandList[j]);

                                            //INSERT SYSTEM LOG
                                            //'1205': delete order #00275799 sell stop 0.10XAUUSD at 1401.30
                                            content = "'System': delete order #" + Business.Market.SymbolList[i].CommandList[j].CommandCode + " " +
                                                Business.Market.SymbolList[i].CommandList[j].Type.Name + " " + Model.TradingCalculate.Instance.BuildStringWithDigit(
                                                Business.Market.SymbolList[i].CommandList[j].Size.ToString(), 2) + " " + Business.Market.SymbolList[i].CommandList[j].Symbol.Name + " at " +
                                                Model.TradingCalculate.Instance.BuildStringWithDigit(Business.Market.SymbolList[i].CommandList[j].OpenPrice.ToString(),
                                                Business.Market.SymbolList[i].CommandList[j].Symbol.Digit) + " [Good till today including SL/TP]";

                                            TradingServer.Facade.FacadeAddNewSystemLog(5, content, "[Good till today including SL/TP]", "", "");

                                            //REMOVE COMMAND IN SYMBOL LIST
                                            Business.Market.SymbolList[i].CommandList.RemoveAt(j);

                                            j--;
                                            #endregion                                                                                    
                                        }
                                    }
                                    break;
                                #endregion

                                #region GOOL TILL TODAY EXCLUDING SL/TP
                                case "Good till today excluding SL/TP":
                                    {
                                        bool isPending = TradingServer.Model.TradingCalculate.Instance.CheckIsPendingPosition(Business.Market.SymbolList[i].CommandList[j].Type.ID);
                                        if (isPending)
                                        {
                                            string content = string.Empty;
                                            #region REMOVE PENDING ORDER OF INVESTOR

                                            #region COMMENT CODE(REMOVE COMMAND IN COMMAND EXECUTOR AND INVESTOR LIST
                                            ////REMOVE COMMAND IN COMMAND EXECUTOR
                                            //if (Business.Market.CommandExecutor != null)
                                            //{
                                            //    int countCommand = Business.Market.CommandExecutor.Count;
                                            //    for (int n = 0; n < countCommand; n++)
                                            //    {
                                            //        if (Business.Market.CommandExecutor[n].ID == Business.Market.SymbolList[i].CommandList[j].ID)
                                            //        {
                                            //            Business.Market.CommandExecutor.Remove(Business.Market.CommandExecutor[n]);
                                            //            break;
                                            //        }
                                            //    }
                                            //}

                                            ////bool deleteCommandExe = TradingServer.Facade.FacadeRemoveOpenTradeInCommandExecutor(Business.Market.SymbolList[i].CommandList[j].ID);

                                            ////DELETE COMMAND IN INVESTOR COMMAND LIST
                                            //TradingServer.Facade.FacadeRemoveOpenTradeInInvestorList(Business.Market.SymbolList[i].CommandList[j].ID,
                                            //    Business.Market.SymbolList[i].CommandList[j].Investor.InvestorID);
                                            #endregion
                                            
                                            Business.OpenRemove newOpenRemove = new OpenRemove();
                                            newOpenRemove.InvestorID = Business.Market.SymbolList[i].CommandList[j].Investor.InvestorID;
                                            newOpenRemove.IsExecutor = true;
                                            newOpenRemove.IsInvestor = true;
                                            newOpenRemove.IsSymbol = false;
                                            newOpenRemove.OpenTradeID = Business.Market.SymbolList[i].CommandList[j].ID;
                                            newOpenRemove.SymbolName = Business.Market.SymbolList[i].Name;
                                            Business.Market.AddCommandToRemoveList(newOpenRemove);

                                            //DELETE COMMAND IN DATABASE
                                            TradingServer.Facade.FacadeDeleteOpenTradeByID(Business.Market.SymbolList[i].CommandList[j].ID);

                                            //INSERT DATABASE THEN CANCEL PENDING ORDER
                                            TradingServer.Facade.FacadeAddNewCommandHistory(Business.Market.SymbolList[i].CommandList[j].Investor.InvestorID,
                                                Business.Market.SymbolList[i].CommandList[j].Type.ID, Business.Market.SymbolList[i].CommandList[j].CommandCode,
                                                Business.Market.SymbolList[i].CommandList[j].OpenTime, Business.Market.SymbolList[i].CommandList[j].OpenPrice,
                                                DateTime.Now, 0, 0, 0, 0, Business.Market.SymbolList[i].CommandList[j].ExpTime, Business.Market.SymbolList[i].CommandList[j].Size,
                                                Business.Market.SymbolList[i].CommandList[j].StopLoss, Business.Market.SymbolList[i].CommandList[j].TakeProfit,
                                                Business.Market.SymbolList[i].CommandList[j].ClientCode, Business.Market.SymbolList[i].SymbolID,
                                                Business.Market.SymbolList[i].CommandList[j].Taxes, 0, Business.Market.SymbolList[i].CommandList[j].Comment, "11",
                                                Business.Market.SymbolList[i].CommandList[j].TotalSwap,
                                                Business.Market.SymbolList[i].CommandList[j].RefCommandID,
                                                Business.Market.SymbolList[i].CommandList[j].AgentRefConfig,
                                                Business.Market.SymbolList[i].CommandList[j].IsActivePending,
                                                Business.Market.SymbolList[i].CommandList[j].IsStopLossAndTakeProfit);

                                            //SENT NOTIFY TO CLIENT
                                            string message = "STO8546";
                                            if (Business.Market.SymbolList[i].CommandList[j].Investor.ClientCommandQueue == null)
                                                Business.Market.SymbolList[i].CommandList[j].Investor.ClientCommandQueue = new List<string>();

                                            Business.Market.SymbolList[i].CommandList[j].Investor.ClientCommandQueue.Add(message);

                                            //SEND NOTIFY DELETE PENDING ORDER TO MANAGER
                                            TradingServer.Facade.FacadeSendNoticeManagerRequest(2, Business.Market.SymbolList[i].CommandList[j]);

                                            //INSERT SYSTEM LOG
                                            //'1205': delete order #00275799 sell stop 0.10XAUUSD at 1401.30
                                            content = "'System': delete order #" + Business.Market.SymbolList[i].CommandList[j].CommandCode + " " +
                                                Business.Market.SymbolList[i].CommandList[j].Type.Name + " " + Model.TradingCalculate.Instance.BuildStringWithDigit(
                                                Business.Market.SymbolList[i].CommandList[j].Size.ToString(), 2) + " " + Business.Market.SymbolList[i].CommandList[j].Symbol.Name + " at " +
                                                Model.TradingCalculate.Instance.BuildStringWithDigit(Business.Market.SymbolList[i].CommandList[j].OpenPrice.ToString(),
                                                Business.Market.SymbolList[i].CommandList[j].Symbol.Digit) + " [Good till today excluding SL/TP]";

                                            TradingServer.Facade.FacadeAddNewSystemLog(5, content, "[Good till today excluding SL/TP]", "", "");

                                            //REMOVE COMMAND IN SYMBOL LIST
                                            Business.Market.SymbolList[i].CommandList.RemoveAt(j);

                                            j--;
                                            #endregion                                            
                                        }
                                    }
                                    break;
                                #endregion
                            }
                        }
                    }
                    #endregion    
                }
            }                      
        }
        /// <summary>
        /// DELETE ONLINE COMMAND BY COMMAND ID IN CLASS MARKET AND DATABASE. IT CALL BY MANAGER
        /// </summary>
        /// <param name="CommandID"></param>
        /// <returns></returns>
        internal bool DeleteOpenTradeByRefCommandID(int refCommandID)
        {
            bool Result = false;

            #region Find Command In Investor List Of Class Market And Delete Command
            if (Business.Market.InvestorList != null)
            {
                bool Flag = false;
                int count = Business.Market.InvestorList.Count;
                for (int i = 0; i < Business.Market.InvestorList.Count; i++)
                {
                    if (Flag == true)
                        break;

                    if (Business.Market.InvestorList[i].CommandList != null && Business.Market.InvestorList[i].CommandList.Count > 0)
                    {
                        for (int j = 0; j < Business.Market.InvestorList[i].CommandList.Count; j++)
                        {
                            if (Business.Market.InvestorList[i].CommandList[j].RefCommandID == refCommandID)
                            {
                                Business.OpenTrade temp = Business.Market.InvestorList[i].CommandList[j];

                                bool isBuy = TradingServer.Facade.FacadeGetIsBuyByTypeID(temp.Type.ID);
                                int ResultHistory = -1;
                                temp.CloseTime = DateTime.Now;
                                temp.Profit = 0;

                                #region ADD COMMAND TO HISTORY
                                //CALL FUNCTION INSERT COMMAND TO HISTORY COMMAND WITH PROFIT = 0
                                ResultHistory = TradingServer.Facade.FacadeAddNewCommandHistory(temp.Investor.InvestorID,
                                    temp.Type.ID, temp.CommandCode,
                                    temp.OpenTime, temp.OpenPrice,
                                    temp.CloseTime, temp.ClosePrice,
                                    0/*Profit*/, 0/*SWAP*/,
                                    0/*COMMISSION*/, temp.ExpTime,
                                    temp.Size, temp.StopLoss,
                                    temp.TakeProfit, temp.ClientCode,
                                    temp.Symbol.SymbolID, temp.Taxes, 0,
                                    temp.Comment, "6", 0/*TOTAL SWAP*/,
                                    temp.RefCommandID,
                                    temp.AgentRefConfig,
                                    temp.IsActivePending,
                                    temp.IsStopLossAndTakeProfit);
                                #endregion

                                //CALL FUNCTION DELETE COMMAND IN ONLINE COMMAND
                                TradingServer.Facade.FacadeDeleteOpenTradeByID(temp.ID);

                                if (ResultHistory > 0)
                                {
                                    #region MAP COMMAND SEND TO CLIENT
                                    StringBuilder Message = new StringBuilder();
                                    Message.Append("CloseCommandByManager$True,CLOSE COMMAND COMMPLETE,");
                                    Message.Append(temp.ID);
                                    Message.Append(",");
                                    Message.Append(temp.Investor.InvestorID);
                                    Message.Append(",");
                                    Message.Append(temp.Symbol.Name);
                                    Message.Append(",");
                                    Message.Append(temp.Size);
                                    Message.Append(",");
                                    Message.Append(isBuy);
                                    Message.Append(",");
                                    Message.Append(temp.OpenTime);
                                    Message.Append(",");
                                    Message.Append(temp.OpenPrice);
                                    Message.Append(",");
                                    Message.Append(temp.StopLoss);
                                    Message.Append(",");
                                    Message.Append(temp.TakeProfit);
                                    Message.Append(",");
                                    Message.Append(temp.ClosePrice);
                                    Message.Append(",");
                                    Message.Append("0,0,0,Comment,");
                                    Message.Append(temp.ID);
                                    Message.Append(",");
                                    Message.Append(temp.Type.Name);
                                    Message.Append(",");
                                    Message.Append(1);
                                    Message.Append(",");
                                    Message.Append(temp.ExpTime);
                                    Message.Append(",");
                                    Message.Append(temp.ClientCode);
                                    Message.Append(",");
                                    Message.Append(temp.CommandCode);
                                    Message.Append(",");
                                    Message.Append(temp.IsHedged);
                                    Message.Append(",");
                                    Message.Append(temp.Type.ID);
                                    Message.Append(",");
                                    Message.Append(temp.Margin);
                                    Message.Append(",Close,");
                                    Message.Append(temp.CloseTime);
                                    #endregion

                                    if (Business.Market.InvestorList[i].ClientCommandQueue == null)
                                        Business.Market.InvestorList[i].ClientCommandQueue = new List<string>();

                                    Business.Market.InvestorList[i].ClientCommandQueue.Add(Message.ToString());

                                    #region COMMENT CODE(REMOVE COMMAND IN COMMAND EXECUTOR AND SYMBOL LIST
                                    ////CALL FUNCTION REMOVE COMMAND IN SYMBOL LIST OF CLASS MAKRET
                                    //bool resultRemoveCommnadSymbol = TradingServer.Facade.FacadeRemoveOpenTradeInCommandList(CommandID);
                                    #endregion

                                    //NEW SOLUTION ADD COMMAND TO REMOVE LIST
                                    //Business.OpenTrade newOpenTrade = Business.Market.InvestorList[i].CommandList[j];
                                    Business.OpenRemove newOpenRemove = new OpenRemove();
                                    newOpenRemove.InvestorID = Business.Market.InvestorList[i].InvestorID;
                                    newOpenRemove.OpenTradeID = Business.Market.InvestorList[i].CommandList[j].ID;
                                    newOpenRemove.SymbolName = temp.Symbol.Name;
                                    newOpenRemove.IsExecutor = true;
                                    newOpenRemove.IsSymbol = true;
                                    newOpenRemove.IsInvestor = false;
                                    Business.Market.AddCommandToRemoveList(newOpenRemove);

                                    //SEND NOTIFY TO MANAGER
                                    TradingServer.Facade.FacadeSendNoticeManagerRequest(2, Business.Market.InvestorList[i].CommandList[j]);

                                    //SEND NOTIFY CHANGE ACCOUNT
                                    TradingServer.Facade.FacadeSendNotifyManagerRequest(3, Business.Market.InvestorList[i]);

                                    #region BLOCK OBJECT COMMAND IN INVESTOR LIST
                                    lock (Business.Market.syncObject)
                                    {
                                        //REMOVE COMMAND IN INVESTOR COMMAND LIST OF CLASS MARKET
                                        Business.Market.InvestorList[i].CommandList.RemoveAt(j);
                                    }
                                    #endregion

                                    if (Business.Market.InvestorList[i].CommandList.Count > 0)
                                    {
                                        //RECALCULATION TOTAL MARGIN OF ACCOUNT
                                        //Business.Market.InvestorList[i].ReCalculationTotalMargin();
                                        Business.Margin newMargin = new Margin();
                                        newMargin = Business.Market.InvestorList[i].CommandList[0].Symbol.CalculationTotalMargin(Business.Market.InvestorList[i].CommandList);
                                        Business.Market.InvestorList[i].Margin = newMargin.TotalMargin;
                                        Business.Market.InvestorList[i].FreezeMargin = newMargin.TotalFreezeMargin;
                                    }
                                    else
                                    {
                                        Business.Market.InvestorList[i].Margin = 0;
                                        Business.Market.InvestorList[i].FreezeMargin = 0;
                                        Business.Market.InvestorList[i].Profit = 0;
                                        Business.Market.InvestorList[i].FreeMargin = 0;
                                    }

                                    Result = true;
                                }

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

            return Result;
        }
        /// <summary>
        /// DELETE ONLINE COMMAND BY COMMAND ID IN CLASS MARKET AND DATABASE. IT CALL BY MANAGER
        /// </summary>
        /// <param name="CommandID"></param>
        /// <returns></returns>
        internal bool DeleteOpenTradeByMT4(int refCommandID)
        {
            bool Result = false;

            #region Find Command In Investor List Of Class Market And Delete Command
            if (Business.Market.InvestorList != null)
            {
                bool Flag = false;
                int count = Business.Market.InvestorList.Count;
                for (int i = 0; i < Business.Market.InvestorList.Count; i++)
                {
                    if (Flag == true)
                        break;

                    if (Business.Market.InvestorList[i].CommandList != null && Business.Market.InvestorList[i].CommandList.Count > 0)
                    {
                        for (int j = 0; j < Business.Market.InvestorList[i].CommandList.Count; j++)
                        {
                            if (Business.Market.InvestorList[i].CommandList[j].RefCommandID == refCommandID)
                            {
                                Business.OpenTrade temp = Business.Market.InvestorList[i].CommandList[j];

                                bool isBuy = TradingServer.Facade.FacadeGetIsBuyByTypeID(temp.Type.ID);

                                temp.CloseTime = DateTime.Now;
                                temp.Profit = 0;

                                #region MAP COMMAND SEND TO CLIENT
                                StringBuilder Message = new StringBuilder();
                                Message.Append("CloseCommandByManager$True,CLOSE COMMAND COMMPLETE,");
                                Message.Append(temp.ID);
                                Message.Append(",");
                                Message.Append(temp.Investor.InvestorID);
                                Message.Append(",");
                                Message.Append(temp.Symbol.Name);
                                Message.Append(",");
                                Message.Append(temp.Size);
                                Message.Append(",");
                                Message.Append(isBuy);
                                Message.Append(",");
                                Message.Append(temp.OpenTime);
                                Message.Append(",");
                                Message.Append(temp.OpenPrice);
                                Message.Append(",");
                                Message.Append(temp.StopLoss);
                                Message.Append(",");
                                Message.Append(temp.TakeProfit);
                                Message.Append(",");
                                Message.Append(temp.ClosePrice);
                                Message.Append(",");
                                Message.Append("0,0,0,Comment,");
                                Message.Append(temp.ID);
                                Message.Append(",");
                                Message.Append(temp.Type.Name);
                                Message.Append(",");
                                Message.Append(1);
                                Message.Append(",");
                                Message.Append(temp.ExpTime);
                                Message.Append(",");
                                Message.Append(temp.ClientCode);
                                Message.Append(",");
                                Message.Append(temp.CommandCode);
                                Message.Append(",");
                                Message.Append(temp.IsHedged);
                                Message.Append(",");
                                Message.Append(temp.Type.ID);
                                Message.Append(",");
                                Message.Append(temp.Margin);
                                Message.Append(",Close,");
                                Message.Append(temp.CloseTime);
                                #endregion

                                if (Business.Market.InvestorList[i].ClientCommandQueue == null)
                                    Business.Market.InvestorList[i].ClientCommandQueue = new List<string>();

                                Business.Market.InvestorList[i].ClientCommandQueue.Add(Message.ToString());

                                //NEW SOLUTION ADD COMMAND TO REMOVE LIST
                                //Business.OpenTrade newOpenTrade = Business.Market.InvestorList[i].CommandList[j];
                                Business.OpenRemove newOpenRemove = new OpenRemove();
                                newOpenRemove.InvestorID = Business.Market.InvestorList[i].InvestorID;
                                newOpenRemove.OpenTradeID = Business.Market.InvestorList[i].CommandList[j].ID;
                                newOpenRemove.SymbolName = temp.Symbol.Name;
                                newOpenRemove.IsExecutor = true;
                                newOpenRemove.IsSymbol = true;
                                newOpenRemove.IsInvestor = false;
                                Business.Market.AddCommandToRemoveList(newOpenRemove);

                                #region BLOCK OBJECT COMMAND IN INVESTOR LIST
                                lock (Business.Market.syncObject)
                                {
                                    //REMOVE COMMAND IN INVESTOR COMMAND LIST OF CLASS MARKET
                                    Business.Market.InvestorList[i].CommandList.RemoveAt(j);
                                }
                                #endregion

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

            return Result;
        }
        /// <summary>
        /// DELETE ONLINE COMMAND BY COMMAND ID IN CLASS MARKET AND DATABASE. IT CALL BY MANAGER
        /// </summary>
        /// <param name="CommandID"></param>
        /// <returns></returns>
        internal bool DeleteOpenTradeByCommandID(int CommandID)
        {
            bool Result = false;

            #region FIND COMMAND IN COMMAND EXECUTOR
            //if (Business.Market.CommandExecutor != null)
            //{
            //    int count = Business.Market.CommandExecutor.Count;
            //    for (int i = 0; i < Business.Market.CommandExecutor.Count; i++)
            //    {
            //        if (Business.Market.CommandExecutor[i].ID == CommandID)
            //        {
            //            Business.Market.CommandExecutor.RemoveAt(i);
            //            break;
            //        }
            //    }
            //}
            #endregion

            #region Find Command In Investor List Of Class Market And Delete Command
            if (Business.Market.InvestorList != null)
            {
                bool Flag = false;
                int count = Business.Market.InvestorList.Count;
                for (int i = 0; i < Business.Market.InvestorList.Count; i++)
                {
                    if (Flag == true)
                        break;

                    if (Business.Market.InvestorList[i].CommandList != null && Business.Market.InvestorList[i].CommandList.Count > 0)
                    {
                        int countCommand = Business.Market.InvestorList[i].CommandList.Count;
                        for (int j = 0; j < Business.Market.InvestorList[i].CommandList.Count; j++)
                        {
                            if (Business.Market.InvestorList[i].CommandList[j].ID == CommandID)
                            {
                                bool isBuy = TradingServer.Facade.FacadeGetIsBuyByTypeID(Business.Market.InvestorList[i].CommandList[j].Type.ID);
                                int ResultHistory = -1;
                                Business.Market.InvestorList[i].CommandList[j].CloseTime = DateTime.Now;
                                Business.Market.InvestorList[i].CommandList[j].Profit = 0;

                                #region ADD COMMAND TO HISTORY
                                //CALL FUNCTION INSERT COMMAND TO HISTORY COMMAND WITH PROFIT = 0
                                ResultHistory = TradingServer.Facade.FacadeAddNewCommandHistory(Business.Market.InvestorList[i].CommandList[j].Investor.InvestorID,
                                    Business.Market.InvestorList[i].CommandList[j].Type.ID, Business.Market.InvestorList[i].CommandList[j].CommandCode,
                                    Business.Market.InvestorList[i].CommandList[j].OpenTime, Business.Market.InvestorList[i].CommandList[j].OpenPrice,
                                    Business.Market.InvestorList[i].CommandList[j].CloseTime, Business.Market.InvestorList[i].CommandList[j].ClosePrice,
                                    0/*Profit*/, 0/*SWAP*/,
                                    0/*COMMISSION*/, Business.Market.InvestorList[i].CommandList[j].ExpTime,
                                    Business.Market.InvestorList[i].CommandList[j].Size, Business.Market.InvestorList[i].CommandList[j].StopLoss,
                                    Business.Market.InvestorList[i].CommandList[j].TakeProfit, Business.Market.InvestorList[i].CommandList[j].ClientCode,
                                    Business.Market.InvestorList[i].CommandList[j].Symbol.SymbolID, Business.Market.InvestorList[i].CommandList[j].Taxes, 0,
                                    Business.Market.InvestorList[i].CommandList[j].Comment, "6", 0/*TOTAL SWAP*/,
                                    Business.Market.InvestorList[i].CommandList[j].RefCommandID,
                                    Business.Market.InvestorList[i].CommandList[j].AgentRefConfig,
                                    Business.Market.InvestorList[i].CommandList[j].IsActivePending,
                                    Business.Market.InvestorList[i].CommandList[j].IsStopLossAndTakeProfit);
                                #endregion

                                //CALL FUNCTION DELETE COMMAND IN ONLINE COMMAND
                                TradingServer.Facade.FacadeDeleteOpenTradeByID(Business.Market.InvestorList[i].CommandList[j].ID);

                                if (ResultHistory > 0)
                                {
                                    #region MAP COMMAND SEND TO CLIENT
                                    string Message = "CloseCommandByManager$True,CLOSE COMMAND COMMPLETE," + Business.Market.InvestorList[i].CommandList[j].ID + "," +
                                        Business.Market.InvestorList[i].CommandList[j].Investor.InvestorID + "," +
                                         Business.Market.InvestorList[i].CommandList[j].Symbol.Name + "," +
                                         Business.Market.InvestorList[i].CommandList[j].Size + "," + isBuy + "," +
                                         Business.Market.InvestorList[i].CommandList[j].OpenTime + "," +
                                         Business.Market.InvestorList[i].CommandList[j].OpenPrice + "," +
                                         Business.Market.InvestorList[i].CommandList[j].StopLoss + "," +
                                         Business.Market.InvestorList[i].CommandList[j].TakeProfit + "," +
                                         Business.Market.InvestorList[i].CommandList[j].ClosePrice + "," +
                                         "0," +
                                         "0," +
                                         "0," + "Comment," +
                                         Business.Market.InvestorList[i].CommandList[j].ID + "," +
                                         Business.Market.InvestorList[i].CommandList[j].Type.Name + "," + 1 + "," +
                                         Business.Market.InvestorList[i].CommandList[j].ExpTime + "," +
                                         Business.Market.InvestorList[i].CommandList[j].ClientCode + "," +
                                         Business.Market.InvestorList[i].CommandList[j].CommandCode + "," +
                                         Business.Market.InvestorList[i].CommandList[j].IsHedged + "," +
                                         Business.Market.InvestorList[i].CommandList[j].Type.ID + "," +
                                         Business.Market.InvestorList[i].CommandList[j].Margin + ",Close," +
                                         Business.Market.InvestorList[i].CommandList[j].CloseTime;
                                    #endregion

                                    if (Business.Market.InvestorList[i].ClientCommandQueue == null)
                                        Business.Market.InvestorList[i].ClientCommandQueue = new List<string>();

                                    Business.Market.InvestorList[i].ClientCommandQueue.Add(Message);

                                    string notifyAgent = "DeleteOrderByManager$True,CLOSE COMMAND COMMPLETE," + Business.Market.InvestorList[i].CommandList[j].ID + "," +
                                        Business.Market.InvestorList[i].CommandList[j].Investor.InvestorID + "," +
                                         Business.Market.InvestorList[i].CommandList[j].Symbol.Name + "," +
                                         Business.Market.InvestorList[i].CommandList[j].Size + "," + isBuy + "," +
                                         Business.Market.InvestorList[i].CommandList[j].OpenTime + "," +
                                         Business.Market.InvestorList[i].CommandList[j].OpenPrice + "," +
                                         Business.Market.InvestorList[i].CommandList[j].StopLoss + "," +
                                         Business.Market.InvestorList[i].CommandList[j].TakeProfit + "," +
                                         Business.Market.InvestorList[i].CommandList[j].ClosePrice + "," +
                                         "0," +
                                         "0," +
                                         "0," + "Comment," +
                                         Business.Market.InvestorList[i].CommandList[j].ID + "," +
                                         Business.Market.InvestorList[i].CommandList[j].Type.Name + "," + 1 + "," +
                                         Business.Market.InvestorList[i].CommandList[j].ExpTime + "," +
                                         Business.Market.InvestorList[i].CommandList[j].ClientCode + "," +
                                         Business.Market.InvestorList[i].CommandList[j].CommandCode + "," +
                                         Business.Market.InvestorList[i].CommandList[j].IsHedged + "," +
                                         Business.Market.InvestorList[i].CommandList[j].Type.ID + "," +
                                         Business.Market.InvestorList[i].CommandList[j].Margin + ",Close," +
                                         Business.Market.InvestorList[i].CommandList[j].CloseTime;

                                    Business.AgentNotify newAgentNotify = new AgentNotify();
                                    newAgentNotify.NotifyMessage = notifyAgent;
                                    TradingServer.Agent.AgentConfig.Instance.AddNotifyToAgent(newAgentNotify, Business.Market.InvestorList[i].InvestorGroupInstance);

                                    #region COMMENT CODE(REMOVE COMMAND IN COMMAND EXECUTOR AND SYMBOL LIST
                                    ////CALL FUNCTION REMOVE COMMAND IN SYMBOL LIST OF CLASS MAKRET
                                    //bool resultRemoveCommnadSymbol = TradingServer.Facade.FacadeRemoveOpenTradeInCommandList(CommandID);
                                    #endregion

                                    //NEW SOLUTION ADD COMMAND TO REMOVE LIST
                                    //Business.OpenTrade newOpenTrade = Business.Market.InvestorList[i].CommandList[j];
                                    Business.OpenRemove newOpenRemove = new OpenRemove();
                                    newOpenRemove.InvestorID = Business.Market.InvestorList[i].InvestorID;
                                    newOpenRemove.OpenTradeID = Business.Market.InvestorList[i].CommandList[j].ID;
                                    newOpenRemove.SymbolName = Business.Market.InvestorList[i].CommandList[j].Symbol.Name;
                                    newOpenRemove.IsExecutor = true;
                                    newOpenRemove.IsSymbol = true;
                                    newOpenRemove.IsInvestor = false;
                                    Business.Market.AddCommandToRemoveList(newOpenRemove);

                                    //SEND NOTIFY TO MANAGER
                                    TradingServer.Facade.FacadeSendNoticeManagerRequest(2, Business.Market.InvestorList[i].CommandList[j]);

                                    //SEND NOTIFY CHANGE ACCOUNT
                                    TradingServer.Facade.FacadeSendNotifyManagerRequest(3, Business.Market.InvestorList[i]);

                                    #region BLOCK OBJECT COMMAND IN INVESTOR LIST
                                    lock (Business.Market.syncObject)
                                    {
                                        //REMOVE COMMAND IN INVESTOR COMMAND LIST OF CLASS MARKET
                                        Business.Market.InvestorList[i].CommandList.RemoveAt(j);
                                    }
                                    #endregion

                                    if (Business.Market.InvestorList[i].CommandList.Count > 0)
                                    {
                                        //RECALCULATION TOTAL MARGIN OF ACCOUNT
                                        //Business.Market.InvestorList[i].ReCalculationTotalMargin();
                                        Business.Margin newMargin = new Margin();
                                        newMargin = Business.Market.InvestorList[i].CommandList[0].Symbol.CalculationTotalMargin(Business.Market.InvestorList[i].CommandList);
                                        Business.Market.InvestorList[i].Margin = newMargin.TotalMargin;
                                        Business.Market.InvestorList[i].FreezeMargin = newMargin.TotalFreezeMargin;
                                    }
                                    else
                                    {
                                        Business.Market.InvestorList[i].Margin = 0;
                                        Business.Market.InvestorList[i].FreezeMargin = 0;
                                        Business.Market.InvestorList[i].Profit = 0;
                                        Business.Market.InvestorList[i].FreeMargin = 0;
                                    }

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

            #region FIND COMMAND IN SYMBOL LIST AND DELETE
            //if (Business.Market.SymbolList != null)
            //{
            //    bool flag = false;
            //    int count = Business.Market.SymbolList.Count;
            //    for (int i = 0; i < Business.Market.SymbolList.Count; i++)
            //    {
            //        if (flag)
            //            break;

            //        if (Business.Market.SymbolList[i].CommandList != null)
            //        {
            //            int countCommand = Business.Market.SymbolList[i].CommandList.Count;
            //            for (int j = 0; j < Business.Market.SymbolList[i].CommandList.Count; j++)
            //            {
            //                if (Business.Market.SymbolList[i].CommandList[j].ID == CommandID)
            //                {
            //                    Business.Market.SymbolList[i].CommandList.RemoveAt(j);
            //                    flag = true;

            //                    break;
            //                }
            //            }
            //        }
            //    }
            //}
            #endregion

            return Result;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private static Business.OpenRemove GetOpenTradeRemove()
        {
            Business.OpenRemove result = new OpenRemove();
            if (Business.Market.RemoveCommandList != null && Business.Market.RemoveCommandList.Count > 0)
            {
                if (Business.Market.RemoveCommandList[0] != null)
                {
                    result = Business.Market.RemoveCommandList[0];
                    Business.Market.RemoveCommandList.Remove(Business.Market.RemoveCommandList[0]);
                }
                else
                {
                    Business.Market.RemoveCommandList.Remove(Business.Market.RemoveCommandList[0]);
                }
            }

            return result;
        }
        /// <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
            }
        }