change length of indicator dynamically

Questions about MultiCharts .NET and user contributed studies.
User avatar
ernay
Posts: 14
Joined: 08 Jun 2020
Has thanked: 9 times

change length of indicator dynamically

Postby ernay » 27 Jul 2020

hi there,
I'm using a 1minute chart and want to have an indicator (like a moving average) that gives me a length equal to the number of bars in the current session. So basically, on each new bar, the length of the indicator would increase by 1, and would be reflected in the plot. I tried to implement this, but the indicator will not change from the original length used. Is there a way to "reinitialize" the indicator on each new bar with the new length I want to use?

Thanks.

User avatar
Vlada MultiCharts
Posts: 293
Joined: 22 Apr 2020
Has thanked: 8 times
Been thanked: 76 times

Re: change length of indicator dynamically

Postby Vlada MultiCharts » 06 Aug 2020

Hello ernay,

You can use the keyword recalculate.

Here is an example:

Code: Select all

inputs: Price( Close ), Displace( 0 ); var: var0( 0 ); var: ReCalcPersist Length( 9 ); print(currentbar + MaxBarsBack , " ", Length); var0 = AverageFC( Price, Length ) ; condition1 = Displace >= 0 or CurrentBar > AbsValue( Displace ) ; if condition1 then begin Plot1[Displace]( var0, "Avg" ) ; end; if getappinfo(aiRealTimeCalc) = 1 and LastBarOnChart and barstatus(1) = 2 then begin length = length + 1; recalculate; end;

User avatar
ernay
Posts: 14
Joined: 08 Jun 2020
Has thanked: 9 times

Re: change length of indicator dynamically

Postby ernay » 08 Aug 2020

Thanks Vlada. What would this look like for power language?

Best,
Ernie

User avatar
Vlada MultiCharts
Posts: 293
Joined: 22 Apr 2020
Has thanked: 8 times
Been thanked: 76 times

Re: change length of indicator dynamically

Postby Vlada MultiCharts » 19 Aug 2020

Hello Ernie,

Above is an example for regular MultiCharts (PowerLanguage).

You can also check the example for MultiCharts .NET:

Code: Select all

using System; using System.Drawing; using PowerLanguage.Function; namespace PowerLanguage.Indicator { [SameAsSymbol(true)] public class MovAVGRec: IndicatorObject { public MovAVGRec(object ctx): base(ctx){} private ISeries<double> price {get; set;} private AverageFC m_averagefc1; private VariableSeries<Double> m_avg; private IPlotObject Plot1; public int length = 9; [Input] public int displace { get; set; } protected override void Create() { m_averagefc1 = new AverageFC(this); m_avg = new VariableSeries<Double>(this); Plot1 = AddPlot(new PlotAttributes("Avg", 0, Color.Yellow, Color.Empty, 0, 0, true)); } protected override void StartCalc() { Output.WriteLine(" Length = {0} ", length); price = Bars.Close; m_averagefc1.price = price; m_averagefc1.length = length; } protected override void CalcBar() { m_avg.Value = m_averagefc1[0]; if (displace >= 0 || Bars.CurrentBar > Math.Abs(displace)) { Plot1.Set(displace, m_avg.Value); } if (Environment.IsRealTimeCalc && Bars.LastBarOnChart && Bars.Status == EBarState.Close) { length = length + 1; ExecControl.Recalculate(); } } } }


Return to “MultiCharts .NET”