Looking for a fast function to calculate high and low  [SOLVED]

Posts: 111
Joined: 14 Nov 2012
Has thanked: 41 times
Been thanked: 18 times

Looking for a fast function to calculate high and low  [SOLVED]

Hi, I have a minute chart and need to find the highest/lowest of an ISeries in the last 10 days (14400 bars). Since the Highest() and Lowest() methods iterate through all the bars it takes a lot of time to calculate the chart for long time frames.

Is there any high performance function available to do this? I wrote a function trying to reduce the iterations by tracking the Highs and Lows on the bars and comparing edge values. But I think it does not considerably make the operation faster.

Code: Select all

`using System;namespace PowerLanguage.Function{    public class HighLowFC : FunctionSeries<Tuple<double, double>>    {        public HighLowFC(CStudyControl master, int dataStream = 0) : base(master, dataStream)        {        }        public ISeries<double> Price { get; set; }        public int Length { get; set; }        public VariableSeries<double> High { get; private set; }        public VariableSeries<double> Low { get; private set; }        protected override void Create()        {            High = (VariableSeries<double>)this.CreateSeriesVar<double>();            Low = (VariableSeries<double>)this.CreateSeriesVar<double>();        }        protected override void StartCalc()        {        }        protected override Tuple<double, double> CalcBar()        {            var hilo = GetHighestLowest(Price, Length);            High.Value = hilo.Item1;            Low.Value = hilo.Item2;            return hilo;        }        public Tuple<double, double> GetHighestLowest(ISeries<double> series, int length, int barsback = 0)        {            double max;            bool maxFound = false, minFound = false;            var min = max = series[barsback];            var lastHigh = High[barsback + 1];            var lastLow = Low[barsback + 1];            var previousFirst = series[barsback + Length];            if (PublicFunctions.DoubleGreaterEquals(max, lastHigh))                maxFound = true;            else if (!PublicFunctions.DoubleEquals(lastHigh, previousFirst))            {                max = lastHigh;                maxFound = true;            }            if (PublicFunctions.DoubleLessEquals(min, lastLow))                minFound = true;            else if (!PublicFunctions.DoubleEquals(lastLow, previousFirst))            {                min = lastLow;                minFound = true;            }            if (maxFound && minFound)            {                return Tuple.Create(max, min);            }            for (var index = barsback; index < barsback + length; ++index)            {                var val = series[index];                if (max < val) max = val;                if (min > val) min = val;            }            return Tuple.Create(max, min); ;        }    }}`
Last edited by orad on 23 Nov 2015, edited 1 time in total.