private void OnTradeBar(ITradeBar newCandle)
        {
            DateTime tclose = newCandle.Time;
            DateTime topen  = newCandle.OpenTime;

            if (FormingCandle == null)
            {
                //tbere isn't any currently forming candle, create one using new candle as generator
                var opeTime = GetOpenTime(newCandle.CloseTime, Resolution);
                FormingCandle = new Candlestick(opeTime, newCandle, Resolution);
            }
            if (topen >= FormingCandle.CloseTime || tclose > FormingCandle.CloseTime)
            {
                //old candle is ended, the new candle is already part of the next one
                OnConsolidated?.Invoke(FormingCandle);
                //use new candle as generator
                var opeTime = GetOpenTime(newCandle.CloseTime, Resolution);
                FormingCandle = new Candlestick(opeTime, newCandle, Resolution);
            }
            else
            {
                //the new candle is part of the forming candle
                FormingCandle.Merge(newCandle);
                //check if candle is completed and emit it
                if (tclose == FormingCandle.Time)
                {
                    OnConsolidated?.Invoke(FormingCandle);
                    FormingCandle = null;
                }
            }
        }
 public ChartBarVol(ITradeBar c)
     : base(c.Time.ToAxisDouble(), c.Open, c.High, c.Low, c.Close)
 {
     this.BuyVolume  = c.Close > c.Open ? c.QuoteAssetVolume : 0;
     this.SellVolume = c.Close > c.Open ? 0 : c.QuoteAssetVolume;
     this.X          = c.OpenTime.ToAxisDouble();
 }
        public ChartBar(ITradeBar c)
            : base(0, c.High, c.Low, c.Open, c.Close)
        {
            var time = c.CloseTime - TimeSpan.FromSeconds(c.Timeframe.TotalSeconds / 2);

            this.X = time.ToAxisDouble();
        }
Example #4
0
        public void UpdateLastBar(ITradeBar bar)
        {
            if (LastBar == null || this.LastBar.Time < bar.Time)
            {
                this.LastBar = bar;
            }

            if (FirstBar == null || this.FirstBar.Time > bar.Time)
            {
                this.FirstBar = bar;
            }
        }
Example #5
0
        private void UpdateStartAndEnd(ITradeBar bar)
        {
            if (this.EndOfData < bar.Time)
            {
                this.EndOfData = bar.Time;
            }

            if (this.StartOfData > bar.Time)
            {
                this.StartOfData = bar.Time;
            }
        }
Example #6
0
        public bool TryGetRecord(DateTime time, out ITradeBar record)
        {
            record = null;

            int ind = BinarySearchByOpenTime(time);

            if (ind > -1)
            {
                record = Records[ind];
                return(true);
            }
            else
            {
                return(false);
            }
        }
Example #7
0
        public ITradeBar GetLast(Func <ITradeBar, bool> criteria)
        {
            this.PositionPush();
            this.SeekNearestPreceding(LastTickTime);
            ITradeBar res = null;

            while (this.Previous())
            {
                if (criteria(this.Tick))
                {
                    res = this.Tick;
                    break;
                }
            }
            this.PositionPop();
            return(res);
        }
Example #8
0
        /// <summary>
        /// Adds a tradebar to currently loaded set
        /// </summary>
        /// <param name="c"></param>
        public void AddBar(Candlestick c)
        {
            ITradeBar lastCandle = this.Ticks.Count > 0 ? this.Ticks[this.Ticks.Count - 1] : null;

            if (c.Timeframe != this.Id.Resolution)
            {
                //throw new InvalidOperationException("Bad timeframe for candle");
                Logger.Error("Bad timeframe for candle");
            }
            else
            {
                this.UpdateStartAndEnd(c);
                //if this candle open is preceding last candle open we need to insert it in sorted fashion
                var toAdd = c; //new Candlestick(c);
                if (lastCandle?.OpenTime > toAdd.OpenTime)
                {
                    int i     = this.Ticks.BinarySearch(toAdd, CandlestickTimeComparer.Instance);
                    int index = i;
                    if (i > -1)
                    {
                        this.Ticks[index] = toAdd;
                    }
                    else
                    {
                        index = ~i;
                        this.Ticks.Insert(index, toAdd);
                    }
                    if (index > 0)
                    {
                        Debug.Assert(this.Ticks[index].OpenTime >= this.Ticks[index - 1].OpenTime);
                    }
                    if (index + 1 < this.Ticks.Count)
                    {
                        Debug.Assert(this.Ticks[index].OpenTime <= this.Ticks[index + 1].OpenTime);
                    }
                }
                else
                {
                    this.Ticks.Add(toAdd);
                }
            }
        }
        internal void UpdateFirstKnownData(SymbolHistoryId info, ITradeBar firstAvailable)
        {
            var data = GetMetaDataInternal(info);

            data.FirstKnownData = firstAvailable;
        }