Composing .NET functions

Questions about MultiCharts .NET and user contributed studies.
aha
Posts: 1
Joined: 15 Sep 2020

Composing .NET functions

Postby aha » 15 Sep 2020

Hi,

can't figure out how to do this basic stuff. What I'm trying to do is compose custom function with the standard functions (or other functions).

Below is example just illustrating what I'm trying to. I would expect it to work like this but apparently no. What is the missing step?

Indicator composing custom function with ema

Code: Select all

using System; using System.Drawing; using System.Linq; using PowerLanguage.Function; namespace PowerLanguage.Indicator{ public class Function_Composition_Test : IndicatorObject { public Function_Composition_Test(object _ctx):base(_ctx){} private IPlotObject plot1; private FunctionCompositionTest functionCompositionTest; private XAverage emaOfTest; protected override void Create() { plot1 = AddPlot(new PlotAttributes("", EPlotShapes.Line, Color.Red)); functionCompositionTest = new FunctionCompositionTest(this); emaOfTest = new XAverage(functionCompositionTest); } protected override void StartCalc() { emaOfTest.Length = 20; } protected override void CalcBar(){ // indicator logic plot1.Set(emaOfTest[0]); } } }
test function

Code: Select all

using System; using System.Drawing; using System.Linq; namespace PowerLanguage { namespace Function { public sealed class FunctionCompositionTest : FunctionSeries<System.Double> { public FunctionCompositionTest(CStudyControl _master) : base(_master) { } public FunctionCompositionTest(CStudyControl _master, int _ds) : base(_master, _ds) { } private int counter = 0; protected override void Create() { this.counter = 0; } protected override void StartCalc() { } protected override System.Double CalcBar() { this.counter++; return (double) this.counter % 5; } } } }

User avatar
Tammy MultiCharts
Posts: 200
Joined: 06 Aug 2020
Has thanked: 6 times
Been thanked: 65 times

Re: Composing .NET functions

Postby Tammy MultiCharts » 22 Sep 2020

Hello aha,

You can use the standard built-in functions, like MACD or BarNumber, as a reference.

Please check out this version of your code. It should work fine if we got your goal correctly:

Code: Select all

using System; using System.Drawing; using System.Linq; using PowerLanguage.Function; namespace PowerLanguage.Indicator { public class Function_Composition_Test : IndicatorObject { private Function.FunctionCompositionTest f_func_comp; private Function.XAverage f_xaverage; private VariableSeries<Double> VarSer_Func_Comp_Test; private IPlotObject Plot1; private IPlotObject Plot2; public Function_Composition_Test(object ctx) : base(ctx) { length_xaverage = 9; } [Input] public int length_xaverage { get; set; } protected override void Create() { f_func_comp = new Function.FunctionCompositionTest(this); f_xaverage = new Function.XAverage(this); VarSer_Func_Comp_Test = new VariableSeries<Double>(this); Plot1 = AddPlot(new PlotAttributes("XAverage", 0, Color.Cyan, Color.Empty, 0, 0, true)); Plot2 = AddPlot(new PlotAttributes("XAverage_FunctionCompositionTest", 0, Color.Yellow, Color.Empty, 0, 0, true)); } protected override void StartCalc() { f_xaverage.Price = VarSer_Func_Comp_Test; f_xaverage.Length = length_xaverage; VarSer_Func_Comp_Test.DefaultValue = 0; } protected override void CalcBar() { VarSer_Func_Comp_Test.Value = f_func_comp[0]; Plot1.Set(0, VarSer_Func_Comp_Test.Value); Plot2.Set(0, f_xaverage[0]); } } }


Return to “MultiCharts .NET”