When using the Lambda<T> type, the expression is evaluated on every call to the lambda object. For example for the following lambda
Code: Select all
var averagePrices = new Lambda<double>(bb => Bars.AvgPrice(bb));
I was looking for a version of Lambda VariableSeries (or buffered lambda) which gets a lambda expression and buffers the values to make subsequent queries faster. Do we have something like that in the MultiCharts.NET class libraries? I noticed there is a LambdaRef<T> but I'm not sure if it is what I want or how to use it.
The following is my implementation of a BufferedLambda, however, this is untested and I'm thinking there must be a more correct and efficient way to do this.LambdaRef<T>: The lambda implementation of IVar
Code: Select all
using System;
using PowerLanguage.Function;
namespace PowerLanguage
{
public class BufferedLambda<T> : FunctionSeries<T>
{
public BufferedLambda(CStudyControl ctx, int dataStream = 0) : base(ctx, dataStream) { }
public Lambda<T> Lambda { get; set; }
private VariableSeries<T> _series;
private int _lastBarCalculated;
protected override void Create()
{
_series = new VariableSeries<T>(this);
}
protected override void StartCalc()
{
_lastBarCalculated = -1;
}
protected override T CalcBar()
{
if (Lambda == null)
throw new NullReferenceException("Lambda input cannot be null.");
if (Bars.CurrentBar <= _lastBarCalculated)
return _series.Value;
_lastBarCalculated = Bars.CurrentBar;
return _series.Value = Lambda.Value;
}
}
}