Buffered Lamda

Questions about MultiCharts .NET and user contributed studies.
User avatar
orad
Posts: 121
Joined: 14 Nov 2012
Has thanked: 50 times
Been thanked: 20 times

Buffered Lamda

Postby orad » 26 Dec 2015

Hi,

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));
If you call averagePrices[0] a 1000 times then Bars.AvgPrice(0) is evaluated a 1000 times. This is unlike VariableSeries which stores the value once for each bar and then for subsequent queries for the same bar will just retrieve it from memory.

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.
LambdaRef<T>: The lambda implementation of IVar
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.

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

}
}
Do you have any ideas/suggestions? Thanks!

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: Buffered Lamda

Postby Henry MultiСharts » 07 Jan 2016

Hello orad,

Your understanding of the Lambda is correct, but your approach of storing the AvgPrice once and not recalculating it is wrong. On subsequent calculations the value must be recalculated (as the value has changed, therefore the actual AvgPrice has changed as well). Have you tried just creating a series variable and assigning the AvgPrice value to it in the beginning of the CalcBar?

Code: Select all

VariableSeries<double> _as;
protected override void Create()

{
_as = new VariableSeries<double>(this);
}
protected override void CalcBar()

{
_as.Value = Bars.AvgPrice(0);
}

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

Re: Buffered Lamda

Postby orad » 07 Jan 2016

Hi Henry, the code I posted above tries to encapsulates the logic you just explained. It uses a series variable to buffer the lambda result for each bar. It calculates once per bar, not per the whole series. The reason I wanted a functionality like this was that I noticed sometimes even the same bar is calculated several times and could create a performance hit for lambdas with heavier calculations. But I'm not sure if my code above is a reliable buffer as I noticed in some situations it did not give the exact results as the lambda.

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: Buffered Lamda

Postby Henry MultiСharts » 08 Jan 2016

Hello orad,

in your example you have created a series function, it will already return series data, therefore you can reference its previous values. No Lambda is required here.


Return to “MultiCharts .NET”