Exemple #1
0
        public IList <double> Execute(IList <double> values)
        {
            var security = Context.Runtime.Securities.First();
            var bars     = security.Bars;
            var count    = Math.Min(bars.Count, values.Count);

            if (count == 0)
            {
                return(new ConstList <double>(values.Count, 0));
            }

            var results = Context?.GetArray <double>(values.Count) ?? new double[values.Count];

            TimeFrameUtils.GetFirstBounds(TimeFrame, bars[0].Date, out var firstDateTime, out var lastDateTime);
            var sum = 0D;

            for (var i = 0; i < count; i++)
            {
                var barDate = bars[i].Date;
                if (barDate >= lastDateTime)
                {
                    TimeFrameUtils.GetBounds(TimeFrame, barDate, ref firstDateTime, ref lastDateTime);
                    sum = 0;
                }
                results[i] = sum += values[i];
            }
            for (var i = count; i < results.Length; i++)
            {
                results[i] = sum;
            }

            return(results);
        }
Exemple #2
0
        public IList <double> Execute(IList <double> source)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            var security = Context.Runtime.Securities.First();
            var bars     = security.Bars;
            var minCount = Math.Min(source.Count, bars.Count);

            if (minCount == 0)
            {
                return(EmptyArrays.Double);
            }

            TimeFrameUtils.GetFirstBounds(TimeFrame, bars[0].Date, out var firstDateTime, out var lastDateTime);
            var results = Context.GetArray <double>(minCount);

            for (int i = 0, firstIndex = 0; i <= results.Length; i++)
            {
                if (i == results.Length || bars[i].Date >= lastDateTime)
                {
                    var count = i - firstIndex;
                    if (count > 0)
                    {
                        var firstValue = source[firstIndex];
                        if (firstValue == 0)
                        {
                            firstValue = double.MaxValue;
                        }

                        results[firstIndex] = 0;
                        for (int j = 1, index = firstIndex + 1; j < count; j++, index++)
                        {
                            results[index] = (source[index] - firstValue) / firstValue * 100;
                        }
                    }
                    if (i == results.Length)
                    {
                        break;
                    }

                    TimeFrameUtils.GetBounds(TimeFrame, bars[i].Date, ref firstDateTime, ref lastDateTime);
                    firstIndex = i;
                }
            }
            return(results);
        }
Exemple #3
0
        public double Execute(double value, int index)
        {
            if (index < m_index)
            {
                throw new InvalidOperationException();
            }

            if (index == m_index)
            {
                return(m_sum);
            }

            var security = Context.Runtime.Securities.First();
            var bars     = security.Bars;
            var count    = Math.Min(bars.Count, index + 1);

            if (count == 0)
            {
                return(0);
            }

            if (m_firstDateTime == default(DateTime))
            {
                TimeFrameUtils.GetFirstBounds(TimeFrame, bars[index].Date, out m_firstDateTime, out m_lastDateTime);
            }

            for (var i = m_index + 1; i < count; i++)
            {
                var barDate = bars[i].Date;
                if (barDate >= m_lastDateTime)
                {
                    TimeFrameUtils.GetBounds(TimeFrame, barDate, ref m_firstDateTime, ref m_lastDateTime);
                    m_sum = 0;
                }
                m_sum += value;
            }
            m_index = index;
            return(m_sum);
        }
Exemple #4
0
        public IList <double> Execute(ISecurity security)
        {
            if (security == null)
            {
                throw new ArgumentNullException(nameof(security));
            }

            if (TimeFrame <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(TimeFrame));
            }

            var timeFrame = TimeFrameFactory.Create(TimeFrame, TimeFrameUnit);

            var bars = security.Bars;

            if (bars.Count == 0)
            {
                return(EmptyArrays.Double);
            }

            if (bars.Count == 1)
            {
                return new[] { bars[0].Volume }
            }
            ;

            DateTime firstDateTime, lastDateTime;

            TimeFrameUtils.GetFirstBounds(timeFrame, bars[0].Date, out firstDateTime, out lastDateTime);
            var results = Context?.GetArray <double>(bars.Count) ?? new double[bars.Count];

            for (int i = 0, firstIndex = 0; i <= results.Length; i++)
            {
                if (i == results.Length || bars[i].Date >= lastDateTime)
                {
                    var count = i - firstIndex;
                    if (count > 0)
                    {
                        var result = 0D;
                        switch (ValueMode)
                        {
                        case ValueForPeriodMode.Sum:
                            for (int j = 0, index = firstIndex; j < count; j++, index++)
                            {
                                result        += GetValue(security, index);
                                results[index] = result;
                            }
                            break;

                        case ValueForPeriodMode.Average:
                            for (int j = 0, index = firstIndex; j < count; j++, index++)
                            {
                                result        += GetValue(security, index);
                                results[index] = result / (j + 1);
                            }
                            break;

                        default:
                            throw new InvalidEnumArgumentException(nameof(ValueMode), (int)ValueMode, ValueMode.GetType());
                        }
                    }
                    if (i == results.Length)
                    {
                        break;
                    }

                    TimeFrameUtils.GetBounds(timeFrame, bars[i].Date, ref firstDateTime, ref lastDateTime);
                    firstIndex = i;
                }
            }
            return(results);
        }