//Delete a record
        public void DeleteTrade(Trade trade)
        {
            try
            {
                using (var unitOfWork = new EFUnitOfWork())
                {
                    var tradeRepo =
                        new TradeRepository(new EFRepository <Trade>(), unitOfWork);
                    {
                        ObjectSet <Trade> tradeObj =
                            ((CurrentDeskClientsEntities)tradeRepo.Repository.UnitOfWork.Context).Trades;
                        var getOldTrade = tradeObj.Where(t => t.OrderID == trade.OrderID).FirstOrDefault();

                        if (getOldTrade != null)
                        {
                            tradeRepo.Delete(getOldTrade);
                            tradeRepo.Save();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                CommonErrorLogger.CommonErrorLog(ex, System.Reflection.MethodBase.GetCurrentMethod().Name);
            }
        }
        /// <summary>
        /// This method returns list of logins that has made
        /// trade in particular time period
        /// </summary>
        /// <param name="checkDate"></param>
        /// <returns></returns>
        public List <int> GetAllLoginsTradedToday(DateTime checkDate)
        {
            DateTime lastCheckDate = DateTime.UtcNow;

            try
            {
                using (var unitOfWork = new EFUnitOfWork())
                {
                    var tradeRepo =
                        new TradeRepository(new EFRepository <Trade>(), unitOfWork);

                    ObjectSet <Trade> tradeObjSet =
                        ((CurrentDeskClientsEntities)tradeRepo.Repository.UnitOfWork.Context).Trades;

                    var dateNow = DateTime.UtcNow.Date;

                    return(tradeObjSet.Where(trd => trd.TradeDateTime >= dateNow || trd.TradeDateTime >= checkDate).GroupBy(x => x.Login).Select(x => (int)x.Key).ToList());
                }
            }
            catch (Exception ex)
            {
                CommonErrorLogger.CommonErrorLog(ex, System.Reflection.MethodBase.GetCurrentMethod().Name);
                throw;
            }
        }
        /// <summary>
        /// Update  an existing trade
        /// </summary>
        /// <param name="tr"></param>
        public void UpdateTrade(Trade tr)
        {
            try
            {
                using (var unitOfWork = new EFUnitOfWork())
                {
                    var tradeRepo =
                        new TradeRepository(new EFRepository <Trade>(), unitOfWork);

                    ObjectSet <Trade> tradeObj =
                        ((CurrentDeskClientsEntities)tradeRepo.Repository.UnitOfWork.Context).Trades;
                    var getOldTrade = tradeObj.Where(t => t.OrderID == tr.OrderID).FirstOrDefault();


                    if (getOldTrade != null)
                    {
                        getOldTrade.Activation      = tr.Activation;
                        getOldTrade.ClosePrice      = tr.ClosePrice;
                        getOldTrade.CloseTime       = tr.CloseTime;
                        getOldTrade.Cmd             = tr.Cmd;
                        getOldTrade.Comment         = tr.Comment;
                        getOldTrade.Commission      = tr.Commission;
                        getOldTrade.CommissionAgent = tr.CommissionAgent;
                        getOldTrade.ConvRates       = tr.ConvRates;
                        getOldTrade.ConvReserv      = tr.ConvReserv;
                        getOldTrade.Digits          = tr.Digits;
                        getOldTrade.Expiration      = tr.Expiration;
                        getOldTrade.InternalID      = tr.InternalID;
                        getOldTrade.Login           = tr.Login;
                        getOldTrade.Magic           = tr.Magic;
                        getOldTrade.MarginRate      = tr.MarginRate;
                        getOldTrade.OpenPrice       = tr.OpenPrice;
                        getOldTrade.OpenTime        = tr.OpenTime;
                        getOldTrade.OrderID         = tr.OrderID;
                        getOldTrade.Profit          = tr.Profit;
                        getOldTrade.Sl            = tr.Sl;
                        getOldTrade.Spread        = tr.Spread;
                        getOldTrade.State         = tr.State;
                        getOldTrade.Storage       = tr.Storage;
                        getOldTrade.Symbol        = tr.Symbol;
                        getOldTrade.Taxes         = tr.Taxes;
                        getOldTrade.Timestamp     = tr.Timestamp;
                        getOldTrade.Tp            = tr.Tp;
                        getOldTrade.ValueDate     = tr.ValueDate;
                        getOldTrade.Volume        = tr.Volume;
                        getOldTrade.TradeDateTime = tr.TradeDateTime;

                        tradeRepo.Save();
                    }
                }
            }
            catch (Exception ex)
            {
                CommonErrorLogger.CommonErrorLog(ex, System.Reflection.MethodBase.GetCurrentMethod().Name);
            }
        }
        /// <summary>
        /// This method inserts new open trade into Trades table
        /// </summary>
        /// <param name="trade">trade</param>
        public void InsertTrade(dynamic trade)
        {
            try
            {
                using (var unitOfWork = new EFUnitOfWork())
                {
                    var tradeRepo =
                        new TradeRepository(new EFRepository <Trade>(), unitOfWork);

                    Trade newTrade = new Trade();
                    newTrade.Activation      = trade.activation;
                    newTrade.ClosePrice      = trade.close_price;
                    newTrade.CloseTime       = trade.close_time;
                    newTrade.Cmd             = trade.cmd;
                    newTrade.Comment         = trade.comment;
                    newTrade.Commission      = trade.commission;
                    newTrade.CommissionAgent = trade.commission_agent;
                    newTrade.ConvRates       = trade.conv_rates[0];
                    newTrade.ConvReserv      = trade.conv_reserv;
                    newTrade.Digits          = trade.digits;
                    newTrade.Expiration      = trade.expiration;
                    newTrade.InternalID      = trade.internal_id;
                    newTrade.Login           = trade.login;
                    newTrade.Magic           = trade.magic;
                    newTrade.MarginRate      = trade.margin_rate;
                    newTrade.OpenPrice       = trade.open_price;
                    newTrade.OpenTime        = trade.open_time;
                    newTrade.OrderID         = trade.order;
                    newTrade.Profit          = trade.profit;
                    newTrade.Sl        = trade.sl;
                    newTrade.Spread    = trade.spread;
                    newTrade.State     = trade.state;
                    newTrade.Storage   = trade.storage;
                    newTrade.Symbol    = trade.symbol;
                    newTrade.Taxes     = trade.taxes;
                    newTrade.Timestamp = trade.timestamp;
                    newTrade.Tp        = trade.tp;
                    newTrade.ValueDate = trade.value_date;
                    newTrade.Volume    = trade.volume;

                    //Unix Epoch on January 1st, 1970
                    DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
                    newTrade.TradeDateTime = origin.AddSeconds(trade.timestamp);

                    tradeRepo.Add(newTrade);
                    tradeRepo.Save();
                }
            }
            catch (Exception ex)
            {
                CommonErrorLogger.CommonErrorLog(ex, System.Reflection.MethodBase.GetCurrentMethod().Name);
                throw;
            }
        }
        /// <summary>
        /// This Function Will get all the open trades related to the list of asset manager.
        /// </summary>
        /// <param name="loginIDList">AssetManager Platform LoginIDList</param>
        /// <param name="lastProcessedID">Last Processed ID</param>
        /// <returns>List of Open Trades</returns>
        public List <Trade> GetAssetManagerOpenTrades(List <int> loginIDList, int lastProcessedID)
        {
            try
            {
                using (var unitOfWork = new EFUnitOfWork())
                {
                    var tradeRepo =
                        new TradeRepository(new EFRepository <Trade>(), unitOfWork);

                    return(((CurrentDeskClientsEntities)tradeRepo.Repository.UnitOfWork.Context).Trades.
                           Where(x => loginIDList.Contains((int)x.Login) && x.PK_TradeID > lastProcessedID).OrderBy(x => x.PK_TradeID).ToList());
                }
            }
            catch (Exception exceptionMessage)
            {
                CommonErrorLogger.CommonErrorLog(exceptionMessage, System.Reflection.MethodBase.GetCurrentMethod().Name);
                return(null);
            }
        }
        // Add your own data access methods here.  If you wish to
        // expose your public method to a WCF service, marked them with
        // the attribute [NCPublish], and another T4 template will generate your service contract

        /// <summary>
        /// This method gets all trades from Trades table
        /// </summary>
        /// <returns></returns>
        public List <Trade> GetAllTrades()
        {
            try
            {
                using (var unitOfWork = new EFUnitOfWork())
                {
                    var tradeRepo =
                        new TradeRepository(new EFRepository <Trade>(), unitOfWork);

                    //Returning List Of Trades
                    return(tradeRepo.All().ToList());
                }
            }
            catch (Exception ex)
            {
                CommonErrorLogger.CommonErrorLog(ex, System.Reflection.MethodBase.GetCurrentMethod().Name);
                throw;
            }
        }
        /// <summary>
        /// Add a new trade
        /// </summary>
        /// <param name="trade"></param>
        public void AddTrade(Trade trade)
        {
            try
            {
                using (var unitOfWork = new EFUnitOfWork())
                {
                    var tradeRepo =
                        new TradeRepository(new EFRepository <Trade>(), unitOfWork);

                    tradeRepo.Add(trade);

                    tradeRepo.Save();
                }
            }
            catch (Exception ex)
            {
                CommonErrorLogger.CommonErrorLog(ex, System.Reflection.MethodBase.GetCurrentMethod().Name);
            }
        }
        public dynamic GetUserOpenTrades(int loginId)
        {
            dynamic lstTrades = new {
                Pnl       = 0.0m,
                Commision = 0.0m,
                Swap      = 0.0m
            };

            try
            {
                using (var unitOfWork = new EFUnitOfWork())
                {
                    var tradeRepo =
                        new TradeRepository(new EFRepository <Trade>(), unitOfWork);

                    ObjectSet <Trade> tradeObj =
                        ((CurrentDeskClientsEntities)tradeRepo.Repository.UnitOfWork.Context).Trades;


                    lstTrades = (from t in tradeObj
                                 where t.Login == loginId
                                 group t by t.Login into g
                                 select new
                    {
                        Pnl = g.Sum(p => p.Profit),
                        Commision = g.Sum(p => p.Commission),
                        Swap = g.Sum(p => p.Storage)
                    }).FirstOrDefault();
                }
            }
            catch (Exception ex)
            {
                CommonErrorLogger.CommonErrorLog(ex, System.Reflection.MethodBase.GetCurrentMethod().Name);
            }

            return(lstTrades);
        }
        //Synch Trades table
        public void SaveOpenTrades(List <Trade> lstTrade)
        {
            try
            {
                using (var unitOfWork = new EFUnitOfWork())
                {
                    var tradeRepo =
                        new TradeRepository(new EFRepository <Trade>(), unitOfWork);

                    ObjectSet <Trade> tradeObj =
                        ((CurrentDeskClientsEntities)tradeRepo.Repository.UnitOfWork.Context).Trades;

                    var lstDBTrade = tradeObj.ToList();


                    var lstNewTrades = lstTrade.Where(nt => !lstDBTrade.Select(db => db.OrderID).Contains(nt.OrderID)).ToList();
                    foreach (var trades in lstNewTrades)
                    {
                        tradeRepo.Add(trades);
                    }

                    // Delete close trades
                    var lstOldDBTrades = lstDBTrade.Where(od => !lstTrade.Select(db => db.OrderID).Contains(od.OrderID)).ToList();
                    foreach (var od in lstOldDBTrades)
                    {
                        tradeRepo.Delete(od);
                    }

                    tradeRepo.Save();
                }
            }
            catch (Exception ex)
            {
                CommonErrorLogger.CommonErrorLog(ex, System.Reflection.MethodBase.GetCurrentMethod().Name);
            }
        }