Ejemplo n.º 1
0
        protected override void OnSave(BitArrayWriter writer, IEnumerable <ExecutionMessage> messages, TickMetaInfo metaInfo)
        {
            if (metaInfo.IsEmpty())
            {
                var first = messages.First();

                metaInfo.FirstId      = metaInfo.PrevId = first.TradeId ?? 0;
                metaInfo.ServerOffset = first.ServerTime.Offset;
            }

            writer.WriteInt(messages.Count());

            var allowNonOrdered  = metaInfo.Version >= MarketDataVersions.Version48;
            var isUtc            = metaInfo.Version >= MarketDataVersions.Version50;
            var allowDiffOffsets = metaInfo.Version >= MarketDataVersions.Version54;
            var isTickPrecision  = metaInfo.Version >= MarketDataVersions.Version55;
            var useLong          = metaInfo.Version >= MarketDataVersions.Version56;

            foreach (var msg in messages)
            {
                if (msg.ExecutionType != ExecutionTypes.Tick)
                {
                    throw new ArgumentOutOfRangeException(nameof(messages), msg.ExecutionType, LocalizedStrings.Str1695Params.Put(msg));
                }

                var tradeId = msg.TradeId ?? 0;

                // сделки для индексов имеют нулевой номер
                if (tradeId < 0)
                {
                    throw new ArgumentOutOfRangeException(nameof(messages), tradeId, LocalizedStrings.Str1020);
                }

                // execution ticks (like option execution) may be a zero cost
                // ticks for spreads may be a zero cost or less than zero
                //if (msg.TradePrice < 0)
                //	throw new ArgumentOutOfRangeException(nameof(messages), msg.TradePrice, LocalizedStrings.Str1021Params.Put(msg.TradeId));

                metaInfo.PrevId = writer.SerializeId(tradeId, metaInfo.PrevId);

                // pyhta4og.
                // http://stocksharp.com/forum/yaf_postsm6450_Oshibka-pri-importie-instrumientov-s-Finama.aspx#post6450

                var volume = msg.TradeVolume;

                if (metaInfo.Version < MarketDataVersions.Version53)
                {
                    if (volume == null)
                    {
                        throw new ArgumentException(LocalizedStrings.Str1022Params.Put((object)msg.TradeId ?? msg.TradeStringId), nameof(messages));
                    }

                    if (volume < 0)
                    {
                        throw new ArgumentOutOfRangeException(nameof(messages), volume, LocalizedStrings.Str1022Params.Put(msg.TradeId));
                    }

                    writer.WriteVolume(volume.Value, metaInfo, SecurityId);
                }
                else
                {
                    writer.Write(volume != null);

                    if (volume != null)
                    {
                        if (volume < 0)
                        {
                            throw new ArgumentOutOfRangeException(nameof(messages), volume, LocalizedStrings.Str1022Params.Put(msg.TradeId));
                        }

                        writer.WriteVolume(volume.Value, metaInfo, SecurityId);
                    }
                }

                writer.WritePriceEx(msg.GetTradePrice(), metaInfo, SecurityId, useLong);
                writer.WriteSide(msg.OriginSide);

                var lastOffset = metaInfo.LastServerOffset;
                metaInfo.LastTime         = writer.WriteTime(msg.ServerTime, metaInfo.LastTime, LocalizedStrings.Str985, allowNonOrdered, isUtc, metaInfo.ServerOffset, allowDiffOffsets, isTickPrecision, ref lastOffset);
                metaInfo.LastServerOffset = lastOffset;

                if (metaInfo.Version < MarketDataVersions.Version40)
                {
                    continue;
                }

                if (metaInfo.Version < MarketDataVersions.Version47)
                {
                    writer.WriteLong((msg.LocalTime - msg.ServerTime).Ticks);
                }
                else
                {
                    var hasLocalTime = true;

                    if (metaInfo.Version >= MarketDataVersions.Version49)
                    {
                        hasLocalTime = msg.HasLocalTime(msg.ServerTime);
                        writer.Write(hasLocalTime);
                    }

                    if (hasLocalTime)
                    {
                        lastOffset               = metaInfo.LastLocalOffset;
                        metaInfo.LastLocalTime   = writer.WriteTime(msg.LocalTime, metaInfo.LastLocalTime, LocalizedStrings.Str1024, allowNonOrdered, isUtc, metaInfo.LocalOffset, allowDiffOffsets, isTickPrecision, ref lastOffset, true);
                        metaInfo.LastLocalOffset = lastOffset;
                    }
                }

                if (metaInfo.Version < MarketDataVersions.Version42)
                {
                    continue;
                }

                if (metaInfo.Version >= MarketDataVersions.Version51)
                {
                    writer.Write(msg.IsSystem != null);

                    if (msg.IsSystem != null)
                    {
                        writer.Write(msg.IsSystem.Value);
                    }
                }
                else
                {
                    writer.Write(msg.IsSystem ?? true);
                }

                if (msg.IsSystem == false)
                {
                    if (metaInfo.Version >= MarketDataVersions.Version51)
                    {
                        writer.WriteNullableInt(msg.TradeStatus);
                    }
                    else
                    {
                        writer.WriteInt(msg.TradeStatus ?? 0);
                    }
                }

                var oi = msg.OpenInterest;

                if (metaInfo.Version < MarketDataVersions.Version46)
                {
                    writer.WriteVolume(oi ?? 0m, metaInfo, SecurityId);
                }
                else
                {
                    writer.Write(oi != null);

                    if (oi != null)
                    {
                        writer.WriteVolume(oi.Value, metaInfo, SecurityId);
                    }
                }

                if (metaInfo.Version < MarketDataVersions.Version45)
                {
                    continue;
                }

                writer.Write(msg.IsUpTick != null);

                if (msg.IsUpTick != null)
                {
                    writer.Write(msg.IsUpTick.Value);
                }

                if (metaInfo.Version < MarketDataVersions.Version52)
                {
                    continue;
                }

                writer.Write(msg.Currency != null);

                if (msg.Currency != null)
                {
                    writer.WriteInt((int)msg.Currency.Value);
                }
            }
        }
Ejemplo n.º 2
0
        protected override void OnSave(BitArrayWriter writer, IEnumerable <ExecutionMessage> messages, TradeMetaInfo metaInfo)
        {
            if (metaInfo.IsEmpty())
            {
                var first = messages.First();

                metaInfo.FirstId      = metaInfo.PrevId = first.TradeId;
                metaInfo.ServerOffset = first.ServerTime.Offset;
            }

            writer.WriteInt(messages.Count());

            var allowNonOrdered = metaInfo.Version >= MarketDataVersions.Version48;
            var isUtc           = metaInfo.Version >= MarketDataVersions.Version50;

            foreach (var msg in messages)
            {
                if (msg.ExecutionType != ExecutionTypes.Tick)
                {
                    throw new ArgumentOutOfRangeException("messages", msg.ExecutionType, LocalizedStrings.Str1019Params.Put(msg.TradeId));
                }

                // сделки для индексов имеют нулевой номер
                if (msg.TradeId < 0)
                {
                    throw new ArgumentOutOfRangeException("messages", msg.TradeId, LocalizedStrings.Str1020);
                }

                // execution ticks (like option execution) may be a zero cost
                // ticks for spreads may be a zero cost or less than zero
                //if (msg.TradePrice < 0)
                //	throw new ArgumentOutOfRangeException("messages", msg.TradePrice, LocalizedStrings.Str1021Params.Put(msg.TradeId));

                // pyhta4og.
                // http://stocksharp.com/forum/yaf_postsm6450_Oshibka-pri-importie-instrumientov-s-Finama.aspx#post6450

                if (msg.Volume < 0)
                {
                    throw new ArgumentOutOfRangeException("messages", msg.Volume, LocalizedStrings.Str1022Params.Put(msg.TradeId));
                }

                metaInfo.PrevId = writer.SerializeId(msg.TradeId, metaInfo.PrevId);

                writer.WriteVolume(msg.Volume, metaInfo, SecurityId);
                writer.WritePriceEx(msg.TradePrice, metaInfo, SecurityId);
                writer.WriteSide(msg.OriginSide);

                metaInfo.LastTime = writer.WriteTime(msg.ServerTime, metaInfo.LastTime, LocalizedStrings.Str1023, allowNonOrdered, isUtc, metaInfo.ServerOffset);

                if (metaInfo.Version < MarketDataVersions.Version40)
                {
                    continue;
                }

                if (metaInfo.Version < MarketDataVersions.Version47)
                {
                    writer.WriteLong(SecurityId.GetLatency(msg.ServerTime, msg.LocalTime).Ticks);
                }
                else
                {
                    var hasLocalTime = true;

                    if (metaInfo.Version >= MarketDataVersions.Version49)
                    {
                        hasLocalTime = !msg.LocalTime.IsDefault() && msg.LocalTime != msg.ServerTime;
                        writer.Write(hasLocalTime);
                    }

                    if (hasLocalTime)
                    {
                        metaInfo.LastLocalTime = writer.WriteTime(msg.LocalTime, metaInfo.LastLocalTime, LocalizedStrings.Str1024, allowNonOrdered, isUtc, metaInfo.LocalOffset);
                    }
                }

                if (metaInfo.Version < MarketDataVersions.Version42)
                {
                    continue;
                }

                writer.Write(msg.IsSystem);

                if (!msg.IsSystem)
                {
                    writer.WriteInt(msg.TradeStatus);
                }

                var oi = msg.OpenInterest;

                if (metaInfo.Version < MarketDataVersions.Version46)
                {
                    writer.WriteVolume(oi ?? 0m, metaInfo, SecurityId);
                }
                else
                {
                    writer.Write(oi != null);

                    if (oi != null)
                    {
                        writer.WriteVolume(oi.Value, metaInfo, SecurityId);
                    }
                }

                if (metaInfo.Version < MarketDataVersions.Version45)
                {
                    continue;
                }

                writer.Write(msg.IsUpTick != null);

                if (msg.IsUpTick != null)
                {
                    writer.Write(msg.IsUpTick.Value);
                }
            }
        }