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); ;
}
}
}