Exemple #1
0
        protected override int InternalCalculate(IEnumerable <Bar> bars = null)
        {
            List <Bar> history;

            if (bars != null)
            {
                history = new List <Bar>(bars);
            }
            else if (_buff.Values.Count == 0)
            {
                history = _dataProvider.GetBars(_selection);
            }
            else
            {
                var sel = _selection.Clone() as Selection;
                sel.From = _buff.Values.Last().Date;
                sel.To   = DateTime.MaxValue;
                history  = _dataProvider.GetBars(sel);
            }

            if (history == null || history.Count == 0)
            {
                return(0);
            }

            MA.Calculate(bars);

            for (var i = _buff.Length > 0 ? _buff.Length - 1 : 0; i < MA.Series[0].Values.Count; i++)
            {
                _buff.AppendOrUpdate(MA.Series[0].Values[i].Date, MA.Series[0].Values[i].Value);
            }

            foreach (var bar in history)
            {
                var ma = _buff.Values.FirstOrDefault(p => p.Date.Equals(bar.Date));
                if (ma == null)
                {
                    continue;
                }

                if (ma.Value == EMPTY_VALUE)
                {
                    Series[0].AppendOrUpdate(ma.Date, ma.Value);
                }
                else
                {
                    Series[0].AppendOrUpdate(ma.Date, (double)GetPrice(bar, PriceConstants.LOW) - ma.Value);
                }
            }

            return(history.Count);
        }
        protected override int InternalCalculate(IEnumerable <Bar> bars = null)
        {
            List <Bar> history;

            if (bars != null)
            {
                history = new List <Bar>(bars);
            }
            else if (Series[0].Values.Count < 2)
            {
                history = _dataProvider.GetBars(_selection);
            }
            else
            {
                var sel = _selection.Clone() as Selection;
                sel.From = Series[0].Values[Series[0].Values.Count - 2].Date;
                sel.To   = DateTime.MaxValue;
                history  = _dataProvider.GetBars(sel);
            }

            if (history == null || history.Count < 2)
            {
                return(0);
            }

            // True range calculation
            for (var i = 1; i < history.Count; i++)
            {
                var hi        = GetPrice(history[i], PriceConstants.HIGH);
                var lo        = GetPrice(history[i], PriceConstants.LOW);
                var prevClose = GetPrice(history[i - 1], PriceConstants.CLOSE);
                if (_tr.Length == 0)
                {
                    _tr.AppendOrUpdate(history[i].Date, (double)(hi - lo));
                }
                else
                {
                    var value = Math.Max((hi - lo), Math.Abs((hi - prevClose)));
                    value = Math.Max(value, Math.Abs((lo - prevClose)));
                    _tr.AppendOrUpdate(history[i].Date, (double)value);
                }
            }

            //Avg. true range calculation
            for (var i = 0; i < history.Count; i++)
            {
                if (Series[0].Length < Period - 1)
                {
                    Series[0].AppendOrUpdate(history[i].Date, EMPTY_VALUE);
                }
                else if (Series[0].Length == Period - 1)
                {
                    var sum = _tr.Values.GetRange(0, Period).Sum(p => p.Value) * (1 / Period);
                    Series[0].AppendOrUpdate(history[i].Date, sum);
                }
                else
                {
                    var prevATR = Series[0].Values.LastOrDefault(p => p.Date < history[i].Date);
                    if (prevATR == null)
                    {
                        continue;
                    }

                    var tr = _tr.Values.FirstOrDefault(p => p.Date.Equals(history[i].Date));
                    if (tr != null)
                    {
                        Series[0].AppendOrUpdate(history[i].Date, ((prevATR.Value * (Period - 1)) + tr.Value) / Period);
                    }
                }
            }

            return(history.Count - 1);
        }