/// <summary>
        /// 
        /// </summary>
        public void ReCalculationTotalMargin()
        {
            if (Business.Market.InvestorList != null)
            {
                int count = Business.Market.InvestorList.Count;
                for (int i = 0; i < count; i++)
                {
                    if (Business.Market.InvestorList[i].CommandList != null && Business.Market.InvestorList[i].CommandList.Count > 0)
                    {
                        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].FreeMargin = newMargin.TotalFreezeMargin;

                        //Notify to mangager change margin
                        TradingServer.Facade.FacadeSendNotifyManagerRequest(3, Business.Market.InvestorList[i]);
                    }
                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="objInvestor"></param>
        internal bool UpdateInvestor(Business.Investor objInvestor,string ipaddress,string code)
        {
            bool Result = false;
            StringBuilder content = new StringBuilder();
            StringBuilder beforeContent = new StringBuilder();
            StringBuilder afterContent = new StringBuilder();

            content.Append("'" + code + "': update 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 == objInvestor.InvestorID)
                    {
                        content.Append("'" + Business.Market.InvestorList[i].Code + "': ");
                        using (TransactionScope ts = new TransactionScope())
                        {
                            Business.Market.InvestorList[i].InvestorStatusID = objInvestor.InvestorStatusID;

                            #region Find Investor Group
                            if (Business.Market.InvestorList[i].InvestorGroupInstance.InvestorGroupID != objInvestor.InvestorGroupInstance.InvestorGroupID)
                            {
                                beforeContent.Append("group: " + Business.Market.InvestorList[i].InvestorGroupInstance.Name + " - ");

                                if (Business.Market.InvestorGroupList != null)
                                {
                                    int countInvestorGroup = Business.Market.InvestorGroupList.Count;
                                    for (int j = 0; j < countInvestorGroup; j++)
                                    {
                                        if (Business.Market.InvestorGroupList[j].InvestorGroupID == objInvestor.InvestorGroupInstance.InvestorGroupID)
                                        {
                                            Business.Market.InvestorList[i].InvestorGroupInstance = Business.Market.InvestorGroupList[j];
                                            afterContent.Append("group: " + Business.Market.InvestorGroupList[j].Name + " - ");
                                            break;
                                        }
                                    }
                                }
                            }
                            #endregion

                            #region UPDATE PASSWORD
                            if (!string.IsNullOrEmpty(objInvestor.PrimaryPwd))
                                Business.Market.InvestorList[i].PrimaryPwd = objInvestor.PrimaryPwd;

                            if (!string.IsNullOrEmpty(objInvestor.ReadOnlyPwd))
                                Business.Market.InvestorList[i].ReadOnlyPwd = objInvestor.ReadOnlyPwd;

                            if (!string.IsNullOrEmpty(objInvestor.PhonePwd))
                                Business.Market.InvestorList[i].PhonePwd = objInvestor.PhonePwd;
                            #endregion

                            #region UPDATE PROPERTY AGENT ID
                            Business.Market.InvestorList[i].AgentID = objInvestor.AgentID;
                            #endregion

                            #region UPDATE PROPERTY ISDISABLE ACCOUNT
                            if (Business.Market.InvestorList[i].IsDisable != objInvestor.IsDisable)
                            {
                                if (Business.Market.InvestorList[i].IsDisable)
                                    beforeContent.Append("disable account: on - ");
                                else
                                    beforeContent.Append("disable account: off - ");

                                if (objInvestor.IsDisable)
                                    afterContent.Append("disable account: on - ");
                                else
                                    afterContent.Append("disable account: off - ");

                                Business.Market.InvestorList[i].IsDisable = objInvestor.IsDisable;
                            }
                            #endregion

                            #region UPDATE PROPERTY ALLOW CHANGE PASSWORD
                            if (Business.Market.InvestorList[i].AllowChangePwd != objInvestor.AllowChangePwd)
                            {
                                if (Business.Market.InvestorList[i].AllowChangePwd)
                                    beforeContent.Append("allow changed password: on - ");
                                else
                                    beforeContent.Append("allow changed password: off - ");

                                if (objInvestor.AllowChangePwd)
                                    afterContent.Append("allow changed password: on - ");
                                else
                                    afterContent.Append("allow changed password: off - ");

                                Business.Market.InvestorList[i].AllowChangePwd = objInvestor.AllowChangePwd;
                            }
                            #endregion

                            #region UPDATE PROPERTY SEND REPORT
                            if (Business.Market.InvestorList[i].SendReport != objInvestor.SendReport)
                            {
                                if (Business.Market.InvestorList[i].SendReport)
                                    beforeContent.Append("enable send report: on - ");
                                else
                                    beforeContent.Append("enable send report: off - ");

                                if (objInvestor.SendReport)
                                    afterContent.Append("enable send report: on - ");
                                else
                                    afterContent.Append("enable send report: off - ");

                                Business.Market.InvestorList[i].SendReport = objInvestor.SendReport;
                            }
                            #endregion

                            #region UPDATE PROPERTY READONLY
                            if (Business.Market.InvestorList[i].ReadOnly != objInvestor.ReadOnly)
                            {
                                if (Business.Market.InvestorList[i].ReadOnly)
                                    beforeContent.Append("enable read only account: on - ");
                                else
                                    beforeContent.Append("enable read only account: off - ");

                                if (objInvestor.ReadOnly)
                                    afterContent.Append("enable read only account: on - ");
                                else
                                    afterContent.Append("enalbe read only account: off - ");

                                Business.Market.InvestorList[i].ReadOnly = objInvestor.ReadOnly;
                            }
                            #endregion

                            #region UPDATE PROPERTY INVESTOR COMMENT
                            if (Business.Market.InvestorList[i].InvestorComment != objInvestor.InvestorComment)
                            {
                                beforeContent.Append("investor comment: " + Business.Market.InvestorList[i].InvestorComment + " - ");
                                afterContent.Append("investor comment: " + objInvestor.InvestorComment + " - ");

                                Business.Market.InvestorList[i].InvestorComment = objInvestor.InvestorComment;
                            }
                            #endregion

                            #region UPDATE PROPERTY LEVERAGE
                            if (Business.Market.InvestorList[i].Leverage != objInvestor.Leverage)
                            {
                                beforeContent.Append("leverage: " + Business.Market.InvestorList[i].Leverage + " - ");
                                afterContent.Append("leverage: " + objInvestor.Leverage + " - ");

                                Business.Market.InvestorList[i].Leverage = objInvestor.Leverage;

                                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 < countCommand; j++)
                                    {
                                        Business.Market.InvestorList[i].CommandList[j].CalculatorMarginCommand(Business.Market.InvestorList[i].CommandList[j]);
                                    }

                                    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;

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

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

                            #region UPDATE PROPERTY TAXRATE
                            if (Business.Market.InvestorList[i].TaxRate != objInvestor.TaxRate)
                            {
                                beforeContent.Append("taxrate: " + Business.Market.InvestorList[i].TaxRate + " - ");
                                afterContent.Append("taxrate: " + objInvestor.TaxRate + " - ");

                                Business.Market.InvestorList[i].TaxRate = objInvestor.TaxRate;
                            }
                            #endregion

                            #region UPDATE PROPERTY IDPASSPORT
                            if (Business.Market.InvestorList[i].IDPassport != objInvestor.IDPassport)
                            {
                                beforeContent.Append("ID passport: " + Business.Market.InvestorList[i].IDPassport + " - ");
                                afterContent.Append("ID passport: " + objInvestor.IDPassport + " - ");

                                Business.Market.InvestorList[i].IDPassport = objInvestor.IDPassport;
                            }
                            #endregion

                            Investor.DBWInvestorInstance.UpdateInvestor(objInvestor);

                            ts.Complete();
                            Result = true;

                            //NOTIFY TO MANAGER THEN INVESTOR ACCOUNT UPDATE
                            TradingServer.Facade.FacadeSendNotifyManagerRequest(3, Business.Market.InvestorList[i]);

                            #region IF COMMAND LIST IN INVESTOR > 0 THEN UPDATE INVESTOR GROUP FALSE(DMD)
                            //if (Business.Market.InvestorList[i].CommandList.Count > 0)
                            //{
                            //    content.Append("[falied] exists open postion");
                            //    TradingServer.Facade.FacadeAddNewSystemLog(3, content.ToString(), "[update investor]", ipaddress, code);

                            //    return false;
                            //}
                            //else
                            //{

                            //}
                            #endregion
                        }

                        break;
                    }
                }
            }

            if (Result == false)
            {
                Investor.DBWInvestorInstance.UpdateInvestor(objInvestor);
                Result = true;
            }

            string tempBeforeContent = beforeContent.ToString();
            if (tempBeforeContent.EndsWith(" - "))
                tempBeforeContent = tempBeforeContent.Remove(tempBeforeContent.Length - 2, 2);

            string tempAftertConten = afterContent.ToString();
            if (tempAftertConten.EndsWith(" - "))
                tempAftertConten = tempAftertConten.Remove(tempAftertConten.Length - 2, 2);

            if (!string.IsNullOrEmpty(tempBeforeContent.Trim()) && !string.IsNullOrEmpty(tempAftertConten.Trim()))
            {
                content.Append(tempBeforeContent + " -> " + tempAftertConten);
                TradingServer.Facade.FacadeAddNewSystemLog(3, content.ToString(), "[update investor]", ipaddress, code);
            }

            return Result;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="objInvestor"></param>
        internal bool UpdateInvestor(Business.Investor objInvestor)
        {
            bool Result = false;
            StringBuilder content = new StringBuilder();
            StringBuilder beforeContent = new StringBuilder();
            StringBuilder afterContent = new StringBuilder();

            if (Business.Market.InvestorList != null)
            {
                int count = Business.Market.InvestorList.Count;
                for (int i = 0; i < count; i++)
                {
                    if (Business.Market.InvestorList[i].InvestorID == objInvestor.InvestorID)
                    {
                        using (TransactionScope ts = new TransactionScope())
                        {
                            Business.Market.InvestorList[i].InvestorStatusID = objInvestor.InvestorStatusID;

                            #region Find Investor Group
                            if (Business.Market.InvestorList[i].InvestorGroupInstance.InvestorGroupID != objInvestor.InvestorGroupInstance.InvestorGroupID)
                            {
                                #region IF COMMAND LIST IN INVESTOR > 0 THEN UPDATE INVESTOR GROUP FALSE(DMD)
                                if (Business.Market.InvestorList[i].CommandList.Count > 0)
                                {
                                    return false;
                                }
                                #endregion

                                beforeContent.Append("group: " + Business.Market.InvestorList[i].InvestorGroupInstance.Name + " - ");

                                if (Business.Market.InvestorGroupList != null)
                                {
                                    int countInvestorGroup = Business.Market.InvestorGroupList.Count;
                                    for (int j = 0; j < countInvestorGroup; j++)
                                    {
                                        if (Business.Market.InvestorGroupList[j].InvestorGroupID == objInvestor.InvestorGroupInstance.InvestorGroupID)
                                        {
                                            Business.Market.InvestorList[i].InvestorGroupInstance = Business.Market.InvestorGroupList[j];
                                            beforeContent.Append("group: " + Business.Market.InvestorGroupList[j].Name + " - ");
                                            break;
                                        }
                                    }
                                }
                            }
                            #endregion

                            if (!string.IsNullOrEmpty(objInvestor.PrimaryPwd))
                                Business.Market.InvestorList[i].PrimaryPwd = objInvestor.PrimaryPwd;

                            if (!string.IsNullOrEmpty(objInvestor.ReadOnlyPwd))
                                Business.Market.InvestorList[i].ReadOnlyPwd = objInvestor.ReadOnlyPwd;

                            if (!string.IsNullOrEmpty(objInvestor.PhonePwd))
                                Business.Market.InvestorList[i].PhonePwd = objInvestor.PhonePwd;

                            Business.Market.InvestorList[i].AgentID = objInvestor.AgentID;

                            if (Business.Market.InvestorList[i].IsDisable != objInvestor.IsDisable)
                            {
                                if (Business.Market.InvestorList[i].IsDisable)
                                    beforeContent.Append("disable account: on");
                                else
                                    beforeContent.Append("disable account: off");

                                if (objInvestor.IsDisable)
                                    afterContent.Append("disable account: on");
                                else
                                    afterContent.Append("disable account: off");

                                Business.Market.InvestorList[i].IsDisable = objInvestor.IsDisable;
                            }

                            if (Business.Market.InvestorList[i].AllowChangePwd != objInvestor.AllowChangePwd)
                            {
                                if (Business.Market.InvestorList[i].AllowChangePwd)
                                    beforeContent.Append("allow changed password: on - ");
                                else
                                    beforeContent.Append("allow changed password: off - ");

                                if (objInvestor.AllowChangePwd)
                                    afterContent.Append("allow changed password: on - ");
                                else
                                    afterContent.Append("allow changed password: off - ");

                                Business.Market.InvestorList[i].AllowChangePwd = objInvestor.AllowChangePwd;
                            }

                            if (Business.Market.InvestorList[i].SendReport != objInvestor.SendReport)
                            {
                                if (Business.Market.InvestorList[i].SendReport)
                                    beforeContent.Append("enable send report: on - ");
                                else
                                    beforeContent.Append("enable send report: off - ");

                                if (objInvestor.SendReport)
                                    afterContent.Append("enable send report: on - ");
                                else
                                    afterContent.Append("enable send report: off - ");

                                Business.Market.InvestorList[i].SendReport = objInvestor.SendReport;
                            }

                            if (Business.Market.InvestorList[i].ReadOnly != objInvestor.ReadOnly)
                            {
                                if (Business.Market.InvestorList[i].ReadOnly)
                                    beforeContent.Append("enable read only account: on - ");
                                else
                                    beforeContent.Append("enable read only account: off - ");

                                if (objInvestor.ReadOnly)
                                    afterContent.Append("enable read only account: on - ");
                                else
                                    afterContent.Append("enalbe read only account: off - ");

                                Business.Market.InvestorList[i].ReadOnly = objInvestor.ReadOnly;
                            }

                            if (Business.Market.InvestorList[i].InvestorComment != objInvestor.InvestorComment)
                            {
                                beforeContent.Append("investor command: " + Business.Market.InvestorList[i].InvestorComment + " - ");
                                afterContent.Append("investor command: " + objInvestor.InvestorComment + " - ");

                                Business.Market.InvestorList[i].InvestorComment = objInvestor.InvestorComment;
                            }

                            if (Business.Market.InvestorList[i].Leverage != objInvestor.Leverage)
                            {
                                beforeContent.Append("leverage: " + Business.Market.InvestorList[i].Leverage + " - ");
                                afterContent.Append("leverage: " + objInvestor.Leverage + " - ");

                                Business.Market.InvestorList[i].Leverage = objInvestor.Leverage;

                                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 < countCommand; j++)
                                    {
                                        Business.Market.InvestorList[i].CommandList[j].CalculatorMarginCommand(Business.Market.InvestorList[i].CommandList[j]);
                                    }

                                    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;

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

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

                            if (Business.Market.InvestorList[i].TaxRate != objInvestor.TaxRate)
                            {
                                beforeContent.Append("taxrate: " + Business.Market.InvestorList[i].TaxRate + " - ");
                                afterContent.Append("taxrate: " + objInvestor.TaxRate + " - ");

                                Business.Market.InvestorList[i].TaxRate = objInvestor.TaxRate;
                            }

                            if (Business.Market.InvestorList[i].IDPassport != objInvestor.IDPassport)
                            {
                                beforeContent.Append("ID passport: " + Business.Market.InvestorList[i].IDPassport + " - ");
                                afterContent.Append("ID passport: " + objInvestor.IDPassport + " - ");

                                Business.Market.InvestorList[i].IDPassport = objInvestor.IDPassport;
                            }

                            Investor.DBWInvestorInstance.UpdateInvestor(objInvestor);

                            ts.Complete();
                            Result = true;

                            //NOTIFY TO MANAGER THEN INVESTOR ACCOUNT UPDATE
                            TradingServer.Facade.FacadeSendNotifyManagerRequest(3, Business.Market.InvestorList[i]);
                        }

                        break;
                    }
                }
            }

            if (Result == false)
            {
                Investor.DBWInvestorInstance.UpdateInvestor(objInvestor);
                Result = true;
            }

            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="InvestorID"></param>
        /// <param name="Money"></param>
        /// <returns></returns>
        public bool WithDrawals(int InvestorID, double Money, string Comment)
        {
            bool Result = false;

            Money = Math.Round(Money, 2);

            if (Money <= 0)
                return false;

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

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

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

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

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

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

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

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

                                                    //Add Command To Command History

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

                                                    j--;
                                                }
                                            }
                                        }

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

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

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

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

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

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

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

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

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

                                        Result = true;

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

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

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

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

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

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

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

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

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

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

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

                                    Result = true;

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

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

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

                        break;
                    }
                }
            }

            return Result;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Command"></param>
        /// <returns></returns>
        public bool CheckValidAccountInvestor(Business.OpenTrade Command)
        {
            bool Result = false;
            if (Command != null)
            {
                //Calculator Margin Level
                double tempMarginLevel = 0;
                double tempProfit = 0;
                double tempTotalMargin = 0;
                double tempEquity = 0;
                double tempFreeMargin = 0;
                double marginPending = 0;
                double marginCommand = 0;
                bool isExits = false;

                List<Business.OpenTrade> tempOpenTrade = new List<OpenTrade>();
                List<Business.OpenTrade> listPending = new List<OpenTrade>();
                if (Command.Investor.CommandList != null)
                {
                    int count = Command.Investor.CommandList.Count;
                    for (int i = 0; i < count; i++)
                    {
                        if (Command.Investor.CommandList[i].ID == Command.ID)
                            isExits = true;

                        bool isPending = TradingServer.Model.TradingCalculate.Instance.CheckIsPendingPosition(Command.Investor.CommandList[i].Type.ID);
                        if (isPending)
                        {
                            Command.CalculatorMarginCommand(Command.Investor.CommandList[i]);
                            listPending.Add(Command.Investor.CommandList[i]);
                        }
                        else
                        {
                            Command.CalculatorMarginCommand(Command.Investor.CommandList[i]);
                            tempOpenTrade.Add(Command.Investor.CommandList[i]);
                        }

                        //if (Command.Investor.CommandList[i].Type.ID == 7 ||
                        //    Command.Investor.CommandList[i].Type.ID == 8 ||
                        //    Command.Investor.CommandList[i].Type.ID == 9 ||
                        //    Command.Investor.CommandList[i].Type.ID == 10 ||
                        //    Command.Investor.CommandList[i].Type.ID == 17 ||
                        //    Command.Investor.CommandList[i].Type.ID == 18 ||
                        //    Command.Investor.CommandList[i].Type.ID == 19 ||
                        //    Command.Investor.CommandList[i].Type.ID == 20)
                        //{
                        //    if (Command.Type.ID == 7 || Command.Type.ID == 8 || Command.Type.ID == 9 || Command.Type.ID == 10 ||
                        //        Command.Type.ID == 17 || Command.Type.ID == 18 || Command.Type.ID == 19 || Command.Type.ID == 20)
                        //    {
                        //        Command.CalculatorMarginCommand(Command.Investor.CommandList[i]);
                        //        listPending.Add(Command.Investor.CommandList[i]);
                        //        marginPending += Command.Investor.CommandList[i].Margin;
                        //    }
                        //}
                        //else
                        //{
                        //tempOpenTrade.Add(Command.Investor.CommandList[i]);
                        //}
                    }
                }

                Command.CalculatorMarginCommand(Command);

                if (!isExits)
                {
                    bool isPendingCommand = TradingServer.Model.TradingCalculate.Instance.CheckIsPendingPosition(Command.Type.ID);
                    if (isPendingCommand)
                        listPending.Add(Command);
                    else
                        tempOpenTrade.Add(Command);
                }

                //Business.Margin newMargin = new Business.Margin();
                //newMargin = Command.Symbol.CalculationTotalMargin(tempOpenTrade);
                ////marginCommand = newMargin.TotalMargin + newMargin.TotalFreezeMargin;
                //marginCommand = newMargin.TotalFreezeMargin;

                Business.Margin newMargin = new Business.Margin();
                newMargin = Command.Symbol.CalculationTotalMarginPending(listPending, tempOpenTrade);
                //tempTotalMargin = marginCommand + marginPending;
                tempTotalMargin = newMargin.TotalMargin + newMargin.TotalFreezeMargin;

                //if (Command.Type.ID == 7 || Command.Type.ID == 8 || Command.Type.ID == 9 || Command.Type.ID == 10 ||
                //    Command.Type.ID == 17 || Command.Type.ID == 18 || Command.Type.ID == 19 || Command.Type.ID == 20)
                //{
                //    tempTotalMargin = marginCommand + Command.Margin + marginPending;
                //}
                //else
                //{
                //    tempTotalMargin = marginCommand;
                //}

                tempProfit = Command.Investor.Profit + Command.Profit;
                tempEquity = Command.Investor.Balance + Command.Investor.Credit + tempProfit;
                tempFreeMargin = tempEquity - tempTotalMargin;
                tempMarginLevel = (tempEquity * 100) / tempTotalMargin;

                if (tempMarginLevel >= 100)
                {
                    Result = true;
                }
                else
                {
                    Result = false;
                }
            }

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