public static void AddMetadataObject(DerivativeRelation entity)
 {
     string sql = "INSERT DerivativeRelation(InstrumentId,UnderlyingInstrument1Id,UnderlyingInstrument1IdInverted,UnderlyingInstrument2Id,AskOperand1Type,AskOperator1Type,AskOperand2Type,AskOperator2Type,AskOperand3,BidOperand1Type,BidOperator1Type,BidOperand2Type,BidOperator2Type,BidOperand3,LastOperand1Type,LastOperator1Type,LastOperand2Type,LastOperator2Type,LastOperand3) VALUES (@instrumentId,@underlyingInstrument1Id,@underlyingInstrument1IdInverted,@underlyingInstrument2Id,@askOperand1Type,@askOperator1Type,@askOperand2Type,@askOperator2Type,@askOperand3,@bidOperand1Type,@bidOperator1Type,@bidOperand2Type,@bidOperator2Type,@bidOperand3,@lastOperand1Type,@lastOperator1Type,@lastOperand2Type,@lastOperator2Type,@lastOperand3);SELECT SCOPE_IDENTITY()";
     DataAccess.GetInstance().ExecuteScalar(sql, CommandType.Text,
         new SqlParameter("@instrumentId", entity.Id),
         new SqlParameter("@underlyingInstrument1Id", entity.UnderlyingInstrument1Id),
         new SqlParameter("@underlyingInstrument1IdInverted", entity.UnderlyingInstrument1IdInverted),
         new SqlParameter("@underlyingInstrument2Id", entity.UnderlyingInstrument2Id.HasValue ? (object)entity.UnderlyingInstrument2Id : DBNull.Value),
         new SqlParameter("@askOperand1Type", entity.AskOperand1Type),
         new SqlParameter("@askOperator1Type", entity.AskOperator1Type.HasValue ? (object)entity.AskOperator1Type : DBNull.Value),
         new SqlParameter("@askOperand2Type", entity.AskOperand2Type.HasValue ? (object)entity.AskOperand2Type : DBNull.Value),
         new SqlParameter("@askOperator2Type", entity.AskOperator2Type),
         new SqlParameter("@askOperand3", entity.AskOperand3),
         new SqlParameter("@bidOperand1Type", entity.BidOperand1Type),
         new SqlParameter("@bidOperator1Type", entity.BidOperator1Type.HasValue ? (object)entity.BidOperator1Type : DBNull.Value),
         new SqlParameter("@bidOperand2Type", entity.BidOperand2Type.HasValue ? (object)entity.BidOperand2Type: DBNull.Value),
         new SqlParameter("@bidOperator2Type", entity.BidOperator2Type),
         new SqlParameter("@bidOperand3", entity.BidOperand3),
         new SqlParameter("@lastOperand1Type", entity.LastOperand1Type),
         new SqlParameter("@lastOperator1Type", entity.LastOperator1Type.HasValue ? (object)entity.LastOperator1Type : DBNull.Value),
         new SqlParameter("@lastOperand2Type", entity.LastOperand2Type.HasValue ? (object)entity.LastOperand2Type : DBNull.Value),
         new SqlParameter("@lastOperator2Type", entity.LastOperator2Type),
         new SqlParameter("@lastOperand3", entity.LastOperand3));
 }
 public void Add(DerivativeRelation derivativeRelation)
 {
     this._Instruments.Single(i => i.Id == derivativeRelation.Id).VmDerivativeRelation = new VmDerivativeRelation(derivativeRelation);
 }
 public void Add(DerivativeRelation derivativeRelation)
 {
     this._Instruments.Single(i => i.Id == derivativeRelation.Id).VmDerivativeRelation = new VmDerivativeRelation(derivativeRelation);
 }
 public VmDerivativeRelation(DerivativeRelation derivativeRelation)
     : base(derivativeRelation)
 {
     this._DerivativeRelation = derivativeRelation;
 }
        internal static void GetQuotationMetadata(
            Dictionary<string, QuotationSource> quotationSources,
            Dictionary<int, Instrument> instruments,
            //Dictionary<int, Dictionary<int, InstrumentSourceRelation>> instrumentSourceRelations,
            Dictionary<int, Dictionary<string, InstrumentSourceRelation>> instrumentSourceRelations,
            Dictionary<int, DerivativeRelation> derivativeRelations,
            Dictionary<int, PriceRangeCheckRule> priceRangeCheckRules,
            Dictionary<int, WeightedPriceRule> weightedPriceRules,
            Dictionary<int, GeneralQuotation> lastQuotations
            )
        {
            string sql = "dbo.GetInitialDataForQuotationManager";
            DataAccess.GetInstance().ExecuteReader(sql, CommandType.StoredProcedure, delegate(SqlDataReader reader)
            {
                while (reader.Read())
                {
                    QuotationSource quotationSource = new QuotationSource();
                    quotationSource.Id = (int)reader["Id"];
                    quotationSource.Name = (string)reader["Name"];
                    quotationSource.AuthName = (string)reader["AuthName"];
                    quotationSource.Password = (string)reader["Password"];
                    quotationSources.Add(quotationSource.Name, quotationSource);
                }
                reader.NextResult();
                while (reader.Read())
                {
                    Instrument instrument = new Instrument();
                    instrument.Id = (int)reader["Id"];
                    instrument.Code = (string)reader["Code"];
                    instrument.AdjustPoints = (int)reader["AdjustPoints"];
                    instrument.AdjustIncrement = (int)reader["AdjustIncrement"];
                    instrument.DecimalPlace = (int)reader["DecimalPlace"];
                    instrument.IsDerivative = (bool)reader["IsDerivative"];
                    instrument.InactiveTime = reader["InactiveTime"] == DBNull.Value ? null : (int?)reader["InactiveTime"];
                    instrument.UseWeightedPrice = reader["UseWeightedPrice"] == DBNull.Value ? null : (bool?)reader["UseWeightedPrice"];
                    instrument.IsSwitchUseAgio = reader["IsSwitchUseAgio"] == DBNull.Value ? null : (bool?)reader["IsSwitchUseAgio"];
                    instrument.AgioSeconds = reader["AgioSeconds"] == DBNull.Value ? null : (int?)reader["AgioSeconds"];
                    instrument.LeastTicks = reader["LeastTicks"] == DBNull.Value ? null : (int?)reader["LeastTicks"];
                    instrument.IsActive = true;
                    instruments.Add(instrument.Id, instrument);
                }
                reader.NextResult();
                while (reader.Read())
                {
                    InstrumentSourceRelation relation = new InstrumentSourceRelation();
                    relation.Id = (int)reader["Id"];
                    relation.SourceId = (int)reader["SourceId"];
                    relation.SourceSymbol = (string)reader["SourceSymbol"];
                    relation.InstrumentId = (int)reader["InstrumentId"];
                    relation.Inverted = (bool)reader["Inverted"];
                    relation.IsActive = (bool)reader["IsActive"];
                    relation.IsDefault = (bool)reader["IsDefault"];
                    relation.Priority = (int)reader["Priority"];
                    relation.SwitchTimeout = (int)reader["SwitchTimeout"];
                    relation.AdjustPoints = (int)reader["AdjustPoints"];
                    relation.AdjustIncrement = (int)reader["AdjustIncrement"];
                    //Dictionary<int, InstrumentSourceRelation> sources;
                    //if (!instrumentSourceRelations.TryGetValue(relation.InstrumentId, out sources))
                    //{
                    //    sources = new Dictionary<int, InstrumentSourceRelation>();
                    //    instrumentSourceRelations.Add(relation.InstrumentId, sources);
                    //}
                    //sources.Add(relation.SourceId, relation);

                    Dictionary<string, InstrumentSourceRelation> relations;
                    if (!instrumentSourceRelations.TryGetValue(relation.SourceId, out relations))
                    {
                        relations = new Dictionary<string, InstrumentSourceRelation>();
                        instrumentSourceRelations.Add(relation.SourceId, relations);
                    }
                    relations.Add(relation.SourceSymbol, relation);
                }
                reader.NextResult();
                while (reader.Read())
                {
                    DerivativeRelation derivativeRelation = new DerivativeRelation();
                    derivativeRelation.Id = (int)reader["InstrumentId"];
                    derivativeRelation.UnderlyingInstrument1Id = (int)reader["UnderlyingInstrument1Id"];
                    derivativeRelation.UnderlyingInstrument1IdInverted = (bool)reader["UnderlyingInstrument1IdInverted"];
                    derivativeRelation.UnderlyingInstrument2Id = reader["UnderlyingInstrument2Id"] == DBNull.Value ? null : (int?)reader["UnderlyingInstrument2Id"];
                    derivativeRelation.AskOperand1Type = (OperandType)(byte)reader["AskOperand1Type"];
                    derivativeRelation.AskOperator1Type = reader["AskOperator1Type"] == DBNull.Value ? null : (OperatorType?)(byte)reader["AskOperator1Type"];
                    derivativeRelation.AskOperand2Type = reader["AskOperand2Type"] == DBNull.Value ? null : (OperandType?)(byte)reader["AskOperand2Type"];
                    derivativeRelation.AskOperator2Type = (OperatorType)(byte)reader["AskOperator2Type"];
                    derivativeRelation.AskOperand3 = (decimal)reader["AskOperand3"];
                    derivativeRelation.BidOperand1Type = (OperandType)(byte)reader["BidOperand1Type"];
                    derivativeRelation.BidOperator1Type = reader["BidOperator1Type"] == DBNull.Value ? null : (OperatorType?)(byte)reader["BidOperator1Type"];
                    derivativeRelation.BidOperand2Type = reader["BidOperand2Type"] == DBNull.Value ? null : (OperandType?)(byte)reader["BidOperand2Type"];
                    derivativeRelation.BidOperator2Type = (OperatorType)(byte)reader["BidOperator2Type"];
                    derivativeRelation.BidOperand3 = (decimal)reader["BidOperand3"];
                    derivativeRelation.LastOperand1Type = (OperandType)(byte)reader["LastOperand1Type"];
                    derivativeRelation.LastOperator1Type = reader["LastOperator1Type"] == DBNull.Value ? null : (OperatorType?)(byte)reader["LastOperator1Type"];
                    derivativeRelation.LastOperand2Type = reader["LastOperand2Type"] == DBNull.Value ? null : (OperandType?)(byte)reader["LastOperand2Type"];
                    derivativeRelation.LastOperator2Type = (OperatorType)(byte)reader["LastOperator2Type"];
                    derivativeRelation.LastOperand3 = (decimal)reader["LastOperand3"];
                    derivativeRelations.Add(derivativeRelation.Id, derivativeRelation);
                }
                reader.NextResult();
                while (reader.Read())
                {
                    PriceRangeCheckRule priceRangeCheckRule = new PriceRangeCheckRule();
                    priceRangeCheckRule.Id = (int)reader["InstrumentId"];
                    priceRangeCheckRule.DiscardOutOfRangePrice = (bool)reader["DiscardOutOfRangePrice"];
                    priceRangeCheckRule.OutOfRangeType = (OutOfRangeType)(byte)reader["OutOfRangeType"];
                    priceRangeCheckRule.ValidVariation = (int)reader["ValidVariation"];
                    priceRangeCheckRule.OutOfRangeWaitTime = (int)reader["OutOfRangeWaitTime"];
                    priceRangeCheckRule.OutOfRangeCount = (int)reader["OutOfRangeCount"];
                    priceRangeCheckRules.Add(priceRangeCheckRule.Id, priceRangeCheckRule);
                }
                reader.NextResult();
                while(reader.Read())
                {
                    WeightedPriceRule weightedPriceRule = new WeightedPriceRule();
                    weightedPriceRule.Id = (int)reader["InstrumentId"];
                    weightedPriceRule.Multiplier = (decimal)reader["Multiplier"];
                    weightedPriceRule.AskAskWeight = (int)reader["AskAskWeight"];
                    weightedPriceRule.AskBidWeight = (int)reader["AskBidWeight"];
                    weightedPriceRule.AskLastWeight = (int)reader["AskLastWeight"];
                    weightedPriceRule.BidAskWeight = (int)reader["BidAskWeight"];
                    weightedPriceRule.BidBidWeight = (int)reader["BidBidWeight"];
                    weightedPriceRule.BidLastWeight = (int)reader["BidLastWeight"];
                    weightedPriceRule.LastAskWeight = (int)reader["LastAskWeight"];
                    weightedPriceRule.LastBidWeight = (int)reader["LastBidWeight"];
                    weightedPriceRule.LastLastWeight = (int)reader["LastLastWeight"];
                    weightedPriceRule.AskAverageWeight = (int)reader["AskAverageWeight"];
                    weightedPriceRule.BidAverageWeight = (int)reader["BidAverageWeight"];
                    weightedPriceRule.LastAverageWeight = (int)reader["LastAverageWeight"];
                    weightedPriceRule.AskAdjust = (decimal)reader["AskAdjust"];
                    weightedPriceRule.BidAdjust = (decimal)reader["BidAdjust"];
                    weightedPriceRule.LastAdjust = (decimal)reader["LastAdjust"];
                    weightedPriceRules.Add(weightedPriceRule.Id, weightedPriceRule);
                }
                reader.NextResult();
                while (reader.Read())
                {
                    GeneralQuotation lastQuotation = new GeneralQuotation();
                    lastQuotation.SourceId = (int)reader["SourceId"];
                    lastQuotation.InstrumentId = (int)reader["InstrumentId"];
                    lastQuotation.Timestamp = (DateTime)reader["Timestamp"];
                    lastQuotation.Ask = (double)reader["Ask"];
                    lastQuotation.Bid = (double)reader["Bid"];
                    lastQuotation.Last = reader["Last"] == DBNull.Value ? null : (double?)reader["Last"];
                    lastQuotation.High = reader["High"] == DBNull.Value ? null : (double?)reader["High"];
                    lastQuotation.Low = reader["Low"] == DBNull.Value ? null : (double?)reader["Low"];
                    lastQuotations.Add(lastQuotation.InstrumentId, lastQuotation);
                }
            });
        }
 public void AddMetadataObject(DerivativeRelation derivativeRelation)
 {
     this._ConfigMetadata.DerivativeRelations.Add(derivativeRelation.Id, derivativeRelation);
 }
        private GeneralQuotation Derive(GeneralQuotation quotation, DerivativeRelation relation)
        {
            GeneralQuotation quotation1, quotation2 = null;
            if (quotation.InstrumentId == relation.UnderlyingInstrument1Id)
            {
                quotation1 = quotation;
                if (relation.UnderlyingInstrument2Id.HasValue)
                {
                    if (!this._LastQuotationManager.LastAccepted.TryGetLastQuotation(relation.UnderlyingInstrument2Id.Value, out quotation2))
                    {
                        Logger.AddEvent(TraceEventType.Warning, "DerivativeController.Derive can not get Last quotation by instrumentId:{0}", relation.UnderlyingInstrument2Id.Value);
                        return null;
                    }
                }
            }
            else
            {
                quotation2 = quotation;
                if (!this._LastQuotationManager.LastAccepted.TryGetLastQuotation(relation.UnderlyingInstrument1Id, out quotation1))
                {
                    Logger.AddEvent(TraceEventType.Warning, "DerivativeController.Derive can not get Last quotation by instrumentId:{0}", relation.UnderlyingInstrument1Id);
                    return null;
                }
            }

            GeneralQuotation GeneralQuotation = new GeneralQuotation { InstrumentId = relation.Id, SourceId = quotation.SourceId, Timestamp = quotation.Timestamp };
            double price;
            if (this.TryGetPrice(relation.AskOperand1Type, relation.AskOperator1Type,
                relation.AskOperand2Type, relation.AskOperator2Type,
                (double)relation.AskOperand3,
                relation, GeneralQuotation, quotation1, quotation2, out price))
            {
                GeneralQuotation.Ask = price;
            }
            else
            {
                return null;
            }

            if (this.TryGetPrice(relation.BidOperand1Type, relation.BidOperator1Type,
                relation.BidOperand2Type, relation.BidOperator2Type,
                (double)relation.BidOperand3,
                relation, GeneralQuotation, quotation1, quotation2, out price))
            {
                GeneralQuotation.Bid = price;
            }
            else
            {
                return null;
            }

            if (this.TryGetPrice(relation.LastOperand1Type, relation.LastOperator1Type,
                relation.LastOperand2Type, relation.LastOperator2Type,
                (double)relation.LastOperand3,
                relation, GeneralQuotation, quotation1, quotation2, out price))
            {
                GeneralQuotation.Last = price;
            }
            else
            {
                GeneralQuotation.Last = null;
            }

            return GeneralQuotation;
        }
        private bool TryGetPrice(OperandType operand1Type, OperatorType? operator1Type, OperandType? operand2Type, OperatorType operator2Type, double operand3,
            DerivativeRelation relation, GeneralQuotation GeneralQuotation, GeneralQuotation quotation1, GeneralQuotation quotation2, out double price)
        {
            price = 0;
            // operand1
            switch (operand1Type)
            {
                case OperandType.Ask:
                    price = relation.UnderlyingInstrument1IdInverted ? 1 / quotation1.Ask : quotation1.Ask;
                    break;
                case OperandType.Bid:
                    price = relation.UnderlyingInstrument1IdInverted ? 1 / quotation1.Bid : quotation1.Bid;
                    break;
                case OperandType.Last:
                    if (quotation1.Last.HasValue)
                    {
                        price = relation.UnderlyingInstrument1IdInverted ? 1 / quotation1.Last.Value : quotation1.Last.Value;
                    }
                    else
                    {
                        Logger.AddEvent(TraceEventType.Warning, "DerivativeController.Derive operand1 quotation1.Last==null quotation1.instrumentId:{0}", quotation1.InstrumentId);
                        return false;
                    }
                    break;
                default:
                    Logger.AddEvent(TraceEventType.Warning, "DerivativeController.Derive operand1 operand1Type is Invalid, relation.instrumentId:{0}, operand1Type:{1}",
                        relation.Id, operand1Type);
                    return false;
            }

            if (quotation2 != null && operator1Type.HasValue)
            {
                // operand2
                double operand2;
                switch (operand2Type.Value)
                {
                    case OperandType.Ask:
                        operand2 = quotation2.Ask;
                        break;
                    case OperandType.Bid:
                        operand2 = quotation2.Bid;
                        break;
                    case OperandType.Last:
                        if (quotation1.Last.HasValue)
                        {
                            operand2 = quotation2.Last.Value;
                        }
                        else
                        {
                            Logger.AddEvent(TraceEventType.Warning, "DerivativeController.Derive operand2 quotation1.Last==null, quotation2.instrumentId:{0}", quotation2.InstrumentId);
                            return false;
                        }
                        break;
                    default:
                        Logger.AddEvent(TraceEventType.Warning, "DerivativeController.Derive operand2Type is Invalid, relation.instrumentId:{0}, operand2Type:{1}",
                            relation.Id, operand2Type);
                        return false;
                }
                if (operator1Type.Value == OperatorType.Multiply)
                {
                    price *= operand2;
                }
                else
                {
                    price /= operand2;
                }
            }

            // operand3
            if (operator2Type == OperatorType.Multiply)
            {
                price *= operand3;
            }
            else
            {
                price /= operand3;
            }
            return true;
        }