public AnalyticsViewModel()
        {
            Factory = new SourceInstrumentDataFactory();

            Mediator.Instance.PortfolioChanged += (s, e) =>
            {
                UpdateCurrentPortfolio(e.Portfolio);
                Positions = RepositoryReader.LoadPositions(e.Portfolio);
                UpdateCurrentPortfolioPrices(e.Portfolio);
                PortfolioProfit = PortfolioAnalytics.CalculatePortFolioProfit(Positions);
                PortfolioReturn = PortfolioAnalytics.CalculatePortfolioReturn(Positions);
                s = this;
                e.AnalyticList = HistoricalReturnSeries;
            };



            Mediator.Instance.AddPosition += (s, e) =>
            {
                Positions.Add(e.Position);
                PortfolioProfit = PortfolioAnalytics.CalculatePortFolioProfit(Positions);
                PortfolioReturn = PortfolioAnalytics.CalculatePortfolioReturn(Positions);
                s = this;
                e.AnalyticList = HistoricalReturnSeries;
            };
        }
 private Position UpdatePositionCurrentPrice(Position position)
 {
     InstrumentHistoricalData          = Factory.OutPutSourceInstrumentData(position.InstrumentRepositoryPath, position.Ticker, 2, position.InstrumentFileType);
     position.CurrentPrice             = InstrumentHistoricalData.Last().Close;
     position.CurrentOpenPositionValue = PortfolioAnalytics.CalculateCurrentOpenPositionValue(position);
     position.Profit     = PortfolioAnalytics.CalculatePositionProfit(position);
     position.ReturnRate = PortfolioAnalytics.CalculatePositionReturn(position);
     return(position);
 }
        private Position UpdatePositionInstrumentData(Position position)
        {
            InstrumentHistoricalData = new List <SourceInstrumentQuote>();
            decimal positionDuration = PortfolioAnalytics.CalculatePositionDuration(position.EntryDate);

            InstrumentHistoricalData          = Factory.OutPutSourceInstrumentData(position.InstrumentRepositoryPath, position.Ticker, Convert.ToInt32(positionDuration), position.InstrumentFileType);
            position.CurrentPrice             = InstrumentHistoricalData.Last().Close;
            position.CurrentOpenPositionValue = PortfolioAnalytics.CalculateCurrentOpenPositionValue(position);
            position.Profit     = PortfolioAnalytics.CalculatePositionProfit(position);
            position.ReturnRate = PortfolioAnalytics.CalculatePositionReturn(position);
            return(position);
        }
        /// <summary>
        /// Updates the instrument data for the portfolio and populates a collection of daily returns
        /// </summary>
        /// <param name="portfolio"></param>
        private void UpdateCurrentPortfolioPrices(Portfolio portfolio)
        {
            HistoricalReturnSeries = new List <Analytic>();
            if (Positions.Count() > 0)
            {
                foreach (Position position in Positions)
                {
                    UpdatePositionInstrumentData(position);
                    CalculateHistoricalReturn(position);
                }


                HistoricalReturnSeries = PortfolioAnalytics.CombineAnalyticsInParallel(CombinedAnalytic).ToList();
            }

            else
            {
                HistoricalReturnSeries.Clear();
            }
        }