using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.Threading.Tasks;
using Forms=System.Windows.Forms;
using System.Threading;
using System.Windows;
using System.Windows.Media;
using System.IO;
using MarketData;
using MarketData.Utils;
using MarketData.MarketDataModel;
using MarketData.Generator;
using MarketData.DataAccess;
using TradeBlotter.DataAccess;
using TradeBlotter.Command;
using TradeBlotter.Model;
using Microsoft.Research.DynamicDataDisplay.DataSources;
using System.Windows.Threading;
using MarketData.Generator.Momentum;
using TradeBlotter.UIUtils;
using MarketData.Generator.Model;
using MarketData.Generator.MGSHMomentum;
using MarketData.Generator.Interface;
using StopLimit=MarketData.MarketDataModel.StopLimit;
using TradeBlotter.Extensions;

namespace TradeBlotter.ViewModels
{
  public class MGSHMomentumViewModel : WorkspaceViewModel
  {
// generic section
    private const String DISPLAY_NAME = "MGSHMomentum Model";
    private bool busyIndicator = false;
    private String busyContent=null;
    private String initialPath=null;
    private String pathFileName=null;

    private ObservableCollection<MGSHMomentumCandidate> momentumCandidates=new ObservableCollection<MGSHMomentumCandidate>();
    private MGSHMomentumCandidate selectedItem=null;
    private RelayCommand runCommand;
    private RelayCommand stochasticsCommand;
    private RelayCommand relativeStrengthCommand;
    private RelayCommand macdCommand;
    private RelayCommand bollingerBandCommand;
    private RelayCommand priceHistoryCommand;
    private RelayCommand stickerValuationCommand;
    private RelayCommand dcfValuationCommand;
    private RelayCommand dividendHistoryCommand;
    private RelayCommand analystRatingsCommand;
    private RelayCommand addToWatchListCommand;
    private RelayCommand removeFromWatchListCommand;
    private RelayCommand movingAverageCommand;
    private RelayCommand displayHistoricalCommand;
// session section
    private ModelStatistics modelStatistics=null;
    private NVPDictionary nvpDictionary=null;
    private ObservableCollection<String> nvpDictionaryKeys = default;
    private MGSHConfiguration configuration=null;
    private MGSHSessionParams sessionParams;
    private String selectedParameter=null;
    private MGSHPositionModelCollection positions=null;
    private MGSHPositionModel selectedPosition=null;
    private ObservableCollection<String> monitorIntervals;
    private String selectedMonitorInterval;
    private bool monitorRunning=false;
    private DispatcherTimer dispatcherTimer = new DispatcherTimer(); 

    private RelayCommand loadFileCommand;
    private RelayCommand reloadCommand;
    private RelayCommand monitorCommand;

    private RelayCommand stochasticsCommandPosition;
    private RelayCommand relativeStrengthCommandPosition;
    private RelayCommand macdCommandPosition;
    private RelayCommand bollingerBandCommandPosition;
    private RelayCommand bollingerBandCommandPositionAll;
    private RelayCommand priceHistoryCommandPosition;
    private RelayCommand stickerValuationCommandPosition;
    private RelayCommand dcfValuationCommandPosition;
    private RelayCommand dividendHistoryCommandPosition;
    private RelayCommand analystRatingsCommandPosition;
    private RelayCommand addToWatchListCommandPosition;
    private RelayCommand removeFromWatchListCommandPosition;
    private RelayCommand movingAverageCommandPosition;
    private RelayCommand displayHistoricalCommandPosition;
    private RelayCommand displayHeadlinesCommandPosition;
    private RelayCommand editPositionCommandPosition;
    private RelayCommand closePositionCommandPosition;

// chart plotter
    private bool showAsGainLoss=true;
    private bool isLegendVisible=false;
    private RelayCommand toggleReturnOrPercentCommand=null;  
    private ModelPerformanceSeries modelPerformanceSeries=null;

    public MGSHMomentumViewModel(bool loadedFromParams=false)
    {
      PropertyChanged += OnMomentumViewModelPropertyChanged;
      base.DisplayName = DISPLAY_NAME;
      monitorIntervals=new ObservableCollection<String>();
      monitorIntervals.Add("30");
      monitorIntervals.Add("60");
      monitorIntervals.Add("90");
      monitorIntervals.Add("120");
      selectedMonitorInterval=monitorIntervals[0];
      configuration=new MGSHConfiguration();
      NVPCollection nvpCollection=configuration.ToNVPCollection();
      nvpDictionary=nvpCollection.ToDictionary();
      List<String> dictionaryKeys=new List<String>(nvpDictionary.Keys);
      dictionaryKeys.Sort();
      nvpDictionaryKeys=new ObservableCollection<String>(dictionaryKeys);
      selectedParameter=nvpDictionaryKeys[0];
      dispatcherTimer.Tick += DispatcherTimerTick;
      dispatcherTimer.Interval = new TimeSpan(0, 0, int.Parse(selectedMonitorInterval));
      base.OnPropertyChanged("MonitorIntervals");
      base.OnPropertyChanged("SelectedMonitorInterval");
      base.OnPropertyChanged("Parameters");
      base.OnPropertyChanged("SelectedParameter");
      base.OnPropertyChanged("ParameterValue");
      base.OnPropertyChanged("LastTradeDate");
      base.OnPropertyChanged("NextTradeDate");
    }

    protected override void OnDispose()
    {
      StopMonitor();
      base.OnDispose();
    }

// ******************************************************************************************** P E R S I S T E N C E ********************************************************************************************
    public override bool CanPersist()
    {
      return true;
    }

    public override SaveParameters GetSaveParameters()
    {
      SaveParameters saveParams = new SaveParameters();
      if (null == pathFileName) return null;
      saveParams.Add(new KeyValuePair<String, String>("Type",GetType().Namespace+"."+GetType().Name));
      saveParams.Add(new KeyValuePair<String, String>("PathFileName", pathFileName));
      return saveParams;    
    }

    public override void SetSaveParameters(SaveParameters saveParameters)
    {
      try
      {
        pathFileName = (from KeyValuePair<String, String> item in saveParameters where item.Key.Equals("PathFileName") select item).FirstOrDefault().Value;
        if(!LoadSessionFile())pathFileName=null;
      }
      catch (Exception exception)
      {
        MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Exception:{0}",exception.ToString()));
      }
    }
// **************************************************************************************************************************************************************************
    public bool BusyIndicator
    {
      get 
      { 
        return busyIndicator; 
      }
      set
      {
        busyIndicator = value;
        base.OnPropertyChanged("BusyIndicator");
      }
    }

    public String BusyContent
    {
      get
      {
        return busyContent;
      }
      set
      {
        busyContent=value;
        base.OnPropertyChanged("BusyContent");
      }
    }
// **************************************************************************************************************************************************************************
    public ObservableCollection<MenuItem> CandidateMenuItems
    {
      get
      {
        ObservableCollection<MenuItem> collection = new ObservableCollection<MenuItem>();
        collection.Add(new MenuItem() { Text = "Display Bollinger Band", MenuItemClickedCommand = DisplayBollingerBand, StaysOpenOnClick = false });
        collection.Add(new MenuItem() { Text = "Display Dividend History", MenuItemClickedCommand = DisplayDividendHistory, StaysOpenOnClick = false });
        collection.Add(new MenuItem() { Text = "Display Sticker Valuation", MenuItemClickedCommand = DisplayStickerValuation, StaysOpenOnClick = false });
  			collection.Add(new MenuItem() { Text = "Display Historical", MenuItemClickedCommand = DisplayHistorical, StaysOpenOnClick = false });
        collection.Add(new MenuItem() { Text = "Display Stochastics", MenuItemClickedCommand = DisplayStochastics, StaysOpenOnClick = false });
        collection.Add(new MenuItem() { Text = "Display Relative Strength", MenuItemClickedCommand=DisplayRelativeStrength, StaysOpenOnClick = false });
        collection.Add(new MenuItem() { Text = "Display MACD", MenuItemClickedCommand = DisplayMACD, StaysOpenOnClick = false });
        collection.Add(new MenuItem() { Text = "Display Price History", MenuItemClickedCommand = DisplayPriceHistory, StaysOpenOnClick = false });
        collection.Add(new MenuItem() { Text = "Display Moving Average", MenuItemClickedCommand = DisplayMovingAverage, StaysOpenOnClick = false });
        collection.Add(new MenuItem() { Text = "Display Analyst Ratings", MenuItemClickedCommand = DisplayAnalystRatings, StaysOpenOnClick = false });
        collection.Add(new MenuItem() { Text = "Display DCF Valuation", MenuItemClickedCommand = DisplayDCFValuation, StaysOpenOnClick = false });
        collection.Add(new MenuItem() { Text = "Add to Watchlist", MenuItemClickedCommand = AddToWatchList, StaysOpenOnClick = false });
        collection.Add(new MenuItem() { Text = "Remove from Watchlist", MenuItemClickedCommand = RemoveFromWatchList, StaysOpenOnClick = false });
        return collection;
      }
    }

    public ObservableCollection<MenuItem> PositionsMenuItems
    {
      get
      {
        ObservableCollection<MenuItem> collection = new ObservableCollection<MenuItem>();
        collection.Add(new MenuItem() { Text = "Display Bollinger Band Model Position", MenuItemClickedCommand = DisplayBollingerBandPosition, StaysOpenOnClick = false });
        collection.Add(new MenuItem() { Text = "Display Bollinger Band All", MenuItemClickedCommand = DisplayBollingerBandPositionAll, StaysOpenOnClick = false });
        collection.Add(new MenuItem() { Text = "Display Headlines", MenuItemClickedCommand = DisplayHeadlinesPosition, StaysOpenOnClick = false });
        collection.Add(new MenuItem() { Text = "Display Dividend History", MenuItemClickedCommand = DisplayDividendHistoryPosition, StaysOpenOnClick = false });
        collection.Add(new MenuItem() { Text = "Display Sticker Valuation", MenuItemClickedCommand = DisplayStickerValuationPosition, StaysOpenOnClick = false });
  			collection.Add(new MenuItem() { Text = "Display Historical", MenuItemClickedCommand = DisplayHistoricalPosition, StaysOpenOnClick = false });
        collection.Add(new MenuItem() { Text = "Display Stochastics", MenuItemClickedCommand = DisplayStochasticsPosition, StaysOpenOnClick = false });
        collection.Add(new MenuItem() { Text = "Display Relative Strength", MenuItemClickedCommand=DisplayRelativeStrengthPosition, StaysOpenOnClick = false });
        collection.Add(new MenuItem() { Text = "Display MACD", MenuItemClickedCommand = DisplayMACDPosition, StaysOpenOnClick = false });
        collection.Add(new MenuItem() { Text = "Display Price History", MenuItemClickedCommand = DisplayPriceHistoryPosition, StaysOpenOnClick = false });
        collection.Add(new MenuItem() { Text = "Display Moving Average", MenuItemClickedCommand = DisplayMovingAveragePosition, StaysOpenOnClick = false });
        collection.Add(new MenuItem() { Text = "Display Analyst Ratings", MenuItemClickedCommand = DisplayAnalystRatingsPosition, StaysOpenOnClick = false });
        collection.Add(new MenuItem() { Text = "Display DCF Valuation", MenuItemClickedCommand = DisplayDCFValuationPosition, StaysOpenOnClick = false });
        collection.Add(new MenuItem() { Text = "Add to Watchlist", MenuItemClickedCommand = AddToWatchListPosition, StaysOpenOnClick = false });
        collection.Add(new MenuItem() { Text = "Remove from Watchlist", MenuItemClickedCommand = RemoveFromWatchListPosition, StaysOpenOnClick = false });
        collection.Add(new MenuItem() { Text = "Close Position...",MenuItemClickedCommand=ClosePosition,StaysOpenOnClick=false });
        collection.Add(new MenuItem() { Text = "Edit Position...",MenuItemClickedCommand=EditPosition,StaysOpenOnClick=false });
        return collection;
      }
    }
                          
    private void OnMomentumViewModelPropertyChanged(object sender, PropertyChangedEventArgs eventArgs)
    {
    }

// ************************************************************************************************************************************************
// ************************************************************************************************************************************************
// ************************************************************************************************************************************************
    public override String DisplayName
    {
      get
      {
        if(null==pathFileName)return "MGSHMomentum Model";
        String pureFileName = Utility.GetFileNameNoExtension(pathFileName);
        return "MGSHMomentum Model ("+pureFileName+")";      
      }
    }

    public override String Title
    {
      get 
      {
        return DisplayName;
      }
    }

// ********************************************************************************************************************************************************************************
    private void Run()
    {
      try
      {
        if(null==sessionParams)return;
        DateGenerator dateGenerator = new DateGenerator();
        DateTime selectedDate = dateGenerator.FindPrevBusinessDay(sessionParams.TradeDate);
        BusyIndicator = true;
        BusyContent="Running Momentum...";
        Task workerTask = Task.Factory.StartNew(() =>
        {
          MGSHConfiguration localConfiguration=new MGSHConfiguration();
          localConfiguration.MaxPositions = int.Parse(nvpDictionary["MaxPositions"].Value);
          localConfiguration.HoldingPeriod = int.Parse(nvpDictionary["HoldingPeriod"].Value);
          MGSHMomentumCandidates candidates = MGSHMomentumGenerator.GenerateMomentumWithFallback(selectedDate, configuration == null ? localConfiguration : configuration);
          momentumCandidates=new ObservableCollection<MGSHMomentumCandidate>();
          foreach(MGSHMomentumCandidate momentumCandidate in candidates)momentumCandidates.Add(momentumCandidate);
        });
        workerTask.ContinueWith((continuation)=>
        {
          BusyIndicator = false;
          base.OnPropertyChanged("AllItems");
          base.OnPropertyChanged("Title");
        });
      }
      finally
      {
      }
    }
// ******************************************************************************************************************************************************
// *************************************************************** T O O L T I P   C A L L O U T S ******************************************************
// *******************************************************************************************************************************************************
// Candidate
      public String CompanyDescription
      {
        get
        {
          if(null== selectedItem || null==selectedItem.Symbol)return "No row selected.";
          CompanyProfile companyProfile=CompanyProfileDA.GetCompanyProfile(selectedItem.Symbol);
          if(null==companyProfile || null==companyProfile.Description || "".Equals(companyProfile.Description))return "No description found.";
          StringBuilder sb=new StringBuilder();
          sb.Append(companyProfile.Sector).Append("/").Append(companyProfile.Industry).Append("\n").Append(companyProfile.Description);
          return sb.ToString();
        }
      }

      public String VelocityDescription
      {
        get
        {
          if(null== selectedItem || null==selectedItem.Symbol)return "No row selected.";
          return "Velocity is the percentage range of the current price within the price history.";
        }
      }

      public String BetaDescription
      {
        get
        {
          if(null== selectedItem || null==selectedItem.Symbol)return "No row selected.";
          return "A beta of less than 1 means that the security is theoretically less volatile than the market. A beta of greater than 1 indicates that the security's price is theoretically more volatile than the market. For example, if a stock's beta is 1.2, it's theoretically 20% more volatile than the market.";
        }
      }

// Position
      public String CompanyDescriptionSelectedPosition
      {
        get
        {
          if(null== selectedPosition || null==selectedPosition.Symbol)return "No row selected.";
          CompanyProfile companyProfile=CompanyProfileDA.GetCompanyProfile(selectedPosition.Symbol);
          if(null==companyProfile || null==companyProfile.Description || "".Equals(companyProfile.Description))return "No description found.";
          StringBuilder sb = new StringBuilder();
          sb.Append(companyProfile.Sector).Append("/").Append(companyProfile.Industry).Append("\n").Append(companyProfile.Description);
          return sb.ToString();
        }
      }
// ****************************************************************************************************************************************************************
// ****************************************************************************************************************************************************************
// ****************************************************************************************************************************************************************
    public MGSHMomentumCandidate SelectedItem
    {
      get
      { 
        return selectedItem; 
      }
      set
      {
        if (value == selectedItem) return;
        selectedItem = value;
        base.OnPropertyChanged("SelectedItem");
      }
    }

    public MGSHPositionModel SelectedPosition
    {
      get{return selectedPosition;}
      set{selectedPosition=value;base.OnPropertyChanged("SelectedPosition");}
    }

    public String LastTradeDate
    {
      get 
      { 
        if(null == sessionParams)return Utility.DateTimeToStringMMSDDSYYYY(Utility.Epoch);
        DateGenerator dateGenerator=new DateGenerator();
        return Utility.DateTimeToStringMMSDDSYYYY(dateGenerator.FindPrevBusinessDay(sessionParams.TradeDate));
      }
    }

    public String NextTradeDate
    {
      get
      {
        if(null == sessionParams)return Utility.DateTimeToStringMMSDDSYYYY(Utility.Epoch);
        return Utility.DateTimeToStringMMSDDSYYYY(sessionParams.TradeDate);
      }
    }

    public ObservableCollection<MGSHMomentumCandidate> AllItems
    {
      get{return momentumCandidates;}
    }

    public MGSHPositionModelCollection AllPositions
    {
      get{return positions;}
    }

    public ObservableCollection<String> Parameters
    {
      get{return nvpDictionaryKeys;}
    }

    public ObservableCollection<String> MonitorIntervals
    {
      get{return monitorIntervals;}
    }

    public bool CanMonitor
    {
      get{return IsTradeFileLoaded;}
    }

    public String MonitorStatus
    {
      get{return monitorRunning?"Stop Monitor":"Start Monitor";}
    }

    public String SelectedMonitorInterval
    {
      get{return selectedMonitorInterval;}
      set{selectedMonitorInterval=value;base.OnPropertyChanged("SelectedMonitorInterval");}
    }

    public String SelectedParameter
    {
      get{return selectedParameter;}
      set{selectedParameter=value;base.OnPropertyChanged("SelectedParameter");base.OnPropertyChanged("ParameterValue");}
    }

    public String ParameterValue
    {
      get
      {
        if(null==nvpDictionary||null==nvpDictionaryKeys||null==selectedParameter)return null;
        return nvpDictionary[selectedParameter].Value;
      }
    }

    public String CashBalance
    {
      get
      {
        if(null==sessionParams) return "";
        return Utility.FormatCurrency(sessionParams.CashBalance);
      }
    }

    public String NonTradeableCash
    {
      get
      {
        if(null==sessionParams) return "";
        return Utility.FormatCurrency(sessionParams.NonTradeableCash);
      }
    }

    public String HedgeCash
    {
      get
      {
        if(null==sessionParams) return "";
        return Utility.FormatCurrency(sessionParams.HedgeCashBalance);
      }
    }

    public String ModelExpectation
    {
      get
      {
        if(null==modelStatistics) return "";
        return Utility.FormatNumber(modelStatistics.Expectancy,2);
      }
    }

    public Brush ExpectationColor
    {
      get
      {
        if(null==modelStatistics) return UIUtils.BrushCollection.GetContextBrush(BrushCollection.BrushColor.Black);
        if(modelStatistics.Expectancy>0.00) return UIUtils.BrushCollection.GetContextBrush(BrushCollection.BrushColor.Black);
        return UIUtils.BrushCollection.GetContextBrush(BrushCollection.BrushColor.Red);
      }
    }

    public String ExpectationDescription
    {
      get
      {
        if(null==modelStatistics) return "";
        StringBuilder sb=new StringBuilder();
        sb.Append("Expectancy is (percentage of winning trades * average gain) / (percentage of losing trades * average loss).").Append("\n");
        sb.Append("Total Trades : ").Append(modelStatistics.TotalTrades).Append("\n");
        sb.Append("Winning Trades : ").Append(modelStatistics.WinningTrades).Append("\n");
        sb.Append("Losing Trades : ").Append(modelStatistics.LosingTrades).Append("\n");
        sb.Append("Winning Trades : ").Append(Utility.FormatNumber(modelStatistics.WinningTradesPercent,2)).Append("%").Append("\n");
        sb.Append("Losing Trades : ").Append(Utility.FormatNumber(modelStatistics.LosingTradesPercent,2)).Append("%").Append("\n");
        sb.Append("Average Winning Trade Gain : ").Append(Utility.FormatNumber(modelStatistics.AverageWinningTradePercentGain,2)).Append("%").Append("\n");
        sb.Append("Average Losing Trade Loss : ").Append(Utility.FormatNumber(modelStatistics.AverageLosingTradePercentLoss,2)).Append("%").Append("\n");
        sb.Append("Expectancy : ").Append(Utility.FormatNumber(modelStatistics.Expectancy,2)).Append("\n");
        sb.Append("\n");
        sb.Append("Maintain a positive Expectancy and you're a winner.");
        sb.Append("\n");
        sb.Append("The calculations are based on closed positions.");
        return sb.ToString();
      }
    }  

    // *************************************************************************************************************************************************************
    // *********************************************************************** I C O M M A N D  ********************************************************************
    // *************************************************************************************************************************************************************
    public ICommand DisplayHistorical
    {
      get
      {
        if (displayHistoricalCommand == null)
        {
          displayHistoricalCommand = new RelayCommand(param => 
          {
            SaveParameters saveParams = SaveParameters.Parse("Type,TradeBlotter.ViewModels.HistoricalViewModel,SelectedSymbol," + selectedItem.Symbol + ",SelectedWatchList,{All}");
            saveParams.Referer=this;
            WorkspaceInstantiator.Invoke(saveParams);
          }, param => { return null != selectedItem && null != selectedItem.Symbol; });
        }
        return displayHistoricalCommand;
      }
    }

    public ICommand RunCommand
    {
      get
      {
        if (runCommand == null)
        {
          runCommand = new RelayCommand(param => this.Run(), param => {return true;});
        }
        return runCommand;
      }
    }

    public ICommand DisplayMovingAverage
    {
      get
      {
        if (movingAverageCommand == null)
        {
          movingAverageCommand = new RelayCommand(param => this.DisplayMovingAverageCommand(selectedItem.Symbol), param => { return null != selectedItem && null != selectedItem.Symbol; });
        }                      
        return movingAverageCommand;
      }
    }

    public ICommand DisplayAnalystRatings
    {
      get
      {
        if (analystRatingsCommand == null)
        {
          analystRatingsCommand = new RelayCommand(param => this.DisplayAnalystRatingsCommand(), param => { return null != selectedItem && null != selectedItem.Symbol; });
        }
        return analystRatingsCommand;
      }
    }

    public ICommand DisplayMACD
    {
      get
      {
        if (macdCommand == null)
        {
          macdCommand = new RelayCommand(param => this.DisplayMACDCommand(), param => { return null != selectedItem && null != selectedItem.Symbol; });
        }
        return macdCommand;
      }
    }

    public ICommand DisplayStochastics
    {
      get
      {
        if (stochasticsCommand == null)
        {
          stochasticsCommand = new RelayCommand(param => this.DisplayStochasticsCommand(), param => { return null != selectedItem && null != selectedItem.Symbol; });
        }
        return stochasticsCommand;
      }
    }

    public ICommand DisplayRelativeStrength
    {
      get
      {
        if (relativeStrengthCommand == null)
        {
          relativeStrengthCommand = new RelayCommand(param => this.DisplayRelativeStrengthCommand(), param => { return null != selectedItem && null!=selectedItem.Symbol; });
        }
        return relativeStrengthCommand;
      }
    }

    public ICommand DisplayStickerValuation
    {
      get
      {
        if (null == stickerValuationCommand)
        {
          stickerValuationCommand = new RelayCommand(param =>
          {
            SaveParameters saveParams = SaveParameters.Parse("Type,TradeBlotter.ViewModels.StickerPriceViewModel,SelectedSymbol," + selectedItem.Symbol + ",SelectedWatchList,{All}");
            saveParams.Referer=this;
            WorkspaceInstantiator.Invoke(saveParams);
          }, param => { return null != selectedItem && null!=selectedItem.Symbol; });
        }
        return stickerValuationCommand;
      }
    }

    public ICommand DisplayDCFValuation
    {
      get
      {
        if (null == dcfValuationCommand)
        {
          dcfValuationCommand = new RelayCommand(param =>
          {
            SaveParameters saveParams = SaveParameters.Parse("Type,TradeBlotter.ViewModels.DCFValuationViewModel,SelectedSymbol," + selectedItem.Symbol + ",SelectedWatchList,{All}");
            saveParams.Referer=this;
            WorkspaceInstantiator.Invoke(saveParams);
          }, param => { return null != selectedItem && null!=selectedItem.Symbol; });
        }
        return dcfValuationCommand;
      }
    }

    public ICommand DisplayPriceHistory
    {
      get
      {
        if (priceHistoryCommand == null)
        {
          priceHistoryCommand = new RelayCommand(param => this.DisplayPriceHistoryCommand(), param => { return null != selectedItem && null != selectedItem.Symbol; });
        }
        return priceHistoryCommand;
      }
    }

    public ICommand DisplayDividendHistory
    {
      get
      {
        if (dividendHistoryCommand == null)
        {
          dividendHistoryCommand = new RelayCommand(param => 
          {
            SaveParameters saveParams = SaveParameters.Parse("Type,TradeBlotter.ViewModels.DividendHistoryViewModel,SelectedSymbol," + selectedItem.Symbol + ",SelectedWatchList,{All}");
            saveParams.Referer=this;
            WorkspaceInstantiator.Invoke(saveParams);
          }, param => { return null != selectedItem && null != selectedItem.Symbol; });
        }
        return dividendHistoryCommand;
      }
    }

    public void DisplayPriceHistoryCommand()
    {
      SaveParameters saveParams = SaveParameters.Parse("Type,TradeBlotter.ViewModels.PricingViewModel,SelectedSymbol," + selectedItem.Symbol + ",SelectedWatchList,{All},SelectedDayCount,180");
      saveParams.Referer=this;
      WorkspaceInstantiator.Invoke(saveParams);
    }

    public ICommand DisplayBollingerBand
    {
      get
      {
        if (bollingerBandCommand == null)
        {
          bollingerBandCommand = new RelayCommand(param => this.DisplayBollingerBandCommand(), param => { return null != selectedItem && null != selectedItem.Symbol; });
        }
        return bollingerBandCommand;
      }
    }

    public ICommand AddToWatchList
    {
      get
      {
        if (addToWatchListCommand == null)
        {
          addToWatchListCommand = new RelayCommand(param => this.AddToWatchListCommand(selectedItem.Symbol),
            param => 
              {
                if(null==selectedItem || null==selectedItem.Symbol)return false;
                if (WatchListDA.IsInWatchList(selectedItem.Symbol)) return false;
                return true;
              });
        }
        return addToWatchListCommand;
      }
    }

    public ICommand RemoveFromWatchList
    {
      get
      {
        if (removeFromWatchListCommand == null)
        {
          removeFromWatchListCommand = new RelayCommand(param => this.RemoveFromWatchListCommand(selectedItem.Symbol),
            param => 
              {
                if(null==selectedItem || null==selectedItem.Symbol)return false;
                if (!WatchListDA.IsInWatchList(selectedItem.Symbol)) return false;
                return true;
              });
        }
        return removeFromWatchListCommand;
      }
    }
// *************************************************************************************************************************************************************
// **************************************************************** I C O M M A N D  P O S I T I O N ***********************************************************
// *************************************************************************************************************************************************************
    public ICommand DisplayHeadlinesPosition
    {
      get
      {
        if (null == displayHeadlinesCommandPosition)
        {
          displayHeadlinesCommandPosition = new RelayCommand(param =>
          {
            SaveParameters saveParams = SaveParameters.Parse("Type,TradeBlotter.ViewModels.HeadlinesViewModel,SelectedSymbol," + selectedPosition.Symbol + ",SelectedWatchList,Valuations");
            saveParams.Referer=this;
            WorkspaceInstantiator.Invoke(saveParams);
          }, param => { return null != selectedPosition && null!=selectedPosition.Symbol; });
        }
        return displayHeadlinesCommandPosition;
      }
    }

    public ICommand DisplayHistoricalPosition
    {
      get
      {
        if (displayHistoricalCommandPosition == null)
        {
          displayHistoricalCommandPosition = new RelayCommand(param => 
          {
            SaveParameters saveParams = SaveParameters.Parse("Type,TradeBlotter.ViewModels.HistoricalViewModel,SelectedSymbol," + selectedPosition.Symbol + ",SelectedWatchList,{All}");
            saveParams.Referer=this;
            WorkspaceInstantiator.Invoke(saveParams);
          }, param => { return null != selectedPosition && null != selectedPosition.Symbol; });
        }
        return displayHistoricalCommandPosition;
      }
    }

    public ICommand DisplayAnalystRatingsPosition
    {
      get
      {
        if (analystRatingsCommandPosition == null)
        {
          analystRatingsCommandPosition = new RelayCommand(param => this.DisplayAnalystRatingsCommandPosition(), param => { return null != selectedPosition && null != selectedPosition.Symbol; });
        }
        return analystRatingsCommandPosition;
      }
    }

    public ICommand DisplayMovingAveragePosition
    {
      get
      {
        if (movingAverageCommandPosition == null)
        {
          movingAverageCommandPosition = new RelayCommand(param => this.DisplayMovingAverageCommand(selectedPosition.Symbol), param => { return null != selectedPosition && null != selectedPosition.Symbol; });
        }                      
        return movingAverageCommandPosition;
      }
    }

    public ICommand DisplayMACDPosition
    {
      get
      {
        if (macdCommandPosition == null)
        {
          macdCommandPosition = new RelayCommand(param => this.DisplayMACDCommandPosition(), param => { return null != selectedPosition && null != selectedPosition.Symbol; });
        }
        return macdCommandPosition;
      }
    }

    public ICommand DisplayStochasticsPosition
    {
      get
      {
        if (stochasticsCommandPosition == null)
        {
          stochasticsCommandPosition = new RelayCommand(param => this.DisplayStochasticsCommandPosition(), param => { return null != selectedPosition && null != selectedPosition.Symbol; });
        }
        return stochasticsCommandPosition;
      }
    }

    public ICommand DisplayRelativeStrengthPosition
    {
      get
      {
        if (relativeStrengthCommandPosition == null)
        {
          relativeStrengthCommandPosition = new RelayCommand(param => this.DisplayRelativeStrengthCommandPosition(),param => { return null != selectedPosition && null!= selectedPosition.Symbol; });
        }
        return relativeStrengthCommandPosition;
      }
    }

    public ICommand DisplayStickerValuationPosition
    {
      get
      {
        if (null == stickerValuationCommandPosition)
        {
          stickerValuationCommandPosition= new RelayCommand(param =>
          {
            SaveParameters saveParams = SaveParameters.Parse("Type,TradeBlotter.ViewModels.StickerPriceViewModel,SelectedSymbol," + selectedPosition.Symbol + ",SelectedWatchList,{All}");
            saveParams.Referer=this;
            WorkspaceInstantiator.Invoke(saveParams);
          }, param => { return null != selectedPosition && null!=selectedPosition.Symbol; });
        }
        return stickerValuationCommandPosition;
      }
    }

    public ICommand DisplayDCFValuationPosition
    {
      get
      {
        if (null == dcfValuationCommandPosition)
        {
          dcfValuationCommandPosition = new RelayCommand(param =>
          {
            SaveParameters saveParams = SaveParameters.Parse("Type,TradeBlotter.ViewModels.DCFValuationViewModel,SelectedSymbol," + selectedPosition.Symbol + ",SelectedWatchList,{All}");
            saveParams.Referer=this;
            WorkspaceInstantiator.Invoke(saveParams);
          }, param => { return null != selectedPosition && null!=selectedPosition.Symbol; });
        }
        return dcfValuationCommandPosition;
      }
    }

    public ICommand DisplayPriceHistoryPosition
    {
      get
      {
        if (priceHistoryCommandPosition == null)
        {
          priceHistoryCommandPosition = new RelayCommand(param => this.DisplayPriceHistoryCommandPosition(), param => { return null != selectedPosition && null != selectedPosition.Symbol; });
        }
        return priceHistoryCommandPosition;
      }
    }

    public ICommand DisplayDividendHistoryPosition
    {
      get
      {
        if (dividendHistoryCommandPosition == null)
        {
          dividendHistoryCommandPosition = new RelayCommand(param => 
          {
            SaveParameters saveParams = SaveParameters.Parse("Type,TradeBlotter.ViewModels.DividendHistoryViewModel,SelectedSymbol," + selectedPosition.Symbol + ",SelectedWatchList,{All}");
            saveParams.Referer=this;
            WorkspaceInstantiator.Invoke(saveParams);
          }, param => { return null != selectedPosition && null != selectedPosition.Symbol; });
        }
        return dividendHistoryCommandPosition;
      }
    }

    public ICommand DisplayBollingerBandPosition
    {
      get
      {
        if (bollingerBandCommandPosition == null)
        {
          bollingerBandCommandPosition = new RelayCommand(param => this.DisplayBollingerBandCommandPosition(), param => { return null != selectedPosition && null != selectedPosition.Symbol; });
        }
        return bollingerBandCommandPosition;
      }
    }

    public ICommand DisplayBollingerBandPositionAll
    {
      get
      {
        if (bollingerBandCommandPositionAll == null)
        {
          bollingerBandCommandPositionAll = new RelayCommand(param => this.DisplayBollingerBandCommandPositionAll(), param => { return null != selectedPosition && null != selectedPosition.Symbol; });
        }
        return bollingerBandCommandPositionAll;
      }
    }

    public ICommand AddToWatchListPosition
    {
      get
      {
        if (addToWatchListCommandPosition == null)
        {
          addToWatchListCommandPosition = new RelayCommand(param => this.AddToWatchListCommand(selectedPosition.Symbol),
            param => 
              {
                if(null==selectedPosition || null==selectedPosition.Symbol)return false;
                if (WatchListDA.IsInWatchList(selectedPosition.Symbol)) return false;
                return true;
              });

        }
        return addToWatchListCommandPosition;
      }
    }

    public ICommand RemoveFromWatchListPosition
    {
      get
      {
        if (removeFromWatchListCommandPosition == null)
        {
          removeFromWatchListCommandPosition = new RelayCommand(param => this.RemoveFromWatchListCommand(selectedPosition.Symbol), param => 
              {
                if(null==selectedPosition || null==selectedPosition.Symbol)return false;
                if (!WatchListDA.IsInWatchList(selectedPosition.Symbol)) return false;
                return true;
              });
        }
        return removeFromWatchListCommandPosition;
      }
    }

    public ICommand ClosePosition
    {
      get
      {
        if(closePositionCommandPosition==null)
        {
          closePositionCommandPosition=new RelayCommand(param => this.ClosePositionCommand(selectedPosition),param =>
          {
            if(null==selectedPosition||null==selectedPosition.Symbol) return false;
            return true;
          });
        }
        return closePositionCommandPosition;
      }
    }

    public ICommand EditPosition
    {
      get
      {
        if(editPositionCommandPosition==null)
        {
          editPositionCommandPosition=new RelayCommand(param => this.EditPositionCommand(selectedPosition),param =>
          {
            if(null==selectedPosition||null==selectedPosition.Symbol||!Utility.IsEpoch(selectedPosition.SellDate)) return false;
            return true;
          });
        }
        return editPositionCommandPosition;
      }
    }
// *************************************************************************************************************************************************************
// ******************************************************************* I C O M M A N D   S E S S I O N *********************************************************
// *************************************************************************************************************************************************************
    public ICommand Reload
    {
      get
      {
        if(reloadCommand==null)
        {
          reloadCommand=new RelayCommand(param => this.ReloadCommand(),param => 
					{ 
					  return ReloadEnabled; 
					});
        }
        return reloadCommand;
      }
    }

		public bool ReloadEnabled
		{
		  get
			{
			  return !String.IsNullOrEmpty(pathFileName);
			}
		}

    public ICommand LoadFile
    {                                                                                                                       
      get
      {
        if (loadFileCommand == null)
        {
          loadFileCommand = new RelayCommand(param => this.LoadFileCommand(), param => { return true; });
        }
        return loadFileCommand;
      }
    }

    public ICommand Monitor
    {                                                                                                                       
      get
      {
        if (monitorCommand == null)
        {
          monitorCommand = new RelayCommand(param => this.MonitorCommand(), param => { return IsTradeFileLoaded; });
        }
        return monitorCommand;
      }
    }
    private bool IsTradeFileLoaded
    {
      get{return null==pathFileName?false:true;}
    }
// *************************************************************************************************************************************************************
// ********************************************************************** C O M M A N D  W O R K E R S  ********************************************************
// *************************************************************************************************************************************************************
    public void DisplayBollingerBandCommand()
    {
      SaveParameters saveParams = SaveParameters.Parse("Type,TradeBlotter.ViewModels.BollingerBandViewModel,SelectedSymbol," + selectedItem.Symbol + ",SelectedWatchList,{All},SelectedDayCount,360");
      saveParams.Referer=this;
      WorkspaceInstantiator.Invoke(saveParams);
    }

    public void DisplayAnalystRatingsCommand()
    {
      SaveParameters saveParams = SaveParameters.Parse("Type,TradeBlotter.ViewModels.AnalystRatingsViewModel,SelectedSymbol," + selectedItem.Symbol + ",SelectedWatchList,{All}");
      saveParams.Referer=this;
      WorkspaceInstantiator.Invoke(saveParams);
    }

    public void DisplayStochasticsCommand()
    {
      SaveParameters saveParams = SaveParameters.Parse("Type,TradeBlotter.ViewModels.StochasticsViewModel,SelectedSymbol," + selectedItem.Symbol + ",SelectedWatchList,{All},SelectedDayCount,180");
      saveParams.Referer=this;
      WorkspaceInstantiator.Invoke(saveParams);
    }

    public void DisplayRelativeStrengthCommand()
    {
      SaveParameters saveParams = SaveParameters.Parse("Type,TradeBlotter.ViewModels.RSIViewModel,SelectedSymbol," + selectedItem.Symbol + ",SelectedWatchList,{All},SelectedDayCount,60,SelectedRSIDayCount,3");
      saveParams.Referer=this;
      WorkspaceInstantiator.Invoke(saveParams);
    }

    public void DisplayMACDCommand()
    {
      SaveParameters saveParams = SaveParameters.Parse("Type,TradeBlotter.ViewModels.MACDViewModel,SelectedSymbol," + selectedItem.Symbol + ",SelectedWatchList,{All},SelectedDayCount,180");
      saveParams.Referer=this;
      WorkspaceInstantiator.Invoke(saveParams);
    }
    // *************************************************************************************************************************************************************
    // ********************************************************************** C O M M A N D  W O R K E R S  P O S I T I O N ****************************************
    // *************************************************************************************************************************************************************
    public void ClosePositionCommand(MGSHPositionModel selectedPosition)
    {
      bool deleteStop=false;
      MGSHPosition clonedPosition=MGSHPosition.Clone(selectedPosition.Position);
      bool hasStopLimit=PortfolioDA.HasStopLimit(clonedPosition.Symbol);
      IPosition changedPosition=ClosePositionDialog.Prompt("Close Position",clonedPosition,hasStopLimit,ref deleteStop);
      if(null==changedPosition) return;
      MGSHMomentumBacktest mgshMomentumBacktest = new MGSHMomentumBacktest();
      if(!mgshMomentumBacktest.ClosePosition(changedPosition.Symbol,changedPosition.PurchaseDate,changedPosition.SellDate,changedPosition.CurrentPrice,pathFileName))
      {
        MessageBox.Show("Failed to close the position, check log for details.","Close Position");
       return;
      }
      if(deleteStop) PortfolioDA.DeleteStopLimit(changedPosition.Symbol);
      String strMessage=String.Format("Closed position for {0}, Purchase Date:{1}, Sell Date{2}, Current Price:{3}, Delete Stop:{4} to {5}.  A backup was created.",
      changedPosition.Symbol,changedPosition.PurchaseDate.ToShortDateString(),changedPosition.SellDate.ToShortDateString(),Utility.FormatCurrency(changedPosition.CurrentPrice),deleteStop,pathFileName);
      MessageBox.Show(strMessage,"Close Position");
      LoadSessionFile();
    }

    public void EditPositionCommand(MGSHPositionModel selectedPosition)
    {
      MGSHPosition clonedPosition = MGSHPosition.Clone(selectedPosition.Position);
      IPosition changedPosition = EditPositionDialog.Prompt("Edit Position", clonedPosition);
      if (null == changedPosition) return;
      MGSHMomentumBacktest mgshMomentumBacktest = new MGSHMomentumBacktest();
      if (!mgshMomentumBacktest.EditPosition(changedPosition.Symbol, changedPosition.PurchaseDate, changedPosition.PurchasePrice, changedPosition.InitialStopLimit, changedPosition.TrailingStopLimit, pathFileName))
      {
        MessageBox.Show("Failed to edit the position, check log for details.", "Edit Position");
        return;
      }
      if (!selectedPosition.TrailingStopLimit.Equals(changedPosition.TrailingStopLimit))
      {
        selectedPosition.TrailingStopLimit = changedPosition.TrailingStopLimit;
        selectedPosition.LastStopAdjustment = DateTime.Now.Date;
        StopLimit stopLimit = PortfolioDA.GetStopLimit(changedPosition.Symbol);
        if (null == stopLimit)
        {
          stopLimit = new StopLimit();
          stopLimit.Symbol = changedPosition.Symbol;
          stopLimit.StopType = StopLimitConstants.STOP_QUOTE;
          stopLimit.Shares = changedPosition.Shares;
        }
        stopLimit.StopPrice = changedPosition.TrailingStopLimit;
        PortfolioDA.InsertUpdateStopLimit(stopLimit);
      }
      selectedPosition.PurchaseDate = changedPosition.PurchaseDate;
      selectedPosition.PurchasePrice = changedPosition.PurchasePrice;
      selectedPosition.TrailingStopLimit = changedPosition.TrailingStopLimit;
      selectedPosition.InitialStopLimit = changedPosition.InitialStopLimit;
      String strMessage = String.Format("Edited Position for {0} Purchase Date:{1} Purchase Price:{2}, Trailing Stop:{3}. A backup was created.",
      selectedPosition.Symbol, selectedPosition.PurchaseDate.ToShortDateString(), Utility.FormatCurrency(selectedPosition.PurchasePrice), Utility.FormatCurrency(selectedPosition.TrailingStopLimit));
      MessageBox.Show(strMessage, "Edit Position");
      LoadSessionFile();
    }

    public void DisplayBollingerBandCommandPosition()
    {
      MarketData.MarketDataModel.StopLimits stopLimits = GetHistoricalStopLimitsMarketDataModel();

      StringBuilder sb = new StringBuilder();
      SaveParameters saveParams = null;
      sb = new StringBuilder();
      sb.Append("Type,TradeBlotter.ViewModels.BollingerBandPositionViewModel,SelectedSymbol,");
      sb.Append(selectedPosition.Symbol).Append(",");
      sb.Append("SelectedWatchList,{All},SelectedDayCount,");
      sb.Append(GetDayCountSelectionForBollingerBands(selectedPosition));
      saveParams = SaveParameters.Parse(sb.ToString());
      SaveParameters stopLimitParams = StopLimitsExtensions.FromStopLimits(stopLimits);
      saveParams.AddRange(stopLimitParams);
      SaveParameters portfolioTradesParams = PortfolioTradesExtensions.FromPosition(selectedPosition);
      saveParams.AddRange(portfolioTradesParams);
      saveParams.Referer=this;
      WorkspaceInstantiator.Invoke(saveParams);
    }

    public void DisplayBollingerBandCommandPositionAll()
    {
      MarketData.MarketDataModel.StopLimits stopLimits = GetHistoricalStopLimitsMarketDataModel();

      StringBuilder sb = new StringBuilder();
      SaveParameters saveParams = null;
      sb = new StringBuilder();
      sb.Append("Type,TradeBlotter.ViewModels.BollingerBandViewModel,SelectedSymbol,");
      sb.Append(selectedPosition.Symbol).Append(",");
      sb.Append("SelectedWatchList,{All},SelectedDayCount,");
      sb.Append(GetDayCountSelectionForBollingerBands(selectedPosition, true));
      saveParams = SaveParameters.Parse(sb.ToString());
      SaveParameters stopLimitParams = StopLimitsExtensions.FromStopLimits(stopLimits);
      saveParams.AddRange(stopLimitParams);
      saveParams.Referer=this;
      WorkspaceInstantiator.Invoke(saveParams);
    }

    private int GetDayCountSelectionForBollingerBands(MGSHPositionModel selectedPosition,bool ignoreActivePosition=false)
    {
      DateGenerator dateGenerator=new DateGenerator();
      DateTime maxDate = DateTime.Today;

      if(!ignoreActivePosition)
      {
        maxDate = PricingDA.GetLatestDate(selectedPosition.Symbol);
        if(!selectedPosition.IsActivePosition)
        {
          maxDate = selectedPosition.SellDate;
        }
      }
      int daysBetween=dateGenerator.DaysBetween(selectedPosition.PurchaseDate, maxDate);
      if(daysBetween<90)return 90;
      if(daysBetween<180)return 180;
      if(daysBetween<360)return 360;
      if(daysBetween<720)return 720;
      if(daysBetween<1440)return 1440;
      return 3600;
    }

    public void DisplayAnalystRatingsCommandPosition()
    {
      SaveParameters saveParams = SaveParameters.Parse("Type,TradeBlotter.ViewModels.AnalystRatingsViewModel,SelectedSymbol," + selectedPosition.Symbol + ",SelectedWatchList,{All}");
      saveParams.Referer=this;
      WorkspaceInstantiator.Invoke(saveParams);
    }

    public void DisplayStochasticsCommandPosition()
    {
      SaveParameters saveParams = SaveParameters.Parse("Type,TradeBlotter.ViewModels.StochasticsViewModel,SelectedSymbol," + selectedPosition.Symbol + ",SelectedWatchList,{All},SelectedDayCount,180");
      saveParams.Referer=this;
      WorkspaceInstantiator.Invoke(saveParams);
    }

    public void DisplayRelativeStrengthCommandPosition()
    {
      SaveParameters saveParams = SaveParameters.Parse("Type,TradeBlotter.ViewModels.RSIViewModel,SelectedSymbol," + selectedPosition.Symbol + ",SelectedWatchList,{All},SelectedDayCount,60,SelectedRSIDayCount,3");
      saveParams.Referer=this;
      WorkspaceInstantiator.Invoke(saveParams);
    }

    public void DisplayMACDCommandPosition()
    {
      SaveParameters saveParams = SaveParameters.Parse("Type,TradeBlotter.ViewModels.MACDViewModel,SelectedSymbol," + selectedPosition.Symbol + ",SelectedWatchList,{All},SelectedDayCount,180");
      saveParams.Referer=this;
      WorkspaceInstantiator.Invoke(saveParams);
    }

    public void DisplayPriceHistoryCommandPosition()
    {
      SaveParameters saveParams = SaveParameters.Parse("Type,TradeBlotter.ViewModels.PricingViewModel,SelectedSymbol," + selectedPosition.Symbol + ",SelectedWatchList,{All},SelectedDayCount,180");
      saveParams.Referer=this;
      WorkspaceInstantiator.Invoke(saveParams);
    }
// ***************************************************************************************************************************************************************************
// ********************************************************************* G E N E R A L  W O R K E R S  ***********************************************************************
// ***************************************************************************************************************************************************************************
    public void DisplayMovingAverageCommand(String symbol)
    {
      SaveParameters saveParams=new SaveParameters();
      saveParams.Add(new KeyValuePair<String, String>("Type", "TradeBlotter.ViewModels.MovingAverageViewModel"));
      saveParams.Add(new KeyValuePair<String, String>("SelectedSymbol", symbol));
      saveParams.Add(new KeyValuePair<String, String>("SelectedWatchList", Constants.CONST_ALL));
      saveParams.Add(new KeyValuePair<String, String>("SelectedDayCount", "360"));
      saveParams.Add(new KeyValuePair<String, String>("IsLegendVisible", "true"));
      saveParams.Referer=this;
      WorkspaceInstantiator.Invoke(saveParams);
    }

    public void AddToWatchListCommand(String symbol)
    {
      if (WatchListDA.IsInWatchList(symbol))
      {
        System.Windows.MessageBox.Show("'"+symbol+"' is already in watchlist","Info", MessageBoxButton.OK,  MessageBoxImage.Information);
        return;
      }
      if (!WatchListDA.AddToWatchList(symbol))
      {
        System.Windows.MessageBox.Show("Error adding '"+symbol+"' to watchlist","Warning", MessageBoxButton.OK,  MessageBoxImage.Warning);
      }
      else
      {
        System.Windows.MessageBox.Show("Added '"+symbol+"'","Success", MessageBoxButton.OK,  MessageBoxImage.Information);
      }
    }

    public void RemoveFromWatchListCommand(String symbol)
    {
      if (!WatchListDA.IsInWatchList(symbol))
      {
        System.Windows.MessageBox.Show("'"+symbol+"' is not in watchlist","Info", MessageBoxButton.OK,  MessageBoxImage.Information);
        return;
      }
      if (!WatchListDA.RemoveFromWatchList(symbol))
      {
        System.Windows.MessageBox.Show("Error removing '"+symbol+"' from watchlist","Warning", MessageBoxButton.OK,  MessageBoxImage.Warning);
      }
      else
      {
        System.Windows.MessageBox.Show("Removed '"+symbol+"'","Success", MessageBoxButton.OK,  MessageBoxImage.Information);
      }
    }
// **************************************************************************************************************************************************************************
// ********************************************************************** C O M M A N D  W O R K E R S  S E S S I O N ********************************************************
// ***************************************************************************************************************************************************************************
    public void MonitorCommand()
    {
      if(monitorRunning)StopMonitor();
      else StartMonitor();
    }

    private void StopMonitor()
    {
      if(!monitorRunning)return;
      dispatcherTimer.Stop();
      monitorRunning=false;
      base.OnPropertyChanged("MonitorStatus");
    }

    private void StartMonitor()
    {
      if(monitorRunning)return;
      dispatcherTimer.Start();
      monitorRunning=true;
      Dispatcher.CurrentDispatcher.BeginInvoke((Action)(() => { DispatcherTimerTick(null,null); }), DispatcherPriority.Normal);
      DispatcherTimerTick(null,null);
      base.OnPropertyChanged("MonitorStatus");
    }

    private void DispatcherTimerTick(object sender, EventArgs e)
    {
      UpdatePositionPrices(false);
      UpdatePositionRSI3(true);
      RunPerformance();
    }

    private void UpdatePositionPrices(bool change=true)
    {
      try
      {
        DateTime today=DateTime.Now;
        if(null==positions||0==positions.Count)return;
        List<String> symbols=(from MGSHPositionModel position in positions where position.IsActivePosition select position.Symbol).Distinct().ToList();
        foreach(String symbol in symbols)
        {
          var selectedPositions=(from MGSHPositionModel position in positions where position.IsActivePosition && position.Symbol.Equals(symbol) select position);
          foreach(MGSHPositionModel selectedPosition in selectedPositions)
          {
            Price price=PricingDA.GetPrice(symbol);
            if(null==price)continue;
            selectedPosition.CurrentPrice=price.Close;
            selectedPosition.Volume=price.Volume;
            selectedPosition.LastUpdated=price.Date.Date<today.Date?price.Date:today;
          }
        }
        if(change) positions.OnCollectionChanged();
      }
      catch(Exception exception)
      {
        MDTrace.WriteLine(LogLevel.DEBUG,exception.ToString());
      }
    }

    private void UpdatePositionRSI3(bool change=true)
    {
      try
      {
        if(null==positions||0==positions.Count)return;
        List<String> symbols=(from MGSHPositionModel position in positions where position.IsActivePosition select position.Symbol).Distinct().ToList();
        foreach(String symbol in symbols)
        {
          var selectedPositions=(from MGSHPositionModel position in positions where position.IsActivePosition && position.Symbol.Equals(symbol) select position);
          foreach(MGSHPositionModel selectedPosition in selectedPositions)
          {
            RSICollection rsiCollection=RSIGenerator.GenerateRSI(symbol,30,3);
            if(null==rsiCollection||0==rsiCollection.Count)continue;
            selectedPosition.RSI3=rsiCollection[rsiCollection.Count-1].RSI;
          }
        }
        if(change)positions.OnCollectionChanged();
      }
      catch(Exception exception)
      {
        MDTrace.WriteLine(LogLevel.DEBUG,exception.ToString());
      }
    }

		public void ReloadCommand()
		{
		  LoadSessionFile();
		}

    public void LoadFileCommand()
    {
      Forms.OpenFileDialog openFileDialog=new Forms.OpenFileDialog();
      openFileDialog.Filter="text file (*.txt)|*.txt";
      openFileDialog.FilterIndex=0;
      openFileDialog.RestoreDirectory=false;
      if(null!=initialPath)openFileDialog.InitialDirectory=initialPath;
      else openFileDialog.InitialDirectory=Directory.GetCurrentDirectory();
      if(!Forms.DialogResult.OK.Equals(openFileDialog.ShowDialog()))return;
      pathFileName=openFileDialog.FileName;
      if(!MGSHSessionManager.IsValidSessionFile(pathFileName))
      {
        MessageBox.Show(String.Format("Invalid session file '{0}'",pathFileName));
        pathFileName=null;
      }
      else LoadSessionFile();
    }

    public bool LoadSessionFile()
    {
      BusyIndicator = true;
      BusyContent = $"Loading {Utility.GetFileNameNoExtension(pathFileName)}...";

      Task workerTask = Task.Factory.StartNew(() =>
      {
        try
        {
          if(!MGSHSessionManager.IsValidSessionFile(pathFileName))return false;
          initialPath=Path.GetDirectoryName(pathFileName);
          sessionParams=MGSHSessionManager.RestoreSession(pathFileName);
          if(null==sessionParams)
          { 
            MessageBox.Show(String.Format("Unable to open {0}",pathFileName),"Error",MessageBoxButton.OK,MessageBoxImage.Exclamation ,MessageBoxResult.OK,MessageBoxOptions.DefaultDesktopOnly);
            pathFileName=null;
            return false;
          }
          modelStatistics=MGSHMomentumBacktest.GetModelStatistics(sessionParams);
          modelPerformanceSeries=MGSHMomentumBacktest.GetModelPerformance(sessionParams);
          configuration=sessionParams.Configuration;
          NVPCollection nvpCollection=sessionParams.Configuration.ToNVPCollection();
          nvpDictionary=nvpCollection.ToDictionary();
          List<String> dictionaryKeys=new List<String>(nvpDictionary.Keys);
          dictionaryKeys.Sort();
          nvpDictionaryKeys=new ObservableCollection<String>(dictionaryKeys);
          selectedParameter=nvpDictionaryKeys[0];
          positions=new MGSHPositionModelCollection();
          positions.Add(sessionParams.ActivePositions);    // active positions will go into their assigned slot
          positions.Add(sessionParams.HedgePositions, sessionParams.ActivePositions.GetMaxSlotNumber()+1); // -1 is a special slot so active hedge positions will always appear in the slot position 1 past the max
          positions.Add(sessionParams.AllPositions);
          UpdatePositionPrices(false);
          UpdatePositionRSI3(true);
          RunPerformance();
          return true;
        }
        catch(Exception exception)
        {
          MessageBox.Show(String.Format("Unable to open {0}",pathFileName),"Error",MessageBoxButton.OK,MessageBoxImage.Exclamation ,MessageBoxResult.OK,MessageBoxOptions.DefaultDesktopOnly);
          pathFileName=null;
          return false;
        }
      });
      workerTask.ContinueWith(continuation =>
      {
        BusyIndicator=false;
        base.OnPropertyChanged("Parameters");
        base.OnPropertyChanged("SelectedParameter");
        base.OnPropertyChanged("ParameterValue");
        base.OnPropertyChanged("Title");
        base.OnPropertyChanged("DisplayName");
        base.OnPropertyChanged("AllPositions");
        base.OnPropertyChanged("CanMonitor");
        base.OnPropertyChanged("CashBalance");
        base.OnPropertyChanged("NonTradeableCash");
        base.OnPropertyChanged("HedgeCash");
        base.OnPropertyChanged("ModelExpectation");
        base.OnPropertyChanged("ExpectationColor");
        base.OnPropertyChanged("ExpectationDescription");
        base.OnPropertyChanged("ReloadEnabled");
        base.OnPropertyChanged("LastTradeDate");
        base.OnPropertyChanged("NextTradeDate");
      });
      return true;
    }

    private void RunPerformance()
    {
      if(null==sessionParams)return;
      modelPerformanceSeries=MGSHMomentumBacktest.GetModelPerformance(sessionParams);
      base.OnPropertyChanged("Data");
      base.OnPropertyChanged("GraphTitle");
    }
// ****************************************************************************  C H A R T  P L O T T E R *********************************************************
    public String LegendVisible
    {
      get
      {
        if(isLegendVisible) return "true";
        return "false";
      }
      set
      {
        isLegendVisible=Boolean.Parse(value);
        base.OnPropertyChanged("LegendVisible");
      }
    }

    public String PercentButtonText
    {
      get
      {
        if(!showAsGainLoss) return "Show $";
        else return "Show %";
      }
    }

    public ICommand ToggleReturnOrPercentCommand
    {
      get
      {
        if(toggleReturnOrPercentCommand==null)
        {
          toggleReturnOrPercentCommand=new RelayCommand(action =>
          {
            showAsGainLoss=!showAsGainLoss;
            base.OnPropertyChanged("Data");
            base.OnPropertyChanged("PercentButtonText");
            base.OnPropertyChanged("GraphTitle");
          },action => { return true; });
        }
        return toggleReturnOrPercentCommand;
      }
    }

    public CompositeDataSource Data
    {
      get
      {
        if(null==modelPerformanceSeries) return null;
        CompositeDataSource compositeDataSource=null;
        compositeDataSource=GainLossModel.GainLoss(modelPerformanceSeries,showAsGainLoss);
        return compositeDataSource;
      }
    }

    public String GraphTitle
    {
      get
      {
        if(null==sessionParams||null==modelPerformanceSeries) return "";
        StringBuilder sb=new StringBuilder();
        MGSHPositions allPositions=sessionParams.GetCombinedPositions();
        DateTime minDate=allPositions.Min(x => x.PurchaseDate);
        DateTime maxDate=PricingDA.GetLatestDate();

        if(modelPerformanceSeries.Count<2)
        {
          sb.Append(showAsGainLoss?"$ GainLoss":"% Return");
          sb.Append(" ");
          sb.Append("(").Append(minDate.ToShortDateString()).Append("-").Append(maxDate.ToShortDateString()).Append(")");
          sb.Append(showAsGainLoss?Utility.FormatCurrency(modelPerformanceSeries[modelPerformanceSeries.Count-1].CumulativeGainLoss):Utility.FormatPercent(modelPerformanceSeries[modelPerformanceSeries.Count-1].CumProdMinusOne));
          return sb.ToString();
        }
        if(showAsGainLoss)
        {
          double latestGainLoss=modelPerformanceSeries[modelPerformanceSeries.Count-1].CumulativeGainLoss;
          double change=modelPerformanceSeries[modelPerformanceSeries.Count-1].GainLossDOD;
          sb.Append("$ GainLoss");
          sb.Append(" ");
          sb.Append("(").Append(minDate.ToShortDateString()).Append("-").Append(maxDate.ToShortDateString()).Append(")");
          sb.Append(" ");
          sb.Append(Utility.FormatCurrency(latestGainLoss));
          sb.Append(",");
          sb.Append(" ");
          sb.Append(change>0.00?"+":"").Append(Utility.FormatCurrency(change));
        }
        else
        {
          double latestCumGainLoss=modelPerformanceSeries[modelPerformanceSeries.Count-1].CumProdMinusOne;
          double prevCumGainLoss=modelPerformanceSeries[modelPerformanceSeries.Count-2].CumProdMinusOne;
          double change=latestCumGainLoss-prevCumGainLoss;
          sb.Append("% Return");
          sb.Append(" ");
          sb.Append("(").Append(minDate.ToShortDateString()).Append("-").Append(maxDate.ToShortDateString()).Append(")");
          sb.Append(" ");
          sb.Append(Utility.FormatPercent(latestCumGainLoss));
          sb.Append(",");
          sb.Append(" ");
          sb.Append(change>0.00?"+":"").Append(Utility.FormatPercent(change));
        }
        return sb.ToString();
      }
    }
    // ***************************************************************************************************************************************************
    // This getter returns non-model (MarketData.MarketDataModel) specific stop limits to pass along to the bollinger bands
    public MarketData.MarketDataModel.StopLimits GetHistoricalStopLimitsMarketDataModel()
    {
      if (null == sessionParams || null == selectedPosition) return null;
      DateGenerator dateGenerator = new DateGenerator();

      MarketData.MarketDataModel.StopLimits marketDataModelStopLimits = new MarketData.MarketDataModel.StopLimits();

      MarketData.Generator.Model.StopLimits stopLimits =
        new MarketData.Generator.Model.StopLimits((from MarketData.Generator.Model.StopLimit stopLimit in sessionParams.StopLimits
          where stopLimit.StopLimitId.Equals(selectedPosition.Symbol + Utility.DateTimeToStringYYYYMMDDMMSSTT(selectedPosition.PurchaseDate)) select stopLimit).
          OrderByDescending(x => x.AnalysisDate).ToList());

      MarketData.MarketDataModel.StopLimit initialStopLimit = new MarketData.MarketDataModel.StopLimit();
      initialStopLimit.Symbol = selectedPosition.Symbol;
      initialStopLimit.Shares = 0;
      initialStopLimit.StopPrice = selectedPosition.InitialStopLimit;
      initialStopLimit.StopType = StopLimitConstants.STOP_QUOTE;
      initialStopLimit.EffectiveDate = selectedPosition.PurchaseDate;
      initialStopLimit.Active = 1;
      marketDataModelStopLimits.Add(initialStopLimit);

      foreach (MarketData.Generator.Model.StopLimit stopLimit in stopLimits)
      {
        MarketData.MarketDataModel.StopLimit marketDataModelStopLimit = new MarketData.MarketDataModel.StopLimit();
        marketDataModelStopLimit.Symbol = stopLimit.Symbol;
        marketDataModelStopLimit.Shares = 0;
        marketDataModelStopLimit.StopPrice = stopLimit.NewStop;
        marketDataModelStopLimit.StopType = StopLimitConstants.STOP_QUOTE;
        marketDataModelStopLimit.EffectiveDate = stopLimit.AnalysisDate;
        marketDataModelStopLimit.Active = 1;
        marketDataModelStopLimits.Add(marketDataModelStopLimit);
      }

      return marketDataModelStopLimits;
    }

// ****************************************************************************************************************************************************************
// *************************************************************************** T O O L T I P  C A L L O U T S *****************************************************
// ****************************************************************************************************************************************************************
    public String ToolTipTrailingStop
    {
      get
      {
        if(null==selectedPosition) return "Please select a position by clicking on a row.";
        StringBuilder sb=new StringBuilder();
        sb.Append("[TrailingStop]=([TrailingStopLimit])").Append("\n");
        sb.Append(Utility.FormatCurrency(selectedPosition.TrailingStopLimit)).Append("=").Append(Utility.FormatCurrency(selectedPosition.TrailingStopLimit)).Append("\n");
        List<MarketData.Generator.Model.StopLimit> stopLimits=GetHistoricalStopLimits();
        if(selectedPosition.TrailingStopLimit > selectedPosition.PurchasePrice)
        {
          sb.Append("Riskless Gain").Append(" = ");
          sb.Append(String.Format("(Trailing Stop - Purchase Price) * Shares"));
          sb.Append("\n");
          sb.Append(String.Format("{0}",Utility.FormatCurrency((selectedPosition.TrailingStopLimit-selectedPosition.PurchasePrice)*selectedPosition.Shares)));
          sb.Append(String.Format("=({0}-{1})*{2}",Utility.FormatCurrency(selectedPosition.TrailingStopLimit),Utility.FormatCurrency(selectedPosition.PurchasePrice),Utility.FormatNumber(selectedPosition.Shares,3)));
          sb.Append("\n");
        }
        if(null!=stopLimits && 0!=stopLimits.Count)
        {
          sb.Append("Stop History").Append("\n");
          foreach(MarketData.Generator.Model.StopLimit stopLimit in stopLimits)
          {
            sb.Append(String.Format("Analysis Date:{0} New Stop:{1} Previous Stop:{2}",Utility.DateTimeToStringMMSDDSYYYY(stopLimit.AnalysisDate),Utility.FormatCurrency(stopLimit.NewStop),Utility.FormatCurrency(stopLimit.PreviousStop)));
            sb.Append("\n");
          }
        }
        return sb.ToString();
      }
    }

    public String ToolTipInitialStop
    {
      get
      {
        if(null==selectedPosition) return "Please select a position by clicking on a row.";
        StringBuilder sb=new StringBuilder();
        sb.Append("[InitialStop]=([InitialStopLimit])").Append("\n");
        sb.Append(Utility.FormatCurrency(selectedPosition.InitialStopLimit)).Append("=").Append(Utility.FormatCurrency(selectedPosition.InitialStopLimit));
        return sb.ToString();
      }
    }

    public String ToolTipR
    {
      get
      {
        if(null==selectedPosition)return "Please select a position by clicking on a row.";
        StringBuilder sb=new StringBuilder();
        sb.Append("Current R/Share.  See RMultiple for initial risk assumptions.").Append("\n");
        sb.Append("R/Share=TrailingStop>PurchasePrice?0.00:PurchasePrice-TrailingStop").Append("\n");
        if(selectedPosition.TrailingStopLimit>selectedPosition.PurchasePrice)
        {
          sb.Append(Utility.FormatCurrency(0)).Append("=").Append(Utility.FormatCurrency(0));
        }
        else
        {
          sb.Append(Utility.FormatCurrency(selectedPosition.TrailingStopLimit>selectedPosition.PurchasePrice?0.00:selectedPosition.PurchasePrice-selectedPosition.TrailingStopLimit));
          sb.Append("=").Append(Utility.FormatCurrency(selectedPosition.PurchasePrice)).Append("-").Append(Utility.FormatCurrency(selectedPosition.TrailingStopLimit));
        }
        return sb.ToString();
      }
    }

    public String ToolTipTotalRiskExposure
    {
      get
      {
        if(null==selectedPosition)return "Please select a position by clicking on a row.";
        StringBuilder sb=new StringBuilder();
        sb.Append("Current Risk.  See RMultiple for initial risk assumptions.").Append("\n");
        sb.Append("[TotalRiskExposure]=([R/Share]*[Shares])").Append("\n");
        if(null==selectedPosition) return sb.ToString();
        sb.Append(Utility.FormatCurrency(selectedPosition.TotalRiskExposure)).Append("=(").Append(Utility.FormatCurrency(selectedPosition.R)).Append("*").Append(Utility.FormatNumber(selectedPosition.Shares,0)).Append(")");
        return sb.ToString();
      }
    }

    public String ToolTipRMultiple
    {
      get
      {
        if(null==selectedPosition)return "Please select a position by clicking on a row.";
        StringBuilder sb=new StringBuilder();
        sb.Append("RMultiple is based on original position setup.").Append("\n");
        sb.Append("Original Exposure=").Append(Utility.FormatCurrency(selectedPosition.PurchasePrice*selectedPosition.Shares)).Append("\n");
        sb.Append("Original R/Share=PurchasePrice-InitialStop").Append("\n");
        sb.Append(Utility.FormatCurrency(selectedPosition.PurchasePrice-selectedPosition.InitialStopLimit)).Append("=").Append(Utility.FormatCurrency(selectedPosition.PurchasePrice)).Append("-").Append(Utility.FormatCurrency(selectedPosition.InitialStopLimit)).Append("\n");
        sb.Append("Original Risk=Original R/Share*Shares").Append("\n");
        sb.Append(Utility.FormatCurrency((selectedPosition.PurchasePrice-selectedPosition.InitialStopLimit)*selectedPosition.Shares)).Append("=").Append(Utility.FormatCurrency(selectedPosition.PurchasePrice-selectedPosition.InitialStopLimit)).Append("*").Append(selectedPosition.Shares).Append("\n");
        sb.Append("RMultiple=(CurrentPrice-PurchasePrice)/(PurchasePrice-InitialStop)").Append("\n");
        sb.Append(selectedPosition.RMultipleAsString).Append("=").Append("(").Append(Utility.FormatCurrency(selectedPosition.CurrentPrice)).Append("-").Append(Utility.FormatCurrency(selectedPosition.PurchasePrice)).Append(")");
        sb.Append("/").Append("(").Append(Utility.FormatCurrency(selectedPosition.PurchasePrice)).Append("-").Append(Utility.FormatCurrency(selectedPosition.InitialStopLimit)).Append(")");
        return sb.ToString();
      }
    }

    public String ToolTipSold
    {
      get
      {
        if(null==selectedPosition)return "Please select a position by clicking on a row.";
        StringBuilder sb=new StringBuilder();
        if(selectedPosition.IsActivePosition)
        {
          sb.Append($"{selectedPosition.Symbol} is currently active.");
        }
        else
        {
          sb.Append($"{selectedPosition.Symbol} {selectedPosition.Comment}.");
        }

        return sb.ToString();
      }
    }

    public String ToolTipExposure
    {
      get
      {
        if(null==selectedPosition)return "Please select a position by clicking on a row.";
        StringBuilder sb=new StringBuilder();
        if(selectedPosition.IsActivePosition)
        {
          sb.Append("Exposure = PurchasePrice * Shares\n");
          sb.Append($"{Utility.FormatCurrency(selectedPosition.Exposure)} = {Utility.FormatCurrency(selectedPosition.PurchasePrice)} * {Utility.FormatNumber(selectedPosition.Shares,3)}");
        }
        else
        {
          sb.Append("Original Exposure = PurchasePrice * Shares\n");
          sb.Append($"{Utility.FormatCurrency(selectedPosition.PurchasePrice * selectedPosition.Shares)} = {Utility.FormatCurrency(selectedPosition.PurchasePrice)} * {Utility.FormatNumber(selectedPosition.Shares,3)}");
        }

        return sb.ToString();
      }
    }

// ************************************************************** T O O L T I P  H E L P E R S  **************************************************************
    public MarketData.Generator.Model.StopLimits GetHistoricalStopLimits()
    {
      if(null==sessionParams||null==selectedPosition) return null;
      MarketData.Generator.Model.StopLimits stopLimits=new MarketData.Generator.Model.StopLimits(
        (from MarketData.Generator.Model.StopLimit stopLimit in sessionParams.StopLimits where stopLimit.StopLimitId.Equals(selectedPosition.Symbol + Utility.DateTimeToStringYYYYMMDDMMSSTT(selectedPosition.PurchaseDate)) select stopLimit)
        .OrderByDescending(x => x.AnalysisDate).ToList());
      return stopLimits;
    }
  }
}

