internal void SwitchActiveSource(SwitchRelationBooleanPropertyMessage message)
        {
            VmInstrument vmInstrument = this._Instruments.Single(i => i.Id == message.InstrumentId);

            vmInstrument.SourceRelations.Single(r => r.Id == message.NewRelationId).ApplyChangeToUI(message.PropertyName, true);
            vmInstrument.SourceRelations.Single(r => r.Id == message.OldRelationId).ApplyChangeToUI(message.PropertyName, false);
        }
        public void Add(InstrumentSourceRelation relation)
        {
            VmInstrument      vmInstrument      = this._Instruments.Single(i => i.Id == relation.InstrumentId);
            VmQuotationSource vmQuotationSource = this._QuotationSources.Single(s => s.Id == relation.SourceId);

            vmInstrument.SourceRelations.Add(new VmInstrumentSourceRelation(relation, vmInstrument, vmQuotationSource));
        }
        public void SetPrimitiveQuotation(PrimitiveQuotation quotation)
        {
            VmInstrument vmInstrument = this._Instruments.SingleOrDefault(i => i.Id == quotation.InstrumentId);

            if (vmInstrument != null)
            {
                VmInstrumentSourceRelation relation = vmInstrument.SourceRelations.SingleOrDefault(r => r.SourceId == quotation.SourceId);
                if (relation != null)
                {
                    if (relation.SetSourceQuotation(new VmSourceQuotation(quotation)))
                    {
                        for (int i = 0; i < vmInstrument.SourceRelations.Count; i++)
                        {
                            VmInstrumentSourceRelation relation2 = vmInstrument.SourceRelations[i];
                            if (!object.ReferenceEquals(relation2, relation))
                            {
                                relation2.SetSourceQuotation(new VmSourceQuotation(new PrimitiveQuotation()
                                {
                                    Timestamp = quotation.Timestamp
                                }));
                            }
                        }
                    }
                }
            }
        }
        public InstrumentSourceRelationWindow(VmInstrument vmInstrument, EditMode editMode, VmInstrumentSourceRelation vmRelation = null)
        {
            InitializeComponent();
            this._EditMode = editMode;
            this._vmInstrument = vmInstrument;
            this._originRelation = vmRelation;

            if (editMode == EditMode.AddNew)
            {
                this._Relation = new InstrumentSourceRelation() { InstrumentId = this._vmInstrument.Id, SwitchTimeout = 60, Priority = 30 };
                this._Relation.IsActive = vmInstrument.SourceRelations.Count == 0;  // Only for UI display(the backend can process normally)
                this._Relation.IsDefault = false;
                this.IsDefaultCheckBox.IsEnabled = false;
            }
            else
            {
                this._Relation = vmRelation.InstrumentSourceRelation.Clone();
                this.SourcesComboBox.IsEnabled = false;
            }
            InstrumentCodeTextBlock.Text = vmInstrument.Code;
            this.BindSourcesComboBox();
            this.DataContext = this._Relation;

            this._HintMessage = new HintMessage(this.HintTextBlock);
            this.Loaded += InstrumentSourceRelationWindow_Loaded;
        }
 internal void SetQuotation(QuotationsMessage message)
 {
     foreach (GeneralQuotation generalQuotation in message.Quotations)
     {
         VmInstrument vmInstrument = this._Instruments.SingleOrDefault(i => i.Id == generalQuotation.InstrumentId);
         if (vmInstrument != null)
         {
             vmInstrument.SetQuotation(generalQuotation, vmInstrument.DecimalPlace);
         }
     }
 }
 public void BindToInstrument(VmInstrument instrument)
 {
     if (instrument == null || instrument.IsDerivative)
     {
         this.DataContext = this.RelationGrid.ItemsSource = null;
         this.AddRelationButton.Visibility = Visibility.Hidden;
     }
     else
     {
         this.DataContext = instrument;
         this.RelationGrid.ItemsSource = instrument.SourceRelations;
         this.AddRelationButton.Visibility = Visibility.Visible;
     }
 }
 public void BindToInstrument(VmInstrument instrument)
 {
     if (instrument == null || instrument.IsDerivative)
     {
         this.DataContext = null;
     }
     else
     {
         if (!object.ReferenceEquals(this.DataContext, instrument))
         {
             this.DataContext = instrument;
         }
     }
 }
        public DerivedInstrumentWindow(EditMode editMode, VmInstrument vmInstrument = null)
        {
            InitializeComponent();
            this._EditMode = editMode;
            this._vmInstrument = vmInstrument;
            this._InstrumentData = new InstrumentData();
            if (editMode == EditMode.AddNew)
            {
                this._InstrumentData.Instrument = new Instrument() { IsDerivative = true };
                this._InstrumentData.DerivativeRelation = new DerivativeRelation()
                {
                    AskOperand1Type = OperandType.Ask,
                    AskOperand2Type = OperandType.Ask,
                    AskOperator1Type = OperatorType.Multiply,
                    AskOperator2Type = OperatorType.Multiply,
                    AskOperand3 = 1,
                    BidOperand1Type = OperandType.Bid,
                    BidOperand2Type = OperandType.Bid,
                    BidOperator1Type = OperatorType.Multiply,
                    BidOperator2Type = OperatorType.Multiply,
                    BidOperand3 = 1,
                    LastOperand1Type = OperandType.Last,
                    LastOperand2Type = OperandType.Last,
                    LastOperator1Type = OperatorType.Multiply,
                    LastOperator2Type = OperatorType.Multiply,
                    LastOperand3 = 1,
                    UnderlyingInstrument1IdInverted = false,
                    UnderlyingInstrument2Id = null
                };
                if(VmQuotationManager.Instance.Instruments.Count>0)
                {
                    this._InstrumentData.DerivativeRelation.UnderlyingInstrument1Id = VmQuotationManager.Instance.Instruments[0].Id;
                }
            }
            else
            {
                this._InstrumentData.Instrument = vmInstrument.Instrument.Clone();
                this._InstrumentData.DerivativeRelation = vmInstrument.VmDerivativeRelation.DerivativeRelation.Clone();
            }

            this.DataContext = this._InstrumentData;
            this._HintMessage = new HintMessage(this.HintTextBlock);
        }
        public InstrumentWindow(EditMode editMode, VmInstrument vmInstrument =  null)
        {
            InitializeComponent();
            this._EditMode = editMode;
            this._vmInstrument = vmInstrument;
            this._InstrumentData = new InstrumentData();
            if (editMode == EditMode.AddNew)
            {
                this._InstrumentData.Instrument = new Instrument() { IsDerivative = false, IsSwitchUseAgio = false, UseWeightedPrice = false };
                this._InstrumentData.PriceRangeCheckRule = new PriceRangeCheckRule();
                this._InstrumentData.WeightedPriceRule = new WeightedPriceRule();
            }
            else
            {
                this._InstrumentData.Instrument = vmInstrument.Instrument.Clone();
                this._InstrumentData.PriceRangeCheckRule = vmInstrument.VmPriceRangeCheckRule.PriceRangeCheckRule.Clone();
                this._InstrumentData.WeightedPriceRule = vmInstrument.VmWeightedPriceRule.WeightedPriceRule.Clone();
            }

            this.DataContext = this._InstrumentData;
            this._HintMessage = new HintMessage(this.HintTextBlock);
        }
 public VmInstrumentSourceRelation(InstrumentSourceRelation relation, VmInstrument instrument, VmQuotationSource quotationSource)
     : base(relation)
 {
     this._Relation = relation;
     this._Instrument = instrument;
     this._QuotationSource = quotationSource;
     this.SourceQuotations = new ObservableCollection<VmSourceQuotation>();
     this.PropertyChanged += delegate(object sender, PropertyChangedEventArgs e)
     {
         if (e.PropertyName == FieldSR.IsActive)
         {
             this.OnPropertyChanged("ActiveState");
         }
     };
 }
        public void Initialize()
        {
            try
            {
                //if (this._MetadataNotLoaded)
                //{
                ConfigMetadata metadata = ConsoleClient.Instance.GetConfigMetadata();

                this._QuotationSources.Clear();
                foreach (var source in metadata.QuotationSources.Values)
                {
                    this._QuotationSources.Add(new VmQuotationSource(source));
                }

                this._Instruments.Clear();
                foreach (var instrument in metadata.Instruments.Values)
                {
                    VmInstrument vmInstrument = new VmInstrument(instrument);
                    if (instrument.IsDerivative)
                    {
                        vmInstrument.VmDerivativeRelation = new VmDerivativeRelation(metadata.DerivativeRelations[instrument.Id]);
                    }
                    else
                    {
                        foreach (Dictionary <string, InstrumentSourceRelation> dict in metadata.InstrumentSourceRelations.Values)
                        {
                            var relation = dict.Values.SingleOrDefault(r => r.InstrumentId == instrument.Id);
                            if (relation != null)
                            {
                                VmQuotationSource vmQuotationSource = this._QuotationSources.Single(s => s.Id == relation.SourceId);
                                vmInstrument.SourceRelations.Add(new VmInstrumentSourceRelation(relation, vmInstrument, vmQuotationSource));
                            }
                        }

                        if (!instrument.IsDerivative)
                        {
                            vmInstrument.VmPriceRangeCheckRule = new VmPriceRangeCheckRule(metadata.PriceRangeCheckRules[instrument.Id]);
                            if (metadata.WeightedPriceRules.ContainsKey(instrument.Id))
                            {
                                vmInstrument.VmWeightedPriceRule = new VmWeightedPriceRule(metadata.WeightedPriceRules[instrument.Id]);
                            }
                            else
                            {
                                vmInstrument.VmWeightedPriceRule = new VmWeightedPriceRule(new WeightedPriceRule());
                            }
                        }
                    }

                    // set quotation
                    GeneralQuotation generalQuotation;
                    if (metadata.LastQuotations.TryGetValue(instrument.Id, out generalQuotation))
                    {
                        vmInstrument.SetQuotation(generalQuotation, vmInstrument.DecimalPlace);
                    }

                    this._Instruments.Add(vmInstrument);
                    //}
                    //this._MetadataNotLoaded = false;
                }
            }
            catch (Exception exception)
            {
                Logger.TraceEvent(TraceEventType.Error, "VmQuotationManager.LoadMetadata\r\n{0}", exception);
            }
        }
 private bool FindVmInstrumentSourceRelation(int relationId, out VmInstrumentSourceRelation relation, out VmInstrument instrument)
 {
     relation   = null;
     instrument = null;
     foreach (VmInstrument vmInstrument in this._Instruments)
     {
         relation = vmInstrument.SourceRelations.SingleOrDefault(r => r.Id == relationId);
         if (relation != null)
         {
             instrument = vmInstrument;
             break;
         }
     }
     return(relation != null);
 }
 private bool FindVmInstrumentSourceRelation(int relationId, out VmInstrumentSourceRelation relation, out VmInstrument instrument)
 {
     relation = null;
     instrument = null;
     foreach (VmInstrument vmInstrument in this._Instruments)
     {
         relation = vmInstrument.SourceRelations.SingleOrDefault(r => r.Id == relationId);
         if (relation != null)
         {
             instrument = vmInstrument;
             break;
         }
     }
     return relation != null;
 }
        public void Initialize()
        {
            try
            {
                //if (this._MetadataNotLoaded)
                //{
                    ConfigMetadata metadata = ConsoleClient.Instance.GetConfigMetadata();

                    this._QuotationSources.Clear();
                    foreach (var source in metadata.QuotationSources.Values)
                    {
                        this._QuotationSources.Add(new VmQuotationSource(source));
                    }

                    this._Instruments.Clear();
                    foreach (var instrument in metadata.Instruments.Values)
                    {
                        VmInstrument vmInstrument = new VmInstrument(instrument);
                        if (instrument.IsDerivative)
                        {
                            vmInstrument.VmDerivativeRelation = new VmDerivativeRelation(metadata.DerivativeRelations[instrument.Id]);
                        }
                        else
                        {
                            foreach (Dictionary<string, InstrumentSourceRelation> dict in metadata.InstrumentSourceRelations.Values)
                            {
                                var relation = dict.Values.SingleOrDefault(r => r.InstrumentId == instrument.Id);
                                if (relation != null)
                                {
                                    VmQuotationSource vmQuotationSource = this._QuotationSources.Single(s => s.Id == relation.SourceId);
                                    vmInstrument.SourceRelations.Add(new VmInstrumentSourceRelation(relation, vmInstrument, vmQuotationSource));
                                }
                            }

                            if (!instrument.IsDerivative)
                            {
                                vmInstrument.VmPriceRangeCheckRule = new VmPriceRangeCheckRule(metadata.PriceRangeCheckRules[instrument.Id]);
                                if (metadata.WeightedPriceRules.ContainsKey(instrument.Id))
                                {
                                    vmInstrument.VmWeightedPriceRule = new VmWeightedPriceRule(metadata.WeightedPriceRules[instrument.Id]);
                                }
                                else
                                {
                                    vmInstrument.VmWeightedPriceRule = new VmWeightedPriceRule(new WeightedPriceRule());
                                }
                            }
                        }

                        // set quotation
                        GeneralQuotation generalQuotation;
                        if (metadata.LastQuotations.TryGetValue(instrument.Id, out generalQuotation))
                        {
                            vmInstrument.SetQuotation(generalQuotation, vmInstrument.DecimalPlace);
                        }

                        this._Instruments.Add(vmInstrument);
                    //}
                    //this._MetadataNotLoaded = false;
                }
            }
            catch(Exception exception)
            {
                Logger.TraceEvent(TraceEventType.Error, "VmQuotationManager.LoadMetadata\r\n{0}", exception);
            }
        }
 private void SelectRow(VmInstrument vmInstrument)
 {
     this._CurrentVmInstrument = vmInstrument;
     if (this._CurrentVmInstrument != null)
     {
         this.DataContext = this._CurrentVmInstrument;
         this._Timer.Change(500, Timeout.Infinite);
     }
 }