Use of Lambda object?  [SOLVED]

Questions about MultiCharts .NET and user contributed studies.
Xyzzy
Posts: 162
Joined: 19 Mar 2011
Has thanked: 43 times
Been thanked: 79 times

Use of Lambda object?

Postby Xyzzy » 03 Aug 2012

I'm trying to get my head around the new syntax for MC .Net strategies. I'm not really familiar with the Lambda object, or with lambda expressions in general.

The following lines are from the MovAvg_Cross_LE signal. Could someone please explain what the "= new Lambda" line is doing? Is there a way to rewrite this without using lambda expressions?

Code: Select all

private AverageFC m_averagefc1;
...
protected override void StartCalc(){
...
m_averagefc1.price = price;
m_averagefc1.length = new Lambda<Int32>(delegate { return length; });
...
}
I found a description of the Lambda class in the help files, but it just says "The lambda implementation of ISeries." I also couldn't find a help topic for the AverageFC class.

Many thanks in advance.

bluejack
Posts: 42
Joined: 02 Aug 2012
Has thanked: 25 times
Been thanked: 27 times

Re: Use of Lambda object?

Postby bluejack » 03 Aug 2012

Lambda is used to supply a function for a data point in the series. In this case its a simple function which always returns the "length"-number for every point in the length data series.

When the code runs through the average series it will call your "return length;" function for every point and request a value for AverageFC.length.

I suppose at the moment its there to create series which consists of constant values only. If the lambda delegate would have parameters you could make more fancy things with it.

riverTrader
Posts: 64
Joined: 15 Aug 2011
Has thanked: 3 times
Been thanked: 50 times

Re: Use of Lambda object?  [SOLVED]

Postby riverTrader » 04 Aug 2012

I take it from your question you may not be familiar with C#/.net; if so the previous answer might seem a little vague.

Simply put the Lamda function directs the series you have created to get its input data from the indicator you are creating. The alternative would be to give the series (eg. Length) a fixed value -- the Lamda function says to the series "check with the ("parent") indicator at each calculation (bar) and get the current value of Length"

the delegate { return length; } is instructing C# to create an anonmyous delegate that allows the m_averagefc1 to call back to the (public) length property.

[Input]
public int length{
get { return m_length; }
set { m_length = value; }
}

It is a little behind the scenes trickery (and one of the most powerful features of C#) that allows the m_averagefc1 object to call a method (a property is a method) on another object without knowing anything about the internals of the object.

Indicator > m_averagefc1 > calcSeries > indicator(delegate) > length

Let me know if you have questions.

Xyzzy
Posts: 162
Joined: 19 Mar 2011
Has thanked: 43 times
Been thanked: 79 times

Re: Use of Lambda object?

Postby Xyzzy » 04 Aug 2012

Thanks very much riverTrader! That's a great explanation, and very helpful.

Xyzzy
Posts: 162
Joined: 19 Mar 2011
Has thanked: 43 times
Been thanked: 79 times

Re: Use of Lambda object?

Postby Xyzzy » 04 Aug 2012

I have a few follow-up questions...

First, is it necessary to use lambda functions here? It seems a bit complicated. Wouldn't it be easier for the signal (e.g., MovAvg_Cross_LE) just to pass its "length" value to the function? E.g., something like:

Code: Select all

protected override void StartCalc(){
...
m_averagefc1.length = this.length;
...
}
Alternatively, the m_averagefc1 object is getting a reference to the parent object in the constructor:

Code: Select all

m_averagefc1 = new AverageFC(this);
Since the m_averagefc1 object has a reference to the parent object, couldn't it simply access the ".length" property of the parent itself, without using a separate lambda function?

Second, I also see that the AverageFC class is taking an "ISeries<int>" for the length value. (You can see this if you right-click on the "AverageFC" reference in Visual Studio, and select "Go to Definition....")

Is there a reason that it needs an "ISeries<int>" for the length, as opposed to just a regular int? To me, it seems that the "length" value is something that's static -- it shouldn't change from bar to bar, and therefore shouldn't need to be a series variable. Perhaps I'm missing something.

I hope I'm not being difficult -- I'm just trying to get my head around the nuances of the API for MC .Net. :-)

User avatar
Henry MultiСharts
Posts: 9093
Joined: 25 Aug 2011
Has thanked: 1258 times
Been thanked: 2923 times

Re: Use of Lambda object?

Postby Henry MultiСharts » 07 Aug 2012

Is there a reason that it needs an "ISeries<int>" for the length, as opposed to just a regular int?
Hello Xyzzy,

We converted PowerLanguage function's code to MultiCharts .NET C# code. Therefore some functions have such untidy code.
We will fix it later and make these indicators and strategies public.

Emmanuel
Posts: 303
Joined: 21 May 2009
Has thanked: 83 times
Been thanked: 22 times

Re: Use of Lambda object?

Postby Emmanuel » 07 Aug 2012

Hi Henry

Can you use this converter to convert PowerLanguage function's code to MultiCharts .NET Visual Basic code ?

User avatar
Henry MultiСharts
Posts: 9093
Joined: 25 Aug 2011
Has thanked: 1258 times
Been thanked: 2923 times

Re: Use of Lambda object?

Postby Henry MultiСharts » 07 Aug 2012

Hello Emmanuel,

This is not a completely automated tool. This is a semi-automated tool for developers that helps to convert some parts of PowerLanguage code to C# code. Manual interference is still required with it.


Return to “MultiCharts .NET”