Referencing previous Data2 calculated values  [SOLVED]

Questions about MultiCharts and user contributed studies.
kagein
Posts: 55
Joined: 12 Oct 2017
Has thanked: 16 times
Been thanked: 10 times

Referencing previous Data2 calculated values

Postby kagein » 12 Nov 2020

On a chart with data1 being a 1 hour chart and data2 being a daily chart, I'm trying to reference previous bar values of a moving average from data2 but im not getting the right results. Ive included an indicator that shows the issue, i should be getting 5 distinct values for variable mavg, instead im getting just two. Any ideas where im going wrong?



Code: Select all

vars: x(0), mavg(0 data2); mavg = Average( close , 5 ) data2; if LastBarOnChart_s = true and barstatus(2) = 2 then begin once for x = 0 to 4 begin print("mavg :" , NumToStr(mavg[x] , 2)); end; end;
Output results:

Code: Select all

mavg :3439.30 mavg :3441.70 mavg :3441.70 mavg :3441.70 mavg :3441.70

User avatar
ABC
Posts: 718
Joined: 16 Dec 2006
Location: www.abctradinggroup.com
Has thanked: 125 times
Been thanked: 408 times
Contact:

Re: Referencing previous Data2 calculated values

Postby ABC » 13 Nov 2020

kagein,

this could be caused by a typo in your code.
When you declare "mavg" the variable is initialized with "(0 data2);". This would not tie the variable to data2 as you are missing a comma between 0 and data2. "0 Data2" is just a value like Close Data2 for example.

Regards,

ABC

On a chart with data1 being a 1 hour chart and data2 being a daily chart, I'm trying to reference previous bar values of a moving average from data2 but im not getting the right results. Ive included an indicator that shows the issue, i should be getting 5 distinct values for variable mavg, instead im getting just two. Any ideas where im going wrong?



Code: Select all

vars: x(0), mavg(0 data2); mavg = Average( close , 5 ) data2; if LastBarOnChart_s = true and barstatus(2) = 2 then begin once for x = 0 to 4 begin print("mavg :" , NumToStr(mavg[x] , 2)); end; end;
Output results:

Code: Select all

mavg :3439.30 mavg :3441.70 mavg :3441.70 mavg :3441.70 mavg :3441.70

kagein
Posts: 55
Joined: 12 Oct 2017
Has thanked: 16 times
Been thanked: 10 times

Re: Referencing previous Data2 calculated values  [SOLVED]

Postby kagein » 13 Nov 2020

Thank you, that was exactly the problem.


Return to “MultiCharts”