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