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

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

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

                        #region CLOSE COMMAND

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

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

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

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

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

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

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

                                this.Balance += totalProfit;

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

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

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

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

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

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

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

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

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

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

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

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

                        #endregion

                        result = true;
                        break;
                    }
                }
            }

            return result;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="listCommand"></param>
        /// <returns></returns>
        internal Margin CalculationTotalMargin(List<Business.OpenTrade> nlistCommand)
        {
            Margin result = new Margin();
            result.TotalMargin = 0;
            result.TotalFreezeMargin = 0;
            if (nlistCommand == null || nlistCommand.Count == 0)
            {
                return result;
            }

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

            #region for 1
            for (int i = 0; i < nlistCommand.Count; i++)
            {
                if (nlistCommand[i].Type.ID != 3 & nlistCommand[i].Type.ID != 4 & nlistCommand[i].Type.ID != 7 & nlistCommand[i].Type.ID != 8 &
                    nlistCommand[i].Type.ID != 9 & nlistCommand[i].Type.ID != 10 && nlistCommand[i].Type.ID != 17 && nlistCommand[i].Type.ID != 18 &&
                    nlistCommand[i].Type.ID != 19 && nlistCommand[i].Type.ID != 20 && !nlistCommand[i].IsClose)
                {
                    Business.OpenTrade newOpenTrade = new Business.OpenTrade();
                    newOpenTrade.ClosePrice = nlistCommand[i].ClosePrice;
                    //newOpenTrade.CloseTime = nlistCommand[i].CloseTime;
                    //newOpenTrade.ExpTime = nlistCommand[i].ExpTime;
                    newOpenTrade.ID = nlistCommand[i].ID;
                    newOpenTrade.Investor = nlistCommand[i].Investor;
                    //newOpenTrade.OpenPrice = nlistCommand[i].OpenPrice;
                    //newOpenTrade.OpenTime = nlistCommand[i].OpenTime;
                    newOpenTrade.Size = nlistCommand[i].Size;
                    //newOpenTrade.StopLoss = nlistCommand[i].StopLoss;
                    newOpenTrade.Symbol = new Symbol();
                    newOpenTrade.Symbol = nlistCommand[i].Symbol;
                    //newOpenTrade.TakeProfit = nlistCommand[i].TakeProfit;
                    //newOpenTrade.NumberUpdate = nlistCommand[i].NumberUpdate;
                    newOpenTrade.Type = new TradeType();
                    newOpenTrade.Type = nlistCommand[i].Type;
                    //newOpenTrade.ClientCode = nlistCommand[i].ClientCode;
                    //newOpenTrade.IsClose = nlistCommand[i].IsClose;
                    //newOpenTrade.Profit = nlistCommand[i].Profit;
                    //newOpenTrade.Swap = nlistCommand[i].Swap;
                    //newOpenTrade.Commission = nlistCommand[i].Commission;
                    //newOpenTrade.CommandCode = nlistCommand[i].CommandCode;
                    newOpenTrade.Margin = nlistCommand[i].Margin;
                    newOpenTrade.IsHedged = nlistCommand[i].Symbol.IsHedged;
                    listCommand.Add(newOpenTrade);
                }
            }
            #endregion
            double margin, lots, change;
            if (listCommand.Count > 0)
            {
                double leverage = GetLeverageSymbol(listCommand[0]);
                if (leverage >= 0) leverage = listCommand[0].Investor.Leverage;

                for (int i = 0; i < listCommand.Count; i++)
                {
                    if ((listCommand[i].Type.ID % 2 != 0) && listCommand[i].Size != 0 && listCommand[i].IsHedged)
                    {
                        for (int f = 0; f < listCommand.Count; f++)
                        {
                            #region Non FreezeMargin
                            if ((listCommand[f].Symbol.Name == listCommand[i].Symbol.Name) &&
                                (listCommand[f].Type.ID == listCommand[i].Type.ID + 1) && (listCommand[f].Size != 0) && listCommand[i].IsHedged && !listCommand[i].Symbol.UseFreezeMargin)
                            {
                                //result += listCommand[i].Margin;
                                change = Math.Abs(listCommand[i].Size - listCommand[f].Size);
                                if (change == 0)
                                {
                                    lots = listCommand[i].Size + listCommand[f].Size;
                                    result.TotalMargin += CalculationMarginHedged(leverage, listCommand[i].Symbol.MarginHedged, lots, listCommand[i].Symbol);
                                    listCommand[f].Size = 0;
                                    listCommand[f].Margin = 0;
                                    listCommand[i].Size = 0;
                                    listCommand[i].Margin = 0;
                                    break;
                                }
                                else
                                {
                                    if (listCommand[i].Size > listCommand[f].Size)
                                    {
                                        margin = (listCommand[i].Margin / listCommand[i].Size) * change;
                                        listCommand[i].Margin = margin;
                                        listCommand[i].Size = change;
                                        lots = listCommand[f].Size * 2;
                                        result.TotalMargin += CalculationMarginHedged(leverage, listCommand[i].Symbol.MarginHedged, lots, listCommand[i].Symbol);
                                        listCommand[f].Size = 0;
                                    }
                                    else
                                    {
                                        margin = (listCommand[f].Margin / listCommand[f].Size) * change;
                                        listCommand[f].Margin = margin;
                                        listCommand[f].Size = change;
                                        lots = listCommand[i].Size * 2;
                                        result.TotalMargin += CalculationMarginHedged(leverage, listCommand[i].Symbol.MarginHedged, lots, listCommand[i].Symbol);
                                        listCommand[i].Size = 0;
                                        break;
                                    }
                                }
                            }
                            #endregion

                            #region FreezeMargin
                            if ((listCommand[f].Symbol.Name == listCommand[i].Symbol.Name) &&
                               (listCommand[f].Type.ID == listCommand[i].Type.ID + 1) && (listCommand[f].Size != 0) && listCommand[i].IsHedged && listCommand[i].Symbol.UseFreezeMargin)
                            {
                                //result += listCommand[i].Margin;
                                change = Math.Abs(listCommand[i].Size - listCommand[f].Size);
                                if (change == 0)
                                {
                                    lots = listCommand[i].Size + listCommand[f].Size;
                                    result.TotalFreezeMargin += this.CalculationFreezeMarginHedged(leverage, listCommand[i].Symbol.MarginHedged, lots, listCommand[i].Symbol.FreezeMarginHedged, listCommand[i].Symbol);
                                    listCommand[f].Size = 0;
                                    listCommand[f].Margin = 0;
                                    listCommand[i].Size = 0;
                                    listCommand[i].Margin = 0;
                                    break;
                                }
                                else
                                {
                                    if (listCommand[i].Size > listCommand[f].Size)
                                    {
                                        margin = (listCommand[i].Margin / listCommand[i].Size) * change;
                                        listCommand[i].Margin = margin;
                                        listCommand[i].Size = change;
                                        lots = listCommand[f].Size * 2;
                                        result.TotalFreezeMargin += CalculationFreezeMarginHedged(leverage, listCommand[i].Symbol.MarginHedged, lots, listCommand[i].Symbol.FreezeMarginHedged, listCommand[i].Symbol);
                                        listCommand[f].Size = 0;
                                    }
                                    else
                                    {
                                        margin = (listCommand[f].Margin / listCommand[f].Size) * change;
                                        listCommand[f].Margin = margin;
                                        listCommand[f].Size = change;
                                        lots = listCommand[i].Size * 2;
                                        result.TotalFreezeMargin += CalculationFreezeMarginHedged(leverage, listCommand[i].Symbol.MarginHedged, lots, listCommand[i].Symbol.FreezeMarginHedged, listCommand[i].Symbol);
                                        listCommand[i].Size = 0;
                                        break;
                                    }
                                }
                            }
                            #endregion
                        }
                    }
                }

                for (int i = 0; i < listCommand.Count; i++)
                {
                    if (listCommand[i].Size != 0)
                    {
                        if (!listCommand[i].Symbol.UseFreezeMargin)
                        {
                            result.TotalMargin += listCommand[i].Margin;
                        }else
                        {
                            result.TotalFreezeMargin += listCommand[i].Margin;
                        }
                    }
                }
            }
            return result;
        }
예제 #3
0
        /// <summary>
        /// Re Calculation Account Then Init Server
        /// </summary>
        internal void ReCalculationAccountInit()
        {
            this.IsCalculating = true;

            if (this.CommandList != null)
            {
                if (this.CommandList.Count > 0 && this.CommandList != null)
                {
                    //RECALCULATION PROFIT OF COMMAND WITH TICK ONLINE IS GET FROM CANDLES 1M
                    for (int i = 0; i < this.CommandList.Count; i++)
                    {
                        bool isPending = TradingServer.Model.TradingCalculate.Instance.CheckIsPendingPosition(this.CommandList[i].Type.ID);
                        if (!isPending)
                        {
                            this.CommandList[i].CalculatorProfitCommand(this.CommandList[i]);
                            this.CommandList[i].Profit = this.CommandList[i].Symbol.ConvertCurrencyToUSD(this.CommandList[i].Symbol.Currency, this.CommandList[i].Profit, false,
                                this.CommandList[i].SpreaDifferenceInOpenTrade, this.CommandList[i].Symbol.Digit);

                            this.CommandList[i].CalculatorMarginCommand(this.CommandList[i]);
                        }

                        //if (this.CommandList[i].Type.ID != 3 && this.CommandList[i].Type.ID != 4 &&
                        //    this.CommandList[i].Type.ID != 7 && this.CommandList[i].Type.ID != 8 &&
                        //    this.CommandList[i].Type.ID != 9 && this.CommandList[i].Type.ID != 10 &&
                        //    this.CommandList[i].Type.ID != 17 && this.CommandList[i].Type.ID != 18 &&
                        //    this.CommandList[i].Type.ID != 19 && this.CommandList[i].Type.ID != 20)
                        //{

                        //}
                    }

                    Business.Margin newMargin = new Margin();
                    newMargin = this.CommandList[0].Symbol.CalculationTotalMargin(this.CommandList);
                    this.Margin = newMargin.TotalMargin;
                    this.FreezeMargin = newMargin.TotalFreezeMargin;

                    this.Profit = this.InvestorGroupInstance.CalculationTotalProfit(this.CommandList);
                    this.Equity = this.Balance + this.Credit + this.Profit;
                    double Loss = 0;
                    double Profit = 0;
                    int Method = -1;

                    switch (this.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;
                            Profit = this.InvestorGroupInstance.CalculationTotalProfitPositive(this.CommandList);
                            break;
                        case "use unrealized loss only":
                            Method = 3;
                            Loss = this.InvestorGroupInstance.CalculationTotalLoss(this.CommandList);
                            break;
                    }

                    double totalMargin = this.Margin + this.FreezeMargin;
                    this.FreeMargin = this.InvestorGroupInstance.CalculationTotalFreeMargin(totalMargin, this.Balance, this.Equity, Profit, Loss, Method);
                    this.MarginLevel = (this.Equity * 100) / (this.Margin + this.FreezeMargin);
                }
                else
                {
                    this.Margin = 0;
                    this.FreeMargin = 0;
                    this.MarginLevel = 0;
                    this.Profit = 0;
                }
            }

            this.IsCalculating = false;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Command"></param>
        private void ProcessSetTask()
        {
            Investor.IsInProcess = true;
            this.isInTask = true;

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

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

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

                    this.UpdateCommands.RemoveAt(0);

                    if (Command == null)
                        continue;

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

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

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

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

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

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

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

                                                    string msg = "CloseCommandByManager$True,Close Command Complete," + Command.ID + "," + Command.Investor.InvestorID + "," + Command.Symbol.Name + "," +
                                                        Command.Size + "," + IsBuy + "," + Command.OpenTime + "," + Command.OpenPrice + "," + Command.StopLoss + "," + Command.TakeProfit + "," +
                                                        Command.ClosePrice + "," + Command.Commission + "," + Command.Swap + "," + Command.Profit + "," + "Comment," + Command.ID + "," + Command.Type.Name + "," +
                                                        1 + "," + Command.ExpTime + "," + Command.ClientCode + "," + Command.CommandCode + "," + Command.IsHedged + "," + Command.Type.ID + "," + Command.Margin +
                                                        ",Close";

                                                    string msgNotify = "CloseCommandByManager$True,Close Command Complete," + Command.ID + "," + Command.Investor.InvestorID + "," + Command.Symbol.Name + "," +
                                                        Command.Size + "," + IsBuy + "," + Command.OpenTime + "," + Command.OpenPrice + "," + Command.StopLoss + "," + Command.TakeProfit + "," +
                                                        Command.ClosePrice + "," + Command.Commission + "," + Command.Swap + "," + Command.Profit + "," + "Comment," + Command.ID + "," + Command.Type.Name + "," +
                                                        1 + "," + Command.ExpTime + "," + Command.ClientCode + "," + Command.CommandCode + "," + Command.IsHedged + "," + Command.Type.ID + "," + Command.Margin +
                                                        ",Close," + Command.AgentRefConfig + "," + Command.SpreaDifferenceInOpenTrade;

                                                    //SEND COMMAND TO AGENT SERVER
                                                    Business.AgentNotify newAgentNotify = new AgentNotify();
                                                    newAgentNotify.NotifyMessage = msgNotify;
                                                    TradingServer.Agent.AgentConfig.Instance.AddNotifyToAgent(newAgentNotify, Command.Investor.InvestorGroupInstance);
                                                }
                                                else
                                                {
                                                    Message = "CloseCommand$True,Close Command Complete," + Command.ID + "," + Command.Investor.InvestorID + "," + Command.Symbol.Name + "," +
                                                        Command.Size + "," + IsBuy + "," + Command.OpenTime + "," + Command.OpenPrice + "," + Command.StopLoss + "," + Command.TakeProfit + "," +
                                                        Command.ClosePrice + "," + Command.Commission + "," + Command.Swap + "," + Command.Profit + "," + "Comment," + Command.ID + "," + Command.Type.Name + "," +
                                                        1 + "," + Command.ExpTime + "," + Command.ClientCode + "," + Command.CommandCode + "," + Command.IsHedged + "," + Command.Type.ID + "," + Command.Margin +
                                                        ",Close," + Command.CloseTime;

                                                    string msg = "CloseCommandByManager$True,Close Command Complete," + Command.ID + "," + Command.Investor.InvestorID + "," + Command.Symbol.Name + "," +
                                                        Command.Size + "," + IsBuy + "," + Command.OpenTime + "," + Command.OpenPrice + "," + Command.StopLoss + "," + Command.TakeProfit + "," +
                                                        Command.ClosePrice + "," + Command.Commission + "," + Command.Swap + "," + Command.Profit + "," + "Comment," + Command.ID + "," + Command.Type.Name + "," +
                                                        1 + "," + Command.ExpTime + "," + Command.ClientCode + "," + Command.CommandCode + "," + Command.IsHedged + "," + Command.Type.ID + "," + Command.Margin +
                                                        ",Close";

                                                    string msgNotify = "CloseCommandByManager$True,Close Command Complete," + Command.ID + "," + Command.Investor.InvestorID + "," + Command.Symbol.Name + "," +
                                                         Command.Size + "," + IsBuy + "," + Command.OpenTime + "," + Command.OpenPrice + "," + Command.StopLoss + "," + Command.TakeProfit + "," +
                                                         Command.ClosePrice + "," + Command.Commission + "," + Command.Swap + "," + Command.Profit + "," + "Comment," + Command.ID + "," + Command.Type.Name + "," +
                                                         1 + "," + Command.ExpTime + "," + Command.ClientCode + "," + Command.CommandCode + "," + Command.IsHedged + "," + Command.Type.ID + "," + Command.Margin +
                                                         ",Close," + Command.AgentRefConfig + "," + Command.SpreaDifferenceInOpenTrade;

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

                                                this.ClientCommandQueue.Add(Message);
                                                #endregion

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

                                                            break;
                                                        }
                                                    }
                                                }
                                                #endregion

                                                int commandId = Command.ID;

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

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

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

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

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

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

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

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

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

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

                                                string msg = string.Empty;

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

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

                                                    string msgNotify = "CloseCommandByManager$True,Close Command Complete," + Command.ID + "," + Command.Investor.InvestorID + "," + Command.Symbol.Name + "," +
                                                        Command.Size + "," + IsBuy + "," + Command.OpenTime + "," + Command.OpenPrice + "," + Command.StopLoss + "," + Command.TakeProfit + "," +
                                                        Command.ClosePrice + "," + Command.Commission + "," + Command.Swap + "," + Command.Profit + "," + "Comment," + Command.ID + "," + Command.Type.Name + "," +
                                                        1 + "," + Command.ExpTime + "," + Command.ClientCode + "," + Command.CommandCode + "," + Command.IsHedged + "," + Command.Type.ID + "," + Command.Margin +
                                                        ",Close," + Command.AgentRefConfig + "," + Command.SpreaDifferenceInOpenTrade;

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

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

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

                                                    string msgNotify = "CloseCommand$True,Close Command Complete," + Command.ID + "," + Command.Investor.InvestorID + "," + Command.Symbol.Name + "," +
                                                        Command.Size + "," + IsBuy + "," + Command.OpenTime + "," + Command.OpenPrice + "," + Command.StopLoss + "," + Command.TakeProfit + "," +
                                                        Command.ClosePrice + "," + Command.Commission + "," + Command.Swap + "," + Command.Profit + "," + "Comment," + Command.ID + "," + Command.Type.Name + "," +
                                                        1 + "," + Command.ExpTime + "," + Command.ClientCode + "," + Command.CommandCode + "," + Command.IsHedged + "," + Command.Type.ID + "," + Command.Margin +
                                                        ",Close," + Command.AgentRefConfig + "," + Command.SpreaDifferenceInOpenTrade;

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

                                                    msg = Message;
                                                }
                                                #endregion

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

                                                                            break;
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                            #endregion
                                                            #endregion

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

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

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

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

                                                                        break;
                                                                    }
                                                                }
                                                            }
                                                            #endregion

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

                                                                        break;
                                                                    }
                                                                }
                                                            }
                                                            #endregion

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

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

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

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

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

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

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

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

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

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

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

                                                            //}

                                                            this.CommandList[i].IsProcess = false;

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

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

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

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

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

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

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

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

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

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

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

                                                            string Message = "CloseCommand$True,Close Command Complete," + Command.ID + "," + Command.Investor.InvestorID + "," + Command.Symbol.Name + "," +
                                                                Command.Size + "," + IsBuy + "," + Command.OpenTime + "," + Command.OpenPrice + "," + Command.StopLoss + "," + Command.TakeProfit + "," +
                                                                Command.ClosePrice + "," + Command.Commission + "," + Command.Swap + "," + Command.Profit + "," + "Comment," + Command.ID + "," + Command.Type.Name + "," +
                                                                1 + "," + Command.ExpTime + "," + Command.ClientCode + "," + Command.CommandCode + "," + Command.IsHedged + "," + Command.Type.ID + "," + Command.Margin + ",Close," +
                                                                DateTime.Now;

                                                            this.ClientCommandQueue.Add(Message);
                                                            #endregion

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

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

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

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

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

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

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

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

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

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

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

                                            #endregion
                                        }

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

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

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

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

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

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

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

                    Command = null;
                }
                catch (Exception ex)
                {

                }
                #endregion
            }

            this.isInTask = false;
            Investor.IsInProcess = false;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="nlistCommand"></param>
        /// <returns></returns>
        internal Margin CalculationTotalMarginPending(List<Business.OpenTrade> nlistCommand, List<Business.OpenTrade> nlistSpotCommand)
        {
            Margin result = new Margin();
            result.TotalMargin = 0;
            result.TotalFreezeMargin = 0;
            if (nlistCommand.Count == 0 && nlistSpotCommand.Count == 0)
            {
                return result;
            }

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

            #region for 1
            for (int i = 0; i < nlistCommand.Count; i++)
            {
                if (nlistCommand[i].Type.ID != 3 & nlistCommand[i].Type.ID != 4 && nlistCommand[i].Type.ID != 1 && nlistCommand[i].Type.ID != 2
                    && nlistCommand[i].Type.ID != 17 && nlistCommand[i].Type.ID != 18 &&
                    nlistCommand[i].Type.ID != 19 && nlistCommand[i].Type.ID != 20 && !nlistCommand[i].IsClose)
                {
                    Business.OpenTrade newOpenTrade = new Business.OpenTrade();
                    newOpenTrade.ClosePrice = nlistCommand[i].ClosePrice;
                    newOpenTrade.ID = nlistCommand[i].ID;
                    newOpenTrade.Investor = nlistCommand[i].Investor;
                    newOpenTrade.Size = nlistCommand[i].Size;
                    newOpenTrade.Symbol = new Symbol();
                    newOpenTrade.Symbol = nlistCommand[i].Symbol;
                    newOpenTrade.Type = new TradeType();
                    newOpenTrade.Type.ID = Business.Symbol.ConvertCommandIsBuySell(nlistCommand[i].Type.ID);
                    newOpenTrade.Margin = nlistCommand[i].Margin;
                    newOpenTrade.IsHedged = nlistCommand[i].Symbol.IsHedged;
                    listCommand.Add(newOpenTrade);
                }
            }
            for (int i = 0; i < nlistSpotCommand.Count; i++)
            {
                if (nlistSpotCommand[i].Type.ID != 3 & nlistSpotCommand[i].Type.ID != 4
                    && nlistSpotCommand[i].Type.ID != 17 && nlistSpotCommand[i].Type.ID != 18 &&
                    nlistSpotCommand[i].Type.ID != 19 && nlistSpotCommand[i].Type.ID != 20 && !nlistSpotCommand[i].IsClose)
                {
                    Business.OpenTrade newOpenTrade = new Business.OpenTrade();
                    newOpenTrade.ClosePrice = nlistSpotCommand[i].ClosePrice;
                    newOpenTrade.ID = nlistSpotCommand[i].ID;
                    newOpenTrade.Investor = nlistSpotCommand[i].Investor;
                    newOpenTrade.Size = nlistSpotCommand[i].Size;
                    newOpenTrade.Symbol = new Symbol();
                    newOpenTrade.Symbol = nlistSpotCommand[i].Symbol;
                    newOpenTrade.Type = new TradeType();
                    newOpenTrade.Type.ID = Business.Symbol.ConvertCommandIsBuySell(nlistSpotCommand[i].Type.ID);
                    newOpenTrade.Margin = nlistSpotCommand[i].Margin;
                    newOpenTrade.IsHedged = nlistSpotCommand[i].Symbol.IsHedged;
                    listCommand.Add(newOpenTrade);
                }
            }
            #endregion
            double margin, lots, change;
            if (listCommand.Count > 0)
            {
                double leverage = GetLeverageSymbol(listCommand[0]);
                if (leverage >= 0) leverage = listCommand[0].Investor.Leverage;

                for (int i = 0; i < listCommand.Count; i++)
                {
                    if ((listCommand[i].Type.ID % 2 != 0) && listCommand[i].Size != 0 && listCommand[i].IsHedged)
                    {
                        for (int f = 0; f < listCommand.Count; f++)
                        {
                            #region Non FreezeMargin
                            if ((listCommand[f].Symbol.Name == listCommand[i].Symbol.Name) &&
                                (listCommand[f].Type.ID == listCommand[i].Type.ID + 1) && (listCommand[f].Size != 0) && listCommand[i].IsHedged && !listCommand[i].Symbol.UseFreezeMargin)
                            {
                                change = Math.Abs(listCommand[i].Size - listCommand[f].Size);
                                if (change == 0)
                                {
                                    lots = listCommand[i].Size + listCommand[f].Size;
                                    result.TotalMargin += CalculationMarginHedged(leverage, listCommand[i].Symbol.MarginHedged, lots, listCommand[i].Symbol);
                                    listCommand[f].Size = 0;
                                    listCommand[f].Margin = 0;
                                    listCommand[i].Size = 0;
                                    listCommand[i].Margin = 0;
                                    break;
                                }
                                else
                                {
                                    if (listCommand[i].Size > listCommand[f].Size)
                                    {
                                        margin = (listCommand[i].Margin / listCommand[i].Size) * change;
                                        listCommand[i].Margin = margin;
                                        listCommand[i].Size = change;
                                        lots = listCommand[f].Size * 2;
                                        result.TotalMargin += CalculationMarginHedged(leverage, listCommand[i].Symbol.MarginHedged, lots, listCommand[i].Symbol);
                                        listCommand[f].Size = 0;
                                    }
                                    else
                                    {
                                        margin = (listCommand[f].Margin / listCommand[f].Size) * change;
                                        listCommand[f].Margin = margin;
                                        listCommand[f].Size = change;
                                        lots = listCommand[i].Size * 2;
                                        result.TotalMargin += CalculationMarginHedged(leverage, listCommand[i].Symbol.MarginHedged, lots, listCommand[i].Symbol);
                                        listCommand[i].Size = 0;
                                        break;
                                    }
                                }
                            }
                            #endregion

                            #region FreezeMargin
                            if ((listCommand[f].Symbol.Name == listCommand[i].Symbol.Name) && (listCommand[f].Type.ID != 7) && (listCommand[f].Type.ID != 8) &&
                                 (listCommand[f].Type.ID != 9) && (listCommand[f].Type.ID != 10) && (listCommand[f].Type.ID == listCommand[i].Type.ID + 1) && (listCommand[f].Size != 0) &&
                                 listCommand[i].IsHedged && listCommand[i].Symbol.UseFreezeMargin)
                            {
                                //result += listCommand[i].Margin;
                                change = Math.Abs(listCommand[i].Size - listCommand[f].Size);
                                if (change == 0)
                                {
                                    lots = listCommand[i].Size + listCommand[f].Size;
                                    result.TotalFreezeMargin += this.CalculationFreezeMarginHedged(leverage, listCommand[i].Symbol.MarginHedged, lots, listCommand[i].Symbol.FreezeMarginHedged, listCommand[i].Symbol);
                                    listCommand[f].Size = 0;
                                    listCommand[f].Margin = 0;
                                    listCommand[i].Size = 0;
                                    listCommand[i].Margin = 0;
                                    break;
                                }
                                else
                                {
                                    if (listCommand[i].Size > listCommand[f].Size)
                                    {
                                        margin = (listCommand[i].Margin / listCommand[i].Size) * change;
                                        listCommand[i].Margin = margin;
                                        listCommand[i].Size = change;
                                        lots = listCommand[f].Size * 2;
                                        result.TotalFreezeMargin += CalculationFreezeMarginHedged(leverage, listCommand[i].Symbol.MarginHedged, lots, listCommand[i].Symbol.FreezeMarginHedged, listCommand[i].Symbol);
                                        listCommand[f].Size = 0;
                                    }
                                    else
                                    {
                                        margin = (listCommand[f].Margin / listCommand[f].Size) * change;
                                        listCommand[f].Margin = margin;
                                        listCommand[f].Size = change;
                                        lots = listCommand[i].Size * 2;
                                        result.TotalFreezeMargin += CalculationFreezeMarginHedged(leverage, listCommand[i].Symbol.MarginHedged, lots, listCommand[i].Symbol.FreezeMarginHedged, listCommand[i].Symbol);
                                        listCommand[i].Size = 0;
                                        break;
                                    }
                                }
                            }
                            #endregion
                        }
                    }
                }

                for (int i = 0; i < listCommand.Count; i++)
                {
                    if (listCommand[i].Size != 0)
                    {
                        if (!listCommand[i].Symbol.UseFreezeMargin)
                        {
                            if (listCommand[i].Type.ID != 7 && listCommand[i].Type.ID != 8
                                && listCommand[i].Type.ID != 9 && listCommand[i].Type.ID != 10)
                            {
                                result.TotalMargin += listCommand[i].Margin;
                            }
                            if (listCommand[i].Type.ID == 7 || listCommand[i].Type.ID == 8 ||
                                listCommand[i].Type.ID == 9 || listCommand[i].Type.ID == 10)
                            {
                                result.TotalMargin += listCommand[i].Margin;
                            }
                        }
                        else
                        {
                            if ( listCommand[i].Type.ID != 7 && listCommand[i].Type.ID != 8
                                && listCommand[i].Type.ID != 9 && listCommand[i].Type.ID != 10)
                            {
                                result.TotalFreezeMargin += listCommand[i].Margin;
                            }
                        }
                    }
                }
            }
            return result;
        }
        /// <summary>
        /// EVENT WEEK CLOSE TRADE SYMBOL
        /// </summary>
        internal void CloseTradeSymbol(string TargetName, Business.TimeEvent timeEvent)
        {
            return;

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

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

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

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

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

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

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

                                                                                break;
                                                                            }
                                                                        }
                                                                    }
                                                                }

                                                                break;
                                                            }
                                                        }
                                                    }
                                                    #endregion

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

                                                j--;

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

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

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

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

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

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

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

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

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

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

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

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

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

                        break;
                    }
                }
            }
        }
        /// <summary>
        /// EVENT WEEK OPEN TRADE SYMBOL
        /// </summary>
        internal void OpenTradeSymbol(string TargetName, Business.TimeEvent timeEvent)
        {
            return;

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

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

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

                            #region RESET FREEZE MARGIN AND CALCULATION MARGION OF COMMAND
                            if (Business.Market.SymbolList[i].CommandList != null)
                            {                                
                                int countCommand = Business.Market.SymbolList[i].CommandList.Count;
                                for (int j = 0; j < countCommand; j++)
                                {
                                    bool flagInvestor = false;
                                    Business.Market.SymbolList[i].CommandList[j].CalculatorMarginCommand(Business.Market.SymbolList[i].CommandList[j]);
                                    Business.Market.SymbolList[i].CommandList[j].FreezeMargin = 0;

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

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

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

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

                            //            if (!flagInvestor)
                            //            {
                            //                listInvestor.Add(Business.Market.SymbolList[i].CommandList[j].Investor);
                            //            }
                            //        }
                            //    }
                            //    #endregion
                            //}
                            //else    //Second Session
                            //{
                            //    #region RESET FREEZE MARGIN AND CALCULATION MARGION OF COMMAND
                            //    if (Business.Market.SymbolList[i].CommandList != null)
                            //    {
                            //        int countCommand = Business.Market.SymbolList[i].CommandList.Count;
                            //        for (int j = 0; j < countCommand; j++)
                            //        {
                            //            bool flagInvestor = false;

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

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

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

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

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

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

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

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

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

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

                        break;
                    }
                }
            }         
        }
        /// <summary>
        /// UPDATE TRADING CONFIG, SETTING PARAMETER DEFAULT
        /// </summary>
        /// <param name="objSymbolConfig"></param>
        internal bool UpdateTradingConfig(Business.ParameterItem objSymbolConfig)
        {
            bool Result = false;
            StringBuilder content = new StringBuilder();

            if (Market.SymbolList != null)
            {
                int count = Market.SymbolList.Count;
                for (int i = 0; i < count; i++)
                {
                    if (Market.SymbolList[i].SymbolID == objSymbolConfig.SecondParameterID)
                    {
                        if (Market.SymbolList[i].ParameterItems != null)
                        {
                            int countConfig = Market.SymbolList[i].ParameterItems.Count;
                            for (int j = 0; j < countConfig; j++)
                            {
                                if (Market.SymbolList[i].ParameterItems[j].Code == objSymbolConfig.Code)
                                {
                                    #region CHANGE CONFIG SYMBOL AND UPDATE CONFIG DEAFULT
                                    //Set ParameterID For Config
                                    objSymbolConfig.ParameterItemID = Market.SymbolList[i].ParameterItems[j].ParameterItemID;

                                    //Update Value Default Of Symbol
                                    #region Update Property Contract Size Of Symbol
                                    if (objSymbolConfig.Code == "S025")
                                    {
                                        double ContractSize = 0;
                                        double.TryParse(objSymbolConfig.NumValue, out ContractSize);
                                        Market.SymbolList[i].ContractSize = ContractSize;
                                    }
                                    #endregion

                                    #region Update Property Tick Price Of Symbol
                                    if (objSymbolConfig.Code == "S030")
                                    {
                                        double TickPrice = 0;
                                        double.TryParse(objSymbolConfig.NumValue, out TickPrice);
                                        Market.SymbolList[i].TickPrice = TickPrice;
                                    }
                                    #endregion

                                    #region Update Property Tick Size Of Symbol
                                    if (objSymbolConfig.Code == "S029")
                                    {
                                        double TickSize = 0;
                                        double.TryParse(objSymbolConfig.NumValue, out TickSize);
                                        Market.SymbolList[i].TickSize = TickSize;
                                    }
                                    #endregion

                                    #region Update Property Profit Calculation Of Symbol
                                    if (objSymbolConfig.Code == "S033")
                                    {
                                        Market.SymbolList[i].ProfitCalculation = objSymbolConfig.StringValue;
                                    }
                                    #endregion

                                    #region Update Property Digit Of Symbol
                                    if (objSymbolConfig.Code == "S003")
                                    {
                                        int Digit = 0;
                                        int.TryParse(objSymbolConfig.NumValue, out Digit);
                                        Market.SymbolList[i].Digit = Digit;
                                    }
                                    #endregion

                                    #region Update Property Currency
                                    if (objSymbolConfig.Code == "S007")
                                    {
                                        Market.SymbolList[i].Currency = objSymbolConfig.StringValue.Trim();
                                    }
                                    #endregion

                                    #region Update Property Initital Margin
                                    if (objSymbolConfig.Code == "S026")
                                    {
                                        double InitialMargin = -1;
                                        double.TryParse(objSymbolConfig.NumValue, out InitialMargin);
                                        Market.SymbolList[i].InitialMargin = InitialMargin;
                                    }
                                    #endregion

                                    #region Update Property Spread By Default Of Symbol
                                    if (objSymbolConfig.Code == "S013")
                                    {
                                        double SpreadByDefault = 0;
                                        double.TryParse(objSymbolConfig.NumValue, out SpreadByDefault);
                                        Market.SymbolList[i].SpreadByDefault = SpreadByDefault;
                                    }
                                    #endregion

                                    #region Update Property Sread Balance Of Symbol
                                    if (objSymbolConfig.Code == "S016")
                                    {
                                        double SpreadBalance = 0;
                                        double.TryParse(objSymbolConfig.NumValue, out SpreadBalance);
                                        Market.SymbolList[i].SpreadBalace = SpreadBalance;
                                    }
                                    #endregion

                                    #region Update Property Long Only Of Symbol
                                    if (objSymbolConfig.Code == "S014")
                                    {
                                        bool LongOnly = false;
                                        if (objSymbolConfig.BoolValue == 1)
                                        {
                                            LongOnly = true;
                                        }
                                        Market.SymbolList[i].LongOnly = LongOnly;
                                    }
                                    #endregion

                                    #region Update Property Limit Stop Level Of Symbol
                                    if (objSymbolConfig.Code == "S015")
                                    {
                                        int LimitLevel = 0;
                                        int.TryParse(objSymbolConfig.NumValue, out LimitLevel);
                                        Market.SymbolList[i].LimitLevel = LimitLevel;
                                    }

                                    if (objSymbolConfig.Code == "S046")
                                    {
                                        int StopLevel = 0;
                                        int.TryParse(objSymbolConfig.NumValue, out StopLevel);
                                        Market.SymbolList[i].StopLevel = StopLevel;
                                    }

                                    if (objSymbolConfig.Code == "S047")
                                    {
                                        int SLTP = 0;
                                        int.TryParse(objSymbolConfig.NumValue, out SLTP);
                                        Market.SymbolList[i].StopLossTakeProfitLevel = SLTP;
                                    }
                                    #endregion

                                    #region Update Property Freeze Level Of Symbol
                                    if (objSymbolConfig.Code == "S017")
                                    {
                                        int FreezeLevel = 0;
                                        int.TryParse(objSymbolConfig.NumValue, out FreezeLevel);
                                        Market.SymbolList[i].FreezeLevel = FreezeLevel;
                                    }
                                    #endregion

                                    #region Update Property Allow Read Time In Symbol
                                    if (objSymbolConfig.Code == "S018")
                                    {
                                        bool AllowReadTime = false;
                                        if (objSymbolConfig.BoolValue == 1)
                                        {
                                            AllowReadTime = true;
                                        }

                                        Market.SymbolList[i].AllowReadTime = AllowReadTime;
                                    }
                                    #endregion

                                    #region Update Property Filter Of Symbol
                                    if (objSymbolConfig.Code == "S022")
                                    {
                                        int Filter = 0;
                                        int.TryParse(objSymbolConfig.NumValue, out Filter);
                                        Market.SymbolList[i].Filter = Filter;
                                    }
                                    #endregion

                                    #region Update Property Filtration Level Of Symbol
                                    if (objSymbolConfig.Code == "S020")
                                    {
                                        int FiltrationsLevel = 0;
                                        int.TryParse(objSymbolConfig.NumValue, out FiltrationsLevel);
                                        Market.SymbolList[i].FiltrationsLevel = FiltrationsLevel;
                                    }
                                    #endregion

                                    #region Update Property Auto Limit Of Symbol
                                    if (objSymbolConfig.Code == "S021")
                                    {
                                        Market.SymbolList[i].AutoLimit = objSymbolConfig.StringValue;
                                    }
                                    #endregion

                                    #region Update Property IsHedged Of Symbol
                                    if (objSymbolConfig.Code == "S034")
                                    {
                                        bool IsHedged = false;
                                        if (objSymbolConfig.BoolValue == 1)
                                            IsHedged = true;
                                        Market.SymbolList[i].IsHedged = IsHedged;

                                        //RECALCULATION ALL INVESTOR
                                        Business.Investor.investorInstance.ReCalculationTotalMargin();
                                    }
                                    #endregion

                                    #region Update Property Trade Of Symbol
                                    if (objSymbolConfig.Code == "S008")
                                    {
                                        Market.SymbolList[i].Trade = objSymbolConfig.StringValue;
                                    }
                                    #endregion

                                    #region Update Event Symbol
                                    //if (objSymbolConfig.Code == "S042")
                                    //{
                                    //    TradingServer.Facade.FacadeUpdateSymbolEvent(objSymbolConfig.StringValue, Business.Market.SymbolList[i].Name,
                                    //        objSymbolConfig.Code, Business.Market.SymbolList[i].ParameterItems[j].ParameterItemID);
                                    //}

                                    //if (objSymbolConfig.Code == "S043")
                                    //{
                                    //    TradingServer.Facade.FacadeUpdateSymbolEvent(objSymbolConfig.StringValue, Business.Market.SymbolList[i].Name,
                                    //        objSymbolConfig.Code, Business.Market.SymbolList[i].ParameterItems[j].ParameterItemID);
                                    //}
                                    #endregion

                                    #region UPDATE PROPERTY APPLY SPREAD
                                    if (objSymbolConfig.Code == "S050")
                                    {
                                        bool applySpread = false;
                                        if (objSymbolConfig.BoolValue == 1)
                                        {
                                            applySpread = true;
                                        }

                                        Business.Market.SymbolList[i].ApplySpread = applySpread;
                                    }
                                    #endregion

                                    #region UPDATE PROPERTY USE FREEZE MARGIN
                                    if (objSymbolConfig.Code == "S051")
                                    {
                                        bool useFreezeMargin = false;
                                        if (objSymbolConfig.BoolValue == 1)
                                            useFreezeMargin = true;

                                        Business.Market.SymbolList[i].IsEnableFreezeMargin = useFreezeMargin;
                                    }
                                    #endregion

                                    #region UPDATE PROPERTY FREEZE MARGIN
                                    if (objSymbolConfig.Code == "S052")
                                    {
                                        double freezeMargin = 0;
                                        double.TryParse(objSymbolConfig.NumValue.ToString(), out freezeMargin);
                                        Business.Market.SymbolList[i].FreezeMargin = freezeMargin;
                                    }
                                    #endregion

                                    #region UPDATE PROPERTY FREEZE MARGIN HEDGED
                                    if (objSymbolConfig.Code == "S053")
                                    {
                                        double freezeMarginH = 0;
                                        double.TryParse(objSymbolConfig.NumValue, out freezeMarginH);
                                        Business.Market.SymbolList[i].FreezeMarginHedged = freezeMarginH;
                                    }
                                    #endregion

                                    #region UPDATE PROPERTY MARGIN HEDGED
                                    if (objSymbolConfig.Code == "S028")
                                    {
                                        double marginH = 0;
                                        double.TryParse(objSymbolConfig.NumValue, out marginH);
                                        Business.Market.SymbolList[i].MarginHedged = marginH;
                                    }
                                    #endregion

                                    //Update Value Parameter Item
                                    Market.SymbolList[i].ParameterItems[j].Name = objSymbolConfig.Name;
                                    Market.SymbolList[i].ParameterItems[j].Code = objSymbolConfig.Code;
                                    Market.SymbolList[i].ParameterItems[j].BoolValue = objSymbolConfig.BoolValue;
                                    Market.SymbolList[i].ParameterItems[j].StringValue = objSymbolConfig.StringValue;
                                    Market.SymbolList[i].ParameterItems[j].NumValue = objSymbolConfig.NumValue;
                                    Market.SymbolList[i].ParameterItems[j].DateValue = objSymbolConfig.DateValue;

                                    //Set ParameterID Again
                                    objSymbolConfig.ParameterItemID = Market.SymbolList[i].ParameterItems[j].ParameterItemID;
                                    #endregion

                                    break;
                                }
                            }
                        }

                        #region UPDATE CALCULATION SYMBOL
                        if (objSymbolConfig.Code == "S025" || objSymbolConfig.Code == "S026" ||
                            objSymbolConfig.Code == "S027" || objSymbolConfig.Code == "S028" ||
                            objSymbolConfig.Code == "S029" || objSymbolConfig.Code == "S030" ||
                            objSymbolConfig.Code == "S031" || objSymbolConfig.Code == "S032" ||
                            objSymbolConfig.Code == "S034")
                        {
                            List<Business.Investor> listInvestor = new List<Investor>();
                            if (Business.Market.SymbolList[i].CommandList != null && Business.Market.SymbolList[i].CommandList.Count > 0)
                            {
                                int countCommand = Business.Market.SymbolList[i].CommandList.Count;
                                for (int j = 0; j < countCommand; j++)
                                {
                                    bool flagInvestor = false;
                                    Business.Market.SymbolList[i].CommandList[j].CalculatorMarginCommand(Business.Market.SymbolList[i].CommandList[j]);

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

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

                            if (listInvestor != null && listInvestor.Count > 0)
                            {
                                int countInvestor = listInvestor.Count;
                                for (int j = 0; j < countInvestor; j++)
                                {
                                    if (listInvestor[j].CommandList != null && listInvestor[j].CommandList.Count > 0)
                                    {
                                        int countCommand = listInvestor[j].CommandList.Count;
                                        for (int n = 0; n < countCommand; n++)
                                        {
                                            if (listInvestor[j].CommandList[n].Symbol.Name == Market.SymbolList[i].Name)
                                            {
                                                listInvestor[j].CommandList[n].CalculatorMarginCommand(listInvestor[j].CommandList[n]);
                                            }
                                        }
                                    }

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

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

                                    //SEND NOTIFY TO CLIENT
                                    TradingServer.Business.Market.SendNotifyToClient("IAC04332451", 3, listInvestor[j].InvestorID);
                                }
                            }
                        }
                        #endregion
                    }
                    else
                    {
                        //Find In Reference Symbol
                        this.UpdateTradingConfigReference(objSymbolConfig, Market.SymbolList[i].RefSymbol);
                    }
                }
            }

            Result = ParameterItem.DBWTradingConfigInstance.UpdateTradingConfig(objSymbolConfig.ParameterItemID, objSymbolConfig.SecondParameterID, -1,
                objSymbolConfig.Name, objSymbolConfig.Code, objSymbolConfig.BoolValue, objSymbolConfig.StringValue, objSymbolConfig.NumValue, objSymbolConfig.DateValue);

            #region SEARCH IN INVESTOR ONLINE AND SENT COMMAND UPDATE CONFIG SYMBOL
            //SEARCH IN INVESTOR ONLINE AND SENT COMMAND UPDATE CONFIG SYMBOL
            //if (Business.Market.InvestorList != null && Business.Market.InvestorList.Count > 0)
            //{
            //    int count = Business.Market.InvestorList.Count;
            //    for (int i = 0; i < count; i++)
            //    {
            //        if (Business.Market.InvestorList[i].IsOnline)
            //        {
            //            if (Business.Market.InvestorList[i].ClientCommandQueue == null)
            //                Business.Market.InvestorList[i].ClientCommandQueue = new List<string>();

            //            Business.Market.InvestorList[i].ClientCommandQueue.Add("RSC1235789$" + objSymbolConfig.ParameterItemID + "," + objSymbolConfig.SecondParameterID + ",-1" +
            //                objSymbolConfig.Name + "," + objSymbolConfig.Code + "," + objSymbolConfig.BoolValue + "," + objSymbolConfig.StringValue + "," + objSymbolConfig.NumValue +
            //                objSymbolConfig.DateValue);
            //        }
            //    }
            //}
            #endregion

            if (objSymbolConfig.Code == "S042" || objSymbolConfig.Code == "S043")
            {
                //Call Function Init Time Event Symbol
                Business.Market.marketInstance.InitTimeEventInSymbol();

                //CALL FUNCTION INIT TIME MARKET
                Business.Market.marketInstance.InitTimeEventServer();
            }

            if (objSymbolConfig.Code == "S044" || objSymbolConfig.Code == "S045")
            {
                Business.Market.marketInstance.InitSymbolFuture();
            }

            return Result;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="listTradingConfig"></param>
        /// <returns></returns>
        internal bool UpdateTradingConfig(List<Business.ParameterItem> listTradingConfig, string code,string ipAddress)
        {
            bool Result = false;
            bool isExits = false;
            StringBuilder content = new StringBuilder();
            StringBuilder beforeContent = new StringBuilder();
            StringBuilder afterContent = new StringBuilder();

            content.Append("'" + code + "': update config symbol ");

            if (listTradingConfig != null)
            {
                int countTradingConfig = listTradingConfig.Count;
                for (int a = 0; a < countTradingConfig; a++)
                {
                    if (Market.SymbolList != null)
                    {
                        int count = Market.SymbolList.Count;
                        for (int i = 0; i < count; i++)
                        {
                            if (Market.SymbolList[i].SymbolID == listTradingConfig[a].SecondParameterID)
                            {
                                #region CHECK FIRST RUN
                                if (!isExits)
                                {
                                    content.Append("'" + Business.Market.SymbolList[i].Name + "': ");
                                    isExits = true;
                                }
                                #endregion

                                #region UPDATE PARAMETER ON TRADING CONFIG
                                if (Market.SymbolList[i].ParameterItems != null)
                                {
                                    int countConfig = Market.SymbolList[i].ParameterItems.Count;
                                    for (int j = 0; j < countConfig; j++)
                                    {
                                        if (Market.SymbolList[i].ParameterItems[j].Code == listTradingConfig[a].Code)
                                        {
                                            #region FILTER CODE GET NAME UPDATE BEFORE
                                            switch (Market.SymbolList[i].ParameterItems[j].Code)
                                            {
                                                case "S001":    //SYMBOL NAME
                                                    beforeContent.Append("symbol name: " + Market.SymbolList[i].ParameterItems[j].StringValue + " - ");
                                                    break;
                                                case "S002":    //SOURCE
                                                    break;
                                                case "S003":    //DIGIT
                                                    beforeContent.Append("digit: " + Market.SymbolList[i].ParameterItems[j].NumValue + " - ");
                                                    break;
                                                case "S004":    //DESCRIPTION
                                                    beforeContent.Append("description: " + Market.SymbolList[i].ParameterItems[j].StringValue + " - ");
                                                    break;
                                                case "S005":    //TYPE
                                                    beforeContent.Append("type: " + Market.SymbolList[i].ParameterItems[j].StringValue + " - ");
                                                    break;
                                                case "S006":    //EXECUTION
                                                    beforeContent.Append("execution: " + Market.SymbolList[i].ParameterItems[j].StringValue + " - ");
                                                    break;
                                                case "S007":    //CURRENCY
                                                    beforeContent.Append("currency: " + Market.SymbolList[i].ParameterItems[j].StringValue + " - ");
                                                    break;
                                                case "S008":    //TRADE
                                                    beforeContent.Append("trade: " + Market.SymbolList[i].ParameterItems[j].StringValue + " - ");
                                                    break;
                                                case "S009":    //BACKGROUND
                                                    break;
                                                case "S010": //MARGIN CURRENCY
                                                    beforeContent.Append("margin currency: " + Market.SymbolList[i].ParameterItems[j].StringValue + " - ");
                                                    break;
                                                case "S011":    //MAXIMUM LOTS FOR IE
                                                    beforeContent.Append("maximum lots for IE: " + Market.SymbolList[i].ParameterItems[j].StringValue + " - ");
                                                    break;
                                                case "S012":    //ORDERS
                                                    beforeContent.Append("orders: " + Market.SymbolList[i].ParameterItems[j].StringValue + " - ");
                                                    break;
                                                case "S013":    //SPREAD BY DEFAULT
                                                    beforeContent.Append("spread by default: " + Market.SymbolList[i].ParameterItems[j].NumValue + " - ");
                                                    break;
                                                case "S014":    //LONG ONLY
                                                    if (Market.SymbolList[i].ParameterItems[j].BoolValue == 1)
                                                        beforeContent.Append("long only: enable - ");
                                                    else
                                                        beforeContent.Append("long only: disable - ");
                                                    break;
                                                case "S015":
                                                    beforeContent.Append("limit level: " + Market.SymbolList[i].ParameterItems[j].NumValue + " - ");
                                                    break;
                                                case "S016":
                                                    beforeContent.Append("spread balance: " + Market.SymbolList[i].ParameterItems[j].StringValue + " - ");
                                                    break;
                                                case "S017":
                                                    beforeContent.Append("freeze level: " + Market.SymbolList[i].ParameterItems[j].NumValue + " - ");
                                                    break;
                                                case "S018":
                                                    if (Business.Market.SymbolList[i].ParameterItems[j].BoolValue == 1)
                                                        beforeContent.Append("use datafeed time stamp: on - ");
                                                    else
                                                        beforeContent.Append("use datafeed time stamp: off - ");
                                                    break;
                                                case "S019":
                                                    break;
                                                case "S020":
                                                    beforeContent.Append("filtration level: " + Market.SymbolList[i].ParameterItems[j].NumValue + " - ");
                                                    break;
                                                case "S021":
                                                    beforeContent.Append("auto limit: " + Market.SymbolList[i].ParameterItems[j].StringValue + " - ");
                                                    break;
                                                case "S022":
                                                    beforeContent.Append("filter: " + Market.SymbolList[i].ParameterItems[j].NumValue + " - ");
                                                    break;
                                                case "S023":
                                                    break;
                                                case "S024":
                                                    break;
                                                case "S025":
                                                    beforeContent.Append("contract size: " + Market.SymbolList[i].ParameterItems[j].NumValue + " - ");
                                                    break;
                                                case "S026":
                                                    beforeContent.Append("initial margin: " + Market.SymbolList[i].ParameterItems[j].NumValue + " - ");
                                                    break;
                                                case "S027":
                                                    break;
                                                case "S028":
                                                    beforeContent.Append("hedge: " + Market.SymbolList[i].ParameterItems[j].NumValue + " - ");
                                                    break;
                                                case "S029":
                                                    beforeContent.Append("tick size: " + Market.SymbolList[i].ParameterItems[j].NumValue + " - ");
                                                    break;
                                                case "S030":
                                                    beforeContent.Append("tick price: " + Market.SymbolList[i].ParameterItems[j].NumValue + " - ");
                                                    break;
                                                case "S031":
                                                    beforeContent.Append("percentage: " + Market.SymbolList[i].ParameterItems[j].NumValue + " - ");
                                                    break;
                                                case "S032":
                                                    beforeContent.Append("margin calculation: " + Market.SymbolList[i].ParameterItems[j].StringValue + " - ");
                                                    break;
                                                case "S033":
                                                    beforeContent.Append("profit calculation: " + Market.SymbolList[i].ParameterItems[j].StringValue + " - ");
                                                    break;
                                                case "S034":
                                                    if (Market.SymbolList[i].ParameterItems[j].BoolValue == 1)
                                                        beforeContent.Append("strong hedged margin mode: enable - ");
                                                    else
                                                        beforeContent.Append("string hedged margin mode: disable - ");
                                                    break;
                                                case "S035":
                                                    if (Business.Market.SymbolList[i].ParameterItems[j].BoolValue == 1)
                                                        beforeContent.Append("enable swap: on - ");
                                                    else
                                                        beforeContent.Append("enable swap: off - ");
                                                    break;
                                                case "S036":
                                                    beforeContent.Append("swap calculation: " + Market.SymbolList[i].ParameterItems[j].StringValue + " - ");
                                                    break;
                                                case "S037":
                                                    beforeContent.Append("long position: " + Market.SymbolList[i].ParameterItems[j].NumValue + " - ");
                                                    break;
                                                case "S038":
                                                    beforeContent.Append("short position: " + Market.SymbolList[i].ParameterItems[j].NumValue + " - ");
                                                    break;
                                                case "S039":
                                                    beforeContent.Append("3-day swaps: " + Market.SymbolList[i].ParameterItems[j].StringValue + " - ");
                                                    break;
                                                case "S040":
                                                    break;
                                                case "S041":
                                                    break;
                                                case "S042":
                                                    beforeContent.Append("trade session: " + Market.SymbolList[i].ParameterItems[j].StringValue + " - ");
                                                    break;
                                                case "S043":
                                                    beforeContent.Append("quote session: " + Market.SymbolList[i].ParameterItems[j].StringValue + " - ");
                                                    break;
                                                case "S044":
                                                    beforeContent.Append("close only: " + Market.SymbolList[i].ParameterItems[j].DateValue + " - ");
                                                    break;
                                                case "S045":
                                                    beforeContent.Append("time expired: " + Market.SymbolList[i].ParameterItems[j].DateValue + " - ");
                                                    break;
                                                case "S046":
                                                    beforeContent.Append("stop level: " + Market.SymbolList[i].ParameterItems[j].NumValue + " - ");
                                                    break;
                                                case "S047":
                                                    beforeContent.Append("stoploss and takeprofit level: " + Market.SymbolList[i].ParameterItems[j].NumValue + " - ");
                                                    break;
                                                case "S048":
                                                    beforeContent.Append("start focus digit: " + Market.SymbolList[i].ParameterItems[j].NumValue + " - ");
                                                    break;
                                                case "S049":
                                                    beforeContent.Append("end focus digit: " + Market.SymbolList[i].ParameterItems[j].NumValue + " - ");
                                                    break;
                                                case "S050":
                                                    if (Market.SymbolList[i].ParameterItems[j].BoolValue == 1)
                                                        beforeContent.Append("apply spread: enable - ");
                                                    else
                                                        beforeContent.Append("apply spread: disable - ");
                                                    break;
                                                case "S051":
                                                    if (Market.SymbolList[i].ParameterItems[j].BoolValue == 1)
                                                        beforeContent.Append("use freeze margin: enable - ");
                                                    else
                                                        beforeContent.Append("use freeze margin: disable - ");
                                                    break;
                                                case "S052":
                                                    beforeContent.Append("freeze margin: " + Market.SymbolList[i].ParameterItems[j].NumValue + " - ");
                                                    break;
                                                case "S053":
                                                    beforeContent.Append("freeze margin hedged: " + Market.SymbolList[i].ParameterItems[j].NumValue + " - ");
                                                    break;
                                                case "S054":
                                                    beforeContent.Append("on hold time : " + Market.SymbolList[i].ParameterItems[j].NumValue + " - ");
                                                    break;
                                            }
                                            #endregion

                                            #region FILTER CODE GET NAME UPDATE AFTER
                                            switch (listTradingConfig[a].Code)
                                            {
                                                case "S001":    //SYMBOL NAME
                                                    afterContent.Append("symbol name: " + listTradingConfig[a].StringValue + " - ");
                                                    break;
                                                case "S002":    //SOURCE
                                                    break;
                                                case "S003":    //DIGIT
                                                    afterContent.Append("digit: " + listTradingConfig[a].NumValue + " - ");
                                                    break;
                                                case "S004":    //DESCRIPTION
                                                    afterContent.Append("description: " + listTradingConfig[a].StringValue + " - ");
                                                    break;
                                                case "S005":    //TYPE
                                                    afterContent.Append("type: " + listTradingConfig[a].StringValue + " - ");
                                                    break;
                                                case "S006":    //EXECUTION
                                                    afterContent.Append("execution: " + listTradingConfig[a].StringValue + " - ");
                                                    break;
                                                case "S007":    //CURRENCY
                                                    afterContent.Append("currency: " + listTradingConfig[a].StringValue + " - ");
                                                    break;
                                                case "S008":    //TRADE
                                                    afterContent.Append("trade: " + listTradingConfig[a].StringValue + " - ");
                                                    break;
                                                case "S009":    //BACKGROUND
                                                    break;
                                                case "S010": //MARGIN CURRENCY
                                                    afterContent.Append("margin currency: " + listTradingConfig[a].StringValue + " - ");
                                                    break;
                                                case "S011":    //MAXIMUM LOTS FOR IE
                                                    afterContent.Append("maximum lots for IE: " + listTradingConfig[a].StringValue + " - ");
                                                    break;
                                                case "S012":    //ORDERS
                                                    afterContent.Append("orders: " + listTradingConfig[a].StringValue + " - ");
                                                    break;
                                                case "S013":    //SPREAD BY DEFAULT
                                                    afterContent.Append("spread by default: " + listTradingConfig[a].NumValue + " - ");
                                                    break;
                                                case "S014":    //LONG ONLY
                                                    if (listTradingConfig[a].BoolValue == 1)
                                                        afterContent.Append("long only: enable - ");
                                                    else
                                                        afterContent.Append("long only: disable - ");
                                                    break;
                                                case "S015":
                                                    afterContent.Append("limit level: " + listTradingConfig[a].NumValue + " - ");
                                                    break;
                                                case "S016":
                                                    afterContent.Append("spread balance: " + listTradingConfig[a].StringValue + " - ");
                                                    break;
                                                case "S017":
                                                    afterContent.Append("freeze level: " + listTradingConfig[a].NumValue + " - ");
                                                    break;
                                                case "S018":
                                                    if (listTradingConfig[a].BoolValue == 1)
                                                        afterContent.Append("use datafeed time stamp: on - ");
                                                    else
                                                        afterContent.Append("use datafeed time stamp: off - ");
                                                    break;
                                                case "S019":
                                                    break;
                                                case "S020":
                                                    afterContent.Append("filtration level: " + listTradingConfig[a].NumValue + " - ");
                                                    break;
                                                case "S021":
                                                    afterContent.Append("auto limit: " + listTradingConfig[a].StringValue + " - ");
                                                    break;
                                                case "S022":
                                                    afterContent.Append("filter: " + listTradingConfig[a].NumValue + " - ");
                                                    break;
                                                case "S023":
                                                    break;
                                                case "S024":
                                                    break;
                                                case "S025":
                                                    afterContent.Append("contract size: " + listTradingConfig[a].NumValue + " - ");
                                                    break;
                                                case "S026":
                                                    afterContent.Append("initial margin: " + listTradingConfig[a].NumValue + " - ");
                                                    break;
                                                case "S027":
                                                    break;
                                                case "S028":
                                                    afterContent.Append("hedge: " + listTradingConfig[a].NumValue + " - ");
                                                    break;
                                                case "S029":
                                                    afterContent.Append("tick size: " + listTradingConfig[a].NumValue + " - ");
                                                    break;
                                                case "S030":
                                                    afterContent.Append("tick price: " + listTradingConfig[a].NumValue + " - ");
                                                    break;
                                                case "S031":
                                                    afterContent.Append("percentage: " + listTradingConfig[a].NumValue + " - ");
                                                    break;
                                                case "S032":
                                                    afterContent.Append("margin calculation: " + listTradingConfig[a].StringValue + " - ");
                                                    break;
                                                case "S033":
                                                    afterContent.Append("profit calculation: " + listTradingConfig[a].StringValue + " - ");
                                                    break;
                                                case "S034":
                                                    if (listTradingConfig[a].BoolValue == 1)
                                                        afterContent.Append("strong hedged margin mode: enable - ");
                                                    else
                                                        afterContent.Append("string hedged margin mode: disable - ");
                                                    break;
                                                case "S035":
                                                    if (listTradingConfig[a].BoolValue == 1)
                                                        afterContent.Append("enable swap: on - ");
                                                    else
                                                        afterContent.Append("enable swap: off - ");
                                                    break;
                                                case "S036":
                                                    afterContent.Append("swap calculation: " + listTradingConfig[a].StringValue + " - ");
                                                    break;
                                                case "S037":
                                                    afterContent.Append("long position: " + listTradingConfig[a].NumValue + " - ");
                                                    break;
                                                case "S038":
                                                    afterContent.Append("short position: " + listTradingConfig[a].NumValue + " - ");
                                                    break;
                                                case "S039":
                                                    afterContent.Append("3-day swaps: " + listTradingConfig[a].StringValue + " - ");
                                                    break;
                                                case "S040":
                                                    break;
                                                case "S041":
                                                    break;
                                                case "S042":
                                                    afterContent.Append("trade session: " + listTradingConfig[a].StringValue + " - ");
                                                    break;
                                                case "S043":
                                                    afterContent.Append("quote session: " + listTradingConfig[a].StringValue + " - ");
                                                    break;
                                                case "S044":
                                                    afterContent.Append("close only: " + listTradingConfig[a].DateValue + " - ");
                                                    break;
                                                case "S045":
                                                    afterContent.Append("time expired: " + listTradingConfig[a].DateValue + " - ");
                                                    break;
                                                case "S046":
                                                    afterContent.Append("stop level: " + listTradingConfig[a].NumValue + " - ");
                                                    break;
                                                case "S047":
                                                    afterContent.Append("stoploss and takeprofit level: " + listTradingConfig[a].NumValue + " - ");
                                                    break;
                                                case "S048":
                                                    afterContent.Append("start focus digit: " + listTradingConfig[a].NumValue + " - ");
                                                    break;
                                                case "S049":
                                                    afterContent.Append("end focus digit: " + listTradingConfig[a].NumValue + " - ");
                                                    break;
                                                case "S050":
                                                    if (listTradingConfig[a].BoolValue == 1)
                                                        afterContent.Append("apply spread: enable - ");
                                                    else
                                                        afterContent.Append("apply spread: disable - ");
                                                    break;
                                                case "S051":
                                                    if (listTradingConfig[a].BoolValue == 1)
                                                        afterContent.Append("use freeze margin: enable - ");
                                                    else
                                                        afterContent.Append("use freeze margin: disable - ");
                                                    break;
                                                case "S052":
                                                    afterContent.Append("freeze margin: " + listTradingConfig[a].NumValue + " - ");
                                                    break;
                                                case "S053":
                                                    afterContent.Append("freeze margin hedged: " + listTradingConfig[a].NumValue + " - ");
                                                    break;
                                                case "S054":
                                                    afterContent.Append("on hold time : " + listTradingConfig[a].NumValue + " - ");
                                                    break;
                                            }
                                            #endregion

                                            #region CHANGE CONFIG SYMBOL AND UPDATE CONFIG DEAFULT
                                            //Set ParameterID For Config
                                            listTradingConfig[a].ParameterItemID = Market.SymbolList[i].ParameterItems[j].ParameterItemID;

                                            //Update Value Default Of Symbol
                                            #region Update Property Contract Size Of Symbol
                                            if (listTradingConfig[a].Code == "S025")
                                            {
                                                double ContractSize = 0;
                                                double.TryParse(listTradingConfig[a].NumValue, out ContractSize);
                                                Market.SymbolList[i].ContractSize = ContractSize;
                                            }
                                            #endregion

                                            #region Update Property Tick Price Of Symbol
                                            if (listTradingConfig[a].Code == "S030")
                                            {
                                                double TickPrice = 0;
                                                double.TryParse(listTradingConfig[a].NumValue, out TickPrice);
                                                Market.SymbolList[i].TickPrice = TickPrice;
                                            }
                                            #endregion

                                            #region Update Property Tick Size Of Symbol
                                            if (listTradingConfig[a].Code == "S029")
                                            {
                                                double TickSize = 0;
                                                double.TryParse(listTradingConfig[a].NumValue, out TickSize);
                                                Market.SymbolList[i].TickSize = TickSize;
                                            }
                                            #endregion

                                            #region Update Property Profit Calculation Of Symbol
                                            if (listTradingConfig[a].Code == "S033")
                                            {
                                                Market.SymbolList[i].ProfitCalculation = listTradingConfig[a].StringValue;
                                            }
                                            #endregion

                                            #region Update Property Digit Of Symbol
                                            if (listTradingConfig[a].Code == "S003")
                                            {
                                                int Digit = 0;
                                                int.TryParse(listTradingConfig[a].NumValue, out Digit);
                                                Market.SymbolList[i].Digit = Digit;
                                            }
                                            #endregion

                                            #region Update Property Currency
                                            if (listTradingConfig[a].Code == "S007")
                                            {
                                                Market.SymbolList[i].Currency = listTradingConfig[a].StringValue.Trim();
                                            }
                                            #endregion

                                            #region Update Property Initital Margin
                                            if (listTradingConfig[a].Code == "S026")
                                            {
                                                double InitialMargin = -1;
                                                double.TryParse(listTradingConfig[a].NumValue, out InitialMargin);
                                                Market.SymbolList[i].InitialMargin = InitialMargin;
                                            }
                                            #endregion

                                            #region Update Property Spread By Default Of Symbol
                                            if (listTradingConfig[a].Code == "S013")
                                            {
                                                double SpreadByDefault = 0;
                                                double.TryParse(listTradingConfig[a].NumValue, out SpreadByDefault);
                                                Market.SymbolList[i].SpreadByDefault = SpreadByDefault;
                                            }
                                            #endregion

                                            #region Update Property Sread Balance Of Symbol
                                            if (listTradingConfig[a].Code == "S016")
                                            {
                                                double SpreadBalance = 0;
                                                double.TryParse(listTradingConfig[a].NumValue, out SpreadBalance);
                                                Market.SymbolList[i].SpreadBalace = SpreadBalance;
                                            }
                                            #endregion

                                            #region Update Property Long Only Of Symbol
                                            if (listTradingConfig[a].Code == "S014")
                                            {
                                                bool LongOnly = false;
                                                if (listTradingConfig[a].BoolValue == 1)
                                                {
                                                    LongOnly = true;
                                                }
                                                Market.SymbolList[i].LongOnly = LongOnly;
                                            }
                                            #endregion

                                            #region Update Property Limit Stop Level Of Symbol
                                            if (listTradingConfig[a].Code == "S015")
                                            {
                                                int LimitLevel = 0;
                                                int.TryParse(listTradingConfig[a].NumValue, out LimitLevel);
                                                Market.SymbolList[i].LimitLevel = LimitLevel;
                                            }

                                            if (listTradingConfig[a].Code == "S046")
                                            {
                                                int StopLevel = 0;
                                                int.TryParse(listTradingConfig[a].NumValue, out StopLevel);
                                                Market.SymbolList[i].StopLevel = StopLevel;
                                            }

                                            if (listTradingConfig[a].Code == "S047")
                                            {
                                                int SLTP = 0;
                                                int.TryParse(listTradingConfig[a].NumValue, out SLTP);
                                                Market.SymbolList[i].StopLossTakeProfitLevel = SLTP;
                                            }
                                            #endregion

                                            #region Update Property Freeze Level Of Symbol
                                            if (listTradingConfig[a].Code == "S017")
                                            {
                                                int FreezeLevel = 0;
                                                int.TryParse(listTradingConfig[a].NumValue, out FreezeLevel);
                                                Market.SymbolList[i].FreezeLevel = FreezeLevel;
                                            }
                                            #endregion

                                            #region Update Property Allow Read Time In Symbol
                                            if (listTradingConfig[a].Code == "S018")
                                            {
                                                bool AllowReadTime = false;
                                                if (listTradingConfig[a].BoolValue == 1)
                                                {
                                                    AllowReadTime = true;
                                                }

                                                Market.SymbolList[i].AllowReadTime = AllowReadTime;
                                            }
                                            #endregion

                                            #region Update Property Filter Of Symbol
                                            if (listTradingConfig[a].Code == "S022")
                                            {
                                                int Filter = 0;
                                                int.TryParse(listTradingConfig[a].NumValue, out Filter);
                                                Market.SymbolList[i].Filter = Filter;
                                            }
                                            #endregion

                                            #region Update Property Filtration Level Of Symbol
                                            if (listTradingConfig[a].Code == "S020")
                                            {
                                                int FiltrationsLevel = 0;
                                                int.TryParse(listTradingConfig[a].NumValue, out FiltrationsLevel);
                                                Market.SymbolList[i].FiltrationsLevel = FiltrationsLevel;
                                            }
                                            #endregion

                                            #region Update Property Auto Limit Of Symbol
                                            if (listTradingConfig[a].Code == "S021")
                                            {
                                                Market.SymbolList[i].AutoLimit = listTradingConfig[a].StringValue;
                                            }
                                            #endregion

                                            #region Update Property IsHedged Of Symbol
                                            if (listTradingConfig[a].Code == "S034")
                                            {
                                                bool IsHedged = false;
                                                if (listTradingConfig[a].BoolValue == 1)
                                                    IsHedged = true;
                                                Market.SymbolList[i].IsHedged = IsHedged;

                                                //RECALCULATION ALL INVESTOR
                                                Business.Investor.investorInstance.ReCalculationTotalMargin();
                                            }
                                            #endregion

                                            #region Update Property Trade Of Symbol
                                            if (listTradingConfig[a].Code == "S008")
                                            {
                                                Market.SymbolList[i].Trade = listTradingConfig[a].StringValue;
                                            }
                                            #endregion

                                            #region UPDATE PROPERTY APPLY SPREAD
                                            if (listTradingConfig[a].Code == "S050")
                                            {
                                                bool applySpread = false;
                                                if (listTradingConfig[a].BoolValue == 1)
                                                {
                                                    applySpread = true;
                                                }

                                                Business.Market.SymbolList[i].ApplySpread = applySpread;
                                            }
                                            #endregion

                                            #region UPDATE PROPERTY USE FREEZE MARGIN
                                            if (listTradingConfig[a].Code == "S051")
                                            {
                                                bool useFreezeMargin = false;
                                                if (listTradingConfig[a].BoolValue == 1)
                                                    useFreezeMargin = true;

                                                Business.Market.SymbolList[i].IsEnableFreezeMargin = useFreezeMargin;
                                            }
                                            #endregion

                                            #region UPDATE PROPERTY FREEZE MARGIN
                                            if (listTradingConfig[a].Code == "S052")
                                            {
                                                double freezeMargin = 0;
                                                double.TryParse(listTradingConfig[a].NumValue.ToString(), out freezeMargin);
                                                Business.Market.SymbolList[i].FreezeMargin = freezeMargin;
                                            }
                                            #endregion

                                            #region UPDATE PROPERTY FREEZE MARGIN HEDGED
                                            if (listTradingConfig[a].Code == "S053")
                                            {
                                                double freezeMarginH = 0;
                                                double.TryParse(listTradingConfig[a].NumValue, out freezeMarginH);
                                                Business.Market.SymbolList[i].FreezeMarginHedged = freezeMarginH;
                                            }
                                            #endregion

                                            #region UPDATE PROPERTY MARGIN HEDGED
                                            if (listTradingConfig[a].Code == "S028")
                                            {
                                                double marginH = 0;
                                                double.TryParse(listTradingConfig[a].NumValue, out marginH);
                                                Business.Market.SymbolList[i].MarginHedged = marginH;
                                            }
                                            #endregion

                                            //Update Value Parameter Item
                                            Market.SymbolList[i].ParameterItems[j].Name = listTradingConfig[a].Name;
                                            Market.SymbolList[i].ParameterItems[j].Code = listTradingConfig[a].Code;
                                            Market.SymbolList[i].ParameterItems[j].BoolValue = listTradingConfig[a].BoolValue;
                                            Market.SymbolList[i].ParameterItems[j].StringValue = listTradingConfig[a].StringValue;
                                            Market.SymbolList[i].ParameterItems[j].NumValue = listTradingConfig[a].NumValue;
                                            Market.SymbolList[i].ParameterItems[j].DateValue = listTradingConfig[a].DateValue;

                                            //Set ParameterID Again
                                            listTradingConfig[a].ParameterItemID = Market.SymbolList[i].ParameterItems[j].ParameterItemID;
                                            #endregion

                                            break;
                                        }
                                    }
                                }
                                #endregion

                                #region UPDATE CALCULATION SYMBOL
                                if (listTradingConfig[a].Code == "S025" || listTradingConfig[a].Code == "S026" ||
                                    listTradingConfig[a].Code == "S027" || listTradingConfig[a].Code == "S028" ||
                                    listTradingConfig[a].Code == "S029" || listTradingConfig[a].Code == "S030" ||
                                    listTradingConfig[a].Code == "S031" || listTradingConfig[a].Code == "S032" ||
                                    listTradingConfig[a].Code == "S034")
                                {
                                    List<Business.Investor> listInvestor = new List<Investor>();
                                    if (Business.Market.SymbolList[i].CommandList != null && Business.Market.SymbolList[i].CommandList.Count > 0)
                                    {
                                        int countCommand = Business.Market.SymbolList[i].CommandList.Count;
                                        for (int j = 0; j < countCommand; j++)
                                        {
                                            bool flagInvestor = false;
                                            Business.Market.SymbolList[i].CommandList[j].CalculatorMarginCommand(Business.Market.SymbolList[i].CommandList[j]);

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

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

                                    if (listInvestor != null && listInvestor.Count > 0)
                                    {
                                        int countInvestor = listInvestor.Count;
                                        for (int j = 0; j < countInvestor; j++)
                                        {
                                            if (listInvestor[j].CommandList != null && listInvestor[j].CommandList.Count > 0)
                                            {
                                                int countCommand = listInvestor[j].CommandList.Count;
                                                for (int n = 0; n < countCommand; n++)
                                                {
                                                    if (listInvestor[j].CommandList[n].Symbol.Name == Market.SymbolList[i].Name)
                                                    {
                                                        listInvestor[j].CommandList[n].CalculatorMarginCommand(listInvestor[j].CommandList[n]);
                                                    }
                                                }
                                            }

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

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

                                            //SEND NOTIFY TO CLIENT
                                            TradingServer.Business.Market.SendNotifyToClient("IAC04332451", 3, listInvestor[j].InvestorID);
                                        }
                                    }
                                }
                                #endregion

                                #region UPDATE TRADING CONFIG TO DATABASE AND INIT TIME EVENT IN SYSTEM
                                Result = ParameterItem.DBWTradingConfigInstance.UpdateTradingConfig(listTradingConfig[a].ParameterItemID, listTradingConfig[a].SecondParameterID, -1,
                                    listTradingConfig[a].Name, listTradingConfig[a].Code, listTradingConfig[a].BoolValue, listTradingConfig[a].StringValue, listTradingConfig[a].NumValue,
                                    listTradingConfig[a].DateValue);

                                if (listTradingConfig[a].Code == "S042" || listTradingConfig[a].Code == "S043")
                                {
                                    //Call Function Init Time Event Symbol
                                    Business.Market.marketInstance.InitTimeEventInSymbol();

                                    //CALL FUNCTION INIT TIME MARKET
                                    Business.Market.marketInstance.InitTimeEventServer();
                                }

                                if (listTradingConfig[a].Code == "S044" || listTradingConfig[a].Code == "S045")
                                {
                                    Business.Market.marketInstance.InitSymbolFuture();
                                }
                                #endregion

                                break;
                            }
                            else
                            {
                                //Find In Reference Symbol
                                this.UpdateTradingConfigReference(listTradingConfig[a], Market.SymbolList[i].RefSymbol);
                            }
                        }
                    }
                }
            }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

                                    Result = true;
                                }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            return Result;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Command"></param>
        public void AddCommand(OpenTrade Command)
        {
            string comment = string.Empty;
            string mode = string.Empty;
            string content = string.Empty;            

            #region Set Property IsBuy And CommandType Send To Client
            bool IsBuy = false;
            string CommandType = "SellFuture";
            switch (Command.Type.ID)
            {
                case 11:
                    IsBuy = true;
                    CommandType = "BuyFuture";
                    break;
                case 12:
                    IsBuy = false;
                    CommandType = "SellFuture";
                    break;
                case 17:
                    IsBuy = true;
                    CommandType = "BuyStopFutureCommand";
                    break;
                case 18:
                    IsBuy = false;
                    CommandType = "SellStopFutureCommand";
                    break;
                case 19:
                    IsBuy = true;
                    CommandType = "BuyLimitFutureCommand";
                    break;
                case 20:
                    IsBuy = false;
                    CommandType = "SellLimitFutureCommand";
                    break;
            }            
            #endregion

            int Result = -1;

            Command.IsClose = false;
            if (!Command.IsReOpen)
                Command.OpenTime = DateTime.Now;

            Command.CloseTime = Command.OpenTime;            
            Command.Taxes = 0;

            if (string.IsNullOrEmpty(Command.Comment))
                Command.Comment = "[future command]";

            #region Find Price Close Of Symbol
            switch (Command.Type.ID)
            {
                case 11:
                    Command.ClosePrice = Command.Symbol.TickValue.Bid;
                    comment = "[future buy]";
                    mode = "buy future";
                    break;
                case 12:
                    Command.ClosePrice = Command.Symbol.TickValue.Ask;
                    comment = "[future sell]";
                    mode = "sell future";
                    break;     
                case 17:    //BUY STOP FUTURE COMMAND
                    Command.ClosePrice = Command.Symbol.TickValue.Bid;
                    comment = "[buy stop future command]";
                    mode = "buy stop future";
                    break;
                case 18:    //SELL STOP FUTURE COMMAND
                    Command.ClosePrice = Command.Symbol.TickValue.Ask;
                    comment = "[sell stop future command]";
                    mode = "sell stop future";
                    break;
                case 19:    //BUY LIMIT FUTURE COMMAND
                    Command.ClosePrice = Command.Symbol.TickValue.Bid;
                    comment = "[buy limit future command]";
                    mode = "buy limit future";
                    break;
                case 20:    //SELL LIMIT FUTURE COMMAND
                    Command.ClosePrice = Command.Symbol.TickValue.Ask;
                    comment = "[sell limit future command]";
                    mode = "sell limit future";
                    break;
            }
            #endregion

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

            #region CHECK ISCLOSE ONLY OF FUTURE MARKET AREA
            if (Command.Symbol.isCloseOnlyFuture)
            {
                #region INSERT SYSTEM LOG 
                content = "'" + Command.Investor.Code + "': " + mode + " " + size + " " + Command.Symbol.Name + " at " + openPrice +
                               " sl: " + stopLoss + " tp: " + takeProfit + " (" + bid + "/" + ask + ") unsuccessful [symbol close only]";
                comment = "[symbol close only]";

                TradingServer.Facade.FacadeAddNewSystemLog(5, content, comment, Command.Investor.IpAddress, Command.Investor.Code);
                #endregion

                #region CHECK SYMBOL CLOSE ONLY
                if (Command.IsServer)
                {
                    string Message = "AddCommandByManager$False,AFC00001," + 0 + "," + Command.Investor.InvestorID + "," +
                           Command.Symbol.Name + "," + Command.Size + "," + IsBuy + "," + Command.OpenTime + "," + Command.OpenPrice + "," + Command.StopLoss + "," +
                               Command.TakeProfit + "," + Command.ClosePrice + "," + Command.Commission + "," + Command.Swap + "," + Command.Profit + "," + "Comment," + Command.ID + "," +
                               CommandType + "," + 1 + "," + Command.ExpTime + "," + Command.ClientCode + "," + 0 + "," + Command.IsHedged + "," + Command.Type.ID + "," + Command.Margin + ",Open";

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

                    Command.Investor.ClientCommandQueue.Add(Message);

                    return;
                }
                else
                {
                    string Message = "AddCommand$False,AFC00001," + 0 + "," + Command.Investor.InvestorID + "," +
                           Command.Symbol.Name + "," + Command.Size + "," + IsBuy + "," + Command.OpenTime + "," + Command.OpenPrice + "," + Command.StopLoss + "," +
                               Command.TakeProfit + "," + Command.ClosePrice + "," + Command.Commission + "," + Command.Swap + "," + Command.Profit + "," + "Comment," + Command.ID + "," +
                               CommandType + "," + 1 + "," + Command.ExpTime + "," + Command.ClientCode + "," + 0 + "," + Command.IsHedged + "," + Command.Type.ID + "," + Command.Margin + ",Open";

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

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

            #region GET TIME EXP AND COMPARE WITH TIME OPEN COMMAND
            //Set Exp Time
            if (Command.Symbol.ParameterItems != null)
            {
                int countParameter = Command.Symbol.ParameterItems.Count;
                for (int i = 0; i < countParameter; i++)
                {
                    if (Command.Symbol.ParameterItems[i].Code == "S045")
                    {
                        if (Command.Symbol.ParameterItems[i].DateValue < DateTime.Now)
                        {
                            #region INSERT SYSTEM LOG IF MAKE FUTURE COMMAND UNCOMPLETE
                            content = "'" + Command.Investor.Code + "': " + mode + " " + size + " " + Command.Symbol.Name + " at " + openPrice +
                               " sl: " + stopLoss + " tp: " + takeProfit + " (" + bid + "/" + ask + ") unsuccessful [symbol exp time]";
                            comment = "[symbol exp time]";

                            TradingServer.Facade.FacadeAddNewSystemLog(5, content, comment, Command.Investor.IpAddress, Command.Investor.Code);
                            #endregion

                            #region COMPARE TIME EXP WITH TIME CURRENT
                            if (Command.IsServer)
                            {
                                string Message = "AddCommandByManager$False,AFC00002," + 0 + "," + Command.Investor.InvestorID + "," +
                                       Command.Symbol.Name + "," + Command.Size + "," + IsBuy + "," + Command.OpenTime + "," + Command.OpenPrice + "," + Command.StopLoss + "," +
                                           Command.TakeProfit + "," + Command.ClosePrice + "," + Command.Commission + "," + Command.Swap + "," + Command.Profit + "," + "Comment," + Command.ID + "," +
                                           CommandType + "," + 1 + "," + Command.ExpTime + "," + Command.ClientCode + "," + 0 + "," + Command.IsHedged + "," + Command.Type.ID + "," + Command.Margin + ",Open";

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

                                Command.Investor.ClientCommandQueue.Add(Message);

                                return;
                            }
                            else
                            {
                                string Message = "AddCommand$False,AFC00002," + 0 + "," + Command.Investor.InvestorID + "," +
                                       Command.Symbol.Name + "," + Command.Size + "," + IsBuy + "," + Command.OpenTime + "," + Command.OpenPrice + "," + Command.StopLoss + "," +
                                           Command.TakeProfit + "," + Command.ClosePrice + "," + Command.Commission + "," + Command.Swap + "," + Command.Profit + "," + "Comment," + Command.ID + "," +
                                           CommandType + "," + 1 + "," + Command.ExpTime + "," + Command.ClientCode + "," + 0 + "," + Command.IsHedged + "," + Command.Type.ID + "," + Command.Margin + ",Open";

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

                                Command.Investor.ClientCommandQueue.Add(Message);
                                return;
                            }
                            #endregion                            
                        }
                        else
                        {
                            Command.ExpTime = Command.Symbol.ParameterItems[i].DateValue;
                        }

                        break;
                    }
                }
            }
            #endregion            

            if (Command.Type.ID == 11 || Command.Type.ID == 12)
            {
                //Calculator Profit Of Command        
                Command.CalculatorProfitCommand(Command);
                Command.Profit = Command.Symbol.ConvertCurrencyToUSD(Command.Symbol.Currency, Command.Profit, false, Command.SpreaDifferenceInOpenTrade, Command.Symbol.Digit);

                #region CALCULATION MARGIN FOR COMMAND
                //Call Function Calculator Margin Of Command            
                Command.CalculatorMarginCommand(Command);
                #endregion

                Command.Commission = Model.CalculationFormular.Instance.CalculationCommission(Command); 
            }
 
            string CommandCode = string.Empty;

            #region REND CLIENT CODE IF CLIENT CODE == EMPTY
            if (string.IsNullOrEmpty(Command.ClientCode))
            {
                string tempCode = string.Empty;
                Random ran = new Random();
                int tempRan = ran.Next(0000000, 9999999);
                Command.ClientCode = Command.Investor.InvestorID + "_" + tempRan;
                bool isOK = false;

                while (!isOK)
                {
                    if (Command.Investor.CommandList != null && Command.Investor.CommandList.Count > 0)
                    {
                        int count = Command.Investor.CommandList.Count;
                        for (int i = 0; i < count; i++)
                        {
                            if (Command.Investor.CommandList[i].ClientCode == Command.ClientCode)
                            {
                                Command.ClientCode = Command.Investor.InvestorID + "_" + ran.Next(0000000, 9999999);
                                break;
                            }
                        }

                        isOK = true;
                    }
                    else
                    {
                        isOK = true;
                    }
                }
            }
            #endregion            

            #region Add Command To Database And Build Command Code
            //Add Command To Database
            //                
            Result = TradingServer.Facade.FacadeAddNewOpenTrade(Command);

            Command.ID = Result;

            //Call Function Update Command Code Of Command
            CommandCode = TradingServer.Model.TradingCalculate.Instance.BuildCommandCode(Result.ToString());
            TradingServer.Facade.FacadeUpdateCommandCode(Result, CommandCode);
            #endregion

            if (Result > 0)
            {
                double spreaDifferenceInCommand = Model.CommandFramework.CommandFrameworkInstance.GetSpreadDifference(Command.Symbol.SecurityID, Command.Investor.InvestorGroupInstance.InvestorGroupID);

                #region Build Command And Add Command To CommandList In Symbol And Command List In Investor
                //Build Two Instance OpenTrade
                //One Instance For Investor 
                //One Instance For Symbol And MarketArea
                #region Build Instance OpenTrade For Investor
                Business.OpenTrade newOpenTradeInvestor = new OpenTrade();
                newOpenTradeInvestor.ID = Result;
                newOpenTradeInvestor.ClientCode = Command.ClientCode;
                newOpenTradeInvestor.ClosePrice = Command.ClosePrice;
                newOpenTradeInvestor.CloseTime = Command.CloseTime;
                newOpenTradeInvestor.CommandCode = CommandCode;
                newOpenTradeInvestor.Commission = Command.Commission;
                newOpenTradeInvestor.ExpTime = Command.ExpTime;                
                newOpenTradeInvestor.Investor = Command.Investor;
                newOpenTradeInvestor.IsClose = Command.IsClose;
                newOpenTradeInvestor.OpenPrice = Command.OpenPrice;
                newOpenTradeInvestor.OpenTime = Command.OpenTime;
                newOpenTradeInvestor.Profit = Command.Profit;
                newOpenTradeInvestor.Size = Command.Size;
                newOpenTradeInvestor.StopLoss = Command.StopLoss;
                newOpenTradeInvestor.Swap = Command.Swap;
                newOpenTradeInvestor.Symbol = Command.Symbol;
                newOpenTradeInvestor.TakeProfit = Command.TakeProfit;
                newOpenTradeInvestor.Type = new TradeType();
                newOpenTradeInvestor.Type.ID = Command.Type.ID;
                newOpenTradeInvestor.Type.Name = Command.Type.Name;                
                newOpenTradeInvestor.Margin = Command.Margin;
                newOpenTradeInvestor.IGroupSecurity = Command.IGroupSecurity;
                newOpenTradeInvestor.Commission = Command.Commission;
                newOpenTradeInvestor.IsHedged = Command.IsHedged;
                newOpenTradeInvestor.SpreaDifferenceInOpenTrade = spreaDifferenceInCommand;
                newOpenTradeInvestor.AgentCommission = Command.AgentCommission;
                #endregion

                #region Find Investor In Investor List And Add Command To Investor List
                //Find Investor In Investor List And Add Command To Investor List
                if (Business.Market.InvestorList != null)
                {
                    int countInvestor = Business.Market.InvestorList.Count;
                    for (int n = 0; n < countInvestor; n++)
                    {
                        if (Business.Market.InvestorList[n].InvestorID == newOpenTradeInvestor.Investor.InvestorID)
                        {
                            if (Business.Market.InvestorList[n].CommandList != null)
                            {
                                Business.Market.InvestorList[n].CommandList.Add(newOpenTradeInvestor);
                            }
                            else
                            {
                                Business.Market.InvestorList[n].CommandList = new List<OpenTrade>();
                                Business.Market.InvestorList[n].CommandList.Add(newOpenTradeInvestor);
                            }

                            break;
                        }
                    }
                }
                #endregion

                #region Build Instance Open Trade For Symbol
                Business.OpenTrade newOpenTradeSymbol = new OpenTrade();
                newOpenTradeSymbol.ID = Result;
                newOpenTradeSymbol.ClientCode = Command.ClientCode;
                newOpenTradeSymbol.ClosePrice = Command.ClosePrice;
                newOpenTradeSymbol.CloseTime = Command.CloseTime;
                newOpenTradeSymbol.CommandCode = CommandCode;
                newOpenTradeSymbol.Commission = Command.Commission;
                newOpenTradeSymbol.ExpTime = Command.ExpTime;                
                newOpenTradeSymbol.Investor = Command.Investor;
                newOpenTradeSymbol.IsClose = false;
                newOpenTradeSymbol.OpenPrice = Command.OpenPrice;
                newOpenTradeSymbol.OpenTime = Command.OpenTime;
                newOpenTradeSymbol.Profit = Command.Profit;
                newOpenTradeSymbol.Size = Command.Size;
                newOpenTradeSymbol.StopLoss = Command.StopLoss;
                newOpenTradeSymbol.Swap = Command.Swap;
                newOpenTradeSymbol.Symbol = Command.Symbol;
                newOpenTradeSymbol.TakeProfit = Command.TakeProfit;
                newOpenTradeSymbol.Type = new TradeType();
                newOpenTradeSymbol.Type.ID = Command.Type.ID;
                newOpenTradeSymbol.Type.Name = Command.Type.Name;                
                newOpenTradeSymbol.Margin = Command.Margin;
                newOpenTradeSymbol.IGroupSecurity = Command.IGroupSecurity;
                newOpenTradeSymbol.Commission = Command.Commission;
                newOpenTradeSymbol.IsHedged = Command.IsHedged;
                newOpenTradeSymbol.SpreaDifferenceInOpenTrade = spreaDifferenceInCommand;
                newOpenTradeSymbol.AgentCommission = Command.AgentCommission;
                #endregion

                #region Find Symbol In Market And Add Command To Market Area And List Symbol
                //Find Symbol In Market And Add Command To Market Area And List Symbol
                if (Business.Market.SymbolList != null)
                {
                    int countSymbol = Business.Market.SymbolList.Count;
                    for (int i = 0; i < countSymbol; i++)
                    {
                        if (Business.Market.SymbolList[i].SymbolID == newOpenTradeSymbol.Symbol.SymbolID)
                        {
                            if (Business.Market.SymbolList[i].CommandList != null)
                            {
                                Business.Market.SymbolList[i].CommandList.Add(newOpenTradeSymbol);
                            }
                            else
                            {
                                Business.Market.SymbolList[i].CommandList = new List<OpenTrade>();
                                Business.Market.SymbolList[i].CommandList.Add(newOpenTradeSymbol);
                            }
                            break;
                        }
                    }
                }
                #endregion

                #region Build Instance Open Trade For Command Executor
                Business.OpenTrade newOpenTradeExe = new OpenTrade();
                newOpenTradeExe.ID = Result;
                newOpenTradeExe.ClientCode = Command.ClientCode;
                newOpenTradeExe.ClosePrice = Command.ClosePrice;
                newOpenTradeExe.CloseTime = Command.CloseTime;
                newOpenTradeExe.CommandCode = CommandCode;
                newOpenTradeExe.Commission = Command.Commission;
                newOpenTradeExe.ExpTime = Command.ExpTime;                
                newOpenTradeExe.Investor = Command.Investor;
                newOpenTradeExe.IsClose = false;
                newOpenTradeExe.OpenPrice = Command.OpenPrice;
                newOpenTradeExe.OpenTime = Command.OpenTime;
                newOpenTradeExe.Profit = Command.Profit;
                newOpenTradeExe.Size = Command.Size;
                newOpenTradeExe.StopLoss = Command.StopLoss;
                newOpenTradeExe.Swap = Command.Swap;
                newOpenTradeExe.Symbol = Command.Symbol;
                newOpenTradeExe.TakeProfit = Command.TakeProfit;
                newOpenTradeExe.Type = new TradeType();
                newOpenTradeExe.Type.ID = Command.Type.ID;
                newOpenTradeExe.Type.Name = Command.Type.Name;                
                newOpenTradeExe.Margin = Command.Margin;
                newOpenTradeExe.IGroupSecurity = Command.IGroupSecurity;
                newOpenTradeExe.Commission = Command.Commission;
                newOpenTradeExe.IsHedged = Command.IsHedged;
                newOpenTradeExe.SpreaDifferenceInOpenTrade = spreaDifferenceInCommand;
                newOpenTradeExe.AgentCommission = Command.AgentCommission;
                #endregion

                Business.Market.CommandExecutor.Add(newOpenTradeExe);

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

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

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

                    Command.Investor.ClientCommandQueue.Add(Message);
                }
                else
                {
                    string Message = "AddCommand$True,Add New Command Complete," + Result + "," + Command.Investor.InvestorID + "," +
                        Command.Symbol.Name + "," + Command.Size + "," + IsBuy + "," + Command.OpenTime + "," + Command.OpenPrice + "," + Command.StopLoss + "," +
                            Command.TakeProfit + "," + Command.ClosePrice + "," + Command.Commission + "," + Command.Swap + "," + Command.Profit + "," + "Comment," + Command.ID + "," +
                            CommandType + "," + 1 + "," + Command.ExpTime + "," + Command.ClientCode + "," + CommandCode + "," + Command.IsHedged + "," + Command.Type.ID + "," + Command.Margin + ",Open";

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

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

                Command.IsHedged = Command.Symbol.IsHedged;

                //Command.Investor.UpdateCommand(Command);
                Business.Margin newMargin = new Margin();
                newMargin = Command.Symbol.CalculationTotalMargin(Command.Investor.CommandList);
                Command.Investor.Margin = newMargin.TotalMargin;
                Command.Investor.FreezeMargin = newMargin.TotalFreezeMargin;

                //Business.RequestDealer Request = new RequestDealer();
                //NOTIFY INVESTOR TO MANAGER
                TradingServer.Facade.FacadeSendNotifyManagerRequest(3, Command.Investor);

                //SEND NOTIFY TO MANAGER THEN ADD NEW ACCOUNT
                TradingServer.Facade.FacadeSendNoticeManagerRequest(1, Command);
                #endregion

                #region INSERT SYSTEM LOG IF MAKE FUTURE COMMAND COMPLETE
                content = "'" + Command.Investor.Code + "': future order #" + CommandCode + " " + mode + " " + size + " " + Command.Symbol.Name + " at " + openPrice + " commission: " + Command.Commission;
                TradingServer.Facade.FacadeAddNewSystemLog(5, content, comment, Command.Investor.IpAddress, Command.Investor.Code);
                #endregion                
            }
            else
            {
                #region Return Error Can't Insert Command To Database For Client
                //Add Result To Client Command Queue Of Investor
                string Message = "AddCommand$False,Can't Insert Database," + Result + "," + Command.Investor.InvestorID + "," +
                    Command.Symbol.Name + "," + Command.Size + "," + IsBuy + "," + Command.OpenTime + "," + Command.OpenPrice + "," + Command.StopLoss + "," +
                        Command.TakeProfit + "," + Command.ClosePrice + "," + Command.Commission + "," + Command.Swap + "," + Command.Profit + "," + "Comment," + Command.ID + "," +
                        CommandType + "," + 1 + "," + Command.ExpTime + "," + Command.ClientCode + "," + "0000000," + Command.IsHedged + "," + Command.Type.ID + "," + Command.Margin + ",Open";

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

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