상속: Event
예제 #1
0
 protected void Emit(DataObject obj)
 {
     if (!this.emitter.queue.IsFull())
         this.emitter.queue.Write(obj);
     else
         Console.WriteLine($"DataProcessor::Emit Can not write data object. Data queue is full. Data queue size = {this.emitter.queue.Size}");
 }
예제 #2
0
        internal void Process(DataObject obj)
        {
            if (this.providerId != -1 && ((Tick)obj).ProviderId != this.providerId)
                return;

            if (!InSession(obj.DateTime))
                return;

            OnData(obj);
        }
예제 #3
0
        public void Add(DataObject obj)
        {
            lock (Sync)
            {
                if (obj.DateTime.Ticks == 0)
                {
                    Console.WriteLine("DataSeries::Add Error: can not add object with DateTime = 0");
                    return;
                }

                if (!this.writeOpened)
                    OpenWrite();

                if (Count == 0)
                {
                    DateTime1 = obj.DateTime;
                    DateTime2 = obj.DateTime;
                }
                else
                {
                    if (obj.DateTime < DateTime2)
                    {
                        Insert(obj);
                        return;
                    }
                    DateTime2 = obj.DateTime;
                }
                Count++;
                this.writeKey.AddObject(obj);
                if (this.writeKey.count == this.writeKey.size)
                {
                    WriteKey(this.writeKey);
                    if (!CacheObjects && this.writeKey != this.readKey && this.writeKey != this.insertKey && this.writeKey != this.deleteKey)
                        this.writeKey.objects = null;

                    this.writeKey = new DataKey(this.file, null, this.writeKey.position, -1);
                    this.writeKey.number = this.bufferCount;
                    this.writeKey.index1 = Count - 1;
                    this.writeKey.index2 = Count - 1;
                    this.writeKey.changed = true;
                    this.bufferCount++;
                    this.cache[this.writeKey.number] = this.writeKey;
                }
                this.changed = true;
                this.file.isModified = true;
            }
        }
예제 #4
0
        protected virtual DataObject OnData(DataObject obj)
        {
            if (obj.TypeId == DataObjectType.Bar)
            {
                var bar = (Bar)obj;
                if (EmitBarOpen)
                    Emit(new Bar(bar.OpenDateTime, bar.OpenDateTime, bar.InstrumentId, bar.Type, bar.Size, bar.Open, 0, 0, 0, 0, 0));

                if (EmitBarOpenTrade)
                    Emit(new Trade(bar.DateTime, 0, bar.InstrumentId, bar.Open, (int)(bar.Volume / 4)));

                if (EmitBarHighTrade && EmitBarLowTrade)
                {
                    if (bar.Close > bar.Open)
                    {
                        Emit(new Trade(new DateTime(bar.DateTime.Ticks + (bar.CloseDateTime.Ticks - bar.DateTime.Ticks) / 3), 0, bar.InstrumentId, bar.Low, (int)(bar.Volume / 4)));
                        Emit(new Trade(new DateTime(bar.DateTime.Ticks + (bar.CloseDateTime.Ticks - bar.DateTime.Ticks) * 2 / 3), 0, bar.InstrumentId, bar.High, (int)(bar.Volume / 4)));
                    }
                    else
                    {
                        Emit(new Trade(new DateTime(bar.DateTime.Ticks + (bar.CloseDateTime.Ticks - bar.DateTime.Ticks) / 3), 0, bar.InstrumentId, bar.High, (int)(bar.Volume / 4L)));
                        Emit(new Trade(new DateTime(bar.DateTime.Ticks + (bar.CloseDateTime.Ticks - bar.DateTime.Ticks) * 2 / 3), 0, bar.InstrumentId, bar.Low, (int)(bar.Volume / 4)));
                    }
                }
                else
                {
                    if (EmitBarHighTrade)
                        Emit(new Trade(new DateTime(bar.DateTime.Ticks + (bar.CloseDateTime.Ticks - bar.DateTime.Ticks) / 2), 0, bar.InstrumentId, bar.High, (int)(bar.Volume / 4)));

                    if (EmitBarLowTrade)
                        Emit(new Trade(new DateTime(bar.DateTime.Ticks + (bar.CloseDateTime.Ticks - bar.DateTime.Ticks) / 2), 0, bar.InstrumentId, bar.Low, (int)(bar.Volume / 4)));
                }

                if (EmitBarCloseTrade)
                    Emit(new Trade(bar.CloseDateTime, 0, bar.InstrumentId, bar.Close, (int)(bar.Volume / 4)));

                if (!EmitBar)
                    return null;
            }
            return obj;
        }
예제 #5
0
 protected override DateTime GetBarCloseDateTime(DataObject obj)
 {
     return GetDataObjectDateTime(obj, this.type).Date.Add(this.session2);
 }
예제 #6
0
 protected override void OnData(DataObject obj)
 {
     base.OnData(obj);
     if (this.bar.Volume >= this.barSize)
         EmitBar();
 }
예제 #7
0
 protected override DateTime GetBarCloseDateTime(DataObject obj)
 {
     return GetBarOpenDateTime(obj).AddSeconds(this.barSize);
 }
예제 #8
0
 protected virtual DateTime GetDataObjectDateTime(DataObject obj, ClockType type)
 {
     return type == ClockType.Local ? obj.DateTime : ((Tick)obj).ExchangeDateTime;
 }
예제 #9
0
 protected virtual DateTime GetBarOpenDateTime(DataObject obj) => obj.DateTime;
예제 #10
0
 public void Log(DataObject data, int groupId)
 {
     EventServer.OnLog(new GroupEvent(data, GroupManager.GroupById[groupId]));
 }
예제 #11
0
 public DataObject(DataObject obj) : base(obj.DateTime)
 {
 }
예제 #12
0
 private void SyncLocalClockWithDataObject(DataObject obj)
 {
     if (this.framework.Clock.Mode == ClockMode.Simulation)
         this.framework.Clock.DateTime = obj.DateTime;
     else
         obj.DateTime = this.framework.Clock.DateTime;
 }
예제 #13
0
 public override void Save(Instrument instrument, DataObject obj, SaveMode option = SaveMode.Add)
 {
     throw new NotImplementedException();
 }
예제 #14
0
 public void OnData(DataObject data)=> OnEvent(data);
예제 #15
0
 protected internal void EmitData(DataObject data, bool queued = true)
 {
     if (queued && this.dataQueue != null)
         this.dataQueue.Enqueue(data);
     else
         this.framework.EventServer.OnData(data);
 }
예제 #16
0
 public void UpdateObject(int index, DataObject obj)
 {
     this.objects[index] = obj;
     this.changed        = true;
 }
예제 #17
0
 public void Serialize(BinaryWriter writer, DataObject obj)
 {
     var streamer = this.streamsById[obj.TypeId];
     writer.Write(streamer.typeId);
     writer.Write(streamer.GetVersion(obj));
     streamer.Write(writer, obj);
 }
예제 #18
0
 public void Log(DataObject data, Group group)
 {
     EventServer.OnLog(new GroupEvent(data, group));
 }
예제 #19
0
 public void Save(Instrument instrument, DataObject obj, SaveMode option = SaveMode.Add) => Server.Save(instrument, obj, option);
예제 #20
0
 public DataObject(DataObject obj) : base(obj.DateTime)
 {
 }
예제 #21
0
 public void Save(int instrumentId, DataObject obj, SaveMode option = SaveMode.Add)
 {
     var i = this.framework.InstrumentManager.GetById(instrumentId);
     if (i != null)
         Server.Save(i, obj, option);
     else
         Console.WriteLine($"DataManager::Save Instrument with id does not exist in the framework id = {instrumentId}");
 }
예제 #22
0
 protected virtual DateTime GetBarCloseDateTime(DataObject obj) => obj.DateTime;
예제 #23
0
 public void Save(string symbol, DataObject obj, SaveMode option = SaveMode.Add)
 {
     var i = this.framework.InstrumentManager[symbol];
     if (i != null)
         Server.Save(i, obj, option);
     else
         Console.WriteLine($"DataManager::Save Instrument with symbol does not exist in the framework {symbol}");
 }
예제 #24
0
 protected override DateTime GetBarOpenDateTime(DataObject obj)
 {
     var t = GetDataObjectDateTime(obj, this.type);
     long seconds = (long)t.TimeOfDay.TotalSeconds / this.barSize * this.barSize;
     return t.Date.AddSeconds(seconds);
 }
예제 #25
0
 public virtual void Save(Instrument instrument, DataObject obj, SaveMode option = SaveMode.Add)
 {
     // no-op
 }
예제 #26
0
 protected override void OnData(DataObject obj)
 {
     var tick = (Tick)obj;
     if (this.bar != null) // when the bar is not new
     {
         if ((tick.Price > this.bar.High || tick.Price < this.bar.Low) && 10000 * (this.bar.High - this.bar.Low) >= this.barSize)
         {
             this.bar.DateTime = tick.DateTime;
             EmitBar();
         }
     }
     base.OnData(obj);
 }
예제 #27
0
 public void OnData(DataObject data) => OnEvent(data);
예제 #28
0
        protected override void OnData(DataObject obj)
        {
            bool barOpen = this.bar == null;
            base.OnData(obj);
            if (barOpen)
            {
                if (!AddReminder(GetBarCloseDateTime(obj), this.type))
                {
                    if (!(obj is Tick))
                        Console.WriteLine($"SessionBarFactoryItem::OnData Can not add reminder. Object is not tick! Clock = {this.type} local datetime = {this.factory.Framework.Clock.DateTime} exchange datetime = {this.factory.Framework.ExchangeClock.DateTime} object = {obj} reminder datetime = {GetBarCloseDateTime(obj)}");
                    else
                        Console.WriteLine($"SessionBarFactoryItem::OnData Can not add reminder. Clock = {this.type} local datetime = {this.factory.Framework.Clock.DateTime} exchange dateTime = {this.factory.Framework.ExchangeClock.DateTime} object = {obj} object exchange datetime = {((Tick)obj).ExchangeDateTime} reminder datetime = {GetBarCloseDateTime(obj)}");
                }
            }

            //bool barOpen = this.bar == null;
            //base.OnData(obj);
            //if (!barOpen || AddReminder(GetBarCloseDateTime(obj), this.type))
            //    return;
            //if (!(obj is Tick))
            //    Console.WriteLine($"SessionBarFactoryItem::OnData Can not add reminder. Object is not tick! Clock = {this.type} local datetime = {this.factory.Framework.Clock.DateTime} exchange datetime = {this.factory.Framework.ExchangeClock.DateTime} object = {obj} reminder datetime = {GetBarCloseDateTime(obj)}");
            //else
            //    Console.WriteLine($"SessionBarFactoryItem::OnData Can not add reminder. Clock = {this.type} local datetime = {this.factory.Framework.Clock.DateTime} exchange dateTime = {this.factory.Framework.ExchangeClock.DateTime} object = {obj} object exchange datetime = {((Tick)obj).ExchangeDateTime} reminder datetime = {GetBarCloseDateTime(obj)}");
        }
예제 #29
0
        public virtual void Update(long index, DataObject obj)
        {
            lock (Sync)
            {
                var dataObject = Get(index);
                if (dataObject.DateTime != obj.DateTime)
                {
                    Console.WriteLine("DataSeries::Update Can not update object with different datetime");
                    return;
                }

                bool changed = this.readKey.changed;
                this.readKey.UpdateObject((int) (index - this.readKey.index1), obj);
                if (!changed)
                    WriteKey(this.readKey);
                this.file.isModified = true;
            }
        }
예제 #30
0
        // TODO: rewrite it using goto statement!!!
        internal void OnData(DataObject obj)
        {
            /*
                        var tick = (Tick)obj;
                        var items = ItemLists[tick.InstrumentId];
                        if (items == null)
                            return;

                        int i = 0;
                        while (i < items.Count)
                        {
                            var item = items[i];
                            switch (item.barInput)
                            {
                                case BarInput.Trade:
                                    if (tick.TypeId == DataObjectType.Trade)
                                    {
                                        item.Process(tick);
                                        i++;
                                        continue;
                                    }
                                    break;
                                case BarInput.Bid:
                                    if (tick.TypeId == DataObjectType.Bid)
                                    {
                                        item.Process(tick);
                                        i++;
                                        continue;
                                    }
                                    break;
                                case BarInput.Ask:
                                    if (tick.TypeId == DataObjectType.Ask)
                                    {
                                        item.Process(tick);
                                        i++;
                                        continue;
                                    }
                                    break;
                                case BarInput.Middle:
                                    switch (tick.TypeId)
                                    {
                                        case DataObjectType.Bid:
                                            {
                                                var ask = this.framework.DataManager.GetAsk(tick.InstrumentId);
                                                if (ask == null)
                                                {
                                                    i++;
                                                    continue;
                                                }
                                                tick = new Tick(obj.DateTime, tick.ProviderId, tick.InstrumentId, (ask.Price + tick.Price) / 2.0, (ask.Size + tick.Size) / 2);
                                                break;
                                            }
                                        case DataObjectType.Ask:
                                            {
                                                Bid bid = this.framework.DataManager.GetBid(tick.InstrumentId);
                                                if (bid == null)
                                                {
                                                    i++;
                                                    continue;
                                                }
                                                tick = new Tick(obj.dateTime, tick.ProviderId, tick.InstrumentId, (bid.Price + tick.Price) / 2.0, (bid.Size + tick.Size) / 2);
                                                break;
                                            }
                                        case DataObjectType.Trade:
                                            i++;
                                            continue;
                                    }
                                    if (obj.TypeId != DataObjectType.Ask)
                                    {
                                        item.Process(tick);
                                        i++;
                                        continue;
                                    }
                                    break;
                                case BarInput.Tick:
                                    {
                                        item.Process(tick);
                                        i++;
                                        continue;
                                    }
                                case BarInput.BidAsk:
                                    if (tick.TypeId != DataObjectType.Trade)
                                    {
                                        item.Process(tick);
                                        i++;
                                        continue;
                                    }
                                    break;
                                default:
                                    Console.WriteLine($"BarFactory::OnData BarInput is not supported : {item.barInput}");
                                    break;
                            }
                            i++;
                        }
            */
            var tick = (Tick) obj;
            var items = ItemLists[tick.InstrumentId];
            if (items != null)
            {
                int i = 0;
                while (i < items.Count)
                {
                    var item = items[i];
                    switch (item.barInput)
                    {
                        case BarInput.Trade:
                            if (tick.TypeId == DataObjectType.Trade)
                            {
                                goto IL_19C;
                            }
                            break;
                        case BarInput.Bid:
                            if (tick.TypeId == DataObjectType.Bid)
                            {
                                goto IL_19C;
                            }
                            break;
                        case BarInput.Ask:
                            if (tick.TypeId == DataObjectType.Ask)
                            {
                                goto IL_19C;
                            }
                            break;
                        case BarInput.Middle:
                            switch (tick.TypeId)
                            {
                                case DataObjectType.Bid:
                                {
                                    var ask = this.framework.DataManager.GetAsk(tick.InstrumentId);
                                    if (ask == null)
                                    {
                                        goto IL_1A3;
                                    }
                                    tick = new Tick(obj.dateTime, tick.ProviderId, tick.InstrumentId,
                                        (ask.Price + tick.Price)/2.0, (ask.Size + tick.Size)/2);
                                    break;
                                }
                                case DataObjectType.Ask:
                                {
                                    var bid = this.framework.DataManager.GetBid(tick.InstrumentId);
                                    if (bid == null)
                                    {
                                        goto IL_1A3;
                                    }
                                    tick = new Tick(obj.dateTime, tick.ProviderId, tick.InstrumentId,
                                        (bid.Price + tick.Price)/2.0, (bid.Size + tick.Size)/2);
                                    break;
                                }
                                case DataObjectType.Trade:
                                    goto IL_1A3;
                            }
                            if (obj.TypeId != DataObjectType.Ask)
                            {
                                goto IL_19C;
                            }
                            break;
                        case BarInput.Tick:
                            goto IL_19C;
                        case BarInput.BidAsk:
                            if (tick.TypeId != DataObjectType.Trade)
                            {
                                goto IL_19C;
                            }
                            break;
                        default:
                            Console.WriteLine($"BarFactory::OnData BarInput is not supported : {item.barInput}");
                            break;
                    }
                    IL_1A3:
                    i++;
                    continue;
                    IL_19C:
                    item.Process(tick);
                    goto IL_1A3;
                }
            }

        }
예제 #31
0
        private void Insert(DataObject obj)
        {
            Count++;
            if (this.writeKey.dateTime1 <= obj.DateTime && obj.DateTime <= this.writeKey.dateTime2)
            {
                this.writeKey.AddObject(obj);
                if (this.writeKey.count >= this.writeKey.size)
                {
                    WriteKey(this.writeKey);
                    this.writeKey = new DataKey(this.file, null, this.writeKey.position, -1L)
                    {
                        number = this.bufferCount,
                        index1 = Count,
                        index2 = Count,
                        changed = true
                    };
                    this.bufferCount++;
                    this.cache[this.writeKey.number] = this.writeKey;
                }
                else
                {
                    this.changed = true;
                }
                this.file.isModified = true;
                return;
            }

            var key = GetKey(obj.DateTime, this.insertKey, IndexOption.Prev);
            if (this.insertKey == null)
            {
                this.insertKey = key;
            }
            else if (this.insertKey != key)
            {
                if (this.insertKey.changed)
                    WriteKey(this.insertKey);
                if (!CacheObjects && this.insertKey != this.readKey && this.insertKey != this.writeKey && this.insertKey != this.deleteKey)
                    this.insertKey.objects = null;
                this.insertKey = key;
            }
            this.insertKey.GetObjects();
            if (this.insertKey.count < this.insertKey.size)
            {
                this.insertKey.AddObject(obj);
                if (this.insertKey.count == this.insertKey.size)
                    WriteKey(this.insertKey);
            }
            else
            {
                key = new DataKey(this.file, null, -1L, -1L);
                int num = this.insertKey.GetIndex(obj.DateTime, SearchOption.Next);
                if (num == -1)
                {
                    key.AddObject(obj);
                }
                else
                {
                    for (int i = num; i < this.insertKey.count; i++)
                    {
                        key.AddObject(this.insertKey.objects[i]);
                        this.insertKey.objects[i] = null;
                    }
                    this.insertKey.count = num;
                    this.insertKey.index2 = this.insertKey.index1 + this.insertKey.count - 1;
                    if (this.insertKey.count != 0)
                        this.insertKey.dateTime2 = this.insertKey.objects[this.insertKey.count - 1].DateTime;
                    this.insertKey.AddObject(obj);
                }
                InsertKey(key, this.insertKey);
            }
            if (this.readKey != null && this.readKey.number > this.insertKey.number)
            {
                this.readKey.index1 += 1;
                this.readKey.index2 += 1;
            }
            if (this.writeKey != null && this.writeKey.number > this.insertKey.number)
            {
                this.writeKey.index1 += 1;
                this.writeKey.index2 += 1;
            }
            if (this.deleteKey != null && this.deleteKey.number > this.insertKey.number)
            {
                this.deleteKey.index1 += 1;
                this.deleteKey.index2 += 1;
            }
            this.insertKey.changed = true;
            this.changed = true;
            this.file.isModified = true;
        }
예제 #32
0
 protected virtual void OnData(DataObject obj)
 {
     var tick = obj as Tick;
     if (this.bar == null)
     {
         // new bar begins!
         this.bar = new Bar
         {
             InstrumentId = tick.InstrumentId,
             Type = this.barType,
             Size = this.barSize,
             OpenDateTime = GetBarOpenDateTime(obj),
             DateTime = this.GetDataObjectDateTime(obj, ClockType.Local),
             Open = tick.Price,
             High = tick.Price,
             Low = tick.Price,
             Close = tick.Price,
             Volume = tick.Size,
             Status = BarStatus.Open
         };
         this.factory.Framework.EventServer.OnEvent(this.bar);
     }
     else
     {
         // update it!
         if (tick.Price > this.bar.High)
             this.bar.High = tick.Price;
         if (tick.Price < this.bar.Low)
             this.bar.Low = tick.Price;
         this.bar.Close = tick.Price;
         this.bar.Volume += tick.Size;
         this.bar.DateTime = GetDataObjectDateTime(obj, ClockType.Local);
     }
     ++this.bar.N;
 }
예제 #33
0
 public virtual void Save(Instrument instrument, DataObject obj, SaveMode option = SaveMode.Add)
 {
     // no-op
 }