/// <summary>
        ///
        /// </summary>
        /// <param name="instrument">If we're updating or cloning an instrument, pass it here.</param>
        /// <param name="addingNew">True if adding a new instrument. False if we're updating an instrument.</param>
        /// <param name="addingContFut">True if adding a continuous futures instrument.</param>
        public AddInstrumentManuallyWindow(Instrument instrument = null, bool addingNew = true, bool addingContFut = false)
        {
            InitializeComponent();

            //If it's a continuous future, make the continuous future tab visible
            if ((instrument != null && instrument.IsContinuousFuture) ||
                addingContFut)
            {
                ContFutTabItem.Visibility = Visibility.Visible;
                TypeComboBox.IsEnabled = false;
            }
            else
            {
                ContFutTabItem.Visibility = Visibility.Hidden;
            }

            DataContext = this;
            _addingNew = addingNew;

            _context = new MyDBContext();

            if (instrument != null)
            {
                _context.Instruments.Attach(instrument);
                _context.Entry(instrument).Reload();
                if (instrument.Exchange != null)
                    _context.Entry(instrument.Exchange).Reload();

                if (instrument.ContinuousFuture != null)
                {
                    _context.ContinuousFutures.Attach(instrument.ContinuousFuture);
                    _context.Entry(instrument.ContinuousFuture).Reload();
                }

                if (!addingNew)
                {
                    if (instrument.Tags != null)
                    {
                        foreach (Tag tag in instrument.Tags)
                        {
                            _context.Tags.Attach(tag);
                        }
                    }

                    if (instrument.Sessions != null)
                    {
                        foreach (InstrumentSession session in instrument.Sessions)
                        {
                            _context.InstrumentSessions.Attach(session);
                        }
                    }
                }

                TheInstrument = addingNew ? (Instrument)instrument.Clone() : instrument;
                if (TheInstrument.Tags == null) TheInstrument.Tags = new List<Tag>();
                if (TheInstrument.Sessions == null) TheInstrument.Sessions = new List<InstrumentSession>();

                TheInstrument.Sessions = TheInstrument.Sessions.OrderBy(x => x.OpeningDay).ThenBy(x => x.OpeningTime).ToList();

                _originalSessions = new List<InstrumentSession>(TheInstrument.Sessions);
            }
            else
            {
                TheInstrument = new Instrument
                {
                    Tags = new List<Tag>(),
                    Sessions = new List<InstrumentSession>()
                };

                //need to do some extra stuff if it's a continuous future
                if (addingContFut)
                {
                    TheInstrument.ContinuousFuture = new ContinuousFuture();
                    TheInstrument.Type = InstrumentType.Future;
                    TheInstrument.IsContinuousFuture = true;
                }

                CustomRadioBtn.IsChecked = true;
            }

            //Tags
            Tags = new ObservableCollection<CheckBoxTag>();
            foreach (Tag t in _context.Tags)
            {
                Tags.Add(new CheckBoxTag(t, TheInstrument.Tags.Contains(t)));
            }

            //Sessions
            SelectedSessions = new ObservableCollection<InstrumentSession>(TheInstrument.Sessions);

            //Window title
            if (addingNew)
            {
                Title = "Add New Instrument";
                AddBtn.Content = "Add";
            }
            else
            {
                Title = "Modify Instrument";
                AddBtn.Content = "Modify";
            }

            Exchanges = new ObservableCollection<Exchange>();

            var exchangeList = _context.Exchanges.AsEnumerable().OrderBy(x => x.Name);
            foreach (Exchange e in exchangeList)
            {
                Exchanges.Add(e);
            }

            //fill template box
            var templates = _context.SessionTemplates.Include("Sessions").ToList();
            foreach (SessionTemplate t in templates)
            {
                TemplateComboBox.Items.Add(t);
            }
            if (TheInstrument.SessionsSource == SessionsSource.Template)
            {
                TemplateComboBox.SelectedItem = templates.First(x => x.ID == TheInstrument.SessionTemplateID);
            }

            //set the right radio button...
            CustomRadioBtn.IsChecked = TheInstrument.SessionsSource == SessionsSource.Custom;
            TemplateRadioBtn.IsChecked = TheInstrument.SessionsSource == SessionsSource.Template;
            ExchangeRadioBtn.IsChecked = TheInstrument.SessionsSource == SessionsSource.Exchange;

            //populate instrument type combobox with enum values
            var instrumentTypeValues = MyUtils.GetEnumValues<InstrumentType>();
            foreach (InstrumentType t in instrumentTypeValues)
            {
                TypeComboBox.Items.Add(t);
            }

            //populate option type combobox with enum values
            var optionTypeValues = MyUtils.GetEnumValues<OptionType>();
            foreach (OptionType t in optionTypeValues)
            {
                OptionTypeComboBox.Items.Add(t);
            }

            var dataSources = _context.Datasources.AsEnumerable();
            foreach (Datasource d in dataSources)
            {
                DatasourceComboBox.Items.Add(d);
            }

            //sort the sessions so they're ordered properly...
            SessionsGrid.Items.SortDescriptions.Add(new System.ComponentModel.SortDescription("OpeningDay", System.ComponentModel.ListSortDirection.Ascending));

            //fill the RolloverRuleType combobox
            var rolloverTypes = MyUtils.GetEnumValues<ContinuousFuturesRolloverType>();
            foreach (ContinuousFuturesRolloverType t in rolloverTypes)
            {
                if (t != ContinuousFuturesRolloverType.Time)
                    RolloverRuleType.Items.Add(t);
            }

            //fill the RootSymbolComboBox
            foreach (UnderlyingSymbol s in _context.UnderlyingSymbols)
            {
                RootSymbolComboBox.Items.Add(s);
            }

            ContractMonths = new ObservableCollection<KeyValuePair<int, string>>();
            //fill the continuous futures contrat month combobox
            for (int i = 1; i < 10; i++)
            {
                ContractMonths.Add(new KeyValuePair<int, string>(i, MyUtils.Ordinal(i) + " Contract"));
            }

            //time or rule-based rollover, set the radio button check
            if (TheInstrument.ContinuousFuture != null)
            {
                if (TheInstrument.ContinuousFuture.RolloverType == ContinuousFuturesRolloverType.Time)
                {
                    RolloverTime.IsChecked = true;
                }
                else
                {
                    RolloverRule.IsChecked = true;
                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="instrument">If we're updating or cloning an instrument, pass it here.</param>
        /// <param name="addingNew">True if adding a new instrument. False if we're updating an instrument.</param>
        public AddInstrumentManuallyWindow(Instrument instrument = null, bool addingNew = true)
        {
            InitializeComponent();

            DataContext = this;
            _addingNew = addingNew;

            var context = new MyDBContext();

            if (instrument != null)
            {
                context.Instruments.Attach(instrument);
                context.Entry(instrument).Reload();
                context.Entry(instrument.Exchange).Reload();
                TheInstrument = (Instrument)instrument.Clone();
                if (TheInstrument.Tags == null) TheInstrument.Tags = new List<Tag>();
                if (TheInstrument.Sessions == null) TheInstrument.Sessions = new List<InstrumentSession>();
                TheInstrument.Sessions = TheInstrument.Sessions.OrderBy(x => x.OpeningDay).ThenBy(x => x.OpeningTime).ToList();
            }
            else
            {
                TheInstrument = new Instrument
                {
                    Tags = new List<Tag>(),
                    Sessions = new List<InstrumentSession>()
                };
            }

            Tags = new ObservableCollection<CheckBoxTag>();
            foreach (Tag t in context.Tags)
            {
                Tags.Add(new CheckBoxTag(t, TheInstrument.Tags.Contains(t)));
            }

            if (addingNew)
            {
                Title = "Add New Instrument";
                AddBtn.Content = "Add";
            }
            else
            {
                Title = "Modify Instrument";
                AddBtn.Content = "Modify";
                _originalInstrument = instrument;
            }

            Exchanges = new ObservableCollection<Exchange>();

            var exchangeList = context.Exchanges.AsEnumerable().OrderBy(x => x.Name);
            foreach (Exchange e in exchangeList)
            {
                Exchanges.Add(e);
            }

            //fill template box
            var templates = context.SessionTemplates.Include("Sessions").ToList();
            foreach (SessionTemplate t in templates)
            {
                TemplateComboBox.Items.Add(t);
            }
            if (TheInstrument.SessionsSource == SessionsSource.Template)
            {
                TemplateComboBox.SelectedItem = templates.First(x => x.ID == TheInstrument.SessionTemplateID);
            }

            //set the right radio button...
            CustomRadioBtn.IsChecked = TheInstrument.SessionsSource == SessionsSource.Custom;
            TemplateRadioBtn.IsChecked = TheInstrument.SessionsSource == SessionsSource.Template;
            ExchangeRadioBtn.IsChecked = TheInstrument.SessionsSource == SessionsSource.Exchange;

            //populate instrument type combobox with enum values
            var instrumentTypeValues = MyUtils.GetEnumValues<InstrumentType>();
            foreach (InstrumentType t in instrumentTypeValues)
            {
                TypeComboBox.Items.Add(t);
            }

            //populate option type combobox with enum values
            var optionTypeValues = MyUtils.GetEnumValues<OptionType>();
            foreach (OptionType t in optionTypeValues)
            {
                OptionTypeComboBox.Items.Add(t);
            }

            var dataSources = context.Datasources.AsEnumerable();
            foreach (Datasource d in dataSources)
            {
                DatasourceComboBox.Items.Add(d);
            }

            //sort the sessions so they're ordered properly...
            SessionsGrid.Items.SortDescriptions.Add(new System.ComponentModel.SortDescription("OpeningDay", System.ComponentModel.ListSortDirection.Ascending));

            context.Dispose();
        }