/// <summary>
        /// 
        /// </summary>
        /// <param name="investorID"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        internal bool ChangePrimaryPassword(int investorID, string oldPwd, string password)
        {
            bool result = false;
            string hash = TradingServer.Model.ValidateCheck.Encrypt(password);
            //VERIFY OLD PASSWORD
            string hashOldPwd = TradingServer.Model.ValidateCheck.Encrypt(oldPwd);
            bool checkMasterPwd = TradingServer.Facade.FacadeCheckMasterPassword(investorID, hashOldPwd);

            if (checkMasterPwd)
            {
                if (Business.Market.IsConnectMT4)
                {
                    #region CONNECT WITH MT4
                    //get investor code
                    Business.Investor newInvestor = TradingServer.Facade.FacadeGetInvestorByInvestorID(investorID);
                    if (newInvestor != null && newInvestor.InvestorID > 0)
                    {
                        string cmd = BuildCommandElement5ConnectMT4.Mode.BuildCommand.Instance.ConvertResetPasswordToString(newInvestor.Code, oldPwd, password);

                        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)
                                {
                                    if (Business.Market.InvestorList[i].AllowChangePwd)
                                    {
                                        //string resultMT4 = Business.Market.InstanceSocket.SendToMT4(Business.Market.DEFAULT_IPADDRESS, Business.Market.DEFAULT_PORT, cmd);
                                        string resultMT4 = Element5SocketConnectMT4.Business.SocketConnect.Instance.SendSocket(cmd);
                                        //string resultMT4 = LibraryAPI.CPPOut.Command(cmd);
                                        //string resultMT4 = Business.Market.InstanceGlobalDelegate.SendCommand(cmd);

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

                                        if (result)
                                        {
                                            Business.Market.InvestorList[i].PrimaryPwd = hash;

                                            //UPDATE PRIMARY PASSWORD IN DATABASE
                                            TradingServer.Facade.FacadeUpdatePasswordByCode(Business.Market.InvestorList[i].Code, hash);
                                            result = true;
                                        }

                                        break;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                }
                            }
                        }
                    }
                    #endregion
                }
                else
                {
                    #region NO CONNECT WITH MT4
                    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)
                            {
                                if (Business.Market.InvestorList[i].AllowChangePwd)
                                {
                                    Business.Market.InvestorList[i].PrimaryPwd = hash;
                                    result = true;
                                    break;
                                }
                                else
                                {
                                    return false;
                                }
                            }
                        }
                    }
                    #endregion
                }

                if (result)
                {
                    result = Investor.DBWInvestorInstance.UpdatePrimaryPasword(investorID, hash);

                    //SEND NOTIFY TO AGENT SYSTEM
                    //SEND COMMAND TO AGENT SERVER
                    string Message = "UpdatePrimaryPassword$" + result + "{" + hash + "{" + investorID;
                    Business.AgentNotify newAgentNotify = new AgentNotify();
                    newAgentNotify.NotifyMessage = Message;
                    TradingServer.Agent.AgentConfig.Instance.AddNotifyToAgent(newAgentNotify);
                }
            }
            else
            {
                return false;
            }

            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>
        /// 
        /// </summary>
        /// <param name="Money"></param>
        /// <param name="InvestorID"></param>
        /// <returns></returns>
        internal bool AddCredit(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 creditBefore = Business.Market.InvestorList[i].Credit;
                        Credit = Business.Market.InvestorList[i].Credit + Money;

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

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

                            //Call Function Calculation Account
                            Business.Market.InvestorList[i].ReCalculationAccount();

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

                            string Message = "AddCredit$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 = "ACD01";

                            //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 ADD CREDIT
                            TradingServer.Facade.FacadeSendNotifyManagerRequest(3, Business.Market.InvestorList[i]);

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

                        string content = 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);
                        content = "'" + Business.Market.InvestorList[i].Code + "': credit " + strBalance + " credit in " + strMoney + " -> " + strCreditAfter;
                        TradingServer.Facade.FacadeAddNewSystemLog(3, content, "[credit in account]", "", Business.Market.InvestorList[i].Code);

                        break;
                    }
                }
            }

            return Result;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Money"></param>
        /// <param name="InvestorID"></param>
        /// <returns></returns>
        internal bool Deposit(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)
                    {
                        //Calculation Balance
                        double Balance = 0;
                        double balanceBefore = Business.Market.InvestorList[i].Balance;
                        Balance = Business.Market.InvestorList[i].Balance + Money;

                        //Update Balance In Database
                        bool UpdateBalance = false;
                        UpdateBalance = TradingServer.Facade.FacadeUpdateBalance(InvestorID, Balance);

                        Business.Market.InvestorList[i].TotalDeposit += Money;

                        if (UpdateBalance == false)
                        {
                            return false;
                        }
                        else
                        {
                            //Update Balance In Market
                            Business.Market.InvestorList[i].Balance = Balance;

                            //Call Function Calculation All Account Of Investor
                            Business.Market.InvestorList[i].ReCalculationAccount();

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

                            string Message = "AddDeposit$True,Add Deposit 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);

                            Result = true;

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

                        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 = "ADP01";

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

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

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

                        //ADD HISTORY DEPOSIT TO TABLE COMMAND HISTORY
                        int resultAddHistory = TradingServer.Facade.FacadeAddNewCommandHistory(InvestorID, 13, "", DateTime.Now, 0, DateTime.Now, 0, Money, 0, 0, DateTime.Now, 0, 0, 0, "", -1, 0, 0, Comment, "9", 0, -1, "", false, false);

                        if (resultAddHistory > 0)
                        {
                            string commandHistory = TradingServer.Model.TradingCalculate.Instance.BuildCommandCode(resultAddHistory.ToString());

                            TradingServer.Facade.FacadeUpdateCommandCodeHistory(commandHistory, resultAddHistory);
                        }

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

                        break;
                    }
                }
            }

            return Result;
        }
        /// <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>
        /// UPDATE ONLINE COMMAND IN SYMBOL LIST AND INVESTOR LIST OF CLASS MAKRET
        /// AND UPDATE OPEN TRADE ON DATABASE
        /// </summary>
        /// <param name="InvestorID"></param>
        /// <param name="CommandID"></param>
        /// <param name="Commission"></param>
        /// <param name="ExpTime"></param>
        /// <param name="OpenPrice"></param>
        /// <param name="OpenTime"></param>
        /// <param name="StopLoss"></param>
        /// <param name="Swap"></param>
        /// <param name="TakeProfit"></param>
        /// <param name="SymbolName"></param>
        internal bool UpdateOpenTrade(int InvestorID, int CommandID, double Commission, DateTime ExpTime, double OpenPrice, DateTime OpenTime,
            double StopLoss, double Swap, double TakeProfit, string SymbolName, double taxes, string comment, double agentCommission, double size)
        {
            bool Result = false;
            double profit = 0;

            #region UPDATE ONLINE COMMAND IN INVESTOR LIST
            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)
                    {
                        if (Business.Market.InvestorList[i].CommandList != null)
                        {
                            int countCommand = Business.Market.InvestorList[i].CommandList.Count;
                            for (int j = 0; j < countCommand; j++)
                            {
                                if (Business.Market.InvestorList[i].CommandList[j].ID == CommandID)
                                {
                                    //SET NEW VALUE FOR ONLINE COMMAND CURRENT
                                    Business.Market.InvestorList[i].CommandList[j].Commission = Commission;
                                    Business.Market.InvestorList[i].CommandList[j].ExpTime = ExpTime;
                                    Business.Market.InvestorList[i].CommandList[j].OpenPrice = OpenPrice;
                                    Business.Market.InvestorList[i].CommandList[j].OpenTime = OpenTime;
                                    Business.Market.InvestorList[i].CommandList[j].StopLoss = StopLoss;
                                    Business.Market.InvestorList[i].CommandList[j].Swap = Swap;
                                    Business.Market.InvestorList[i].CommandList[j].TakeProfit = TakeProfit;
                                    Business.Market.InvestorList[i].CommandList[j].Comment = comment;
                                    Business.Market.InvestorList[i].CommandList[j].Size = size;
                                    Business.Market.InvestorList[i].CommandList[j].Profit = profit;

                                    bool isPending = TradingServer.Model.TradingCalculate.Instance.CheckIsPendingPosition(
                                        Business.Market.InvestorList[i].CommandList[j].Type.ID);

                                    if (!isPending)
                                    {
                                        Business.Market.InvestorList[i].CommandList[j].CalculatorProfitCommand(Business.Market.InvestorList[i].CommandList[j]);
                                        profit = Business.Market.InvestorList[i].CommandList[j].Symbol.ConvertCurrencyToUSD(
                                            Business.Market.InvestorList[i].CommandList[j].Symbol.Currency,
                                            Business.Market.InvestorList[i].CommandList[j].Profit, false,
                                            Business.Market.InvestorList[i].CommandList[j].SpreaDifferenceInOpenTrade,
                                            Business.Market.InvestorList[i].CommandList[j].Symbol.Digit);

                                        Business.Market.InvestorList[i].CommandList[j].Profit = profit;
                                    }

                                    //CALL FUNCTION UPDATE ONLINE COMMAND IN DATABASE
                                    bool ResultUpdate = false;
                                    ResultUpdate = this.UpdateOnlineCommand(Business.Market.InvestorList[i].CommandList[j]);

                                    bool IsBuy = false;
                                    if (Business.Market.InvestorList[i].CommandList[j].Type.ID == 1 ||
                                        Business.Market.InvestorList[i].CommandList[j].Type.ID == 7 ||
                                        Business.Market.InvestorList[i].CommandList[j].Type.ID == 9 ||
                                        Business.Market.InvestorList[i].CommandList[j].Type.ID == 11)
                                        IsBuy = true;

                                    #region MAP COMMAND TO CLIENT
                                    //SEND NOTIFY TO CLIENT
                                    string Message = "UpdateCommandByManager$True,UPDATE COMMAND BY MANAGER 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 + "," + 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 + "," +
                                                        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 + ",Update";

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

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

                                    //SEND NOTIFY TO AGENT SERVER
                                    Message += "," + Business.Market.InvestorList[i].CommandList[j].AgentRefConfig + "," +
                                        Business.Market.InvestorList[i].CommandList[j].SpreaDifferenceInOpenTrade;

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

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

                                    Result = true;
                                    break;
                                }
                            }
                        }

                        break;
                    }
                }
            }
            #endregion

            #region UPDATE ONLINE COMMAND IN COMMAND EXECUTOR
            if (Business.Market.CommandExecutor != null)
            {
                int count = Business.Market.CommandExecutor.Count;
                for (int i = 0; i < count; i++)
                {
                    if (Business.Market.CommandExecutor[i].ID == CommandID)
                    {
                        #region UPDATE COMMAND OF INVESTOR IN COMMAND EXECUTOR
                        //SET NEW VALUE FOR ONLINE COMMAND CURRENT
                        Business.Market.CommandExecutor[i].Commission = Commission;
                        Business.Market.CommandExecutor[i].ExpTime = ExpTime;
                        Business.Market.CommandExecutor[i].OpenPrice = OpenPrice;
                        Business.Market.CommandExecutor[i].OpenTime = OpenTime;
                        Business.Market.CommandExecutor[i].StopLoss = StopLoss;
                        Business.Market.CommandExecutor[i].Swap = Swap;
                        Business.Market.CommandExecutor[i].TakeProfit = TakeProfit;
                        Business.Market.CommandExecutor[i].Comment = comment;
                        Business.Market.CommandExecutor[i].Size = size;

                        break;
                        #endregion

                    }
                }
            }
            #endregion

            #region UPDATE ONLINE COMMAND IN SYMBOL LIST
            if (Business.Market.SymbolList != null)
            {
                int count = Business.Market.SymbolList.Count;
                for (int i = 0; i < count; i++)
                {
                    if (Business.Market.SymbolList[i].Name == SymbolName)
                    {
                        if (Business.Market.SymbolList[i].CommandList != null)
                        {
                            int countCommand = Business.Market.SymbolList[i].CommandList.Count;
                            for (int j = 0; j < countCommand; j++)
                            {
                                if (Business.Market.SymbolList[i].CommandList[j].ID == CommandID)
                                {
                                    //SET NEW VALUE FOR ONLINE COMMAND CURRENT
                                    Business.Market.SymbolList[i].CommandList[j].Commission = Commission;
                                    Business.Market.SymbolList[i].CommandList[j].ExpTime = ExpTime;
                                    Business.Market.SymbolList[i].CommandList[j].OpenPrice = OpenPrice;
                                    Business.Market.SymbolList[i].CommandList[j].OpenTime = OpenTime;
                                    Business.Market.SymbolList[i].CommandList[j].StopLoss = StopLoss;
                                    Business.Market.SymbolList[i].CommandList[j].Swap = Swap;
                                    Business.Market.SymbolList[i].CommandList[j].TakeProfit = TakeProfit;
                                    Business.Market.SymbolList[i].CommandList[j].Comment = comment;
                                    Business.Market.SymbolList[i].CommandList[j].Size = size;
                                    Business.Market.SymbolList[i].CommandList[j].Profit = profit;

                                    break;
                                }
                            }
                        }

                        break;
                    }
                }
            }
            #endregion

            return Result;
        }
        /// <summary>
        /// EXTRACT COMMAND AND CALL FUNCTION
        /// </summary>
        /// <param name="Cmd"></param>
        /// <returns></returns>
        public List<string> ExtractServerCommand(string Cmd, string ipAddress, string code)
        {
            List<string> StringResult = new List<string>();
            if (!string.IsNullOrEmpty(Cmd))
            {
                string[] subCommand = Cmd.Split('#');
                if (subCommand.Length > 0)
                {
                    int count = subCommand.Length;  
                    for (int i = 0; i < count; i++)
                    {
                        string[] subValue = subCommand[i].Split('$');
                        switch (subValue[0])
                        {
                            #region Function Class Symbol(LOG)
                            case "SelectSymbol":
                                {
                                    bool checkip = Facade.FacadeCheckIpManagerAndAdmin(code, ipAddress);
                                    if (checkip)
                                    {
                                        StringResult = this.SelectSymbolInSymbolList();
                                    }
                                    //#region INSERT SYSTEM LOG
                                    ////INSERT SYSTEM LOG
                                    //string content = "'" + code + "': " + StringResult.Count + " symbol has been request";
                                    //string comment = "[request symbol]";
                                    //TradingServer.Facade.FacadeAddNewSystemLog(4, content, comment, ipAddress, code);
                                    //#endregion                                   
                                }
                                break;
                            #endregion

                            #region Function Class TradingConfig(SymbolConfig)
                            case "SelectTradingConfigBySymbolID":
                                {
                                    string[] listParameter = subValue[1].Split(',');
                                    string temp = string.Empty;
                                    if (listParameter.Length > 0)
                                    {
                                        int countParameter = listParameter.Length;
                                        string result = string.Empty;
                                        for (int j = 0; j < countParameter; j++)
                                        {
                                            temp = this.ExtractCommandServer(subValue[0] + "$" + listParameter[j], "", code);
                                            StringResult.Add(temp);
                                        }
                                    }
                                }
                                break;
                            #endregion

                            #region GET TRADING CONFIG BY LIST SYMBOL ID
                            case "GetTradingConfigByListSymbolID":
                                {
                                    string[] listSymbolID = subValue[1].Split(',');
                                    int countListSymbolID = listSymbolID.Length;
                                    for (int j = 0; j < count; j++)
                                    {
                                        string temp = string.Empty;
                                        temp = this.SelectTradingConfigBySymbolIDInSymbolList(int.Parse(listSymbolID[j]));
                                        StringResult.Add(subValue[0] + "$" + temp);
                                    }
                                }
                                break;
                            #endregion

                            #region Function Class Investor Group
                            case "SelectInvestorGroup":
                                {
                                    string temp = string.Empty;
                                    temp = this.ExtractCommandServer(subValue[0], "", code);
                                    StringResult.Add(temp);
                                }
                                break;
                            #endregion

                            #region Function Class Investor Group Config
                            case "SelectInvestorGroupConfigByInvestorGroupID":
                                {
                                    bool checkip = Facade.FacadeCheckIpManagerAndAdmin(code, ipAddress);
                                    if (checkip)
                                    {
                                        string[] listParameter = subValue[1].Split(',');
                                        string temp = string.Empty;
                                        if (listParameter.Length > 0)
                                        {
                                            int countParameter = listParameter.Length;
                                            string result = string.Empty;
                                            for (int j = 0; j < countParameter; j++)
                                            {
                                                temp = this.ExtractCommandServer(subValue[0] + "$" + listParameter[j], "", code);
                                                StringResult.Add(temp);
                                            }
                                        }
                                    }
                                }
                                break;
                            #endregion

                            #region Function Class Security
                            case "SelectSecurity":
                                {
                                    bool checkip = Facade.FacadeCheckIpManagerAndAdmin(code, ipAddress);
                                    if (checkip)
                                    {
                                        string temp = string.Empty;
                                        temp = this.ExtractCommandServer(subValue[0], "", code);
                                        StringResult.Add(temp);
                                    }
                                }
                                break;
                            #endregion

                            #region Function Class Security Config
                            case "SelectSecurityConfigBySecurityID":
                                {
                                    string[] listParameter = subValue[1].Split(',');
                                    string temp = string.Empty;
                                    if (listParameter.Length > 0)
                                    {
                                        int countParameter = listParameter.Length;
                                        string result = string.Empty;
                                        for (int j = 0; j < countParameter; j++)
                                        {
                                            temp = this.ExtractCommandServer(subValue[0] + "$" + listParameter[j], "", code);
                                            result = listParameter[j] + "*" + temp + "@";
                                        }
                                        StringResult.Add(result);
                                    }
                                }
                                break;
                            #endregion

                            #region Function Class Investor
                            case "SelectInvestor":
                                {
                                    string temp = string.Empty;
                                    temp = this.ExtractCommandServer(subValue[0], "", code);
                                    StringResult.Add(temp);
                                }
                                break;

                            #region GET ALL INVESTOR IN DATABASE(GET FROM TO)(LOG COMMENT)
                            case "SelectInvestorWithRowNumber":
                                {
                                    List<Business.Investor> Result = new List<Investor>();
                                    if (subValue.Length == 2)
                                    {
                                        string[] subParameter = subValue[1].Split(',');
                                        if (subParameter.Length == 2)
                                        {
                                            int From = -1;
                                            int To = -1;
                                            int.TryParse(subParameter[0], out From);
                                            int.TryParse(subParameter[1], out To);
                                            int RowNumber = To - From;
                                            //Result = TradingServer.Facade.FacadeGetInvestorWithRowNumber(RowNumber, From);
                                            Result = TradingServer.Facade.FacadeGetInvestorFormTo(From, To);
                                            if (Result != null && Result.Count > 0)
                                            {
                                                int countInvestor = Result.Count;
                                                for (int j = 0; j < countInvestor; j++)
                                                {
                                                    string Message = string.Empty;
                                                    Message = subValue[0] + "$" + Result[j].InvestorID + "," + Result[j].InvestorStatusID + "," + Result[j].InvestorGroupInstance.InvestorGroupID + "," +
                                                        Result[j].AgentID + "," + Result[j].Balance + "," + Result[j].Credit + "," + Result[j].Code + "," +
                                                        Result[j].IsDisable + "," + Result[j].TaxRate + "," + Result[j].Leverage + "," +
                                                        Result[j].InvestorProfileID + "," + Result[j].Address + "," + Result[j].Phone + "," +
                                                        Result[j].City + "," + Result[j].Country + "," + Result[j].Email + "," + Result[j].ZipCode + "," +
                                                        Result[j].RegisterDay + "," + Result[j].InvestorComment + "," + Result[j].State + "," + Result[j].NickName + "," +
                                                        Result[j].AllowChangePwd + "," + Result[j].ReadOnly + "," + Result[j].SendReport + "," + Result[j].IsOnline;

                                                    StringResult.Add(Message);
                                                }
                                            }
                                            //else
                                            //{
                                            //if (From == 0)
                                            //{
                                            //    Result = TradingServer.Facade.FacadeGetAllInvestor();
                                            //    if (Result != null && Result.Count > 0)
                                            //    {
                                            //        int countInvestor = Result.Count;
                                            //        for (int j = 0; j < countInvestor; j++)
                                            //        {
                                            //            string Message = string.Empty;
                                            //            Message = subValue[0] + "$" + Result[j].InvestorID + "," + Result[j].InvestorStatusID + "," + Result[j].InvestorGroupInstance.InvestorGroupID + "," +
                                            //                Result[j].AgentID + "," + Result[j].Balance + "," + Result[j].Credit + "," + Result[j].Code + "," +
                                            //                Result[j].IsDisable + "," + Result[j].TaxRate + "," + Result[j].Leverage + "," +
                                            //                Result[j].InvestorProfileID + "," + Result[j].Address + "," + Result[j].Phone + "," +
                                            //                Result[j].City + "," + Result[j].Country + "," + Result[j].Email + "," + Result[j].ZipCode + "," +
                                            //                Result[j].RegisterDay + "," + Result[j].Comment + "," + Result[j].State + "," + Result[j].NickName + "," +
                                            //                Result[j].AllowChangePwd + "," + Result[j].ReadOnly + "," + Result[j].SendReport + "," + Result[j].IsOnline;

                                            //            StringResult.Add(Message);
                                            //        }
                                            //    }
                                            //}
                                            //else
                                            //{
                                            //    string Message = subValue[0] + "$";
                                            //    StringResult.Add(Message);
                                            //}
                                            //}

                                            #region INSERT SYSTEM LOG
                                            //INSERT SYSTEM LOG
                                            //'2222': 22450 accounts have been requested                                            
                                            //string content = "'" + code + "': " + StringResult.Count + " accounts have been requested";
                                            //string comment = "[account request]";
                                            //TradingServer.Facade.FacadeAddNewSystemLog(3, content, comment, ipAddress, code);
                                            #endregion
                                        }
                                    }
                                }
                                break;
                            #endregion

                            #region GET INVESTOR ONLINE IN RAM(GET FROM TO)
                            case "SelectInvestorOnline":
                                {
                                    List<Business.Investor> Result = new List<Investor>();
                                    if (subValue.Length == 2)
                                    {
                                        string[] subParameter = subValue[1].Split(',');
                                        if (subParameter.Length == 2)
                                        {
                                            int From = -1;
                                            int To = -1;
                                            int.TryParse(subParameter[0], out From);
                                            int.TryParse(subParameter[1], out To);
                                            Result = TradingServer.Facade.FacadeGetInvestorOnline(From, To);
                                            if (Result != null && Result.Count > 0)
                                            {
                                                int countInvestor = Result.Count;
                                                for (int j = 0; j < countInvestor; j++)
                                                {
                                                    string Message = string.Empty;
                                                    Message = subValue[0] + "$" + Result[j].InvestorID + "," + Result[j].InvestorStatusID + "," + Result[j].InvestorGroupInstance.InvestorGroupID + "," +
                                                        Result[j].AgentID + "," + Result[j].Balance + "," + Result[j].Credit + "," + Result[j].Code + "," +
                                                        Result[j].IsDisable + "," + Result[j].TaxRate + "," + Result[j].Leverage + "," +
                                                        Result[j].InvestorProfileID + "," + Result[j].Address + "," + Result[j].Phone + "," +
                                                        Result[j].City + "," + Result[j].Country + "," + Result[j].Email + "," + Result[j].ZipCode + "," +
                                                        Result[j].RegisterDay + "," + Result[j].InvestorComment + "," + Result[j].State + "," + Result[j].NickName + "," +
                                                        Result[j].AllowChangePwd + "," + Result[j].ReadOnly + "," + Result[j].SendReport + "," + Result[j].IsOnline;

                                                    StringResult.Add(Message);
                                                }
                                            }
                                            else
                                            {
                                                string Message = subValue[0] + "$";
                                                StringResult.Add(Message);
                                            }
                                        }
                                    }

                                    #region INSERT SYSTEM LOG
                                    //INSERT SYSTEM LOG
                                    //'2222': 22450 accounts have been requested                                            
                                    string content = "'" + code + "': " + StringResult.Count + " account have been requested";
                                    string comment = "[account request]";
                                    TradingServer.Facade.FacadeAddNewSystemLog(3, content, comment, ipAddress, code);
                                    #endregion
                                }
                                break;
                            #endregion

                            #region GET INVESTOR ONLINE IN RAM BY LIST GROUP(GET FROM TO)
                            case "GetInvestorOnlineByListGroup":
                                {
                                    List<int> ListGroupID = new List<int>();
                                    int From = -1;
                                    int To = -1;
                                    if (subValue[1].Length > 0)
                                    {
                                        string[] subParameter = subValue[1].Split(',');
                                        int.TryParse(subParameter[0], out From);
                                        int.TryParse(subParameter[1], out To);
                                        int countParameter = subParameter.Length;
                                        for (int j = 2; j < countParameter; j++)
                                        {
                                            int InvestorGroupID = -1;
                                            int.TryParse(subParameter[j], out InvestorGroupID);
                                            ListGroupID.Add(InvestorGroupID);
                                        }

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

                                        if (ListGroupID != null && ListGroupID.Count > 0)
                                        {
                                            int countListGroup = ListGroupID.Count;
                                            for (int n = 0; n < countListGroup; n++)
                                            {
                                                Result = TradingServer.Facade.FacadeGetInvestorOnlineByGroupID(From, To, ListGroupID[n]);

                                                if (Result != null && Result.Count > 0)
                                                {
                                                    int countResult = Result.Count;
                                                    for (int j = 0; j < countResult; j++)
                                                    {
                                                        string Message = subValue[0] + "$" + Result[j].InvestorID + "," + Result[j].InvestorStatusID + "," +
                                                            Result[j].InvestorGroupInstance.InvestorGroupID + "," +
                                                            Result[j].AgentID + "," + Result[j].Balance + "," + Result[j].Credit + "," + Result[j].Code + "," +
                                                            Result[j].IsDisable + "," + Result[j].TaxRate + "," + Result[j].Leverage + "," +
                                                            Result[j].InvestorProfileID + "," + Result[j].Address + "," + Result[j].Phone + "," +
                                                            Result[j].City + "," + Result[j].Country + "," + Result[j].Email + "," + Result[j].ZipCode + "," +
                                                            Result[j].RegisterDay + "," + Result[j].InvestorComment + "," + Result[j].State + "," + Result[j].NickName + "," +
                                                            Result[j].AllowChangePwd + "," + Result[j].ReadOnly + "," + Result[j].SendReport + "," + Result[j].IsOnline;

                                                        StringResult.Add(Message);
                                                    }
                                                }
                                            }
                                        }

                                        if (StringResult.Count == 0)
                                        {
                                            string Message = subValue[0] + "$";
                                            StringResult.Add(Message);
                                        }
                                    }
                                }
                                break;
                            #endregion

                            #region GET INVESTOR WITH COMMAND IN RAM(GET FROM TO)
                            case "SelectInvestorWithCommand":
                                {
                                    List<Business.Investor> Result = new List<Investor>();
                                    if (subValue.Length == 2)
                                    {
                                        string[] subParameter = subValue[1].Split(',');
                                        if (subParameter.Length == 2)
                                        {
                                            int From = -1;
                                            int To = -1;
                                            int.TryParse(subParameter[0], out From);
                                            int.TryParse(subParameter[1], out To);

                                            Result = TradingServer.Facade.FacadeGetInvestorWithCommand(From, To);
                                            if (Result != null && Result.Count > 0)
                                            {
                                                int countResult = Result.Count;
                                                for (int j = 0; j < countResult; j++)
                                                {
                                                    string Message = string.Empty;
                                                    Message = subValue[0] + "$" + Result[j].InvestorID + "," + Result[j].InvestorStatusID + "," + Result[j].InvestorGroupInstance.InvestorGroupID + "," +
                                                        Result[j].AgentID + "," + Result[j].Balance + "," + Result[j].Credit + "," + Result[j].Code + "," +
                                                        Result[j].IsDisable + "," + Result[j].TaxRate + "," + Result[j].Leverage + "," +
                                                        Result[j].InvestorProfileID + "," + Result[j].Address + "," + Result[j].Phone + "," +
                                                        Result[j].City + "," + Result[j].Country + "," + Result[j].Email + "," + Result[j].ZipCode + "," +
                                                        Result[j].RegisterDay + "," + Result[j].InvestorComment + "," + Result[j].State + "," + Result[j].NickName + "," +
                                                        Result[j].AllowChangePwd + "," + Result[j].ReadOnly + "," + Result[j].SendReport + "," + Result[j].Credit;

                                                    StringResult.Add(Message);
                                                }
                                            }
                                            else
                                            {
                                                string Message = subValue[0] + "$";
                                                StringResult.Add(Message);
                                            }
                                        }
                                    }
                                }
                                break;
                            #endregion

                            #region GET INVESTOR WITH MARGIN LEVEL IN RAM
                            case "SelectInvestorWithMarginLevel":
                                {
                                    List<Business.Investor> Result = new List<Investor>();
                                    Result = TradingServer.Facade.FacadeGetInvestorWithMarginLevel();
                                    if (Result != null && Result.Count > 0)
                                    {
                                        int countInvestor = Result.Count;
                                        for (int j = 0; j < countInvestor; j++)
                                        {
                                            string Message = string.Empty;
                                            Message = subValue[0] + "$" + Result[j].InvestorID + "," + Result[j].InvestorStatusID + "," + Result[j].InvestorGroupInstance.InvestorGroupID + "," +
                                                Result[j].AgentID + "," + Result[j].Balance + "," + Result[j].Credit + "," + Result[j].Code + "," +
                                                Result[j].IsDisable + "," + Result[j].TaxRate + "," + Result[j].Leverage + "," +
                                                Result[j].InvestorProfileID + "," + Result[j].Address + "," + Result[j].Phone + "," +
                                                Result[j].City + "," + Result[j].Country + "," + Result[j].Email + "," + Result[j].ZipCode + "," +
                                                Result[j].RegisterDay + "," + Result[j].InvestorComment + "," + Result[j].State + "," + Result[j].NickName + "," +
                                                Result[j].AllowChangePwd + "," + Result[j].ReadOnly + "," + Result[j].SendReport + "," + Result[j].IsOnline;

                                            StringResult.Add(Message);
                                        }
                                    }
                                    else
                                    {
                                        string Message = subValue[0] + "$";
                                        StringResult.Add(Message);
                                    }
                                }
                                break;
                            #endregion

                            #region GET INVESTOR BY INVESTOR GROUP ID(GET FROM TO) IN RAM
                            case "GetInvestorByInvestorGroupID":
                                {
                                    bool checkip = Facade.FacadeCheckIpAdmin(code, ipAddress);
                                    if (checkip)
                                    {
                                        if (subValue.Length == 2)
                                        {
                                            int InvestorGroupID = -1;
                                            int From = -1;
                                            int To = -1;

                                            string[] subParameter = subValue[1].Split(',');

                                            if (subParameter.Length > 3)
                                            {
                                                int.TryParse(subParameter[0], out From);
                                                int.TryParse(subParameter[1], out To);
                                                int.TryParse(subParameter[2], out InvestorGroupID);

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

                                                Result = TradingServer.Facade.FacadeGetInvestorByInvestorGroupID(InvestorGroupID, From, To);

                                                if (Result != null && Result.Count > 0)
                                                {
                                                    int countResult = Result.Count;
                                                    for (int j = 0; j < countResult; j++)
                                                    {
                                                        string Message = subValue[0] + "$" + Result[j].InvestorID + "," + Result[j].InvestorStatusID + "," + InvestorGroupID + "," +
                                                            Result[j].AgentID + "," + Result[j].Balance + "," + Result[j].Credit + "," + Result[j].Code + "," +
                                                            Result[j].IsDisable + "," + Result[j].TaxRate + "," + Result[j].Leverage + "," +
                                                            Result[j].InvestorProfileID + "," + Result[j].Address + "," + Result[j].Phone + "," +
                                                            Result[j].City + "," + Result[j].Country + "," + Result[j].Email + "," + Result[j].ZipCode + "," +
                                                            Result[j].RegisterDay + "," + Result[j].InvestorComment + "," + Result[j].State + "," + Result[j].NickName + "," +
                                                            Result[j].AllowChangePwd + "," + Result[j].ReadOnly + "," + Result[j].SendReport + "," + Result[j].IsOnline;

                                                        StringResult.Add(Message);
                                                    }
                                                }
                                                else
                                                {
                                                    string Message = subValue[0] + "$";
                                                    StringResult.Add(Message);
                                                }
                                            }
                                        }
                                    }
                                }
                                break;
                            #endregion

                            #region GET INVESTOR BY INVESTOR GROUP ID GET FROM TO IN DATABASE(LOG COMMENT)
                            case "GetInvestorByInvestorGroupIDWithDB":
                                {
                                    bool checkip = Facade.FacadeCheckIpAdmin(code, ipAddress);
                                    if (checkip)
                                    {
                                        List<Business.Investor> Result = new List<Investor>();
                                        if (subValue.Length == 2)
                                        {
                                            int From = -1;
                                            int To = -1;
                                            int InvestorGroupID = -1;

                                            string[] subParameter = subValue[1].Split(',');
                                            if (subParameter.Length == 3)
                                            {
                                                int.TryParse(subParameter[0], out From);
                                                int.TryParse(subParameter[1], out To);
                                                int.TryParse(subParameter[2], out InvestorGroupID);
                                                int RowNumber = To - From;
                                                //Result = TradingServer.Facade.FacadeGetInvestorByInvestorGroupDB(InvestorGroupID, RowNumber, From);
                                                Result = TradingServer.Facade.FacadeGetInvestorByGroupFromTo(InvestorGroupID, From, To);
                                                if (Result != null && Result.Count > 0)
                                                {
                                                    int countInvestor = Result.Count;
                                                    for (int j = 0; j < countInvestor; j++)
                                                    {
                                                        string Message = string.Empty;
                                                        Message = subValue[0] + "$" + Result[j].InvestorID + "," + Result[j].InvestorStatusID + "," + Result[j].InvestorGroupInstance.InvestorGroupID + "," +
                                                            Result[j].AgentID + "," + Result[j].Balance + "," + Result[j].Credit + "," + Result[j].Code + "," +
                                                            Result[j].IsDisable + "," + Result[j].TaxRate + "," + Result[j].Leverage + "," +
                                                            Result[j].InvestorProfileID + "," + Result[j].Address + "," + Result[j].Phone + "," +
                                                            Result[j].City + "," + Result[j].Country + "," + Result[j].Email + "," + Result[j].ZipCode + "," +
                                                            Result[j].RegisterDay + "," + Result[j].InvestorComment + "," + Result[j].State + "," + Result[j].NickName + "," +
                                                            Result[j].AllowChangePwd + "," + Result[j].ReadOnly + "," + Result[j].SendReport + "," + Result[j].IsOnline + "," +
                                                            Result[j].Margin + "," + Result[j].FreezeMargin + "," + Result[j].PhonePwd + "," + Result[j].IDPassport + "," +
                                                            Result[j].TotalDeposit;

                                                        StringResult.Add(Message);
                                                    }
                                                }
                                                //else
                                                //{
                                                //    if (From == 0)
                                                //    {
                                                //        Result = TradingServer.Facade.FacadeGetInvestorByInvestorGroup(InvestorGroupID);
                                                //        if (Result != null && Result.Count > 0)
                                                //        {
                                                //            int countInvestor = Result.Count;
                                                //            for (int j = 0; j < countInvestor; j++)
                                                //            {
                                                //                string Message = string.Empty;
                                                //                Message = subValue[0] + "$" + Result[j].InvestorID + "," + Result[j].InvestorStatusID + "," + Result[j].InvestorGroupInstance.InvestorGroupID + "," +
                                                //                    Result[j].AgentID + "," + Result[j].Balance + "," + Result[j].Credit + "," + Result[j].Code + "," +
                                                //                    Result[j].IsDisable + "," + Result[j].TaxRate + "," + Result[j].Leverage + "," +
                                                //                    Result[j].InvestorProfileID + "," + Result[j].Address + "," + Result[j].Phone + "," +
                                                //                    Result[j].City + "," + Result[j].Country + "," + Result[j].Email + "," + Result[j].ZipCode + "," +
                                                //                    Result[j].RegisterDay + "," + Result[j].Comment + "," + Result[j].State + "," + Result[j].NickName + "," +
                                                //                    Result[j].AllowChangePwd + "," + Result[j].ReadOnly + "," + Result[j].SendReport + "," + Result[j].IsOnline;

                                                //                StringResult.Add(Message);
                                                //            }
                                                //        }
                                                //    }                                                
                                                //}
                                            }
                                        }

                                        if (StringResult.Count == 0)
                                        {
                                            string Message = subValue[0] + "$";
                                            StringResult.Add(Message);
                                        }
                                    }
                                    else
                                    {
                                        string Message = subValue[0] + "$MCM005";
                                        StringResult.Add(Message);
                                    }
                                    #region INSERT SYSTEM LOG
                                    //INSERT SYSTEM LOG
                                    //'2222': 22450 accounts have been requested                                                                                
                                    //string content = "'" + code + "': " + StringResult.Count + " account have been requested";
                                    //string comment = "[account request]";
                                    //TradingServer.Facade.FacadeAddNewSystemLog(3, content, comment, ipAddress, code);
                                    #endregion
                                }
                                break;
                            #endregion

                            #region GET INVESTOR BY INVESTOR GROUP LIST(GET FROM TO) IN RAM
                            case "GetInvestorByListGroup":
                                {
                                    bool checkip = Facade.FacadeCheckIpManagerAndAdmin(code, ipAddress);
                                    if (checkip)
                                    {
                                        List<int> ListGroupID = new List<int>();
                                        int From = -1;
                                        int To = -1;
                                        if (subValue[1].Length > 0)
                                        {
                                            string[] subParameter = subValue[1].Split(',');
                                            int.TryParse(subParameter[0], out From);
                                            int.TryParse(subParameter[1], out To);
                                            int countParameter = subParameter.Length;
                                            for (int j = 2; j < countParameter; j++)
                                            {
                                                int InvestorGroupID = -1;
                                                int.TryParse(subParameter[j], out InvestorGroupID);
                                                ListGroupID.Add(InvestorGroupID);
                                            }

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

                                            Result = TradingServer.Facade.FacadeGetInvestorByGroupList(From, To, ListGroupID);

                                            if (Result != null && Result.Count > 0)
                                            {
                                                int countResult = Result.Count;
                                                for (int j = 0; j < countResult; j++)
                                                {
                                                    string Message = subValue[0] + "$" + Result[j].InvestorID + "," + Result[j].InvestorStatusID + "," +
                                                        Result[j].InvestorGroupInstance.InvestorGroupID + "," +
                                                        Result[j].AgentID + "," + Result[j].Balance + "," + Result[j].Credit + "," + Result[j].Code + "," +
                                                        Result[j].IsDisable + "," + Result[j].TaxRate + "," + Result[j].Leverage + "," +
                                                        Result[j].InvestorProfileID + "," + Result[j].Address + "," + Result[j].Phone + "," +
                                                        Result[j].City + "," + Result[j].Country + "," + Result[j].Email + "," + Result[j].ZipCode + "," +
                                                        Result[j].RegisterDay + "," + Result[j].InvestorComment + "," + Result[j].State + "," + Result[j].NickName + "," +
                                                        Result[j].AllowChangePwd + "," + Result[j].ReadOnly + "," + Result[j].SendReport + "," + Result[j].IsOnline + "," +
                                                        Result[j].Margin + "," + Result[j].FreezeMargin + "," + Result[j].PhonePwd + "," + Result[j].IDPassport;

                                                    StringResult.Add(Message);
                                                }
                                            }
                                        }
                                    }
                                    if (StringResult.Count == 0)
                                    {
                                        string Message = subValue[0] + "$";
                                        StringResult.Add(Message);
                                    }
                                }
                                break;
                            #endregion

                            case "GetInvestorAccountByListCode":
                                {
                                    string[] subParameter = subValue[1].Split(',');
                                    List<string> listCode = new List<string>();
                                    if (subParameter != null && subParameter.Length > 0)
                                    {
                                        int countCode = subParameter.Length;
                                        for (int j = 0; j < countCode; j++)
                                        {
                                            listCode.Add(subParameter[j]);
                                        }
                                    }

                                    List<Business.Investor> result = TradingServer.Facade.FacadeGetInvestorByListCode(listCode);

                                    if (result != null && result.Count > 0)
                                    {
                                        int countResult = result.Count;
                                        for (int j = 0; j < countResult; j++)
                                        {
                                            string Message = subValue[0] + "$" + result[j].InvestorID + "," + result[j].InvestorStatusID + "," +
                                                        result[j].InvestorGroupInstance.InvestorGroupID + "," +
                                                        result[j].AgentID + "," + result[j].Balance + "," + result[j].Credit + "," + result[j].Code + "," +
                                                        result[j].IsDisable + "," + result[j].TaxRate + "," + result[j].Leverage + "," +
                                                        result[j].InvestorProfileID + "," + result[j].Address + "," + result[j].Phone + "," +
                                                        result[j].City + "," + result[j].Country + "," + result[j].Email + "," + result[j].ZipCode + "," +
                                                        result[j].RegisterDay + "," + result[j].InvestorComment + "," + result[j].State + "," + result[j].NickName + "," +
                                                        result[j].AllowChangePwd + "," + result[j].ReadOnly + "," + result[j].SendReport + "," + result[j].IsOnline + "," +
                                                        result[j].Margin + "," + result[j].FreezeMargin + "," + result[j].PhonePwd + "," + result[j].IDPassport;

                                            StringResult.Add(Message);
                                        }
                                    }
                                    else
                                    {
                                        string message = subValue[0] + "$";
                                        StringResult.Add(message);
                                    }
                                }
                                break;
                            #endregion

                            #region Function Class Open Trade
                            //GET ALL ONLINE COMMAND IN INVESTOR LIST OF CLASS MARKET(SERVER CALL)
                            case "SelectOnlineCommand":
                                {
                                    bool checkip = Facade.FacadeCheckIpManagerAndAdmin(code, ipAddress);
                                    if (checkip)
                                    {
                                        List<Business.OpenTrade> Result = new List<OpenTrade>();
                                        if (subValue.Length == 2)
                                        {
                                            string[] subParameter = subValue[1].Split(',');
                                            int From = -1;
                                            int To = -1;
                                            int.TryParse(subParameter[0], out From);
                                            int.TryParse(subParameter[1], out To);

                                            Result = TradingServer.Facade.FacadeGetOnlineCommand(From, To);
                                            if (Result != null && Result.Count > 0)
                                            {
                                                int countCommand = Result.Count;
                                                for (int j = 0; j < countCommand; j++)
                                                {
                                                    string Message = subValue[0] + "$" + Result[j].ClientCode + "," + Result[j].ClosePrice + "," + Result[j].CloseTime + "," +
                                                        Result[j].CommandCode + "," + Result[j].Commission + "," + Result[j].ExpTime + "," +
                                                        Result[j].ID + "," + Result[j].Investor.InvestorID + "," + Result[j].IsClose + "," +
                                                        Result[j].IsHedged + "," + Result[j].Margin + "," + Result[j].MaxDev + "," + Result[j].OpenPrice + "," +
                                                        Result[j].OpenTime + "," + Result[j].Profit + "," + Result[j].Size + "," + Result[j].StopLoss + "," +
                                                        Result[j].Swap + "," + Result[j].Symbol.Name + "," + Result[j].TakeProfit + "," + Result[j].Taxes + "," +
                                                        Result[j].Type.Name + "," + Result[j].Type.ID + "," + Result[j].Symbol.ContractSize + "," +
                                                        /*Result[j].Symbol.SpreadDifference*/ Result[j].SpreaDifferenceInOpenTrade + "," + Result[j].Symbol.Currency + "," +
                                                        Result[j].Comment + "," + Result[j].AgentCommission;

                                                    StringResult.Add(Message);
                                                }
                                            }
                                            else
                                            {
                                                string Message = subValue[0] + "$";
                                                StringResult.Add(Message);
                                            }
                                        }
                                    }
                                }
                                break;

                            //SELECT ONLINE COMMAND BY INVESTOR ID IN INVESTOR LIST OF CLASS MARKET(SERVER CALL)
                            case "SelectOnlineCommandByInvestorID":
                                {
                                    List<Business.OpenTrade> Result = new List<OpenTrade>();
                                    int InvestorID = -1;
                                    int.TryParse(subValue[1], out InvestorID);
                                    Result = TradingServer.Facade.FacadeGetOnlineCommandByInvestorID(InvestorID);

                                    if (Result != null && Result.Count > 0)
                                    {
                                        int countCommand = Result.Count;
                                        for (int j = 0; j < countCommand; j++)
                                        {
                                            string Message = subValue[0] + "$" + Result[j].ClientCode + "," + Result[j].ClosePrice + "," + Result[j].CloseTime + "," +
                                                Result[j].CommandCode + "," + Result[j].Commission + "," + Result[j].ExpTime + "," + Result[j].ID + "," +
                                                Result[j].Investor.InvestorID + "," + Result[j].IsClose + "," + Result[j].IsHedged + "," + Result[j].Margin + "," +
                                                Result[j].MaxDev + "," + Result[j].OpenPrice + "," + Result[j].OpenTime + "," + Result[j].Profit + "," +
                                                Result[j].Size + "," + Result[j].StopLoss + "," + Result[j].Swap + "," + Result[j].Symbol.Name + "," + Result[j].TakeProfit + "," +
                                                Result[j].Taxes + "," + Result[j].Type.Name + "," + Result[j].Type.ID + "," + Result[j].Symbol.ContractSize + "," +
                                                Result[j].Symbol.Currency + "," + Result[j].Comment + "," + Result[j].AgentCommission;

                                            StringResult.Add(Message);
                                        }
                                    }
                                    else
                                    {
                                        string Message = subValue[0] + "$";
                                        StringResult.Add(Message);
                                    }
                                }
                                break;

                            case "SelectOnlineCommandByInvestorWithStartEnd":
                                {
                                    bool checkip = Facade.FacadeCheckIpAdmin(code, ipAddress);
                                    if (checkip)
                                    {
                                        List<Business.OpenTrade> Result = new List<OpenTrade>();
                                        int InvestorID = -1;
                                        int start = 0;
                                        int end = 0;

                                        string[] subParameter = subValue[1].Split(',');
                                        int.TryParse(subParameter[0], out InvestorID);
                                        int.TryParse(subParameter[1], out start);
                                        int.TryParse(subParameter[2], out end);

                                        Result = TradingServer.Facade.FacadeGetOpenTradeByInvestorWithStartEnd(InvestorID, start, end);

                                        if (Result != null && Result.Count > 0)
                                        {
                                            int countCommand = Result.Count;
                                            for (int j = 0; j < countCommand; j++)
                                            {
                                                string Message = subValue[0] + "$" + Result[j].ClientCode + "," + Result[j].ClosePrice + "," + Result[j].CloseTime + "," +
                                                    Result[j].CommandCode + "," + Result[j].Commission + "," + Result[j].ExpTime + "," + Result[j].ID + "," +
                                                    Result[j].Investor.InvestorID + "," + Result[j].IsClose + "," + Result[j].IsHedged + "," + Result[j].Margin + "," +
                                                    Result[j].MaxDev + "," + Result[j].OpenPrice + "," + Result[j].OpenTime + "," + Result[j].Profit + "," +
                                                    Result[j].Size + "," + Result[j].StopLoss + "," + Result[j].Swap + "," + Result[j].Symbol.Name + "," + Result[j].TakeProfit + "," +
                                                    Result[j].Taxes + "," + Result[j].Type.Name + "," + Result[j].Type.ID + "," + Result[j].Symbol.ContractSize + "," + Result[j].SpreaDifferenceInOpenTrade + "," +
                                                    Result[j].Symbol.Currency + "," + Result[j].Comment + "," + Result[j].AgentCommission;

                                                StringResult.Add(Message);
                                            }
                                        }
                                        else
                                        {
                                            string Message = subValue[0] + "$";
                                            StringResult.Add(Message);
                                        }
                                    }
                                    else
                                    {
                                        string Message = subValue[0] + "$MCM005";
                                        StringResult.Add(Message);
                                    }
                                }
                                break;

                            case "SelectOnlineCommandByGroupList":
                                {
                                    bool checkip = Facade.FacadeCheckIpManagerAndAdmin(code, ipAddress);
                                    if (checkip)
                                    { 
                                        List<int> ListGroupID = new List<int>();
                                        int From = -1;
                                        int To = -1;
                                        if (subValue[1].Length > 0)
                                        {
                                            string[] subParameter = subValue[1].Split(',');
                                            if (subParameter.Length > 0)
                                            {
                                                int.TryParse(subParameter[0], out From);
                                                int.TryParse(subParameter[1], out To);

                                                int countParameter = subParameter.Length;
                                                for (int j = 2; j < countParameter; j++)
                                                {
                                                    int InvestorGroupID = -1;
                                                    int.TryParse(subParameter[j], out InvestorGroupID);
                                                    ListGroupID.Add(InvestorGroupID);
                                                }

                                                List<Business.OpenTrade> Result = new List<OpenTrade>();
                                                Result = TradingServer.Facade.FacadeGetOpenTradeByGroupList(From, To, ListGroupID);

                                                if (Result != null && Result.Count > 0)
                                                {
                                                    int countCommand = Result.Count;
                                                    for (int j = 0; j < countCommand; j++)
                                                    {
                                                        string Message = subValue[0] + "$" + Result[j].ClientCode + "," + Result[j].ClosePrice + "," + Result[j].CloseTime + "," +
                                                            Result[j].CommandCode + "," + Result[j].Commission + "," + Result[j].ExpTime + "," + Result[j].ID + "," +
                                                            Result[j].Investor.InvestorID + "," + Result[j].IsClose + "," + Result[j].IsHedged + "," + Result[j].Margin + "," +
                                                            Result[j].MaxDev + "," + Result[j].OpenPrice + "," + Result[j].OpenTime + "," + Result[j].Profit + "," +
                                                            Result[j].Size + "," + Result[j].StopLoss + "," + Result[j].Swap + "," + Result[j].Symbol.Name + "," + Result[j].TakeProfit + "," +
                                                            Result[j].Taxes + "," + Result[j].Type.Name + "," + Result[j].Type.ID + "," + Result[j].Symbol.ContractSize + "," +
                                                            Result[j].SpreaDifferenceInOpenTrade + "," + Result[j].Symbol.Currency + "," + Result[j].Comment + "," + Result[j].AgentCommission + "," +
                                                            Result[j].IsActivePending + "," + Result[j].IsStopLossAndTakeProfit;

                                                        StringResult.Add(Message);
                                                    }
                                                }
                                                else
                                                {
                                                    string Message = subValue[0] + "$";
                                                    StringResult.Add(Message);
                                                }
                                            }
                                        }
                                    }
                                }
                                break;

                            case "GetOnlineCommandByListInvestorCode":
                                {
                                    List<string> listCode = new List<string>();
                                    string[] subParameter = subValue[1].Split(',');
                                    if(subParameter!=null && subParameter.Length>0)
                                    {
                                        int countCode = subParameter.Length;
                                        for (int j = 0; j < countCode; j++)
                                        {
                                            listCode.Add(subParameter[j]);
                                        }
                                    }

                                    List<Business.OpenTrade> result = TradingServer.Facade.FacadeGetOpenTradeByListInvestorCode(listCode);
                                    if (result != null)
                                    {
                                        int countResult = result.Count;
                                        for (int j = 0; j < countResult; j++)
                                        {
                                            string Message = subValue[0] + "$" + result[j].ClientCode + "," + result[j].ClosePrice + "," + result[j].CloseTime + "," +
                                                            result[j].CommandCode + "," + result[j].Commission + "," + result[j].ExpTime + "," + result[j].ID + "," +
                                                            result[j].Investor.InvestorID + "," + result[j].IsClose + "," + result[j].IsHedged + "," + result[j].Margin + "," +
                                                            result[j].MaxDev + "," + result[j].OpenPrice + "," + result[j].OpenTime + "," + result[j].Profit + "," +
                                                            result[j].Size + "," + result[j].StopLoss + "," + result[j].Swap + "," + result[j].Symbol.Name + "," + result[j].TakeProfit + "," +
                                                            result[j].Taxes + "," + result[j].Type.Name + "," + result[j].Type.ID + "," + result[j].Symbol.ContractSize + "," +
                                                            result[j].SpreaDifferenceInOpenTrade + "," + result[j].Symbol.Currency + "," + result[j].Comment + "," + result[j].AgentCommission + "," +
                                                            result[j].Investor.Code;

                                            StringResult.Add(Message);
                                        }
                                    }
                                    else
                                    {
                                        string message = subValue[0] + "$";
                                        StringResult.Add(message);
                                    }
                                }
                                break;
                            #endregion

                            #region Function Class IGroupSymbol
                            case "AddIGroupSymbol":
                                {
                                    bool checkip = Facade.FacadeCheckIpAdmin(code, ipAddress);
                                    if (checkip)
                                    {
                                        int ResultAddNew = -1;
                                        List<Business.IGroupSymbol> Result = new List<IGroupSymbol>();
                                        Result = this.ExtractIGroupSymbol(subValue[1]);
                                        //Call Function Add New IGroup Symbol
                                        if (Result != null)
                                        {
                                            int countResult = Result.Count;
                                            for (int j = 0; j < countResult; j++)
                                            {
                                                ResultAddNew = TradingServer.Facade.FacadeAddNewIGroupSymbol(Result[j].SymbolID, Result[j].InvestorGroupID);

                                                string Message = subValue[0] + "$" + ResultAddNew.ToString() + "," + Result[j].SymbolID + "," + Result[j].InvestorGroupID;
                                                StringResult.Add(Message);
                                            }
                                        }
                                    }
                                    else
                                    {
                                        string Message = subValue[0] + "$MCM005";
                                        StringResult.Add(Message);
                                    }
                                }
                                break;

                            case "SelectIGroupSymbolConfigByIGroupSymbolID":
                                {
                                    if (!string.IsNullOrEmpty(subValue[1]))
                                    {
                                        string[] listParameter = subValue[1].Split(',');
                                        string temp = string.Empty;
                                        if (listParameter.Length > 0)
                                        {
                                            int countParameter = listParameter.Length;
                                            string result = string.Empty;
                                            for (int j = 0; j < countParameter; j++)
                                            {
                                                temp = this.ExtractCommandServer(subValue[0] + "$" + listParameter[j], "", code);
                                                StringResult.Add(temp);
                                            }
                                        }
                                    }
                                }
                                break;
                            #endregion

                            #region Function Class IGroupSecurity
                            case "AddIGroupSecurity":
                                {
                                    bool checkip = Facade.FacadeCheckIpAdmin(code, ipAddress);
                                    if (checkip)
                                    {
                                        if (!string.IsNullOrEmpty(subValue[1]))
                                        {
                                            int ResultAddNew = -1;
                                            List<Business.IGroupSecurity> Result = new List<IGroupSecurity>();
                                            Result = this.ExtractIGroupSecurity(subValue[1]);
                                            if (Result != null)
                                            {
                                                int countResult = Result.Count;
                                                for (int j = 0; j < countResult; j++)
                                                {
                                                    ResultAddNew = TradingServer.Facade.FacadeAddIGroupSecurity(Result[j].InvestorGroupID, Result[j].SecurityID);

                                                    string Message = subValue[0] + "$" + ResultAddNew.ToString() + "," + Result[j].InvestorGroupID + "," + Result[j].SecurityID;
                                                    StringResult.Add(Message);
                                                }
                                            }

                                            //SEND COMMAND TO AGENT SERVER
                                            string strAgent = string.Empty;
                                            if (StringResult != null)
                                            {
                                                int countResult = StringResult.Count;
                                                for (int j = 0; j < countResult; j++)
                                                {
                                                    strAgent += StringResult[j] + "|";
                                                }
                                            }

                                            #region SEND NOTIFY TO AGENT
                                            if (strAgent.EndsWith("|"))
                                                strAgent = strAgent.Remove(strAgent.Length - 1, 1);

                                            Business.AgentNotify newAgentNotify = new AgentNotify();
                                            newAgentNotify.NotifyMessage = strAgent;
                                            TradingServer.Agent.AgentConfig.Instance.AddNotifyToAgent(newAgentNotify);
                                            #endregion
                                        }
                                    }
                                    else
                                    {
                                        string Message = subValue[0] + "$MCM005";
                                        StringResult.Add(Message);
                                    }
                                }
                                break;

                            case "SelectIGroupSecurityConfigByIGroupSecurityID":
                                {
                                    bool checkip = Facade.FacadeCheckIpAdmin(code, ipAddress);
                                    if (checkip)
                                    {
                                        if (!string.IsNullOrEmpty(subValue[1]))
                                        {
                                            string[] listParameter = subValue[1].Split(',');
                                            string temp = string.Empty;
                                            if (listParameter.Length > 0)
                                            {
                                                int countParameter = listParameter.Length;
                                                string result = string.Empty;
                                                for (int j = 0; j < countParameter; j++)
                                                {
                                                    temp = this.ExtractCommandServer(subValue[0] + "$" + listParameter[j], "", code);
                                                    StringResult.Add(temp);
                                                }
                                            }
                                        }
                                    }
                                }
                                break;
                            #endregion

                            #region Function Class Investor Status
                            case "SelectInvestorStatus":
                                {
                                    List<Business.InvestorStatus> Result = new List<InvestorStatus>();
                                    Result = TradingServer.Facade.FacadeGetAllInvestorStatus();

                                    if (Result != null)
                                    {
                                        int countInvestorStatus = Result.Count;
                                        for (int j = 0; j < countInvestorStatus; j++)
                                        {
                                            string Message = subValue[0] + "$" + Result[j].InvestorStatusID + "," + Result[j].Name;

                                            StringResult.Add(Message);
                                        }
                                    }
                                }
                                break;
                            #endregion

                            #region FUNCTION CLASS MARKET
                            case "TickOnline":
                                {
                                    if (Business.Market.SymbolList != null)
                                    {
                                        int countSymbol = Business.Market.SymbolList.Count;
                                        for (int j = 0; j < countSymbol; j++)
                                        {
                                            if (Business.Market.SymbolList[j].TickValue != null && Business.Market.SymbolList[j].TickValue.Ask > 0)
                                            {
                                                string Message = subValue[0] + "$" + Business.Market.SymbolList[j].TickValue.Ask + "," +
                                                    Business.Market.SymbolList[j].TickValue.Bid + "," + Business.Market.SymbolList[j].TickValue.Status + "," +
                                                    Business.Market.SymbolList[j].TickValue.SymbolID + "," + Business.Market.SymbolList[j].TickValue.SymbolName + "," +
                                                    Business.Market.SymbolList[j].TickValue.TickTime;

                                                StringResult.Add(Message);
                                            }
                                        }
                                    }
                                }
                                break;
                            #endregion

                            #region FUNCTION CLASS INVESTOR ACCOUNT LOG
                            case "GetInvestorAccountLogByInvestor":
                                {
                                    List<Business.InvestorAccountLog> Result = new List<InvestorAccountLog>();
                                    int InvestorID = -1;
                                    int.TryParse(subValue[1], out InvestorID);
                                    Result = TradingServer.Facade.FacadeGetInvestorAccountLogByInvestorID(InvestorID);
                                    if (Result != null)
                                    {
                                        int countResult = Result.Count;
                                        for (int j = 0; j < countResult; j++)
                                        {
                                            string Message = subValue[0] + "$" + Result[j].InvestorID + "," + Result[j].Name + "," + Result[j].ID + "," +
                                                Result[j].DealID + "," + Result[j].Date + "," + Result[j].Comment + "," + Result[j].Amount;

                                            StringResult.Add(Message);
                                        }
                                    }
                                }
                                break;
                            #endregion

                            #region FUNCTION CANDLES
                            case "GetCandlesToDay":
                                {
                                    int TimeFrame = 0;
                                    string[] subParameter = subValue[1].Split(',');
                                    List<string> result = new List<string>();
                                    if (subParameter.Length == 2)
                                    {
                                        int.TryParse(subParameter[1], out TimeFrame);
                                        result = ProcessQuoteLibrary.FacadeDataLog.FacadeGetPriceToDay(subParameter[0], TimeFrame);
                                        if (result != null)
                                        {
                                            int countResult = result.Count;
                                            for (int j = 0; j < countResult; j++)
                                            {
                                                string Message = subValue[0] + "$" + result[j];
                                                StringResult.Add(Message);
                                            }
                                        }
                                    }
                                    else
                                    {
                                        string Message = subValue[0] + "$";
                                        StringResult.Add(Message);
                                    }
                                }
                                break;

                            case "GetCandlesInTime":
                                {
                                    bool checkip = Facade.FacadeCheckIpAdmin(code, ipAddress);
                                    if (checkip)
                                    {
                                        int TimeFrame = 0;
                                        DateTime StartTime = new DateTime();
                                        DateTime endTime = new DateTime();
                                        string[] subParameter = subValue[1].Split(',');
                                        List<string> Result = new List<string>();
                                        if (subParameter.Length == 4)
                                        {
                                            int.TryParse(subParameter[1], out TimeFrame);
                                            DateTime.TryParse(subParameter[2], out StartTime);
                                            DateTime.TryParse(subParameter[3], out endTime);
                                            //Result = ProcessQuoteLibrary.FacadeDataLog.FacadeGetPriceInTime(subParameter[0], TimeFrame, StartTime, endTime);
                                            Result = ProcessQuoteLibrary.FacadeDataLog.FacadeGetCandlesStartEndTime(subParameter[0], TimeFrame, StartTime, endTime);
                                            if (Result != null && Result.Count > 0)
                                            {
                                                int countResult = Result.Count;
                                                for (int j = 0; j < countResult; j++)
                                                {
                                                    string Message = subValue[0] + "$" + Result[j];
                                                    StringResult.Add(Message);
                                                }
                                            }
                                            else
                                            {
                                                string Message = subValue[0] + "$";
                                                StringResult.Add(Message);
                                            }
                                        }
                                        else
                                        {
                                            string Message = subValue[0] + "$";
                                            StringResult.Add(Message);
                                        }
                                    }
                                    else
                                    {
                                        string Message = subValue[0] + "$MCM005";
                                        StringResult.Add(Message);
                                    }
                                }
                                break;
                            #endregion

                            #region FUNCTION MARKET CONFIG
                            case "GetMarketConfig":
                                {
                                    bool checkip = Facade.FacadeCheckIpManagerAndAdmin(code, ipAddress);
                                    if (checkip)
                                    {
                                        if (Business.Market.MarketConfig != null)
                                        {
                                            int countMarketConfig = Business.Market.MarketConfig.Count;
                                            for (int j = 0; j < countMarketConfig; j++)
                                            {
                                                string Message = subValue[0] + "$" + Business.Market.MarketConfig[j].ParameterItemID + "," +
                                                    Business.Market.MarketConfig[j].SecondParameterID + "," +
                                                    Business.Market.MarketConfig[j].Code + "," +
                                                    Business.Market.MarketConfig[j].Name + "," +
                                                    Business.Market.MarketConfig[j].BoolValue + "," +
                                                    Business.Market.MarketConfig[j].StringValue + "," +
                                                    Business.Market.MarketConfig[j].NumValue + "," +
                                                    Business.Market.MarketConfig[j].DateValue;

                                                StringResult.Add(Message);
                                            }
                                        }
                                    }
                                }
                                break;
                            #endregion

                            #region FUNCTION ALERT
                            case "SelectAlertByInvestorID":
                                {
                                    string temp = string.Empty;
                                    List<Business.PriceAlert> Result = new List<PriceAlert>();
                                    string[] subParameter = subValue[1].Split(',');
                                    if (subParameter.Length > 0)
                                    {
                                        int Start = int.Parse(subParameter[0]);
                                        int End = int.Parse(subParameter[1]);
                                        int InvestorID = int.Parse(subParameter[2]);
                                        Result = TradingServer.Facade.FacadeGetAlertByInvestorID(InvestorID, Start, End);
                                        if (Result != null && Result.Count > 0)
                                        {
                                            for (int n = 0; n < Result.Count; n++)
                                            {
                                                temp = subValue[0] + "$" + Result[n].ID + "," + Result[n].Symbol + "," + Result[n].Email + "," + Result[n].PhoneNumber
                                                        + "," + Result[n].Value + "," + Result[n].AlertCondition.ToString() + "," + Result[n].AlertAction.ToString() + "," + Result[n].IsEnable
                                                        + "," + Result[n].DateCreate + "," + Result[n].DateActive + "," + Result[n].InvestorID + "," + Result[n].Notification;
                                                StringResult.Add(temp);
                                            }
                                        }
                                        else
                                        {
                                            string Message = subValue[0] + "$";
                                            StringResult.Add(Message);
                                        }
                                    }
                                }
                                break;
                            #endregion

                            #region FUNCTION NEWS
                            case "SelectTopNews":
                                {
                                    string temp = string.Empty;
                                    List<Business.News> Result = new List<News>();
                                    Result = Market.NewsList;
                                    if (Result != null && Result.Count > 0)
                                    {
                                        for (int n = 0; n < Result.Count; n++)
                                        {
                                            temp = subValue[0] + "$" + Result[n].ID + "█" + Result[n].Title + "█" + Result[n].Catetory + "█" + Result[n].DateCreated
                                                    + "█" + Result[n].Body;
                                            StringResult.Add(temp);
                                        }
                                    }
                                    else
                                    {
                                        string Message = subValue[0] + "$";
                                        StringResult.Add(Message);
                                    }
                                }
                                break;
                            #endregion

                            #region FUNCTION ORDER DATA
                            case "GetOrderWithDeal":
                                {
                                    bool checkip = Facade.FacadeCheckIpAdmin(code, ipAddress);
                                    if (checkip)
                                    {
                                        string[] subParameter = subValue[1].Split(',');
                                        if (subParameter.Length == 3)
                                        {
                                            List<Business.OpenTrade> Result = new List<OpenTrade>();

                                            Business.OpenTrade newOpenTrade = new OpenTrade();
                                            newOpenTrade = TradingServer.Facade.FacadeGetHistoryByCommandCode(subParameter[0]);

                                            if (newOpenTrade == null || newOpenTrade.ID <= 0)
                                            {
                                                if (Business.Market.CommandExecutor != null)
                                                {
                                                    int countCommand = Business.Market.CommandExecutor.Count;
                                                    for (int j = 0; j < countCommand; j++)
                                                    {
                                                        if (Business.Market.CommandExecutor[j].CommandCode == subParameter[0])
                                                        {
                                                            newOpenTrade = Business.Market.CommandExecutor[j];
                                                            break;
                                                        }
                                                    }
                                                }
                                            }

                                            if (newOpenTrade != null && newOpenTrade.ID > 0)
                                            {
                                                string symbolName = "";
                                                double contractSize = 0;
                                                string currency = "";
                                                if (newOpenTrade.Symbol != null)
                                                {
                                                    symbolName = newOpenTrade.Symbol.Name;
                                                    contractSize = newOpenTrade.Symbol.ContractSize;
                                                    currency = newOpenTrade.Symbol.Currency;
                                                }

                                                string message = subValue[0] + "$" + newOpenTrade.ClientCode + "," + newOpenTrade.ClosePrice + "," + newOpenTrade.CloseTime + "," +
                                                   newOpenTrade.CommandCode + "," + newOpenTrade.Commission + "," + newOpenTrade.ExpTime + "," + newOpenTrade.ID + "," +
                                                   newOpenTrade.Investor.Code + "," + newOpenTrade.IsClose + "," + newOpenTrade.IsHedged + "," + newOpenTrade.Margin + "," +
                                                   newOpenTrade.MaxDev + "," + newOpenTrade.OpenPrice + "," + newOpenTrade.OpenTime + "," + newOpenTrade.Profit + "," +
                                                   newOpenTrade.Size + "," + newOpenTrade.StopLoss + "," + newOpenTrade.Swap + "," + symbolName + "," + newOpenTrade.TakeProfit + "," +
                                                   newOpenTrade.Taxes + "," + newOpenTrade.Type.Name + "," + newOpenTrade.Type.ID + "," + contractSize + "," +
                                                   currency + "," + newOpenTrade.Taxes + "," + newOpenTrade.Comment + "," + newOpenTrade.AgentCommission + "," + newOpenTrade.Investor.InvestorID;

                                                StringResult.Add(message);
                                            }

                                            if (StringResult.Count == 0)
                                            {
                                                string message = subValue[0] + "$end";
                                                StringResult.Add(message);
                                            }

                                            if (StringResult.Count == 0)
                                            {
                                                string message = subValue[0] + "$end";
                                                StringResult.Add(message);
                                            }
                                        }
                                    }
                                    else
                                    {
                                        string Message = subValue[0] + "$MCM005";
                                        StringResult.Add(Message);
                                    }
                                }
                                break;

                            case "GetAllOrderData":
                                {
                                    bool checkip = Facade.FacadeCheckIpAdmin(code, ipAddress);
                                    if (checkip)
                                    {
                                        string[] subParameter = subValue[1].Split(',');
                                        if (subParameter.Length == 3)
                                        {
                                            List<Business.OpenTrade> Result = new List<OpenTrade>();
                                            int InvestorID = 0;
                                            int Start = 0;
                                            int End = 0;

                                            int.TryParse(subParameter[1], out Start);
                                            int.TryParse(subParameter[2], out End);

                                            int RowNumber = End - Start;

                                            bool isWildcards = this.IsWildCards(subParameter[0]);

                                            if (isWildcards)
                                            {
                                                #region GET ORDER WITH WILDCARDS
                                                //GET ALL ORDER WITH WILDCARDS
                                                Result = TradingServer.Facade.FacadeGetAllHistoryWithStartEnd(RowNumber, Start);

                                                #region START GET ONLINE ORDER
                                                if (Start == 0)
                                                {
                                                    bool isExistsListOrder = false;
                                                    if (Business.Market.TempListOrder != null)
                                                    {
                                                        int countTempListOrder = Business.Market.TempListOrder.Count;
                                                        for (int j = 0; j < countTempListOrder; j++)
                                                        {
                                                            if (Business.Market.TempListOrder[j].AdminCode == code)
                                                            {
                                                                if (Business.Market.CommandExecutor != null)
                                                                {
                                                                    for (int n = 0; n < Business.Market.CommandExecutor.Count; n++)
                                                                    {
                                                                        if (Business.Market.TempListOrder[j].ListOrder == null)
                                                                            Business.Market.TempListOrder[j].ListOrder = new List<OpenTrade>();

                                                                        Business.Market.TempListOrder[j].ListOrder.Add(Business.Market.CommandExecutor[n]);
                                                                    }
                                                                }

                                                                isExistsListOrder = true;

                                                                break;
                                                            }
                                                        }
                                                    }

                                                    if (!isExistsListOrder)
                                                    {
                                                        List<Business.OpenTrade> tempOrder = new List<OpenTrade>();
                                                        if (Business.Market.CommandExecutor != null)
                                                        {
                                                            for (int n = 0; n < Business.Market.CommandExecutor.Count; n++)
                                                            {
                                                                tempOrder.Add(Business.Market.CommandExecutor[n]);
                                                            }
                                                        }

                                                        if (tempOrder != null && tempOrder.Count > 0)
                                                        {
                                                            Business.OrderInvestor newOrderInvestor = new OrderInvestor();
                                                            newOrderInvestor.AdminCode = code;
                                                            newOrderInvestor.ListOrder = tempOrder;

                                                            Business.Market.TempListOrder.Add(newOrderInvestor);
                                                        }
                                                    }
                                                }
                                                #endregion

                                                #region GET ORDER ONLINE
                                                if (Result == null || Result.Count <= 0)
                                                {
                                                    if (Business.Market.TempListOrder != null)
                                                    {
                                                        int countTempOpenTrade = Business.Market.TempListOrder.Count;
                                                        for (int m = 0; m < countTempOpenTrade; m++)
                                                        {
                                                            if (Business.Market.TempListOrder[m].AdminCode == code)
                                                            {
                                                                if (End > Business.Market.TempListOrder[m].ListOrder.Count)
                                                                    End = Business.Market.TempListOrder[m].ListOrder.Count;

                                                                if (Start > End)
                                                                    Start = 0;

                                                                for (int n = Start; n < End; n++)
                                                                {
                                                                    Result.Add(Business.Market.TempListOrder[m].ListOrder[n]);
                                                                    Business.Market.TempListOrder[m].ListOrder.RemoveAt(n);
                                                                    n--;

                                                                    if (End > Business.Market.TempListOrder[m].ListOrder.Count)
                                                                        End = Business.Market.TempListOrder[m].ListOrder.Count;

                                                                    if (Business.Market.TempListOrder[m].ListOrder.Count == 0 ||
                                                                        Business.Market.TempListOrder[m].ListOrder == null)
                                                                    {
                                                                        Business.Market.TempListOrder.RemoveAt(m);
                                                                    }
                                                                }

                                                                break;
                                                            }
                                                        }
                                                    }
                                                }
                                                #endregion

                                                #region GET HISTORY BY INVESTOR ID
                                                if (Result != null && Result.Count > 0)
                                                {
                                                    int countResult = Result.Count;
                                                    for (int j = 0; j < countResult; j++)
                                                    {
                                                        string symbolName = "";
                                                        double contractSize = 0;
                                                        string currency = "";
                                                        if (Result[j].Symbol != null)
                                                        {
                                                            symbolName = Result[j].Symbol.Name;
                                                            contractSize = Result[j].Symbol.ContractSize;
                                                            currency = Result[j].Symbol.Currency;
                                                        }

                                                        string message = subValue[0] + "$" + Result[j].ClientCode + "," + Result[j].ClosePrice + "," + Result[j].CloseTime + "," +
                                                           Result[j].CommandCode + "," + Result[j].Commission + "," + Result[j].ExpTime + "," + Result[j].ID + "," +
                                                           Result[j].Investor.Code + "," + Result[j].IsClose + "," + Result[j].IsHedged + "," + Result[j].Margin + "," +
                                                           Result[j].MaxDev + "," + Result[j].OpenPrice + "," + Result[j].OpenTime + "," + Result[j].Profit + "," +
                                                           Result[j].Size + "," + Result[j].StopLoss + "," + Result[j].Swap + "," + symbolName + "," + Result[j].TakeProfit + "," +
                                                           Result[j].Taxes + "," + Result[j].Type.Name + "," + Result[j].Type.ID + "," + contractSize + "," +
                                                           currency + "," + Result[j].Taxes + "," + Result[j].Comment + "," + Result[j].AgentCommission + "," + Result[j].Investor.InvestorID;

                                                        StringResult.Add(message);
                                                    }
                                                }
                                                #endregion
                                                #endregion
                                            }
                                            else
                                            {
                                                #region SELECT NORMAL
                                                //SELECT NORMAL
                                                InvestorID = TradingServer.Facade.FacadeGetInvestorIDByCode(subParameter[0]);

                                                if (InvestorID != -1)
                                                {
                                                    #region GET HISTORY BY INVESTOR ID
                                                    Result = TradingServer.Facade.FacadeGetHistoryByInvestor(InvestorID, RowNumber, Start);

                                                    #region START GET ONLINE ORDER
                                                    if (Start == 0)
                                                    {
                                                        bool isExists = false;
                                                        if (Business.Market.TempListOrder != null)
                                                        {
                                                            int countTempListOrder = Business.Market.TempListOrder.Count;
                                                            for (int j = 0; j < countTempListOrder; j++)
                                                            {
                                                                if (Business.Market.TempListOrder[j].AdminCode == code)
                                                                {
                                                                    if (Business.Market.CommandExecutor != null)
                                                                    {
                                                                        for (int n = 0; n < Business.Market.CommandExecutor.Count; n++)
                                                                        {
                                                                            if (Business.Market.CommandExecutor[n].Investor.InvestorID == InvestorID)
                                                                            {
                                                                                if (Business.Market.TempListOrder[j].ListOrder == null)
                                                                                    Business.Market.TempListOrder[j].ListOrder = new List<OpenTrade>();

                                                                                Business.Market.TempListOrder[j].ListOrder.Add(Business.Market.CommandExecutor[n]);
                                                                            }
                                                                        }
                                                                    }

                                                                    isExists = true;

                                                                    break;
                                                                }
                                                            }
                                                        }

                                                        if (!isExists)
                                                        {
                                                            List<Business.OpenTrade> tempOrder = new List<OpenTrade>();
                                                            if (Business.Market.CommandExecutor != null)
                                                            {
                                                                for (int n = 0; n < Business.Market.CommandExecutor.Count; n++)
                                                                {
                                                                    if (Business.Market.CommandExecutor[n].Investor.InvestorID == InvestorID)
                                                                    {
                                                                        tempOrder.Add(Business.Market.CommandExecutor[n]);
                                                                    }
                                                                }
                                                            }

                                                            if (tempOrder != null && tempOrder.Count > 0)
                                                            {
                                                                Business.OrderInvestor newOrderInvestor = new OrderInvestor();
                                                                newOrderInvestor.AdminCode = code;
                                                                newOrderInvestor.ListOrder = tempOrder;

                                                                Business.Market.TempListOrder.Add(newOrderInvestor);
                                                            }
                                                        }
                                                    }
                                                    #endregion

                                                    #region GET ORDER ONLINE
                                                    if (Result == null || Result.Count <= 0)
                                                    {
                                                        if (Business.Market.TempListOrder != null)
                                                        {
                                                            int countTempOpenTrade = Business.Market.TempListOrder.Count;
                                                            for (int m = 0; m < countTempOpenTrade; m++)
                                                            {
                                                                if (Business.Market.TempListOrder[m].AdminCode == code)
                                                                {
                                                                    if (End > Business.Market.TempListOrder[m].ListOrder.Count)
                                                                        End = Business.Market.TempListOrder[m].ListOrder.Count;

                                                                    if (Start > End)
                                                                        Start = 0;

                                                                    for (int n = Start; n < End; n++)
                                                                    {
                                                                        Result.Add(Business.Market.TempListOrder[m].ListOrder[n]);
                                                                        Business.Market.TempListOrder[m].ListOrder.RemoveAt(n);
                                                                        n--;

                                                                        if (End > Business.Market.TempListOrder[m].ListOrder.Count)
                                                                            End = Business.Market.TempListOrder[m].ListOrder.Count;

                                                                        if (Business.Market.TempListOrder[m].ListOrder.Count == 0 ||
                                                                            Business.Market.TempListOrder[m].ListOrder == null)
                                                                        {
                                                                            Business.Market.TempListOrder.RemoveAt(m);
                                                                        }
                                                                    }

                                                                    break;
                                                                }
                                                            }
                                                        }
                                                    }
                                                    #endregion

                                                    #region MAP COMMAND TO STRING
                                                    if (Result != null && Result.Count > 0)
                                                    {
                                                        int countResult = Result.Count;
                                                        for (int j = 0; j < countResult; j++)
                                                        {
                                                            string symbolName = "";
                                                            double contractSize = 0;
                                                            string currency = "";
                                                            if (Result[j].Symbol != null)
                                                            {
                                                                symbolName = Result[j].Symbol.Name;
                                                                contractSize = Result[j].Symbol.ContractSize;
                                                                currency = Result[j].Symbol.Currency;
                                                            }

                                                            string message = subValue[0] + "$" + Result[j].ClientCode + "," + Result[j].ClosePrice + "," + Result[j].CloseTime + "," +
                                                               Result[j].CommandCode + "," + Result[j].Commission + "," + Result[j].ExpTime + "," + Result[j].ID + "," +
                                                               Result[j].Investor.Code + "," + Result[j].IsClose + "," + Result[j].IsHedged + "," + Result[j].Margin + "," +
                                                               Result[j].MaxDev + "," + Result[j].OpenPrice + "," + Result[j].OpenTime + "," + Result[j].Profit + "," +
                                                               Result[j].Size + "," + Result[j].StopLoss + "," + Result[j].Swap + "," + symbolName + "," + Result[j].TakeProfit + "," +
                                                               Result[j].Taxes + "," + Result[j].Type.Name + "," + Result[j].Type.ID + "," + contractSize + "," +
                                                               currency + "," + Result[j].Taxes + "," + Result[j].Comment + "," + Result[j].AgentCommission + "," + Result[j].Investor.InvestorID;

                                                            StringResult.Add(message);
                                                        }
                                                    }
                                                    #endregion

                                                    #endregion

                                                    #region GET HISTORY BY CODE(COMMENT)
                                                    //if (StringResult.Count == 0)
                                                    //{
                                                    //    if (Start == 0)
                                                    //    {
                                                    //        Business.OpenTrade newOpenTrade = new OpenTrade();
                                                    //        newOpenTrade = TradingServer.Facade.FacadeGetHistoryByCommandCode(subParameter[0]);

                                                    //        #region GET ORDER ONLINE
                                                    //        if (newOpenTrade == null || newOpenTrade.ID <= 0)
                                                    //        {
                                                    //            if (Business.Market.CommandExecutor != null)
                                                    //            {
                                                    //                for (int j = 0; j < Business.Market.CommandExecutor.Count; j++)
                                                    //                {
                                                    //                    if (Business.Market.CommandExecutor[j].CommandCode == subParameter[0])
                                                    //                    {
                                                    //                        newOpenTrade = Business.Market.CommandExecutor[j];
                                                    //                        break;
                                                    //                    }
                                                    //                }
                                                    //            }
                                                    //        }
                                                    //        #endregion

                                                    //        #region MAP COMMAND TO STRING
                                                    //        if (newOpenTrade != null && newOpenTrade.ID > 0)
                                                    //        {
                                                    //            //string message = subValue[0] + "$" + Result[0].ClientCode + "," + Result[0].ClosePrice + "," + Result[0].CloseTime + "," +
                                                    //            //   Result[0].CommandCode + "," + Result[0].Commission + "," + Result[0].ExpTime + "," + Result[0].ID + "," +
                                                    //            //   Result[0].Investor.Code + "," + Result[0].IsClose + "," + Result[0].IsHedged + "," + Result[0].Margin + "," +
                                                    //            //   Result[0].MaxDev + "," + Result[0].OpenPrice + "," + Result[0].OpenTime + "," + Result[0].Profit + "," +
                                                    //            //   Result[0].Size + "," + Result[0].StopLoss + "," + Result[0].Swap + "," + Result[0].Symbol.Name + "," + Result[0].TakeProfit + "," +
                                                    //            //   Result[0].Taxes + "," + Result[0].Type.Name + "," + Result[0].Type.ID + "," + Result[0].Symbol.ContractSize + "," +
                                                    //            //   Result[0].Symbol.Currency + "," + Result[0].Taxes + "," + Result[0].Comment + "," + Result[0].AgentCommission;

                                                    //            string message = subValue[0] + "$" + newOpenTrade.ClientCode + "," + newOpenTrade.ClosePrice + "," + newOpenTrade.CloseTime + "," +
                                                    //               newOpenTrade.CommandCode + "," + newOpenTrade.Commission + "," + newOpenTrade.ExpTime + "," + newOpenTrade.ID + "," +
                                                    //               newOpenTrade.Investor.Code + "," + newOpenTrade.IsClose + "," + newOpenTrade.IsHedged + "," + newOpenTrade.Margin + "," +
                                                    //               newOpenTrade.MaxDev + "," + newOpenTrade.OpenPrice + "," + newOpenTrade.OpenTime + "," + newOpenTrade.Profit + "," +
                                                    //               newOpenTrade.Size + "," + newOpenTrade.StopLoss + "," + newOpenTrade.Swap + "," + symbolName + "," + newOpenTrade.TakeProfit + "," +
                                                    //               newOpenTrade.Taxes + "," + newOpenTrade.Type.Name + "," + newOpenTrade.Type.ID + "," + contractSize + "," +
                                                    //               currency + "," + newOpenTrade.Taxes + "," + newOpenTrade.Comment + "," + newOpenTrade.AgentCommission;

                                                    //            StringResult.Add(message);
                                                    //        }
                                                    //        #endregion
                                                    //    }
                                                    //    else
                                                    //    {
                                                    //        List<Business.OpenTrade> listOpenTrade = new List<OpenTrade>();
                                                    //        listOpenTrade = TradingServer.Facade.FacadeGetOnlineCommandByInvestorID(InvestorID);
                                                    //        if (listOpenTrade != null && listOpenTrade.Count > 0)
                                                    //        {
                                                    //            int countOpenTrade = listOpenTrade.Count;
                                                    //            if (End > countOpenTrade)
                                                    //                End = countOpenTrade;

                                                    //            if (Start > End)
                                                    //                Start = End;

                                                    //            for (int j = Start; j < End; j++)
                                                    //            {
                                                    //                string message = subValue[0] + "$" + listOpenTrade[j].ClientCode + "," + listOpenTrade[j].ClosePrice + "," +
                                                    //                    listOpenTrade[j].CloseTime + "," + listOpenTrade[j].CommandCode + "," + listOpenTrade[j].Commission + "," +
                                                    //                    listOpenTrade[j].ExpTime + "," + listOpenTrade[j].ID + "," + listOpenTrade[j].Investor.Code + "," +
                                                    //                    listOpenTrade[j].IsClose + "," + listOpenTrade[j].IsHedged + "," + listOpenTrade[j].Margin + "," +
                                                    //                    listOpenTrade[j].MaxDev + "," + listOpenTrade[j].OpenPrice + "," + listOpenTrade[j].OpenTime + "," +
                                                    //                    listOpenTrade[j].Profit + "," + listOpenTrade[j].Size + "," + listOpenTrade[j].StopLoss + "," +
                                                    //                    listOpenTrade[j].Swap + "," + listOpenTrade[j].Symbol.Name + "," + listOpenTrade[j].TakeProfit + "," +
                                                    //                    listOpenTrade[j].Taxes + "," + listOpenTrade[j].Type.Name + "," + listOpenTrade[j].Type.ID + "," +
                                                    //                    listOpenTrade[j].Symbol.ContractSize + "," + listOpenTrade[j].Symbol.Currency + "," +
                                                    //                    listOpenTrade[j].Taxes + "," + listOpenTrade[j].Comment + "," + listOpenTrade[j].AgentCommission;

                                                    //                StringResult.Add(message);
                                                    //            }
                                                    //        }
                                                    //    }
                                                    //}
                                                    #endregion

                                                    if (StringResult.Count == 0)
                                                    {
                                                        string message = subValue[0] + "$end";
                                                        StringResult.Add(message);
                                                    }
                                                }
                                                #endregion
                                            }

                                            if (StringResult.Count == 0)
                                            {
                                                string message = subValue[0] + "$end";
                                                StringResult.Add(message);
                                            }
                                        }
                                    }
                                    else
                                    {
                                        string Message = subValue[0] + "$MCM005";
                                        StringResult.Add(Message);
                                    }
                                }
                                break;

                            case "GetOrderData":
                                {
                                    bool checkip = Facade.FacadeCheckIpAdmin(code, ipAddress);
                                    if (checkip)
                                    {
                                        string[] subParameter = subValue[1].Split(',');
                                        if (subParameter.Length == 3)
                                        {
                                            List<Business.OpenTrade> Result = new List<OpenTrade>();
                                            int InvestorID = 0;
                                            int Start = 0;
                                            int End = 0;

                                            bool isWildcards = this.IsWildCards(subParameter[0]);

                                            if (isWildcards)
                                            {
                                                #region GET ORDER WITH WILDCARDS
                                                int.TryParse(subParameter[1], out Start);
                                                int.TryParse(subParameter[2], out End);

                                                int RowNumber = End - Start;

                                                //GET ALL ORDER WITH WILDCARDS
                                                Result = TradingServer.Facade.FacadeGetAllHistoryWithStartEnd(RowNumber, Start);

                                                #region GET HISTORY BY INVESTOR ID
                                                if (Result != null && Result.Count > 0)
                                                {
                                                    int countResult = Result.Count;
                                                    for (int j = 0; j < countResult; j++)
                                                    {
                                                        string symbolName = "";
                                                        double contractSize = 0;
                                                        string currency = "";
                                                        if (Result[j].Symbol != null)
                                                        {
                                                            symbolName = Result[j].Symbol.Name;
                                                            contractSize = Result[j].Symbol.ContractSize;
                                                            currency = Result[j].Symbol.Currency;
                                                        }

                                                        string message = subValue[0] + "$" + Result[j].ClientCode + "," + Result[j].ClosePrice + "," + Result[j].CloseTime + "," +
                                                           Result[j].CommandCode + "," + Result[j].Commission + "," + Result[j].ExpTime + "," + Result[j].ID + "," +
                                                           Result[j].Investor.Code + "," + Result[j].IsClose + "," + Result[j].IsHedged + "," + Result[j].Margin + "," +
                                                           Result[j].MaxDev + "," + Result[j].OpenPrice + "," + Result[j].OpenTime + "," + Result[j].Profit + "," +
                                                           Result[j].Size + "," + Result[j].StopLoss + "," + Result[j].Swap + "," + symbolName + "," + Result[j].TakeProfit + "," +
                                                           Result[j].Taxes + "," + Result[j].Type.Name + "," + Result[j].Type.ID + "," + contractSize + "," +
                                                           currency + "," + Result[j].Taxes + "," + Result[j].Comment + "," + Result[j].AgentCommission + "," + Result[j].Investor.InvestorID;

                                                        StringResult.Add(message);
                                                    }
                                                }
                                                #endregion
                                                #endregion
                                            }
                                            else
                                            {
                                                //SELECT NORMAL
                                                InvestorID = TradingServer.Facade.FacadeGetInvestorIDByCode(subParameter[0]);

                                                if (InvestorID != -1)
                                                {
                                                    int.TryParse(subParameter[1], out Start);
                                                    int.TryParse(subParameter[2], out End);

                                                    int RowNumber = End - Start;

                                                    #region GET HISTORY BY INVESTOR ID
                                                    Result = TradingServer.Facade.FacadeGetHistoryByInvestor(InvestorID, RowNumber, Start);
                                                    if (Result != null && Result.Count > 0)
                                                    {
                                                        int countResult = Result.Count;
                                                        for (int j = 0; j < countResult; j++)
                                                        {
                                                            string symbolName = "";
                                                            double contractSize = 0;
                                                            string currency = "";
                                                            if (Result[j].Symbol != null)
                                                            {
                                                                symbolName = Result[j].Symbol.Name;
                                                                contractSize = Result[j].Symbol.ContractSize;
                                                                currency = Result[j].Symbol.Currency;
                                                            }

                                                            string message = subValue[0] + "$" + Result[j].ClientCode + "," + Result[j].ClosePrice + "," + Result[j].CloseTime + "," +
                                                               Result[j].CommandCode + "," + Result[j].Commission + "," + Result[j].ExpTime + "," + Result[j].ID + "," +
                                                               Result[j].Investor.Code + "," + Result[j].IsClose + "," + Result[j].IsHedged + "," + Result[j].Margin + "," +
                                                               Result[j].MaxDev + "," + Result[j].OpenPrice + "," + Result[j].OpenTime + "," + Result[j].Profit + "," +
                                                               Result[j].Size + "," + Result[j].StopLoss + "," + Result[j].Swap + "," + symbolName + "," + Result[j].TakeProfit + "," +
                                                               Result[j].Taxes + "," + Result[j].Type.Name + "," + Result[j].Type.ID + "," + contractSize + "," +
                                                               currency + "," + Result[j].Taxes + "," + Result[j].Comment + "," + Result[j].AgentCommission + "," + Result[j].Investor.InvestorID;

                                                            StringResult.Add(message);
                                                        }
                                                    }
                                                    #endregion

                                                    #region GET HISTORY BY CODE(COMMENT CODE)
                                                    //if (StringResult.Count == 0)
                                                    //{
                                                    //    if (Start == 0)
                                                    //    {
                                                    //        Business.OpenTrade newOpenTrade = new OpenTrade();
                                                    //        newOpenTrade = TradingServer.Facade.FacadeGetHistoryByCommandCode(subParameter[0]);
                                                    //        if (newOpenTrade != null && newOpenTrade.ID > 0)
                                                    //        {
                                                    //            string message = subValue[0] + "$" + Result[0].ClientCode + "," + Result[0].ClosePrice + "," + Result[0].CloseTime + "," +
                                                    //               Result[0].CommandCode + "," + Result[0].Commission + "," + Result[0].ExpTime + "," + Result[0].ID + "," +
                                                    //               Result[0].Investor.Code + "," + Result[0].IsClose + "," + Result[0].IsHedged + "," + Result[0].Margin + "," +
                                                    //               Result[0].MaxDev + "," + Result[0].OpenPrice + "," + Result[0].OpenTime + "," + Result[0].Profit + "," +
                                                    //               Result[0].Size + "," + Result[0].StopLoss + "," + Result[0].Swap + "," + Result[0].Symbol.Name + "," + Result[0].TakeProfit + "," +
                                                    //               Result[0].Taxes + "," + Result[0].Type.Name + "," + Result[0].Type.ID + "," + Result[0].Symbol.ContractSize + "," +
                                                    //               Result[0].Symbol.Currency + "," + Result[0].Taxes + "," + Result[0].Comment + "," + Result[0].AgentCommission;

                                                    //            StringResult.Add(message);
                                                    //        }
                                                    //    }
                                                    //    else
                                                    //    {
                                                    //        List<Business.OpenTrade> listOpenTrade = new List<OpenTrade>();
                                                    //        listOpenTrade = TradingServer.Facade.FacadeGetOnlineCommandByInvestorID(InvestorID);
                                                    //        if (listOpenTrade != null && listOpenTrade.Count > 0)
                                                    //        {
                                                    //            int countOpenTrade = listOpenTrade.Count;
                                                    //            if (End > countOpenTrade)
                                                    //                End = countOpenTrade;

                                                    //            for (int j = Start; j < End; j++)
                                                    //            {
                                                    //                string message = subValue[0] + "$" + listOpenTrade[j].ClientCode + "," + listOpenTrade[j].ClosePrice + "," +
                                                    //                    listOpenTrade[j].CloseTime + "," + listOpenTrade[j].CommandCode + "," + listOpenTrade[j].Commission + "," +
                                                    //                    listOpenTrade[j].ExpTime + "," + listOpenTrade[j].ID + "," + listOpenTrade[j].Investor.Code + "," +
                                                    //                    listOpenTrade[j].IsClose + "," + listOpenTrade[j].IsHedged + "," + listOpenTrade[j].Margin + "," +
                                                    //                    listOpenTrade[j].MaxDev + "," + listOpenTrade[j].OpenPrice + "," + listOpenTrade[j].OpenTime + "," +
                                                    //                    listOpenTrade[j].Profit + "," + listOpenTrade[j].Size + "," + listOpenTrade[j].StopLoss + "," +
                                                    //                    listOpenTrade[j].Swap + "," + listOpenTrade[j].Symbol.Name + "," + listOpenTrade[j].TakeProfit + "," +
                                                    //                    listOpenTrade[j].Taxes + "," + listOpenTrade[j].Type.Name + "," + listOpenTrade[j].Type.ID + "," +
                                                    //                    listOpenTrade[j].Symbol.ContractSize + "," + listOpenTrade[j].Symbol.Currency + "," +
                                                    //                    listOpenTrade[j].Taxes + "," + listOpenTrade[j].Comment + "," + listOpenTrade[j].AgentCommission;

                                                    //                StringResult.Add(message);
                                                    //            }
                                                    //        }
                                                    //    }
                                                    //}
                                                    #endregion

                                                    if (StringResult.Count == 0)
                                                    {
                                                        string message = subValue[0] + "$end";
                                                        StringResult.Add(message);
                                                    }
                                                }
                                            }


                                            if (StringResult.Count == 0)
                                            {
                                                string message = subValue[0] + "$end";
                                                StringResult.Add(message);
                                            }
                                        }
                                    }
                                    else
                                    {
                                        string message = subValue[0] + "$MCM005";
                                        StringResult.Add(message);
                                    }
                                }
                                break;

                            case "GetOrderOpenOnly":
                                {
                                    bool checkip = Facade.FacadeCheckIpAdmin(code, ipAddress);
                                    if (checkip)
                                    {
                                        string[] subParameter = subValue[1].Split(',');
                                        if (subParameter.Length == 3)
                                        {
                                            int start = 0;
                                            int end = 0;

                                            int.TryParse(subParameter[1], out start);
                                            int.TryParse(subParameter[2], out end);

                                            List<Business.OpenTrade> tempResult = new List<OpenTrade>();

                                            bool isWildCards = this.IsWildCards(subParameter[0]);

                                            if (isWildCards)
                                            {
                                                #region SEARCH WITH WILDCARDS
                                                if (Business.Market.CommandExecutor != null)
                                                {
                                                    int countCommand = Business.Market.CommandExecutor.Count;
                                                    for (int j = 0; j < countCommand; j++)
                                                    {
                                                        tempResult.Add(Business.Market.CommandExecutor[j]);
                                                    }
                                                }

                                                if (tempResult.Count > 0 && tempResult != null)
                                                {
                                                    int countTempResult = tempResult.Count;
                                                    if (countTempResult < end)
                                                        end = countTempResult;
                                                    for (int j = start; j < end; j++)
                                                    {
                                                        string message = subValue[0] + "$" + tempResult[j].ClientCode + "," + tempResult[j].ClosePrice + "," + tempResult[j].CloseTime + "," +
                                                               tempResult[j].CommandCode + "," + tempResult[j].Commission + "," + tempResult[j].ExpTime + "," + tempResult[j].ID + "," +
                                                               tempResult[j].Investor.Code + "," + tempResult[j].IsClose + "," + tempResult[j].IsHedged + "," + tempResult[j].Margin + "," +
                                                               tempResult[j].MaxDev + "," + tempResult[j].OpenPrice + "," + tempResult[j].OpenTime + "," + tempResult[j].Profit + "," +
                                                               tempResult[j].Size + "," + tempResult[j].StopLoss + "," + tempResult[j].Swap + "," + tempResult[j].Symbol.Name + "," + tempResult[j].TakeProfit + "," +
                                                               tempResult[j].Taxes + "," + tempResult[j].Type.Name + "," + tempResult[j].Type.ID + "," + tempResult[j].Symbol.ContractSize + "," +
                                                               tempResult[j].Symbol.Currency + "," + tempResult[j].Taxes + "," + tempResult[j].Comment + "," + tempResult[j].AgentCommission + "," +
                                                               tempResult[j].Investor.InvestorID;

                                                        StringResult.Add(message);
                                                    }
                                                }
                                                #endregion
                                            }
                                            else
                                            {
                                                #region SEARCH IN COMMAND EXECUTOR WITH SEARCH VALUE IS INVESTOR ID
                                                if (Business.Market.CommandExecutor != null)
                                                {
                                                    int countCommand = Business.Market.CommandExecutor.Count;
                                                    for (int j = 0; j < countCommand; j++)
                                                    {
                                                        if (Business.Market.CommandExecutor[j].Investor.Code.ToUpper() == subParameter[0].ToUpper())
                                                        {
                                                            tempResult.Add(Business.Market.CommandExecutor[j]);
                                                        }
                                                    }
                                                }
                                                #endregion

                                                #region IF TEMP COUNT RESULT <=0 THEN FIND WITH SEARCH VALUE IS COMMAND CODE
                                                if (tempResult.Count <= 0)
                                                {
                                                    if (Business.Market.CommandExecutor != null)
                                                    {
                                                        int countCommand = Business.Market.CommandExecutor.Count;
                                                        for (int j = 0; j < countCommand; j++)
                                                        {
                                                            if (Business.Market.CommandExecutor[j].CommandCode == subParameter[0])
                                                            {
                                                                tempResult.Add(Business.Market.CommandExecutor[j]);
                                                                break;
                                                            }
                                                        }
                                                    }
                                                }
                                                #endregion

                                                if (tempResult.Count > 0 && tempResult != null)
                                                {
                                                    int countTempResult = tempResult.Count;
                                                    if (countTempResult < end)
                                                        end = countTempResult;
                                                    for (int j = start; j < end; j++)
                                                    {
                                                        string message = subValue[0] + "$" + tempResult[j].ClientCode + "," + tempResult[j].ClosePrice + "," + tempResult[j].CloseTime + "," +
                                                               tempResult[j].CommandCode + "," + tempResult[j].Commission + "," + tempResult[j].ExpTime + "," + tempResult[j].ID + "," +
                                                               tempResult[j].Investor.Code + "," + tempResult[j].IsClose + "," + tempResult[j].IsHedged + "," + tempResult[j].Margin + "," +
                                                               tempResult[j].MaxDev + "," + tempResult[j].OpenPrice + "," + tempResult[j].OpenTime + "," + tempResult[j].Profit + "," +
                                                               tempResult[j].Size + "," + tempResult[j].StopLoss + "," + tempResult[j].Swap + "," + tempResult[j].Symbol.Name + "," + tempResult[j].TakeProfit + "," +
                                                               tempResult[j].Taxes + "," + tempResult[j].Type.Name + "," + tempResult[j].Type.ID + "," + tempResult[j].Symbol.ContractSize + "," +
                                                               tempResult[j].Symbol.Currency + "," + tempResult[j].Taxes + "," + tempResult[j].Comment + "," + tempResult[j].AgentCommission + "," +
                                                               tempResult[j].Investor.InvestorID;

                                                        StringResult.Add(message);
                                                    }
                                                }
                                            }

                                            if (StringResult.Count == 0)
                                            {
                                                string message = subValue[0] + "$end";
                                                StringResult.Add(message);
                                            }

                                            #region INSERT SYSTEM LOG
                                            //INSERT SYSTEM LOG
                                            string content = "'" + code + "': " + StringResult.Count + " orders have been request";
                                            string comment = "[request order open only]";
                                            TradingServer.Facade.FacadeAddNewSystemLog(3, content, comment, ipAddress, code);
                                            #endregion
                                        }
                                    }
                                    else
                                    {
                                        string Message = subValue[0] + "$MCM005";
                                        StringResult.Add(Message);
                                    }
                                }
                                break;

                            //GET ORDER IN DATABASE WITH TABLE COMMAND HISTORY 
                            case "GetOrderDataWithTime":
                                {
                                    bool checkip = Facade.FacadeCheckIpManagerAndAdmin(code, ipAddress);
                                    if (checkip)
                                    {
                                        string[] subParameter = subValue[1].Split(',');
                                        if (subParameter.Length == 6)
                                        {
                                            int from = 0;
                                            int to = 0;
                                            int investorID = 0;
                                            int ManagerID = 0;
                                            DateTime timeStart;
                                            DateTime timeEnd;

                                            int.TryParse(subParameter[0], out from);
                                            int.TryParse(subParameter[1], out to);
                                            int.TryParse(subParameter[2], out investorID);
                                            int.TryParse(subParameter[3], out ManagerID);
                                            DateTime.TryParse(subParameter[4], out timeStart);
                                            DateTime.TryParse(subParameter[5], out timeEnd);

                                            List<Business.OpenTrade> Result = new List<OpenTrade>();

                                            #region FIRST GET DATA HISTORY OF INVESTOR
                                            if (Business.Market.AgentList != null)
                                            {
                                                int countAgent = Business.Market.AgentList.Count;
                                                bool isExists = false;
                                                for (int j = 0; j < countAgent; j++)
                                                {   
                                                    if (Business.Market.AgentList[j].AgentID == ManagerID)
                                                    {
                                                        if (from == 0)
                                                        {
                                                            #region REMOVE LIST HISTORY OF AGENT
                                                            if (Business.Market.AgentList[j].ListHistoryInvestor != null &&
                                                                Business.Market.AgentList[j].ListHistoryInvestor.Count > 0)
                                                            {
                                                                int countHistory = Business.Market.AgentList[j].ListHistoryInvestor.Count;
                                                                for (int n = 0; n < countHistory; n++)
                                                                {
                                                                    Business.Market.AgentList[j].ListHistoryInvestor.RemoveAt(n);
                                                                }
                                                            }
                                                            #endregion

                                                            #region GET HISTORY IN DATABASE AND ADD TO LIST HISTORY OF AGENT
                                                            Result = TradingServer.Facade.FacadeGetCommandHistoryWithStarLimit(investorID, ManagerID, timeStart, timeEnd);

                                                            if (Result != null && Result.Count > 0)
                                                            {
                                                                int countResult = Result.Count;

                                                                if (Business.Market.AgentList[j].ListHistoryInvestor == null)
                                                                    Business.Market.AgentList[j].ListHistoryInvestor = new List<OpenTrade>();

                                                                for (int m = 0; m < countResult; m++)
                                                                {
                                                                    Business.Market.AgentList[j].ListHistoryInvestor.Add(Result[m]);
                                                                }
                                                            }
                                                            #endregion
                                                        }

                                                        int rowNumber = to - from;
                                                        if (Business.Market.AgentList[j].ListHistoryInvestor == null)
                                                            Business.Market.AgentList[j].ListHistoryInvestor = new List<OpenTrade>();

                                                        if (Business.Market.AgentList[j].ListHistoryInvestor.Count < rowNumber)
                                                            rowNumber = Business.Market.AgentList[j].ListHistoryInvestor.Count;

                                                        for (int n = 0; n < rowNumber; n++)
                                                        {
                                                            string symbolName = string.Empty;
                                                            string currency = string.Empty;
                                                            string typeName = string.Empty;

                                                            if (Business.Market.AgentList[j].ListHistoryInvestor[0].Symbol != null)
                                                            {
                                                                symbolName = Business.Market.AgentList[j].ListHistoryInvestor[0].Symbol.Name;
                                                                currency = Business.Market.AgentList[j].ListHistoryInvestor[0].Symbol.Currency;
                                                            }

                                                            if (Business.Market.AgentList[j].ListHistoryInvestor[0].Type.ID == 13 ||
                                                                Business.Market.AgentList[j].ListHistoryInvestor[0].Type.ID == 14 ||
                                                                Business.Market.AgentList[j].ListHistoryInvestor[0].Type.ID == 15 ||
                                                                Business.Market.AgentList[j].ListHistoryInvestor[0].Type.ID == 16)
                                                            {
                                                                Business.Market.AgentList[j].ListHistoryInvestor[0].CloseTime = DateTime.MinValue;
                                                            }

                                                            #region MAP STRING
                                                            string message = subValue[0] + "$" + Business.Market.AgentList[j].ListHistoryInvestor[0].ClientCode + "," +
                                                                Business.Market.AgentList[j].ListHistoryInvestor[0].ClosePrice + "," +
                                                                Business.Market.AgentList[j].ListHistoryInvestor[0].CloseTime + "," +
                                                                Business.Market.AgentList[j].ListHistoryInvestor[0].CommandCode + "," +
                                                                Business.Market.AgentList[j].ListHistoryInvestor[0].Commission + "," +
                                                                Business.Market.AgentList[j].ListHistoryInvestor[0].ExpTime + "," +
                                                                Business.Market.AgentList[j].ListHistoryInvestor[0].ID + "," +
                                                                Business.Market.AgentList[j].ListHistoryInvestor[0].Investor.InvestorID + "," +
                                                                true + "," + false + "," + "0,0" + "," +
                                                                Business.Market.AgentList[j].ListHistoryInvestor[0].OpenPrice + "," +
                                                                Business.Market.AgentList[j].ListHistoryInvestor[0].OpenTime + "," +
                                                                Business.Market.AgentList[j].ListHistoryInvestor[0].Profit + "," +
                                                                Business.Market.AgentList[j].ListHistoryInvestor[0].Size + "," +
                                                                Business.Market.AgentList[j].ListHistoryInvestor[0].StopLoss + "," +
                                                                Business.Market.AgentList[j].ListHistoryInvestor[0].Swap + "," + symbolName + "," +
                                                                Business.Market.AgentList[j].ListHistoryInvestor[0].TakeProfit + "," +
                                                                Business.Market.AgentList[j].ListHistoryInvestor[0].Taxes + "," +
                                                                Business.Market.AgentList[j].ListHistoryInvestor[0].Type.Name + "," +
                                                                Business.Market.AgentList[j].ListHistoryInvestor[0].Type.ID + ",-1,-1" + "," + currency + "," +
                                                                Business.Market.AgentList[j].ListHistoryInvestor[0].Comment + "," +
                                                                Business.Market.AgentList[j].ListHistoryInvestor[0].AgentCommission + "," +
                                                                Business.Market.AgentList[j].ListHistoryInvestor[0].Investor.InvestorID;


                                                            StringResult.Add(message);
                                                            #endregion

                                                            Business.Market.AgentList[j].ListHistoryInvestor.RemoveAt(0);
                                                        }

                                                        isExists = true;
                                                        break;
                                                    }
                                                }

                                                if (!isExists)
                                                {
                                                    int countAdminList = Business.Market.AdminList.Count;
                                                    for (int j = 0; j < countAdminList; j++)
                                                    {
                                                        if (Business.Market.AdminList[j].AgentID == ManagerID)
                                                        {
                                                            if (from == 0)
                                                            {
                                                                #region REMOVE LIST HISTORY OF AGENT
                                                                if (Business.Market.AdminList[j].ListHistoryInvestor != null &&
                                                                    Business.Market.AdminList[j].ListHistoryInvestor.Count > 0)
                                                                    Business.Market.AdminList[j].ListHistoryInvestor.Clear();
                                                                
                                                                #endregion

                                                                #region GET HISTORY IN DATABASE AND ADD TO LIST HISTORY OF AGENT
                                                                Result = TradingServer.Facade.FacadeGetCommandHistoryWithStarLimit(investorID, ManagerID, timeStart, timeEnd);

                                                                if (Result != null && Result.Count > 0)
                                                                {
                                                                    int countResult = Result.Count;

                                                                    if (Business.Market.AdminList[j].ListHistoryInvestor == null)
                                                                        Business.Market.AdminList[j].ListHistoryInvestor = new List<OpenTrade>();

                                                                    for (int m = 0; m < countResult; m++)
                                                                    {
                                                                        Business.Market.AdminList[j].ListHistoryInvestor.Add(Result[m]);
                                                                    }
                                                                }
                                                                #endregion
                                                            }

                                                            int rowNumber = to - from;
                                                            if (Business.Market.AdminList[j].ListHistoryInvestor == null)
                                                                Business.Market.AdminList[j].ListHistoryInvestor = new List<OpenTrade>();

                                                            if (Business.Market.AdminList[j].ListHistoryInvestor.Count < rowNumber)
                                                                rowNumber = Business.Market.AdminList[j].ListHistoryInvestor.Count;

                                                            for (int n = 0; n < rowNumber; n++)
                                                            {
                                                                string symbolName = string.Empty;
                                                                string currency = string.Empty;
                                                                string typeName = string.Empty;

                                                                if (Business.Market.AdminList[j].ListHistoryInvestor[0].Symbol != null)
                                                                {
                                                                    symbolName = Business.Market.AdminList[j].ListHistoryInvestor[0].Symbol.Name;
                                                                    currency = Business.Market.AdminList[j].ListHistoryInvestor[0].Symbol.Currency;
                                                                }

                                                                if (Business.Market.AdminList[j].ListHistoryInvestor[0].Type.ID == 13 ||
                                                                    Business.Market.AdminList[j].ListHistoryInvestor[0].Type.ID == 14 ||
                                                                    Business.Market.AdminList[j].ListHistoryInvestor[0].Type.ID == 15 ||
                                                                    Business.Market.AdminList[j].ListHistoryInvestor[0].Type.ID == 16)
                                                                {
                                                                    Business.Market.AdminList[j].ListHistoryInvestor[0].CloseTime = DateTime.MinValue;
                                                                }

                                                                #region MAP STRING
                                                                string message = subValue[0] + "$" + Business.Market.AdminList[j].ListHistoryInvestor[0].ClientCode + "," +
                                                                    Business.Market.AdminList[j].ListHistoryInvestor[0].ClosePrice + "," +
                                                                    Business.Market.AdminList[j].ListHistoryInvestor[0].CloseTime + "," +
                                                                    Business.Market.AdminList[j].ListHistoryInvestor[0].CommandCode + "," +
                                                                    Business.Market.AdminList[j].ListHistoryInvestor[0].Commission + "," +
                                                                    Business.Market.AdminList[j].ListHistoryInvestor[0].ExpTime + "," +
                                                                    Business.Market.AdminList[j].ListHistoryInvestor[0].ID + "," +
                                                                    Business.Market.AdminList[j].ListHistoryInvestor[0].Investor.InvestorID + "," +
                                                                    true + "," + false + "," + "0,0" + "," +
                                                                    Business.Market.AdminList[j].ListHistoryInvestor[0].OpenPrice + "," +
                                                                    Business.Market.AdminList[j].ListHistoryInvestor[0].OpenTime + "," +
                                                                    Business.Market.AdminList[j].ListHistoryInvestor[0].Profit + "," +
                                                                    Business.Market.AdminList[j].ListHistoryInvestor[0].Size + "," +
                                                                    Business.Market.AdminList[j].ListHistoryInvestor[0].StopLoss + "," +
                                                                    Business.Market.AdminList[j].ListHistoryInvestor[0].Swap + "," + symbolName + "," +
                                                                    Business.Market.AdminList[j].ListHistoryInvestor[0].TakeProfit + "," +
                                                                    Business.Market.AdminList[j].ListHistoryInvestor[0].Taxes + "," +
                                                                    Business.Market.AdminList[j].ListHistoryInvestor[0].Type.Name + "," +
                                                                    Business.Market.AdminList[j].ListHistoryInvestor[0].Type.ID + ",-1,-1" + "," + currency + "," +
                                                                    Business.Market.AdminList[j].ListHistoryInvestor[0].Comment + "," +
                                                                    Business.Market.AdminList[j].ListHistoryInvestor[0].AgentCommission + "," +
                                                                    Business.Market.AdminList[j].ListHistoryInvestor[0].Investor.InvestorID;

                                                                StringResult.Add(message);
                                                                #endregion

                                                                Business.Market.AdminList[j].ListHistoryInvestor.RemoveAt(0);
                                                            }

                                                            break;
                                                        }
                                                    }
                                                }
                                            }
                                            #endregion
                                        }

                                        if (StringResult.Count == 0)
                                        {
                                            string message = subValue[0] + "$";
                                            StringResult.Add(message);
                                        }

                                        #region INSERT SYSTEM LOG
                                        //INSERT SYSTEM LOG
                                        string content = "'" + code + "': " + StringResult.Count + " orders have been request";
                                        string comment = "[request order]";
                                        TradingServer.Facade.FacadeAddNewSystemLog(3, content, comment, ipAddress, code);
                                        #endregion
                                    }
                                    else
                                    {
                                        string message = subValue[0] + "$MCM005";
                                        StringResult.Add(message);
                                    }
                                }
                                break;

                            case "GetDepositInvestor":
                                {
                                    string[] subParameter = subValue[1].Split('{');
                                    List<int> listInvestor = new List<int>();
                                    int countInvestor = subParameter.Length;
                                    for (int j = 0; j < countInvestor; j++)
                                    {
                                        listInvestor.Add(int.Parse(subParameter[j]));
                                    }

                                    Dictionary<int, double> result = Facade.FacadeGetDepositByListInvestorID(listInvestor);

                                    if (result != null)
                                    {
                                        foreach (KeyValuePair<int, double> d in result)
                                        {
                                            string temp = subValue[0] + "$" + d.Key + "{" + d.Value;
                                            StringResult.Add(temp);
                                        }
                                    }
                                }
                                break;

                            case "GetTopOpenClosePosition":
                                {
                                    bool checkip = Facade.FacadeCheckIpManagerAndAdmin(code, ipAddress);
                                    if (checkip)
                                    {
                                        string[] subParameter = subValue[1].Split('|');
                                        if (subParameter.Length == 2)
                                        {
                                            List<Business.OpenTrade> listOpenPosition = new List<OpenTrade>();
                                            List<Business.OpenTrade> listClosePosition = new List<OpenTrade>();
                                            List<Business.OpenTrade> listAllPosition = new List<OpenTrade>();
                                            List<int> listInvestorID = new List<int>();
                                            int numberGet = int.Parse(subParameter[0]);
                                            string[] subListGroup = subParameter[1].Split(',');

                                            #region GET OPEN POSITION INVESTOR AND GET INVESTOR ID
                                            if (subListGroup != null && subListGroup.Length > 0)
                                            {
                                                int countSubGroupt = subListGroup.Length;
                                                for (int j = 0; j < countSubGroupt; j++)
                                                {
                                                    int grouptID = int.Parse(subListGroup[j]);

                                                    if (Business.Market.InvestorList != null)
                                                    {
                                                        int countGroupt = Business.Market.InvestorList.Count;
                                                        for (int k = 0; k < countGroupt; k++)
                                                        {
                                                            if (Business.Market.InvestorList[k].InvestorGroupInstance.InvestorGroupID == grouptID)
                                                            {
                                                                if (Business.Market.InvestorList[k].CommandList != null)
                                                                {
                                                                    int countCommand = Business.Market.InvestorList[k].CommandList.Count;
                                                                    for (int n = 0; n < countCommand; n++)
                                                                    {
                                                                        bool isPending = Model.TradingCalculate.Instance.CheckIsPendingPosition(Business.Market.InvestorList[k].CommandList[n].Type.ID);
                                                                        if (!isPending)
                                                                            listOpenPosition.Add(Business.Market.InvestorList[k].CommandList[n]);
                                                                    }
                                                                }

                                                                listInvestorID.Add(Business.Market.InvestorList[k].InvestorID);
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                            else
                                            {
                                                StringResult.Add(subValue[0] + "$IVC0002");
                                            }
                                            #endregion

                                            #region GET CLOSE POSITION AND MAP COMMAND TO CLIENT
                                            listClosePosition = TradingServer.Facade.FacadeGetTopClosePosition(numberGet, listInvestorID);

                                            if (listClosePosition != null)
                                                listAllPosition = listClosePosition;
                                            
                                            if (listOpenPosition != null && listOpenPosition.Count > 0)
                                            {
                                                listOpenPosition = listOpenPosition.OrderByDescending(o => o.OpenTime).ToList();

                                                int countGet = 0;
                                                if (numberGet > listOpenPosition.Count)
                                                    countGet = listOpenPosition.Count;
                                                else
                                                    countGet = numberGet;

                                                for (int j = 0; j < countGet; j++)
                                                {
                                                    listOpenPosition[j].IsClose = false;

                                                    listAllPosition.Add(listOpenPosition[j]);   
                                                }
                                            }
                                            #endregion

                                            #region SORT POSITION
                                            if (listAllPosition != null)
                                            {
                                                int countPosition = listAllPosition.Count;
                                                for (int j = 0; j < countPosition; j++)
                                                {
                                                    for (int n = j + 1; n < countPosition - 1; n++)
                                                    {
                                                        if (listAllPosition[j].IsClose)
                                                        {
                                                            if (listAllPosition[n].IsClose)
                                                            {
                                                                TimeSpan span = listAllPosition[j].CloseTime - listAllPosition[n].CloseTime;
                                                                if (span.TotalSeconds > 0)
                                                                {
                                                                    Business.OpenTrade temp = new OpenTrade();
                                                                    temp = listAllPosition[n];
                                                                    listAllPosition[n] = listAllPosition[j];
                                                                    listAllPosition[j] = temp;
                                                                }
                                                            }
                                                            else
                                                            {
                                                                TimeSpan span = listAllPosition[j].CloseTime - listAllPosition[n].OpenTime;
                                                                if (span.TotalSeconds > 0)
                                                                {
                                                                    Business.OpenTrade temp = new OpenTrade();
                                                                    temp = listAllPosition[n];
                                                                    listAllPosition[n] = listAllPosition[j];
                                                                    listAllPosition[j] = temp;
                                                                }
                                                            }
                                                        }
                                                        else
                                                        {
                                                            if (listAllPosition[n].IsClose)
                                                            {
                                                                TimeSpan span = listAllPosition[j].OpenTime - listAllPosition[n].CloseTime;
                                                                if (span.TotalSeconds > 0)
                                                                {
                                                                    Business.OpenTrade temp = new OpenTrade();
                                                                    temp = listAllPosition[n];
                                                                    listAllPosition[n] = listAllPosition[j];
                                                                    listAllPosition[j] = temp;
                                                                }
                                                            }
                                                            else
                                                            {
                                                                TimeSpan span = listAllPosition[j].OpenTime - listAllPosition[n].OpenTime;
                                                                if (span.TotalSeconds > 0)
                                                                {
                                                                    Business.OpenTrade temp = new OpenTrade();
                                                                    temp = listAllPosition[n];
                                                                    listAllPosition[n] = listAllPosition[j];
                                                                    listAllPosition[j] = temp;
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                            #endregion

                                            if (listAllPosition != null && listAllPosition.Count > 0)
                                            {
                                                int countGet = int.Parse(subParameter[0]);
                                                if (countGet > listAllPosition.Count)
                                                    countGet = listAllPosition.Count;

                                                for (int j = 0; j < countGet; j++)
                                                {
                                                    string message = subValue[0] + "$" + listAllPosition[j].ClientCode + "," +
                                                            listAllPosition[j].ClosePrice + "," + listAllPosition[j].CloseTime + "," +
                                                            listAllPosition[j].CommandCode + "," + listAllPosition[j].Commission + "," +
                                                            listAllPosition[j].ExpTime + "," + listAllPosition[j].ID + "," +
                                                            listAllPosition[j].Investor.InvestorID + "," + listAllPosition[j].IsClose + "," +
                                                            listAllPosition[j].IsHedged + "," + listAllPosition[j].Margin + "," +
                                                            listAllPosition[j].MaxDev + "," + listAllPosition[j].OpenPrice + "," +
                                                            listAllPosition[j].OpenTime + "," + listAllPosition[j].Profit + "," +
                                                            listAllPosition[j].Size + "," + listAllPosition[j].StopLoss + "," +
                                                            listAllPosition[j].Swap + "," + listAllPosition[j].Symbol.Name + "," +
                                                            listAllPosition[j].TakeProfit + "," + listAllPosition[j].Taxes + "," +
                                                            listAllPosition[j].Type.Name + "," + listAllPosition[j].Type.ID + "," +
                                                            listAllPosition[j].Symbol.ContractSize + "," + listAllPosition[j].SpreaDifferenceInOpenTrade + "," +
                                                            listAllPosition[j].Symbol.Currency + "," + listAllPosition[j].Comment + "," +
                                                            listAllPosition[j].AgentCommission + "," + listAllPosition[j].IsActivePending + "," +
                                                            listAllPosition[j].IsStopLossAndTakeProfit;

                                                    StringResult.Add(message);
                                                }
                                            }
                                        }
                                        else
                                        {
                                            StringResult.Add(subValue[0] + "$IVC0001");
                                        }
                                    }
                                }
                                break;
                            #endregion

                            #region FUNCTION REPORT
                            case "GetCommandHistoryWithGroupList":
                                {
                                    bool checkip = Facade.FacadeCheckIpManagerAndAdmin(code, ipAddress);
                                    if (checkip)
                                    {
                                        string[] subParameter = subValue[1].Split(',');
                                        if (subParameter.Length > 0)
                                        {
                                            int start = 0;
                                            int end = 0;
                                            int managerID = 0;
                                            DateTime startTime;
                                            DateTime endTime;
                                            List<int> InvestorList = new List<int>();
                                            List<Business.OpenTrade> Result = new List<OpenTrade>();

                                            int.TryParse(subParameter[0], out start);
                                            int.TryParse(subParameter[1], out end);
                                            int.TryParse(subParameter[2], out managerID);
                                            DateTime.TryParse(subParameter[3], out startTime);
                                            DateTime.TryParse(subParameter[4], out endTime);

                                            //set new time start,end
                                            startTime = new DateTime(startTime.Year, startTime.Month, startTime.Day, 00, 00, 00);
                                            endTime = new DateTime(endTime.Year, endTime.Month, endTime.Day, 23, 59, 59);

                                            for (int j = 5; j < subParameter.Length; j++)
                                            {
                                                int investorID = 0;
                                                int.TryParse(subParameter[j], out investorID);
                                                InvestorList.Add(investorID);
                                            }

                                            if (Business.Market.AgentList != null)
                                            {
                                                int countAgent = Business.Market.AgentList.Count;
                                                for (int j = 0; j < countAgent; j++)
                                                {
                                                    if (Business.Market.AgentList[j].AgentID == managerID)
                                                    {
                                                        if (start == 0)
                                                        {
                                                            #region REMOVE ALL DATA HISTORY REPORT OF INVESTOR
                                                            Business.Market.AgentList[j].ListHistoryReport = new List<OpenTrade>();
                                                            #endregion

                                                            #region GET REPORT IN DATABASE AND ADD TO LIST HISTORY REPORT
                                                            DateTime timeLastAccount = startTime.AddDays(-1);
                                                            Result = TradingServer.Facade.FacadegetCommandHistoryWithInvestorList(InvestorList, managerID, startTime, endTime);

                                                            if (Result != null)
                                                            {
                                                                int countResult = Result.Count;

                                                                if (Business.Market.AgentList[j].ListHistoryReport == null)
                                                                    Business.Market.AgentList[j].ListHistoryReport = new List<OpenTrade>();

                                                                for (int n = 0; n < countResult; n++)
                                                                {
                                                                    Business.Market.AgentList[j].ListHistoryReport.Add(Result[n]);
                                                                }
                                                            }
                                                            #endregion

                                                            #region GET LAST ACCOUNT
                                                            DateTime timeEndLastAccount = new DateTime(timeLastAccount.Year, timeLastAccount.Month, timeLastAccount.Day, 23, 59, 59);
                                                            List<Business.LastBalance> listLastAccount = TradingServer.Facade.FacadeGetLastBalanceByTimeListInvestor(InvestorList, timeLastAccount, timeEndLastAccount);
                                                            if (listLastAccount != null)
                                                            {
                                                                int countLastAccount = listLastAccount.Count;
                                                                for (int n = 0; n < countLastAccount; n++)
                                                                {
                                                                    Business.OpenTrade newOpenTrade = new OpenTrade();
                                                                    newOpenTrade.AgentCommission = listLastAccount[n].Balance;
                                                                    newOpenTrade.AskServer = listLastAccount[n].ClosePL;
                                                                    newOpenTrade.BidServer = listLastAccount[n].Credit;
                                                                    newOpenTrade.ClosePrice = listLastAccount[n].CreditAccount;
                                                                    newOpenTrade.Commission = listLastAccount[n].CreditOut;
                                                                    newOpenTrade.FreezeMargin = listLastAccount[n].Deposit;
                                                                    newOpenTrade.Margin = listLastAccount[n].FloatingPL;
                                                                    newOpenTrade.MaxDev = listLastAccount[n].FreeMargin;
                                                                    newOpenTrade.NumberUpdate = listLastAccount[n].InvestorID;
                                                                    newOpenTrade.OpenPrice = listLastAccount[n].LastEquity;
                                                                    newOpenTrade.OpenTime = listLastAccount[n].LogDate;
                                                                    newOpenTrade.Profit = listLastAccount[n].LastMargin;
                                                                    newOpenTrade.Size = listLastAccount[n].PLBalance;
                                                                    newOpenTrade.SpreaDifferenceInOpenTrade = listLastAccount[n].Withdrawal;
                                                                    newOpenTrade.Comment = listLastAccount[n].LoginCode;
                                                                    newOpenTrade.Type = new TradeType();
                                                                    newOpenTrade.Type.ID = 21;
                                                                    newOpenTrade.Type.Name = "LastBalance";
                                                                    newOpenTrade.Investor = new Investor();

                                                                    Business.Market.AgentList[j].ListHistoryReport.Add(newOpenTrade);
                                                                }
                                                            }
                                                            #endregion

                                                            #region GET END LAST ACCOUNT
                                                            DateTime tempStartTime = new DateTime(endTime.Year, endTime.Month, endTime.Day, 00, 00, 00);
                                                            DateTime tempEndTime = new DateTime(endTime.Year, endTime.Month, endTime.Day, 23, 59, 59);
                                                            List<Business.LastBalance> listEndLastAccount = TradingServer.Facade.FacadeGetLastBalanceByTimeListInvestor(InvestorList, tempStartTime, tempEndTime);
                                                            if (listEndLastAccount != null)
                                                            {
                                                                int countLastAccount = listEndLastAccount.Count;
                                                                for (int n = 0; n < countLastAccount; n++)
                                                                {
                                                                    Business.OpenTrade newOpenTrade = new OpenTrade();
                                                                    newOpenTrade.AgentCommission = listEndLastAccount[n].Balance;
                                                                    newOpenTrade.AskServer = listEndLastAccount[n].ClosePL;
                                                                    newOpenTrade.BidServer = listEndLastAccount[n].Credit;
                                                                    newOpenTrade.ClosePrice = listEndLastAccount[n].CreditAccount;
                                                                    newOpenTrade.Commission = listEndLastAccount[n].CreditOut;
                                                                    newOpenTrade.FreezeMargin = listEndLastAccount[n].Deposit;
                                                                    newOpenTrade.Margin = listEndLastAccount[n].FloatingPL;
                                                                    newOpenTrade.MaxDev = listEndLastAccount[n].FreeMargin;
                                                                    newOpenTrade.NumberUpdate = listEndLastAccount[n].InvestorID;
                                                                    newOpenTrade.OpenPrice = listEndLastAccount[n].LastEquity;
                                                                    newOpenTrade.OpenTime = listEndLastAccount[n].LogDate;
                                                                    newOpenTrade.Profit = listEndLastAccount[n].LastMargin;
                                                                    newOpenTrade.Size = listEndLastAccount[n].PLBalance;
                                                                    newOpenTrade.SpreaDifferenceInOpenTrade = listEndLastAccount[n].Withdrawal;
                                                                    newOpenTrade.Comment = listEndLastAccount[n].LoginCode;
                                                                    newOpenTrade.Type = new TradeType();
                                                                    newOpenTrade.Type.ID = 22;
                                                                    newOpenTrade.Type.Name = "EndLastBalance";
                                                                    newOpenTrade.Investor = new Investor();

                                                                    Business.Market.AgentList[j].ListHistoryReport.Add(newOpenTrade);
                                                                }
                                                            }
                                                            #endregion
                                                        }

                                                        if (Business.Market.AgentList[j].ListHistoryReport != null)
                                                        {
                                                            int rowNumber = end - start;
                                                            if (Business.Market.AgentList[j].ListHistoryReport.Count < rowNumber)
                                                                rowNumber = Business.Market.AgentList[j].ListHistoryReport.Count;

                                                            #region FOR GET ORDER WITH START ,END
                                                            for (int n = 0; n < rowNumber; n++)
                                                            {
                                                                string symbolName = string.Empty;
                                                                if (Business.Market.AgentList[j].ListHistoryReport[0].Type == null ||
                                                                    Business.Market.AgentList[j].ListHistoryReport[0].Investor == null)
                                                                    continue;

                                                                if (Business.Market.AgentList[j].ListHistoryReport[0].Type.ID == 21)
                                                                {
                                                                    //string messageLastBalance = "LastBalance$" + Business.Market.AgentList[j].ListHistoryReport[0].Investor.InvestorID + "," +
                                                                    //    Business.Market.AgentList[j].ListHistoryReport[0].Profit;

                                                                    string messageLastBalance = "LastBalance$" + Business.Market.AgentList[j].ListHistoryReport[0].NumberUpdate + "," +
                                                                        Business.Market.AgentList[j].ListHistoryReport[0].AgentCommission + "," +
                                                                        Business.Market.AgentList[j].ListHistoryReport[0].AskServer + "," +
                                                                        Business.Market.AgentList[j].ListHistoryReport[0].FreezeMargin + "," +
                                                                        Business.Market.AgentList[j].ListHistoryReport[0].Margin + "," +
                                                                        Business.Market.AgentList[j].ListHistoryReport[0].BidServer + "," +
                                                                        Business.Market.AgentList[j].ListHistoryReport[0].OpenPrice + "," +
                                                                        Business.Market.AgentList[j].ListHistoryReport[0].Profit + "," +
                                                                        Business.Market.AgentList[j].ListHistoryReport[0].MaxDev + "," +
                                                                        Business.Market.AgentList[j].ListHistoryReport[0].OpenTime + "," +
                                                                        Business.Market.AgentList[j].ListHistoryReport[0].Commission + "," +
                                                                        Business.Market.AgentList[j].ListHistoryReport[0].SpreaDifferenceInOpenTrade + "," +
                                                                        Business.Market.AgentList[j].ListHistoryReport[0].ClosePrice + "," +
                                                                        Business.Market.AgentList[j].ListHistoryReport[0].Comment;

                                                                    StringResult.Add(messageLastBalance);

                                                                    Business.Market.AgentList[j].ListHistoryReport.RemoveAt(0);

                                                                    //n--;

                                                                    //if (Business.Market.AgentList[j].ListHistoryReport.Count < rowNumber)
                                                                    //    rowNumber = Business.Market.AgentList[j].ListHistoryReport.Count;
                                                                }
                                                                else if (Business.Market.AgentList[j].ListHistoryReport[0].Type.ID == 22)
                                                                {
                                                                    string messageLastBalance = "EndLastBalance$" + Business.Market.AgentList[j].ListHistoryReport[0].NumberUpdate + "," +
                                                                       Business.Market.AgentList[j].ListHistoryReport[0].AgentCommission + "," +
                                                                       Business.Market.AgentList[j].ListHistoryReport[0].AskServer + "," +
                                                                       Business.Market.AgentList[j].ListHistoryReport[0].FreezeMargin + "," +
                                                                       Business.Market.AgentList[j].ListHistoryReport[0].Margin + "," +
                                                                       Business.Market.AgentList[j].ListHistoryReport[0].BidServer + "," +
                                                                       Business.Market.AgentList[j].ListHistoryReport[0].OpenPrice + "," +
                                                                       Business.Market.AgentList[j].ListHistoryReport[0].Profit + "," +
                                                                       Business.Market.AgentList[j].ListHistoryReport[0].MaxDev + "," +
                                                                       Business.Market.AgentList[j].ListHistoryReport[0].OpenTime + "," +
                                                                       Business.Market.AgentList[j].ListHistoryReport[0].Commission + "," +
                                                                       Business.Market.AgentList[j].ListHistoryReport[0].SpreaDifferenceInOpenTrade + "," +
                                                                       Business.Market.AgentList[j].ListHistoryReport[0].ClosePrice + "," +
                                                                       Business.Market.AgentList[j].ListHistoryReport[0].Comment;

                                                                    StringResult.Add(messageLastBalance);

                                                                    Business.Market.AgentList[j].ListHistoryReport.RemoveAt(0);
                                                                }
                                                                else
                                                                {
                                                                    if (Business.Market.AgentList[j].ListHistoryReport[0].Symbol != null)
                                                                        symbolName = Business.Market.AgentList[j].ListHistoryReport[0].Symbol.Name;

                                                                    string message = subValue[0] + "$" + Business.Market.AgentList[j].ListHistoryReport[0].ClientCode + "," +
                                                                        Business.Market.AgentList[j].ListHistoryReport[0].ClosePrice + "," +
                                                                        Business.Market.AgentList[j].ListHistoryReport[0].CloseTime + "," +
                                                                        Business.Market.AgentList[j].ListHistoryReport[0].CommandCode + "," +
                                                                        Business.Market.AgentList[j].ListHistoryReport[0].Commission + "," +
                                                                        Business.Market.AgentList[j].ListHistoryReport[0].ExpTime + "," +
                                                                        Business.Market.AgentList[j].ListHistoryReport[0].ID + "," +
                                                                        Business.Market.AgentList[j].ListHistoryReport[0].Investor.InvestorID + "," +
                                                                        true + "," + false + "," + "0,0" + "," +
                                                                        Business.Market.AgentList[j].ListHistoryReport[0].OpenPrice + "," +
                                                                        Business.Market.AgentList[j].ListHistoryReport[0].OpenTime + "," +
                                                                        Business.Market.AgentList[j].ListHistoryReport[0].Profit + "," +
                                                                        Business.Market.AgentList[j].ListHistoryReport[0].Size + "," +
                                                                        Business.Market.AgentList[j].ListHistoryReport[0].StopLoss + "," +
                                                                        Business.Market.AgentList[j].ListHistoryReport[0].Swap + "," + symbolName + "," +
                                                                        Business.Market.AgentList[j].ListHistoryReport[0].TakeProfit + "," +
                                                                        Business.Market.AgentList[j].ListHistoryReport[0].Taxes + "," +
                                                                        Business.Market.AgentList[j].ListHistoryReport[0].Type.Name + "," +
                                                                        Business.Market.AgentList[j].ListHistoryReport[0].Type.ID + ",-1,-1" + "," +
                                                                        Business.Market.AgentList[j].ListHistoryReport[0].AgentCommission + "," +
                                                                        Business.Market.AgentList[j].ListHistoryReport[0].Investor.AgentID + "," +
                                                                        Business.Market.AgentList[j].ListHistoryReport[0].Comment;

                                                                    StringResult.Add(message);

                                                                    Business.Market.AgentList[j].ListHistoryReport.RemoveAt(0);

                                                                    //n--;

                                                                    //if (Business.Market.AgentList[j].ListHistoryReport.Count < rowNumber)
                                                                    //    rowNumber = Business.Market.AgentList[j].ListHistoryReport.Count;
                                                                }
                                                            }
                                                            #endregion
                                                        }

                                                        break;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    if (StringResult.Count == 0)
                                    {
                                        string message = subValue[0] + "$";
                                        StringResult.Add(message);
                                    }
                                }
                                break;
                            #endregion

                            #region FUNCTION SYSTEM LOG
                            case "GetLogByTime":
                                {
                                    bool checkip = Facade.FacadeCheckIpManagerAndAdmin(code, ipAddress);
                                    if (checkip)
                                    {
                                        string[] subParameter = subValue[1].Split(',');
                                        if (subParameter.Length == 7)
                                        {
                                            #region GET PARAMETER
                                            List<Business.SystemLog> result = new List<SystemLog>();
                                            DateTime begin;
                                            DateTime end;
                                            int typeID = 0;
                                            int positionStart = 0;
                                            int positionEnd = 0;

                                            int managerID = 0;
                                            bool isExists = false;
                                            //GetLogByTime$00000093,-1,05/01/2012 00:00:00,12/13/2012 15:01:13,0,10000,37
                                            int.TryParse(subParameter[1], out typeID);
                                            DateTime.TryParse(subParameter[2], out begin);
                                            DateTime.TryParse(subParameter[3], out end);
                                            int.TryParse(subParameter[4], out positionStart);
                                            int.TryParse(subParameter[5], out positionEnd);
                                            int.TryParse(subParameter[6], out managerID);
                                            #endregion

                                            #region GET LOG IF POSITION START = 0
                                            if (positionStart == 0)
                                            {
                                                if (string.IsNullOrEmpty(subParameter[0]))
                                                {
                                                    if (typeID == -1)
                                                    {
                                                        result = TradingServer.Facade.FacadeGetSystemLogByTime(begin, end);
                                                    }
                                                    else
                                                    {
                                                        result = TradingServer.Facade.FacadeGetSystemLogByTimeAndType(typeID, begin, end);
                                                    }
                                                }
                                                else
                                                {
                                                    if (typeID == -1)
                                                    {
                                                        string contentSearch = "%" + subParameter[0] + "%";
                                                        
                                                        //IPAddress IP = null;
                                                        bool isParseIP = true;//IPAddress.TryParse(subParameter[0], out IP);
                                                        bool isNumber = false;
                                                        int valueAddress = -1;
                                                        int dotIndex = subParameter[0].IndexOf('.');
                                                        if (dotIndex > 0)
                                                        {
                                                            string[] subAddress = subParameter[0].Split('.');
                                                            if (subAddress.Length == 4)
                                                            {
                                                                for (int n = 0; n < subAddress.Length; n++)
                                                                {
                                                                    isNumber = int.TryParse(subAddress[n], out valueAddress);
                                                                    if (!isNumber)
                                                                    {
                                                                        isParseIP = false;
                                                                        break;
                                                                    }
                                                                    else
                                                                    {
                                                                        if (valueAddress > 254)
                                                                        {
                                                                            isParseIP = false;
                                                                            break;
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                            else
                                                            {
                                                                isParseIP = false;
                                                            }
                                                        }
                                                        else
                                                        {
                                                            isParseIP = false;
                                                        }

                                                        if (isParseIP)
                                                            result = TradingServer.Facade.FacadeGetLogByIPAddress(begin, end, subParameter[0]);
                                                        else
                                                            result = TradingServer.Facade.FacadeGetLogLikeCode(begin, end, contentSearch);
                                                    }
                                                    else
                                                    {
                                                        string contentSearch = "%" + subParameter[0] + "%";
                                                        //IPAddress IP = null;
                                                        bool isParseIP = true;//IPAddress.TryParse(subParameter[0], out IP);
                                                        bool isNumber = false;
                                                        int valueAddress = -1;
                                                        int dotIndex = subParameter[0].IndexOf('.');
                                                        if (dotIndex > 0)
                                                        {
                                                            string[] subAddress = subParameter[0].Split('.');
                                                            if (subAddress.Length == 4)
                                                            {
                                                                for (int n = 0; n < subAddress.Length; n++)
                                                                {
                                                                    isNumber = int.TryParse(subAddress[n], out valueAddress);
                                                                    if (!isNumber)
                                                                    {
                                                                        isParseIP = false;
                                                                        break;
                                                                    }
                                                                    else
                                                                    {
                                                                        if (valueAddress > 254)
                                                                        {
                                                                            isParseIP = false;
                                                                            break;
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                            else
                                                            {
                                                                isParseIP = false;
                                                            }
                                                        }
                                                        else
                                                        {
                                                            isParseIP = false;
                                                        }

                                                        if (isParseIP)
                                                            result = TradingServer.Facade.FacadeGetLogByIPAddressAndType(begin, end, subParameter[0], typeID);
                                                        else
                                                            result = TradingServer.Facade.FacadeGetSystemLogByCodeAndTime(begin, end, typeID, contentSearch);
                                                    }
                                                }
                                            }
                                            #endregion

                                            #region MAP RESULT TO AGENT LIST SYSTEM LOG
                                            if (Business.Market.AdminList != null && Business.Market.AdminList.Count > 0)
                                            {
                                                int countAgent = Business.Market.AdminList.Count;
                                                for (int j = 0; j < countAgent; j++)
                                                {
                                                    if (Business.Market.AdminList[j].AgentID == managerID)
                                                    {
                                                        if (positionStart == 0)
                                                        {
                                                            Business.Market.AdminList[j].ListSystemLog = new List<SystemLog>();

                                                            if (result != null)
                                                            {
                                                                int countResult = result.Count;
                                                                for (int n = 0; n < countResult; n++)
                                                                {
                                                                    Business.Market.AdminList[j].ListSystemLog.Add(result[n]);
                                                                }
                                                            }
                                                        }

                                                        int rowNumber = positionEnd - positionStart;
                                                        if (Business.Market.AdminList[j].ListSystemLog.Count < rowNumber)
                                                            rowNumber = Business.Market.AdminList[j].ListSystemLog.Count;

                                                        for (int m = 0; m < rowNumber; m++)
                                                        {
                                                            string message = subValue[0] + "$" + Business.Market.AdminList[j].ListSystemLog[0].ID + "," +
                                                                Business.Market.AdminList[j].ListSystemLog[0].LogDay + "," +
                                                                Business.Market.AdminList[j].ListSystemLog[0].IPAddress + "," +
                                                                Business.Market.AdminList[j].ListSystemLog[0].LogContent + "," +
                                                                Business.Market.AdminList[j].ListSystemLog[0].Comment;

                                                            StringResult.Add(message);

                                                            Business.Market.AdminList[j].ListSystemLog.RemoveAt(0);
                                                        }

                                                        isExists = true;

                                                        break;
                                                    }
                                                }
                                            }
                                            #endregion

                                            #region MAP RESULT TO AGENT LIST SYSTEM LOG
                                            if (!isExists)
                                            {
                                                if (Business.Market.AgentList != null && Business.Market.AgentList.Count > 0)
                                                {
                                                    int countAgent = Business.Market.AgentList.Count;
                                                    for (int j = 0; j < countAgent; j++)
                                                    {
                                                        if (Business.Market.AgentList[j].AgentID == managerID)
                                                        {
                                                            if (positionStart == 0)
                                                            {
                                                                Business.Market.AgentList[j].ListSystemLog = new List<SystemLog>();

                                                                if (result != null)
                                                                {
                                                                    int countResult = result.Count;
                                                                    for (int n = 0; n < countResult; n++)
                                                                    {
                                                                        Business.Market.AgentList[j].ListSystemLog.Add(result[n]);
                                                                    }
                                                                }
                                                            }

                                                            int rowNumber = positionEnd - positionStart;
                                                            if (Business.Market.AgentList[j].ListSystemLog.Count < rowNumber)
                                                                rowNumber = Business.Market.AgentList[j].ListSystemLog.Count;

                                                            for (int m = 0; m < rowNumber; m++)
                                                            {
                                                                string message = subValue[0] + "$" + Business.Market.AgentList[j].ListSystemLog[0].ID + "," +
                                                                    Business.Market.AgentList[j].ListSystemLog[0].LogDay + "," +
                                                                    Business.Market.AgentList[j].ListSystemLog[0].IPAddress + "," +
                                                                    Business.Market.AgentList[j].ListSystemLog[0].LogContent + "," +
                                                                    Business.Market.AgentList[j].ListSystemLog[0].Comment;

                                                                StringResult.Add(message);

                                                                Business.Market.AgentList[j].ListSystemLog.RemoveAt(0);
                                                            }

                                                            break;
                                                        }
                                                    }
                                                }
                                            }
                                            #endregion

                                            #region INSERT SYSTEM LOG
                                            string content = "'" + code + "': Requested '" + subParameter[0] + "' ";

                                            string contentResult = string.Empty;
                                            if (result != null)
                                            {
                                                if (result.Count > 0)
                                                    contentResult = result.Count + " rows returned";
                                                else
                                                    contentResult = result.Count + " row returned";
                                            }
                                            else
                                            {
                                                contentResult = "0 row returned";
                                            }

                                            content = content + contentResult;
                                            string comment = "[Requested Log]";
                                            TradingServer.Facade.FacadeAddNewSystemLog(3, content, comment, ipAddress, code);
                                            #endregion
                                        }
                                    }

                                    #region RETURN -1 IF DON'T FIND LOG
                                    if (StringResult.Count == 0)
                                    {
                                        string message = subValue[0] + "$-1";
                                        StringResult.Add(message);
                                    }
                                    #endregion
                                }
                                break;
                            #endregion

                            #region GET TOP INTERNAL MAIL TO INVESTOR
                            case "GetTopInternalMailToInvestor":
                                {
                                    List<Business.InternalMail> result = new List<InternalMail>();
                                    result = TradingServer.Facade.FacadeGetTopInternalMailToInvestor(subValue[1]);

                                    if (result != null)
                                    {
                                        int countResult = result.Count;
                                        for (int j = 0; j < countResult; j++)
                                        {
                                            string message = subValue[j] + "$" + result[j].InternalMailID + "," + TradingServer.Model.TradingCalculate.Instance.ConvertStringToHex(result[j].Subject) + "," +
                                                TradingServer.Model.TradingCalculate.Instance.ConvertStringToHex(result[j].From) + "," +
                                                TradingServer.Model.TradingCalculate.Instance.ConvertStringToHex(result[j].FromName) + "," +
                                                TradingServer.Model.TradingCalculate.Instance.ConvertStringToHex(result[j].Time.ToString()) + "," +
                                                TradingServer.Model.TradingCalculate.Instance.ConvertStringToHex(result[j].Content);
                                            StringResult.Add(message);
                                        }
                                    }

                                    if (StringResult.Count == 0)
                                    {
                                        StringResult.Add(subValue[1] + "$-1");
                                    }
                                }
                                break;

                            case "GetInternalMailToInvestor":
                                {
                                    int start = 0, end = 0;
                                    string[] tem = subValue[1].Split(',');
                                    int.TryParse(tem[0], out start);
                                    int.TryParse(tem[1], out end);
                                    List<Business.InternalMail> result = new List<InternalMail>();
                                    result = TradingServer.Facade.FacadeGetInternalMailToInvestor(start, end, tem[2]);

                                    if (result != null)
                                    {
                                        int countResult = result.Count;
                                        for (int j = 0; j < countResult; j++)
                                        {
                                            string message = subValue[0] + "$" + result[j].InternalMailID + "," + TradingServer.Model.TradingCalculate.Instance.ConvertStringToHex(result[j].Subject) + "," +
                                                TradingServer.Model.TradingCalculate.Instance.ConvertStringToHex(result[j].From) + "," +
                                                TradingServer.Model.TradingCalculate.Instance.ConvertStringToHex(result[j].FromName) + "," +
                                                TradingServer.Model.TradingCalculate.Instance.ConvertStringToHex(result[j].Time.ToString()) + "," +
                                                result[j].IsNew.ToString() + "," +
                                                TradingServer.Model.TradingCalculate.Instance.ConvertStringToHex(result[j].Content);
                                            StringResult.Add(message);
                                        }
                                    }

                                    if (StringResult.Count == 0)
                                    {
                                        StringResult.Add(subValue[0] + "$");
                                    }
                                }
                                break;

                            #endregion

                            #region CHECK TIME EVENT DEBUG
                            case "DayEvent":
                                {
                                    if (Business.Market.DayEvent != null)
                                    {
                                        int countDayEvent = Business.Market.DayEvent.Count;
                                        for (int j = 0; j < countDayEvent; j++)
                                        {
                                            if (Business.Market.DayEvent[j].TargetFunction != null)
                                            {
                                                int countTarget = Business.Market.DayEvent[j].TargetFunction.Count;
                                                for (int n = 0; n < countTarget; n++)
                                                {
                                                    string message = subValue[0] + "$Event Type: " + Business.Market.DayEvent[j].EventType + "-Every: " + Business.Market.DayEvent[j].Every + "-Enable: " +
                                                        Business.Market.DayEvent[j].IsEnable + "-Event Position: " + Business.Market.DayEvent[j].TargetFunction[n].EventPosition + "-Day EventID: " +
                                                        Business.Market.DayEvent[j].TimeEventID + "-Time Execution: " + Business.Market.DayEvent[j].TimeExecution + " " + Business.Market.TimerEventDay.Interval;

                                                    StringResult.Add(message);
                                                }
                                            }
                                        }
                                    }
                                }
                                break;

                            case "WeekEvent":
                                {
                                    if (Business.Market.WeekEvent != null)
                                    {
                                        int countWeekEvent = Business.Market.WeekEvent.Count;
                                        for (int j = 0; j < countWeekEvent; j++)
                                        {
                                            if (Business.Market.WeekEvent[j].TargetFunction != null)
                                            {
                                                int countTarget = Business.Market.WeekEvent[j].TargetFunction.Count;
                                                for (int n = 0; n < countTarget; n++)
                                                {
                                                    string message = subValue[0] + "$Event Type: " + Business.Market.WeekEvent[j].EventType + "-Every: " + Business.Market.WeekEvent[j].Every + "-Enable: " +
                                                        Business.Market.WeekEvent[j].IsEnable + "-Event Position: " + Business.Market.WeekEvent[j].TargetFunction[n].EventPosition + "-EventID: " +
                                                        Business.Market.WeekEvent[j].TimeEventID + "-TimeExecution: " + Business.Market.WeekEvent[j].TimeExecution + " " + Business.Market.TimerEventWeek.Interval;

                                                    StringResult.Add(message);
                                                }
                                            }
                                        }
                                    }
                                    break;
                                }

                            case "YearEvent":
                                {
                                    if (Business.Market.YearEvent != null)
                                    {
                                        if (Business.Market.YearEvent != null)
                                        {
                                            int countYearEvent = Business.Market.YearEvent.Count;
                                            for (int j = 0; j < countYearEvent; j++)
                                            {
                                                if (Business.Market.YearEvent[j].TargetFunction != null)
                                                {
                                                    int countTarget = Business.Market.YearEvent[j].TargetFunction.Count;
                                                    for (int n = 0; n < countTarget; n++)
                                                    {
                                                        string message = subValue[0] + "$Event Type: " + Business.Market.YearEvent[j].EventType + "-Every: " + Business.Market.YearEvent[j].Every + "-Enable: " +
                                                            Business.Market.YearEvent[j].IsEnable + "-EventPosition: " + Business.Market.YearEvent[j].TargetFunction[n].EventPosition + "-EventID: " +
                                                            Business.Market.YearEvent[j].TimeEventID + "-Time Execution: " + Business.Market.YearEvent[j].TimeExecution + " " + Business.Market.TimerEventYear.Interval;

                                                        StringResult.Add(message);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                                break;
                            #endregion

                            #region GET TICK ONLINE INIT MARKET
                            case "GetTickOnline":
                                {
                                    if (Business.Market.QuoteList != null)
                                    {
                                        int countQuote = Business.Market.QuoteList.Count;
                                        for (int j = 0; j < countQuote; j++)
                                        {
                                            if (Business.Market.QuoteList[i].RefSymbol != null)
                                            {
                                                int countRefSymbol = Business.Market.QuoteList[i].RefSymbol.Count;
                                                for (int n = 0; n < countRefSymbol; n++)
                                                {
                                                    if (Business.Market.QuoteList[j].RefSymbol[n].TickValue != null &&
                                                        !string.IsNullOrEmpty(Business.Market.QuoteList[j].RefSymbol[n].TickValue.SymbolName))
                                                    {
                                                        string message = Business.Market.QuoteList[j].RefSymbol[n].TickValue.Bid + "?" +
                                                            //Business.Market.QuoteList[j].RefSymbol[n].TickValue.Ask + "?" +
                                                            Business.Market.QuoteList[j].RefSymbol[n].TickValue.TickTime + "?" +
                                                            Business.Market.QuoteList[j].RefSymbol[n].TickValue.SymbolName + "?" +
                                                            Business.Market.QuoteList[j].RefSymbol[n].TickValue.HighInDay + "?" +
                                                            Business.Market.QuoteList[j].RefSymbol[n].TickValue.LowInDay + "?" +
                                                            //Business.Market.QuoteList[j].RefSymbol[n].TickValue.HighAsk + "?" +
                                                            //Business.Market.QuoteList[j].RefSymbol[n].TickValue.LowAsk + "?" +
                                                            Business.Market.QuoteList[j].RefSymbol[n].TickValue.Status;

                                                        StringResult.Add(message);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                                break;

                            case "GetTickOnlineManager":
                                {
                                    bool checkip = Facade.FacadeCheckIpManagerAndAdmin(code, ipAddress);
                                    if (checkip)
                                    {
                                        if (Business.Market.SymbolList != null)
                                        {
                                            for (int j = Business.Market.SymbolList.Count - 1; j >= 0; j--)
                                            {
                                                if (Business.Market.SymbolList[j] != null)
                                                {
                                                    if (Business.Market.SymbolList[j].TickValue != null)
                                                    {
                                                        string message = "GetTickOnlineManager$" + Business.Market.SymbolList[j].TickValue.Bid + "}" +
                                                            Business.Market.SymbolList[j].TickValue.Ask + "}" +
                                                            Business.Market.SymbolList[j].TickValue.SymbolName + "}" +
                                                            Business.Market.SymbolList[j].TickValue.TickTime.Ticks + "}" +
                                                            Business.Market.SymbolList[j].TickValue.Status + "}" +
                                                            Business.Market.SymbolList[j].TickValue.HighInDay + "}" +
                                                            Business.Market.SymbolList[j].TickValue.LowInDay;
                                                        StringResult.Add(message);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }

                                break;
                            #endregion

                            #region GET CANDLES DAY(GET OPEN OPEN PRICE IN DAY)
                            case "GetCandlesByDate":
                                {
                                    string[] subParameter = subValue[1].Split('|');
                                    int port = -1;
                                    int investorID = -1;
                                    DateTime time;
                                    List<string> listSymbol = new List<string>();
                                    if (subParameter.Length == 7)
                                    {
                                        int month, day, year;

                                        int.TryParse(subParameter[1], out month);
                                        int.TryParse(subParameter[2], out day);
                                        int.TryParse(subParameter[3], out year);

                                        time = new DateTime(year, month, day, 00, 00, 00);

                                        int.TryParse(subParameter[4], out port);
                                        int.TryParse(subParameter[5], out investorID);

                                        Business.Investor.investorInstance.UpdateLastConnect(investorID, subParameter[6]);

                                        string[] subListSymbol = subParameter[0].Split('{');
                                        int countSymbol = subListSymbol.Length;
                                        for (int j = 0; j < countSymbol; j++)
                                        {
                                            if (!string.IsNullOrEmpty(subListSymbol[j]))
                                            {
                                                listSymbol.Add(subListSymbol[j]);
                                            }
                                        }

                                        List<string> temp = ProcessQuoteLibrary.Business.QuoteProcess.GetCandlesByDate(listSymbol, time, port);

                                        if (temp != null && temp.Count > 0)
                                        {
                                            int countTemp = temp.Count;
                                            for (int j = 0; j < countTemp; j++)
                                            {
                                                if (!string.IsNullOrEmpty(temp[j]))
                                                {
                                                    string message = subValue[0] + "$" + temp[j];
                                                    StringResult.Add(message);
                                                }
                                            }
                                        }
                                        else
                                        {
                                            string message = subValue[0] + "$";
                                            StringResult.Add(message);
                                        }
                                    }
                                }
                                break;
                            #endregion

                            #region ADMIN OR MANGER REQUEST CLIENT LOG
                            case "RequestClientLog":
                                {
                                    if (Business.Market.ListClientLogs != null)
                                    {
                                        int countClientLog = Business.Market.ListClientLogs.Count;
                                        for (int j = 0; j < count; j++)
                                        {
                                            if (Business.Market.ListClientLogs[j].AdminCode == code)
                                            {
                                                int countWhile = 0;
                                                while (!Business.Market.ListClientLogs[j].IsComplete)
                                                {   
                                                    System.Threading.Thread.Sleep(1000);
                                                    countWhile++;
                                                    if (countWhile == 5)
                                                        Business.Market.ListClientLogs[j].IsComplete = true;
                                                }

                                                if (Business.Market.ListClientLogs[j].ClientLogs.Count > 0)
                                                    StringResult = Business.Market.ListClientLogs[j].ClientLogs;
                                                else
                                                    StringResult.Add(subValue[0] + "$");

                                                Business.Market.ListClientLogs[j].ClientLogs.Clear();

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

            return StringResult;
        }
        /// <summary>
        /// UPDATE IGROUP SECURITY CONFIG 
        /// </summary>
        /// <param name="objParameterItem"></param>
        /// <returns></returns>
        internal bool UpdateIGroupSecurityConfig(Business.ParameterItem objParameterItem)
        {
            bool Result = false;

            try
            {
                if (Business.Market.IGroupSecurityList != null)
                {
                    int count = Business.Market.IGroupSecurityList.Count;
                    for (int i = 0; i < count; i++)
                    {
                        if (Business.Market.IGroupSecurityList[i].IGroupSecurityID == objParameterItem.SecondParameterID)
                        {
                            if (Business.Market.IGroupSecurityList[i].IGroupSecurityConfig != null)
                            {
                                bool isValid = true;
                                bool isValidPipRebate = true;
                                double spreadByDefault = 0;
                                double sparedDiffirence = 0;
                                int symbolID = 0;
                                double totalPip = 0;

                                int countParameter = Business.Market.IGroupSecurityList[i].IGroupSecurityConfig.Count;

                                #region GET SPREAD BY DEFAULT IN GROUP
                                //if (Business.Market.InvestorGroupList != null)
                                //{
                                //    int countGroup = Business.Market.InvestorGroupList.Count;
                                //    for (int j = 0; j < countGroup; j++)
                                //    {
                                //        if (Business.Market.InvestorGroupList[j].InvestorGroupID == Business.Market.IGroupSecurityList[i].InvestorGroupID)
                                //        {
                                //            if (Business.Market.InvestorGroupList[j].ParameterItems != null)
                                //            {
                                //                int countGroupParameter = Business.Market.InvestorGroupList[j].ParameterItems.Count;
                                //                for (int n = 0; n < countGroupParameter; n++)
                                //                {
                                //                    if (Business.Market.InvestorGroupList[j].ParameterItems[n].Code == "S013")
                                //                    {
                                //                        spreadByDefault = double.Parse(Business.Market.InvestorGroupList[j].ParameterItems[n].NumValue);

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

                                if (objParameterItem.Code == "B04")
                                {
                                    sparedDiffirence = double.Parse(objParameterItem.NumValue);

                                    if (Business.Market.SymbolList != null)
                                    {
                                        int countSymbol = Business.Market.SymbolList.Count;
                                        for (int n = 0; n < countSymbol; n++)
                                        {
                                            if (Business.Market.SymbolList[n].SecurityID == Business.Market.IGroupSecurityList[i].SecurityID)
                                            {
                                                if (Business.Market.SymbolList[n].ParameterItems != null)
                                                {
                                                    int countPara = Business.Market.SymbolList[n].ParameterItems.Count;
                                                    for (int m = 0; m < countPara; m++)
                                                    {
                                                        if (Business.Market.SymbolList[n].ParameterItems[m].Code == "S013")
                                                        {
                                                            spreadByDefault = double.Parse(Business.Market.SymbolList[n].ParameterItems[m].NumValue);
                                                            break;
                                                        }
                                                    }
                                                }

                                                symbolID = Business.Market.SymbolList[n].SymbolID;

                                                #region SEND COMMAND REQUEST TO AGENT CHECK VALID UPDATE VALUE IN IGROUPSECURITY CONFIG
                                                if (Business.Market.IsConnectAgent)
                                                {
                                                    totalPip = spreadByDefault + sparedDiffirence;
                                                    if (Business.Market.ListAgentConfig != null)
                                                    {
                                                        int countAgent = Business.Market.ListAgentConfig.Count;
                                                        for (int k = 0; k < countAgent; k++)
                                                        {
                                                            string strCmd = "UpdateGroupPipRebate$" + totalPip + "{" + symbolID;
                                                            string agentResult = Business.Market.ListAgentConfig[k].clientAgent.StringDefaultPort(strCmd, "");
                                                            string[] subValue = agentResult.Split('$');
                                                            if (subValue[0] == "UpdateGroupPipRByET5")
                                                                isValidPipRebate = bool.Parse(subValue[1]);
                                                        }
                                                    }
                                                }
                                                #endregion

                                                break;
                                            }
                                        }
                                    }
                                }

                                #region GET SPARED DIFFIRENCE IN IGROUPSECURITY AND GET SYMBOL ID
                                for (int j = 0; j < countParameter; j++)
                                {
                                    if (Business.Market.IGroupSecurityList[i].IGroupSecurityConfig[j].Code == "B04")
                                    {
                                        sparedDiffirence = double.Parse(Business.Market.IGroupSecurityList[i].IGroupSecurityConfig[j].NumValue);

                                        break;
                                    }
                                }
                                #endregion

                                #region SEND COMMAND REQUEST TO AGENT CHECK VALID UPDATE COMMISSION IN IGROUPSECUITY CONFIG
                                if (Business.Market.IsConnectAgent)
                                {
                                    if (objParameterItem.Code == "B14")
                                    {
                                        ///UpdateGroupCommByET5$commission{groupID
                                        ///
                                        string strCmd = "UpdateGroupCommByET5$" + objParameterItem.NumValue + "{" + Business.Market.IGroupSecurityList[i].InvestorGroupID;
                                        if (Business.Market.ListAgentConfig != null)
                                        {
                                            int countAgent = Business.Market.ListAgentConfig.Count;
                                            for (int j = 0; j < countAgent; j++)
                                            {
                                                string agentResult = Business.Market.ListAgentConfig[j].clientAgent.StringDefaultPort(strCmd, "");
                                                string[] subValue = agentResult.Split('$');
                                                if (subValue[0] == "UpdateGroupCommByET5")
                                                    isValid = bool.Parse(subValue[1]);
                                            }
                                        }
                                    }
                                }
                                #endregion

                                if (isValid && isValidPipRebate)
                                {
                                    for (int j = 0; j < countParameter; j++)
                                    {
                                        if (Business.Market.IGroupSecurityList[i].IGroupSecurityConfig[j].Code == objParameterItem.Code)
                                        {
                                            #region UPDATE VALUE DEFAULT SPREAD DIFFIRENCE IN SYMBOL LIST
                                            if (objParameterItem.Code == "B04")
                                            {
                                                if (Business.Market.SymbolList != null)
                                                {
                                                    int countSymbol = Business.Market.SymbolList.Count;
                                                    for (int n = 0; n < countSymbol; n++)
                                                    {
                                                        if (Business.Market.SymbolList[n].SecurityID == Business.Market.IGroupSecurityList[i].SecurityID)
                                                        {
                                                            Business.Market.SymbolList[n].SpreadDifference = double.Parse(objParameterItem.NumValue);
                                                            if (Business.Market.SymbolList[n].CommandList != null)
                                                            {
                                                                int countCommand = Business.Market.SymbolList[n].CommandList.Count;
                                                                for (int m = 0; m < countCommand; m++)
                                                                {
                                                                    Business.Market.SymbolList[n].CommandList[m].SpreaDifferenceInOpenTrade = double.Parse(objParameterItem.NumValue);
                                                                }
                                                            }

                                                            break;
                                                        }
                                                    }
                                                }
                                            }
                                            #endregion

                                            #region UPDATE COMMISSION AND NOTIFY TO AGENT
                                            if (Business.Market.IsConnectAgent)
                                            {
                                                if (objParameterItem.Code == "B14")
                                                {
                                                    //SEND COMMAND TO AGENT SERVER
                                                    //UpdateGroupCommission$Commisson{GroupID
                                                    string message = "UpdateGroupCommission$" + objParameterItem.NumValue + "{" + Business.Market.IGroupSecurityList[i].InvestorGroupID;
                                                    Business.AgentNotify newAgentNotify = new AgentNotify();
                                                    newAgentNotify.NotifyMessage = message;
                                                    TradingServer.Agent.AgentConfig.Instance.AddNotifyToAgent(newAgentNotify);
                                                }
                                            }
                                            #endregion

                                            #region UPDATE VALUE
                                            //Update Value Parameter Item
                                            Market.IGroupSecurityList[i].IGroupSecurityConfig[j].Name = objParameterItem.Name;
                                            Market.IGroupSecurityList[i].IGroupSecurityConfig[j].Code = objParameterItem.Code;
                                            Market.IGroupSecurityList[i].IGroupSecurityConfig[j].BoolValue = objParameterItem.BoolValue;
                                            Market.IGroupSecurityList[i].IGroupSecurityConfig[j].StringValue = objParameterItem.StringValue;
                                            Market.IGroupSecurityList[i].IGroupSecurityConfig[j].NumValue = objParameterItem.NumValue;
                                            Market.IGroupSecurityList[i].IGroupSecurityConfig[j].DateValue = objParameterItem.DateValue;

                                            //Set ParameterID
                                            objParameterItem.ParameterItemID = Market.IGroupSecurityList[i].IGroupSecurityConfig[j].ParameterItemID;

                                            Result = ParameterItem.DBWIGroupSecurityConfig.UpdateIGroupSecurityConfig(objParameterItem.ParameterItemID, objParameterItem.SecondParameterID, -1,
                                                        objParameterItem.Name, objParameterItem.Code, objParameterItem.BoolValue, objParameterItem.StringValue, objParameterItem.NumValue, objParameterItem.DateValue);
                                            #endregion

                                            break;
                                        }
                                    }
                                }
                                else
                                {
                                    Result = false;

                                    //LOG SYSTEM CAN'T UPDATE BECAUSE AGENT CAN'T UPDATE
                                    TradingServer.Facade.FacadeAddNewSystemLog(5, "can't update igroupsecurity because agent can't update", "[Update IGroupSecurity]", "", "");
                                }
                            }
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                TradingServer.Facade.FacadeAddNewSystemLog(5, ex.ToString(), "[UpdateIGroupSecurity]", "", "");
            }

            return Result;
        }
        /// <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 OPEN TRADE SYMBOL
        /// </summary>
        internal void OpenTradeSymbol(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)
                    {
                        if (!Business.Market.SymbolList[i].IsTrade)
                        {
                            Business.Market.SymbolList[i].IsTrade = true;

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

                        if (Business.Market.SymbolList[i].IsEnableFreezeMargin)
                        {
                            Business.Market.SymbolList[i].UseFreezeMargin = false;
                            List<Business.Investor> listInvestor = new List<Investor>();

                            #region RESET FREEZE MARGIN AND CALCULATION MARGION OF COMMAND
                            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].CalculatorMarginCommand(Business.Market.SymbolList[i].CommandList[j]);
                                    Business.Market.SymbolList[i].CommandList[j].FreezeMargin = 0;

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

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

                            #region COMMENT CODE(06/07/2011)
                            //if (timeEvent.NumSession == 0)    //First session
                            //{
                            //    #region RESET FREEZE MARGIN AND CALCULATION MARGION OF COMMAND
                            //    if (Business.Market.SymbolList[i].CommandList != null)
                            //    {
                            //        bool flagInvestor = false;
                            //        int countCommand = Business.Market.SymbolList[i].CommandList.Count;
                            //        for (int j = 0; j < countCommand; j++)
                            //        {
                            //            Business.Market.SymbolList[i].CommandList[j].CalculatorMarginCommand(Business.Market.SymbolList[i].CommandList[j]);
                            //            Business.Market.SymbolList[i].CommandList[j].FreezeMargin = 0;

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

                            //            if (!flagInvestor)
                            //            {
                            //                listInvestor.Add(Business.Market.SymbolList[i].CommandList[j].Investor);
                            //            }
                            //        }
                            //    }
                            //    #endregion
                            //}
                            //else    //Second Session
                            //{
                            //    #region RESET FREEZE MARGIN AND CALCULATION MARGION OF COMMAND
                            //    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;

                            //            DateTime timeSession = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, timeEvent.Time.Hour, timeEvent.Time.Minute, 00);
                            //            TimeSpan checkSessionOpen = Business.Market.SymbolList[i].CommandList[j].OpenTime - timeSession;
                            //            if (checkSessionOpen.TotalSeconds > 0)  //command open in session
                            //            {
                            //                Business.Market.SymbolList[i].CommandList[j].FreezeMargin = 0;
                            //                Business.Market.SymbolList[i].CommandList[j].CalculatorMarginCommand(Business.Market.SymbolList[i].CommandList[j]);
                            //            }
                            //            else    //command open before session
                            //            {
                            //                Business.Market.SymbolList[i].CommandList[j].FreezeMargin = 0;
                            //                Business.Market.SymbolList[i].CommandList[j].Margin = Business.Market.SymbolList[i].CommandList[j].CalculationFreezeMarginCommand(Business.Market.SymbolList[i].CommandList[j]);
                            //            }

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

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

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

                                    if (listInvestor[j].CommandList != null && listInvestor[j].CommandList.Count > 0)
                                    {
                                        Business.Margin newMargin = new Margin();
                                        newMargin = listInvestor[j].CommandList[0].Symbol.CalculationTotalMargin(listInvestor[j].CommandList);
                                        listInvestor[j].Margin = newMargin.TotalMargin;
                                        listInvestor[j].FreezeMargin = newMargin.TotalFreezeMargin;
                                    }
                                    else
                                    {
                                        listInvestor[j].Margin = 0;
                                        listInvestor[j].FreezeMargin = 0;
                                    }

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

                                    //SEND NOTIFY TO MANAGER THEN CLOSE COMMAND
                                    TradingServer.Facade.FacadeSendNotifyManagerRequest(3, listInvestor[j]);

                                    if (listInvestor[j].IsOnline)
                                    {
                                        string message = "FRZM14785213";

                                        if (listInvestor[j].ClientCommandQueue == null)
                                            listInvestor[j].ClientCommandQueue = new List<string>();

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

                        break;
                    }
                }
            }         
        }
Exemplo n.º 11
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="openTrade"></param>
        private void UpdateRuleClosePending(Business.OpenTrade openTrade, double Size)
        {
            string content = string.Empty;
            string comment = "[system modify order]";
            string mode = TradingServer.Facade.FacadeGetTypeNameByTypeID(openTrade.Type.ID).ToLower();

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

            #region Find In List Symbol And Update SIZE
            if (Business.Market.SymbolList != null)
            {
                bool Flag = false;
                int count = Business.Market.SymbolList.Count;
                for (int i = 0; i < 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 < countCommand; j++)
                        {
                            if (Business.Market.SymbolList[i].CommandList[j].ID == openTrade.ID)
                            {
                                string Message = string.Empty;

                                Business.Market.SymbolList[i].CommandList[j].Size = Size;

                                #region INSERT SYSTEM LOG AFTER MODIFY COMMAND
                                string Content = string.Empty;
                                Content = "'system': modified #" + openTrade.CommandCode + " " + mode + " " + strSize + " " +
                                    openTrade.Symbol.Name + " at " + openPrice + " sl: " + stopLoss + " tp: " + takeProfit + " (" + bid + "/" + ask + ")";

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

                                TradingServer.Facade.FacadeUpdateOnlineCommandWithTakeProfit(TakeProfit, StopLoss, openTrade.ID, openTrade.Comment,
                                                                                                openTrade.OpenPrice);

                                #region MAP COMMAND TO CLIENT
                                Message = "UpdateCommand$True,UPDATE COMMAND COMPLETE," + openTrade.ID + "," +
                                    Business.Market.SymbolList[i].CommandList[j].Investor.InvestorID + "," +
                                    Business.Market.SymbolList[i].CommandList[j].Symbol.Name + "," +
                                    Business.Market.SymbolList[i].CommandList[j].Size + "," + false + "," +
                                    Business.Market.SymbolList[i].CommandList[j].OpenTime + "," +
                                    Business.Market.SymbolList[i].CommandList[j].OpenPrice + "," +
                                    Business.Market.SymbolList[i].CommandList[j].StopLoss + "," +
                                    Business.Market.SymbolList[i].CommandList[j].TakeProfit + "," +
                                    Business.Market.SymbolList[i].CommandList[j].ClosePrice + "," +
                                    Business.Market.SymbolList[i].CommandList[j].Commission + "," +
                                    Business.Market.SymbolList[i].CommandList[j].Swap + "," +
                                    Business.Market.SymbolList[i].CommandList[j].Profit + "," + "Comment," +
                                    Business.Market.SymbolList[i].CommandList[j].ID + "," +
                                    Business.Market.SymbolList[i].CommandList[j].Type.Name + "," +
                                    1 + "," + Business.Market.SymbolList[i].CommandList[j].ExpTime + "," +
                                    Business.Market.SymbolList[i].CommandList[j].ClientCode + "," +
                                    Business.Market.SymbolList[i].CommandList[j].CommandCode + "," +
                                    Business.Market.SymbolList[i].CommandList[j].IsHedged + "," +
                                    Business.Market.SymbolList[i].CommandList[j].Type.ID + "," +
                                    Business.Market.SymbolList[i].CommandList[j].Margin + ",Update";

                                //If Client Update Command Then Add Message To Client Message
                                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);
                                #endregion

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

                                Flag = true;

                                //SENT COMMAND REMOVE COMMAND TO MANAGER
                                TradingServer.Facade.FacadeSendNoticeManagerRequest(2, Business.Market.SymbolList[i].CommandList[j]);

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

                                //SEND NOTIFY TO MANAGE CHANGE ACCOUNT
                                TradingServer.Facade.FacadeSendNotifyManagerRequest(3, Business.Market.SymbolList[i].CommandList[j].Investor);

                                break;
                            }
                        }
                    }
                }
            }
            #endregion

            #region FIND IN LIST COMMAND EXECUTOR AND UPDATE SIZE
            if (Business.Market.CommandExecutor != null)
            {
                int count = Business.Market.CommandExecutor.Count;
                for (int i = 0; i < count; i++)
                {
                    if (Business.Market.CommandExecutor[i].ID == openTrade.ID)
                    {
                        Business.Market.CommandExecutor[i].Size = Size;

                        break;
                    }
                }
            }
            #endregion

            #region FIND IN INVESTOR COMMAND LIST AND UPDATE SIZE
            if (openTrade.Investor.CommandList != null)
            {
                int count = openTrade.Investor.CommandList.Count;
                for (int i = 0; i < count; i++)
                {
                    if (openTrade.Investor.CommandList[i].ID == openTrade.ID)
                    {
                        openTrade.Investor.CommandList[i].Size = Size;
                        break;
                    }
                }
            }
            #endregion
        }
        /// <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>
        /// BEGIN CALCULATION SWAP OF SYMBOL
        /// </summary>
        internal void BeginCalculationSwap(string TargetName, Business.TimeEvent timeEvent)
        {
            return;

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

            #region BUILD TIME CURRENT
            DateTime timeCurrent;   //USING CHECK THREE DAY SWAP

            if (DateTime.Now.Hour < 10)
                timeCurrent = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day - 1, 00, 00, 00);
            else
                timeCurrent = DateTime.Now;
            #endregion

            //IF DAY OF WEEK == SUNDAY OR SATURDAY
            if (timeCurrent.DayOfWeek == DayOfWeek.Saturday || timeCurrent.DayOfWeek == DayOfWeek.Sunday)
                return;

            #region CALCULATION SWAP
            if (Business.Market.CommandExecutor != null)
            {
                int count = Business.Market.CommandExecutor.Count;
                for (int i = 0; i < Business.Market.CommandExecutor.Count; i++)
                {
                    bool isComplete = false;
                    bool isPending = TradingServer.Model.TradingCalculate.Instance.CheckIsPendingPosition(Business.Market.CommandExecutor[i].Type.ID);
                    if (!isPending)
                    {
                        //Begin log calculation swap
                        string strBeforeBalance = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Business.Market.CommandExecutor[i].Investor.Balance.ToString(), 2);
                        string strBeforeSwap = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Business.Market.CommandExecutor[i].Swap.ToString(), 2);
                        string strBeforeTotalSwap = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Business.Market.CommandExecutor[i].TotalSwap.ToString(), 2);
                        string strBid = string.Empty;
                        string strAsk = string.Empty;

                        #region CHECK IF COMMAND OPEN AFTER TIME CURRENT THEN DON'T CALCULATION SWAP
                        TimeSpan checkTime = timeCurrent - Business.Market.CommandExecutor[i].OpenTime;

                        if (checkTime.TotalSeconds < 0)
                            continue;
                        #endregion

                        bool IsEnable = false;
                        string Type = string.Empty;
                        double LongPosition = 0;
                        double ShortPosition = 0;
                        string ThreeDaySwaps = string.Empty;
                        bool UseOpenPrice = false;
                        int SpreadDifference = 0;
                        double CloseAsk = 0;
                        double closeAskCurrency = 0;
                        double SwapPrice = 0;

                        #region GET PARAMETER CALCULATION SWAP
                        if (Business.Market.CommandExecutor[i].Symbol.ParameterItems != null)
                        {
                            int countParameter = Business.Market.CommandExecutor[i].Symbol.ParameterItems.Count;
                            for (int j = 0; j < countParameter; j++)
                            {
                                if (Business.Market.CommandExecutor[i].Symbol.ParameterItems[j].Code == "S035")
                                {
                                    if (Business.Market.CommandExecutor[i].Symbol.ParameterItems[j].BoolValue == 1)
                                        IsEnable = true;
                                }

                                if (Business.Market.CommandExecutor[i].Symbol.ParameterItems[j].Code == "S036")
                                {
                                    Type = Business.Market.CommandExecutor[i].Symbol.ParameterItems[j].StringValue;
                                }

                                if (Business.Market.CommandExecutor[i].Symbol.ParameterItems[j].Code == "S037")
                                {
                                    double.TryParse(Business.Market.CommandExecutor[i].Symbol.ParameterItems[j].NumValue, out LongPosition);
                                }

                                if (Business.Market.CommandExecutor[i].Symbol.ParameterItems[j].Code == "S038")
                                {
                                    double.TryParse(Business.Market.CommandExecutor[i].Symbol.ParameterItems[j].NumValue, out ShortPosition);
                                }

                                if (Business.Market.CommandExecutor[i].Symbol.ParameterItems[j].Code == "S039")
                                {
                                    ThreeDaySwaps = Business.Market.CommandExecutor[i].Symbol.ParameterItems[j].StringValue;
                                }

                                if (Business.Market.CommandExecutor[i].Symbol.ParameterItems[j].Code == "S040")
                                {
                                    if (Business.Market.CommandExecutor[i].Symbol.ParameterItems[j].BoolValue == 1)
                                        UseOpenPrice = true;
                                }
                            }
                        }
                        #endregion

                        #region CHECK ISENABLE CALCULATION SWAP
                        if (IsEnable)
                        {
                            DayOfWeek ThreeDay = (DayOfWeek)Enum.Parse(typeof(DayOfWeek), ThreeDaySwaps, true);
                            int numThreeday = 1;
                            if (ThreeDay == timeCurrent.DayOfWeek)
                                numThreeday = 3;

                            switch (Type)
                            {
                                #region BY POINTS[ LOTS * LONG_OR_SHORT_POINTS * POINTSIZE ]
                                case "by points[ lots * long_or_short_points * pointsize ]":
                                    {
                                        #region PROCESS GET SPREAD DIFFIRENCES
                                        if (Business.Market.CommandExecutor[i].IGroupSecurity != null)
                                        {
                                            if (Business.Market.CommandExecutor[i].IGroupSecurity.IGroupSecurityConfig != null)
                                            {
                                                int countIGroupSecurityConfig = Business.Market.CommandExecutor[i].IGroupSecurity.IGroupSecurityConfig.Count;
                                                for (int n = 0; n < countIGroupSecurityConfig; n++)
                                                {
                                                    if (Business.Market.CommandExecutor[i].IGroupSecurity.IGroupSecurityConfig[n].Code == "B14")
                                                    {
                                                        int.TryParse(Business.Market.CommandExecutor[i].IGroupSecurity.IGroupSecurityConfig[n].NumValue, out SpreadDifference);
                                                        break;
                                                    }
                                                }
                                            }
                                        }
                                        #endregion

                                        #region PROCESS GET PRICE OF DAY
                                        ProcessQuoteLibrary.Business.BarTick BarTick1M = new ProcessQuoteLibrary.Business.BarTick();
                                        ProcessQuoteLibrary.Business.BarTick barTick1MCurrency = new ProcessQuoteLibrary.Business.BarTick();
                                        //Get Price Bid and Ask Close Of Day
                                        if (ProcessQuoteLibrary.Business.QuoteProcess.listQuotes != null)
                                        {
                                            #region GET ONLINE CANDLE WITH SYMBOL
                                            int countQuote = ProcessQuoteLibrary.Business.QuoteProcess.listQuotes.Count;
                                            for (int n = 0; n < countQuote; n++)
                                            {
                                                if (ProcessQuoteLibrary.Business.QuoteProcess.listQuotes[n].Name == Business.Market.CommandExecutor[i].Symbol.Name)
                                                {
                                                    BarTick1M = ProcessQuoteLibrary.Business.QuoteProcess.listQuotes[n].BarTick1M;
                                                    //CloseAsk = Business.Market.CommandExecutor[i].Symbol.CreateAskPrices(BarTick1M.Close, Business.Market.CommandExecutor[i].Symbol.SpreadByDefault,
                                                    //    Business.Market.CommandExecutor[i].Symbol.Digit, int.Parse(Business.Market.CommandExecutor[i].SpreaDifferenceInOpenTrade.ToString()));
                                                    CloseAsk = Business.Market.CommandExecutor[i].Symbol.CreateAskPrices(Business.Market.CommandExecutor[i].Symbol.Digit,
                                                        int.Parse(Business.Market.CommandExecutor[i].SpreaDifferenceInOpenTrade.ToString()), BarTick1M.CloseAsk);
                                                    break;
                                                }
                                            }
                                            #endregion

                                            #region GET ONLINE CANDLE WITH CURRENCY
                                            for (int m = 0; m < countQuote; m++)
                                            {
                                                if (ProcessQuoteLibrary.Business.QuoteProcess.listQuotes[m].Name.Trim() == Business.Market.CommandExecutor[i].Symbol.Currency.Trim())
                                                {
                                                    barTick1MCurrency = ProcessQuoteLibrary.Business.QuoteProcess.listQuotes[m].BarTick1M;

                                                    //GET DIGIT OF SYMBOL CURRENCY
                                                    if (Business.Market.SymbolList != null)
                                                    {
                                                        int countSymbol = Business.Market.SymbolList.Count;
                                                        for (int k = 0; k < countSymbol; k++)
                                                        {
                                                            if (Business.Market.SymbolList[k].Name.ToUpper().Trim() == Business.Market.CommandExecutor[i].Symbol.Currency.ToUpper().Trim())
                                                            {
                                                                //closeAskCurrency = Business.Market.CommandExecutor[i].Symbol.CreateAskPrices(barTick1MCurrency.Close, 0,
                                                                //   Business.Market.SymbolList[k].Digit, int.Parse(Business.Market.CommandExecutor[i].SpreaDifferenceInOpenTrade.ToString()));
                                                                closeAskCurrency = Business.Market.CommandExecutor[i].Symbol.CreateAskPrices(Business.Market.SymbolList[k].Digit,
                                                                    int.Parse(Business.Market.CommandExecutor[i].SpreaDifferenceInOpenTrade.ToString()), barTick1MCurrency.CloseAsk);
                                                                break;
                                                            }
                                                        }
                                                    }

                                                    break;
                                                }
                                            }
                                            #endregion
                                        }
                                        #endregion

                                        if (BarTick1M.Open > 0 && BarTick1M.Close > 0)
                                        {
                                            SwapPrice = Math.Round(Business.Market.CommandExecutor[i].Symbol.Points(Business.Market.CommandExecutor[i].Size, LongPosition, ShortPosition,
                                                    BarTick1M.Close, CloseAsk, Business.Market.CommandExecutor[i].Symbol.ContractSize,
                                                    Business.Market.CommandExecutor[i].Symbol.Digit, numThreeday, Business.Market.CommandExecutor[i].Type.ID), 2);

                                            //BUILD STRING BID/ASK SYMBOL
                                            strBid = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(BarTick1M.Close.ToString(), Business.Market.CommandExecutor[i].Symbol.Digit);
                                            strAsk = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(CloseAsk.ToString(), Business.Market.CommandExecutor[i].Symbol.Digit);

                                            //if (SwapPrice > 0)
                                            //    SwapPrice = -SwapPrice;

                                            Business.Market.CommandExecutor[i].TotalSwap += SwapPrice;

                                            TradingServer.Facade.FacadeUpdateTotalSwapOnlineCommand(Business.Market.CommandExecutor[i].ID, Business.Market.CommandExecutor[i].TotalSwap);

                                            double totalSwaps = Business.Market.CommandExecutor[i].Swap + SwapPrice;

                                            //Set new Swap To Command
                                            Business.Market.CommandExecutor[i].Swap = totalSwaps;

                                            //Call Fucntion Update Swap In Investor List
                                            bool resultUpdateInInvestorList = Market.marketInstance.FindAndUpdateSwapInInvestorList(Business.Market.CommandExecutor[i].ID,
                                                Business.Market.CommandExecutor[i].Investor.InvestorID, totalSwaps, Business.Market.CommandExecutor[i].TotalSwap);

                                            //Call Function Update Swap In Symbol List
                                            bool resultUpdateInSymbolList = Market.marketInstance.FindAndUpdateSwapInSymbolList(Business.Market.CommandExecutor[i].ID,
                                                Business.Market.CommandExecutor[i].Symbol.Name, totalSwaps, Business.Market.CommandExecutor[i].TotalSwap);

                                            //UPDATE SWAP ONLINE COMMAND
                                            TradingServer.Facade.FacadeUpdateSwapOnlineCommand(Business.Market.CommandExecutor[i].ID,
                                                Business.Market.CommandExecutor[i].Swap);

                                            isComplete = true;
                                        }
                                    }
                                    break;
                                #endregion

                                #region BY MONEY [ LOTS * LONG_OR-SHORT ]
                                case "by money [ lots * long_or-short ]":
                                    {
                                        SwapPrice = Math.Round(Business.Market.CommandExecutor[i].Symbol.Money(Business.Market.CommandExecutor[i].Size, LongPosition, ShortPosition, numThreeday,
                                            Business.Market.CommandExecutor[i].Type.ID), 2);

                                        //BUILD BID/ASK SYMBOL
                                        strBid = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Business.Market.CommandExecutor[i].Symbol.TickValue.Bid.ToString(),
                                            Business.Market.CommandExecutor[i].Symbol.Digit);
                                        strAsk = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Business.Market.CommandExecutor[i].Symbol.TickValue.Ask.ToString(),
                                            Business.Market.CommandExecutor[i].Symbol.Digit);

                                        //if (SwapPrice > 0)
                                        //    SwapPrice = -SwapPrice;
                                                                                
                                        Business.Market.CommandExecutor[i].TotalSwap += SwapPrice;

                                        TradingServer.Facade.FacadeUpdateTotalSwapOnlineCommand(Business.Market.CommandExecutor[i].ID, Business.Market.CommandExecutor[i].TotalSwap);

                                        double totalSwaps = Business.Market.CommandExecutor[i].Swap + SwapPrice;

                                        Business.Market.CommandExecutor[i].Swap = totalSwaps;

                                        //Call Fucntion Update Swap In Investor List
                                        bool resultUpdateInInvestorList = Market.marketInstance.FindAndUpdateSwapInInvestorList(Business.Market.CommandExecutor[i].ID,
                                            Business.Market.CommandExecutor[i].Investor.InvestorID, totalSwaps, Business.Market.CommandExecutor[i].TotalSwap);

                                        //Call Function Update Swap In Symbol List
                                        bool resultUpdateInSymbolList = Market.marketInstance.FindAndUpdateSwapInSymbolList(Business.Market.CommandExecutor[i].ID,
                                            Business.Market.CommandExecutor[i].Symbol.Name, totalSwaps, Business.Market.CommandExecutor[i].TotalSwap);

                                        //UPDATE SWAP IN DATABASE
                                        TradingServer.Facade.FacadeUpdateSwapOnlineCommand(Business.Market.CommandExecutor[i].ID,
                                            Business.Market.CommandExecutor[i].Swap);

                                        isComplete = true;  
                                    }
                                    break;
                                #endregion

                                #region BY INTEREST [ LOTS * LONG_OR_SHORT / 100 / 360 ]
                                case "by interest [ lots * long_or_short / 100 / 360 ]":
                                    {
                                        #region PROCESS GET SPREAD DIFFIRENCES
                                        if (Business.Market.CommandExecutor[i].IGroupSecurity != null)
                                        {
                                            if (Business.Market.CommandExecutor[i].IGroupSecurity.IGroupSecurityConfig != null)
                                            {
                                                int countIGroupSecurityConfig = Business.Market.CommandExecutor[i].IGroupSecurity.IGroupSecurityConfig.Count;
                                                for (int n = 0; n < countIGroupSecurityConfig; n++)
                                                {
                                                    if (Business.Market.CommandExecutor[i].IGroupSecurity.IGroupSecurityConfig[n].Code == "B14")
                                                    {
                                                        int.TryParse(Business.Market.CommandExecutor[i].IGroupSecurity.IGroupSecurityConfig[n].NumValue, out SpreadDifference);
                                                        break;
                                                    }
                                                }
                                            }
                                        }
                                        #endregion

                                        #region PROCESS GET PRICE OF DAY
                                        ProcessQuoteLibrary.Business.BarTick BarTick1M = new ProcessQuoteLibrary.Business.BarTick();
                                        ProcessQuoteLibrary.Business.BarTick barTick1MCurrency = new ProcessQuoteLibrary.Business.BarTick();
                                        //Get Price Bid and Ask Close Of Day
                                        if (ProcessQuoteLibrary.Business.QuoteProcess.listQuotes != null)
                                        {
                                            #region GET ONLINE CANDLE WITH SYMBOL
                                            int countQuote = ProcessQuoteLibrary.Business.QuoteProcess.listQuotes.Count;
                                            for (int n = 0; n < countQuote; n++)
                                            {
                                                if (ProcessQuoteLibrary.Business.QuoteProcess.listQuotes[n].Name == Business.Market.CommandExecutor[i].Symbol.Name)
                                                {
                                                    BarTick1M = ProcessQuoteLibrary.Business.QuoteProcess.listQuotes[n].BarTick1M;
                                                    //CloseAsk = Business.Market.CommandExecutor[i].Symbol.CreateAskPrices(BarTick1M.Close, Business.Market.CommandExecutor[i].Symbol.SpreadByDefault,
                                                    //    Business.Market.CommandExecutor[i].Symbol.Digit, int.Parse(Business.Market.CommandExecutor[i].SpreaDifferenceInOpenTrade.ToString()));
                                                    CloseAsk = Business.Market.CommandExecutor[i].Symbol.CreateAskPrices(Business.Market.CommandExecutor[i].Symbol.Digit,
                                                        int.Parse(Business.Market.CommandExecutor[i].SpreaDifferenceInOpenTrade.ToString()), BarTick1M.CloseAsk);
                                                    break;
                                                }
                                            }
                                            #endregion

                                            #region GET ONLINE CANDLE WITH CURRENCY
                                            for (int m = 0; m < countQuote; m++)
                                            {
                                                if (ProcessQuoteLibrary.Business.QuoteProcess.listQuotes[m].Name.Trim() == Business.Market.CommandExecutor[i].Symbol.Currency.Trim())
                                                {
                                                    barTick1MCurrency = ProcessQuoteLibrary.Business.QuoteProcess.listQuotes[m].BarTick1M;

                                                    //GET DIGIT OF SYMBOL CURRENCY
                                                    if (Business.Market.SymbolList != null)
                                                    {
                                                        int countSymbol = Business.Market.SymbolList.Count;
                                                        for (int k = 0; k < countSymbol; k++)
                                                        {
                                                            if (Business.Market.SymbolList[k].Name.ToUpper().Trim() == Business.Market.CommandExecutor[i].Symbol.Currency.ToUpper().Trim())
                                                            {
                                                                //closeAskCurrency = Business.Market.CommandExecutor[i].Symbol.CreateAskPrices(barTick1MCurrency.Close, 0,
                                                                //   Business.Market.SymbolList[k].Digit, int.Parse(Business.Market.CommandExecutor[i].SpreaDifferenceInOpenTrade.ToString()));
                                                                closeAskCurrency = Business.Market.CommandExecutor[i].Symbol.CreateAskPrices(Business.Market.SymbolList[k].Digit,
                                                                    int.Parse(Business.Market.CommandExecutor[i].SpreaDifferenceInOpenTrade.ToString()), barTick1MCurrency.CloseAsk);
                                                                break;
                                                            }
                                                        }
                                                    }

                                                    break;
                                                }
                                            }
                                            #endregion
                                        }
                                        #endregion

                                        //Call Function Convert If Symbol Is XXXUSD    
                                        #region GET LONG AND SHORT IN IGROUPSYMBOL
                                        if (Business.Market.IGroupSymbolList != null)
                                        {
                                            int countIGroupSymbol = Business.Market.IGroupSymbolList.Count;
                                            for (int j = 0; j < countIGroupSymbol; j++)
                                            {
                                                if (Business.Market.IGroupSymbolList[j].SymbolID == Business.Market.CommandExecutor[i].Symbol.SymbolID &&
                                                    Business.Market.IGroupSymbolList[j].InvestorGroupID == Business.Market.CommandExecutor[i].Investor.InvestorGroupInstance.InvestorGroupID)
                                                {
                                                    if (Business.Market.IGroupSymbolList[j].IGroupSymbolConfig != null)
                                                    {
                                                        int countParameter = Business.Market.IGroupSymbolList[j].IGroupSymbolConfig.Count;
                                                        for (int n = 0; n < countParameter; n++)
                                                        {
                                                            if (Business.Market.IGroupSymbolList[j].IGroupSymbolConfig[n].Code == "GS01")
                                                            {
                                                                LongPosition = double.Parse(Business.Market.IGroupSymbolList[j].IGroupSymbolConfig[n].NumValue);
                                                            }

                                                            if (Business.Market.IGroupSymbolList[j].IGroupSymbolConfig[n].Code == "GS02")
                                                            {
                                                                ShortPosition = double.Parse(Business.Market.IGroupSymbolList[j].IGroupSymbolConfig[n].NumValue);
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                        #endregion

                                        //Calculation Swaps
                                        SwapPrice = Math.Round(Business.Market.CommandExecutor[i].Symbol.Interest(Business.Market.CommandExecutor[i].Size, LongPosition, ShortPosition, BarTick1M.Close,
                                            Business.Market.CommandExecutor[i].Symbol.ContractSize, numThreeday, Business.Market.CommandExecutor[i].Type.ID), 2);

                                        //BUILD BID/ASK SYMBOL
                                        strBid = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(BarTick1M.Close.ToString(), Business.Market.CommandExecutor[i].Symbol.Digit);
                                        strAsk = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(CloseAsk.ToString(), Business.Market.CommandExecutor[i].Symbol.Digit);

                                        double closePrice = 0;

                                        if (Business.Market.CommandExecutor[i].Type.ID == 1)
                                            closePrice = barTick1MCurrency.Close;
                                        else
                                            closePrice = closeAskCurrency;

                                        SwapPrice = Business.Market.CommandExecutor[i].Symbol.ConvertCurrencyToUSD(Business.Market.CommandExecutor[i].Symbol.Currency, SwapPrice, closePrice, 2);

                                        //if (SwapPrice > 0)
                                        //    SwapPrice = -SwapPrice;
                                                                                
                                        Business.Market.CommandExecutor[i].TotalSwap += SwapPrice;

                                        TradingServer.Facade.FacadeUpdateTotalSwapOnlineCommand(Business.Market.CommandExecutor[i].ID, Business.Market.CommandExecutor[i].TotalSwap);

                                        double totalSwaps = Business.Market.CommandExecutor[i].Swap + SwapPrice;

                                        //Set new Swap To Command
                                        Business.Market.CommandExecutor[i].Swap = totalSwaps;

                                        //Call Fucntion Update Swap In Investor List
                                        bool resultUpdateInInvestorList = Market.marketInstance.FindAndUpdateSwapInInvestorList(Business.Market.CommandExecutor[i].ID,
                                            Business.Market.CommandExecutor[i].Investor.InvestorID, totalSwaps, Business.Market.CommandExecutor[i].TotalSwap);

                                        //Call Function Update Swap In Symbol List
                                        bool resultUpdateInSymbolList = Market.marketInstance.FindAndUpdateSwapInSymbolList(Business.Market.CommandExecutor[i].ID,
                                            Business.Market.CommandExecutor[i].Symbol.Name, totalSwaps, Business.Market.CommandExecutor[i].TotalSwap);

                                        //UPDATE SWAP IN DATABASE   
                                        TradingServer.Facade.FacadeUpdateSwapOnlineCommand(Business.Market.CommandExecutor[i].ID,
                                            Business.Market.CommandExecutor[i].Swap);

                                        isComplete = true;
                                    }
                                    break;
                                #endregion

                                #region BY MONEY IN MARGIN CURRENCY [ LOTS * LONG_OR_SHORT ]
                                case "by money in margin currency [ lots * long_or_short ]":
                                    {
                                        #region PROCESS GET SPREAD DIFFIRENCES
                                        if (Business.Market.CommandExecutor[i].IGroupSecurity != null)
                                        {
                                            if (Business.Market.CommandExecutor[i].IGroupSecurity.IGroupSecurityConfig != null)
                                            {
                                                int countIGroupSecurityConfig = Business.Market.CommandExecutor[i].IGroupSecurity.IGroupSecurityConfig.Count;
                                                for (int n = 0; n < countIGroupSecurityConfig; n++)
                                                {
                                                    if (Business.Market.CommandExecutor[i].IGroupSecurity.IGroupSecurityConfig[n].Code == "B14")
                                                    {
                                                        int.TryParse(Business.Market.CommandExecutor[i].IGroupSecurity.IGroupSecurityConfig[n].NumValue, out SpreadDifference);
                                                        break;
                                                    }
                                                }
                                            }
                                        }
                                        #endregion

                                        #region PROCESS GET PRICE OF DAY
                                        ProcessQuoteLibrary.Business.BarTick BarTick1M = new ProcessQuoteLibrary.Business.BarTick();
                                        ProcessQuoteLibrary.Business.BarTick barTick1MCurrency = new ProcessQuoteLibrary.Business.BarTick();
                                        //Get Price Bid and Ask Close Of Day
                                        if (ProcessQuoteLibrary.Business.QuoteProcess.listQuotes != null)
                                        {
                                            #region GET ONLINE CANDLE WITH SYMBOL
                                            int countQuote = ProcessQuoteLibrary.Business.QuoteProcess.listQuotes.Count;
                                            for (int n = 0; n < countQuote; n++)
                                            {
                                                if (ProcessQuoteLibrary.Business.QuoteProcess.listQuotes[n].Name == Business.Market.CommandExecutor[i].Symbol.Name)
                                                {
                                                    BarTick1M = ProcessQuoteLibrary.Business.QuoteProcess.listQuotes[n].BarTick1M;
                                                    //CloseAsk = Business.Market.CommandExecutor[i].Symbol.CreateAskPrices(BarTick1M.Close, Business.Market.CommandExecutor[i].Symbol.SpreadByDefault,
                                                    //    Business.Market.CommandExecutor[i].Symbol.Digit, int.Parse(Business.Market.CommandExecutor[i].SpreaDifferenceInOpenTrade.ToString()));
                                                    CloseAsk = Business.Market.CommandExecutor[i].Symbol.CreateAskPrices(Business.Market.CommandExecutor[i].Symbol.Digit,
                                                        int.Parse(Business.Market.CommandExecutor[i].SpreaDifferenceInOpenTrade.ToString()), BarTick1M.CloseAsk);
                                                    break;
                                                }
                                            }
                                            #endregion

                                            #region GET ONLINE CANDLE WITH CURRENCY
                                            for (int m = 0; m < countQuote; m++)
                                            {
                                                if (ProcessQuoteLibrary.Business.QuoteProcess.listQuotes[m].Name.Trim() == Business.Market.CommandExecutor[i].Symbol.Currency.Trim())
                                                {
                                                    barTick1MCurrency = ProcessQuoteLibrary.Business.QuoteProcess.listQuotes[m].BarTick1M;

                                                    //GET DIGIT OF SYMBOL CURRENCY
                                                    if (Business.Market.SymbolList != null)
                                                    {
                                                        int countSymbol = Business.Market.SymbolList.Count;
                                                        for (int k = 0; k < countSymbol; k++)
                                                        {
                                                            if (Business.Market.SymbolList[k].Name.ToUpper().Trim() == Business.Market.CommandExecutor[i].Symbol.Currency.ToUpper().Trim())
                                                            {
                                                                //closeAskCurrency = Business.Market.CommandExecutor[i].Symbol.CreateAskPrices(barTick1MCurrency.Close, 0,
                                                                //   Business.Market.SymbolList[k].Digit, int.Parse(Business.Market.CommandExecutor[i].SpreaDifferenceInOpenTrade.ToString()));
                                                                closeAskCurrency = Business.Market.CommandExecutor[i].Symbol.CreateAskPrices(Business.Market.SymbolList[k].Digit,
                                                                    int.Parse(Business.Market.CommandExecutor[i].SpreaDifferenceInOpenTrade.ToString()), barTick1MCurrency.CloseAsk);
                                                                break;
                                                            }
                                                        }
                                                    }

                                                    break;
                                                }
                                            }
                                            #endregion
                                        }
                                        #endregion

                                        //Calculation Swap
                                        SwapPrice = Math.Round(Business.Market.CommandExecutor[i].Symbol.MoneyInMarginCurrency(Business.Market.CommandExecutor[i].Size,
                                            LongPosition, ShortPosition, numThreeday, Business.Market.CommandExecutor[i].Type.ID), 2);

                                        //BUILD BID/ASK SYMBOL
                                        strBid = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(BarTick1M.Close.ToString(), Business.Market.CommandExecutor[i].Symbol.Digit);
                                        strAsk = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(CloseAsk.ToString(), Business.Market.CommandExecutor[i].Symbol.Digit);

                                        double closePrice = 0;

                                        if (Business.Market.CommandExecutor[i].Type.ID == 1)
                                            closePrice = barTick1MCurrency.Close;
                                        else
                                            closePrice = closeAskCurrency;

                                        Business.Market.CommandExecutor[i].Symbol.ConvertCurrencyToUSD(Business.Market.CommandExecutor[i].Symbol.Currency, SwapPrice, closePrice, 2);

                                        //if (SwapPrice > 0)
                                        //    SwapPrice = -SwapPrice;

                                        Business.Market.CommandExecutor[i].TotalSwap += SwapPrice;

                                        TradingServer.Facade.FacadeUpdateTotalSwapOnlineCommand(Business.Market.CommandExecutor[i].ID, Business.Market.CommandExecutor[i].TotalSwap);

                                        double totalSwaps = Business.Market.CommandExecutor[i].Swap + SwapPrice;
                                        Business.Market.CommandExecutor[i].Swap = totalSwaps;

                                        //Call Fucntion Update Swap In Investor List
                                        bool resultUpdateInInvestorList = Market.marketInstance.FindAndUpdateSwapInInvestorList(Business.Market.CommandExecutor[i].ID,
                                            Business.Market.CommandExecutor[i].Investor.InvestorID, totalSwaps, Business.Market.CommandExecutor[i].TotalSwap);

                                        //Call Function Update Swap In Symbol List
                                        bool resultUpdateInSymbolList = Market.marketInstance.FindAndUpdateSwapInSymbolList(Business.Market.CommandExecutor[i].ID,
                                            Business.Market.CommandExecutor[i].Symbol.Name, totalSwaps, Business.Market.CommandExecutor[i].TotalSwap);

                                        //UPDATE SWAP IN DATABASE
                                        TradingServer.Facade.FacadeUpdateSwapOnlineCommand(Business.Market.CommandExecutor[i].ID,
                                            Business.Market.CommandExecutor[i].Swap);

                                        isComplete = true;
                                    }
                                    break;
                                #endregion
                            }

                            if (isComplete)
                            {
                                //End login calculation swap
                                string strAfterBalance = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Business.Market.CommandExecutor[i].Investor.Balance.ToString(), 2);
                                string strAfterSwap = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(SwapPrice.ToString(), 2);
                                string strAfterTotalSwap = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Business.Market.CommandExecutor[i].TotalSwap.ToString(), 2);
                                string strSize = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Business.Market.CommandExecutor[i].Size.ToString(), 2);

                                //'00001140: calculation swap order #004432443 - current balance: 40000 - old swap: 5.00'
                                string content = "'" + Business.Market.CommandExecutor[i].Investor.Code + "': calculation swap order #" + Business.Market.CommandExecutor[i].CommandCode +
                                    " name: " + Business.Market.CommandExecutor[i].Symbol.Name + " size: " + strSize + " old balance: " + strBeforeBalance + " - old swap: " + strBeforeSwap +
                                    " - old total swap: " + strBeforeTotalSwap + " -> " + "current balance: " + strAfterBalance + " - current swap: " + strAfterSwap + " - current total swap: " + strAfterTotalSwap + "(" + strBid + "/" + strAsk + ")";

                                TradingServer.Facade.FacadeAddNewSystemLog(5, content, "[calculation swap]", "", Business.Market.CommandExecutor[i].Investor.Code);

                                //SEND NOTIFY TO MANAGER CHANGE COMMAND
                                TradingServer.Facade.FacadeSendNoticeManagerRequest(1, Business.Market.CommandExecutor[i]);

                                //SEND NOTIFY TO MANAGER CHANGE ACCOUNT
                                TradingServer.Facade.FacadeSendNotifyManagerRequest(3, Business.Market.CommandExecutor[i].Investor);

                                #region SEND NOTIFY TO INVESTOR IF IT ONLINE
                                if (Business.Market.CommandExecutor[i].Investor.IsOnline)
                                {
                                    //Send Command To Client, Get Account And Online Command
                                    string MessageGetAccount = "CSW5789";

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

                                    Business.Market.CommandExecutor[i].Investor.ClientCommandQueue.Add(MessageGetAccount);
                                }
                                #endregion

                                #region SEND NOTIFY TO AGENT SYSTEM
                                string msg = "UpdateCommand$True,Update Command," + Business.Market.CommandExecutor[i].ID + "," +
                                        Business.Market.CommandExecutor[i].Investor.InvestorID + "," + Business.Market.CommandExecutor[i].Symbol.Name + "," +
                                        Business.Market.CommandExecutor[i].Size + "," + true + "," + Business.Market.CommandExecutor[i].OpenTime + "," +
                                        Business.Market.CommandExecutor[i].OpenPrice + "," + Business.Market.CommandExecutor[i].StopLoss + "," +
                                        Business.Market.CommandExecutor[i].TakeProfit + "," + Business.Market.CommandExecutor[i].ClosePrice + "," +
                                        Business.Market.CommandExecutor[i].Commission + "," + Business.Market.CommandExecutor[i].Swap + "," +
                                        Business.Market.CommandExecutor[i].Profit + "," + Business.Market.CommandExecutor[i].Comment + "," +
                                        Business.Market.CommandExecutor[i].ID + "," + Business.Market.CommandExecutor[i].Type.Name + "," + 1 + "," +
                                        Business.Market.CommandExecutor[i].ExpTime + "," + Business.Market.CommandExecutor[i].ClientCode + "," +
                                        Business.Market.CommandExecutor[i].CommandCode + "," + Business.Market.CommandExecutor[i].IsHedged + "," +
                                        Business.Market.CommandExecutor[i].Type.ID + "," + Business.Market.CommandExecutor[i].Margin + ",Update" + "," +
                                        Business.Market.CommandExecutor[i].AgentRefConfig + "," + Business.Market.CommandExecutor[i].SpreaDifferenceInOpenTrade;

                                //SEND NOTIFY UPDATE COMMAND TO AGENT
                                Business.AgentNotify newAgentNotify = new AgentNotify();
                                newAgentNotify.NotifyMessage = msg;
                                TradingServer.Agent.AgentConfig.Instance.AddNotifyToAgent(newAgentNotify, Business.Market.CommandExecutor[i].Investor.InvestorGroupInstance);
                                #endregion
                            }
                        }
                        #endregion
                    }
                }
            }
            #endregion

            if (Business.Market.InvestorList != null)
            {
                TradingServer.Facade.FacadeAddNewCommandHistory(Business.Market.InvestorList, timeCurrent);
            }
        }
        /// <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>
        /// <param name="investorID"></param>
        /// <param name="oldPwd"></param>
        /// <param name="newPwd"></param>
        /// <returns></returns>
        internal bool ChangeReadOnlyPassword(int investorID, string oldPwd, string newPwd)
        {
            bool result = false;
            string hash = TradingServer.Model.ValidateCheck.Encrypt(newPwd);
            //VERIFY OLD PASSWORD
            string hashOldPwd = TradingServer.Model.ValidateCheck.Encrypt(oldPwd);
            //bool checkMasterPwd = TradingServer.Facade.FacadeCheckMasterPassword(investorID, hashOldPwd);

            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)
                    {
                        if (Business.Market.InvestorList[i].AllowChangePwd)
                        {
                            if (Business.Market.InvestorList[i].ReadOnlyPwd == hashOldPwd)
                            {
                                bool updatePwd = Investor.DBWInvestorInstance.UpdatePrimaryPasword(investorID, hash);
                                if (updatePwd == true)
                                {
                                    Business.Market.InvestorList[i].ReadOnlyPwd = hash;
                                    result = true;

                                    //SEND NOTIFY TO AGENT SYSTEM
                                    //SEND COMMAND TO AGENT SERVER
                                    string Message = "UpdateReadOnlyPassword$" + result + "{" + hash + "{" + investorID;
                                    Business.AgentNotify newAgentNotify = new AgentNotify();
                                    newAgentNotify.NotifyMessage = Message;
                                    TradingServer.Agent.AgentConfig.Instance.AddNotifyToAgent(newAgentNotify);
                                }
                            }
                        }
                        else
                        {
                            return false;
                        }

                        break;
                    }
                }
            }

            return result;
        }
Exemplo n.º 16
0
        /// <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>
        /// EXTRACT COMMAND AND CALL FUNCTION 
        /// </summary>
        /// <param name="Cmd"></param>
        /// <returns></returns>
        public string ExtractCommandServer(string Cmd, string ipAddress, string code)
        {
            string StringResult = string.Empty;
            string Command = string.Empty;
            string Value = string.Empty;
            if (!string.IsNullOrEmpty(Cmd))
            {
                string[] subValue = new string[2];

                int Position = -1;
                Position = Cmd.IndexOf('$');
                if (Position > 0)
                {
                    Command = Cmd.Substring(0, Position);
                    Value = Cmd.Substring(Position + 1);

                    subValue[0] = Command;
                    subValue[1] = Value;
                }
                else
                {
                    subValue[0] = Cmd;
                }

                if (subValue.Length > 0)
                {
                    switch (subValue[0])
                    {
                        //Command Add New
                        //
                        #region Add Investor Group(LOG)
                        case "AddInvestorGroup":
                            {
                                bool checkip = Facade.FacadeCheckIpAdmin(code, ipAddress);
                                if (checkip)
                                {
                                    int ResultAddNew = -1;
                                    Business.InvestorGroup Result = new InvestorGroup();
                                    Result = this.ExtractInvestorGroup(subValue[1]);
                                    //Call Function Add New Investor Group
                                    ResultAddNew = TradingServer.Facade.FacadeAddNewInvestorGroup(Result);

                                    #region INSERT SYSTEM LOG
                                    //INSERT SYSTEM LOG
                                    //'2222': group config added/changed ['test-duc']
                                    string status = "[Failed]";

                                    if (ResultAddNew > 0)
                                    {
                                        status = "[successs]";
                                        TradingServer.Facade.FacadeCheckUpdateGroupVirtualDealerOnline();
                                    }

                                    string content = "'" + code + "': group config added/changed ['" + Result.Name + "'] " + status;
                                    string comment = "[add new group]";
                                    TradingServer.Facade.FacadeAddNewSystemLog(3, content, comment, ipAddress, code);
                                    #endregion

                                    if (ResultAddNew > 0) TradingServer.Facade.FacadeSendNoticeManagerChangeGroup(3, ResultAddNew);
                                    StringResult = subValue[0] + "$" + ResultAddNew.ToString();
                                }
                                else
                                {
                                    StringResult = subValue[0] + "$MCM005";
                                }
                            }
                            break;
                        #endregion

                        #region Add Investor Group Config
                        case "AddInvestorGroupConfig":
                            {
                                bool checkip = Facade.FacadeCheckIpAdmin(code, ipAddress);
                                if (checkip)
                                {
                                    int ResultAddNew = -1;
                                    List<Business.ParameterItem> Result = new List<ParameterItem>();
                                    Result = this.ExtractParameterItem(subValue[1]);
                                    //Call Function Add New Investor Group Config
                                    ResultAddNew = TradingServer.Facade.FacadeAddNewInvestorGroupConfig(Result);
                                    if (ResultAddNew > 0) Facade.FacadeSendNoticeManagerChangeGroup(2, Result[0].SecondParameterID);
                                    StringResult = subValue[0] + "$" + ResultAddNew.ToString();

                                    //SEND COMMAND TO AGENT SERVER
                                    string strAgent = "AddInvestorGroup$" + Result[0].SecondParameterID;
                                    Business.AgentNotify newAgentNotify = new AgentNotify();
                                    newAgentNotify.NotifyMessage = strAgent;
                                    TradingServer.Agent.AgentConfig.Instance.AddNotifyToAgent(newAgentNotify);
                                }
                                else
                                {
                                    StringResult = subValue[0] + "$MCM005";
                                }
                            }
                            break;
                        #endregion

                        #region Add New Security(LOG)
                        case "AddSecurity":
                            {
                                bool checkip = Facade.FacadeCheckIpAdmin(code, ipAddress);
                                if (checkip)
                                {
                                    if (!string.IsNullOrEmpty(subValue[1]))
                                    {
                                        int ResultAddNew = -1;
                                        int MarketAreaID = -1;
                                        string[] subParameter = subValue[1].Split(',');
                                        int.TryParse(subParameter[2], out MarketAreaID);
                                        if (subParameter.Length > 0)
                                        {
                                            ResultAddNew = TradingServer.Facade.FacadeAddSecurity(subParameter[0], subParameter[1], MarketAreaID);
                                        }

                                        #region INSERT SYSTEM LOG
                                        //INSERT SYSTEM LOG
                                        //'2222': security config added/changed ['test-duc']
                                        string status = "[Failed]";

                                        if (ResultAddNew > 0)
                                            status = "[Success]";

                                        string content = "'" + code + "': security config added/changed ['" + subParameter[0] + "'] " + status;
                                        string comment = "[add new security]";
                                        TradingServer.Facade.FacadeAddNewSystemLog(3, content, comment, ipAddress, code);
                                        #endregion

                                        StringResult = subValue[0] + "$" + ResultAddNew.ToString();
                                    }
                                }
                                else
                                {
                                    StringResult = subValue[0] + "$MCM005";
                                }
                            }
                            break;
                        #endregion

                        #region Add New SecurityConfig
                        case "AddSecurityConfig":
                            {
                                int ResultAddNew = -1;
                                List<Business.ParameterItem> Result = new List<ParameterItem>();
                                Result = this.ExtractParameterItem(subValue[1]);
                                ResultAddNew = TradingServer.Facade.FacadeAddSecurityConfig(Result);
                                StringResult = subValue[0] + "$" + ResultAddNew.ToString();
                            }
                            break;
                        #endregion

                        #region Add New Symbol(LOG)
                        case "AddNewSymbol":
                            {
                                bool checkip = Facade.FacadeCheckIpAdmin(code, ipAddress);
                                if (checkip)
                                {
                                    if (!string.IsNullOrEmpty(subValue[1]))
                                    {
                                        int ResultAddNew = -1;
                                        string[] subParameter = subValue[1].Split(',');
                                        if (subParameter.Length > 0)
                                        {
                                            int SecurityID = 0;
                                            int RefSymbolID = 0;
                                            int MarketAreaID = 0;

                                            int.TryParse(subParameter[0], out SecurityID);
                                            int.TryParse(subParameter[1], out RefSymbolID);
                                            int.TryParse(subParameter[2], out MarketAreaID);

                                            //Call Function Add New Symbol
                                            ResultAddNew = TradingServer.Facade.FacadeAddNewSymbol(SecurityID, RefSymbolID, MarketAreaID, subParameter[3]);

                                            #region INSERT SYSTEM LOG
                                            //INSERT SYSTEM LOG
                                            //'2222': symbol config added/changed ['XAUUSD']
                                            string status = "[Failed]";

                                            if (ResultAddNew > 0)
                                            {
                                                status = "[Success]";
                                                TradingServer.Facade.FacadeCheckUpdateGroupVirtualDealerOnline();
                                            }

                                            string content = "'" + code + "': symbol config added/changed ['" + subParameter[3] + "'] " + status;
                                            string comment = "[add new security]";
                                            TradingServer.Facade.FacadeAddNewSystemLog(3, content, comment, ipAddress, code);
                                            #endregion

                                            if (ResultAddNew > 0) Facade.FacadeSendNoticeManagerChangeSymbol(3, ResultAddNew);
                                            StringResult = subValue[0] + "$" + ResultAddNew.ToString();
                                        }
                                    }
                                }
                                else
                                {
                                    StringResult = subValue[0] + "$MCM005";
                                }
                            }
                            break;
                        #endregion

                        #region Add New TradingConfig(SymbolConfig)
                        case "AddTradingConfig":
                            {
                                bool checkip = Facade.FacadeCheckIpAdmin(code, ipAddress);
                                if (checkip)
                                {
                                    int ResultAddNew = -1;
                                    List<Business.ParameterItem> Result = new List<ParameterItem>();
                                    Result = this.ExtractParameterItem(subValue[1]);
                                    //Call Function Add New TradingConfig(SymbolConfig)
                                    ResultAddNew = TradingServer.Facade.FacadeAddNewTradingConfig(Result);
                                    if (ResultAddNew > 0) Facade.FacadeSendNoticeManagerChangeSymbol(2, Result[0].SecondParameterID);
                                    StringResult = subValue[0] + "$" + ResultAddNew.ToString();

                                    //SEND COMMAND TO AGENT SERVER
                                    string strAgent = "AddNewSymbol$" + Result[0].SecondParameterID;
                                    Business.AgentNotify newAgentNotify = new AgentNotify();
                                    newAgentNotify.NotifyMessage = strAgent;
                                    TradingServer.Agent.AgentConfig.Instance.AddNotifyToAgent(newAgentNotify);
                                }
                                else
                                {
                                    StringResult = subValue[0] + "$MCM005";
                                }
                            }
                            break;
                        #endregion

                        #region Add New Investor(LOG)
                        case "AddNewInvestor":
                            {
                                int ResultAddNew = -1;
                                Business.Investor Result = new Investor();
                                Result = this.ExtractionInvestor(subValue[1]);

                                //Check Email Add Send Mail Confirm Create Account Complete
                                bool checkEmail = Model.TradingCalculate.Instance.IsEmail(Result.Email);

                                if (checkEmail)
                                {
                                    bool CheckCode = false;
                                    CheckCode = TradingServer.Facade.FacadeGetInvestorByCode(Result.Code);
                                    if (CheckCode == true)
                                    {
                                        double balance = Result.Balance;
                                        Result.Balance = 0;
                                        bool checkip = Facade.FacadeCheckIpManagerAndAdmin(code, ipAddress);
                                        if (checkip)
                                        {
                                            bool checkRule = Facade.FacadeCheckPermitAccountManagerAndAdmin(code);
                                            bool checkGroup = Facade.FacadeCheckPermitAccessGroupManagerAndAdmin(code, Result.InvestorGroupInstance.InvestorGroupID);
                                            if (checkRule && checkGroup)
                                            {
                                                ResultAddNew = TradingServer.Facade.FacadeAddNewInvestor(Result);

                                                //Add Investor To Investor List
                                                if (ResultAddNew > 0)
                                                {
                                                    Result.InvestorID = ResultAddNew;
                                                    int resultProfileID = TradingServer.Facade.FacadeAddInvestorProfile(Result);
                                                    Result.InvestorProfileID = resultProfileID;
                                                    if (string.IsNullOrEmpty(Result.AgentID))
                                                        Result.AgentID = "0";

                                                    Business.Market.InvestorList.Add(Result);

                                                    //Deposit account
                                                    Result.Deposit(balance, ResultAddNew, "deposit");
                                                }

                                                StringResult = subValue[0] + "$" + ResultAddNew.ToString() + "," + Result.Code;

                                                #region INSERT SYSTEM LOG
                                                //INSERT SYSTEM LOG
                                                //'2222': new account '9942881' - ngthanhduc
                                                string status = "[Failed]";

                                                if (ResultAddNew > 0)
                                                    status = "[Success]";

                                                string content = "'" + code + "': new account '" + Result.Code + "' - " + Result.NickName + " " + status;
                                                string comment = "[add new account]";
                                                TradingServer.Facade.FacadeAddNewSystemLog(3, content, comment, ipAddress, code);
                                                #endregion
                                            }
                                            else
                                            {
                                                StringResult = subValue[0] + "$MCM006";
                                                string content = "'" + code + "': new account '" + Result.Code + "' - " + Result.NickName + " failed(not enough rights)";
                                                string comment = "[add new account]";
                                                TradingServer.Facade.FacadeAddNewSystemLog(3, content, comment, ipAddress, code);
                                            }
                                            //SEND NOTIFY TO MANAGER
                                            //TradingServer.Facade.FacadeSendNotifyManagerRequest(3, Result);

                                            //TradingServer.Facade.FacadeAutoSendMailRegistration(Result);
                                        }
                                        else
                                        {
                                            StringResult = subValue[0] + "$MCM005";
                                            string content = "'" + code + "': new account '" + Result.Code + "' - " + Result.NickName + " failed(invalid ip)";
                                            string comment = "[add new account]";
                                            TradingServer.Facade.FacadeAddNewSystemLog(3, content, comment, ipAddress, code);
                                        }
                                    }
                                    else
                                    {
                                        StringResult = subValue[0] + "$Account exist";
                                        string content = "'" + code + "': new account '" + Result.Code + "' - " + Result.NickName + " failed(account exist)";
                                        string comment = "[add new account]";
                                        TradingServer.Facade.FacadeAddNewSystemLog(3, content, comment, ipAddress, code);
                                    }
                                }
                                else
                                {
                                    StringResult = subValue[0] + "$Invalid email";
                                    string content = "'" + code + "': new account '" + Result.Code + "' - " + Result.NickName + " failed(invalid email)";
                                    string comment = "[add new account]";
                                    TradingServer.Facade.FacadeAddNewSystemLog(3, content, comment, ipAddress, code);
                                }
                            }
                            break;
                        #endregion

                        #region Add New Agent(LOG)
                        case "AddNewAgent":
                            {
                                bool checkip = Facade.FacadeCheckIpAdmin(code, ipAddress);
                                if (checkip)
                                {
                                    string temp = "";
                                    if (!string.IsNullOrEmpty(subValue[1]))
                                    {
                                        Business.Agent result = new Agent();

                                        #region Create Agent
                                        string[] subParameter = subValue[1].Split('}');
                                        if (subParameter.Length > 0)
                                        {
                                            int agentGroupID = -1;
                                            bool isDisable = false;
                                            bool isIpFilter = false;

                                            int.TryParse(subParameter[0], out agentGroupID);
                                            result.Name = subParameter[1];
                                            result.Comment = subParameter[3];
                                            bool.TryParse(subParameter[4], out isDisable);
                                            bool.TryParse(subParameter[5], out isIpFilter);

                                            result.AgentGroupID = agentGroupID;
                                            result.IsDisable = isDisable;
                                            result.IsIpFilter = isIpFilter;
                                            result.IpForm = subParameter[6];
                                            result.IpTo = subParameter[7];
                                            result.Code = subParameter[8];
                                            result.Pwd = subParameter[9];
                                            result.GroupCondition = subParameter[10];
                                        }
                                        #endregion

                                        int isSpace = result.Code.IndexOf(' ');
                                        if (isSpace > 0)
                                        {
                                            temp = "invalid code login";

                                            string strContent = "'" + code + "': add new agent [Failed] (invalid code login)";
                                            TradingServer.Facade.FacadeAddNewSystemLog(3, strContent, "[add agent]",ipAddress, code);
                                        }
                                        else
                                        {
                                            bool inves = Facade.FacadeGetInvestorByCode(result.Code);
                                            if (inves)
                                            {
                                                int ResultAddNew = TradingServer.Facade.FacadeAddNewAgent(result);

                                                #region INSERT SYSTEM LOG
                                                //INSERT SYSTEM LOG
                                                //'2222': manager config added/changed ['212121']
                                                string status = "[Failed]";

                                                if (ResultAddNew > 0)
                                                    status = "[Success]";

                                                string content = "'" + code + "': manager config added/changed ['" + subParameter[8] + "']" + status;
                                                string comment = "[add agent]";
                                                TradingServer.Facade.FacadeAddNewSystemLog(3, content, comment, ipAddress, code);
                                                #endregion

                                                temp = ResultAddNew.ToString();
                                            }
                                            else temp = "Account is exist";
                                        }
                                    }
                                    StringResult = subValue[0] + "$" + temp;
                                }
                                else
                                {
                                    StringResult = subValue[0] + "$MCM005";
                                }
                            }
                            break;
                        #endregion

                        #region Add News
                        case "AddNews":
                            {
                                int ResultAddNew = -1;
                                if (!string.IsNullOrEmpty(subValue[1]))
                                {
                                    string content = subValue[1];
                                    if (subValue.Length > 2)
                                    {
                                        for (int i = 2; i < subValue.Length; i++)
                                        {
                                            content += subValue[i];
                                        }
                                    }
                                    Business.News result = new News();

                                    #region Create News
                                    string[] subParameter = content.Split('█');
                                    if (subParameter.Length > 2)
                                    {
                                        string title = subParameter[0];
                                        string body = subParameter[1];
                                        string category = subParameter[2];
                                        ResultAddNew = Facade.FacadeAddNews(title, body, DateTime.Now, category);
                                    }
                                    #endregion
                                }
                                StringResult = subValue[0] + "$" + ResultAddNew;
                            }
                            break;
                        #endregion

                        #region Add New Alert
                        case "AddNewAlert":
                            {
                                int ResultAddNew = -1;
                                string timeCreate = "";
                                if (!string.IsNullOrEmpty(subValue[1]))
                                {
                                    Business.PriceAlert result = new PriceAlert();

                                    #region Create Alert
                                    string[] subParameter = subValue[1].Split(',');
                                    if (subParameter.Length > 0)
                                    {
                                        result.TickOnline = new Tick();
                                        result.Symbol = subParameter[0];
                                        result.Email = subParameter[1];
                                        result.PhoneNumber = subParameter[2];
                                        result.Value = double.Parse(subParameter[3]);
                                        #region ConditionAlert & ActionAlert
                                        switch (subParameter[4])
                                        {
                                            case "LargerBid":
                                                {
                                                    result.AlertCondition = Business.ConditionAlert.LargerBid;
                                                    break;
                                                }
                                            case "LargerAsk":
                                                {
                                                    result.AlertCondition = Business.ConditionAlert.LargerAsk;
                                                    break;
                                                }
                                            case "LargerHighBid":
                                                {
                                                    result.AlertCondition = Business.ConditionAlert.LargerHighBid;
                                                    break;
                                                }
                                            case "LargerHighAsk":
                                                {
                                                    result.AlertCondition = Business.ConditionAlert.LargerHighAsk;
                                                    break;
                                                }
                                            case "SmallerBid":
                                                {
                                                    result.AlertCondition = Business.ConditionAlert.SmallerBid;
                                                    break;
                                                }
                                            case "SmallerAsk":
                                                {
                                                    result.AlertCondition = Business.ConditionAlert.SmallerAsk;
                                                    break;
                                                }
                                            case "SmallerLowBid":
                                                {
                                                    result.AlertCondition = Business.ConditionAlert.SmallerLowBid;
                                                    break;
                                                }
                                            case "SmallerLowAsk":
                                                {
                                                    result.AlertCondition = Business.ConditionAlert.SmallerLowAsk;
                                                    break;
                                                }
                                        }
                                        switch (subParameter[5])
                                        {
                                            case "Email":
                                                {
                                                    result.AlertAction = Business.ActionAlert.Email;
                                                    break;
                                                }
                                            case "SMS":
                                                {
                                                    result.AlertAction = Business.ActionAlert.SMS;
                                                    break;
                                                }
                                            case "Sound":
                                                {
                                                    result.AlertAction = Business.ActionAlert.Sound;
                                                    break;
                                                }
                                        }
                                        #endregion
                                        result.IsEnable = bool.Parse(subParameter[6]);
                                        result.InvestorID = int.Parse(subParameter[7]);
                                        result.Notification = subParameter[8];
                                        result.DateCreate = DateTime.Now;
                                        timeCreate = result.DateCreate.ToString();
                                        result.DateActive = result.DateCreate;
                                        ResultAddNew = TradingServer.Facade.FacadeAddNewAlert(result);
                                    }
                                    #endregion

                                }
                                StringResult = subValue[0] + "$" + ResultAddNew + "," + timeCreate;
                            }
                            break;

                        case "InsertNewAlert":
                            {
                                int ResultAddNew = -1;
                                string timeCreate = "";
                                Business.PriceAlert result = new PriceAlert();
                                if (!string.IsNullOrEmpty(subValue[1]))
                                {
                                    #region Create Alert
                                    string[] subParameter = subValue[1].Split('{');
                                    if (subParameter.Length > 0)
                                    {
                                        result.TickOnline = new Tick();
                                        result.Symbol = subParameter[0];
                                        result.Email = subParameter[1];
                                        result.PhoneNumber = subParameter[2];
                                        result.Value = double.Parse(subParameter[3]);
                                        #region ConditionAlert & ActionAlert
                                        switch (subParameter[4])
                                        {
                                            case "LargerBid":
                                                {
                                                    result.AlertCondition = Business.ConditionAlert.LargerBid;
                                                    break;
                                                }
                                            case "LargerAsk":
                                                {
                                                    result.AlertCondition = Business.ConditionAlert.LargerAsk;
                                                    break;
                                                }
                                            case "LargerHighBid":
                                                {
                                                    result.AlertCondition = Business.ConditionAlert.LargerHighBid;
                                                    break;
                                                }
                                            case "LargerHighAsk":
                                                {
                                                    result.AlertCondition = Business.ConditionAlert.LargerHighAsk;
                                                    break;
                                                }
                                            case "SmallerBid":
                                                {
                                                    result.AlertCondition = Business.ConditionAlert.SmallerBid;
                                                    break;
                                                }
                                            case "SmallerAsk":
                                                {
                                                    result.AlertCondition = Business.ConditionAlert.SmallerAsk;
                                                    break;
                                                }
                                            case "SmallerLowBid":
                                                {
                                                    result.AlertCondition = Business.ConditionAlert.SmallerLowBid;
                                                    break;
                                                }
                                            case "SmallerLowAsk":
                                                {
                                                    result.AlertCondition = Business.ConditionAlert.SmallerLowAsk;
                                                    break;
                                                }
                                        }
                                        switch (subParameter[5])
                                        {
                                            case "Email":
                                                {
                                                    result.AlertAction = Business.ActionAlert.Email;
                                                    break;
                                                }
                                            case "SMS":
                                                {
                                                    result.AlertAction = Business.ActionAlert.SMS;
                                                    break;
                                                }
                                            case "Sound":
                                                {
                                                    result.AlertAction = Business.ActionAlert.Sound;
                                                    break;
                                                }
                                        }
                                        #endregion
                                        result.IsEnable = bool.Parse(subParameter[6]);
                                        result.InvestorID = int.Parse(subParameter[7]);
                                        result.Notification = subParameter[8];
                                        result.DateCreate = DateTime.Now;
                                        timeCreate = result.DateCreate.ToString();
                                        result.DateActive = result.DateCreate;
                                        ResultAddNew = TradingServer.Facade.FacadeAddNewAlert(result);
                                    }
                                    #endregion
                                }
                                StringResult = subValue[0] + "$" + ResultAddNew + "{" + result.Symbol + "{" + result.Email + "{" + result.PhoneNumber + "{" +
                                    result.Value + "{" + result.AlertCondition + "{" + result.AlertAction + "{" + result.IsEnable + "{" +
                                    timeCreate + "{" + result.DateActive + "{" + result.InvestorID + "{" + result.Notification;
                            }
                            break;
                        #endregion

                        #region Add New Permit
                        case "AddPermit":
                            {
                                int ResultAddNew = -1;
                                Business.Permit Result = new Permit();
                                Result = this.ExtractionPermit(subValue[1]);
                                ResultAddNew = TradingServer.Facade.FacadeAddNewPermit(Result.AgentGroupID, Result.AgentID, Result.Role.RoleID);
                                StringResult = subValue[0] + "$" + ResultAddNew.ToString();
                            }
                            break;
                        #endregion

                        #region Add New IAgentSecurity
                        case "AddNewIAgentSecurity":
                            {
                                if (!string.IsNullOrEmpty(subValue[1]))
                                {
                                    int ResultAddNew = -1;
                                    int AgentID = -1;
                                    int SecurityID = -1;
                                    bool IsUse = false;
                                    string[] subParameter = subValue[1].Split(',');
                                    if (subParameter.Length > 0)
                                    {
                                        int.TryParse(subParameter[0], out AgentID);
                                        int.TryParse(subParameter[1], out SecurityID);
                                        bool.TryParse(subParameter[2], out IsUse);
                                        ResultAddNew = TradingServer.Facade.FacadeAddNewIAgentSecurity(AgentID, SecurityID, IsUse, subParameter[3], subParameter[4]);
                                        StringResult = subValue[0] + "$" + ResultAddNew.ToString();
                                    }
                                }
                            }
                            break;
                        case "AddListIAgentSecurity":
                            {
                                if (!string.IsNullOrEmpty(subValue[1]))
                                {
                                    int ResultAddNew = -1;
                                    List<Business.IAgentSecurity> Result = new List<IAgentSecurity>();
                                    Result = this.ExtractionIAgentSecurity(subValue[1]);
                                    ResultAddNew = TradingServer.Facade.FacadeAddListIAgentSecurity(Result);
                                    StringResult = subValue[0] + "$" + ResultAddNew.ToString();
                                }
                            }
                            break;
                        #endregion

                        #region Add New IAgentGroup
                        case "AddNewIAgentGroup":
                            {
                                if (!string.IsNullOrEmpty(subValue[1]))
                                {
                                    int ResultAddNew = -1;
                                    int AgentID = -1;
                                    int InvestorGroupID = -1;
                                    string[] subParameter = subValue[1].Split(',');
                                    if (subParameter.Length > 0)
                                    {
                                        int.TryParse(subParameter[0], out AgentID);
                                        int.TryParse(subParameter[1], out InvestorGroupID);
                                        ResultAddNew = TradingServer.Facade.FacadeAddNewIAgentGroup(AgentID, InvestorGroupID);
                                        StringResult = subValue[0] + "$" + ResultAddNew.ToString();
                                    }
                                }
                            }
                            break;
                        #endregion

                        #region Add New Investor Profile
                        case "AddNewInvestorProfile":
                            {
                                int ResultAddNew = -1;
                                //Business.InvestorProfile Result = new InvestorProfile();
                                //Result = this.ExtractInvestorProfile(subValue[1]);
                                //ResultAddNew = TradingServer.Facade.FacadeAddNewInvestorProfile(Result);
                                StringResult = ResultAddNew.ToString();
                            }
                            break;
                        #endregion

                        #region Add New IGroupSecurityConfig
                        case "AddNewIGroupSecurityConfig":
                            {
                                bool checkip = Facade.FacadeCheckIpAdmin(code, ipAddress);
                                if (checkip)
                                {
                                    int ResultAddNew = -1;
                                    List<Business.ParameterItem> Result = new List<ParameterItem>();
                                    Result = this.ExtractParameterItem(subValue[1]);
                                    //Call Function Add New TradingConfig(SymbolConfig)
                                    ResultAddNew = TradingServer.Facade.FacadeAddIGroupSecurityConfig(Result);
                                    StringResult = subValue[0] + "$" + ResultAddNew.ToString();
                                }
                                else
                                {
                                    StringResult = subValue[0] + "$MCM005";
                                }
                            }
                            break;
                        #endregion

                        #region Add New IGroupSymbolConfig
                        case "AddNewIGroupSymbolConfig":
                            {
                                bool checkip = Facade.FacadeCheckIpAdmin(code, ipAddress);
                                if (checkip)
                                {
                                    int ResultAddNew = -1;
                                    List<Business.ParameterItem> Result = new List<ParameterItem>();
                                    Result = this.ExtractParameterItem(subValue[1]);
                                    //Call Function Add New TradingConfig(SymbolConfig)
                                    ResultAddNew = TradingServer.Facade.FacadeAddIGroupSymbolConfig(Result);
                                    StringResult = subValue[0] + "$" + ResultAddNew.ToString();
                                }
                                else
                                {
                                    StringResult = subValue[0] + "$MCM005";
                                }
                            }
                            break;
                        #endregion

                        #region Add Deposit, Add Credit, Withrawals
                        case "AddDeposit":
                            {
                                string[] Parameter = subValue[1].Split(',');
                                int InvestorID = 0;
                                double Money = 0;
                                if (Parameter.Length == 3)
                                {
                                    bool ResultDeposit = false;
                                    int.TryParse(Parameter[0], out InvestorID);

                                    Business.Investor tempInvestor = new Investor();
                                    tempInvestor = TradingServer.Facade.FacadeGetInvestorByInvestorID(InvestorID);

                                    bool parseMoney = double.TryParse(Parameter[1], out Money);
                                    bool checkip = Facade.FacadeCheckIpManager(code, ipAddress);
                                    bool checkRule = Facade.FacadeCheckPermitAddMoney(code);
                                    bool checkGroup = Facade.FacadeCheckPermitAccessGroupManagerAndAdmin(code, tempInvestor.InvestorGroupInstance.InvestorGroupID);
                                    if (parseMoney && checkip && checkRule && checkGroup)
                                    {
                                        ResultDeposit = TradingServer.Facade.FacadeAddDeposit(InvestorID, Money, Parameter[2]);
                                        StringResult = subValue[0] + "$" + ResultDeposit + "," + InvestorID + "," + Money;
                                        #region INSERT SYSTEM LOG
                                        //INSERT SYSTEM LOG
                                        //'2222': account '9789300' withdrawal: 100000.00
                                        string status = "[Success]";
                                        if (!ResultDeposit)
                                        {
                                            status = "[Failed]";
                                        }

                                        string tempMoney = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Money.ToString(), 2);
                                        string content = "'" + code + "': account '" + tempInvestor.Code + "' deposit: " + tempMoney + " " + status;
                                        string comment = "[deposit]";
                                        TradingServer.Facade.FacadeAddNewSystemLog(3, content, comment, ipAddress, code);
                                        #endregion
                                    }
                                    else
                                    {
                                        StringResult = subValue[0] + "$" + false + "," + InvestorID + "," + Money;
                                        string comment = "[deposit]";
                                        string tempCredit = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Money.ToString(), 2);
                                        if (!checkip)
                                        {
                                            string content = "'" + code + "': account '" + tempInvestor.Code + "' deposit: " + tempCredit + " failed (invalid ip)";
                                            TradingServer.Facade.FacadeAddNewSystemLog(3, content, comment, ipAddress, code);
                                        }
                                        if (!checkRule || !checkGroup)
                                        {
                                            string content = "'" + code + "': account '" + tempInvestor.Code + "' deposit: " + tempCredit + " failed (not enough rights)";
                                            TradingServer.Facade.FacadeAddNewSystemLog(3, content, comment, ipAddress, code);
                                        }
                                        if (!parseMoney)
                                        {
                                            string content = "'" + code + "': account '" + tempInvestor.Code + "' deposit: " + tempCredit + " failed (invalid amount)";
                                            TradingServer.Facade.FacadeAddNewSystemLog(3, content, comment, ipAddress, code);
                                        }
                                    }
                                }
                                else
                                {
                                    StringResult = subValue[0] + "$" + false + "," + InvestorID + "," + Money;
                                }
                            }
                            break;

                        case "AddDepostAndCredit":
                            {
                                string[] Parameter = subValue[1].Split(',');
                                if (Parameter.Length == 3)
                                {
                                    bool ResultDeposit = false;
                                    int InvestorID = 0;
                                    int.TryParse(Parameter[0], out InvestorID);
                                    double Deposit = 0;
                                    bool parseDeposit = double.TryParse(Parameter[1], out Deposit);
                                    double Credit = 0;
                                    bool parseCredit = double.TryParse(Parameter[2], out Credit);

                                    if (parseDeposit && parseCredit)
                                    {
                                        ResultDeposit = TradingServer.Facade.FacadeUpdateBalanceAndCredit(InvestorID, Deposit, Credit);

                                        StringResult = subValue[0] + "$" + ResultDeposit + "," + InvestorID + "," + Deposit + "," + Credit;
                                    }
                                    else
                                    {
                                        StringResult = subValue[0] + "$" + false + "," + InvestorID + "," + Deposit + "," + Credit;
                                    }
                                }
                            }
                            break;

                        case "AddCredit":
                            {
                                string[] Parameter = subValue[1].Split(',');
                                if (Parameter.Length == 3)
                                {
                                    bool ResultCredit = false;
                                    int InvestorID = 0;
                                    double Credit = 0;

                                    int.TryParse(Parameter[0], out InvestorID);
                                    bool parseCredit = double.TryParse(Parameter[1], out Credit);

                                    Business.Investor tempInvestor = new Investor();
                                    tempInvestor = TradingServer.Facade.FacadeGetInvestorByInvestorID(InvestorID);
                                    bool checkip = Facade.FacadeCheckIpManager(code, ipAddress);
                                    bool checkRule = Facade.FacadeCheckPermitAddMoney(code);
                                    bool checkGroup = Facade.FacadeCheckPermitAccessGroupManagerAndAdmin(code, tempInvestor.InvestorGroupInstance.InvestorGroupID);
                                    if (parseCredit && checkip && checkRule && checkGroup)
                                    {
                                        ResultCredit = TradingServer.Facade.FacadeAddCredit(InvestorID, Credit, Parameter[2]);

                                        StringResult = subValue[0] + "$" + ResultCredit + "," + InvestorID + "," + Credit;

                                        #region INSERT SYSTEM LOG
                                        //INSERT SYSTEM LOG
                                        //'2222': account '9789300' withdrawal: 100000.00
                                        string status = "[Success]";
                                        if (!ResultCredit)
                                            status = "[Failed]";

                                        string tempCredit = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Credit.ToString(), 2);
                                        string content = "'" + code + "': account '" + tempInvestor.Code + "' credit in: " + tempCredit + " " + status;
                                        string comment = "[credit in]";
                                        TradingServer.Facade.FacadeAddNewSystemLog(3, content, comment, ipAddress, code);
                                        #endregion
                                    }
                                    else
                                    {
                                        StringResult = subValue[0] + "$" + false + "," + InvestorID + "," + Credit;
                                        string comment = "[credit in]";
                                        string tempCredit = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Credit.ToString(), 2);
                                        if (!checkip)
                                        {
                                            string content = "'" + code + "': account '" + tempInvestor.Code + "' credit in: " + tempCredit + " failed (invalid ip)";
                                            TradingServer.Facade.FacadeAddNewSystemLog(3, content, comment, ipAddress, code);
                                        }
                                        if (!checkRule || !checkGroup)
                                        {
                                            string content = "'" + code + "': account '" + tempInvestor.Code + "' credit in: " + tempCredit + " failed (not enough rights)";
                                            TradingServer.Facade.FacadeAddNewSystemLog(3, content, comment, ipAddress, code);
                                        }
                                        if (!parseCredit)
                                        {
                                            string content = "'" + code + "': account '" + tempInvestor.Code + "' credit in: " + tempCredit + " failed (invalid amount)";
                                            TradingServer.Facade.FacadeAddNewSystemLog(3, content, comment, ipAddress, code);
                                        }
                                    }
                                }
                            }
                            break;

                        case "SubCredit":
                            {
                                string[] Parameter = subValue[1].Split(',');
                                if (Parameter.Length == 3)
                                {
                                    bool ResultCredit = false;
                                    int InvestorID = 0;
                                    double Credit = 0;

                                    int.TryParse(Parameter[0], out InvestorID);
                                    bool parseCredit = double.TryParse(Parameter[1], out Credit);

                                    Business.Investor tempInvestor = new Investor();
                                    tempInvestor = TradingServer.Facade.FacadeGetInvestorByInvestorID(InvestorID);

                                    bool checkip = Facade.FacadeCheckIpManager(code, ipAddress);
                                    bool checkRule = Facade.FacadeCheckPermitAddMoney(code);
                                    bool checkGroup = Facade.FacadeCheckPermitAccessGroupManagerAndAdmin(code, tempInvestor.InvestorGroupInstance.InvestorGroupID);
                                    if (parseCredit && checkip && checkRule && checkGroup)
                                    {
                                        ResultCredit = TradingServer.Facade.FacadeSubCredit(InvestorID, Credit, Parameter[2]);
                                        StringResult = subValue[0] + "$" + ResultCredit + "," + InvestorID + "," + Credit;
                                        #region INSERT SYSTEM LOG
                                        //INSERT SYSTEM LOG
                                        //'2222': account '9789300' withdrawal: 100000.00
                                        string status = "[Success]";
                                        if (!ResultCredit)
                                            status = "[Failed]";

                                        string tempCredit = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Credit.ToString(), 2);
                                        string content = "'" + code + "': account '" + tempInvestor.Code + "' credit out: " + tempCredit + " " + status;
                                        string comment = "[credit out]";
                                        TradingServer.Facade.FacadeAddNewSystemLog(3, content, comment, ipAddress, code);
                                        #endregion
                                    }
                                    else
                                    {
                                        StringResult = subValue[0] + "$" + false + "," + InvestorID + "," + Credit;
                                        string tempCredit = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Credit.ToString(), 2);
                                        string comment = "[credit out]";
                                        if (!checkip)
                                        {
                                            string content = "'" + code + "': account '" + tempInvestor.Code + "' credit out: " + tempCredit + " failed (invalid ip)";
                                            TradingServer.Facade.FacadeAddNewSystemLog(3, content, comment, ipAddress, code);
                                        }
                                        if (!checkRule || !checkGroup)
                                        {
                                            string content = "'" + code + "': account '" + tempInvestor.Code + "' credit out: " + tempCredit + " failed (not enough rights)";
                                            TradingServer.Facade.FacadeAddNewSystemLog(3, content, comment, ipAddress, code);
                                        }
                                        if (!parseCredit)
                                        {
                                            string content = "'" + code + "': account '" + tempInvestor.Code + "' credit out: " + tempCredit + " failed (invalid amount)";
                                            TradingServer.Facade.FacadeAddNewSystemLog(3, content, comment, ipAddress, code);
                                        }
                                    }
                                }
                            }
                            break;

                        case "WithRawals":
                            {
                                string[] Parameter = subValue[1].Split(',');
                                if (Parameter.Length == 3)
                                {
                                    bool ResultWithRawals = false;
                                    int InvestorID = 0;
                                    double Money = 0;
                                    int.TryParse(Parameter[0], out InvestorID);
                                    bool parseMoney = double.TryParse(Parameter[1], out Money);

                                    Business.Investor tempInvestor = new Investor();
                                    tempInvestor = TradingServer.Facade.FacadeGetInvestorByInvestorID(InvestorID);
                                    bool checkip = Facade.FacadeCheckIpManager(code, ipAddress);
                                    bool checkRule = Facade.FacadeCheckPermitAddMoney(code);
                                    bool checkGroup = Facade.FacadeCheckPermitAccessGroupManagerAndAdmin(code, tempInvestor.InvestorGroupInstance.InvestorGroupID);
                                    if (parseMoney && checkip && checkRule && checkGroup)
                                    {
                                        ResultWithRawals = TradingServer.Facade.FacadeWithRawals(InvestorID, Money, Parameter[2]);
                                        StringResult = subValue[0] + "$" + ResultWithRawals + "," + InvestorID + "," + Money;
                                        #region INSERT SYSTEM LOG
                                        //INSERT SYSTEM LOG
                                        //'2222': account '9789300' withdrawal: 100000.00
                                        string status = "[Success]";
                                        if (!ResultWithRawals)
                                            status = "[Failed]";

                                        string formatMoney = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Money.ToString(), 2);
                                        string content = "'" + code + "': account '" + tempInvestor.Code + "' withdrawal: " + formatMoney + " " + status;
                                        string comment = "[withdrawal]";
                                        TradingServer.Facade.FacadeAddNewSystemLog(3, content, comment, ipAddress, code);
                                        #endregion
                                    }
                                    else
                                    {
                                        StringResult = subValue[0] + "$" + false + "," + InvestorID + "," + Money;
                                        string comment = "[withdrawal]";
                                        string tempCredit = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Money.ToString(), 2);
                                        if (!checkip)
                                        {
                                            string content = "'" + code + "': account '" + tempInvestor.Code + "' withdrawal: " + tempCredit + " failed (invalid ip)";
                                            TradingServer.Facade.FacadeAddNewSystemLog(3, content, comment, ipAddress, code);
                                        }
                                        if (!checkRule || !checkGroup)
                                        {
                                            string content = "'" + code + "': account '" + tempInvestor.Code + "' withdrawal: " + tempCredit + " failed (not enough rights)";
                                            TradingServer.Facade.FacadeAddNewSystemLog(3, content, comment, ipAddress, code);
                                        }
                                        if (!parseMoney)
                                        {
                                            string content = "'" + code + "': account '" + tempInvestor.Code + "' withdrawal: " + tempCredit + " failed (invalid amount)";
                                            TradingServer.Facade.FacadeAddNewSystemLog(3, content, comment, ipAddress, code);
                                        }
                                    }

                                }
                            }
                            break;
                        #endregion

                        #region MAKE COMMAND BY MANAGER
                        case "AddCommandByManager":
                            {
                                bool checkip = Facade.FacadeCheckIpManager(code, ipAddress);
                                if (checkip)
                                {
                                    bool checkRule = Facade.FacadeCheckPermitCommandManagerAndAdmin(code);
                                    if (checkRule)
                                    {
                                        string[] subParameter = subValue[1].Split(',');
                                        if (subParameter.Length == 9)
                                        {
                                            #region Map Value
                                            int InvestorID = 0;
                                            double Size = 0;
                                            double StopLoss = 0;
                                            double TakeProfit = 0;
                                            double OpenPrice = 0;
                                            int CommandType = 0;
                                            string Symbol = string.Empty;
                                            DateTime ExpTime = DateTime.Now;

                                            int.TryParse(subParameter[0], out InvestorID);
                                            double.TryParse(subParameter[1], out Size);
                                            Symbol = subParameter[2];
                                            double.TryParse(subParameter[3], out StopLoss);
                                            double.TryParse(subParameter[4], out TakeProfit);
                                            double.TryParse(subParameter[5], out OpenPrice);
                                            int.TryParse(subParameter[6], out CommandType);
                                            DateTime.TryParse(subParameter[7], out ExpTime);
                                            #endregion
                                            if (Size > 0)
                                            {
                                                Business.OpenTrade newCommand = new OpenTrade();
                                                newCommand = TradingServer.Facade.FacadeFillInstanceOpenTrade(InvestorID, subParameter[2], CommandType);

                                                newCommand.Size = Size;
                                                newCommand.StopLoss = StopLoss;
                                                newCommand.TakeProfit = TakeProfit;
                                                newCommand.OpenPrice = OpenPrice;
                                                newCommand.ExpTime = ExpTime;
                                                newCommand.IsServer = true;
                                                newCommand.Comment = subParameter[8];

                                                //CALCULATION MARGIN
                                                //newCommand.CalculatorMarginCommand(newCommand);
                                                bool checkGroup = Facade.FacadeCheckPermitAccessGroupManagerAndAdmin(code, newCommand.Investor.InvestorGroupInstance.InvestorGroupID);
                                                if (checkGroup)
                                                {
                                                    bool checkAccount = newCommand.CheckValidAccountInvestor(newCommand);

                                                    if (checkAccount)
                                                    {
                                                        bool IsTrade = false;
                                                        double Minimum = -1;
                                                        double Maximum = -1;
                                                        double Step = -1;
                                                        bool ResultCheckStepLots = false;

                                                        //CHECK MIN MAX LOTS COMMAND
                                                        #region Get Config IGroupSecurity
                                                        if (newCommand.IGroupSecurity.IGroupSecurityConfig != null)
                                                        {
                                                            int countIGroupSecurityConfig = newCommand.IGroupSecurity.IGroupSecurityConfig.Count;
                                                            for (int i = 0; i < countIGroupSecurityConfig; i++)
                                                            {
                                                                if (newCommand.IGroupSecurity.IGroupSecurityConfig[i].Code == "B01")
                                                                {
                                                                    if (newCommand.IGroupSecurity.IGroupSecurityConfig[i].BoolValue == 1)
                                                                        IsTrade = true;
                                                                }

                                                                if (newCommand.IGroupSecurity.IGroupSecurityConfig[i].Code == "B11")
                                                                {
                                                                    double.TryParse(newCommand.IGroupSecurity.IGroupSecurityConfig[i].NumValue, out Minimum);
                                                                }

                                                                if (newCommand.IGroupSecurity.IGroupSecurityConfig[i].Code == "B12")
                                                                {
                                                                    double.TryParse(newCommand.IGroupSecurity.IGroupSecurityConfig[i].NumValue, out Maximum);
                                                                }

                                                                if (newCommand.IGroupSecurity.IGroupSecurityConfig[i].Code == "B13")
                                                                {
                                                                    double.TryParse(newCommand.IGroupSecurity.IGroupSecurityConfig[i].NumValue, out Step);
                                                                }
                                                            }
                                                        }
                                                        #endregion

                                                        ResultCheckStepLots = newCommand.IGroupSecurity.CheckStepLots(Minimum, Maximum, Step, newCommand.Size);

                                                        if (ResultCheckStepLots)
                                                        {
                                                            newCommand.DealerCode = code;
                                                            newCommand.Symbol.MarketAreaRef.AddCommand(newCommand);
                                                            StringResult = subValue[0] + "$" + true + "," + "MCM001";
                                                        }
                                                        else
                                                        {
                                                            StringResult = subValue[0] + "$" + false + "," + "MCM003";
                                                        }
                                                    }
                                                    else
                                                    {
                                                        StringResult = subValue[0] + "$" + false + "," + "MCM002";
                                                    }
                                                }
                                                else
                                                {
                                                    StringResult = subValue[0] + "$" + false + "," + "MCM006";
                                                    string content = "'" + code + "': manager open command failed(not enough rights)";
                                                    string comment = "[manager open command]";
                                                    TradingServer.Facade.FacadeAddNewSystemLog(5, content, comment, ipAddress, code);
                                                }
                                            }
                                            else
                                            {
                                                StringResult = subValue[0] + "$" + false + "," + "MCM003";
                                            }
                                        }
                                        else
                                        {
                                            StringResult = subValue[0] + "$" + false + "," + "MCM004";
                                        }
                                    }
                                    else
                                    {
                                        StringResult = subValue[0] + "$" + false + "," + "MCM006";
                                        string content = "'" + code + "': manager open command failed(not enough rights)";
                                        string comment = "[manager open command]";
                                        TradingServer.Facade.FacadeAddNewSystemLog(5, content, comment, ipAddress, code);
                                    }
                                }
                                else
                                {
                                    StringResult = subValue[0] + "$" + false + "," + "MCM005";
                                    string content = "'" + code + "': manager open command failed(invalid ip)";
                                    string comment = "[manager open command]";
                                    TradingServer.Facade.FacadeAddNewSystemLog(5, content, comment, ipAddress, code);
                                }
                            }
                            break;
                        #endregion

                        #region FUNCTION IN CANDLES(LOG)
                        case "AddNewCandles":
                            {
                                bool checkip = Facade.FacadeCheckIpAdmin(code, ipAddress);
                                if (checkip)
                                {
                                    bool Result = false;
                                    string[] subParameter = subValue[1].Split(',');
                                    if (subParameter.Length == 8)
                                    {
                                        DateTime Time;
                                        double High = 0;
                                        double Low = 0;
                                        double Open = 0;
                                        double Close = 0;
                                        double openAsk = 0;
                                        double highAsk = 0;
                                        double lowAsk = 0;
                                        double closeAsk = 0;
                                        int Volume = 0;
                                        int TimeFrame = 0;

                                        DateTime.TryParse(subParameter[1], out Time);
                                        double.TryParse(subParameter[2], out High);
                                        double.TryParse(subParameter[3], out Low);
                                        double.TryParse(subParameter[4], out Open);
                                        double.TryParse(subParameter[5], out Close);
                                        int.TryParse(subParameter[6], out Volume);
                                        int.TryParse(subParameter[7], out TimeFrame);
                                        //double.TryParse(subParameter[8], out openAsk);
                                        //double.TryParse(subParameter[9], out highAsk);
                                        //double.TryParse(subParameter[10], out lowAsk);
                                        //double.TryParse(subParameter[11], out closeAsk);

                                        ProcessQuoteLibrary.Business.Candles objCandles = new ProcessQuoteLibrary.Business.Candles();
                                        objCandles.Close = Close;
                                        objCandles.CloseAsk = closeAsk;
                                        objCandles.High = High;
                                        objCandles.HighAsk = highAsk;
                                        objCandles.Low = Low;
                                        objCandles.LowAsk = lowAsk;
                                        objCandles.Name = subParameter[0];
                                        objCandles.Open = Open;
                                        objCandles.OpenAsk = openAsk;
                                        objCandles.Time = Time;
                                        objCandles.TimeFrame = TimeFrame;
                                        objCandles.Volume = Volume;

                                        int ResultAdd = ProcessQuoteLibrary.Business.QuoteProcess.AddCandles(objCandles);
                                        if (ResultAdd > 0)
                                            Result = true;

                                        #region INSERT SYSTEM LOG
                                        //INSERT SYSTEM LOG
                                        //'2222': XAUUSD 1 bars added
                                        string status = "[Failed]";

                                        if (ResultAdd > 0)
                                            status = "[Success]";

                                        string content = "'" + code + "': " + subParameter[0] + " 1 bars added";
                                        string comment = "[add new candles]";
                                        TradingServer.Facade.FacadeAddNewSystemLog(3, content, comment, ipAddress, code);
                                        #endregion

                                        StringResult = subValue[0] + "$" + Result + "," + ResultAdd;
                                    }
                                    else
                                    {
                                        StringResult = subValue[0] + "$" + false + "," + -1;
                                    }
                                }
                                else
                                {
                                    StringResult = subValue[0] + "$MCM005";
                                }
                            }
                            break;

                        case "DeleteCandles":
                            {
                                bool checkip = Facade.FacadeCheckIpAdmin(code, ipAddress);
                                if (checkip)
                                {
                                    bool Result = false;
                                    string[] subParameter = subValue[1].Split(',');
                                    if (subParameter.Length == 2)
                                    {
                                        int CandlesID = 0;
                                        int TimeFrame = 0;

                                        int.TryParse(subParameter[0], out CandlesID);
                                        int.TryParse(subParameter[1], out TimeFrame);

                                        Result = ProcessQuoteLibrary.Business.QuoteProcess.DeleteCandlesByID(CandlesID, TimeFrame);

                                        #region INSERT SYSTEM LOG
                                        //INSERT SYSTEM LOG
                                        //'2222': XAUUSD M1 1 bars deleted
                                        string status = "[Failed]";

                                        if (Result)
                                            status = "[Success]";

                                        string content = "'" + code + "': " + subParameter[0] + " 1 bars deleted";
                                        string comment = "[delete candles]";
                                        TradingServer.Facade.FacadeAddNewSystemLog(3, content, comment, ipAddress, code);
                                        #endregion

                                        StringResult = subValue[0] + "$" + Result;
                                    }
                                    else
                                    {
                                        StringResult = subValue[0] + "$" + false;
                                    }
                                }
                                else
                                {
                                    StringResult = subValue[0] + "$MCM005";
                                }
                            }
                            break;

                        case "UpdateCandles":
                            {
                                bool checkip = Facade.FacadeCheckIpAdmin(code, ipAddress);
                                if (checkip)
                                {
                                    bool Result = false;
                                    string[] subParameter = subValue[1].Split(',');
                                    if (subParameter.Length == 8)
                                    {
                                        int CandlesID = 0;
                                        DateTime Time;
                                        int Volume = 0;
                                        double Open = 0;
                                        double Close = 0;
                                        double High = 0;
                                        double Low = 0;
                                        double openAsk = 0;
                                        double highAsk = 0;
                                        double lowAsk = 0;
                                        double closeAsk = 0;
                                        int TimeFrame = 0;

                                        int.TryParse(subParameter[0], out CandlesID);
                                        DateTime.TryParse(subParameter[1], out Time);
                                        int.TryParse(subParameter[2], out Volume);
                                        double.TryParse(subParameter[3], out Open);
                                        double.TryParse(subParameter[4], out Close);
                                        double.TryParse(subParameter[5], out High);
                                        double.TryParse(subParameter[6], out Low);
                                        int.TryParse(subParameter[7], out TimeFrame);
                                        //double.TryParse(subParameter[8], out openAsk);
                                        //double.TryParse(subParameter[9], out highAsk);
                                        //double.TryParse(subParameter[10], out lowAsk);
                                        //double.TryParse(subParameter[11], out closeAsk);

                                        ProcessQuoteLibrary.Business.Candles objCandles = new ProcessQuoteLibrary.Business.Candles();
                                        objCandles.Open = Open;
                                        objCandles.High = High;
                                        objCandles.Low = Low;
                                        objCandles.Close = Close;

                                        //objCandles.OpenAsk = openAsk;
                                        //objCandles.HighAsk = highAsk;
                                        //objCandles.LowAsk = lowAsk;
                                        //objCandles.CloseAsk = closeAsk;

                                        objCandles.ID = CandlesID;
                                        objCandles.Time = Time;
                                        objCandles.TimeFrame = TimeFrame;
                                        objCandles.Volume = Volume;

                                        Result = ProcessQuoteLibrary.Business.QuoteProcess.UpdateCandleOnline(objCandles);

                                        StringResult = subValue[0] + "$" + Result;
                                    }
                                    else
                                    {
                                        StringResult = subValue[0] + "$" + false;
                                    }
                                }
                                else
                                {
                                    StringResult = subValue[0] + "$MCM005";
                                }
                            }
                            break;
                        #endregion

                        #region FUNCTION IN JOURNAL
                        case "AddJournal":
                            {
                                string[] subParameter = subValue[1].Split(',');
                                if (subParameter.Length == 4)
                                {
                                    int typeID = 0;
                                    int.TryParse(subParameter[0], out typeID);
                                    bool resultAdd = TradingServer.Facade.FacadeAddNewSystemLog(typeID, subParameter[1], subParameter[2], ipAddress, code);

                                    StringResult = subValue[0] + "$" + resultAdd;
                                }
                                else
                                {
                                    StringResult = subValue[0] + "$" + false;
                                }
                            }
                            break;
                        #endregion

                        #region REOPEN COMMAND(LOG)
                        case "ReOpenComamnd":
                            {
                                bool checkip = Facade.FacadeCheckIpAdmin(code, ipAddress);
                                if (checkip)
                                {
                                    bool checkRule = Facade.FacadeCheckPermitCommandManagerAndAdmin(code);
                                    if (checkRule)
                                    {
                                        string[] subParameter = subValue[1].Split(',');
                                        bool resultRe = false;
                                        if (subParameter.Length == 16)
                                        {
                                            #region MAP VALUE
                                            Business.OpenTrade result = new OpenTrade();
                                            int commandHistoryID;
                                            int commandType;
                                            double lots;
                                            //string symbol;
                                            DateTime openTime;
                                            double openPrice;
                                            double stopLoss;
                                            DateTime closeTime;
                                            double closePrice;
                                            double takeProfit;
                                            double commission;
                                            double agentCommission;
                                            double swap;
                                            //string comment;
                                            double taxes;
                                            DateTime expTime;

                                            int.TryParse(subParameter[0], out commandHistoryID);
                                            int.TryParse(subParameter[1], out commandType);
                                            double.TryParse(subParameter[2], out lots);
                                            //Symbol subparameter[3]
                                            DateTime.TryParse(subParameter[4], out openTime);
                                            double.TryParse(subParameter[5], out openPrice);
                                            double.TryParse(subParameter[6], out stopLoss);
                                            DateTime.TryParse(subParameter[7], out closeTime);
                                            double.TryParse(subParameter[8], out closePrice);
                                            double.TryParse(subParameter[9], out takeProfit);
                                            double.TryParse(subParameter[10], out commission);
                                            double.TryParse(subParameter[11], out agentCommission);
                                            double.TryParse(subParameter[12], out swap);
                                            //subParameter[13], out comment
                                            double.TryParse(subParameter[14], out taxes);
                                            DateTime.TryParse(subParameter[15], out expTime);
                                            #endregion

                                            result = TradingServer.Facade.FacadeGetCommandHistoryByCommandID(commandHistoryID);

                                            if (result != null)
                                            {
                                                Business.OpenTrade tempOpenTrade = new OpenTrade();
                                                tempOpenTrade = TradingServer.ClientFacade.FillInstanceOpenTrade(subParameter[3], result.Investor.InvestorID, commandType);

                                                if (tempOpenTrade.Investor != null && tempOpenTrade.Symbol != null && tempOpenTrade.IGroupSecurity != null)
                                                {
                                                    #region VALID INVESTOR, SYMBOL, IGROUPSECURITY INSTANCE
                                                    //SET NEW DATA
                                                    tempOpenTrade.Size = lots;
                                                    tempOpenTrade.OpenTime = result.OpenTime;
                                                    tempOpenTrade.OpenPrice = openPrice;
                                                    tempOpenTrade.StopLoss = stopLoss;
                                                    tempOpenTrade.CloseTime = closeTime;
                                                    tempOpenTrade.ClosePrice = closePrice;
                                                    tempOpenTrade.TakeProfit = takeProfit;
                                                    tempOpenTrade.Commission = commission;
                                                    tempOpenTrade.AgentCommission = agentCommission;
                                                    tempOpenTrade.Swap = swap;
                                                    //tempOpenTrade.Swap = 0;
                                                    tempOpenTrade.Taxes = taxes;
                                                    tempOpenTrade.ExpTime = expTime;
                                                    tempOpenTrade.Comment = subParameter[13];
                                                    tempOpenTrade.CommandCode = result.CommandCode;
                                                    tempOpenTrade.IsServer = true;
                                                    tempOpenTrade.IsReOpen = true;
                                                    tempOpenTrade.DealerCode = code;
                                                    tempOpenTrade.IsActivePending = result.IsActivePending;
                                                    tempOpenTrade.IsStopLossAndTakeProfit = result.IsStopLossAndTakeProfit;

                                                    //CHECK IF PENDING ORDER THEN SET PROFIT = 0(BECAUSE, IF PENDING ORDER WITH PROFIT != 0 THEN WHEN STOP OUT
                                                    //SYSTEM WILL PLUS PROFIT TO BALANCE
                                                    bool isPending = TradingServer.Model.TradingCalculate.Instance.CheckIsPendingPosition(tempOpenTrade.Type.ID);
                                                    if (isPending)
                                                        tempOpenTrade.Profit = 0;

                                                    tempOpenTrade.Symbol.MarketAreaRef.AddCommand(tempOpenTrade);

                                                    //DELETE COMMAND HISTORY
                                                    resultRe = TradingServer.Facade.FacadeDeleteCommandHistory(commandHistoryID);

                                                    if (resultRe)
                                                    {
                                                        if (Business.Market.InvestorList != null)
                                                        {
                                                            int count = Business.Market.InvestorList.Count;
                                                            for (int j = 0; j < count; j++)
                                                            {
                                                                if (Business.Market.InvestorList[j].InvestorID == tempOpenTrade.Investor.InvestorID)
                                                                {
                                                                    #region UPDATE BALANCE ACCOUNT OF INVESTOR AFTER REOPEN COMMAND
                                                                    double totalProfit = result.Profit + result.Commission + result.Swap;
                                                                    //CHANGE FORMAT REOPEN COMMAND(DONT RETURN SWAP TO BALANCE)
                                                                    //double totalProfit = result.Profit + result.Commission;

                                                                    double totalBalance = Business.Market.InvestorList[j].Balance - totalProfit;
                                                                    #endregion

                                                                    //UPDATE BALANCE IN DATABASE
                                                                    bool updateBalance = TradingServer.Facade.FacadeUpdateBalance(Business.Market.InvestorList[j].InvestorID, totalBalance);

                                                                    if (updateBalance)
                                                                    {
                                                                        Business.Market.InvestorList[j].Balance -= totalProfit;

                                                                        //RECALCULATION ACCOUNT OF INVESTOR
                                                                        Business.Market.InvestorList[j].ReCalculationAccountInit();

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

                                                                        string message = "ROC53709";
                                                                        Business.Market.InvestorList[j].ClientCommandQueue.Add(message);
                                                                    }
                                                                    else
                                                                    {
                                                                        resultRe = false;
                                                                    }

                                                                    break;
                                                                }
                                                            }
                                                        }
                                                    }
                                                    #endregion

                                                    #region INSERT SYSTEM LOG
                                                    //'2222': restore order #11235348
                                                    //'9720467': restore order #11235348, sell 5.00 XAUUSD
                                                    //'2222': open order #11235248 for '9720467' modified - sell 5.00 XAUUSD at 1.44730 -> sell 5.00 XAUUSD at 1.44730

                                                    //INSERT SYSTEM LOG 1
                                                    string content = "'" + code + "': restore order #" + tempOpenTrade.CommandCode;
                                                    string comment = "[restore order]";
                                                    TradingServer.Facade.FacadeAddNewSystemLog(5, content, comment, ipAddress, code);

                                                    //INSERT SYSTEM LOG 2
                                                    string size = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(tempOpenTrade.Size.ToString(), 2);
                                                    string sizeBefore = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(result.Size.ToString(), 2);

                                                    string strOpenPrice = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(tempOpenTrade.OpenPrice.ToString(), tempOpenTrade.Symbol.Digit);
                                                    string strOpenPriceBefore = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(result.OpenPrice.ToString(), result.Symbol.Digit);
                                                    string mode = string.Empty;
                                                    string content1 = string.Empty;
                                                    string type = TradingServer.Model.TradingCalculate.Instance.ConvertTypeIDToString(tempOpenTrade.Type.ID);
                                                    string typeBefore = TradingServer.Model.TradingCalculate.Instance.ConvertTypeIDToString(result.Type.ID);
                                                    string strCommission = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(commission.ToString(), 2);
                                                    string strAgentCommission = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(agentCommission.ToString(), 2);
                                                    string strStopLoss = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(stopLoss.ToString(), tempOpenTrade.Symbol.Digit);
                                                    string strTakeProfit = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(takeProfit.ToString(), tempOpenTrade.Symbol.Digit);
                                                    string strSwap = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(swap.ToString(), 2);

                                                    content = "'" + tempOpenTrade.Investor.Code + "': restore order #" + tempOpenTrade.CommandCode + ", [" + typeBefore + "] " + sizeBefore + " " + tempOpenTrade.Symbol.Name;
                                                    content1 = "'" + code + "': open order #" + tempOpenTrade.CommandCode + " for '" + tempOpenTrade.Investor.Code +
                                                        "' modified - [" + typeBefore + "] " + sizeBefore + " " + result.Symbol.Name + " at " + strOpenPriceBefore + " sl: " + strStopLoss + " tp: " + strTakeProfit +
                                                        " cm: " + strCommission + " agent commission: " + strAgentCommission + " swap: " + strSwap;

                                                    TradingServer.Facade.FacadeAddNewSystemLog(5, content, comment, ipAddress, code);
                                                    TradingServer.Facade.FacadeAddNewSystemLog(5, content1, comment, ipAddress, code);
                                                    #endregion
                                                }
                                                else
                                                {
                                                    if (tempOpenTrade.Investor == null)
                                                    {
                                                        StringResult = subValue[0] + "$MCM007";
                                                        string content = "'" + code + "': restore order #" + tempOpenTrade.CommandCode + " failed(investor account not found)";
                                                        string comment = "[restore order]";
                                                        TradingServer.Facade.FacadeAddNewSystemLog(5, content, comment, ipAddress, code);
                                                    }

                                                    if (tempOpenTrade.Symbol == null)
                                                    {
                                                        StringResult = subValue[0] + "$MCM008";
                                                        string content = "'" + code + "': restore order #" + tempOpenTrade.CommandCode + " failed(symbol not found)";
                                                        string comment = "[restore order]";
                                                        TradingServer.Facade.FacadeAddNewSystemLog(5, content, comment, ipAddress, code);
                                                    }

                                                    if (tempOpenTrade.IGroupSecurity == null)
                                                    {
                                                        StringResult = subValue[0] + "$MCM009";
                                                        string content = "'" + code + "': restore order #" + tempOpenTrade.CommandCode + " failed(symbol account not found)";
                                                        string comment = "[restore order]";
                                                        TradingServer.Facade.FacadeAddNewSystemLog(5, content, comment, ipAddress, code);
                                                    }
                                                }
                                            }
                                        }

                                        StringResult = subValue[0] + "$" + resultRe;
                                    }
                                    else
                                    {
                                        StringResult = subValue[0] + "$MCM006";
                                        string content = "'" + code + "': restore order failed(not enough rights)";
                                        string comment = "[restore order]";
                                        TradingServer.Facade.FacadeAddNewSystemLog(5, content, comment, ipAddress, code);
                                    }
                                }
                                else
                                {
                                    StringResult = subValue[0] + "$MCM005";
                                    string content = "'" + code + "': restore order failed(invalid ip)";
                                    string comment = "[restore order]";
                                    TradingServer.Facade.FacadeAddNewSystemLog(5, content, comment, ipAddress, code);
                                }
                            }
                            break;
                        #endregion

                        #region ADD MARKET CONFIG(LOG)
                        case "AddMarketConfig":
                            {
                                bool checkip = Facade.FacadeCheckIpAdmin(code, ipAddress);
                                if (checkip)
                                {
                                    int ResultAddNew = -1;
                                    List<Business.ParameterItem> Result = new List<ParameterItem>();
                                    Result = this.ExtractParameterItem(subValue[1]);

                                    if (Result != null)
                                    {
                                        int countConfig = Result.Count;
                                        for (int m = 0; m < countConfig; m++)
                                        {
                                            ResultAddNew = TradingServer.Facade.FacadeAddNewMarketConfig(-1, Result[m].Name, Result[m].Code,
                                                Result[m].BoolValue, Result[m].StringValue, Result[m].NumValue, Result[m].DateValue);

                                            #region INSERT SYSTEM LOG
                                            //INSERT SYSTEM LOG
                                            //'2222': symbol config added/changed ['XAUUSD']
                                            string status = "[Failed]";

                                            if (ResultAddNew > 0)
                                                status = "[Success]";

                                            string content = "'" + code + "': market config added/changed ['" + Result[m].Name + "'] " + status;
                                            string comment = "[add market]";
                                            TradingServer.Facade.FacadeAddNewSystemLog(3, content, comment, ipAddress, code);
                                            #endregion
                                        }
                                    }

                                    if (ResultAddNew > 0)
                                    {
                                        Facade.FacadeSendNoticeManagerChangeSymbol(2, Result[0].SecondParameterID);
                                    }
                                    StringResult = subValue[0] + "$" + ResultAddNew.ToString();
                                }
                                else
                                {
                                    StringResult = subValue[0] + "$MCM005";
                                }
                            }
                            break;
                        #endregion

                        #region ADD TICK FROM MANAGER
                        case "AddTickByManager":
                            {
                                string[] subParameter = subValue[1].Split(',');
                                bool result = false;
                                if (subParameter.Length == 3)
                                {
                                    double bid, ask;
                                    double.TryParse(subParameter[1], out bid);
                                    double.TryParse(subParameter[2], out ask);
                                    string symbol = subParameter[0];
                                    Symbol symbolInstance = null;
                                    for (int i = Market.SymbolList.Count - 1; i >= 0; i--)
                                    {
                                        if (symbol == Market.SymbolList[i].Name)
                                        {
                                            symbolInstance = Market.SymbolList[i];
                                            break;
                                        }
                                    }
                                    if (symbolInstance != null)
                                    {
                                        bool checkip = Facade.FacadeCheckIpManager(code, ipAddress);
                                        if (checkip)
                                        {
                                            bool checkRule = Facade.FacadeCheckPermitTickManager(code);
                                            checkRule = true;
                                            if (checkRule)
                                            {
                                                Business.Tick newTick = new Tick();
                                                newTick.Ask = ask;
                                                newTick.Bid = bid;
                                                newTick.SymbolName = symbol;
                                                newTick.IsManager = true;
                                                newTick.TickTime = DateTime.Now;
                                                this.UpdateTick(newTick);
                                                result = true;
                                                string content = "'" + code + "': add tick " + symbol + " "
                                                    + TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(bid.ToString(),symbolInstance.Digit) + " / " + TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(ask.ToString(),symbolInstance.Digit) + " ("
                                                    + TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(symbolInstance.TickValue.Bid.ToString() ,symbolInstance.Digit) + " / " + TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(symbolInstance.TickValue.Ask.ToString(),symbolInstance.Digit) + ")";
                                                string comment = "[add tick]";
                                                TradingServer.Facade.FacadeAddNewSystemLog(3, content, comment, ipAddress, code);
                                            }
                                            else
                                            {
                                                string content = "'" + code + "': add tick failed (not enough rights) " + symbol + " "
                                                    + TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(bid.ToString(), symbolInstance.Digit) + " / " + TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(ask.ToString(), symbolInstance.Digit) + " ("
                                                    + TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(symbolInstance.TickValue.Bid.ToString(), symbolInstance.Digit) + " / " + TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(symbolInstance.TickValue.Ask.ToString(), symbolInstance.Digit) + ")";
                                                string comment = "[add tick]";
                                                TradingServer.Facade.FacadeAddNewSystemLog(3, content, comment, ipAddress, code);
                                            }
                                        }
                                        else
                                        {
                                            string content = "'" + code + "': add tick failed (invalid ip) " + symbol + " "
                                                    + TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(bid.ToString(), symbolInstance.Digit) + " / " + TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(ask.ToString(), symbolInstance.Digit) + " ("
                                                    + TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(symbolInstance.TickValue.Bid.ToString(), symbolInstance.Digit) + " / " + TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(symbolInstance.TickValue.Ask.ToString(), symbolInstance.Digit) + ")";
                                            string comment = "[add tick]";
                                            TradingServer.Facade.FacadeAddNewSystemLog(3, content, comment, ipAddress, code);
                                        }
                                    }
                                }
                                StringResult = subValue[0] + "$" + result;
                            }
                            break;
                        #endregion

                        #region SEND INTERNAL MAIL SERVER TO CLIENT
                        case "SendInternalMailToClient":
                            {
                                string[] subParameter = subValue[1].Split(',');
                                if (subParameter.Length > 0)
                                {
                                    string subject = TradingServer.Model.TradingCalculate.Instance.ConvertHexToString(subParameter[0]);
                                    string from = TradingServer.Model.TradingCalculate.Instance.ConvertHexToString(subParameter[1]);
                                    string fromName = TradingServer.Model.TradingCalculate.Instance.ConvertHexToString(subParameter[2]);
                                    string content = TradingServer.Model.TradingCalculate.Instance.ConvertHexToString(subParameter[3]);

                                    List<string> listInvestorCode = new List<string>();
                                    for (int j = 4; j < subParameter.Length; j++)
                                    {
                                        string login = subParameter[j];
                                        listInvestorCode.Add(login);
                                    }
                                    bool flag = false;
                                    if (listInvestorCode != null && listInvestorCode.Count > 0)
                                    {
                                        int count = listInvestorCode.Count;
                                        for (int i = 0; i < count; i++)
                                        {
                                            if (Business.Market.InvestorList != null)
                                            {
                                                int countInvestor = Business.Market.InvestorList.Count;

                                                for (int j = 0; j < countInvestor; j++)
                                                {
                                                    if (Business.Market.InvestorList[j].Code.Trim() == listInvestorCode[i].Trim())
                                                    {
                                                        bool permit = Facade.FacadeCheckPermitAgentSendMail(from, Business.Market.InvestorList[j].InvestorGroupInstance.InvestorGroupID);
                                                        if (!permit)
                                                        {
                                                            flag = true;
                                                            break;
                                                        }

                                                        #region SEARCH INVESTOR IN INVESTOR LIST
                                                        if (Business.Market.InvestorList[j].IsOnline)
                                                        {
                                                            if (Business.Market.InvestorList[j].ClientCommandQueue == null)
                                                                Business.Market.InvestorList[j].ClientCommandQueue = new List<string>();

                                                            Business.InternalMail newInternalMailMessage = new InternalMail();
                                                            newInternalMailMessage.Content = content;
                                                            newInternalMailMessage.From = from;
                                                            newInternalMailMessage.FromName = fromName;
                                                            newInternalMailMessage.Subject = subject;
                                                            newInternalMailMessage.Time = DateTime.Now;
                                                            newInternalMailMessage.To = listInvestorCode[i];
                                                            newInternalMailMessage.ToName = Business.Market.InvestorList[j].NickName;
                                                            newInternalMailMessage.IsNew = true;

                                                            //ADD NEW MESSAGE TO DATABASE
                                                            int id = TradingServer.Facade.FacadeAddNewInternalMail(newInternalMailMessage);
                                                            string message = "ITM21358$" + id + "█" + subject + "█" + from + "█" + fromName + "█" +
                                                               newInternalMailMessage.Time.ToString() + "█" + newInternalMailMessage.IsNew.ToString() + "█" +
                                                               content;
                                                            //TradingServer.Facade.FacadeAddNewInternalMail(

                                                            int countInvestorOnline = Business.Market.InvestorList[j].CountInvestorOnline(Business.Market.InvestorList[j].InvestorID);
                                                            if (countInvestorOnline > 0)
                                                                Business.Market.InvestorList[j].ClientCommandQueue.Add(message);
                                                        }
                                                        else
                                                        {
                                                            Business.InternalMail newInternalMailMessage = new InternalMail();
                                                            newInternalMailMessage.Content = content;
                                                            newInternalMailMessage.From = from;
                                                            newInternalMailMessage.FromName = fromName;
                                                            newInternalMailMessage.Subject = subject;
                                                            newInternalMailMessage.Time = DateTime.Now;
                                                            newInternalMailMessage.To = listInvestorCode[i];
                                                            newInternalMailMessage.ToName = Business.Market.InvestorList[j].NickName;
                                                            newInternalMailMessage.IsNew = true;

                                                            //ADD NEW MESSAGE TO DATABASE
                                                            TradingServer.Facade.FacadeAddNewInternalMail(newInternalMailMessage);
                                                        }
                                                        #endregion

                                                        flag = true;
                                                    }
                                                }

                                                if (!flag)
                                                {
                                                    Business.InternalMail newInternalMailMessage = new InternalMail();
                                                    newInternalMailMessage.Content = content;
                                                    newInternalMailMessage.From = from;
                                                    newInternalMailMessage.FromName = fromName;
                                                    newInternalMailMessage.Subject = subject;
                                                    newInternalMailMessage.Time = DateTime.Now;
                                                    newInternalMailMessage.To = listInvestorCode[i];
                                                    newInternalMailMessage.IsNew = true;
                                                    int InvestorID = TradingServer.Facade.FacadeGetInvestorIDByCode(listInvestorCode[i]);
                                                    if (InvestorID != -1)
                                                    {
                                                        Business.Agent agent = new Business.Agent();
                                                        agent = TradingServer.Facade.FacadeGetAgentByInvestorID(InvestorID);
                                                        if (agent != null)
                                                        {
                                                            if (agent.Name == "")
                                                            {
                                                                newInternalMailMessage.ToName = newInternalMailMessage.To;
                                                            }
                                                            else
                                                            {
                                                                newInternalMailMessage.ToName = agent.Name;
                                                            }
                                                        }
                                                        else
                                                        {
                                                            newInternalMailMessage.ToName = newInternalMailMessage.To;
                                                        }
                                                    }
                                                    else
                                                    {
                                                        newInternalMailMessage.ToName = newInternalMailMessage.To;
                                                    }

                                                    int id = TradingServer.Facade.FacadeAddNewInternalMail(newInternalMailMessage);
                                                    #region SEARCH AGENT IN AGENT LIST
                                                    for (int j = 0; j < Business.Market.AgentList.Count; j++)
                                                    {
                                                        if (Business.Market.AgentList[j].Code == listInvestorCode[i])
                                                        {
                                                            //add here
                                                            Business.Market.AgentList[j].AgentMail.Add(newInternalMailMessage);
                                                            Facade.FacadeSendNoticeManagerChangeMail(1, id, Business.Market.AgentList[j].AgentID);
                                                            break;
                                                        }
                                                    }
                                                    #endregion
                                                }
                                            }
                                        }
                                    }
                                    StringResult = subValue[0] + "$" + flag.ToString();
                                }
                            }
                            break;
                        #endregion

                        #region SEND INTERMAIL CLIENT TO SERVER
                        case "ClientSendInternalMailToServer":
                            {
                                string[] subParameter = subValue[1].Split(',');
                                if (subParameter.Length == 4)
                                {
                                    string subject = subParameter[0];
                                    string from = subParameter[1];
                                    string content = subParameter[2];
                                    string to = subParameter[3];

                                    if (Business.Market.AgentList != null)
                                    {
                                        bool flag = false;
                                        int count = Business.Market.AgentList.Count;
                                        for (int j = 0; j < count; j++)
                                        {
                                            if (Business.Market.AgentList[j].Code == to)
                                            {
                                                if (Business.Market.AgentList[j].IsOnline)
                                                {

                                                }
                                                else
                                                {

                                                }

                                                flag = true;
                                                break;
                                            }
                                        }
                                    }
                                }
                            }
                            break;
                        #endregion

                        #region SEND COMMAND LOGOUT CLIENT
                        case "LogOffClient":
                            {
                                bool result = false;
                                int investorID = 0;
                                if (int.TryParse(subValue[1], out investorID))
                                {
                                    Business.Investor tempInvestor = new Investor();
                                    tempInvestor = TradingServer.Facade.FacadeGetInvestorByInvestorID(investorID);
                                    if (tempInvestor != null)
                                    {
                                        bool checkip = Facade.FacadeCheckIpManager(code, ipAddress);
                                        if (checkip)
                                        {
                                            bool checkGroup = Facade.FacadeCheckPermitAccessGroupManagerAndAdmin(code, tempInvestor.InvestorGroupInstance.InvestorGroupID);
                                            if (checkGroup)
                                            {
                                                result = Business.Market.SendNotifyToClient("LOFF14790251", 3, investorID);

                                                if (result)
                                                {
                                                    string content = "'" + code + "': logoff '" + tempInvestor.Code + "' [Success]";
                                                    string comment = "[logoff client]";
                                                    TradingServer.Facade.FacadeAddNewSystemLog(4, content, comment, ipAddress, code);
                                                }
                                                else
                                                {
                                                    string content = "'" + code + "': logoff '" + tempInvestor.Code + "' [Failed]";
                                                    string comment = "[logoff client]";
                                                    TradingServer.Facade.FacadeAddNewSystemLog(4, content, comment, ipAddress, code);
                                                }
                                            }
                                            else
                                            {
                                                string content = "'" + code + "': logoff '" + tempInvestor.Code + "' failed(not enough rights)";
                                                string comment = "[logoff client]";
                                                TradingServer.Facade.FacadeAddNewSystemLog(4, content, comment, ipAddress, code);
                                            }
                                        }
                                        else
                                        {
                                            string content = "'" + code + "': logoff '" + tempInvestor.Code + "' failed(invalid ip)";
                                            string comment = "[logoff client]";
                                            TradingServer.Facade.FacadeAddNewSystemLog(4, content, comment, ipAddress, code);
                                        }
                                    }
                                }

                                StringResult = subValue[0] + "$" + result;
                            }
                            break;
                        #endregion

                        //Command Delete
                        //
                        #region Delete Investor Group(LOG)
                        case "DeleteInvestorGroup":
                            {
                                if (!string.IsNullOrEmpty(subValue[1]))
                                {
                                    string[] subParameter = subValue[1].Split(',');
                                    if (subParameter.Length == 1)
                                    {
                                        int InvestorGroupID = 0;
                                        int.TryParse(subParameter[0], out InvestorGroupID);
                                        StringResult = subValue[0] + "$" + this.DeleteGroup(InvestorGroupID);
                                    }
                                }
                            }
                            break;

                        case "DeleteInvestorGroupByName":
                            {
                                bool checkip = Facade.FacadeCheckIpAdmin(code, ipAddress);
                                if (checkip)
                                {
                                    if (!string.IsNullOrEmpty(subValue[1]))
                                    {
                                        string[] subParameter = subValue[1].Split(',');
                                        string resultDelete = string.Empty;
                                        if (subParameter.Length == 1)
                                        {
                                            resultDelete = this.DeleteGroup(subParameter[0]);
                                            StringResult = subValue[0] + "$" + resultDelete;
                                        }

                                        #region INSERT SYSTEM LOG
                                        //INSERT SYSTEM LOG
                                        string status = "[Failed]";
                                        if (resultDelete == "DSyE010")
                                            status = "[Success]";

                                        string content = "'" + code + "': " + subParameter[0] + " has been delete " + status;
                                        string comment = "[delete group]";
                                        TradingServer.Facade.FacadeAddNewSystemLog(3, content, comment, ipAddress, code);
                                        #endregion
                                    }
                                }
                                else
                                {
                                    StringResult = subValue[0] + "$MCM005";
                                }
                            }
                            break;
                        #endregion

                        #region Delete Investor Group Config
                        case "DeleteInvestorGroupConfig":
                            {
                                if (!string.IsNullOrEmpty(subValue[1]))
                                {
                                    string[] subParameter = subValue[1].Split(',');
                                    if (subParameter.Length > 0)
                                    {
                                        int InvestorGroupConfigID = 0;
                                        int.TryParse(subParameter[0], out InvestorGroupConfigID);
                                        bool Result = false;
                                        Result = TradingServer.Facade.FacadeDeleteInvestorGroupConfig(InvestorGroupConfigID);
                                        StringResult = subValue[0] + "$" + Result.ToString();
                                    }
                                }
                            }
                            break;
                        #endregion

                        #region Delete IGroupSymbol
                        case "DeleteIGroupSymbol":
                            {
                                bool checkip = Facade.FacadeCheckIpAdmin(code, ipAddress);
                                if (checkip)
                                {
                                    if (!string.IsNullOrEmpty(subValue[1]))
                                    {
                                        string[] subParameter = subValue[1].Split(',');
                                        if (subParameter.Length > 0)
                                        {
                                            int IGroupSymbolID = 0;
                                            int.TryParse(subParameter[0], out IGroupSymbolID);
                                            bool Result = false;
                                            Result = TradingServer.Facade.FacadeDeleteIGroupSymbol(IGroupSymbolID);
                                            StringResult = subValue[0] + "$" + Result.ToString();
                                        }
                                    }
                                }
                                else
                                {
                                    StringResult = subValue[0] + "$MCM005";
                                }
                            }
                            break;
                        #endregion

                        #region Delete IGroupSecurityConfig
                        case "DeleteIGroupSymbolConfigByIGroupSymbolID":
                            {
                                bool checkip = Facade.FacadeCheckIpAdmin(code, ipAddress);
                                if (checkip)
                                {
                                    int IGroupSymbolID = -1;
                                    int.TryParse(subValue[1], out IGroupSymbolID);
                                    StringResult = subValue[0] + "$" + TradingServer.Facade.FacadeDeleteIGroupSymbolConfigByIGroupSymbolID(IGroupSymbolID);
                                }
                                else
                                {
                                    StringResult = subValue[0] + "$MCM005";
                                }
                            }
                            break;
                        #endregion

                        #region Delete IGroupSecurity
                        case "DeleteIGroupSecurity":
                            {
                                bool checkip = Facade.FacadeCheckIpAdmin(code, ipAddress);
                                if (checkip)
                                {
                                    if (!string.IsNullOrEmpty(subValue[1]))
                                    {
                                        string[] subParameter = subValue[1].Split(',');
                                        if (subParameter.Length > 0)
                                        {
                                            int IGroupSecurityID = 0;
                                            int.TryParse(subParameter[0], out IGroupSecurityID);
                                            bool Result = false;
                                            Result = TradingServer.Facade.FacadeDeleteIGroupSecurityByIGroupSecurityID(IGroupSecurityID);
                                            StringResult = subValue[0] + "$" + Result.ToString();
                                        }
                                    }
                                }
                                else
                                {
                                    StringResult = subValue[0] + "$MCM005";
                                }
                            }
                            break;
                        #endregion

                        #region Delete IGroupSecurityConfig
                        case "DeleteIGroupSecurityConfigByIGroupSecurityID":
                            {
                                bool checkip = Facade.FacadeCheckIpAdmin(code, ipAddress);
                                if (checkip)
                                {
                                    int IGroupSecurityID = -1;
                                    int.TryParse(subValue[1], out IGroupSecurityID);
                                    StringResult = subValue[0] + "$" + TradingServer.Facade.FacadeDeleteIGroupSecurityConfigByIGroupSecurity(IGroupSecurityID);
                                }
                                else
                                {
                                    StringResult = subValue[0] + "$MCM005";
                                }
                            }
                            break;
                        #endregion

                        #region Delete Security(LOG)
                        case "DeleteSecurityByName":
                            {
                                bool checkip = Facade.FacadeCheckIpAdmin(code, ipAddress);
                                if (checkip)
                                {
                                    if (!string.IsNullOrEmpty(subValue[1]))
                                    {
                                        string[] subParameter = subValue[1].Split(',');
                                        string resultDelete = string.Empty;
                                        if (subParameter.Length == 1)
                                        {
                                            resultDelete = this.DeleteSecurity(subParameter[0]);
                                            StringResult = subValue[0] + "$" + resultDelete;
                                        }

                                        #region INSERT SYSTEM LOG
                                        //INSERT SYSTEM LOG
                                        string status = "[Failed]";
                                        if (resultDelete == "DSyE006")
                                            status = "[Success]";

                                        string content = "'" + code + "': " + subParameter[0] + " has been delete " + status;
                                        string comment = "[delete security]";
                                        TradingServer.Facade.FacadeAddNewSystemLog(3, content, comment, ipAddress, code);
                                        #endregion
                                    }
                                }
                                else
                                {
                                    StringResult = subValue[0] + "$MCM005";
                                }
                            }
                            break;

                        case "DeleteSecurityByID":
                            {
                                if (!string.IsNullOrEmpty(subValue[1]))
                                {
                                    string[] subParameter = subValue[1].Split(',');
                                    if (subParameter.Length == 1)
                                    {
                                        int SecurityID = 0;
                                        int.TryParse(subParameter[0], out SecurityID);
                                        StringResult = subValue[0] + "$" + this.DeleteSecurity(SecurityID);
                                    }
                                }
                            }
                            break;
                        #endregion

                        #region Delete Symbol(LOG)
                        case "DeleteSymbol":
                            {
                                bool checkip = Facade.FacadeCheckIpAdmin(code, ipAddress);
                                if (checkip)
                                {
                                    if (!string.IsNullOrEmpty(subValue[1]))
                                    {
                                        string[] subParameter = subValue[1].Split(',');
                                        if (subParameter.Length == 1)
                                        {
                                            string result = this.DeleteSymbol(subParameter[0]);
                                            StringResult = subValue[0] + "$" + result;

                                            #region INSERT SYSTEM LOG
                                            //INSERT SYSTEM LOG
                                            string status = "[Failed]";
                                            if (result == "DSyE000")
                                                status = "[Success]";

                                            string content = "'" + code + "': " + subParameter[0] + " has been delete " + status;
                                            string comment = "[delete symbol]";
                                            TradingServer.Facade.FacadeAddNewSystemLog(3, content, comment, ipAddress, code);
                                            #endregion
                                        }
                                    }
                                }
                                else
                                {
                                    StringResult = subValue[0] + "$MCM005";
                                }
                            }
                            break;
                        #endregion

                        #region Delete TradingConfig(SymbolConfig)
                        case "DeleteTradingConfig":
                            {
                                if (!string.IsNullOrEmpty(subValue[1]))
                                {
                                    string[] subParameter = subValue[1].Split(',');
                                    if (subParameter.Length > 0)
                                    {
                                        int TradingConfigID = 0;
                                        int.TryParse(subParameter[0], out TradingConfigID);
                                        bool Result = false;
                                        Result = TradingServer.Facade.FacadeDeleteTradingConfig(TradingConfigID);
                                        StringResult = subValue[0] + "$" + Result.ToString();
                                    }
                                }
                            }
                            break;
                        #endregion

                        #region Delete SecurityConfig
                        case "DeleteSecurityConfig":
                            {
                                if (!string.IsNullOrEmpty(subValue[1]))
                                {
                                    string[] subParameter = subValue[1].Split(',');
                                    if (subParameter.Length > 0)
                                    {
                                        int SecurityConfigID = 0;
                                        int.TryParse(subParameter[0], out SecurityConfigID);
                                        bool Result = false;
                                        Result = TradingServer.Facade.FacadeDeleteSecurityConfigBySecurityConfigID(SecurityConfigID);
                                        StringResult = subValue[0] + "$" + Result.ToString();
                                    }
                                }
                            }
                            break;
                        #endregion

                        #region Delete Investor
                        case "DeleteInvestor":
                            {
                                if (!string.IsNullOrEmpty(subValue[1]))
                                {
                                    string[] subParameter = subValue[1].Split(',');
                                    if (subParameter.Length > 0)
                                    {
                                        int Result = -1;
                                        int InvestorID = 0;
                                        int.TryParse(subParameter[0], out InvestorID);
                                        Result = TradingServer.Facade.FacadeDeleteInvestor(InvestorID);
                                        StringResult = subValue[0] + "$" + Result.ToString();
                                    }
                                }
                            }
                            break;
                        #endregion

                        #region Delete Agent
                        case "DeleteAgentByID":
                            {
                                bool checkip = Facade.FacadeCheckIpAdmin(code, ipAddress);
                                if (checkip)
                                {
                                    if (!string.IsNullOrEmpty(subValue[1]))
                                    {
                                        if (subValue[1].Length > 0)
                                        {
                                            int Result = -1;
                                            int AgentID = 0;
                                            int.TryParse(subValue[1], out AgentID);
                                            TradingServer.Facade.FacadeManagerLogout(AgentID);
                                            TradingServer.Facade.FacadeAdminLogout(AgentID);
                                            Business.Agent agent = new Business.Agent();
                                            agent = TradingServer.Facade.FacadeGetAgentByAgentID(AgentID);
                                            Result = TradingServer.Facade.FacadeDeleteIAgentSecurityByAgentID(AgentID);
                                            if (Result != -1)
                                            {
                                                Result = TradingServer.Facade.FacadeDeleteIAgentGroupByAgentID(AgentID);
                                            }
                                            if (Result != -1)
                                            {
                                                Result = TradingServer.Facade.FacadeDeletePermitByAgentID(AgentID);
                                            }
                                            if (Result != -1)
                                            {
                                                Result = TradingServer.Facade.FacadeDeleteAgent(AgentID);
                                            }
                                            if (Result != -1)
                                            {
                                                Result = TradingServer.Facade.FacadeDeleteInvestor(agent.InvestorID);
                                            }
                                            StringResult = subValue[0] + "$" + Result.ToString();

                                            #region INSERT SYSTEM LOG
                                            //INSERT SYSTEM LOG
                                            string status = "[Failed]";
                                            if (Result > 0)
                                                status = "[Success]";

                                            string content = "'" + code + "': " + agent.Name + " has been delete " + status;
                                            string comment = "[delete agent]";
                                            TradingServer.Facade.FacadeAddNewSystemLog(3, content, comment, ipAddress, code);
                                            #endregion
                                        }
                                    }
                                }
                                else
                                {
                                    StringResult = subValue[0] + "$MCM005";
                                }
                            }
                            break;
                        #endregion

                        #region Delete Alert
                        case "DeleteAlertByID":
                            {
                                if (!string.IsNullOrEmpty(subValue[1]))
                                {
                                    if (subValue[1].Length > 0)
                                    {
                                        string[] subParameter = subValue[1].Split(',');
                                        if (subParameter.Length > 0)
                                        {
                                            int Result = -1;
                                            int alertID = int.Parse(subParameter[0]);
                                            string symbol = subParameter[1];
                                            int investorID = int.Parse(subParameter[2]);
                                            Result = TradingServer.Facade.FacadeDeleteAlertByID(alertID, symbol, investorID);
                                            if (Result != -1)
                                            {
                                                StringResult = subValue[0] + "$" + alertID;
                                            }
                                            else
                                            {
                                                StringResult = subValue[0] + "$" + Result.ToString();
                                            }
                                        }
                                    }
                                }
                            }
                            break;
                        case "DeleteAlert":
                            {
                                if (!string.IsNullOrEmpty(subValue[1]))
                                {
                                    if (subValue[1].Length > 0)
                                    {
                                        string[] subParameter = subValue[1].Split('{');
                                        if (subParameter.Length > 0)
                                        {
                                            int Result = -1;
                                            int alertID = int.Parse(subParameter[0]);
                                            string symbol = subParameter[1];
                                            int investorID = int.Parse(subParameter[2]);
                                            Result = TradingServer.Facade.FacadeDeleteAlertByID(alertID, symbol, investorID);
                                            if (Result != -1)
                                            {
                                                StringResult = subValue[0] + "$" + alertID;
                                            }
                                            else
                                            {
                                                StringResult = subValue[0] + "$" + Result.ToString();
                                            }
                                        }
                                    }
                                }
                            }
                            break;
                        #endregion

                        #region Delete Mail
                        case "DeleteMailByID":
                            {
                                if (!string.IsNullOrEmpty(subValue[1]))
                                {
                                    bool Result = false;
                                    if (subValue[1].Length > 0)
                                    {
                                        int mailID = int.Parse(subValue[1]);
                                        Result = TradingServer.Facade.DeleteInternalMail(mailID);

                                    }
                                    StringResult = subValue[0] + "$" + Result.ToString();
                                }
                            }
                            break;
                        #endregion

                        #region Delete Permit
                        case "DeletePermitByID":
                            {
                                if (!string.IsNullOrEmpty(subValue[1]))
                                {
                                    string[] subParameter = subValue[1].Split(',');
                                    if (subParameter.Length > 0)
                                    {
                                        bool Result = false;
                                        int PermitID = 0;
                                        int.TryParse(subParameter[0], out PermitID);
                                        Result = TradingServer.Facade.FacadeDeletePermitByID(PermitID);
                                        StringResult = subValue[0] + "$" + Result.ToString();
                                    }
                                }
                            }
                            break;
                        #endregion

                        #region Delete IAgentSecurity
                        case "DeleteIAgentSecurityByID":
                            {
                                if (!string.IsNullOrEmpty(subValue[1]))
                                {
                                    bool ResultAddNew = false;
                                    int IAgentSecurityID = 0;
                                    string[] subParameter = subValue[1].Split(',');
                                    if (subParameter.Length > 0)
                                    {
                                        int.TryParse(subParameter[0], out IAgentSecurityID);
                                        ResultAddNew = TradingServer.Facade.FacadeDeleteIAgentSecurityByID(IAgentSecurityID);
                                        StringResult = subValue[0] + "$" + ResultAddNew.ToString();
                                    }
                                }
                            }
                            break;

                        case "DeleteIAgentSecurityByAgentID":
                            {
                                if (!string.IsNullOrEmpty(subValue[1]))
                                {
                                    int ResultAddNew = -1;
                                    int AgentID = 0;
                                    string[] subParameter = subValue[1].Split(',');
                                    if (subParameter.Length > 0)
                                    {
                                        int.TryParse(subParameter[0], out AgentID);
                                        ResultAddNew = TradingServer.Facade.FacadeDeleteIAgentSecurityByAgentID(AgentID);
                                        StringResult = subValue[0] + "$" + ResultAddNew;
                                    }
                                }
                            }
                            break;
                        case "DeleteIAgentSecurityBySecurityID":
                            {
                                if (!string.IsNullOrEmpty(subValue[1]))
                                {
                                    bool ResultAddNew = false;
                                    int SecurityID = 0;
                                    string[] subParameter = subValue[1].Split(',');
                                    if (subParameter.Length > 0)
                                    {
                                        int.TryParse(subParameter[0], out SecurityID);
                                        ResultAddNew = TradingServer.Facade.FacadeDeleteIAgentSecurityBySecurityID(SecurityID);
                                        StringResult = subValue[0] + "$" + ResultAddNew.ToString();
                                    }
                                }
                            }
                            break;
                        #endregion

                        #region Delete IAgentGroup
                        case "DeleteIAgentGroupByID":
                            {
                                if (!string.IsNullOrEmpty(subValue[1]))
                                {
                                    bool ResultAddNew = false;
                                    int IAgentGroupID = 0;
                                    string[] subParameter = subValue[1].Split(',');
                                    if (subParameter.Length > 0)
                                    {
                                        int.TryParse(subParameter[0], out IAgentGroupID);
                                        ResultAddNew = TradingServer.Facade.FacadeDeleteIAgentGroupByID(IAgentGroupID);
                                        StringResult = subValue[0] + "$" + ResultAddNew.ToString();
                                    }
                                }
                            }
                            break;
                        case "DeleteIAgentGroupByAgentID":
                            {
                                if (!string.IsNullOrEmpty(subValue[1]))
                                {
                                    int ResultAddNew = -1;
                                    int AgentID = 0;
                                    string[] subParameter = subValue[1].Split(',');
                                    if (subParameter.Length > 0)
                                    {
                                        int.TryParse(subParameter[0], out AgentID);
                                        ResultAddNew = TradingServer.Facade.FacadeDeleteIAgentGroupByAgentID(AgentID);
                                        StringResult = subValue[0] + "$" + ResultAddNew;
                                    }
                                }
                            }
                            break;
                        case "DeleteIAgentGroupByInvestorGroupID":
                            {
                                if (!string.IsNullOrEmpty(subValue[1]))
                                {
                                    bool ResultAddNew = false;
                                    int InvestorGroupID = 0;
                                    string[] subParameter = subValue[1].Split(',');
                                    if (subParameter.Length > 0)
                                    {
                                        int.TryParse(subParameter[0], out InvestorGroupID);
                                        ResultAddNew = TradingServer.Facade.FacadeDeleteIAgentGroupByInvestorGroupID(InvestorGroupID);
                                        StringResult = subValue[0] + "$" + ResultAddNew.ToString();
                                    }
                                }
                            }
                            break;

                        #endregion

                        #region Delete Investor Profile
                        case "DeleteInvestorProfile":
                            {
                                //if (!string.IsNullOrEmpty(subValue[1]))
                                //{
                                //    string[] subParameter = subValue[1].Split(',');
                                //    if (subParameter.Length > 0)
                                //    {
                                //        bool Result = false;
                                //        int InvestorID = 0;
                                //        int.TryParse(subParameter[0], out InvestorID);
                                //        Result = TradingServer.Facade.FacadeDeleteInvestorProfileByInvestorID(InvestorID);
                                //        StringResult = Result.ToString();
                                //    }
                                //}
                            }
                            break;
                        #endregion

                        #region Close Command By Manager(LOG)
                        case "CloseCommandByManager":
                            {
                                bool checkip = Facade.FacadeCheckIpManagerAndAdmin(code, ipAddress);
                                if (checkip)
                                {
                                    bool checkRule = Facade.FacadeCheckPermitCommandManagerAndAdmin(code);
                                    if (checkRule)
                                    {
                                        bool Result = false;
                                        int CommandID = -1;
                                        double Size = 0;
                                        double ClosePrices = 0;
                                        string[] subParameter = subValue[1].Split(',');
                                        if (subParameter.Length == 3)
                                        {
                                            int.TryParse(subParameter[0], out CommandID);
                                            double.TryParse(subParameter[1], out Size);
                                            double.TryParse(subParameter[2], out ClosePrices);

                                            Business.OpenTrade tempOpenTrade = TradingServer.Facade.FacadeFindOpenTradeInCommandEx(CommandID);
                                            bool checkGroup = Facade.FacadeCheckPermitAccessGroupManagerAndAdmin(code, tempOpenTrade.Investor.InvestorGroupInstance.InvestorGroupID);
                                            if (checkGroup)
                                            {
                                                Result = TradingServer.ClientFacade.FacadeCloseSpotCommandByManager(CommandID, Size, ClosePrices);

                                                #region INSERT SYSTEM LOG
                                                //INSERT SYSTEM LOG
                                                //'2222': account '9789300', close order #11349528 buy 2.00 EURUSD at 1.4624
                                                string size = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(tempOpenTrade.Size.ToString(), 2);
                                                string mode = TradingServer.Facade.FacadeGetTypeNameByTypeID(tempOpenTrade.Type.ID);
                                                string closePrice = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(subParameter[2], tempOpenTrade.Symbol.Digit);
                                                string content = "'" + code + "': account '" + tempOpenTrade.Investor.Code + "' close order #" + tempOpenTrade.CommandCode +
                                                    " " + mode + " " + size + " " + tempOpenTrade.Symbol.Name + " at " + closePrice;
                                                string comment = "[manager close command]";
                                                TradingServer.Facade.FacadeAddNewSystemLog(5, content, comment, ipAddress, code);
                                                #endregion

                                                if (Result)
                                                    StringResult = subValue[0] + "$" + Result + ",MCM001";
                                                else StringResult = subValue[0] + "$" + Result + ",MCM004";
                                            }
                                            else
                                            {
                                                StringResult = subValue[0] + "$" + false + ",MCM006";
                                                string content = "'" + code + "': manager close command failed(not enough rights)";
                                                string comment = "[manager close command]";
                                                TradingServer.Facade.FacadeAddNewSystemLog(5, content, comment, ipAddress, code);
                                            }
                                        }
                                        else
                                        {
                                            StringResult = subValue[0] + "$" + false + ",MCM004";
                                            string content = "'" + code + "': manager close command failed(invalid parameter)";
                                            string comment = "[manager close command]";
                                            TradingServer.Facade.FacadeAddNewSystemLog(5, content, comment, ipAddress, code);
                                        }
                                    }
                                    else
                                    {
                                        StringResult = subValue[0] + "$" + false + ",MCM006";
                                        string content = "'" + code + "': manager close command failed(not enough rights)";
                                        string comment = "[manager close command]";
                                        TradingServer.Facade.FacadeAddNewSystemLog(5, content, comment, ipAddress, code);
                                    }
                                }
                                else
                                {
                                    StringResult = subValue[0] + "$" + false + ",MCM005";
                                    string content = "'" + code + "': manager close command failed(invalid ip)";
                                    string comment = "[manager close command]";
                                    TradingServer.Facade.FacadeAddNewSystemLog(5, content, comment, ipAddress, code);
                                }
                            }
                            break;
                        #endregion

                        #region Close List Command By Manager
                        case "CloseListCommandByManager":
                            {
                                bool Result = false;
                                string[] subParamter = subValue[1].Split(',');
                                if (subParamter.Length > 0)
                                {
                                    int countCommand = subParamter.Length;
                                    for (int j = 0; j < countCommand; j++)
                                    {
                                        int CommandID = -1;
                                        double Size = 0;
                                        double ClosePrices = 0;
                                        int.TryParse(subParamter[j], out CommandID);
                                        j++;
                                        double.TryParse(subParamter[j], out Size);
                                        j++;
                                        double.TryParse(subParamter[j], out ClosePrices);
                                        Result = TradingServer.ClientFacade.FacadeCloseSpotCommandByManager(CommandID, Size, ClosePrices);
                                    }
                                }

                                StringResult = subValue[0] + "$" + Result;
                            }
                            break;
                        #endregion

                        #region Delete Command By Manager(Complete)(LOG)
                        case "DeleteCommandByManager":
                            {
                                bool checkip = Facade.FacadeCheckIpManagerAndAdmin(code, ipAddress);
                                if (checkip)
                                {
                                    bool checkRule = Facade.FacadeCheckPermitCommandManagerAndAdmin(code);
                                    if (checkRule)
                                    {
                                        bool Result = false;
                                        int CommandID = -1;
                                        int.TryParse(subValue[1], out CommandID);
                                        Business.OpenTrade tempOpenTrade = new OpenTrade();

                                        tempOpenTrade = TradingServer.Facade.FacadeFindOpenTradeInCommandEx(CommandID);
                                        bool checkGroup = Facade.FacadeCheckPermitAccessGroupManagerAndAdmin(code, tempOpenTrade.Investor.InvestorGroupInstance.InvestorGroupID);
                                        if (checkGroup)
                                        {
                                            if (tempOpenTrade.Symbol == null || tempOpenTrade.Investor == null || tempOpenTrade.IGroupSecurity == null || tempOpenTrade.Type == null)
                                                return StringResult = subValue[0] + "$" + false;

                                            #region INSERT SYSTEM LOG
                                            //'2222': account '9720467', delete order #11232898 buy 1.00 EURUSD at 1.4474
                                            string size = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(tempOpenTrade.Size.ToString(), 2);
                                            string openPrice = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(tempOpenTrade.OpenPrice.ToString(), tempOpenTrade.Symbol.Digit);
                                            string comment = "[manager delete command]";
                                            string type = TradingServer.Model.TradingCalculate.Instance.ConvertTypeIDToString(tempOpenTrade.Type.ID);

                                            string content = "'" + code + "': account '" + tempOpenTrade.Investor.Code + "' delete order #" + tempOpenTrade.CommandCode +
                                                " " + type + " " + size + " " + tempOpenTrade.Symbol.Name + " at " + openPrice;

                                            TradingServer.Facade.FacadeAddNewSystemLog(5, content, comment, ipAddress, code);
                                            #endregion

                                            Result = TradingServer.Facade.FacadeDeleteOpenTradeByManager(CommandID);
                                            if (Result)
                                                StringResult = subValue[0] + "$" + Result + ",MCM001";
                                            else StringResult = subValue[0] + "$" + Result + ",MCM004";
                                        }
                                        else
                                        {
                                            StringResult = subValue[0] + "$" + false + ",MCM006";
                                            string content = "'" + code + "': manager delete command failed(not enough rights)";
                                            string comment = "[manager delete command]";
                                            TradingServer.Facade.FacadeAddNewSystemLog(5, content, comment, ipAddress, code);
                                        }
                                    }
                                    else
                                    {
                                        StringResult = subValue[0] + "$" + false + ",MCM006";
                                        string content = "'" + code + "': manager delete command failed(not enough rights)";
                                        string comment = "[manager delete command]";
                                        TradingServer.Facade.FacadeAddNewSystemLog(5, content, comment, ipAddress, code);
                                    }
                                }
                                else
                                {
                                    StringResult = subValue[0] + "$" + false + ",MCM005";
                                    string content = "'" + code + "': manager delete command failed(invalid ip)";
                                    string comment = "[manager delete command]";
                                    TradingServer.Facade.FacadeAddNewSystemLog(5, content, comment, ipAddress, code);
                                }
                            }
                            break;
                        #endregion

                        #region DELETE ORDER IN DATABASE
                        case "DeleteOrder":
                            {
                                string[] subParameter = subValue[1].Split(',');
                                bool Result = false;
                                if (subParameter.Length == 2)
                                {
                                    int DealID = int.Parse(subParameter[0]);
                                    int InvestorID = int.Parse(subParameter[1]);
                                    switch (subParameter[1])
                                    {
                                        case "OPT001":
                                            Result = TradingServer.Facade.FacadeDeleteOpenTradeByID(DealID);

                                            if (Result)
                                            {
                                                TradingServer.Facade.FacadeRemoveOpenTradeInInvestorList(DealID, InvestorID);
                                            }
                                            break;
                                        case "CMH01":
                                            Result = TradingServer.Facade.FacadeDeleteCommandHistory(DealID);
                                            break;
                                        case "IAL01":
                                            Result = TradingServer.Facade.FacadeDeleteInvestorAccountLog(DealID);
                                            break;
                                    }
                                }

                                StringResult = subValue[0] + "$" + Result;
                            }
                            break;
                        #endregion

                        #region DELETE MARKET CONFIG(DELETE HOLIDAY)
                        case "DeleteMarketConfig":
                            {
                                bool checkip = Facade.FacadeCheckIpAdmin(code, ipAddress);
                                if (checkip)
                                {
                                    bool result = false;
                                    int marketConfigID = -1;
                                    int.TryParse(subValue[1], out marketConfigID);
                                    result = TradingServer.Facade.FacadeDeleteMarketConfig(marketConfigID);

                                    StringResult = subValue[0] + "$" + result;
                                }
                                else
                                {
                                    StringResult = subValue[0] + "$MCM005";
                                }
                            }
                            break;
                        #endregion

                        //Command Update
                        //
                        #region Update Investor Group(LOG)
                        case "UpdateInvestorGroup":
                            {
                                bool checkip = Facade.FacadeCheckIpAdmin(code, ipAddress);
                                if (checkip)
                                {
                                    bool resultUpdate = false;
                                    Business.InvestorGroup Result = new InvestorGroup();
                                    Result = this.ExtractInvestorGroup(subValue[1]);
                                    resultUpdate = Facade.FacadeUpdateInvestorGroup(Result);

                                    #region INSERT SYSTEM LOG
                                    //INSERT SYSTEM LOG
                                    //'2222': group config added/changed ['test-duc']
                                    //string status = "[Failed]";

                                    if (resultUpdate)
                                    {
                                        TradingServer.Facade.FacadeCheckUpdateGroupVirtualDealerOnline();
                                        //status = "[Success]";
                                    }

                                    //string content = "'" + code + "': group config added/changed ['" + Result.Name + "'] " + status;
                                    //string comment = "[update group]";
                                    //TradingServer.Facade.FacadeAddNewSystemLog(3, content, comment, ipAddress, code);
                                    #endregion

                                    if (resultUpdate)
                                    {
                                        //SEND NOTIFY TO MANAGER
                                        Facade.FacadeSendNoticeManagerChangeGroup(1, Result.InvestorGroupID);

                                        //SEND NOTIFY TO AGENT SERVER
                                        Business.AgentNotify newAgentNotify = new AgentNotify();
                                        newAgentNotify.NotifyMessage = "UpdateGroup$" + Result.InvestorGroupID;
                                        TradingServer.Agent.AgentConfig.Instance.AddNotifyToAgent(newAgentNotify, Result);
                                    }
                                    StringResult = subValue[0] + "$" + resultUpdate.ToString();
                                }
                                else
                                {
                                    StringResult = subValue[0] + "$MCM005";
                                }
                            }
                            break;
                        #endregion

                        #region Update Investor Group Config
                        case "UpdateInvestorGroupConfig":
                            {
                                bool checkip = Facade.FacadeCheckIpAdmin(code, ipAddress);
                                if (checkip)
                                {
                                    bool ResultUpdate = false;
                                    List<Business.ParameterItem> Result = new List<ParameterItem>();
                                    Result = this.ExtractParameterItem(subValue[1]);
                                    ResultUpdate = Facade.FacadeUpdateInvestorGroupConfig(Result, ipAddress, code);

                                    if (ResultUpdate)
                                    {
                                        //SEND NOTIFY TO MANAGER
                                        Facade.FacadeSendNoticeManagerChangeGroup(2, Result[0].SecondParameterID);

                                        //SEND NOTIFY TO AGENT SERVER
                                        if (Business.Market.InvestorGroupList != null)
                                        {
                                            int count = Business.Market.InvestorGroupList.Count;
                                            for (int i = 0; i < count; i++)
                                            {
                                                if (Business.Market.InvestorGroupList[i].InvestorGroupID == Result[0].SecondParameterID)
                                                {
                                                    Business.AgentNotify newAgentNotify = new AgentNotify();
                                                    newAgentNotify.NotifyMessage = "UpdateGroup$" + Business.Market.InvestorGroupList[i].InvestorGroupID;
                                                    TradingServer.Agent.AgentConfig.Instance.AddNotifyToAgent(newAgentNotify, Business.Market.InvestorGroupList[i]);
                                                }
                                            }
                                        }

                                        //SEND NOTIFY TO CLIENT
                                        TradingServer.Business.Market.SendNotifyToClient("UIG69345", 1, Result[0].SecondParameterID);
                                    }

                                    StringResult = subValue[0] + "$" + ResultUpdate.ToString();
                                }
                                else
                                {
                                    StringResult = subValue[0] + "$MCM005";
                                }
                            }
                            break;
                        #endregion

                        #region Update IGroupSymbol
                        case "UpdateIGroupSymbol":
                            {
                                bool resultUpdate = false;
                                List<Business.IGroupSymbol> Result = new List<IGroupSymbol>();
                                Result = this.ExtractIGroupSymbol(subValue[1]);
                                if (Result != null)
                                {
                                    int countResult = Result.Count;
                                    for (int j = 0; j < countResult; j++)
                                    {
                                        resultUpdate = Facade.FacadeUpdateIGroupSymbol(Result[j].IGroupSymbolID, Result[j].SymbolID, Result[j].InvestorGroupID);
                                    }

                                    StringResult = subValue[0] + "$" + resultUpdate.ToString();
                                }
                            }
                            break;
                        #endregion

                        #region Update IGroupSecurity
                        case "UpdateIGroupSecurity":
                            {
                                bool resultUpdate = false;
                                List<Business.IGroupSecurity> Result = new List<IGroupSecurity>();
                                Result = this.ExtractIGroupSecurity(subValue[1]);
                                if (Result != null)
                                {
                                    int countResult = Result.Count;
                                    for (int j = 0; j < countResult; j++)
                                    {
                                        resultUpdate = Facade.FacadeUpdateIGroupSecurity(Result[j].IGroupSecurityID, Result[j].SecurityID, Result[j].InvestorGroupID);
                                    }

                                    StringResult = subValue[0] + "$" + resultUpdate.ToString();
                                }
                            }
                            break;
                        #endregion

                        #region Update Security(LOG)
                        case "UpdateSecurity":
                            {
                                bool checkip = Facade.FacadeCheckIpAdmin(code, ipAddress);
                                if (checkip)
                                {
                                    bool ResultUpdate = false;
                                    if (!string.IsNullOrEmpty(subValue[1]))
                                    {
                                        string[] subParameter = subValue[1].Split(',');
                                        int MarketAreaID = -1;
                                        int.TryParse(subParameter[3], out MarketAreaID);
                                        if (subParameter.Length > 0)
                                        {
                                            int SecurityID = 0;
                                            int.TryParse(subParameter[0], out SecurityID);
                                            ResultUpdate = TradingServer.Facade.FacadeUpdateSecurity(SecurityID, subParameter[1], subParameter[2], MarketAreaID);

                                            #region INSERT SYSTEM LOG
                                            //INSERT SYSTEM LOG
                                            //'2222': group config added/changed ['test-duc']
                                            string status = "[Failed]";

                                            if (ResultUpdate)
                                                status = "[Success]";

                                            string content = "'" + code + "': security config added/changed ['" + subParameter[1] + "'] " + status;
                                            string comment = "[update security]";
                                            TradingServer.Facade.FacadeAddNewSystemLog(3, content, comment, ipAddress, code);
                                            #endregion
                                        }
                                    }

                                    StringResult = subValue[0] + "$" + ResultUpdate.ToString();
                                }
                                else
                                {
                                    StringResult = subValue[0] + "$MCM005";
                                }
                            }
                            break;
                        #endregion

                        #region Update Symbol(LOG)
                        case "UpdateSymbol":
                            {
                                bool checkip = Facade.FacadeCheckIpAdmin(code, ipAddress);
                                if (checkip)
                                {
                                    bool resultUpdate = false;
                                    if (!string.IsNullOrEmpty(subValue[1]))
                                    {
                                        string[] subParameter = subValue[1].Split(',');
                                        if (subParameter.Length > 0)
                                        {
                                            int SymbolID = 0;
                                            int SecurityID = 0;
                                            int RefSymbolID = 0;
                                            int MarketAreaID = 0;

                                            int.TryParse(subParameter[0], out SymbolID);
                                            int.TryParse(subParameter[1], out SecurityID);
                                            int.TryParse(subParameter[2], out RefSymbolID);
                                            int.TryParse(subParameter[3], out MarketAreaID);

                                            //Call Function Update New Symbol
                                            resultUpdate = TradingServer.Facade.FacadeUpdateSymbol(SymbolID, SecurityID, RefSymbolID, MarketAreaID, subParameter[4]);

                                            #region INSERT SYSTEM LOG
                                            //INSERT SYSTEM LOG
                                            //'2222': group config added/changed ['test-duc']
                                            string status = "[Failed]";

                                            if (resultUpdate)
                                            {
                                                status = "[Success]";
                                                TradingServer.Facade.FacadeCheckUpdateGroupVirtualDealerOnline();
                                            }
                                            string content = "'" + code + "': symbol config added/changed ['" + subParameter[4] + "'] " + status;
                                            string comment = "[update symbol]";
                                            TradingServer.Facade.FacadeAddNewSystemLog(3, content, comment, ipAddress, code);
                                            #endregion

                                            if (resultUpdate)
                                            {
                                                Facade.FacadeSendNoticeManagerChangeSymbol(1, SymbolID);

                                                //SEND COMMAND TO AGENT SERVER
                                                Business.AgentNotify newAgentNotify = new AgentNotify();
                                                newAgentNotify.NotifyMessage = "UpdateSymbol$" + SymbolID;
                                                TradingServer.Agent.AgentConfig.Instance.AddNotifyToAgent(newAgentNotify);
                                            }

                                            StringResult = subValue[0] + "$" + resultUpdate.ToString();
                                        }
                                    }
                                }
                                else
                                {
                                    StringResult = subValue[0] + "$MCM005";
                                }
                            }
                            break;
                        #endregion

                        #region Update TradingConfig(SymbolConfig)
                        case "UpdateTradingConfig":
                            {
                                bool resultUpdate = false;
                                List<Business.ParameterItem> Result = new List<ParameterItem>();
                                //CALL FUNCTION EXTRACT PARAMETER ITEM ONE
                                Result = this.ExtractParameterItem(subValue[1]);
                                int countResult = Result.Count;
                                bool checkip = Facade.FacadeCheckIpManagerAndAdmin(code, ipAddress);
                                if (checkip)
                                {
                                    if (countResult > 0)
                                    {
                                        //for (int j = 0; j < countResult; j++)
                                        //{
                                        //    resultUpdate = Facade.FacadeUpdateTradingConfig(Result[j]);
                                        //}

                                        resultUpdate = Facade.FacadeUpdateTradingConfig(Result, code, ipAddress);
                                        //string status = "[Failed]";
                                        if (resultUpdate)
                                        {
                                            //status = "[Success]";
                                            //SEND NOTITY TO MANAGER
                                            Facade.FacadeSendNoticeManagerChangeSymbol(2, Result[0].SecondParameterID);

                                            //SEND NOTIFY TO CLIENT
                                            TradingServer.Business.Market.SendNotifyToClient("UTC534345", 2, Result[0].SecondParameterID);
                                        }

                                        #region LOG(Duc Comment because in file ParameterItem.TradingConfg have log details)
                                        //string symbol = "", spreadDefault = "", spreadBalanace = "", stops = "", execution = "";
                                        //for (int i = Market.SymbolList.Count - 1; i >= 0; i--)
                                        //{
                                        //    if (Result[0].SecondParameterID == Market.SymbolList[i].SymbolID)
                                        //    {
                                        //        symbol = Market.SymbolList[i].Name;
                                        //        break;
                                        //    }
                                        //}
                                        //for (int i = 0; i < Result.Count; i++)
                                        //{
                                        //    if (Result[i].Code == "S006")
                                        //    {
                                        //        execution = Result[i].StringValue;
                                        //    }
                                        //    if (Result[i].Code == "S015")
                                        //    {
                                        //        stops = Result[i].NumValue;
                                        //    }
                                        //    if (Result[i].Code == "S013")
                                        //    {
                                        //        spreadDefault = Result[i].NumValue;
                                        //    }
                                        //    if (Result[i].Code == "S016")
                                        //    {
                                        //        spreadDefault = Result[i].StringValue;
                                        //    }
                                        //}
                                        ////'2222': EURUSD5 - spread: 1 / 2, stops: 15, execution: Market
                                        //string content = "'" + code + "': " + symbol + " spread default: " + spreadDefault + ", spread balance: " + spreadBalanace
                                        //    + ", stops: " + stops + ", execution: " + execution + status;
                                        //string comment = "[update symbol]";
                                        //TradingServer.Facade.FacadeAddNewSystemLog(3, content, comment, ipAddress, code);
                                        #endregion
                                    }
                                    StringResult = subValue[0] + "$" + resultUpdate.ToString();
                                }
                                else
                                {
                                    StringResult = subValue[0] + "$MCM005";
                                    if (countResult > 0)
                                    {
                                        #region LOG(Duc Comment because in file parameteritem.tradingconfig have log details)
                                        //string symbol = "", spreadDefault = "", spreadBalanace = "", stops = "", execution = "";
                                        //for (int i = Market.SymbolList.Count - 1; i >= 0; i--)
                                        //{
                                        //    if (Result[0].SecondParameterID == Market.SymbolList[i].SymbolID)
                                        //    {
                                        //        symbol = Market.SymbolList[i].Name;
                                        //        break;
                                        //    }
                                        //}
                                        //for (int i = 0; i < Result.Count; i++)
                                        //{
                                        //    if (Result[i].Code == "S006")
                                        //    {
                                        //        execution = Result[i].StringValue;
                                        //    }
                                        //    if (Result[i].Code == "S015")
                                        //    {
                                        //        stops = Result[i].NumValue;
                                        //    }
                                        //    if (Result[i].Code == "S013")
                                        //    {
                                        //        spreadDefault = Result[i].NumValue;
                                        //    }
                                        //    if (Result[i].Code == "S016")
                                        //    {
                                        //        spreadDefault = Result[i].StringValue;
                                        //    }
                                        //}
                                        ////'2222': EURUSD5 - spread: 1 / 2, stops: 15, execution: Market
                                        //string content = "'" + code + "': " + symbol + " spread default: " + spreadDefault + ", spread balance: " + spreadBalanace
                                        //    + ", stops: " + stops + ", execution: " + execution + " failed(invalid ip)";
                                        //string comment = "[update symbol]";
                                        //TradingServer.Facade.FacadeAddNewSystemLog(3, content, comment, ipAddress, code);
                                        #endregion
                                    }
                                }
                            }
                            break;
                        #endregion

                        #region UPDATE MARKET CONFIG
                        case "UpdateMarketConfig":
                            {
                                bool checkip = Facade.FacadeCheckIpAdmin(code, ipAddress);
                                if (checkip)
                                {
                                    bool resultUpdate = false;
                                    List<Business.ParameterItem> Result = new List<ParameterItem>();
                                    Result = this.ExtractParameterItem(subValue[1]);
                                    resultUpdate = TradingServer.Facade.FacadeUpdateMarketConfig(Result, ipAddress, code);
                                    //int countResult = Result.Count;
                                    //for (int j = 0; j < countResult; j++)
                                    //{
                                    //    resultUpdate = TradingServer.Facade.FacadeUpdateMarketConfig(Result[j]);

                                    //    #region INSERT SYSTEM LOG
                                    //    //INSERT SYSTEM LOG
                                    //    //'2222': symbol config added/changed ['XAUUSD']
                                    //    string content = "'" + code + "': market config added/changed ['" + Result[j].Name + "'] ";
                                    //    string comment = "[update market]";
                                    //    TradingServer.Facade.FacadeAddNewSystemLog(3, content, comment, ipAddress, code);
                                    //    #endregion
                                    //}

                                    StringResult = subValue[0] + "$" + resultUpdate;
                                }
                                else
                                {
                                    StringResult = subValue[0] + "$MCM005";
                                }
                            }
                            break;
                        #endregion

                        #region Update SecurityConfig
                        case "UpdateSecurityConfig":
                            {
                                bool ResultUpdate = false;
                                List<Business.ParameterItem> Result = new List<ParameterItem>();
                                Result = this.ExtractParameterItem(subValue[1]);

                                int countResult = Result.Count;
                                for (int j = 0; j < countResult; j++)
                                {
                                    ResultUpdate = Facade.FacadeUpdateSecurityConfig(Result[j]);
                                }

                                StringResult = subValue[0] + "$" + ResultUpdate.ToString();
                            }
                            break;
                        #endregion

                        #region Update Investor(LOG)
                        case "UpdateInvestor":
                            {
                                bool resultUpdate = false;
                                bool resultUpdateProfile = false;
                                Business.Investor Result = new Investor();
                                Result = this.ExtractionInvestor(subValue[1]);
                                bool checkip = Facade.FacadeCheckIpManagerAndAdmin(code, ipAddress);
                                if (checkip)
                                {
                                    bool checkRule = Facade.FacadeCheckPermitAccountManagerAndAdmin(code);
                                    bool checkGroup = Facade.FacadeCheckPermitAccessGroupManagerAndAdmin(code, Result.InvestorGroupInstance.InvestorGroupID);
                                    if (checkRule && checkGroup)
                                    {
                                        //resultUpdate = TradingServer.Facade.FacadeUpdateInvestor(Result);
                                        resultUpdate = TradingServer.Facade.FacadeUpdateInvestor(Result, ipAddress, code);
                                        //resultUpdateProfile = TradingServer.Facade.FacadeUpdateInvestorProfile(Result);
                                        if (resultUpdate)
                                        {
                                            resultUpdateProfile = TradingServer.Facade.FacadeUpdateInvestorProfile(Result, ipAddress, code);

                                            if (Result.PrimaryPwd != "") resultUpdate = TradingServer.Facade.FacadeUpdatePasswordByCode(Result.Code, Result.PrimaryPwd);
                                            if (Result.ReadOnlyPwd != "") resultUpdate = TradingServer.Facade.FacadeUpdateReadPwdByCode(Result.Code, Result.ReadOnlyPwd);
                                            if (Result.PhonePwd != "") resultUpdate = TradingServer.Facade.FacadeUpdatePhonePwdByCode(Result.Code, Result.PhonePwd);

                                            //SEND COMMAND TO AGENT SERVER
                                            Business.AgentNotify newAgentNotify = new AgentNotify();
                                            newAgentNotify.NotifyMessage = "UpdateInvestorNotify$" + Result.InvestorID;
                                            TradingServer.Agent.AgentConfig.Instance.AddNotifyToAgent(newAgentNotify, Result.InvestorGroupInstance);
                                        }

                                        StringResult = subValue[0] + "$" + resultUpdate.ToString();

                                        #region INSERT SYSTEM LOG
                                        ////INSERT SYSTEM LOG
                                        /////'2222': account '9720467' has been updated
                                        //string status = "[Failed]";

                                        //if (resultUpdate)
                                        //    status = "[Success]";

                                        //string content = "'" + code + "': account '" + Result.Code + "' has been updated " + status;
                                        //string comment = "[update account]";
                                        //TradingServer.Facade.FacadeAddNewSystemLog(3, content, comment, ipAddress, code);
                                        #endregion
                                    }
                                    else
                                    {
                                        StringResult = subValue[0] + "$MCM006";
                                        string content = "'" + code + "': account '" + Result.Code + "' has been updated failed(not enough rights)";
                                        string comment = "[update account]";
                                        TradingServer.Facade.FacadeAddNewSystemLog(3, content, comment, ipAddress, code);
                                    }
                                }
                                else
                                {
                                    StringResult = subValue[0] + "$" + "MCM005";
                                    string content = "'" + code + "': account '" + Result.Code + "' has been updated failed(invalid ip)";
                                    string comment = "[update account]";
                                    TradingServer.Facade.FacadeAddNewSystemLog(3, content, comment, ipAddress, code);
                                }
                            }
                            break;
                        #endregion

                        #region UPDATE PASSWORD BY CODE(LOG)
                        case "UpdatePasswordByCode":
                            {
                                string[] subParameter = subValue[1].Split(',');

                                bool resultUpdate = TradingServer.Facade.FacadeUpdatePasswordByCode(subParameter[0], subParameter[1]);

                                #region INSERT SYSTEM LOG
                                //INSERT SYSTEM LOG
                                ///'2222': account '9720467' change password failed
                                string status = "[Failed]";

                                if (resultUpdate)
                                    status = "[Success]";

                                string content = "'" + code + "': account '" + subParameter[0] + "' change master password " + status;
                                string comment = "[change password]";
                                TradingServer.Facade.FacadeAddNewSystemLog(3, content, comment, ipAddress, code);
                                #endregion

                                StringResult = subValue[0] + "$" + resultUpdate;
                            }
                            break;
                        #endregion

                        #region UPDATE READ PASSWORD BY CODE(LOG)
                        case "UpdateReadPwdByCode":
                            {
                                string[] subParameter = subValue[1].Split(',');

                                bool resultUpdate = TradingServer.Facade.FacadeUpdateReadPwdByCode(subParameter[0], subParameter[1]);

                                #region INSERT SYSTEM LOG
                                //INSERT SYSTEM LOG
                                ///'2222': account '9720467' change password failed
                                string status = "[Failed]";

                                if (resultUpdate)
                                    status = "[Success]";

                                string content = "'" + code + "': account '" + subParameter[0] + "' change read password " + status;
                                string comment = "[change password]";
                                TradingServer.Facade.FacadeAddNewSystemLog(3, content, comment, ipAddress, code);
                                #endregion

                                StringResult = subValue[0] + "$" + resultUpdate;
                            }
                            break;
                        #endregion

                        #region UPDATE PHONE PASSWORD(LOG)
                        case "UpdatePhonePwdByCode":
                            {
                                string[] subParameter = subValue[1].Split(',');
                                bool resultUpdate = TradingServer.Facade.FacadeUpdatePhonePwdByCode(subParameter[0], subParameter[1]);

                                #region INSERT SYSTEM LOG
                                //INSERT SYSTEM LOG
                                ///'2222': account '9720467' change password failed
                                string status = "[Failed]";

                                if (resultUpdate)
                                    status = "[Success]";

                                string content = "'" + code + "': account '" + subParameter[0] + "' change phone password " + status;
                                string comment = "[change password]";
                                TradingServer.Facade.FacadeAddNewSystemLog(3, content, comment, ipAddress, code);
                                #endregion

                                StringResult = subValue[0] + "$" + resultUpdate;
                            }
                            break;
                        #endregion

                        #region Update Agent(LOG)
                        case "UpdateAgent":
                            {
                                bool checkip = Facade.FacadeCheckIpAdmin(code, ipAddress);
                                if (checkip)
                                {
                                    bool resultUpdate = false;
                                    Business.Agent Result = new Agent();
                                    Result = this.ExtractionAgent(subValue[1]);
                                    resultUpdate = TradingServer.Facade.FacadeUpdateAgent(Result);
                                    if (Result.Pwd != "") resultUpdate = TradingServer.Facade.FacadeUpdatePasswordByCode(Result.Code, Result.Pwd);

                                    #region INSERT SYSTEM LOG
                                    //INSERT SYSTEM LOG
                                    //'2222': manager configuration has been changed
                                    string status = "[Failed]";

                                    if (resultUpdate)
                                        status = "[Success]";

                                    string content = "'" + code + "': manager configuration has been changed " + status;
                                    string comment = "[update agent]";
                                    TradingServer.Facade.FacadeAddNewSystemLog(3, content, comment, ipAddress, code);
                                    #endregion

                                    StringResult = subValue[0] + "$" + resultUpdate.ToString();
                                }
                                else
                                {
                                    StringResult = subValue[0] + "$MCM005";
                                }
                                break;
                            }
                        case "UpdatePasswordAgent":
                            {
                                bool resultUpdate = false;
                                bool checkip = Facade.FacadeCheckIpManager(code, ipAddress);
                                if (checkip)
                                {
                                    if (subValue[1] != "")
                                    {
                                        string[] tempArr = subValue[1].Split('{');
                                        if (tempArr.Length == 3)
                                        {
                                            int investorID = 0;
                                            int.TryParse(tempArr[0], out investorID);
                                            if (investorID > 0)
                                            {
                                                resultUpdate = TradingServer.Facade.FacadeManagerChangePass(investorID,code, tempArr[1], tempArr[2]);
                                            }
                                        }
                                    }
                                    StringResult = subValue[0] + "$" + resultUpdate;
                                }
                                else
                                {
                                    StringResult = subValue[0] + "$MCM005";
                                }
                                #region INSERT SYSTEM LOG
                                //INSERT SYSTEM LOG
                                string content = "";
                                if (resultUpdate)
                                {
                                    content = "': manager update password [Success]";
                                }
                                else
                                {
                                    if (checkip)
                                    {
                                        content = "': manager update password [Failed]";
                                    }
                                    else
                                    {
                                        content = "': manager update password (invalid ip)[Failed]";
                                    }
                                }
                                string comment = "[update password agent]";
                                TradingServer.Facade.FacadeAddNewSystemLog(3, content, comment, ipAddress, code);
                                #endregion
                                break;
                            }

                        #endregion

                        #region Update Alert
                        case "UpdateAlert":
                            {
                                bool resultUpdate = false;
                                Business.PriceAlert result = new PriceAlert();
                                #region Update Alert
                                string[] subParameter = subValue[1].Split(',');
                                if (subParameter.Length > 0)
                                {
                                    result.TickOnline = new Tick();
                                    result.Symbol = subParameter[0];
                                    result.Email = subParameter[1];
                                    result.PhoneNumber = subParameter[2];
                                    result.Value = double.Parse(subParameter[3]);
                                    #region ConditionAlert & ActionAlert
                                    switch (subParameter[4])
                                    {
                                        case "LargerBid":
                                            {
                                                result.AlertCondition = Business.ConditionAlert.LargerBid;
                                                break;
                                            }
                                        case "LargerAsk":
                                            {
                                                result.AlertCondition = Business.ConditionAlert.LargerAsk;
                                                break;
                                            }
                                        case "LargerHighBid":
                                            {
                                                result.AlertCondition = Business.ConditionAlert.LargerHighBid;
                                                break;
                                            }
                                        case "LargerHighAsk":
                                            {
                                                result.AlertCondition = Business.ConditionAlert.LargerHighAsk;
                                                break;
                                            }
                                        case "SmallerBid":
                                            {
                                                result.AlertCondition = Business.ConditionAlert.SmallerBid;
                                                break;
                                            }
                                        case "SmallerAsk":
                                            {
                                                result.AlertCondition = Business.ConditionAlert.SmallerAsk;
                                                break;
                                            }
                                        case "SmallerLowBid":
                                            {
                                                result.AlertCondition = Business.ConditionAlert.SmallerLowBid;
                                                break;
                                            }
                                        case "SmallerLowAsk":
                                            {
                                                result.AlertCondition = Business.ConditionAlert.SmallerLowAsk;
                                                break;
                                            }
                                    }
                                    switch (subParameter[5])
                                    {
                                        case "Email":
                                            {
                                                result.AlertAction = Business.ActionAlert.Email;
                                                break;
                                            }
                                        case "SMS":
                                            {
                                                result.AlertAction = Business.ActionAlert.SMS;
                                                break;
                                            }
                                        case "Sound":
                                            {
                                                result.AlertAction = Business.ActionAlert.Sound;
                                                break;
                                            }
                                    }
                                    #endregion
                                    result.IsEnable = bool.Parse(subParameter[6]);
                                    result.InvestorID = int.Parse(subParameter[7]);
                                    result.DateCreate = DateTime.Parse(subParameter[8]);
                                    result.DateActive = result.DateCreate;
                                    result.Notification = subParameter[9];
                                    result.ID = int.Parse(subParameter[10]);
                                }
                                #endregion
                                resultUpdate = TradingServer.Facade.FacadeUpdateAlert(result);
                                StringResult = subValue[0] + "$" + resultUpdate.ToString();
                            }
                            break;
                        case "ConfigAlert":
                            {
                                bool resultUpdate = false;
                                Business.PriceAlert result = new PriceAlert();
                                #region Update Alert
                                string[] subParameter = subValue[1].Split('{');
                                if (subParameter.Length > 0)
                                {
                                    result.TickOnline = new Tick();
                                    result.Symbol = subParameter[0];
                                    result.Email = subParameter[1];
                                    result.PhoneNumber = subParameter[2];
                                    result.Value = double.Parse(subParameter[3]);
                                    #region ConditionAlert & ActionAlert
                                    switch (subParameter[4])
                                    {
                                        case "LargerBid":
                                            {
                                                result.AlertCondition = Business.ConditionAlert.LargerBid;
                                                break;
                                            }
                                        case "LargerAsk":
                                            {
                                                result.AlertCondition = Business.ConditionAlert.LargerAsk;
                                                break;
                                            }
                                        case "LargerHighBid":
                                            {
                                                result.AlertCondition = Business.ConditionAlert.LargerHighBid;
                                                break;
                                            }
                                        case "LargerHighAsk":
                                            {
                                                result.AlertCondition = Business.ConditionAlert.LargerHighAsk;
                                                break;
                                            }
                                        case "SmallerBid":
                                            {
                                                result.AlertCondition = Business.ConditionAlert.SmallerBid;
                                                break;
                                            }
                                        case "SmallerAsk":
                                            {
                                                result.AlertCondition = Business.ConditionAlert.SmallerAsk;
                                                break;
                                            }
                                        case "SmallerLowBid":
                                            {
                                                result.AlertCondition = Business.ConditionAlert.SmallerLowBid;
                                                break;
                                            }
                                        case "SmallerLowAsk":
                                            {
                                                result.AlertCondition = Business.ConditionAlert.SmallerLowAsk;
                                                break;
                                            }
                                    }
                                    switch (subParameter[5])
                                    {
                                        case "Email":
                                            {
                                                result.AlertAction = Business.ActionAlert.Email;
                                                break;
                                            }
                                        case "SMS":
                                            {
                                                result.AlertAction = Business.ActionAlert.SMS;
                                                break;
                                            }
                                        case "Sound":
                                            {
                                                result.AlertAction = Business.ActionAlert.Sound;
                                                break;
                                            }
                                    }
                                    #endregion
                                    result.IsEnable = bool.Parse(subParameter[6]);
                                    result.InvestorID = int.Parse(subParameter[7]);
                                    result.DateCreate = DateTime.Parse(subParameter[8]);
                                    result.DateActive = result.DateCreate;
                                    result.Notification = subParameter[9];
                                    result.ID = int.Parse(subParameter[10]);
                                }
                                #endregion
                                resultUpdate = TradingServer.Facade.FacadeUpdateAlert(result);
                                if (resultUpdate)
                                {
                                    StringResult = subValue[0] + "$" + result.ID + "{" + result.Symbol + "{" + result.Email + "{" + result.PhoneNumber + "{" +
                                    result.Value + "{" + result.AlertCondition + "{" + result.AlertAction + "{" + result.IsEnable + "{" +
                                    result.DateCreate + "{" + result.DateActive + "{" + result.InvestorID + "{" + result.Notification;
                                }
                                else
                                {
                                    StringResult = subValue[0] + "$";
                                }

                            }
                            break;
                        #endregion

                        #region Update Mail
                        case "UpdateMailStatus":
                            {
                                string[] subParameter = subValue[1].Split(',');
                                if (subParameter.Length > 0)
                                {
                                    bool isNew = bool.Parse(subParameter[0]);
                                    int mailID = int.Parse(subParameter[1]);
                                    TradingServer.Facade.UpdateInternalMailStatus(isNew, mailID);
                                }
                                StringResult = subValue[0] + "$";
                            }
                            break;
                        #endregion

                        #region UPDATE(RESET) USERCONFIG IPHONE, IPAD, SIVERLIGHT
                        case "AdminResetUserConfig":
                            {
                                int investorID = int.Parse(subValue[1]);

                                bool isUpdate = Investor.DBWInvestorInstance.UpdateAllUserConfig("", "", "", investorID);

                                if (isUpdate)
                                {
                                    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)
                                            {
                                                Business.Market.InvestorList[i].UserConfig = "";
                                                Business.Market.InvestorList[i].UserConfigIpad = "";
                                                Business.Market.InvestorList[i].UserConfigIphone = "";

                                                break;
                                            }
                                        }
                                    }
                                }
                            }
                            break;
                        #endregion

                        #region Update Permit(LOG)
                        case "UpdatePermit":
                            {
                                bool checkip = Facade.FacadeCheckIpAdmin(code, ipAddress);
                                if (checkip)
                                {
                                    bool resultUpdate = false;
                                    int AgentID = -1;
                                    int RoleID = -1;
                                    List<int> ListRoleID = new List<int>();
                                    string[] subParameter = subValue[1].Split(',');
                                    int count = subParameter.Length;
                                    if (count > 0)
                                    {
                                        int.TryParse(subParameter[0], out AgentID);
                                        for (int i = 1; i < count; i++)
                                        {
                                            int.TryParse(subParameter[i], out RoleID);
                                            ListRoleID.Add(RoleID);
                                        }
                                    }
                                    resultUpdate = TradingServer.Facade.FacadeUpdatePermit(AgentID, ListRoleID);

                                    #region INSERT SYSTEM LOG
                                    ////INSERT SYSTEM LOG
                                    ////'2222': manager configuration has been changed
                                    //string status = "[uncomplete]";

                                    //if (resultUpdate)
                                    //    status = "[complete]";

                                    //string content = "'" + code + "': manager configuration has been changed " + status;
                                    //string comment = "[update agent]";
                                    //TradingServer.Facade.FacadeAddNewSystemLog(3, content, comment, ipAddress, code);
                                    #endregion

                                    Facade.FacadeSendNoticeManagerChangeAgent(3, AgentID);
                                    StringResult = subValue[0] + "$" + resultUpdate.ToString();
                                }
                                else
                                {
                                    StringResult = subValue[0] + "$MCM005";
                                }
                            }
                            break;
                        #endregion

                        #region Update IAgentSecurity
                        case "UpdateIAgentSecurity":
                            {
                                bool checkip = Facade.FacadeCheckIpAdmin(code, ipAddress);
                                if (checkip)
                                {
                                    if (!string.IsNullOrEmpty(subValue[1]))
                                    {
                                        bool resultUpdate = false;
                                        List<Business.IAgentSecurity> newListIAgentSecurity = new List<IAgentSecurity>();
                                        newListIAgentSecurity = this.ExtractionIAgentSecurity(subValue[1]);
                                        resultUpdate = TradingServer.Facade.FacadeUpdateIAgentSecurity(newListIAgentSecurity);
                                        StringResult = subValue[0] + "$" + resultUpdate.ToString();
                                    }
                                }
                                else
                                {
                                    StringResult = subValue[0] + "$MCM005";
                                }
                            }
                            break;
                        #endregion

                        #region Update IAgentGroup
                        case "UpdateIAgentGroup":
                            {
                                if (!string.IsNullOrEmpty(subValue[1]))
                                {
                                    bool resultUpdate = false;
                                    int AgentID = -1;
                                    int InvestorGroupID = -1;
                                    string[] subParameter = subValue[1].Split(',');
                                    int count = subParameter.Length;
                                    List<int> ListInvestorID = new List<int>();
                                    if (count > 0)
                                    {
                                        int.TryParse(subParameter[0], out AgentID);
                                        for (int i = 1; i < count; i++)
                                        {
                                            int.TryParse(subParameter[i], out InvestorGroupID);
                                            ListInvestorID.Add(InvestorGroupID);
                                        }
                                    }
                                    resultUpdate = TradingServer.Facade.FacadeUpdateIAgentGroup(AgentID, ListInvestorID);
                                    Facade.FacadeSendNoticeManagerChangeAgent(2, AgentID);
                                    StringResult = subValue[0] + "$" + resultUpdate.ToString();
                                }
                            }
                            break;
                        #endregion

                        #region Update Investor Profile
                        case "UpdateInvestorProfile":
                            {
                                //Business.InvestorProfile Result = new InvestorProfile();
                                //Result = this.ExtractInvestorProfile(subValue[1]);
                                //TradingServer.Facade.FacadeUpdateInvestorProfile(Result);
                                StringResult = "";
                            }
                            break;
                        #endregion

                        #region Update IGroupSecurityConfig
                        case "UpdateIGroupSecurityConfig":
                            {
                                bool checkip = Facade.FacadeCheckIpAdmin(code, ipAddress);
                                if (checkip)
                                {
                                    bool resultUpdate = false;
                                    List<Business.ParameterItem> Result = new List<ParameterItem>();
                                    Result = this.ExtractParameterItem(subValue[1]);

                                    int countResult = Result.Count;
                                    for (int j = 0; j < countResult; j++)
                                    {
                                        resultUpdate = Facade.FacadeUpdateIGroupSecurityConfig(Result[j]);
                                    }

                                    Business.Market.SendNotifyToClient("USTC0532434", 2, -1);

                                    StringResult = subValue[0] + "$" + resultUpdate.ToString();
                                }
                                else
                                {
                                    StringResult = subValue[0] + "$MCM005";
                                }
                            }
                            break;
                        #endregion

                        #region Update IGroupSymbolConfig
                        case "UpdateIGroupSymbolConfig":
                            {
                                bool checkip = Facade.FacadeCheckIpAdmin(code, ipAddress);
                                if (checkip)
                                {
                                    bool resultUpdate = false;
                                    List<Business.ParameterItem> Result = new List<ParameterItem>();
                                    Result = this.ExtractParameterItem(subValue[1]);

                                    int countResult = Result.Count;
                                    for (int j = 0; j < countResult; j++)
                                    {
                                        resultUpdate = Facade.FacadeUpdateIGroupSymbolConfig(Result[j]);
                                    }
                                    StringResult = subValue[0] + "$" + resultUpdate.ToString();
                                }
                                else
                                {
                                    StringResult = subValue[0] + "$MCM005";
                                }
                            }
                            break;
                        #endregion

                        #region Update Online Command(Open Trade)(LOG)
                        case "UpdateOnlineCommandByManager":
                            {
                                bool checkip = Facade.FacadeCheckIpManagerAndAdmin(code, ipAddress);
                                if (checkip)
                                {
                                    bool checkRule = Facade.FacadeCheckPermitCommandManagerAndAdmin(code);
                                    if (checkRule)
                                    {
                                        string[] subParameter = subValue[1].Split(',');
                                        if (subParameter.Length == 13)
                                        {
                                            #region MapValue
                                            int InvestorID = -1;
                                            int CommandID = -1;
                                            double Commission = -1;
                                            DateTime ExpTime;
                                            double OpenPrice = -1;
                                            DateTime OpenTime;
                                            double StopLoss = -1;
                                            double Swap = -1;
                                            double TakeProfit = -1;
                                            double taxes = 0;
                                            double agentCommission = 0;

                                            int.TryParse(subParameter[0], out InvestorID);
                                            int.TryParse(subParameter[1], out CommandID);
                                            double.TryParse(subParameter[2], out Commission);

                                            DateTime.TryParse(subParameter[3], out ExpTime);
                                            double.TryParse(subParameter[4], out OpenPrice);
                                            DateTime.TryParse(subParameter[5], out OpenTime);
                                            double.TryParse(subParameter[6], out StopLoss);
                                            double.TryParse(subParameter[7], out Swap);
                                            double.TryParse(subParameter[8], out TakeProfit);
                                            double.TryParse(subParameter[10], out taxes);
                                            //comment
                                            //double.TryParse(subParameter[12], out agentCommission);

                                            Business.OpenTrade tempOpenTrade = new OpenTrade();
                                            tempOpenTrade = TradingServer.Facade.FacadeFindOpenTradeInCommandEx(CommandID);

                                            string strOpenPriceBefore = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(tempOpenTrade.OpenPrice.ToString(), tempOpenTrade.Symbol.Digit);
                                            string strTakeProfitBefore = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(tempOpenTrade.TakeProfit.ToString(), 2);
                                            string strStopLossBefore = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(tempOpenTrade.StopLoss.ToString(), 2);
                                            string strCommissionBefore = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(tempOpenTrade.Commission.ToString(), 2);
                                            string strAgentCommissionBefore = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(tempOpenTrade.AgentCommission.ToString(), 2);
                                            string strSwapBefore = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(tempOpenTrade.Swap.ToString(), 2);
                                            #endregion
                                            bool checkGroup = Facade.FacadeCheckPermitAccessGroupManagerAndAdmin(code, tempOpenTrade.Investor.InvestorGroupInstance.InvestorGroupID);
                                            if (checkGroup)
                                            {
                                                bool ResultUpdate = false;
                                                ResultUpdate = TradingServer.Facade.FacadeUpdateOnlineCommand(InvestorID, CommandID, Commission, ExpTime, OpenPrice, OpenTime,
                                                    StopLoss, Swap, TakeProfit, subParameter[9], taxes, subParameter[11], agentCommission, tempOpenTrade.Size);
                                                if (ResultUpdate)
                                                    StringResult = subValue[0] + "$" + ResultUpdate + "," + "MCM001";
                                                else StringResult = subValue[0] + "$" + ResultUpdate + "," + "MCM004";

                                                Business.Investor tempInvestor = new Investor();
                                                tempInvestor = TradingServer.Facade.FacadeGetInvestorByInvestorID(InvestorID);

                                                #region INSERT SYSTEM LOG
                                                //'2222': account '9789300', modify order #11346382 sell 1.00 XAUUSD at 1504.85 sl: 1506.55 tp: 1503.85
                                                //INSERT SYSTEM LOG
                                                if (ResultUpdate)
                                                {
                                                    string comment = "[update order]";
                                                    string mode = TradingServer.Model.TradingCalculate.Instance.ConvertTypeIDToString(tempOpenTrade.Type.ID);
                                                    string size = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(tempOpenTrade.Size.ToString(), 2);
                                                    string openPrice = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(OpenPrice.ToString(), tempOpenTrade.Symbol.Digit);
                                                    string stopLoss = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(StopLoss.ToString(), tempOpenTrade.Symbol.Digit);
                                                    string takeProfit = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(TakeProfit.ToString(), tempOpenTrade.Symbol.Digit);
                                                    string strCommissionAfter = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Commission.ToString(), 2);
                                                    string strAgentCommissionAfter = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(agentCommission.ToString(), 2);
                                                    string strSwapAfter = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Swap.ToString(), 2);

                                                    //string content = "'" + code + "': account '" + tempInvestor.Code + "' modify order #" + tempOpenTrade.CommandCode +
                                                    //    " " + mode + " " + size + " " + tempOpenTrade.Symbol.Name + " at " + openPrice + " sl: " + stopLoss + " tp: " + takeProfit;

                                                    string content = "'" + code + "': open order #" + tempOpenTrade.CommandCode + " for '" + tempInvestor.Code + "' modified - " +
                                                        mode + " " + size + " " + tempOpenTrade.Symbol.Name + " at " + strOpenPriceBefore + " - tp: " + strTakeProfitBefore + " - sl: " + strStopLossBefore +
                                                            " - cm: " + strCommissionBefore + " - sw: " + strSwapBefore + " -> " +
                                                            mode + " " + size + " " + tempOpenTrade.Symbol.Name + " at " + openPrice + " - tp: " + takeProfit + " - sl: " + stopLoss + " - cm: " +
                                                            strCommissionAfter + " - sw: " + strSwapAfter;

                                                    TradingServer.Facade.FacadeAddNewSystemLog(5, content, comment, ipAddress, code);
                                                }
                                                #endregion
                                            }
                                            else
                                            {
                                                StringResult = subValue[0] + "$" + false + "," + "MCM006";
                                                string content = "'" + code + "': update order failed(not enough rights)";
                                                string comment = "[update order]";
                                                TradingServer.Facade.FacadeAddNewSystemLog(5, content, comment, ipAddress, code);
                                            }
                                        }
                                        else
                                        {
                                            StringResult = subValue[0] + "$" + false + "," + "MCM004";
                                        }
                                    }
                                    else
                                    {
                                        StringResult = subValue[0] + "$" + false + "," + "MCM006";
                                        string content = "'" + code + "': update order failed(not enough rights)";
                                        string comment = "[update order]";
                                        TradingServer.Facade.FacadeAddNewSystemLog(5, content, comment, ipAddress, code);
                                    }
                                }
                                else
                                {
                                    StringResult = subValue[0] + "$" + false + "," + "MCM005";
                                    string content = "'" + code + "': update order failed(invalid ip)";
                                    string comment = "[update order]";
                                    TradingServer.Facade.FacadeAddNewSystemLog(5, content, comment, ipAddress, code);
                                }
                            }
                            break;
                        #endregion

                        #region Update Candle Online
                        case "UpdateCandleOnline":
                            {
                                string[] subParameter = subValue[1].Split(',');
                                if (subParameter.Length > 0)
                                {
                                    int ID = -1;
                                    DateTime Time;
                                    int Volume = -1;
                                    double Open = -1;
                                    double Close = -1;
                                    double High = -1;
                                    double Low = -1;
                                    double openAsk = -1;
                                    double highAsk = -1;
                                    double lowAsk = -1;
                                    double closeAsk = -1;
                                    int TimeFrame = -1;

                                    int.TryParse(subParameter[0], out ID);
                                    DateTime.TryParse(subParameter[1], out Time);
                                    int.TryParse(subParameter[2], out Volume);
                                    double.TryParse(subParameter[3], out Open);
                                    double.TryParse(subParameter[4], out Close);
                                    double.TryParse(subParameter[5], out High);
                                    double.TryParse(subParameter[6], out Low);
                                    int.TryParse(subParameter[7], out TimeFrame);
                                    double.TryParse(subParameter[8], out openAsk);
                                    double.TryParse(subParameter[9], out highAsk);
                                    double.TryParse(subParameter[10], out lowAsk);
                                    double.TryParse(subParameter[11], out closeAsk);

                                    ProcessQuoteLibrary.Business.Candles objCandles = new ProcessQuoteLibrary.Business.Candles();
                                    objCandles.Close = Close;
                                    objCandles.CloseAsk = closeAsk;
                                    objCandles.High = High;
                                    objCandles.HighAsk = highAsk;
                                    objCandles.ID = ID;
                                    objCandles.Low = Low;
                                    objCandles.LowAsk = lowAsk;
                                    objCandles.Open = Open;
                                    objCandles.OpenAsk = openAsk;
                                    objCandles.Time = Time;
                                    objCandles.TimeFrame = TimeFrame;
                                    objCandles.Volume = Volume;

                                    bool ResultUpdate = false;
                                    ResultUpdate = ProcessQuoteLibrary.Business.QuoteProcess.UpdateCandleOnline(objCandles);

                                    //#region INSERT SYSTEM LOG
                                    ////INSERT SYSTEM LOG
                                    ////'2222': XAUUSD M1 1 bars updated
                                    //string status = "[uncomplete]";

                                    //if (ResultUpdate)
                                    //    status = "[complete]";

                                    //string content = "'" + code + "': " +  + status;
                                    //string comment = "[update agent]";
                                    //TradingServer.Facade.FacadeAddNewSystemLog(3, content, comment, ipAddress, code);
                                    //#endregion

                                    StringResult = subValue[0] + "$" + ResultUpdate;
                                }
                            }
                            break;
                        #endregion

                        #region UPDATE ORDER IN DATABASE(COMMENT)
                        case "UpdateOrderData":
                            {
                                string[] subParameter = subValue[1].Split(',');
                                bool Result = false;
                                Business.OpenTrade newOpenTrade = new OpenTrade();
                                if (subParameter.Length > 0)
                                {
                                    switch (subParameter[subParameter.Length - 1])
                                    {
                                        case "OPT001":
                                            {
                                                double closePrice = 0;
                                                double.TryParse(subParameter[0], out closePrice);
                                                bool resultUpdate = false;

                                                if (closePrice > 0)
                                                {
                                                    newOpenTrade = this.ConvertStringToOpenTrade(subParameter);
                                                    //resultUpdate = TradingServer.Facade.FacadeUpdateOnlineCommand(newOpenTrade.Investor.InvestorID, newOpenTrade.ID, newOpenTrade.Commission,
                                                    //    newOpenTrade.ExpTime, newOpenTrade.OpenPrice, newOpenTrade.OpenTime, newOpenTrade.StopLoss, newOpenTrade.Swap,
                                                    //    newOpenTrade.TakeProfit, newOpenTrade.Symbol.Name);
                                                }
                                                else
                                                {
                                                    newOpenTrade = this.ConvertStringToOpenTrade(subParameter);
                                                    if (newOpenTrade.Type.ID == 1 || newOpenTrade.Type.ID == 5 || newOpenTrade.Type.ID == 7 || newOpenTrade.Type.ID == 9 || newOpenTrade.Type.ID == 11)
                                                    {
                                                        newOpenTrade.ClosePrice = newOpenTrade.Symbol.TickValue.Bid;
                                                    }
                                                    else
                                                    {
                                                        newOpenTrade.ClosePrice = (Business.Symbol.ConvertNumberPip(newOpenTrade.Symbol.Digit, newOpenTrade.SpreaDifferenceInOpenTrade) + newOpenTrade.Symbol.TickValue.Ask);
                                                    }

                                                    //resultUpdate = TradingServer.Facade.FacadeUpdateOnlineCommand(newOpenTrade.Investor.InvestorID, newOpenTrade.ID, newOpenTrade.Commission,
                                                    //    newOpenTrade.ExpTime, newOpenTrade.OpenPrice, newOpenTrade.OpenTime, newOpenTrade.StopLoss, newOpenTrade.Swap,
                                                    //    newOpenTrade.TakeProfit, newOpenTrade.Symbol.Name);
                                                }

                                                if (resultUpdate)
                                                {
                                                    //MAP COMMAND UPDATE TO CLIENT
                                                    string Message = "UpdateCommand$True,UPDATE COMMAND COMPLETE," + newOpenTrade.ID + "," +
                                                                newOpenTrade.Investor.InvestorID + "," + newOpenTrade.Symbol.Name + "," +
                                                                newOpenTrade.Size + "," + false + "," + 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 + ",Update";
                                                }

                                                StringResult = subValue[0] + "$" + resultUpdate;
                                            }
                                            break;
                                        case "CMH01":
                                            {
                                                newOpenTrade = ConvertStringToOpenTrade(subParameter);
                                                bool resultUpdate = TradingServer.Facade.FacadeUpdateCommandHistory(newOpenTrade);

                                                StringResult = subValue[0] + "$" + resultUpdate;
                                            }
                                            break;
                                        case "IAL01":
                                            {
                                                Business.InvestorAccountLog newInvestorAccountLog = new InvestorAccountLog();
                                                int ID = 0;
                                                //deal ID
                                                int InvestorID = 0;
                                                //name
                                                DateTime Date;
                                                //comment
                                                double Amount = 0;
                                                //code
                                                int.TryParse(subParameter[0], out ID);
                                                int.TryParse(subParameter[2], out InvestorID);
                                                DateTime.TryParse(subParameter[4], out Date);
                                                double.TryParse(subParameter[6], out Amount);

                                                newInvestorAccountLog.ID = ID;
                                                newInvestorAccountLog.DealID = subParameter[1];
                                                newInvestorAccountLog.InvestorID = InvestorID;
                                                newInvestorAccountLog.Name = subParameter[3];
                                                newInvestorAccountLog.Date = Date;
                                                newInvestorAccountLog.Comment = subParameter[5];
                                                newInvestorAccountLog.Amount = Amount;
                                                newInvestorAccountLog.Code = subParameter[6];

                                                bool resultUpdate = TradingServer.Facade.FacadeUpdateInvestorAccountLog(newInvestorAccountLog);
                                                StringResult = subValue[0] + "$" + resultUpdate;
                                            }
                                            break;
                                    }
                                }
                            }
                            break;
                        #endregion

                        #region UPDATE COMMAND HISTORY(UPDATE COMMAND HISTORY, UPDATE DEPOSIT....)
                        case "UpdateCommandHistory":
                            {
                                bool checkip = Facade.FacadeCheckIpAdmin(code, ipAddress);
                                if (checkip)
                                {
                                    bool checkRule = Facade.FacadeCheckPermitCommandManagerAndAdmin(code);
                                    if (checkRule)
                                    {
                                        bool resultUpdate = false;
                                        string[] subParameter = subValue[1].Split(',');
                                        if (subParameter.Length == 18)
                                        {
                                            #region MAP STRING TO OBJECT
                                            Business.OpenTrade newOpenTrade = new OpenTrade();

                                            int commandTypeID, commandHistoryID;
                                            DateTime openTime, closeTime, expTime;
                                            double openPrice, closePrice, profit, swap, commission, size, stopLoss, takeProfit, taxes, agentCommission;

                                            int.TryParse(subParameter[0], out commandTypeID);
                                            DateTime.TryParse(subParameter[1], out openTime);
                                            double.TryParse(subParameter[2], out openPrice);
                                            DateTime.TryParse(subParameter[3], out closeTime);
                                            double.TryParse(subParameter[4], out closePrice);
                                            double.TryParse(subParameter[5], out profit);
                                            double.TryParse(subParameter[6], out swap);
                                            double.TryParse(subParameter[7], out commission);
                                            DateTime.TryParse(subParameter[8], out expTime);
                                            double.TryParse(subParameter[9], out size);
                                            double.TryParse(subParameter[10], out stopLoss);
                                            double.TryParse(subParameter[11], out takeProfit);

                                            //int.TryParse(subParameter[12], out symbolID);
                                            double.TryParse(subParameter[13], out taxes);
                                            //string comment  = subparmeter[14]
                                            double.TryParse(subParameter[15], out agentCommission);
                                            int.TryParse(subParameter[16], out commandHistoryID);
                                            //string InvestorCode == .....

                                            newOpenTrade.AgentCommission = agentCommission;
                                            newOpenTrade.ClosePrice = closePrice;
                                            newOpenTrade.CloseTime = closeTime;
                                            newOpenTrade.Comment = subParameter[14];
                                            newOpenTrade.Commission = commission;
                                            newOpenTrade.ExpTime = expTime;
                                            newOpenTrade.ID = commandHistoryID;
                                            newOpenTrade.OpenPrice = openPrice;
                                            newOpenTrade.OpenTime = openTime;
                                            newOpenTrade.Type = new TradeType();
                                            newOpenTrade.Type.ID = commandTypeID;
                                            newOpenTrade.Profit = profit;
                                            newOpenTrade.Size = size;
                                            newOpenTrade.StopLoss = stopLoss;
                                            newOpenTrade.Swap = swap;
                                            newOpenTrade.TakeProfit = takeProfit;
                                            newOpenTrade.Taxes = taxes;

                                            newOpenTrade.Investor = TradingServer.Facade.FacadeFindInvestor(subParameter[17]);
                                            #endregion

                                            if (commandTypeID == 1 || commandTypeID == 2 || commandTypeID == 11 || commandTypeID == 12)
                                            {
                                                #region FILL INSTANT SYMBOL
                                                if (Business.Market.SymbolList != null)
                                                {
                                                    int countSymbol = Business.Market.SymbolList.Count;
                                                    for (int j = 0; j < countSymbol; j++)
                                                    {
                                                        if (Business.Market.SymbolList[j].Name.Trim() == subParameter[12].Trim())
                                                        {
                                                            newOpenTrade.Symbol = Business.Market.SymbolList[j];
                                                            break;
                                                        }
                                                    }
                                                }
                                                #endregion

                                                newOpenTrade.CalculatorProfitCommand(newOpenTrade);
                                                newOpenTrade.Profit = newOpenTrade.Symbol.ConvertCurrencyToUSD(newOpenTrade.Symbol.Currency, newOpenTrade.Profit, false, newOpenTrade.SpreaDifferenceInOpenTrade, newOpenTrade.Symbol.Digit);
                                                newOpenTrade.Profit = Math.Round(newOpenTrade.Profit, 2);
                                            }

                                            //resultUpdate = TradingServer.Facade.FacadeUpdateOnlineCommand(0, commandHistoryID, commission, expTime, openPrice, openTime, stopLoss,
                                            //    swap, takeProfit, subParameter[12], taxes, subParameter[14], agentCommission, size);

                                            Business.OpenTrade tempOpenTrade = TradingServer.Facade.FacadeGetCommandHistoryByCommandID(commandHistoryID);

                                            #region BUIL STRING CHANGER OF ORDER
                                            string change = string.Empty;
                                            string mode = TradingServer.Model.TradingCalculate.Instance.ConvertTypeIDToString(tempOpenTrade.Type.ID);
                                            string strOpenPriceBefore = string.Empty;
                                            string strOpenPriceAfter = string.Empty;
                                            string strClosePriceBefore = string.Empty;
                                            string strClosePriceAfter = string.Empty;

                                            if (tempOpenTrade.Symbol != null)
                                            {
                                                strOpenPriceBefore = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(tempOpenTrade.OpenPrice.ToString(), tempOpenTrade.Symbol.Digit);
                                                strOpenPriceAfter = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(openPrice.ToString(), tempOpenTrade.Symbol.Digit);
                                                strClosePriceBefore = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(tempOpenTrade.ClosePrice.ToString(), tempOpenTrade.Symbol.Digit);
                                                strClosePriceAfter = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(closePrice.ToString(), tempOpenTrade.Symbol.Digit);
                                            }
                                            else
                                            {
                                                strOpenPriceBefore = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(tempOpenTrade.OpenPrice.ToString(), 5);
                                                strOpenPriceAfter = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(openPrice.ToString(), 5);
                                                strClosePriceBefore = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(tempOpenTrade.ClosePrice.ToString(), 5);
                                                strClosePriceAfter = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(closePrice.ToString(), 5);
                                            }

                                            string strStopLossBefore = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(tempOpenTrade.StopLoss.ToString(), 2);
                                            string strStopLossAfter = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(stopLoss.ToString(), 2);
                                            string strTakeProfitBefore = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(tempOpenTrade.TakeProfit.ToString(), 2);
                                            string strTakeProfitAfter = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(takeProfit.ToString(), 2);
                                            string strSwapBefore = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(tempOpenTrade.Swap.ToString(), 2);
                                            string strSwapAfter = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(swap.ToString(), 2);
                                            string strLotsBefore = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(tempOpenTrade.Size.ToString(), 2);
                                            string strLotsAfter = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(size.ToString(), 2);
                                            string strCommissionBefore = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(tempOpenTrade.Commission.ToString(), 2);
                                            string strCommissionAfter = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(commission.ToString(), 2);
                                            string strProfitBefore = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(tempOpenTrade.Profit.ToString(), 2);
                                            string strProfitAfter = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(newOpenTrade.Profit.ToString(), 2);
                                            string strSize = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(size.ToString(), 2);
                                            #endregion

                                            #region UPDATE COMMAND IN DATABASE AND FIX BALANCE
                                            if (Business.Market.InvestorList != null)
                                            {
                                                int count = Business.Market.InvestorList.Count;
                                                for (int i = 0; i < count; i++)
                                                {
                                                    if (Business.Market.InvestorList[i].InvestorID == newOpenTrade.Investor.InvestorID)
                                                    {
                                                        double totalProfit = newOpenTrade.Profit + newOpenTrade.Commission + newOpenTrade.Swap;
                                                        double profitHistory = tempOpenTrade.Profit + tempOpenTrade.Commission + tempOpenTrade.Swap;
                                                        double deltaProfit = profitHistory - totalProfit;

                                                        bool isValid = false;
                                                        if (deltaProfit != 0)
                                                        {
                                                            #region SWITCH COMMAND TYPE
                                                            switch (tempOpenTrade.Type.ID)
                                                            {
                                                                case 13:
                                                                    {
                                                                        double tempBalance = Business.Market.InvestorList[i].Balance - deltaProfit;
                                                                        if (tempBalance > 0 && newOpenTrade.Profit > 0)
                                                                        {
                                                                            Business.Market.InvestorList[i].Balance -= deltaProfit;
                                                                            TradingServer.Facade.FacadeUpdateBalance(Business.Market.InvestorList[i].InvestorID, Business.Market.InvestorList[i].Balance);
                                                                            isValid = true;
                                                                        }
                                                                    }
                                                                    break;

                                                                case 14:
                                                                    {
                                                                        double tempBalance = Business.Market.InvestorList[i].Balance + deltaProfit;
                                                                        if (tempBalance > 0 && newOpenTrade.Profit > 0)
                                                                        {
                                                                            Business.Market.InvestorList[i].Balance += deltaProfit;
                                                                            TradingServer.Facade.FacadeUpdateBalance(Business.Market.InvestorList[i].InvestorID, Business.Market.InvestorList[i].Balance);
                                                                            isValid = true;
                                                                        }
                                                                    }
                                                                    break;

                                                                case 15:
                                                                    {
                                                                        double tempBalance = Business.Market.InvestorList[i].Credit - deltaProfit;
                                                                        if (tempBalance > 0 && newOpenTrade.Profit > 0)
                                                                        {
                                                                            Business.Market.InvestorList[i].Credit -= deltaProfit;
                                                                            TradingServer.Facade.FacadeUpdateCredit(Business.Market.InvestorList[i].InvestorID, Business.Market.InvestorList[i].Credit);
                                                                            isValid = true;
                                                                        }
                                                                    }
                                                                    break;

                                                                case 16:
                                                                    {
                                                                        double tempBalance = Business.Market.InvestorList[i].Credit + deltaProfit;
                                                                        if (tempBalance > 0 && newOpenTrade.Profit > 0)
                                                                        {
                                                                            Business.Market.InvestorList[i].Credit += deltaProfit;
                                                                            TradingServer.Facade.FacadeUpdateCredit(Business.Market.InvestorList[i].InvestorID, Business.Market.InvestorList[i].Credit);
                                                                            isValid = true;
                                                                        }
                                                                    }
                                                                    break;

                                                                default:
                                                                    {
                                                                        Business.Market.InvestorList[i].Balance -= deltaProfit;
                                                                        TradingServer.Facade.FacadeUpdateBalance(Business.Market.InvestorList[i].InvestorID, Business.Market.InvestorList[i].Balance);
                                                                        isValid = true;
                                                                    }
                                                                    break;
                                                            }
                                                            #endregion

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

                                                        if (isValid)
                                                        {
                                                            #region CALCUALTION AGENT COMMISSION
                                                            double deltaAgentCommission = agentCommission - tempOpenTrade.AgentCommission;

                                                            if (deltaAgentCommission != 0)
                                                            {
                                                                if (Business.Market.InvestorList != null)
                                                                {
                                                                    int countInvestor = Business.Market.InvestorList.Count;
                                                                    for (int j = 0; j < countInvestor; j++)
                                                                    {
                                                                        if (Business.Market.InvestorList[j].Code == tempOpenTrade.Investor.AgentID)
                                                                        {
                                                                            Business.Market.InvestorList[j].Balance += deltaAgentCommission;
                                                                            TradingServer.Facade.FacadeUpdateBalance(Business.Market.InvestorList[j].InvestorID, Business.Market.InvestorList[j].Balance);
                                                                            //SEND NOTIFY TO MANAGER with type =3 then balance and credit
                                                                            TradingServer.Facade.FacadeSendNotifyManagerRequest(3, Business.Market.InvestorList[j]);

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

                                                                                int countInvestorOnline = Business.Market.InvestorList[j].CountInvestorOnline(Business.Market.InvestorList[j].InvestorID);
                                                                                if (countInvestorOnline > 0)
                                                                                    Business.Market.InvestorList[j].ClientCommandQueue.Add(Message);
                                                                            }
                                                                            break;
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                            #endregion

                                                            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("UCH1478963");
                                                        }

                                                        //UPDATE COMMAND HISTORY IN DATABASE
                                                        resultUpdate = TradingServer.Facade.FacadeUpdateCommandHistory(newOpenTrade);

                                                        break;
                                                    }
                                                }
                                            }
                                            #endregion

                                            StringResult = subValue[0] + "$" + resultUpdate;
                                            #region LOG
                                            //'2222': the existent order has been overwritten
                                            string status = "[Failed]";
                                            if (resultUpdate)
                                                status = "[Success]";

                                            if (tempOpenTrade.Type.ID == 1 || tempOpenTrade.Type.ID == 2 || tempOpenTrade.Type.ID == 11 || tempOpenTrade.Type.ID == 12)
                                            {
                                                string content = "'" + code + "': close order #" + tempOpenTrade.CommandCode + " for " + tempOpenTrade.Investor.Code +
                                                                " modified: " + mode + " " + strSize + " " + tempOpenTrade.Symbol.Name + " at " + strOpenPriceBefore + " - cp: " +
                                                                strClosePriceBefore + " - tp: " + strTakeProfitBefore + " - sl: " + strStopLossBefore + " - cm: " + strCommissionBefore +
                                                                " - sw: " + strSwapBefore + " - profit: " + strProfitBefore + " -> " + mode + " " + strSize + " " + tempOpenTrade.Symbol.Name +
                                                                " at " + strOpenPriceAfter + " - cp: " + strClosePriceAfter + " - tp: " + strTakeProfitAfter + " - sl: " + strStopLossAfter +
                                                                " - cm: " + strCommissionAfter + " - sw: " + strSwapAfter + " - profit: " + strProfitAfter + status;
                                                string comment = "[update order]";
                                                TradingServer.Facade.FacadeAddNewSystemLog(5, content, comment, ipAddress, code);
                                            }
                                            else
                                            {
                                                switch (tempOpenTrade.Type.ID)
                                                {
                                                    case 13:
                                                        {
                                                            string content = "'" + code + "': close order #" + tempOpenTrade.CommandCode + " for " + tempOpenTrade.Investor.Code +
                                                                " modified - deposit " + strSize + " at " + strOpenPriceBefore + " - cp: " + strClosePriceBefore +
                                                                " - profit: " + strProfitBefore + " -> deposit " + strSize + " at " + strOpenPriceAfter + " - profit: " +
                                                                strProfitAfter + status;
                                                            string comment = "[update order]";
                                                            TradingServer.Facade.FacadeAddNewSystemLog(5, content, comment, ipAddress, code);
                                                        }
                                                        break;
                                                    case 14:
                                                        {
                                                            string content = "'" + code + "': close order #" + tempOpenTrade.CommandCode + " for " + tempOpenTrade.Investor.Code +
                                                                " modified - withdrawal " + strSize + " at " + strOpenPriceBefore + " - cp: " + strClosePriceBefore +
                                                                " - profit: " + strProfitBefore + " -> withdrawal " + strSize + " at " + strOpenPriceAfter + " - profit: " +
                                                                strProfitAfter + status;
                                                            string comment = "[update order]";
                                                            TradingServer.Facade.FacadeAddNewSystemLog(5, content, comment, ipAddress, code);
                                                        }
                                                        break;
                                                    case 15:
                                                        {
                                                            string content = "'" + code + "': close order #" + tempOpenTrade.CommandCode + " for " + tempOpenTrade.Investor.Code +
                                                                " modified - credit in " + strSize + " at " + strOpenPriceBefore + " - cp: " + strClosePriceBefore +
                                                                " - profit: " + strProfitBefore + " -> credit in " + strSize + " at " + strOpenPriceAfter + " - profit: " +
                                                                strProfitAfter + status;
                                                            string comment = "[update order]";
                                                            TradingServer.Facade.FacadeAddNewSystemLog(5, content, comment, ipAddress, code);
                                                        }
                                                        break;
                                                    case 16:
                                                        {
                                                            string content = "'" + code + "': close order #" + tempOpenTrade.CommandCode + " for " + tempOpenTrade.Investor.Code +
                                                                " modified - credit out " + strSize + " at " + strOpenPriceBefore + " - cp: " + strClosePriceBefore +
                                                                " - profit: " + strProfitBefore + " -> credit out " + strSize + " at " + strOpenPriceAfter + " - profit: " +
                                                                strProfitAfter + status;
                                                            string comment = "[update order]";
                                                            TradingServer.Facade.FacadeAddNewSystemLog(5, content, comment, ipAddress, code);
                                                        }
                                                        break;
                                                }
                                            }
                                            #endregion
                                        }
                                    }
                                    else
                                    {
                                        StringResult = subValue[0] + "$MCM006";
                                        string content = "'" + code + "': update order failed(not enough rights)";
                                        string comment = "[update order]";
                                        TradingServer.Facade.FacadeAddNewSystemLog(5, content, comment, ipAddress, code);
                                    }
                                }
                                else
                                {
                                    StringResult = subValue[0] + "$MCM005";
                                    string content = "'" + code + "': update order failed(invalid ip)";
                                    string comment = "[update order]";
                                    TradingServer.Facade.FacadeAddNewSystemLog(5, content, comment, ipAddress, code);
                                }
                            }
                            break;
                        #endregion

                        #region DELETE COMMAND IN HISTORY(DELETE COMMAND, DELETE DEPOSIT)
                        case "DeleteCommandHistory":
                            {
                                bool checkip = Facade.FacadeCheckIpAdmin(code, ipAddress);
                                if (checkip)
                                {
                                    bool resultDelete = false;
                                    int commandHistoryID = 0;

                                    string[] subParameter = subValue[1].Split(',');

                                    int.TryParse(subValue[1], out commandHistoryID);

                                    Business.OpenTrade newOpenTrade = TradingServer.Facade.FacadeGetCommandHistoryByCommandID(commandHistoryID);

                                    //if (newOpenTrade == null)
                                    //    return StringResult = subValue[0] + "$" + false;
                                    bool isDelete = false;
                                    if (newOpenTrade != null)
                                    {
                                        if (newOpenTrade.ID > 0)
                                        {
                                            #region DELETE COMMAND HISTORY
                                            #region FIX BALANCE ACCOUNT
                                            if (newOpenTrade.Type != null)
                                            {
                                                double swap = newOpenTrade.Swap;

                                                switch (newOpenTrade.Type.ID)
                                                {
                                                    #region SPOT COMMAND
                                                    case 1:
                                                        this.FixBalanceAccount(newOpenTrade.Profit, swap, newOpenTrade.Commission, newOpenTrade.Investor.InvestorID, newOpenTrade.AgentCommission);
                                                        break;

                                                    case 2:
                                                        this.FixBalanceAccount(newOpenTrade.Profit, swap, newOpenTrade.Commission, newOpenTrade.Investor.InvestorID, newOpenTrade.AgentCommission);
                                                        break;
                                                    #endregion

                                                    #region FUTURE COMMAND
                                                    case 11:
                                                        this.FixBalanceAccount(newOpenTrade.Profit, swap, newOpenTrade.Commission, newOpenTrade.Investor.InvestorID, newOpenTrade.AgentCommission);
                                                        break;

                                                    case 12:
                                                        this.FixBalanceAccount(newOpenTrade.Profit, swap, newOpenTrade.Commission, newOpenTrade.Investor.InvestorID, newOpenTrade.AgentCommission);
                                                        break;
                                                    #endregion

                                                    #region DEPOSIT
                                                    case 13:
                                                        {
                                                            if (Business.Market.InvestorList != null)
                                                            {
                                                                int count = Business.Market.InvestorList.Count;
                                                                for (int i = 0; i < count; i++)
                                                                {
                                                                    if (Business.Market.InvestorList[i].InvestorID == newOpenTrade.Investor.InvestorID)
                                                                    {
                                                                        Business.Market.InvestorList[i].Balance -= newOpenTrade.Profit;

                                                                        TradingServer.Facade.FacadeUpdateBalance(Business.Market.InvestorList[i].InvestorID, Business.Market.InvestorList[i].Balance);

                                                                        break;
                                                                    }
                                                                }
                                                            }
                                                        }
                                                        break;
                                                    #endregion

                                                    #region WITHDRAWL
                                                    case 14:
                                                        {
                                                            if (Business.Market.InvestorList != null)
                                                            {
                                                                int count = Business.Market.InvestorList.Count;
                                                                for (int i = 0; i < count; i++)
                                                                {
                                                                    if (Business.Market.InvestorList[i].InvestorID == newOpenTrade.Investor.InvestorID)
                                                                    {
                                                                        Business.Market.InvestorList[i].Balance += newOpenTrade.Profit;

                                                                        TradingServer.Facade.FacadeUpdateBalance(Business.Market.InvestorList[i].InvestorID, Business.Market.InvestorList[i].Balance);

                                                                        break;
                                                                    }
                                                                }
                                                            }
                                                        }
                                                        break;
                                                    #endregion

                                                    #region CREDIT IN
                                                    case 15:
                                                        {
                                                            if (Business.Market.InvestorList != null)
                                                            {
                                                                int count = Business.Market.InvestorList.Count;
                                                                for (int i = 0; i < count; i++)
                                                                {
                                                                    if (Business.Market.InvestorList[i].InvestorID == newOpenTrade.Investor.InvestorID)
                                                                    {
                                                                        Business.Market.InvestorList[i].Credit -= newOpenTrade.Profit;

                                                                        TradingServer.Facade.FacadeUpdateBalance(Business.Market.InvestorList[i].InvestorID, Business.Market.InvestorList[i].Balance);

                                                                        break;
                                                                    }
                                                                }
                                                            }
                                                        }
                                                        break;
                                                    #endregion

                                                    #region CREDIT OUT
                                                    case 16:
                                                        {
                                                            if (Business.Market.InvestorList != null)
                                                            {
                                                                int count = Business.Market.InvestorList.Count;
                                                                for (int i = 0; i < count; i++)
                                                                {
                                                                    if (Business.Market.InvestorList[i].InvestorID == newOpenTrade.Investor.InvestorID)
                                                                    {
                                                                        Business.Market.InvestorList[i].Credit += newOpenTrade.Profit;

                                                                        TradingServer.Facade.FacadeUpdateBalance(Business.Market.InvestorList[i].InvestorID, Business.Market.InvestorList[i].Balance);

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

                                            //UPDATE ISDELETE IN DATABASE
                                            isDelete = TradingServer.Facade.FacadeUpdateIsDeleteHistory(true, commandHistoryID);
                                            #endregion
                                        }
                                    }
                                    else
                                    {
                                        //Delete Open Position
                                        newOpenTrade = TradingServer.Facade.FacadeFindOpenTradeInCommandEx(commandHistoryID);

                                        if (newOpenTrade.Symbol == null || newOpenTrade.Investor == null || newOpenTrade.IGroupSecurity == null || newOpenTrade.Type == null)
                                            return StringResult = subValue[0] + "$" + false;

                                        isDelete = TradingServer.Facade.FacadeDeleteOpenTradeByAdmin(commandHistoryID);
                                    }

                                    #region BUILD STRING INSERT LOG
                                    string message = string.Empty;

                                    if (newOpenTrade.Type.ID == 1 || newOpenTrade.Type.ID == 2 || newOpenTrade.Type.ID == 11 || newOpenTrade.Type.ID == 12)
                                    {
                                        #region BUILD STRING INSERT LOG
                                        //'2222': order #2495264 for '2365714' delete - buy 2.00 GBPUSD at 1.48840, profit: 20.00
                                        string mode = string.Empty;
                                        string size = string.Empty;
                                        string openPrice = string.Empty;
                                        string profit = string.Empty;
                                        string swap = string.Empty;
                                        string commission = string.Empty;
                                        size = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(newOpenTrade.Size.ToString(), 2);
                                        mode = TradingServer.Model.TradingCalculate.Instance.ConvertTypeIDToString(newOpenTrade.Type.ID);
                                        openPrice = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(newOpenTrade.OpenPrice.ToString(), newOpenTrade.Symbol.Digit);
                                        profit = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(newOpenTrade.Profit.ToString(), 2);
                                        swap = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(newOpenTrade.Swap.ToString(), 2);
                                        commission = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(newOpenTrade.Commission.ToString(), 2);
                                        message = "'" + code + "': order #" + newOpenTrade.CommandCode + " for '" + newOpenTrade.Investor.Code + "' delete - " +
                                            mode + " " + size + " " + newOpenTrade.Symbol.Name + " at " + openPrice + ", profit: " + profit + " - swap: " +
                                            swap + "- commission: " + commission;
                                        #endregion
                                    }
                                    else
                                    {
                                        #region BUILD STRING INSERT LOG
                                        //'2222': changed balance #14051372 - 100000.00 for ' 90244233' - ' Deposit'
                                        string mode = string.Empty;
                                        string profit = string.Empty;
                                        switch (newOpenTrade.Type.ID)
                                        {
                                            case 13:
                                                mode = "Deposit";
                                                break;

                                            case 14:
                                                mode = "Withdrawal";
                                                break;

                                            case 15:
                                                mode = "Credit In";
                                                break;

                                            case 16:
                                                mode = "Credit Out";
                                                break;
                                        }
                                        profit = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(newOpenTrade.Profit.ToString(), 2);
                                        message = "'" + code + "': changed balance #" + newOpenTrade.CommandCode + " - " + profit + " for '" +
                                            newOpenTrade.Investor.Code + "'" + " - '" + mode + "'";
                                        #endregion
                                    }

                                    TradingServer.Facade.FacadeAddNewSystemLog(5, message, "[delete admin order]", ipAddress, code);
                                    #endregion

                                    #region SEND COMMAND PREFESH DATA CLIENT AND MANAGER
                                    TradingServer.Facade.FacadeSendNotifyManagerRequest(3, newOpenTrade.Investor);
                                    TradingServer.Business.Market.SendNotifyToClient("IAC04332451", 3, newOpenTrade.Investor.InvestorID);
                                    #endregion

                                    #region COMMENT CODE(2-2-2012)
                                    //Business.Investor tempInvestor = new Investor();
                                    //if (Business.Market.InvestorList != null)
                                    //{
                                    //    int count = Business.Market.InvestorList.Count;
                                    //    for (int i = 0; i < count; i++)
                                    //    {
                                    //        if (Business.Market.InvestorList[i].InvestorID == newOpenTrade.Investor.InvestorID)
                                    //        {
                                    //            tempInvestor = Business.Market.InvestorList[i];

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

                                    //resultDelete = TradingServer.Facade.FacadeDeleteCommandHistory(commandHistoryID);

                                    //if (!resultDelete)
                                    //{
                                    //    if (Business.Market.CommandExecutor != null)
                                    //    {
                                    //        int countCommand = Business.Market.CommandExecutor.Count;
                                    //        for (int j = 0; j < countCommand; j++)
                                    //        {
                                    //            if (Business.Market.CommandExecutor[j].ID == commandHistoryID)
                                    //            {
                                    //                resultDelete = TradingServer.Facade.FacadeDeleteOpenTradeByManager(commandHistoryID);

                                    //                if (Business.Market.CommandExecutor[j].Investor.ClientCommandQueue == null)
                                    //                    Business.Market.CommandExecutor[j].Investor.ClientCommandQueue = new List<string>();

                                    //                Business.Market.CommandExecutor[j].Investor.ClientCommandQueue.Add("COD24680");

                                    //                break;
                                    //            }
                                    //        }
                                    //    }
                                    //}
                                    //else
                                    //{
                                    //    if (tempInvestor != null && tempInvestor.InvestorID > 0)
                                    //    {
                                    //        if (tempInvestor.ClientCommandQueue == null)
                                    //            tempInvestor.ClientCommandQueue = new List<string>();

                                    //        tempInvestor.ClientCommandQueue.Add("COD24680");
                                    //    }
                                    //}
                                    #endregion

                                    //StringResult = subValue[0] + "$" + resultDelete;
                                    StringResult = subValue[0] + "$" + isDelete;
                                }
                                else
                                {
                                    StringResult = subValue[0] + "$MCM005";
                                }
                            }
                            break;
                        #endregion

                        #region ACTIVE PENDING ORDER
                        case "ActivePendingOrder":
                            {
                                bool checkip = Facade.FacadeCheckIpManagerAndAdmin(code, ipAddress);
                                if (checkip)
                                {
                                    #region Map Value
                                    bool resultActive = false;
                                    int commandID = 0;
                                    int.TryParse(subValue[1], out commandID);
                                    Business.OpenTrade result = new OpenTrade();
                                    result = TradingServer.Facade.FacadeFindOpenTradeInCommandEx(commandID);

                                    Business.OpenTrade newOpenTrade = new OpenTrade();
                                    newOpenTrade.ClientCode = result.ClientCode;
                                    newOpenTrade.CloseTime = result.CloseTime;
                                    newOpenTrade.ExpTime = result.ExpTime;
                                    newOpenTrade.ID = result.ID;
                                    newOpenTrade.Investor = result.Investor;
                                    newOpenTrade.IsClose = result.IsClose;
                                    newOpenTrade.OpenPrice = result.OpenPrice;
                                    newOpenTrade.OpenTime = result.OpenTime;
                                    newOpenTrade.Size = result.Size;
                                    newOpenTrade.StopLoss = result.StopLoss;
                                    newOpenTrade.Swap = result.Swap;
                                    newOpenTrade.Symbol = result.Symbol;
                                    newOpenTrade.TakeProfit = result.TakeProfit;
                                    newOpenTrade.Type = result.Type;
                                    newOpenTrade.ClosePrice = result.ClosePrice;
                                    newOpenTrade.Margin = result.Margin;
                                    newOpenTrade.CommandCode = result.CommandCode;
                                    newOpenTrade.Commission = result.Commission;
                                    newOpenTrade.IGroupSecurity = result.IGroupSecurity;
                                    #endregion

                                    bool checkRule = Facade.FacadeCheckPermitCommandManagerAndAdmin(code);
                                    bool checkGroup = Facade.FacadeCheckPermitAccessGroupManagerAndAdmin(code, result.Investor.InvestorGroupInstance.InvestorGroupID);
                                    if (checkRule && checkGroup)
                                    {

                                        if (newOpenTrade != null && newOpenTrade.ID > 0)
                                        {
                                            //result.Symbol.MarketAreaRef.UpdateCommand(result);
                                            if (newOpenTrade.Type.ID == 7 || newOpenTrade.Type.ID == 9)
                                            {
                                                #region CASE BUY LIMIT AND BUY STOP
                                                if (newOpenTrade.Symbol.MarketAreaRef != null)
                                                {
                                                    if (newOpenTrade.Symbol.MarketAreaRef.Type != null)
                                                    {
                                                        int countType = newOpenTrade.Symbol.MarketAreaRef.Type.Count;
                                                        for (int m = 0; m < countType; m++)
                                                        {
                                                            if (newOpenTrade.Symbol.MarketAreaRef.Type[m].ID == 1)
                                                            {
                                                                newOpenTrade.Type = new TradeType();
                                                                newOpenTrade.Type.ID = newOpenTrade.Symbol.MarketAreaRef.Type[m].ID;
                                                                newOpenTrade.Type.Name = newOpenTrade.Symbol.MarketAreaRef.Type[m].Name;

                                                                break;
                                                            }
                                                        }
                                                    }
                                                }
                                                #endregion
                                            }
                                            else if (newOpenTrade.Type.ID == 8 || newOpenTrade.Type.ID == 10)
                                            {
                                                #region CASE SELL LIMIT AND SELL STOP
                                                if (newOpenTrade.Symbol.MarketAreaRef != null)
                                                {
                                                    if (newOpenTrade.Symbol.MarketAreaRef.Type != null)
                                                    {
                                                        int countType = newOpenTrade.Symbol.MarketAreaRef.Type.Count;
                                                        for (int m = 0; m < countType; m++)
                                                        {
                                                            if (newOpenTrade.Symbol.MarketAreaRef.Type[m].ID == 2)
                                                            {
                                                                newOpenTrade.Type = new TradeType();
                                                                newOpenTrade.Type.ID = newOpenTrade.Symbol.MarketAreaRef.Type[m].ID;
                                                                newOpenTrade.Type.Name = newOpenTrade.Symbol.MarketAreaRef.Type[m].Name;

                                                                break;
                                                            }
                                                        }
                                                    }
                                                }
                                                #endregion
                                            }

                                            if (newOpenTrade.Type.ID == 17 || newOpenTrade.Type.ID == 19)
                                            {
                                                if (newOpenTrade.Symbol.MarketAreaRef.Type != null)
                                                {
                                                    int countType = newOpenTrade.Symbol.MarketAreaRef.Type.Count;
                                                    for (int m = 0; m < countType; m++)
                                                    {
                                                        if (newOpenTrade.Symbol.MarketAreaRef.Type[m].ID == 11)
                                                        {
                                                            newOpenTrade.Type = new TradeType();
                                                            newOpenTrade.Type.ID = newOpenTrade.Symbol.MarketAreaRef.Type[m].ID;
                                                            newOpenTrade.Type.Name = newOpenTrade.Symbol.MarketAreaRef.Type[m].Name;

                                                            break;
                                                        }
                                                    }
                                                }
                                            }
                                            else if (newOpenTrade.Type.ID == 18 || newOpenTrade.Type.ID == 20)
                                            {
                                                if (newOpenTrade.Symbol.MarketAreaRef.Type != null)
                                                {
                                                    int countType = newOpenTrade.Symbol.MarketAreaRef.Type.Count;
                                                    for (int m = 0; m < countType; m++)
                                                    {
                                                        if (newOpenTrade.Symbol.MarketAreaRef.Type[m].ID == 12)
                                                        {
                                                            newOpenTrade.Type = new TradeType();
                                                            newOpenTrade.Type.ID = newOpenTrade.Symbol.MarketAreaRef.Type[m].ID;
                                                            newOpenTrade.Type.Name = newOpenTrade.Symbol.MarketAreaRef.Type[m].Name;

                                                            break;
                                                        }
                                                    }
                                                }
                                            }

                                            newOpenTrade.IsProcess = true;
                                            newOpenTrade.Investor.UpdateCommand(newOpenTrade);
                                            resultActive = true;

                                            #region INSERT SYSTEM LOG
                                            //INSERT SYSTEM LOG
                                            //'2222': account '9789300', activate order #11349528 buy stop 2.00 EURUSD at 1.5625
                                            string comment = "[active pending order]";
                                            string size = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(result.Size.ToString(), 2);
                                            string openPrice = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(result.OpenPrice.ToString(), result.Symbol.Digit);
                                            string mode = TradingServer.Model.TradingCalculate.Instance.ConvertTypeIDToString(result.Type.ID);
                                            string content = "'" + code + "': account '" + result.Investor.Code + "' activate order #" + result.CommandCode + " " + mode + " " + size +
                                                " " + result.Symbol.Name + " at " + openPrice;

                                            TradingServer.Facade.FacadeAddNewSystemLog(5, content, comment, ipAddress, code);
                                            #endregion
                                        }
                                        if(resultActive)
                                            StringResult = subValue[0] + "$" + resultActive + ",MCM001";
                                        else StringResult = subValue[0] + "$" + resultActive + ",MCM004";
                                    }
                                    else
                                    {
                                        StringResult = subValue[0] + "$" + false + ",MCM006";
                                        string content = "'" + code + "': manager active pending order failed(not enough rights)";
                                        string comment = "[active pending order]";
                                        TradingServer.Facade.FacadeAddNewSystemLog(5, content, comment, ipAddress, code);
                                    }
                                }
                                else
                                {
                                    StringResult = subValue[0] + "$" + false + ",MCM005";
                                    string content = "'" + code + "': manager active pending order failed(invalid ip)";
                                    string comment = "[active pending order]";
                                    TradingServer.Facade.FacadeAddNewSystemLog(5, content, comment, ipAddress, code);
                                }
                            }
                            break;
                        #endregion

                        //Command Select
                        //
                        #region Select Investor Group
                        case "SelectInvestorGroup":
                            {
                                string temp = string.Empty;
                                bool checkip = Facade.FacadeCheckIpManagerAndAdmin(code, ipAddress);
                                if (checkip)
                                {
                                    List<Business.InvestorGroup> Result = new List<InvestorGroup>();
                                    //Result = TradingServer.Facade.FacadeGetAllInvestorGroup();
                                    Result = TradingServer.Business.Market.InvestorGroupList;
                                    if (Result != null)
                                    {
                                        int countInvestorGroup = Result.Count;
                                        for (int n = 0; n < countInvestorGroup; n++)
                                        {
                                            temp += Result[n].InvestorGroupID.ToString() + "," + Result[n].Name + "," + Result[n].Owner + "," + Result[n].DefautDeposite.ToString() + "|";
                                        }
                                    }
                                }
                                StringResult = subValue[0] + "$" + temp;
                            }
                            break;

                        case "SelectInvestorGroupByID":
                            {
                                string temp = string.Empty;
                                bool checkip = Facade.FacadeCheckIpManagerAndAdmin(code, ipAddress);
                                if (checkip)
                                {
                                    int InvestorGroupID = -1;
                                    int.TryParse(subValue[1], out InvestorGroupID);

                                    Business.InvestorGroup Result = new InvestorGroup();
                                    if (Business.Market.InvestorGroupList != null)
                                    {
                                        int countInvestor = Business.Market.InvestorGroupList.Count;
                                        for (int j = 0; j < countInvestor; j++)
                                        {
                                            if (Business.Market.InvestorGroupList[j].InvestorGroupID == InvestorGroupID)
                                            {
                                                Result = Business.Market.InvestorGroupList[j];
                                                break;
                                            }
                                        }
                                    }
                                    //Result = TradingServer.Facade.FacadeGetInvestorGroupByInvestorGroupID(InvestorGroupID);
                                    if (Result != null)
                                    {
                                        temp += Result.InvestorGroupID.ToString() + "," + Result.Name + "," + Result.Owner + "," + Result.DefautDeposite.ToString();
                                    }
                                }
                                StringResult = subValue[0] + "$" + temp;
                            }
                            break;
                        #endregion

                        #region Select Investor Group Config
                        case "SelectInvestorGroupConfigByInvestorGroupID":
                            {
                                string temp = string.Empty;
                                bool checkip = Facade.FacadeCheckIpManagerAndAdmin(code, ipAddress);
                                if (checkip)
                                {
                                    int InvestorGroupID = -1;
                                    int.TryParse(subValue[1], out InvestorGroupID);

                                    if (Market.InvestorGroupList != null)
                                    {
                                        int countInvestorGroup = Market.InvestorGroupList.Count;
                                        for (int n = 0; n < countInvestorGroup; n++)
                                        {
                                            if (Market.InvestorGroupList[n].InvestorGroupID == InvestorGroupID)
                                            {
                                                if (Market.InvestorGroupList[n].ParameterItems != null)
                                                {
                                                    int countParameterItem = Market.InvestorGroupList[n].ParameterItems.Count;
                                                    for (int m = 0; m < countParameterItem; m++)
                                                    {
                                                        temp += Market.InvestorGroupList[n].ParameterItems[m].ParameterItemID.ToString() + "," +
                                                                Market.InvestorGroupList[n].ParameterItems[m].SecondParameterID.ToString() + "," + "-1" + "," +
                                                                Market.InvestorGroupList[n].ParameterItems[m].Name + "," +
                                                                Market.InvestorGroupList[n].ParameterItems[m].Code + "," +
                                                                Market.InvestorGroupList[n].ParameterItems[m].BoolValue.ToString() + "," +
                                                                Market.InvestorGroupList[n].ParameterItems[m].StringValue + "," +
                                                                Market.InvestorGroupList[n].ParameterItems[m].NumValue + "," +
                                                                Market.InvestorGroupList[n].ParameterItems[m].DateValue.ToString() + "|";
                                                    }
                                                }
                                                break;
                                            }
                                        }
                                    }
                                }
                                StringResult = subValue[0] + "$" + temp;
                            }
                            break;
                        #endregion

                        #region Select IGroup Symbol
                        case "SelectIGroupSymbol":
                            {
                                bool checkip = Facade.FacadeCheckIpAdmin(code, ipAddress);
                                if (checkip)
                                {
                                    string temp = string.Empty;
                                    List<Business.IGroupSymbol> Result = new List<IGroupSymbol>();
                                    Result = Business.Market.IGroupSymbolList;
                                    if (Result != null)
                                    {
                                        int countIGroupSymbol = Result.Count;
                                        for (int n = 0; n < countIGroupSymbol; n++)
                                        {
                                            temp += Result[n].IGroupSymbolID.ToString() + "," + Result[n].SymbolID.ToString() + "," + Result[n].InvestorGroupID.ToString() + "|";
                                        }
                                    }
                                    StringResult = subValue[0] + "$" + temp;
                                }
                            }
                            break;

                        case "SelectIGroupSymbolByID":
                            {
                                string temp = string.Empty;
                                int IGroupSymbolID = -1;
                                int.TryParse(subValue[1], out IGroupSymbolID);

                                Business.IGroupSymbol Result = new IGroupSymbol();
                                if (Business.Market.IGroupSymbolList != null)
                                {
                                    int countIGroupSymbol = Business.Market.IGroupSymbolList.Count;
                                    for (int j = 0; j < countIGroupSymbol; j++)
                                    {
                                        if (Business.Market.IGroupSymbolList[j].IGroupSymbolID == IGroupSymbolID)
                                        {
                                            Result = Business.Market.IGroupSymbolList[j];
                                            break;
                                        }
                                    }
                                }

                                //Result = TradingServer.Facade.FacadeGetIGroupSymbolByIGroupSymbolID(IGroupSymbolID);
                                if (Result != null)
                                {
                                    temp = Result.IGroupSymbolID.ToString() + "," + Result.SymbolID.ToString() + "," + Result.InvestorGroupID.ToString();
                                }
                                StringResult = subValue[0] + "$" + temp;
                            }
                            break;

                        case "SelectIGroupSymbolBySymbolID":
                            {
                                string temp = string.Empty;
                                int SymbolID = -1;
                                int.TryParse(subValue[1], out SymbolID);

                                List<Business.IGroupSymbol> Result = new List<IGroupSymbol>();
                                if (Business.Market.IGroupSymbolList != null)
                                {
                                    int countIGroupSymbol = Business.Market.IGroupSymbolList.Count;
                                    for (int j = 0; j < countIGroupSymbol; j++)
                                    {
                                        if (Business.Market.IGroupSymbolList[j].SymbolID == SymbolID)
                                        {
                                            Result.Add(Business.Market.IGroupSymbolList[j]);
                                        }
                                    }
                                }

                                //Result = TradingServer.Facade.FacadeGetIGroupSymbolBySymbolID(SymbolID);
                                if (Result != null)
                                {
                                    int countIGroupSymbol = Result.Count;
                                    for (int n = 0; n < countIGroupSymbol; n++)
                                    {
                                        temp += Result[n].IGroupSymbolID.ToString() + "," + Result[n].SymbolID.ToString() + "," + Result[n].InvestorGroupID.ToString() + "|";
                                    }
                                }

                                StringResult = subValue[0] + "$" + temp;
                            }
                            break;
                        #endregion

                        #region Select IGroupSymbolConfig
                        case "SelectIGroupSymbolConfigByIGroupSymbolID":
                            {
                                bool checkip = Facade.FacadeCheckIpAdmin(code, ipAddress);
                                if (checkip)
                                {
                                    string temp = string.Empty;
                                    int IGroupSymbolID = -1;
                                    int.TryParse(subValue[1], out IGroupSymbolID);
                                    temp = this.GetIGroupSymbolConfigByIGroupSymbolIDInIGroupSymbolList(IGroupSymbolID);
                                    StringResult = subValue[0] + "$" + temp;
                                }
                            }
                            break;
                        #endregion

                        #region Select IGroup Security
                        case "SelectIGroupSecurity":
                            {
                                string temp = string.Empty;
                                bool checkip = Facade.FacadeCheckIpManagerAndAdmin(code, ipAddress);
                                if (checkip)
                                {
                                    List<Business.IGroupSecurity> Result = new List<IGroupSecurity>();
                                    Result = Business.Market.IGroupSecurityList;
                                    if (Result != null)
                                    {
                                        int countIGroupSecurity = Result.Count;
                                        for (int n = 0; n < countIGroupSecurity; n++)
                                        {
                                            temp += Result[n].IGroupSecurityID.ToString() + "," + Result[n].InvestorGroupID.ToString() + "," +
                                                        Result[n].SecurityID.ToString() + "|";
                                        }
                                    }
                                }
                                StringResult = subValue[0] + "$" + temp;
                            }
                            break;

                        case "SelectIGroupSecurityByID":
                            {
                                string temp = string.Empty;
                                int IGroupSecurityID = -1;
                                int.TryParse(subValue[1], out IGroupSecurityID);

                                Business.IGroupSecurity Result = new IGroupSecurity();
                                if (Business.Market.IGroupSecurityList != null)
                                {
                                    int countIGroupSecurity = Business.Market.IGroupSecurityList.Count;
                                    for (int j = 0; j < countIGroupSecurity; j++)
                                    {
                                        if (Business.Market.IGroupSecurityList[j].IGroupSecurityID == IGroupSecurityID)
                                        {
                                            Result = Business.Market.IGroupSecurityList[j];
                                            break;
                                        }
                                    }
                                }

                                if (Result != null)
                                {
                                    temp += Result.IGroupSecurityID.ToString() + "," + Result.InvestorGroupID.ToString() + "," +
                                                    Result.SecurityID.ToString();
                                }

                                StringResult = subValue[0] + "$" + temp;
                            }
                            break;

                        case "SelectIGroupSecurityBySecurityID":
                            {
                                string temp = string.Empty;
                                int SecurityID = -1;
                                int.TryParse(subValue[1], out SecurityID);

                                List<Business.IGroupSecurity> Result = new List<IGroupSecurity>();
                                if (Business.Market.IGroupSecurityList != null)
                                {
                                    int countIGroupSecurity = Business.Market.IGroupSecurityList.Count;
                                    for (int j = 0; j < countIGroupSecurity; j++)
                                    {
                                        if (Business.Market.IGroupSecurityList[j].SecurityID == SecurityID)
                                        {
                                            Result.Add(Business.Market.IGroupSecurityList[j]);
                                        }
                                    }
                                }
                                //Result = TradingServer.Facade.FacadeGetIGroupSecurityBySecurityID(SecurityID);
                                if (Result != null)
                                {
                                    int countIGroupSecurity = Result.Count;
                                    for (int n = 0; n < countIGroupSecurity; n++)
                                    {
                                        temp += Result[n].IGroupSecurityID.ToString() + "," + Result[n].InvestorGroupID.ToString() + "," +
                                                    Result[n].SecurityID.ToString() + "|";
                                    }
                                }

                                StringResult = subValue[0] + "$" + temp;
                            }
                            break;

                        case "SelectIGroupSecurityByInvestorGroupID":
                            {
                                string temp = string.Empty;
                                int InvestorGroupID = -1;
                                int.TryParse(subValue[1], out InvestorGroupID);

                                List<Business.IGroupSecurity> Result = new List<IGroupSecurity>();
                                if (Business.Market.IGroupSecurityList != null)
                                {
                                    int countIGroupSecurity = Business.Market.IGroupSecurityList.Count;
                                    for (int j = 0; j < countIGroupSecurity; j++)
                                    {
                                        if (Business.Market.IGroupSecurityList[j].InvestorGroupID == InvestorGroupID)
                                        {
                                            Result.Add(Business.Market.IGroupSecurityList[j]);
                                        }
                                    }
                                }
                                //Result = TradingServer.Facade.FacadeGetIGroupSecurityByInvestorGroupID(InvestorGroupID);
                                if (Result != null)
                                {
                                    int countIGroupSecurity = Result.Count;
                                    for (int n = 0; n < countIGroupSecurity; n++)
                                    {
                                        temp += Result[n].IGroupSecurityID.ToString() + "," + Result[n].InvestorGroupID.ToString() + "," +
                                                    Result[n].SecurityID.ToString() + "|";
                                    }
                                }

                                StringResult = subValue[0] + "$" + temp;
                            }
                            break;
                        #endregion

                        #region Select IGroupSecurityConfig
                        case "SelectIGroupSecurityConfigByIGroupSecurityID":
                            {
                                string temp = string.Empty;
                                int IGroupSecurityID = -1;
                                int.TryParse(subValue[1], out IGroupSecurityID);
                                temp = this.GetIGroupSecurityConfigByIGroupSecurityIDInIGroupSecurityList(IGroupSecurityID);
                                StringResult = subValue[0] + "$" + temp;
                            }
                            break;
                        #endregion

                        #region Select Security
                        case "SelectSecurity":
                            {
                                string temp = string.Empty;
                                temp = this.SelectSecurityInSecurityList();

                                StringResult = subValue[0] + "$" + temp;
                            }
                            break;

                        case "SelectSecurityByID":
                            {
                                string temp = string.Empty;
                                int SecurityID = -1;
                                int.TryParse(subValue[1], out SecurityID);
                                temp = this.SelectSecurityByIDInSecurityList(SecurityID);
                                StringResult = subValue[0] + "$" + temp;
                            }
                            break;
                        #endregion

                        #region Select Symbol
                        case "SelectSymbol":
                            {
                                //string commandName = "GetAllSymbol$";
                                //string temp = string.Empty;
                                //temp = this.ExtractSelectSymbol();
                                //StringResult = commandName + temp;
                            }
                            break;

                        case "SelectSymbolByID":
                            {
                                string temp = string.Empty;
                                bool checkip = Facade.FacadeCheckIpManagerAndAdmin(code, ipAddress);
                                if (checkip)
                                {
                                    int SymbolID = -1;
                                    int.TryParse(subValue[1], out SymbolID);
                                    temp = this.SelectSymbolByIDInSymbolList(SymbolID);
                                }
                                StringResult = subValue[0] + "$" + temp;
                            }
                            break;
                        #endregion

                        #region Select TradingConfig(SymbolConfig)
                        case "SelectTradingConfigBySymbolID":
                            {
                                string temp = string.Empty;
                                bool checkip = Facade.FacadeCheckIpManagerAndAdmin(code, ipAddress);
                                if (checkip)
                                {
                                    int SymbolID = -1;
                                    int.TryParse(subValue[1], out SymbolID);
                                    temp = this.SelectTradingConfigBySymbolIDInSymbolList(SymbolID);
                                }
                                StringResult = subValue[0] + "$" + temp;
                            }
                            break;
                        #endregion

                        #region Select Security Config
                        case "SelectSecurityConfigBySecurityID":
                            {
                                string temp = string.Empty;
                                int SecurityID = -1;
                                int.TryParse(subValue[1], out SecurityID);
                                temp = this.SelectSecurityConfigBySecurityIDInSecurityList(SecurityID);
                                StringResult = subValue[0] + "$" + temp;
                            }
                            break;
                        #endregion

                        #region Select Investor(LOG COMMENT)
                        case "SelectInvestor":
                            {
                                string temp = string.Empty;
                                List<Business.Investor> Result = new List<Investor>();
                                Result = TradingServer.Facade.FacadeGetAllInvestor();
                                Result = Business.Market.InvestorList;

                                if (Result != null)
                                {
                                    int countInvestor = Result.Count;
                                    for (int n = 0; n < countInvestor; n++)
                                    {
                                        temp += Result[n].InvestorID.ToString() + "," + Result[n].InvestorStatusID.ToString() + "," +
                                                Result[n].InvestorGroupInstance.InvestorGroupID + "," + Result[n].AgentID.ToString() + "," +
                                                Result[n].Balance.ToString() + "," + Result[n].Credit.ToString() + "," +
                                                Result[n].Code + "," + Result[n].IsDisable.ToString() + "," +
                                                Result[n].TaxRate.ToString() + "," + Result[n].Leverage.ToString() + "," +
                                                Result[n].ToString() + "," + Result[n].Address + "," + Result[n].Phone + "," +
                                                Result[n].City + "," + Result[n].Country + "," + Result[n].Email + "," + Result[n].ZipCode + "," +
                                                Result[n].RegisterDay.ToString() + "," + Result[n].InvestorComment + "," + Result[n].State + "," + Result[n].NickName + "|";
                                    }

                                    #region INSERT SYSTEM LOG
                                    //INSERT SYSTEM LOG
                                    //'2222': 22450 accounts have been requested
                                    //string content = "'" + code + "': " + countInvestor + " accounts have been requested";
                                    //string comment = "[account request]";
                                    //TradingServer.Facade.FacadeAddNewSystemLog(3, content, comment, ipAddress, code);
                                    #endregion
                                }

                                StringResult = subValue[0] + "$" + temp;
                            }
                            break;

                        case "LoginServer":
                            {
                                string status = "[Failed]";
                                string temp = string.Empty;
                                string[] subParameter = subValue[1].Split(',');
                                if (subParameter.Length == 4)
                                {
                                    Business.Investor Result = new Investor();
                                    int InvestorIndex = -1;
                                    int.TryParse(subParameter[2], out InvestorIndex);

                                    Result = TradingServer.Facade.FacadeLoginServer(subParameter[0], subParameter[1], InvestorIndex, subParameter[3]);

                                    if (Result != null)
                                    {
                                        temp += Result.InvestorID.ToString() + "," + Result.InvestorStatusID.ToString() + "," +
                                                    Result.InvestorGroupInstance.InvestorGroupID + "," + Result.AgentID.ToString() + "," +
                                                    Result.Balance.ToString() + "," + Result.Credit.ToString() + "," +
                                                    Result.Code + "," + Result.IsDisable.ToString() + "," +
                                                    Result.TaxRate.ToString() + "," + Result.Leverage.ToString() + "," +
                                                    Result.InvestorProfileID.ToString() + "," + Result.Address + "," + Result.Phone + "," +
                                                    Result.City + "," + Result.Country + "," + Result.Email + "," + Result.ZipCode + "," +
                                                    Result.RegisterDay.ToString() + "," + Result.InvestorComment + "," + Result.State + "," + Result.NickName;

                                        status = "[Success]";
                                    }

                                    #region INSERT SYSTEM LOG
                                    //INSERT SYSTEM LOG
                                    //'2222': login admin [success]
                                    string content = "'" + code + "': login admin " + status;
                                    string comment = "[login admin]";
                                    TradingServer.Facade.FacadeAddNewSystemLog(4, content, comment, ipAddress, code);
                                    #endregion

                                    StringResult = subValue[0] + "$" + temp;
                                }
                            }
                            break;

                        case "SelectInvestorByID":
                            {
                                string temp = string.Empty;
                                int InvestorID = -1;
                                int.TryParse(subValue[1], out InvestorID);
                                Business.Investor Result = new Investor();

                                if (Business.Market.InvestorList != null)
                                {
                                    int countInvestor = Business.Market.InvestorList.Count;
                                    for (int j = 0; j < countInvestor; j++)
                                    {
                                        if (Business.Market.InvestorList[j].InvestorID == InvestorID)
                                        {
                                            Result = Business.Market.InvestorList[j];
                                            break;
                                        }
                                    }
                                }

                                if (Result != null)
                                {
                                    temp = Result.InvestorID + "," + Result.InvestorStatusID + "," + Result.InvestorGroupInstance.InvestorGroupID + "," +
                                                Result.AgentID + "," + Math.Round(Result.Balance, 2) + "," + Result.Credit + "," + Result.Code + "," +
                                                Result.IsDisable + "," + Result.TaxRate + "," + Result.Leverage + "," +
                                                Result.InvestorProfileID + "," + Result.Address + "," + Result.Phone + "," +
                                                Result.City + "," + Result.Country + "," + Result.Email + "," + Result.ZipCode + "," +
                                                Result.RegisterDay + "," + Result.Comment + "," + Result.State + "," + Result.NickName + "," +
                                                Result.AllowChangePwd + "," + Result.ReadOnly + "," + Result.SendReport + "," + Result.IsOnline;
                                }

                                #region INSERT SYSTEM LOG
                                //INSERT SYSTEM LOG
                                //'2222': 22450 accounts have been requested
                                int num = 0;
                                if (!string.IsNullOrEmpty(temp))
                                    num = 1;

                                string content = "'" + code + "': " + num + " accounts have been requested";
                                string comment = "[account request]";
                                TradingServer.Facade.FacadeAddNewSystemLog(3, content, comment, ipAddress, code);
                                #endregion

                                if (!string.IsNullOrEmpty(temp))
                                {
                                    StringResult = subValue[0] + "$" + temp;
                                }
                                else
                                {
                                    StringResult = subValue[0] + "$";
                                }
                            }
                            break;

                        case "FindInvestor":    //COMMENT LOG BECAUSE MANAGER AUTO REQUEST ACCOUNT THEN ACCOUNT CHANGE(22/07/2011)
                            {
                                string temp = string.Empty;
                                bool checkip = Facade.FacadeCheckIpManagerAndAdmin(code, ipAddress);
                                if (checkip)
                                {
                                    Business.Investor Result = new Investor();
                                    Result = TradingServer.Facade.FacadeFindInvestor(subValue[1]);

                                    if (Result != null)
                                    {
                                        temp = Result.InvestorID + "," + Result.InvestorStatusID + "," + Result.InvestorGroupInstance.InvestorGroupID + "," +
                                                    Result.AgentID + "," + Result.Balance + "," + Result.Credit + "," + Result.Code + "," +
                                                    Result.IsDisable + "," + Result.TaxRate + "," + Result.Leverage + "," +
                                                    Result.InvestorProfileID + "," + Result.Address + "," + Result.Phone + "," +
                                                    Result.City + "," + Result.Country + "," + Result.Email + "," + Result.ZipCode + "," +
                                                    Result.RegisterDay + "," + Result.InvestorComment + "," + Result.State + "," + Result.NickName + "," +
                                                    Result.AllowChangePwd + "," + Result.ReadOnly + "," + Result.SendReport + "," + Result.IsOnline + "," +
                                                    Result.Margin + "," + Result.FreezeMargin + "," + Result.PhonePwd + "," + Result.IDPassport + "," +
                                                    Result.TotalDeposit;
                                    }

                                    #region INSERT SYSTEM LOG
                                    //INSERT SYSTEM LOG
                                    //'2222': 22450 accounts have been requested
                                    //int num = 0;
                                    //if (!string.IsNullOrEmpty(temp))
                                    //    num = 1;

                                    //string content = "'" + code + "': " + num + " accounts have been requested";
                                    //string comment = "[account request]";
                                    //TradingServer.Facade.FacadeAddNewSystemLog(3, content, comment, ipAddress, code);
                                    #endregion
                                }
                                StringResult = subValue[0] + "$" + temp;
                            }
                            break;

                        case "FindInvestorWithOnlineCommand":
                            {
                                string temp = string.Empty;
                                Business.Investor Result = new Investor();
                                Result = TradingServer.Facade.FacadeFindInvestorWithOnlineCommand(subValue[1]);

                                if (Result != null)
                                {
                                    temp = Result.InvestorID + "," + Result.NickName + "," + Result.Code;
                                }

                                #region INSERT SYSTEM LOG
                                //INSERT SYSTEM LOG
                                //'2222': 22450 accounts have been requested
                                int num = 0;
                                if (!string.IsNullOrEmpty(temp))
                                    num = 1;

                                string content = "'" + code + "': " + num + " accounts have been requested";
                                string comment = "[account request]";
                                TradingServer.Facade.FacadeAddNewSystemLog(3, content, comment, ipAddress, code);
                                #endregion

                                StringResult = subValue[0] + "$" + Result;
                            }
                            break;

                        case "SelectInvestorByInvestorGroupID":
                            {
                                int InvestorGroupID = -1;
                                int.TryParse(subValue[1], out InvestorGroupID);
                            }
                            break;
                        #endregion

                        #region Select Agent
                        case "SelectAgent":
                            {
                                bool checkip = Facade.FacadeCheckIpAdmin(code, ipAddress);
                                if (checkip)
                                {
                                    string temp = string.Empty;
                                    List<Business.Agent> Result = new List<Agent>();
                                    Result = TradingServer.Facade.FacadeGetAllAgent();

                                    if (Result != null)
                                    {
                                        int countAgent = Result.Count;
                                        for (int n = 0; n < countAgent; n++)
                                        {
                                            temp += Result[n].AgentID.ToString() + "}" + Result[n].AgentGroupID.ToString() + "}" + Result[n].Name + "}" + Result[n].InvestorID.ToString()
                                                    + "}" + Result[n].Comment + "}" + Result[n].IsDisable.ToString() + "}" + Result[n].IsIpFilter.ToString() + "}" + Result[n].IpForm
                                                    + "}" + Result[n].IpTo + "}" + Result[n].Code + "}" + Result[n].GroupCondition + "|";
                                        }
                                    }

                                    StringResult = subValue[0] + "$" + temp;
                                }
                            }
                            break;
                        case "SelectAgentOnline":
                            {
                                bool checkip = Facade.FacadeCheckIpManager(code, ipAddress);
                                if (checkip)
                                {
                                    string temp = string.Empty;
                                    List<Business.Agent> Result = new List<Agent>();

                                    for (int i = Market.AgentList.Count - 1; i >= 0; i--)
                                    {
                                        if (Market.AgentList[i].IsVirtualDealer == false && Market.AgentList[i].IsOnline == true)
                                        {
                                            Result.Add(Market.AgentList[i]);
                                        }
                                    }

                                    if (Result != null)
                                    {
                                        int countAgent = Result.Count;
                                        for (int n = 0; n < countAgent; n++)
                                        {
                                            temp += Result[n].AgentID + "}" + Result[n].Code + "}" + Result[n].IsOnline + "}" + Result[n].IsBusy + "|";
                                        }
                                    }

                                    StringResult = subValue[0] + "$" + temp;
                                }
                            }
                            break;
                        case "SelectAgentByID":
                            {
                                string temp = string.Empty;
                                int AgentID = -1;
                                int.TryParse(subValue[1], out AgentID);
                                Business.Agent Result = new Agent();

                                Result = TradingServer.Facade.FacadeGetAgentByAgentID(AgentID);

                                if (Result != null)
                                {
                                    temp += Result.AgentID.ToString() + "," + Result.AgentGroupID.ToString() + "," + Result.Name
                                        + "," + Result.InvestorID.ToString() + "," + Result.Comment + "," + Result.IsDisable.ToString();
                                }

                                StringResult = subValue[0] + "$" + temp;
                            }
                            break;

                        case "LoginAdmin":
                            {
                                string status = "[Failed]";
                                string temp = string.Empty;
                                string[] subParameter = subValue[1].Split(',');
                                int count = subParameter.Length;
                                Business.Agent Result = new Agent();
                                if (count == 2)
                                {
                                    Result = TradingServer.Facade.FacadeAdminLogin(subParameter[0], subParameter[1], ipAddress);
                                    if (Result == null) temp = "-1";
                                    else
                                    {
                                        status = "[Success]";
                                        temp += Result.AgentID.ToString() + "," + Result.AgentGroupID.ToString() + "," + Result.Name + "," + Result.InvestorID.ToString() + "," + Result.Comment + "," + Result.IsDisable.ToString() + "," + ipAddress;
                                    }

                                    #region INSERT SYSTEM LOG
                                    //INSERT SYSTEM LOG
                                    //'2222': login admin [success]

                                    string content = "'" + code + "': login admin " + status;
                                    string comment = "[login admin]";
                                    TradingServer.Facade.FacadeAddNewSystemLog(4, content, comment, ipAddress, code);
                                    #endregion

                                    StringResult = subValue[0] + "$" + temp;
                                }
                            }
                            break;

                        case "AdminLogout":
                            {
                                bool Result = false;
                                bool checkip = Facade.FacadeCheckIpAdmin(code, ipAddress);
                                if (checkip)
                                {
                                    int AgentID = -1;
                                    int.TryParse(subValue[1], out AgentID);

                                    Result = TradingServer.Facade.FacadeAdminLogout(AgentID);

                                    #region INSERT SYSTEM LOG
                                    //INSERT SYSTEM LOG
                                    //'2222': login admin [success]
                                    string status = "[Failed]";
                                    if (Result)
                                        status = "[Success]";

                                    string content = "'" + code + "': logout admin " + status;
                                    string comment = "[logout admin]";
                                    TradingServer.Facade.FacadeAddNewSystemLog(4, content, comment, ipAddress, code);
                                    #endregion
                                }
                                StringResult = subValue[0] + "$" + Result.ToString();
                            }
                            break;

                        case "LoginManager":
                            {
                                string temp = string.Empty;
                                string status = "[Failed]";
                                string[] subParameter = subValue[1].Split(',');
                                int count = subParameter.Length;
                                Business.Agent Result = new Agent();
                                if (count == 3)
                                {
                                    Result = TradingServer.Facade.FacadeManagerLogin(subParameter[0], subParameter[1], subParameter[2], ipAddress);
                                    if (Result == null) temp = "-1";
                                    else
                                    {
                                        status = "[Success]";
                                        temp += Result.AgentID.ToString() + "," + Result.AgentGroupID.ToString() + "," + Result.Name + "," + Result.InvestorID.ToString() + "," + Result.Comment + "," + Result.IsDisable.ToString() + "," + ipAddress;
                                    }

                                    #region INSERT SYSTEM LOG
                                    //INSERT SYSTEM LOG
                                    //'2222': login admin [success]

                                    string content = "'" + code + "': login manager " + status;
                                    string comment = "[login manager]";
                                    TradingServer.Facade.FacadeAddNewSystemLog(4, content, comment, ipAddress, code);
                                    #endregion

                                    StringResult = subValue[0] + "$" + temp;
                                }
                            }
                            break;

                        case "ManagerLogout":
                            {
                                bool Result = false;
                                bool checkip = Facade.FacadeCheckIpManager(code, ipAddress);
                                if (checkip)
                                {
                                    int AgentID = -1;
                                    int.TryParse(subValue[1], out AgentID);

                                    Result = TradingServer.Facade.FacadeManagerLogout(AgentID);

                                    #region INSERT SYSTEM LOG
                                    //INSERT SYSTEM LOG
                                    //'2222': login admin [success]
                                    string status = "[Failed]";
                                    if (Result)
                                        status = "[Success]";

                                    string content = "'" + code + "': logout manager " + status;
                                    string comment = "[logout manager]";
                                    TradingServer.Facade.FacadeAddNewSystemLog(4, content, comment, ipAddress, code);
                                    #endregion
                                }
                                StringResult = subValue[0] + "$" + Result.ToString();
                            }
                            break;

                        case "LoginDealer"://COMMENT LOG BECAUSE MANAGER 1 MINUTE LOGIN DEALER THEN MANY LOG INSERT TO DATABASE(21/07/2011)
                            {
                                bool checkip = Facade.FacadeCheckIpManager(code, ipAddress);
                                if (checkip)
                                {
                                    string[] subParameter = subValue[1].Split(',');
                                    int count = subParameter.Length;
                                    int AgentID = -1;
                                    int.TryParse(subParameter[0], out AgentID);
                                    bool Result = false;
                                    if (count > 0)
                                    {
                                        Result = TradingServer.Facade.FacadeDealerLogin(AgentID, ipAddress);

                                        #region INSERT SYSTEM LOG
                                        //INSERT SYSTEM LOG
                                        //'2222': dealer dispatched connected
                                        string content = "'" + code + "': dealer dispatched connected ";
                                        string comment = "[login dealer]";
                                        TradingServer.Facade.FacadeAddNewSystemLog(4, content, comment, ipAddress, code);
                                        #endregion

                                        StringResult = subValue[0] + "$" + Result.ToString();
                                    }
                                }
                            }
                            break;
                        case "UpdateTimeSyncDealer":
                            {
                                int AgentID = -1;
                                string[] splits = subValue[1].Split(',');
                                if (splits.Length == 2)
                                {
                                    int.TryParse(splits[0], out AgentID);
                                    string KeyActive = splits[1];
                                    if (AgentID > 0)
                                    {
                                        for (int i = Business.Market.AgentList.Count - 1; i >= 0; i--)
                                        {
                                            if (Business.Market.AgentList[i].AgentID == AgentID)
                                            {
                                                Business.Market.AgentList[i].TimeSync = DateTime.Now;
                                                break;
                                            }
                                        }

                                    }
                                }
                                StringResult = subValue[0] + "$";
                                break;
                            }
                        case "DealerLogout": //COMMENT LOG BECAUSE MANAGER 1 MINUTE LOGIN DEALER THEN MANY LOG INSERT TO DATABASE(21/07/2011)
                            {
                                bool checkip = Facade.FacadeCheckIpManager(code, ipAddress);
                                if (checkip)
                                {
                                    int AgentID = -1;
                                    int.TryParse(subValue[1], out AgentID);
                                    bool Result = false;
                                    Result = TradingServer.Facade.FacadeDealerLogout(AgentID);

                                    #region INSERT SYSTEM LOG
                                    //INSERT SYSTEM LOG
                                    //'2222': dealer dispatched disconnected
                                    string content = "'" + code + "': dealer dispatched disconnected ";
                                    string comment = "[logout dealer]";
                                    TradingServer.Facade.FacadeAddNewSystemLog(4, content, comment, ipAddress, code);
                                    #endregion

                                    StringResult = subValue[0] + "$" + Result.ToString();
                                }
                            }
                            break;

                        case "GetRequestToDealer":
                            {
                                string temp = string.Empty;
                                int AgentID = -1;
                                int.TryParse(subValue[1], out AgentID);
                                if (AgentID > 0)
                                {
                                    List<Business.RequestDealer> Result = new List<RequestDealer>();
                                    Result = TradingServer.Facade.FacadeGetRequestToDealer(AgentID);
                                    if (Result == null) temp = "-1";
                                    else
                                    {
                                        int count = Result.Count;
                                        for (int i = 0; i < count; i++)
                                        {
                                            temp += Result[i].AgentID + "," + Result[i].FlagConfirm + "," + Result[i].MaxDev + "," + Result[i].Name + "," + Result[i].Notice
                                                + "," + Result[i].TimeAgentReceive + "," + Result[i].TimeClientRequest + "," + Result[i].Request.Investor.InvestorID
                                                + "," + Result[i].Request.Symbol.Name + "," + Result[i].Request.Size + "," + Result[i].Request.OpenTime
                                                + "," + Result[i].Request.OpenPrice + "," + Result[i].Request.StopLoss + "," + Result[i].Request.TakeProfit
                                                + "," + Result[i].Request.ClosePrice + "," + Result[i].Request.Commission + "," + Result[i].Request.Swap
                                                + "," + Result[i].Request.Profit + "," + Result[i].Request.ID + "," + Result[i].Request.ExpTime + "," + Result[i].Request.ClientCode
                                                + "," + Result[i].Request.IsHedged + "," + Result[i].Request.Type.ID + "," + Result[i].Request.Margin + "," + Result[i].Request.Investor.InvestorGroupInstance.Name
                                                + "," + Result[i].Request.Symbol.Digit + "," + Result[i].Request.Symbol.SpreadByDefault.ToString() + "," +
                                                /*Result[i].Request.Symbol.SpreadDifference*/ Result[i].Request.SpreaDifferenceInOpenTrade
                                                + "," + Result[i].Request.CommandCode + "," + Result[i].Request.IsMultiClose + "," + Result[i].Request.RefCommandID + "|";
                                        }
                                    }
                                    StringResult = subValue[0] + "$" + temp;
                                }
                            }
                            break;
                        case "GetAllRequestDealer":
                            {
                                string temp = string.Empty;

                                List<Business.RequestDealer> Result = new List<RequestDealer>();
                                Result = TradingServer.Facade.FacadeGetAllRequestDealer();
                                if (Result == null) temp = "-1";
                                else
                                {
                                    int count = Result.Count;
                                    for (int i = 0; i < count; i++)
                                    {
                                        temp += Result[i].AgentID + "," + Result[i].FlagConfirm + "," + Result[i].MaxDev + "," + Result[i].Name + "," + Result[i].Notice
                                                + "," + Result[i].TimeAgentReceive + "," + Result[i].TimeClientRequest + "," + Result[i].Request.Investor.InvestorID
                                                + "," + Result[i].Request.Symbol.Name + "," + Result[i].Request.Size + "," + Result[i].Request.OpenTime
                                                + "," + Result[i].Request.OpenPrice + "," + Result[i].Request.StopLoss + "," + Result[i].Request.TakeProfit
                                                + "," + Result[i].Request.ClosePrice + "," + Result[i].Request.Commission + "," + Result[i].Request.Swap
                                                + "," + Result[i].Request.Profit + "," + Result[i].Request.ID + "," + Result[i].Request.ExpTime + "," + Result[i].Request.ClientCode
                                                + "," + Result[i].Request.IsHedged + "," + Result[i].Request.Type.ID + "," + Result[i].Request.Margin + "," + Result[i].Request.Investor.InvestorGroupInstance.Name
                                                + "," + Result[i].Request.Symbol.Digit + "," + Result[i].Request.Symbol.SpreadByDefault.ToString() + "," +
                                            /*Result[i].Request.Symbol.SpreadDifference*/ Result[i].Request.SpreaDifferenceInOpenTrade
                                                + "," + Result[i].Request.CommandCode + "|";
                                    }
                                }
                                StringResult = subValue[0] + "$" + temp;
                            }
                            break;
                        case "GetAllRequestCompare":
                            {
                                string temp = string.Empty;

                                List<Business.RequestDealer> Result = new List<RequestDealer>();
                                Result = TradingServer.Facade.FacadeGetAllRequestCompareDealer();
                                if (Result == null) temp = "-1";
                                else
                                {
                                    int count = Result.Count;
                                    for (int i = 0; i < count; i++)
                                    {
                                        temp += Result[i].AgentID + "," + Result[i].FlagConfirm + "," + Result[i].MaxDev + "," + Result[i].Name + "," + Result[i].Notice
                                                + "," + Result[i].TimeAgentReceive + "," + Result[i].TimeClientRequest + "," + Result[i].Request.Investor.InvestorID
                                                + "," + Result[i].Request.Symbol.Name + "," + Result[i].Request.Size + "," + Result[i].Request.OpenTime
                                                + "," + Result[i].Request.OpenPrice + "," + Result[i].Request.StopLoss + "," + Result[i].Request.TakeProfit
                                                + "," + Result[i].Request.ClosePrice + "," + Result[i].Request.Commission + "," + Result[i].Request.Swap
                                                + "," + Result[i].Request.Profit + "," + Result[i].Request.ID + "," + Result[i].Request.ExpTime + "," + Result[i].Request.ClientCode
                                                + "," + Result[i].Request.IsHedged + "," + Result[i].Request.Type.ID + "," + Result[i].Request.Margin + "," + Result[i].Request.Investor.InvestorGroupInstance.Name
                                                + "," + Result[i].Request.Symbol.Digit + "," + Result[i].Request.Symbol.SpreadByDefault.ToString() + "," +
                                            /*Result[i].Request.Symbol.SpreadDifference*/ Result[i].Request.SpreaDifferenceInOpenTrade
                                                + "," + Result[i].Request.CommandCode + "|";
                                    }
                                }
                                StringResult = subValue[0] + "$" + temp;
                            }
                            break;

                        case "GetNoticeDealer":
                            {
                                bool checkip = Facade.FacadeCheckIpManager(code, ipAddress);
                                if (checkip)
                                {
                                    int AgentID = -1;
                                    string[] splits = subValue[1].Split(',');
                                    if (splits.Length == 2)
                                    {
                                        int.TryParse(splits[0], out AgentID);
                                        string KeyActive = splits[1];
                                        if (AgentID > 0)
                                        {
                                            string Result = "";
                                            Result = TradingServer.Facade.FacadeGetNoticeDealer(AgentID, KeyActive);
                                            if (!string.IsNullOrEmpty(Result))
                                            {
                                                StringResult = subValue[0] + "$" + Result + ",NA28:" + DateTime.Now.Ticks;
                                            }
                                            else
                                            {
                                                StringResult = subValue[0] + "$" + ",NA28:" + DateTime.Now.Ticks;
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    StringResult = "InvalidIP$NA27";
                                }
                            }
                            break;

                        case "GetAllDealerOnline":
                            {
                                string temp = Facade.FacadeGetAllDealerOnline();
                                StringResult = subValue[0] + "$" + temp;
                            }
                            break;

                        case "GetAllArchiveCandlesOffline":
                            {
                                string temp = Facade.FacadeGetAllArchiveCandlesOffline();
                                StringResult = subValue[0] + "$" + temp;
                            }
                            break;

                        case "DealerCommandConfirm":
                            {
                                bool ResultAddNew = false;
                                bool checkip = Facade.FacadeCheckIpManager(code, ipAddress);
                                Business.RequestDealer Result = new RequestDealer();
                                Result = this.ExtractionRequestDealer(subValue[1]);
                                Facade.FillInstanceOpenTrade(Result, Result.Request);
                                if (checkip)
                                {
                                    ResultAddNew = TradingServer.Facade.FacadeDealerCommandConfirm(Result);
                                    StringResult = subValue[0] + "$" + ResultAddNew.ToString();
                                }
                                else
                                {
                                    StringResult = "InvalidIP";
                                    TradingServer.Facade.FacadeAddNewSystemLog(3, "'" + code + "': dealer confirm action not taken (invalid ip)'" + Result.Request.Investor.Code
                                                       + "' " + Result.Name.ToLower() + " " + Facade.FacadeGetTypeCommand(Result.Request.Type.ID) + " " + Result.Request.FormatDoubleToString(Result.Request.Size) + " symbol:"
                                                       + Result.Request.Symbol.Name + " price open:" + Result.Request.MapPriceForDigit(Result.Request.OpenPrice) + " price close:" + Result.Request.MapPriceForDigit(Result.Request.ClosePrice) + " sl:" + Result.Request.MapPriceForDigit(Result.Request.StopLoss)
                                                       + " tp:" + Result.Request.MapPriceForDigit(Result.Request.TakeProfit), "Invalid IP", ipAddress, code);
                                }

                            }
                            break;

                        case "DealerCommandReject":
                            {
                                bool ResultAddNew = false;
                                Business.RequestDealer Result = new RequestDealer();
                                Result = this.ExtractionRequestDealer(subValue[1]);
                                Facade.FillInstanceOpenTrade(Result, Result.Request);
                                bool checkip = Facade.FacadeCheckIpManager(code, ipAddress);
                                if (checkip)
                                {
                                    ResultAddNew = TradingServer.Facade.FacadeDealerCommandReject(Result);
                                    StringResult = subValue[0] + "$" + ResultAddNew.ToString();
                                }
                                else
                                {
                                    StringResult = "InvalidIP";
                                    TradingServer.Facade.FacadeAddNewSystemLog(3, "'" + code + "': dealer reject action not taken (invalid ip)'" + Result.Request.Investor.Code
                                                      + "' " + Result.Name.ToLower() + " " + Facade.FacadeGetTypeCommand(Result.Request.Type.ID) + " " + Result.Request.FormatDoubleToString(Result.Request.Size) + " symbol:"
                                                      + Result.Request.Symbol.Name + " price open:" + Result.Request.MapPriceForDigit(Result.Request.OpenPrice) + " price close:" + Result.Request.MapPriceForDigit(Result.Request.ClosePrice) + " sl:" + Result.Request.MapPriceForDigit(Result.Request.StopLoss)
                                                      + " tp:" + Result.Request.MapPriceForDigit(Result.Request.TakeProfit), "Invalid IP", ipAddress, code);
                                }
                            }
                            break;

                        case "DealerCommandReturn":
                            {
                                bool ResultAddNew = false;
                                Business.RequestDealer Result = new RequestDealer();
                                Result = this.ExtractionRequestDealer(subValue[1]);
                                Facade.FillInstanceOpenTrade(Result, Result.Request);
                                bool checkip = Facade.FacadeCheckIpManager(code, ipAddress);
                                if (checkip)
                                {
                                    ResultAddNew = TradingServer.Facade.FacadeDealerCommandReturn(Result);
                                    StringResult = subValue[0] + "$" + ResultAddNew.ToString();
                                }
                                else
                                {
                                    StringResult = "InvalidIP";
                                    TradingServer.Facade.FacadeAddNewSystemLog(3, "'" + code + "': dealer return action not taken (invalid ip)'" + Result.Request.Investor.Code
                                                      + "' " + Result.Name.ToLower() + " " + Facade.FacadeGetTypeCommand(Result.Request.Type.ID) + " " + Result.Request.FormatDoubleToString(Result.Request.Size) + " symbol:"
                                                      + Result.Request.Symbol.Name + " price open:" + Result.Request.MapPriceForDigit(Result.Request.OpenPrice) + " price close:" + Result.Request.MapPriceForDigit(Result.Request.ClosePrice) + " sl:" + Result.Request.MapPriceForDigit(Result.Request.StopLoss)
                                                      + " tp:" + Result.Request.MapPriceForDigit(Result.Request.TakeProfit), "Invalid IP", ipAddress, code);
                                }
                            }
                            break;

                        #endregion

                        #region Select Internal Mail
                        case "GetInternalMailToInvestorByID":
                            {
                                Business.InternalMail result = new InternalMail();
                                string temp = "";
                                int MailID = int.Parse(subValue[1]);
                                result = TradingServer.Facade.FacadeGetInternalMailToInvestorByID(MailID);
                                if (result != null)
                                {
                                    temp = result.InternalMailID + "," + TradingServer.Model.TradingCalculate.Instance.ConvertStringToHex(result.Subject) + "," +
                                        TradingServer.Model.TradingCalculate.Instance.ConvertStringToHex(result.From) + "," +
                                        TradingServer.Model.TradingCalculate.Instance.ConvertStringToHex(result.FromName) + "," +
                                        TradingServer.Model.TradingCalculate.Instance.ConvertStringToHex(result.Time.ToString()) + "," +
                                        result.IsNew.ToString() + "," +
                                        TradingServer.Model.TradingCalculate.Instance.ConvertStringToHex(result.Content);
                                }

                                StringResult = subValue[0] + "$" + temp;
                            }
                            break;
                        case "GetFullInternalMailToInvestor":
                            {
                                string[] tem = subValue[1].Split('{');
                                string temp = "";
                                List<Business.InternalMail> result = new List<InternalMail>();
                                result = Facade.internalMailInstance.GetInternalMailToInvestor(tem[0]);
                                if (result != null)
                                {
                                    int countResult = result.Count;
                                    for (int j = 0; j < countResult; j++)
                                    {
                                        temp += result[j].InternalMailID + "█" + result[j].Subject + "█" +
                                            result[j].From + "█" + result[j].FromName + "█" + result[j].Time.ToString() + "█" +
                                            result[j].IsNew.ToString() + "█" + result[j].Content + "█";
                                    }
                                }
                                StringResult = subValue[0] + "$" + temp;
                            }
                            break;
                        case "GetInternalMailToAgentByID":
                            {
                                Business.InternalMail result = new InternalMail();
                                string temp = "";
                                bool checkip = Facade.FacadeCheckIpManagerAndAdmin(code, ipAddress);
                                if (checkip)
                                {
                                    string[] spli = subValue[1].Split(',');
                                    int MailID = int.Parse(spli[0]);
                                    result = TradingServer.Facade.FacadeGetInternalMailToAgentByID(MailID, spli[1]);
                                    if (result != null)
                                    {
                                        temp = result.InternalMailID + "," + TradingServer.Model.TradingCalculate.Instance.ConvertStringToHex(result.Subject) + "," +
                                            TradingServer.Model.TradingCalculate.Instance.ConvertStringToHex(result.From) + "," +
                                            TradingServer.Model.TradingCalculate.Instance.ConvertStringToHex(result.FromName) + "," +
                                            TradingServer.Model.TradingCalculate.Instance.ConvertStringToHex(result.Time.ToString()) + "," +
                                            result.IsNew.ToString() + "," +
                                            TradingServer.Model.TradingCalculate.Instance.ConvertStringToHex(result.Content);
                                    }
                                }
                                StringResult = subValue[0] + "$" + temp;
                            }
                            break;

                        #endregion

                        #region Select Alert
                        case "SelectAlertByInvestorID":
                            {
                                string temp = string.Empty;
                                List<Business.PriceAlert> Result = new List<PriceAlert>();
                                string[] subParameter = subValue[1].Split(',');
                                if (subParameter.Length > 0)
                                {
                                    int Start = int.Parse(subParameter[0]);
                                    int End = int.Parse(subParameter[1]);
                                    int InvestorID = int.Parse(subParameter[2]);
                                    Result = TradingServer.Facade.FacadeGetAlertByInvestorID(InvestorID, Start, End);
                                    if (Result != null)
                                    {
                                        for (int n = 0; n < Result.Count; n++)
                                        {
                                            temp += Result[n].ID + "," + Result[n].Symbol + "," + Result[n].Email + "," + Result[n].PhoneNumber
                                                    + "," + Result[n].Value + "," + Result[n].AlertCondition.ToString() + "," + Result[n].AlertAction.ToString() + "," + Result[n].IsEnable
                                                    + "," + Result[n].DateCreate + "," + Result[n].DateActive + "," + Result[n].InvestorID + "," + Result[n].Notification + "|";
                                        }
                                    }
                                }
                                StringResult = subValue[0] + "$" + temp;
                            }
                            break;
                        case "GetAlertByInvestorID":
                            {
                                string temp = string.Empty;
                                List<Business.PriceAlert> Result = new List<PriceAlert>();
                                if (subValue[1] != "")
                                {
                                    int InvestorID = int.Parse(subValue[1]);
                                    Result = TradingServer.Facade.FacadeGetAlertByInvestorID(InvestorID);
                                    if (Result != null)
                                    {
                                        for (int n = 0; n < Result.Count; n++)
                                        {
                                            temp += Result[n].ID + "{" + Result[n].Symbol + "{" + Result[n].Email + "{" + Result[n].PhoneNumber
                                                    + "{" + Result[n].Value + "{" + Result[n].AlertCondition.ToString() + "{" + Result[n].AlertAction.ToString() + "{" + Result[n].IsEnable
                                                    + "{" + Result[n].DateCreate + "{" + Result[n].DateActive + "{" + Result[n].InvestorID + "{" + Result[n].Notification + "`";
                                        }
                                    }
                                }
                                StringResult = subValue[0] + "$" + temp;
                            }
                            break;
                        case "SelectAlertByInvestorIDWithTime":
                            {
                                string temp = string.Empty;
                                List<Business.PriceAlert> Result = new List<PriceAlert>();
                                string[] subParameter = subValue[1].Split(',');
                                if (subParameter.Length > 0)
                                {
                                    int InvestorID = int.Parse(subParameter[0]);
                                    DateTime Start = DateTime.Parse(subParameter[1]);
                                    DateTime End = DateTime.Parse(subParameter[2]);
                                    Result = TradingServer.Facade.FacadeGetAlertByInvestorIDWithTime(InvestorID, Start, End);
                                    if (Result != null)
                                    {
                                        for (int n = 0; n < Result.Count; n++)
                                        {
                                            temp += Result[n].ID + "," + Result[n].Symbol + "," + Result[n].Email + "," + Result[n].PhoneNumber
                                                    + "," + Result[n].Value + "," + Result[n].AlertCondition.ToString() + "," + Result[n].AlertAction.ToString() + "," + Result[n].IsEnable
                                                    + "," + Result[n].DateCreate + "," + Result[n].DateActive + "," + Result[n].InvestorID + "," + Result[n].Notification + "|";
                                        }
                                    }
                                }
                                StringResult = subValue[0] + "$" + temp;
                            }
                            break;
                        case "SelectHistoryAlertWithTime":
                            {
                                string temp = string.Empty;
                                List<Business.PriceAlert> Result = new List<PriceAlert>();
                                string[] subParameter = subValue[1].Split('{');
                                if (subParameter.Length > 0)
                                {
                                    int InvestorID = int.Parse(subParameter[0]);
                                    DateTime Start = DateTime.Parse(subParameter[1]);
                                    DateTime End = DateTime.Parse(subParameter[2]);
                                    Result = TradingServer.Facade.FacadeGetAlertByInvestorIDWithTime(InvestorID, Start, End);
                                    if (Result != null)
                                    {
                                        for (int n = 0; n < Result.Count; n++)
                                        {
                                            temp += Result[n].ID + "{" + Result[n].Symbol + "{" + Result[n].Email + "{" + Result[n].PhoneNumber
                                                    + "{" + Result[n].Value + "{" + Result[n].AlertCondition.ToString() + "{" + Result[n].AlertAction.ToString() + "{" + Result[n].IsEnable
                                                    + "{" + Result[n].DateCreate + "{" + Result[n].DateActive + "{" + Result[n].InvestorID + "{" + Result[n].Notification + "`";
                                        }
                                    }
                                }
                                StringResult = subValue[0] + "$" + temp;
                            }
                            break;
                        #endregion

                        #region Select Permit
                        case "SelectPermit":
                            {
                                bool checkip = Facade.FacadeCheckIpAdmin(code, ipAddress);
                                if (checkip)
                                {
                                    string temp = string.Empty;
                                    List<Business.Permit> Result = new List<Permit>();
                                    Result = TradingServer.Facade.FacadeGetAllPermit();

                                    if (Result != null)
                                    {
                                        int countAgent = Result.Count;
                                        for (int n = 0; n < countAgent; n++)
                                        {
                                            temp += Result[n].PermitID.ToString() + "," + Result[n].AgentGroupID.ToString() + "," + Result[n].AgentID.ToString() + "," + Result[n].Role.RoleID.ToString() + "|";
                                        }
                                    }

                                    StringResult = subValue[0] + "$" + temp;
                                }
                            }
                            break;

                        case "SelectPermitByID":
                            {
                                string temp = string.Empty;
                                int PermitID = -1;
                                int.TryParse(subValue[1], out PermitID);
                                Business.Permit Result = new Permit();

                                Result = TradingServer.Facade.FacadeGetPermitByPermitID(PermitID);

                                if (Result != null)
                                {
                                    temp += Result.PermitID.ToString() + "," + Result.AgentGroupID.ToString() + "," + Result.AgentID.ToString() + "," + Result.Role.RoleID.ToString();
                                }

                                StringResult = subValue[0] + "$" + temp;
                            }
                            break;

                        case "GetCodeAgentSendMailByInvestorGroupID":
                            {
                                string temp = string.Empty;
                                int GroupID = -1;
                                int.TryParse(subValue[1], out GroupID);
                                List<Business.Agent> Result = new List<Agent>();
                                Result = TradingServer.Facade.FacadeGetCodeAgentMailByInvestorGroupID(GroupID);
                                if (Result != null)
                                {
                                    for (int i = 0; i < Result.Count; i++)
                                    {
                                        if (Result[i].Name != "" & Result[i].Code != "")
                                        {
                                            temp += Result[i].Name + "," + Result[i].Code + "|";
                                        }
                                    }
                                }
                                StringResult = subValue[0] + "$" + temp;
                            }
                            break;

                        case "SelectPermitByAgentID":
                            {
                                string temp = string.Empty;
                                bool checkip = Facade.FacadeCheckIpManagerAndAdmin(code, ipAddress);
                                if (checkip)
                                {
                                    int AgentID = -1;
                                    int.TryParse(subValue[1], out AgentID);
                                    List<Business.Permit> Result = new List<Permit>();

                                    Result = TradingServer.Facade.FacadeGetPermitByAgentID(AgentID);
                                    if (Result != null)
                                    {
                                        int countAgent = Result.Count;
                                        for (int n = 0; n < countAgent; n++)
                                        {
                                            temp += Result[n].PermitID.ToString() + "," + Result[n].AgentGroupID.ToString() + "," + Result[n].AgentID.ToString() + "," + Result[n].Role.RoleID.ToString() + "|";
                                        }
                                    }
                                }
                                StringResult = subValue[0] + "$" + temp;
                            }
                            break;
                        #endregion

                        #region Select Role
                        case "SelectRole":
                            {
                                bool checkip = Facade.FacadeCheckIpAdmin(code, ipAddress);
                                if (checkip)
                                {
                                    string temp = string.Empty;
                                    List<Business.Role> Result = new List<Role>();
                                    Result = TradingServer.Facade.FacadeGetAllRole();

                                    if (Result != null)
                                    {
                                        int countAgent = Result.Count;
                                        for (int n = 0; n < countAgent; n++)
                                        {
                                            temp += Result[n].RoleID.ToString() + "," + Result[n].Code + "," + Result[n].Name + "," + Result[n].Comment + "|";
                                        }
                                    }

                                    StringResult = subValue[0] + "$" + temp;
                                }
                            }
                            break;

                        case "SelectRoleByID":
                            {
                                string temp = string.Empty;
                                int RoleID = -1;
                                int.TryParse(subValue[1], out RoleID);
                                Business.Role Result = new Role();

                                Result = TradingServer.Facade.FacadeGetRoleByRoleID(RoleID);

                                if (Result != null)
                                {
                                    temp += Result.RoleID.ToString() + "," + Result.Code + "," + Result.Name + "," + Result.Comment;
                                }

                                StringResult = subValue[0] + "$" + temp;
                            }
                            break;

                        #endregion

                        #region Select IAgentSecurity
                        case "SelectIAgentSecurity":
                            {
                                bool checkip = Facade.FacadeCheckIpAdmin(code, ipAddress);
                                if (checkip)
                                {
                                    string temp = string.Empty;
                                    List<Business.IAgentSecurity> Result = new List<IAgentSecurity>();
                                    Result = TradingServer.Facade.FacadeGetIAgentSecurity();

                                    if (Result != null)
                                    {
                                        int countInvestor = Result.Count;
                                        for (int n = 0; n < countInvestor; n++)
                                        {
                                            temp += Result[n].IAgentSecurityID.ToString() + "," + Result[n].AgentID.ToString() + "," + Result[n].SecurityID.ToString()
                                                    + "," + Result[n].Use.ToString() + "," + Result[n].MinLots.ToString() + "," + Result[n].MaxLots.ToString() + "|";
                                        }
                                    }

                                    StringResult = subValue[0] + "$" + temp;
                                }
                            }
                            break;

                        case "SelectIAgentSecurityByID":
                            {
                                string temp = string.Empty;
                                int IAgentSecurityID = -1;
                                int.TryParse(subValue[1], out IAgentSecurityID);
                                Business.IAgentSecurity Result = new IAgentSecurity();

                                Result = TradingServer.Facade.FacadeGetIAgentSecurityByID(IAgentSecurityID);

                                if (Result != null)
                                {
                                    temp = Result.IAgentSecurityID.ToString() + "," + Result.AgentID.ToString() + "," + Result.SecurityID.ToString() + "," + Result.Use.ToString() + "," + Result.MinLots.ToString() + "," + Result.MaxLots.ToString();
                                }

                                StringResult = subValue[0] + "$" + temp;
                            }
                            break;

                        case "SelectIAgentSecurityByAgentID":
                            {
                                string temp = string.Empty;
                                int AgentID = -1;
                                int.TryParse(subValue[1], out AgentID);
                                List<Business.IAgentSecurity> Result = new List<Business.IAgentSecurity>();

                                Result = TradingServer.Facade.FacadeGetIAgentSecurityByAgentID(AgentID);

                                if (Result != null)
                                {
                                    int countInvestor = Result.Count;
                                    for (int n = 0; n < countInvestor; n++)
                                    {
                                        temp += Result[n].IAgentSecurityID.ToString() + "," + Result[n].AgentID.ToString() + "," + Result[n].SecurityID.ToString()
                                                 + "," + Result[n].Use.ToString() + "," + Result[n].MinLots.ToString() + "," + Result[n].MaxLots.ToString() + "|";
                                    }
                                }

                                StringResult = subValue[0] + "$" + temp;
                            }
                            break;

                        case "SelectIAgentSecurityBySecurityID":
                            {
                                string temp = string.Empty;
                                int SecurityID = -1;
                                int.TryParse(subValue[1], out SecurityID);
                                List<Business.IAgentSecurity> Result = new List<Business.IAgentSecurity>();

                                Result = TradingServer.Facade.FacadeGetIAgentSecurityBySecurityID(SecurityID);

                                if (Result != null)
                                {
                                    int countInvestor = Result.Count;
                                    for (int n = 0; n < countInvestor; n++)
                                    {
                                        temp += Result[n].IAgentSecurityID.ToString() + "," + Result[n].AgentID.ToString() + "," + Result[n].SecurityID.ToString()
                                             + "," + Result[n].Use.ToString() + "," + Result[n].MinLots.ToString() + "," + Result[n].MaxLots.ToString() + "|";
                                    }
                                }

                                StringResult = subValue[0] + "$" + temp;
                            }
                            break;

                        #endregion

                        #region Select IAgentGroup
                        case "SelectIAgentGroup":
                            {
                                string temp = string.Empty;
                                List<Business.IAgentGroup> Result = new List<IAgentGroup>();
                                Result = TradingServer.Facade.FacadeGetAllIAgentGroup();

                                if (Result != null)
                                {
                                    int countInvestor = Result.Count;
                                    for (int n = 0; n < countInvestor; n++)
                                    {
                                        temp += Result[n].IAgentGroupID.ToString() + "," + Result[n].AgentID.ToString() + "," + Result[n].InvestorGroupID.ToString() + "|";
                                    }
                                }

                                StringResult = subValue[0] + "$" + temp;
                            }
                            break;

                        case "SelectIAgentGroupByID":
                            {
                                string temp = string.Empty;
                                int IAgentGroupID = -1;
                                int.TryParse(subValue[1], out IAgentGroupID);
                                Business.IAgentGroup Result = new IAgentGroup();

                                Result = TradingServer.Facade.FacadeGetIAgentGroupByIAgentGroupID(IAgentGroupID);

                                if (Result != null)
                                {
                                    temp = Result.IAgentGroupID.ToString() + "," + Result.AgentID.ToString() + "," + Result.InvestorGroupID.ToString();
                                }

                                StringResult = subValue[0] + "$" + temp;
                            }
                            break;

                        case "SelectIAgentGroupByAgentID":
                            {
                                string temp = string.Empty;
                                bool checkip = Facade.FacadeCheckIpManagerAndAdmin(code, ipAddress);
                                if (checkip)
                                {
                                    int AgentID = -1;
                                    int.TryParse(subValue[1], out AgentID);
                                    List<Business.IAgentGroup> Result = new List<Business.IAgentGroup>();

                                    Result = TradingServer.Facade.FacadeGetIAgentGroupByAgentID(AgentID);

                                    if (Result != null)
                                    {
                                        int countInvestor = Result.Count;
                                        for (int n = 0; n < countInvestor; n++)
                                        {
                                            temp += Result[n].IAgentGroupID.ToString() + "," + Result[n].AgentID.ToString() + "," + Result[n].InvestorGroupID.ToString() + "|";
                                        }
                                    }
                                }
                                StringResult = subValue[0] + "$" + temp;
                            }
                            break;

                        case "SelectIAgentSecurityByInvestorGroupID":
                            {
                                string temp = string.Empty;
                                int InvestorGroupID = -1;
                                int.TryParse(subValue[1], out InvestorGroupID);
                                List<Business.IAgentGroup> Result = new List<Business.IAgentGroup>();

                                Result = TradingServer.Facade.FacadeGetIAgentGroupByInvestorGroupID(InvestorGroupID);

                                if (Result != null)
                                {
                                    int countInvestor = Result.Count;
                                    for (int n = 0; n < countInvestor; n++)
                                    {
                                        temp += Result[n].IAgentGroupID.ToString() + "," + Result[n].AgentID.ToString() + "," + Result[n].InvestorGroupID.ToString() + "|";
                                    }
                                }

                                StringResult = subValue[0] + "$" + temp;
                            }
                            break;

                        #endregion

                        #region Select Market Area
                        case "SelectMarketArea":
                            {
                                string temp = string.Empty;
                                bool checkip = Facade.FacadeCheckIpAdmin(code, ipAddress);
                                if (checkip)
                                {
                                    temp = this.ExtractSelectMarketArea();
                                }
                                StringResult = subValue[0] + "$" + temp;
                            }
                            break;
                        #endregion

                        #region SELECT OPEN TRADE(LOG COMMENT)
                        case "GetOpenTradeByID":
                            {
                                bool checkip = Facade.FacadeCheckIpManagerAndAdmin(code, ipAddress);
                                if (checkip)
                                {
                                    int OpenTradeID = -1;
                                    int.TryParse(subValue[1], out OpenTradeID);
                                    Business.OpenTrade Result = new OpenTrade();
                                    Result = TradingServer.Facade.FacadeGetOpenTradeByOpenTradeID(OpenTradeID);

                                    if (Result.Symbol != null && Result.Investor != null)
                                    {
                                        StringResult = subValue[0] + "$" + Result.ClientCode + "," + Result.ClosePrice + "," + Result.CloseTime + "," +
                                                        Result.CommandCode + "," + Result.Commission + "," + Result.ExpTime + "," +
                                                        Result.ID + "," + Result.Investor.InvestorID + "," + Result.IsClose + "," +
                                                        Result.IsHedged + "," + Result.Margin + "," + Result.MaxDev + "," + Result.OpenPrice + "," +
                                                        Result.OpenTime + "," + Result.Profit + "," + Result.Size + "," + Result.StopLoss + "," +
                                                        Result.Swap + "," + Result.Symbol.Name + "," + Result.TakeProfit + "," + Result.Taxes + "," +
                                                        Result.Type.Name + "," + Result.Type.ID + "," + Result.Symbol.ContractSize + "," +
                                            /*Result.Symbol.SpreadDifference*/ +Result.SpreaDifferenceInOpenTrade + "," + Result.Symbol.Currency + "," +
                                            Result.Comment + "," + Result.AgentCommission;
                                    }

                                    #region INSERT SYSTEM LOG
                                    //INSERT SYSTEM LOG
                                    //'2222': 22450 accounts have been requested
                                    //int num = 0;
                                    //if (Result.ID > 0)
                                    //    num = 1;

                                    //string content = "'" + code + "': " + num + " accounts have been requested";
                                    //string comment = "[account request]";
                                    //TradingServer.Facade.FacadeAddNewSystemLog(3, content, comment, ipAddress, code);
                                    #endregion
                                }
                            }
                            break;
                        #endregion

                        #region SELECT ORDER DATA
                        case "GetListFiles":
                            {
                                bool checkip = Facade.FacadeCheckIpManagerAndAdmin(code, ipAddress);
                                bool checkPermit = Facade.FacadeCheckPermitDownloadStatement(code);
                                if (checkip)
                                {
                                    if (checkPermit)
                                    {
                                        DateTime from = new DateTime();
                                        DateTime to = new DateTime();
                                        string[] times = subValue[1].Split('{');
                                        if (times.Length == 2)
                                        {
                                            from = DateTime.Parse(times[0]);
                                            to = DateTime.Parse(times[1]);
                                            StringResult = subValue[0] + "$" + Model.TradingCalculate.Instance.GetFileInfo(from, to);
                                        }
                                    }
                                    else
                                    {
                                        StringResult = subValue[0] + "$" + "MCM006";
                                    }
                                }
                            }
                            break;
                        case "GetFileContents":
                            {
                                bool checkip = Facade.FacadeCheckIpManagerAndAdmin(code, ipAddress);
                                bool checkPermit = Facade.FacadeCheckPermitDownloadStatement(code);
                                if (checkip)
                                {
                                    if (checkPermit)
                                    {
                                        if (subValue[1] != "")
                                        {
                                            StringResult = subValue[0] + "$" + Model.TradingCalculate.Instance.GetContenFile(subValue[1]);
                                        }
                                    }
                                    else
                                    {
                                        StringResult = subValue[0] + "$" + "MCM006";
                                    }
                                }
                            }
                            break;
                        case "GetOrderByCode":
                            {
                                Business.OrderData Result = new OrderData();
                                Result = TradingServer.Facade.FacadeGetOrderDataByCode(subValue[1]);
                                if (!string.IsNullOrEmpty(Result.Code))
                                {
                                    StringResult = subValue[0] + "$" + Result.Login + "," + Result.Type + "," +
                                                    Result.OpenTime + "," + Result.CloseTime + "," + Result.OneConvRate + "," +
                                                    Result.Commission + "," + Result.Comment + "," + Result.ExpDate + "," +
                                                    Result.Lots + "," + Result.OpenPrice + "," + Result.ClosePrice + "," +
                                                    Result.TwoConvRate + "," + Result.AgentCommission + "," + Result.ValueDate + "," +
                                                    Result.Symbol + "," + Result.StopLoss + "," + Result.TakeProfit + "," +
                                                    Result.MarginRate + "," + Result.Swaps + "," + Result.Taxes + "," + Result.Profit + "," +
                                                    Result.OrderCode + "," + Result.Code;
                                }
                            }
                            break;
                        #endregion

                        #region CHECK PASSWORD
                        case "VerifyMaster":
                            {
                                bool resultCheck = false;
                                string[] subParameter = subValue[1].Split(',');
                                if (subParameter.Length == 2)
                                {
                                    int investorID = 0;
                                    bool check = int.TryParse(subParameter[0], out investorID);
                                    if (check)
                                    {
                                        Business.Investor tempInvestor = new Investor();
                                        tempInvestor = TradingServer.Facade.FacadeGetInvestorByInvestorID(investorID);
                                        bool checkip = Facade.FacadeCheckIpManagerAndAdmin(code, ipAddress);
                                        if (checkip)
                                        {
                                            bool checkRule = Facade.FacadeCheckPermitAccountManagerAndAdmin(code);
                                            bool checkGroup = Facade.FacadeCheckPermitAccessGroupManagerAndAdmin(code, tempInvestor.InvestorGroupInstance.InvestorGroupID);
                                            if (checkRule && checkGroup)
                                            {
                                                resultCheck = TradingServer.Facade.FacadeCheckMasterPassword(investorID, subParameter[1]);
                                                if (resultCheck)
                                                {
                                                    string content = "'" + code + "': checking password of '" + tempInvestor.Code + "' [Success]";
                                                    string comment = "[verify password]";
                                                    TradingServer.Facade.FacadeAddNewSystemLog(3, content, comment, ipAddress, code);
                                                }
                                                else
                                                {
                                                    string content = "'" + code + "': checking password of '" + tempInvestor.Code + "' [Failed]";
                                                    string comment = "[verify password]";
                                                    TradingServer.Facade.FacadeAddNewSystemLog(3, content, comment, ipAddress, code);
                                                }
                                                StringResult = subValue[0] + "$" + resultCheck;
                                            }
                                            else
                                            {
                                                StringResult = subValue[0] + "$MCM006";
                                                string content = "'" + code + "': checking password of '" + tempInvestor.Code + "' failed(not enough rights)";
                                                string comment = "[verify password]";
                                                TradingServer.Facade.FacadeAddNewSystemLog(3, content, comment, ipAddress, code);
                                            }
                                        }
                                        else
                                        {
                                            StringResult = subValue[0] + "$" + "MCM005";
                                            string content = "'" + code + "': checking password of '" + tempInvestor.Code + "' failed(invalid ip)";
                                            string comment = "[verify password]";
                                            TradingServer.Facade.FacadeAddNewSystemLog(3, content, comment, ipAddress, code);
                                        }
                                    }
                                }

                            }
                            break;
                        #endregion

                        #region GET TIME SERVER
                        case "GetTimeServer":
                            {
                                long time = 0;
                                time = TradingServer.ClientFacade.FacadeGetTimeZoneServer();
                                StringResult = subValue[0] + "$" + time;
                            }
                            break;
                        #endregion

                        #region MANUAL SEND REPORT
                        case "ManualSendReport":
                            {
                                string[] subParameter = subValue[1].Split(',');
                                //string investorCode = subParameter[0];
                                //DateTime timeStart = DateTime.Parse(subParameter[1]);
                                //DateTime timeEnd = DateTime.Parse(subParameter[2]);

                                Business.TimeEvent newTimeEvent = new TimeEvent();
                                //Market.marketInstance.SendReportDayManaual(investorCode, timeStart, timeEnd);
                                marketInstance.SendReportDay("All,", newTimeEvent);
                            }
                            break;

                        case "ManualSendReportMonth":
                            {
                                string[] subParameter = subValue[1].Split(',');

                                Business.TimeEvent newTimeEvent = new TimeEvent();
                                marketInstance.SendReportMonth("All", newTimeEvent);
                            }
                            break;
                        #endregion

                        #region MANUAL CALCULATION SWAP
                        case "ManualCalculationSwap":
                            {
                                Business.TimeEvent newTimeEvent = new TimeEvent();
                                marketInstance.BeginCalculationSwap("All", newTimeEvent);
                            }
                            break;
                        #endregion

                        #region VIRTUAL DEALER
                        case "KD312":
                            {
                                bool checkip = Facade.FacadeCheckIpAdmin(code, ipAddress);
                                if (checkip)
                                {
                                    string status = "[Failed]";
                                    VirtualDealer vd = new VirtualDealer();
                                    string result = vd.MapDealer(subValue[1]);
                                    int id;
                                    int.TryParse(result, out id);
                                    if (id == 1)
                                    {
                                        result = vd.AddDealer();
                                        status = "[Success]";
                                    }
                                    string content = "'" + code + "': robot dealer config added/changed ['" + vd.Name + "'] " + status;
                                    string comment = "[add new robot dealer]";
                                    TradingServer.Facade.FacadeAddNewSystemLog(3, content, comment, ipAddress, code);
                                    StringResult = subValue[0] + "$" + result;
                                }
                            }
                            break;

                        case "KD341":
                            {
                                bool checkip = Facade.FacadeCheckIpAdmin(code, ipAddress);
                                if (checkip)
                                {
                                    string status = "[Failed]",contentChange = "";
                                    VirtualDealer vd = new VirtualDealer();
                                    string result = vd.MapDealer(subValue[1]);
                                    int id;
                                    int.TryParse(result, out id);
                                    if (id == 1)
                                    {
                                        contentChange = vd.MapChangeContentConfigVD();
                                        result = vd.UpdateDealer();
                                        status = "[Success]";
                                    }
                                    string content = "'" + code + "': update config robot dealer '" + vd.Name + "': " + contentChange + " " + status;
                                    string comment = "[update config robot dealer]";
                                    TradingServer.Facade.FacadeAddNewSystemLog(3, content, comment, ipAddress, code);
                                    StringResult = subValue[0] + "$" + result;
                                }
                            }
                            break;

                        case "KD485":
                            {
                                bool checkip = Facade.FacadeCheckIpAdmin(code, ipAddress);
                                if (checkip)
                                {
                                    string status = "[Failed]";
                                    int id;
                                    int.TryParse(subValue[1], out id);
                                    string result = "0";
                                    VirtualDealer vd = new VirtualDealer();
                                    if (id > 0)
                                    {
                                        vd.ID = id;
                                        result = vd.DeleteDealer(id);
                                        status = "[Success]";
                                    }
                                    string content = "'" + code + "': delete robot dealer ['" + vd.Name + "'] " + status;
                                    string comment = "[delete config robot dealer]";
                                    TradingServer.Facade.FacadeAddNewSystemLog(3, content, comment, ipAddress, code);
                                    StringResult = subValue[0] + "$" + result;
                                }
                            }
                            break;

                        case "KD678":
                            {
                                bool checkip = Facade.FacadeCheckIpAdmin(code, ipAddress);
                                if (checkip)
                                {
                                    string status = "[Failed]", contentChange = "";
                                    VirtualDealer vd = new VirtualDealer();
                                    string result = vd.MapDealer(subValue[1]);
                                    int id;
                                    int.TryParse(result, out id);
                                    if (id == 1)
                                    {
                                        contentChange = vd.MapChangeContentConfigVD();
                                        result = vd.UpdateDealerInfo();
                                        status = "[Success]";
                                    }
                                    string content = "'" + code + "': update config robot dealer '" + vd.Name + "': " + contentChange + " " + status;
                                    string comment = "[update config robot dealer]";
                                    TradingServer.Facade.FacadeAddNewSystemLog(3, content, comment, ipAddress, code);
                                    StringResult = subValue[0] + "$" + result;
                                }
                            }
                            break;

                        case "KD346":
                            {
                                bool checkip = Facade.FacadeCheckIpAdmin(code, ipAddress);
                                if (checkip)
                                {
                                    string result = "";
                                    VirtualDealer vd = new VirtualDealer();
                                    List<VirtualDealer> vds = new List<VirtualDealer>();
                                    vds = vd.GetAllVirtualDealer();
                                    for (int i = 0; i < vds.Count; i++)
                                    {
                                        result += vds[i].ExtractDealer() + "[";
                                    }
                                    StringResult = subValue[0] + "$" + result;
                                }
                            }
                            break;

                        case "KD394":
                            {
                                bool checkip = Facade.FacadeCheckIpAdmin(code, ipAddress);
                                if (checkip)
                                {
                                    string status = "[Failed]", contentChange = "";
                                    VirtualDealer vd = new VirtualDealer();
                                    string result = vd.MapDealer(subValue[1]);
                                    int id;
                                    int.TryParse(result, out id);
                                    if (id == 1)
                                    {
                                        contentChange = vd.MapChangeContentConfigVD();
                                        result = vd.UpdateDealerSymbol();
                                        status = "[Success]";
                                    }
                                    string content = "'" + code + "': update config robot dealer '" + vd.Name + "': " + contentChange + " " + status;
                                    string comment = "[update config robot dealer]";
                                    TradingServer.Facade.FacadeAddNewSystemLog(3, content, comment, ipAddress, code);
                                    StringResult = subValue[0] + "$" + result;
                                }
                            }
                            break;
                        #endregion

                        #region MONITOR STATUS SYMBOL
                        case "MonitorSymbol":
                            {
                                if (Business.Market.SymbolList != null)
                                {
                                    int count = Business.Market.SymbolList.Count;
                                    for (int j = 0; j < count; j++)
                                    {
                                        if (Business.Market.SymbolList[j].Name == subValue[1])
                                        {
                                            StringResult = subValue[0] + "$Trade Status: " + Business.Market.SymbolList[j].IsTrade + "- Quote Status: " + Business.Market.SymbolList[j].IsQuote;
                                            break;
                                        }
                                    }
                                }
                            }
                            break;
                        #endregion

                        #region COMMAND GET CLIENT LOG
                        case "GetClientLog":
                            {
                                //GetClientLog$LoginCode{StartTime{EndTime
                                string[] subCommand = subValue[1].Split('{');
                                if (subCommand.Length > 0)
                                {
                                    //int investorID = int.Parse(subCommand[0]);
                                    DateTime timeStart = DateTime.Parse(subCommand[1]);
                                    DateTime timeEnd = DateTime.Parse(subCommand[2]);

                                    if (Business.Market.InvestorList != null)
                                    {
                                        bool isExists = false;
                                        int count = Business.Market.InvestorList.Count;
                                        for (int i = 0; i < count; i++)
                                        {
                                            if (Business.Market.InvestorList[i].Code == subCommand[0])
                                            {
                                                #region PROCESS CHECK CLIENT IN MARKET
                                                if (Business.Market.ListClientLogs != null && Business.Market.ListClientLogs.Count > 0)
                                                {
                                                    int countClientLog = Business.Market.ListClientLogs.Count;

                                                    for (int j = 0; j < count; j++)
                                                    {
                                                        if (Business.Market.ListClientLogs[j].AdminCode == code)
                                                        {
                                                            Business.Market.ListClientLogs[j].InvestorID = Business.Market.InvestorList[i].InvestorID;
                                                            Business.Market.ListClientLogs[j].InvestorCode = subCommand[0];
                                                            Business.Market.ListClientLogs[j].IsComplete = false;
                                                            Business.Market.ListClientLogs[j].ClientLogs = new List<string>();

                                                            isExists = true;
                                                            break;
                                                        }
                                                    }
                                                }

                                                if (!isExists)
                                                {
                                                    Business.ClientLog newClientLog = new ClientLog();
                                                    newClientLog.InvestorID = Business.Market.InvestorList[i].InvestorID;
                                                    newClientLog.InvestorCode = subCommand[0];
                                                    newClientLog.AdminCode = code;
                                                    newClientLog.IsComplete = false;
                                                    newClientLog.ClientLogs = new List<string>();

                                                    Business.Market.ListClientLogs.Add(newClientLog);
                                                }
                                                #endregion

                                                //SEND COMMAND TO CLIENT REQUEST LOG
                                                string strRequestActionLog = "ServerRequestLog$" + subCommand[0] + "{" + timeStart + "{" + timeEnd;
                                                Business.Market.InvestorList[i].ClientCommandQueue.Add(strRequestActionLog);

                                                System.Threading.Thread.Sleep(1000);
                                                StringResult = subValue[0] + "$True";
                                                isExists = true;

                                                break;
                                            }
                                        }

                                        if (!isExists)
                                            StringResult = subValue[0] + "$False";
                                    }
                                }
                                else
                                {
                                    StringResult = subValue[0] + "$False";
                                }
                            }
                            break;
                        #endregion
                    }
                }
            }

            return StringResult;
        }
Exemplo n.º 18
0
        /// <summary>
        /// 
        /// </summary>
        internal static void ProcessLastBalance()
        {
            Business.SumLastAccount newLastAccount;
            while (Business.Market.IsProcessLastAccount)
            {
                newLastAccount = Business.Market.GetLastAccount();
                List<Business.LastBalance> listLastBalance = new List<LastBalance>();
                while (newLastAccount != null)
                {
                    double PLBalance = 0;
                    double closePL = 0;
                    double deposit = 0;
                    double balance = 0;
                    double floatingPL = 0;
                    double creditIn = 0;
                    double creditOut = 0;
                    double equity = 0;
                    double withdrawal = 0;
                    double freeMargin = 0;
                    double swap = 0;
                    double commission = 0;
                    double volume = 0;

                    //PROCESS EXECUTOR LAST ACCOUNT
                    if (newLastAccount != null)
                    {
                        #region PROCESS EXECUTOR HISTORY
                        if (newLastAccount.ListHistory != null)
                        {
                            int count = newLastAccount.ListHistory.Count;
                            for (int i = 0; i < count; i++)
                            {
                                if (newLastAccount.ListHistory[i].Type.ID == 21)
                                    continue;

                                if (newLastAccount.ListHistory[i].Type.ID == 13 || newLastAccount.ListHistory[i].Type.ID == 14 ||
                                    newLastAccount.ListHistory[i].Type.ID == 15 || newLastAccount.ListHistory[i].Type.ID == 16)
                                {
                                    if (newLastAccount.ListHistory[i].Type.ID == 13)
                                        deposit += newLastAccount.ListHistory[i].Profit;

                                    if (newLastAccount.ListHistory[i].Type.ID == 14)
                                        withdrawal += newLastAccount.ListHistory[i].Profit;

                                    if (newLastAccount.ListHistory[i].Type.ID == 15)
                                        creditIn += newLastAccount.ListHistory[i].Profit;

                                    if (newLastAccount.ListHistory[i].Type.ID == 16)
                                        creditOut += newLastAccount.ListHistory[i].Profit;
                                }
                                else
                                {
                                    closePL += newLastAccount.ListHistory[i].Profit;
                                    swap += newLastAccount.ListHistory[i].Swap;
                                    commission += newLastAccount.ListHistory[i].Commission;
                                    volume += newLastAccount.ListHistory[i].Size;
                                }
                            }
                        }
                        #endregion

                        #region PROCESS EXECUTOR OPEN TRADE
                        if (newLastAccount.ListOpenTrade != null)
                        {
                            int count = newLastAccount.ListOpenTrade.Count;
                            for (int i = 0; i < count; i++)
                            {
                                floatingPL += newLastAccount.ListOpenTrade[i].Profit + newLastAccount.ListOpenTrade[i].Swap +
                                    newLastAccount.ListOpenTrade[i].Commission;
                            }
                        }
                        #endregion

                        #region SAVE FLOATINGPL AND MONTH VOLUME FOR AGENT SERVER
                        newLastAccount.InvestorAccount.FloatingPL = floatingPL;
                        newLastAccount.InvestorAccount.MonthVolume = volume;
                        newLastAccount.InvestorAccount.TimeSave = TimeEndDay;

                        Business.EndOfDayAgent newEODAgent = new EndOfDayAgent();
                        newEODAgent.InvestorID = newLastAccount.InvestorAccount.InvestorID;
                        newEODAgent.FloatingPL = floatingPL;
                        newEODAgent.MonthVolume = volume;

                        Business.Market.ListEODAgent.Add(newEODAgent);
                        bool isEndCalculation = false;
                        if (Business.Market.ListEODAgent.Count == Business.Market.InvestorList.Count)
                        {
                            #region SEND COMMAND ALERT END OF DAY TO AGENT SYSTEM
                            try
                            {
                                //SEND COMMAND TO AGENT SERVER
                                string msg = "NotifyEndOfDay";
                                Business.AgentNotify newAgentNotify = new AgentNotify();
                                newAgentNotify.NotifyMessage = msg;
                                TradingServer.Agent.AgentConfig.Instance.AddNotifyToAgent(newAgentNotify);
                            }
                            catch (Exception ex)
                            {

                            }
                            #endregion

                            isEndCalculation = true;
                        }
                        #endregion

                        #region GET PREVIOUSLEDGERBALANCE = LAST BALANCE IN COMMAND HISTORY WITH TYPE = 21
                        DateTime tempTimeStart = newLastAccount.TimeEndDay.AddDays(-1);

                        if (tempTimeStart.DayOfWeek == DayOfWeek.Sunday)
                            tempTimeStart = tempTimeStart.AddDays(-2);

                        if (tempTimeStart.DayOfWeek == DayOfWeek.Saturday)
                            tempTimeStart = tempTimeStart.AddDays(-1);

                        DateTime timeStartLastBalance = new DateTime(tempTimeStart.Year, tempTimeStart.Month, tempTimeStart.Day, 00, 00, 00);
                        DateTime timeEndLastBalance = new DateTime(tempTimeStart.Year, tempTimeStart.Month, tempTimeStart.Day, 23, 59, 59);

                        TradingServer.Business.OpenTrade LastBalance = TradingServer.Facade.FacadeGetLastBalanceByInvestor(newLastAccount.InvestorAccount.InvestorID, timeStartLastBalance, 21, timeEndLastBalance);

                        if (LastBalance != null)
                            PLBalance = LastBalance.Profit;
                        #endregion

                        #region CALCULATION ACCOUNT
                        balance = PLBalance + closePL + commission + swap + deposit - withdrawal;
                        equity = balance + floatingPL + newLastAccount.InvestorAccount.Credit;
                        #endregion

                        #region CALCULATION FREE MARGIN
                        freeMargin = equity - newLastAccount.InvestorAccount.Margin;
                        #endregion

                        List<Business.OpenTrade> listOpenTradeMonth =
                                            TradingServer.Facade.FacadeGetHistoryByInvestorInMonth(newLastAccount.InvestorAccount.InvestorID);

                        double monthSize = 0;
                        if (listOpenTradeMonth != null)
                        {
                            int count = listOpenTradeMonth.Count;
                            for (int j = 0; j < count; j++)
                            {
                                monthSize += listOpenTradeMonth[j].Size;
                            }
                        }

                        #region MAP LAST ACCOUNT
                        Business.LastBalance newLastAccountEOD = new LastBalance();
                        newLastAccountEOD.InvestorID = newLastAccount.InvestorAccount.InvestorID;
                        newLastAccountEOD.LoginCode = newLastAccount.InvestorAccount.Code;
                        newLastAccountEOD.PLBalance = PLBalance;
                        newLastAccountEOD.ClosePL = closePL;
                        newLastAccountEOD.Deposit = deposit;
                        newLastAccountEOD.Balance = balance;
                        newLastAccountEOD.FloatingPL = floatingPL;
                        newLastAccountEOD.Credit = creditIn;
                        newLastAccountEOD.CreditOut = creditOut;
                        newLastAccountEOD.LastEquity = equity;
                        newLastAccountEOD.Withdrawal = withdrawal;
                        newLastAccountEOD.LogDate = TimeEndDay;
                        newLastAccountEOD.CreditAccount = newLastAccount.InvestorAccount.Credit;
                        newLastAccountEOD.LastMargin = newLastAccount.InvestorAccount.Margin;
                        newLastAccountEOD.FreeMargin = freeMargin;
                        newLastAccountEOD.MonthSize = monthSize;
                        #endregion

                        #region SEND NOTIFY LIST LAST ACCOUNT TO AGENT
                        //SEND NOTIFY LIST LAST ACCOUNT TO AGENT
                        if (isEndCalculation)
                        {
                            string message = "NotifyLastAccount$";
                            //NotifyLastAccount$InvestorID{LoginCode~CommandID{CommandCode{Profit{Commission{Swap|CommandID{CommandCode{Profit{Commission{Swap|....
                            message += newLastAccountEOD.InvestorID + "{" + newLastAccountEOD.LoginCode + "~";
                            if (newLastAccount.ListOpenTrade != null)
                            {
                                int count = newLastAccount.ListOpenTrade.Count;
                                for (int i = 0; i < count; i++)
                                {
                                    message += newLastAccount.ListOpenTrade[i].ID + "{" + newLastAccount.ListOpenTrade[i].CommandCode + "{" +
                                        newLastAccount.ListOpenTrade[i].Profit + "{" + newLastAccount.ListOpenTrade[i].Commission + "{" +
                                        newLastAccount.ListOpenTrade[i].Swap + "|";
                                }
                            }

                            if (message.EndsWith("|"))
                                message.Remove(message.Length - 1, 1);

                            Business.AgentNotify newAgentNotify = new AgentNotify();
                            newAgentNotify.NotifyMessage = message;
                            TradingServer.Agent.AgentConfig.Instance.AddNotifyToAgent(newAgentNotify);

                            //SEND NOTIFY END CALCULATION LAST ACCOUNT
                            string msgEndCalculation = "NotifyEndCalculation$";
                            Business.AgentNotify notifyEndCalculation = new AgentNotify();
                            notifyEndCalculation.NotifyMessage = msgEndCalculation;
                            TradingServer.Agent.AgentConfig.Instance.AddNotifyToAgent(notifyEndCalculation);
                        }
                        else
                        {
                            string message = "NotifyLastAccount$";
                            //NotifyLastAccount$InvestorID{LoginCode~CommandID{CommandCode{Profit{Commission{Swap|CommandID{CommandCode{Profit{Commission{Swap|....
                            message += newLastAccountEOD.InvestorID + "{" + newLastAccountEOD.LoginCode + "~";
                            if (newLastAccount.ListOpenTrade != null)
                            {
                                int count = newLastAccount.ListOpenTrade.Count;
                                for (int i = 0; i < count; i++)
                                {
                                    message += newLastAccount.ListOpenTrade[i].ID + "{" + newLastAccount.ListOpenTrade[i].CommandCode + "{" +
                                        newLastAccount.ListOpenTrade[i].Profit + "{" + newLastAccount.ListOpenTrade[i].Commission + "{" +
                                        newLastAccount.ListOpenTrade[i].Swap + "|";
                                }
                            }

                            if (message.EndsWith("|"))
                                message.Remove(message.Length - 1, 1);

                            Business.AgentNotify newAgentNotify = new AgentNotify();
                            newAgentNotify.NotifyMessage = message;
                            TradingServer.Agent.AgentConfig.Instance.AddNotifyToAgent(newAgentNotify);
                        }
                        #endregion

                        listLastBalance.Add(newLastAccountEOD);
                    }
                    newLastAccount = Business.Market.GetLastAccount();
                }

                if (listLastBalance != null && listLastBalance.Count > 0)
                    TradingServer.Facade.FacadeAddNewLastAccount(listLastBalance);

                System.Threading.Thread.Sleep(60000);
            }
        }