public static void MergeData(this L1QuoteStreaming q1, L1QuoteStreaming q2)
        {
            if (q1.Symbol != q2.Symbol)
            {
                throw new InvalidCastException(string.Format("Can't merge data: {0} because of different symbols", q1.Symbol));
            }

            var properties = typeof(L1QuoteStreaming).GetProperties();

            foreach (var pi in properties)
            {
                System.Type     propertyType = pi.PropertyType;
                System.TypeCode typeCode     = Type.GetTypeCode(propertyType);

                var type1 = q1.GetType();
                var pi1   = type1.GetProperty(pi.Name);
                var type2 = q2.GetType();
                var pi2   = type2.GetProperty(pi.Name);
                var v1    = pi2.GetValue(q2, null); // Get value of q2's property

                if (typeCode == TypeCode.Int32 ||
                    typeCode == TypeCode.Int64 ||
                    typeCode == TypeCode.Single ||
                    typeCode == TypeCode.Double)
                {
                    if (Convert.ToDouble(v1) != 0.0)
                    {
                        pi1.SetValue(pi1, Convert.ChangeType(v1, typeCode));  // Set the property value
                    }
                }
                else
                {
                    pi1.SetValue(pi1, Convert.ChangeType(v1, typeCode));  // Set the property value
                }
            }
        }
Esempio n. 2
0
        public static L1QuoteStreaming AddOrUpdate(byte[] b)
        {
            bool             exist = false;
            L1QuoteStreaming obj   = null;

            int byteLength = b.Length;
            int tradeTime  = 0;
            int tradeDate  = 0;
            int quoteTime  = 0;
            int quoteDate  = 0;
            int i          = 0;

            string headerId     = b.ConvertToByte(ref i);
            int    msgLength    = b.ConvertToShort(ref i);
            int    sid          = b.ConvertToShort(ref i);
            int    colNum       = b.ConvertToColumnNumber(ref i);
            int    symbolLength = b.ConvertToShort(ref i);
            string symbol       = b.ConvertToString(ref i, symbolLength);

            if (l1quotes.ContainsKey(symbol))
            {
                obj = l1quotes[symbol];

                obj.OldHigh = obj.High;
                obj.OldLow  = obj.Low;

                obj.IsDayHigh  = false;
                obj.IsDayLow   = false;
                obj.IsYearHigh = false;
                obj.IsYearLow  = false;
                obj.IsTraded   = false;

                exist = true;
            }
            else
            {
                obj = new L1QuoteStreaming()
                {
                    Symbol = symbol
                };
            }

            int col = 0;

            while (i < byteLength)
            {
                col = b.ConvertToColumnNumber(ref i);

                switch (col)
                {
                case 1:
                    float newBid = b.ConvertToFloat(ref i);
                    obj.Bid = (newBid > 0) ? newBid : 0;
                    break;

                case 2:
                    float newAsk = b.ConvertToFloat(ref i);
                    obj.Ask = (newAsk > 0) ? newAsk : 0;
                    break;

                case 3:
                    float newLast = b.ConvertToFloat(ref i);
                    obj.Last = (newLast > 0) ? newLast : 0;
                    break;

                case 4:
                    int newBidSize = b.ConvertToInt32(ref i);
                    obj.BidSize = (newBidSize > 0) ? newBidSize * 100 : 0;
                    break;

                case 5:
                    int newAskSize = b.ConvertToInt32(ref i);
                    obj.AskSize = (newAskSize > 0) ? newAskSize * 100 : 0;
                    break;

                case 6:
                    int newBidId = b.ConvertToShort(ref i);
                    obj.BIDID = (newBidId > 0) ? newBidId : 0;
                    break;

                case 7:
                    int newAskId = b.ConvertToShort(ref i);
                    obj.ASKID = (newAskId > 0) ? newAskId : 0;
                    break;

                case 8:
                    long newVolume = b.ConvertToLong(ref i);
                    long oldVolume = obj.Volume;
                    obj.LastTradeSize = (oldVolume > 0) ? Convert.ToInt32(newVolume - oldVolume) : 0;
                    obj.IsTraded      = (oldVolume > 0 && obj.LastTradeSize > 0);
                    obj.Volume        = (newVolume > 0) ? newVolume : 0;

                    // if there is no previous volume, assusme this is first quote.
                    obj.IsFirstQuote = (oldVolume == 0);

                    break;

                case 9:
                    int newLastSize = b.ConvertToInt32(ref i);
                    obj.LastSize = (newLastSize > 0) ? newLastSize : 0;
                    break;

                case 10:
                    tradeTime = b.ConvertToInt32(ref i);     // Seconds from midnight
                    break;

                case 11:
                    quoteTime = b.ConvertToInt32(ref i);
                    break;

                case 12:
                    float high = b.ConvertToFloat(ref i);

                    if (obj.High == 0)
                    {
                        obj.High = high;
                    }

                    //else if (high > obj.High)
                    //{
                    //    obj.High = high;
                    //    if (high > obj.YearHigh)
                    //    {
                    //        obj.YearHigh = high;
                    //    }
                    //}
                    break;

                case 13:
                    float low = b.ConvertToFloat(ref i);

                    if (obj.Low == 0)
                    {
                        obj.Low = low;
                    }

                    //else if (low < obj.Low)
                    //{
                    //    obj.Low = low;
                    //    if (low < obj.YearLow)
                    //    {
                    //        obj.YearLow = low;
                    //    }
                    //}
                    break;

                case 14:
                    obj.Tick = b.ConvertToByte(ref i);
                    break;

                case 15:
                    obj.Close = b.ConvertToFloat(ref i);
                    break;

                case 16:
                    obj.Exchange = b.ConvertToChar(ref i);
                    break;

                case 17:
                    obj.Marginable = b.ConvertToBoolean(ref i);
                    break;

                case 18:
                    obj.Shortable = b.ConvertToBoolean(ref i);
                    break;

                case 22:
                    quoteDate = b.ConvertToInt32(ref i);     // Days from midnight 1970,1,1
                    break;

                case 23:
                    tradeDate = b.ConvertToInt32(ref i);     // Days from midnight 1970,1,1
                    break;

                case 24:
                    obj.Volatility = b.ConvertToFloat(ref i);
                    break;

                case 25:
                    int descLength = b.ConvertToShort(ref i);
                    obj.Description = b.ConvertToString(ref i, descLength).Trim();
                    break;

                case 28:
                    obj.Open = b.ConvertToFloat(ref i);
                    break;

                case 29:
                    obj.Change = b.ConvertToFloat(ref i);
                    break;

                case 30:
                    obj.YearHigh = b.ConvertToFloat(ref i);
                    break;

                case 31:
                    obj.YearLow = b.ConvertToFloat(ref i);
                    break;

                case 32:
                    obj.PERatio = b.ConvertToFloat(ref i);
                    break;

                case 33:
                    obj.DividendAmt = b.ConvertToFloat(ref i);
                    break;

                case 34:
                    obj.DividentYield = b.ConvertToFloat(ref i);
                    break;

                case 37:
                    obj.NAV = b.ConvertToFloat(ref i);
                    break;

                case 38:
                    obj.Fund = b.ConvertToFloat(ref i);
                    break;

                case 39:
                    int exchLength = b.ConvertToShort(ref i);
                    obj.ExchangeName = b.ConvertToString(ref i, exchLength).Trim();
                    break;

                case 40:
                    int dividendDateLength = b.ConvertToShort(ref i);
                    obj.DividendDate = b.ConvertToString(ref i, dividendDateLength);
                    break;

                default:
                    throw new FormatException(string.Format("{0} - L1QuoteStreaming streaming parsing failed. (content: {1})", obj.Symbol, BitConverter.ToString(b)));
                    break;
                }

                if (tradeTime > 0)
                {
                    obj.TradeTime = (tradeDate > 0) ? fromDate.AddDays(tradeDate).AddSeconds(tradeTime) : obj.TradeTime.Date.AddSeconds(tradeTime);
                }

                if (quoteTime > 0)
                {
                    obj.QuoteTime = (quoteDate > 0) ? fromDate.AddDays(quoteDate).AddSeconds(quoteTime) : obj.TradeTime.Date.AddSeconds(quoteTime);
                }
            }

            if (obj.IsTraded)
            {
                obj.SetLastColorCode();
            }

            obj.IsHigherBid = obj.Bid > obj.Last;
            obj.IsLowerAsk  = obj.Ask < obj.Last;

            /*
             * Update Data
             */
            obj.ChangePercent = (obj.Close > 0) ? (obj.Last - obj.Close) / obj.Close : 0;

            #region Update the l1streaming object

            //l1quotes.AddOrUpdate(obj.Symbol, obj, (k, v) => obj);

            if (exist)
            {
                l1quotes[obj.Symbol] = obj;
            }
            else
            {
                l1quotes.Add(obj.Symbol, obj);
            }

            #endregion

            return(obj);
        }