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

User avatar
orad
Posts: 121
Joined: 14 Nov 2012
Has thanked: 50 times
Been thanked: 20 times

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

Postby orad » 23 Nov 2015

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.

User avatar
orad
Posts: 121
Joined: 14 Nov 2012
Has thanked: 50 times
Been thanked: 20 times

Re: Looking for a fast function to calculate high and low

Postby orad » 23 Nov 2015

Update: After some tests on this function, looks like it does significantly reduce the calculation time. I would like to propose this function to be included in MultiCharts, maybe with a different name like BufferedHighLowFC. If you're interested to test and include it in the product with any modifications, you have my permission.


Return to “User Contributed Studies”