Confused about MaxBarsBack

Questions about MultiCharts .NET and user contributed studies.
Talky_Zebra
Posts: 45
Joined: 07 Mar 2024
Has thanked: 13 times
Been thanked: 1 time

Confused about MaxBarsBack

Postby Talky_Zebra » 29 Mar 2024

Still building my MC coding skills, and hit a really curious issue. I used one of the community made signals (ATR, thank you tradetree) as the basis for making a reusable function that could be called by either a signal or an indi. Additionally, just to make it more interesting, I made a separate indi that could plot the Strategy's value.... That way I could visualize if my indicator directly calling the function matched the strategy calling the function.

I ran into a MaxBarsBack problem: code attached
When I use the strategy to pass a period length to the function call, it seems to be capped at 50 MaxBarsBack. I have tried setting the strategy's settings directly

Code: Select all

... [Input] public int period_length { get; set; } [Input] public int max_bars_back { get; set; } ... protected override void StartCalc() { Output.Clear(); // assign inputs atr.period = period_length; ExecInfo.MaxBarsBack = max_bars_back; }
where I could set max_bars_back to 100. When also setting the period_length to 100, there are no errors on that ExecInfo.MaxBarsBack assignment, per se. But the Strategy will give the error: "Tried to reference back more bars (99) than allow by the current MaxBarsBack setting. Please increase the MaxBarsBack setting."
MaxBarsBackerror.JPG
(15.86 KiB) Not downloaded yet
Any period_length I assign at 50 or less works. Anything higher returns a similar error to the above.

When I output the current ExecInfo.MaxBarsBack from the strategy, it shows 50. It does not matter what I set it to - 10 or 100, it reports 50.

Thinking that maybe the indicator needs its MaxBarsBack setting to be higher, I changed it to 100. And it will report that the setting is changed, but I still get the error from the strategy.

I tried getting and setting MaxBarsBack on the function but it will not return a call to Output.WriteLine. I cannot tell if this is the source of the error.

What is the real source of the error? The strategy? The indicator? Or the function? (Edit: I am sure it isn't the matching indicator, when I remove it from the chart and enter in a long period length like 90 or 100, the error still occurs)
How do I change the MaxBarsBack to allow any given length to work while still maintaining the benefits of a strategy that uses a calculation function and a related indicator to show the values?
Attachments
_TEST_ATR_Strategy.Strategy.CS.txt
(1.39 KiB) Downloaded 12 times
_TEST_ATR_Strategy.Indicator.CS.txt
(997 Bytes) Downloaded 13 times
_ATR.Function.CS.txt
(2.76 KiB) Downloaded 13 times

HellGhostEvocatorX
Posts: 81
Joined: 10 Feb 2022
Has thanked: 52 times
Been thanked: 11 times

Re: Confused about MaxBarsBack

Postby HellGhostEvocatorX » 02 Apr 2024

right click on the chart --> format signal --> bottom right "properties" --> in the first tab then set "maximum bars study will reference. Of course you can only set a maximum of as many "bars back" as your symbol contains bars .This would be the way to not set max bars back in code.

Talky_Zebra
Posts: 45
Joined: 07 Mar 2024
Has thanked: 13 times
Been thanked: 1 time

Re: Confused about MaxBarsBack

Postby Talky_Zebra » 05 Apr 2024

HellGhost - Thank you! Awesome, that helps, and I can see that I can even set a default. You deserve two thanks for that.

MultiCharts team - why the heck does it work this way? While I understand that a manual input as described above is needed for non-coding users, I can intelligently and programmatically set what I want for an indicator. Why not for a strategy?

User avatar
Polly MultiCharts
Posts: 203
Joined: 20 Jul 2022
Has thanked: 1 time
Been thanked: 56 times

Re: Confused about MaxBarsBack

Postby Polly MultiCharts » 08 Apr 2024

Hello Talky_Zebra,

MaxBarsBack is set for the whole strategy and it cannot change dynamically because a strategy can consist of many signals. If it worked as in indicators, conflict situations and recursive recalculation could occur when processing a given instruction in different signals of the same strategy.

Talky_Zebra
Posts: 45
Joined: 07 Mar 2024
Has thanked: 13 times
Been thanked: 1 time

Re: Confused about MaxBarsBack

Postby Talky_Zebra » 09 Apr 2024

Ok, an inherent limitation due to the design of the application. Not a bug. Understood


Return to “MultiCharts .NET”