Useful functions extensions

Questions about MultiCharts .NET and user contributed studies.
User avatar
danilo
Posts: 170
Joined: 02 Jan 2007
Location: Italy
Has thanked: 4 times
Been thanked: 9 times

Useful functions extensions

Postby danilo » 05 Sep 2013

I have noted that in MC.NET the functions are declared as class. This mean that to pass the parameters to the function it's necessary to pass the parameters to the class. This makes the code less readable.

Supposing to use the function "Stochastic", the following piece of code show how the function is initialized:

Code: Select all

private Stochastic m_stochastic1;

protected override void Create(){
m_stochastic1 = new Stochastic(this);
}

protected override void StartCalc() {
m_stochastic1.priceh = priceh;
m_stochastic1.pricel = pricel;
m_stochastic1.pricec = pricec;
m_stochastic1.stochlength = stochlength;
m_stochastic1.length1 = smoothinglength1;
m_stochastic1.length2 = smoothinglength2;
m_stochastic1.smoothingtype = smoothingtype;
m_stochastic1.ofastk = m_ofastk;
m_stochastic1.ofastd = m_ofastd;
m_stochastic1.oslowk = m_oslowk;
m_stochastic1.oslowd = m_oslowd;
}
It's possible simplify the initialization of the functions using the C# 3.0 extension. Just add the following static class to your library:

Code: Select all

using System;

namespace PowerLanguage.Function
{
public static class FunctionsExtension
{
// Set function helper
public static void Set<T>(this FunctionSeries<T> fn, params Func<string, object>[] hash)
{
foreach (Func<string, object> member in hash)
{
var propertyName = member.Method.GetParameters()[0].Name;
var propertyValue = member(string.Empty);
fn.GetType().GetProperty(propertyName).SetValue(fn, propertyValue, null);
}
}

public static void Set<T>(this FunctionSimple<T> fn, params Func<string, object>[] hash)
{
foreach (Func<string, object> member in hash)
{
var propertyName = member.Method.GetParameters()[0].Name;
var propertyValue = member(string.Empty);
fn.GetType().GetProperty(propertyName).SetValue(fn, propertyValue, null);
}
}

public static void With<T>(this T obj, Action<T> action)
{
action(obj);
}
}
}
After adding the class above it's possible write the StartClass of the example above in two different ways:

Method 1: Using the "Set" extension

Code: Select all

protected override void StartCalc() {
m_stochastic1.Set (priceh => priceh, pricel => pricel, pricec => pricec,
stochlength => stochlength, length1 => smoothinglength1,
length2 => smoothinglength2, smoothingtype => smoothingtype,
ofastk => m_ofastk, ofastd => m_ofastd, oslowk => m_oslowk, oslowd => m_oslowd);
}
Method 2: Using the "With" extension

Code: Select all

protected override void StartCalc() {
m_stochastic1.With (x => {x.priceh = priceh;
x.pricel = pricel;
x.pricec = pricec;
x.stochlength = stochlength;
x.length1 = smoothinglength1;
x.length2 = smoothinglength2;
x.smoothingtype = smoothingtype;
x.ofastk = m_ofastk;
x.ofastd = m_ofastd;
x.oslowk = m_oslowk;
x.oslowd = m_oslowd;} );
}
In this way the function initialization will be more readable.

Return to “MultiCharts .NET”