public SourceQuotation(PrimitiveQuotation primitiveQuotation, double ask, double bid, string instrumentCode, bool inverted)
        {
            this._PrimitiveQuotation = primitiveQuotation;
            this._Quotation = new GeneralQuotation();
            this._Quotation.InstrumentId = primitiveQuotation.InstrumentId;
            this._Quotation.SourceId = primitiveQuotation.SourceId;
            this._Quotation.OriginCode = instrumentCode;
            this._Quotation.Timestamp = primitiveQuotation.Timestamp;

            this._Quotation.Ask = ask;
            this._Quotation.Bid = bid;

            double price;
            if (PrimitiveQuotation.TryGetPriceValue(primitiveQuotation.Last, out price)) this._Quotation.Last = price;
            if (PrimitiveQuotation.TryGetPriceValue(primitiveQuotation.High, out price)) this._Quotation.High = price;
            if (PrimitiveQuotation.TryGetPriceValue(primitiveQuotation.Low, out price)) this._Quotation.Low = price;

            if (inverted)
            {
                this._Quotation.Ask = 1 / this._Quotation.Ask;
                this._Quotation.Bid = 1 / this._Quotation.Bid;
                if (this._Quotation.Last.HasValue) this._Quotation.Last = 1 / this._Quotation.Last;
                if (this._Quotation.High.HasValue) this._Quotation.High = 1 / this._Quotation.High;
                if (this._Quotation.Low.HasValue) this._Quotation.Low = 1 / this._Quotation.Low;
            }
        }
 public void Derive(GeneralQuotation quotation, List<GeneralQuotation> quotations)
 {
     this._DeriveRecursiveLevel++;
     if (this._DeriveRecursiveLevel > 50)
     {
         this._DeriveRecursiveLevel = 0;
         throw new Exception(string.Format("DerivativeController.Derive too deep SourceId:{0},InstrumentId:{1},OriginCode:{2}", quotation.SourceId, quotation.InstrumentId, quotation.OriginCode));
     }
     try
     {
         var derivativeRelations = this._DerivativeRelations.Values.Where(r => r.UnderlyingInstrument1Id == quotation.InstrumentId || r.UnderlyingInstrument2Id == quotation.InstrumentId);
         foreach (var relation in derivativeRelations)
         {
             GeneralQuotation GeneralQuotation = this.Derive(quotation, relation);
             if (GeneralQuotation != null)
             {
                 quotations.Add(GeneralQuotation);
                 this.Derive(GeneralQuotation, quotations);
             }
         }
         this._DeriveRecursiveLevel--;
     }
     catch
     {
         this._DeriveRecursiveLevel = 0;
         throw;
     }
 }
 public CollectorQuotation(GeneralQuotation quotation)
 {
     this.originCode = quotation.OriginCode;
     this.timestamp = quotation.Timestamp == (new DateTime()) ? DateTime.Now : quotation.Timestamp;
     this.ask = quotation.Ask.ToString();
     this.bid = quotation.Bid.ToString();
     this.high = quotation.High.ToString();
     this.low = quotation.Low.ToString();
     this.volume = quotation.Volume;
     this.totalVolume = quotation.TotalVolume;
 }
 public bool TryGetLastQuotation(int instrumentId, out GeneralQuotation quotation)
 {
     this._LastQuotationLock.EnterReadLock();
     try
     {
         quotation = null;
         if (this._LastAcceptedQuotations.ContainsKey(instrumentId))
         {
             quotation = this._LastAcceptedQuotations[instrumentId];
         }
         return quotation != null;
     }
     finally
     {
         this._LastQuotationLock.ExitReadLock();
     }
 }
 public void TestMethod1()
 {
     ExchangeSystemSetting setting = new ExchangeSystemSetting();
     XmlSerializer serializer = new XmlSerializer(typeof(ManagerSettings));
     ManagerSettings settings = (ManagerSettings)serializer.Deserialize(new FileStream(@"D:\Teams\iExchangeCollection\iExchange3 Team\Manager\Manager.UnitTest\bin\Debug\Configuration\Manager.config", FileMode.Open));
     setting = settings.ExchangeSystems[0];
     QuotationServer quotation = new QuotationServer(setting);
     List<GeneralQuotation> quotations = new List<GeneralQuotation>();
     GeneralQuotation gene = new GeneralQuotation();
     gene.Ask = 10;
     gene.Bid = 10;
     gene.High = 20;
     gene.Low = 8;
     gene.Volume = "12";
     gene.TotalVolume = "13";
     gene.OriginCode = "XAUUSD";
     quotations.Add(gene);
     Token token = new Token(Guid.Empty, UserType.System, AppType.RiskMonitor);
     iExchange.Common.OriginQuotation[] originQs = null;
     iExchange.Common.OverridedQuotation[] overridedQs = null;
     quotation.SetQuotation(token, quotations, out originQs, out overridedQs);
 }
 internal static void SaveLastQuotation(GeneralQuotation quotation)
 {
     DataAccess.GetInstance().ExecuteNonQuery("dbo.LastQuotation_Set", CommandType.StoredProcedure,
         new SqlParameter("@instrumentId", quotation.InstrumentId),
         new SqlParameter("@sourceId", quotation.SourceId),
         new SqlParameter("@timestamp", quotation.Timestamp),
         new SqlParameter("@ask", quotation.Ask),
         new SqlParameter("@bid", quotation.Bid),
         new SqlParameter("@last", quotation.Last.HasValue ? (object)quotation.Last.Value : DBNull.Value),
         new SqlParameter("@high", quotation.High.HasValue ? (object)quotation.High.Value : DBNull.Value),
         new SqlParameter("@low", quotation.Low.HasValue ? (object)quotation.Low.Value : DBNull.Value));
 }
        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 SetQuotationTest()
 {
     ExchangeSystemSetting setting = new ExchangeSystemSetting();
     XmlSerializer serializer = new XmlSerializer(typeof(ManagerSettings));
     ManagerSettings settings = (ManagerSettings)serializer.Deserialize(new FileStream(@"D:\Teams\iExchangeCollection\iExchange3 Team\Manager\Manager.UnitTest\bin\Debug\Configuration\Manager.config", FileMode.Open));
     setting = settings.ExchangeSystems[0]; // 初始化为适当的值
     QuotationServer target = new QuotationServer(setting); // 初始化为适当的值
     Token token = new Token(Guid.Empty, UserType.System, AppType.QuotationCollector); // 初始化为适当的值
     List<GeneralQuotation> quotations = new List<GeneralQuotation>(); // 初始化为适当的值
     GeneralQuotation gene = new GeneralQuotation();
     gene.Ask = 10;
     gene.Bid = 10;
     gene.High = 20;
     gene.Low = 8;
     gene.Volume = "12";
     gene.TotalVolume = "13";
     gene.OriginCode = "XAUUSD";
     quotations.Add(gene);
     iExchange.Common.OriginQuotation[] originQs = null; // 初始化为适当的值
     //iExchange.Common.OriginQuotation[] originQsExpected = null; // 初始化为适当的值
     iExchange.Common.OverridedQuotation[] overridedQs = null; // 初始化为适当的值
     //iExchange.Common.OverridedQuotation[] overridedQsExpected = null; // 初始化为适当的值
     bool expected = true; // 初始化为适当的值
     bool actual;
     actual = target.SetQuotation(token, quotations, out originQs, out overridedQs);
     //Assert.AreEqual(originQsExpected, originQs);
     //Assert.AreEqual(overridedQsExpected, overridedQs);
     Assert.AreEqual(expected, actual);
 }
 public static CollectorQuotation CreateInstance(GeneralQuotation quotation)
 {
     CollectorQuotation cq = null;
     try
     {
         cq = new CollectorQuotation(quotation);
     }
     catch (Exception e)
     {
         AppDebug.LogEvent("CollectorQuotation", string.Format("CreateInstance'{0}',{1}", quotation, e.ToString()), EventLogEntryType.Error);
     }
     return cq;
 }
 public static DealerQuotation CreateInstance(GeneralQuotation quotation)
 {
     DealerQuotation dq = null;
     try
     {
         dq = new DealerQuotation(quotation);
     }
     catch (Exception e)
     {
         AppDebug.LogEvent("DealerQuotation", e.ToString(), EventLogEntryType.Error);
     }
     return dq;
 }
 public DealerQuotation(GeneralQuotation quotation)
 {
     this.instrumentID = new Guid();
     this.timestamp = quotation.Timestamp;
     this.originString = quotation.OriginCode;
     this.highString = quotation.High.ToString();
     this.lowString = quotation.Low.ToString();
     this.volume = quotation.Volume;
     this.totalVolume = quotation.TotalVolume;
     this.origin = null;
     this.originHigh = null;
     this.originLow = null;
 }
        public void SetQuotation(GeneralQuotation generalQuotation, int decimalPlace)
        {
            string format = "F" + decimalPlace.ToString();
            this.Timestamp = generalQuotation.Timestamp;
            this.Bid = generalQuotation.Bid.ToString(format);
            this.Ask = generalQuotation.Ask.ToString(format);
            if (generalQuotation.Last.HasValue) this.Last = generalQuotation.Last.Value.ToString(format);

            this.AskTrend = this.GetPriceTrend(generalQuotation.Ask, this._RawAsk);
            this.BidTrend = this.GetPriceTrend(generalQuotation.Bid, this._RawBid);

            this._RawAsk = generalQuotation.Ask;
            this._RawBid = generalQuotation.Bid;
            this._RawLast = generalQuotation.Last;
        }
        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;
        }