Moving Average Calculation not correct on multiple data stream chart  [SOLVED]

Questions about MultiCharts and user contributed studies.
PD Quig
Posts: 191
Joined: 27 Apr 2010
Location: San Jose
Has thanked: 67 times
Been thanked: 10 times

Moving Average Calculation not correct on multiple data stream chart

Postby PD Quig » 02 Nov 2018

I have signal code that is running on a multiple data stream chart (3R, 8R, and 5-min bars). For reasons I cannot see, the signal is not calculating the correct values for an exponential moving average calculation on the 5-min (data3) bars. That is, it does not produce the same results that an exponential moving average indicator does when applied to the 5-min data stream. I've tried many different code tweaks and still cannot get the signal code to replicate the indicator values. It must be something pretty fundamental, so I'm hoping other sets of eyes will see what I've missed. Could the mix of minute and non-minute data types cause this?

The picture below is the last several rows that the indicator produced. These are correct and match the chart plot.
Indicator_MA_calculation_results.png
(15.68 KiB) Downloaded 997 times
The picture below is the last several rows that the signal produced. These do not match the chart plot.
Signal_MA_calculation_results.png
(16.23 KiB) Downloaded 998 times
I've attached a workspace with two charts: 1) one data stream [5-min] and 2) three data streams [3R, 8R, and 5-min]. Here is the three data stream code stripped down to minimum:

Code: Select all

{*************************************************************************************
Name : $test_MA signal
Description : test MA signal calcs on a 3 data stream chart
Last Modified Date : 11/02/2018
*************************************************************************************}

{************************************************************************************
variable initialization section
*************************************************************************************}

inputs:
MA_Length (20);

variables:

intrabarpersist MA (0, data3);


{*************************************************************************************
5-min MA calculations
*************************************************************************************}

if barstatus(3) = 2 then begin

MA = XAverage(close data3, MA_Length);
end;


{*************************************************************************************
diagnostic print section
*************************************************************************************}

once ClearPrintLog;

if barstatus(3) = 2 then begin

print("MA",
" 5-min bar= ", barnumber data3 + MaxBarsBack:5:0,
" close data3= ", close data3:6:1,
" MA= ", MA:7:2,
" MA[1]= ", MA[1]:7:2,
" MA[2]= ", MA[2]:7:2,
" MA[3]= ", MA[3]:7:2
);
end;
Here's the same code edited to apply to the single data stream chart:

Code: Select all

{*************************************************************************************
Name : $test_MA
Description : test MA signal calcs on a single data stream chart
Last Modified Date : 11/02/2018
*************************************************************************************}

{*************************************************************************************
variable initialization section
*************************************************************************************}

inputs:
MA_Length (20);

variables:
intrabarpersist MA (0);

{************************************************************************************
5-min MA calculations
************************************************************************************}

MA = XAverage(close, MA_Length);

{************************************************************************************
diagnostic print section
************************************************************************************}

once ClearPrintLog;

print("MA",
" 5-min bar= ", barnumber data1 + MaxBarsBack:5:0,
" close data1= ", close data1:6:1,
" MA= ", MA:7:2,
" MA[1]= ", MA[1]:7:2,
" MA[2]= ", MA[2]:7:2,
" MA[3]= ", MA[3]:7:2
);
Attachments
MA Calc Test.wsp
(309.6 KiB) Downloaded 446 times

MAZINGUER
Posts: 75
Joined: 06 Jan 2017
Has thanked: 9 times
Been thanked: 25 times

Re: Moving Average Calculation not correct on multiple data stream chart

Postby MAZINGUER » 03 Nov 2018

Perhaps it is because when you are using the word intrabarpersist along with barstatus = 2 (bar closing tick), what you are doing is calculating the MA of the MA_Length last ticks and not of the MA_Length last bars of 5 minutes.
I think that in your indicator you do not need to use the word barstatus along with the word intrabarpersist
This is what I can think of.
regards

PD Quig
Posts: 191
Joined: 27 Apr 2010
Location: San Jose
Has thanked: 67 times
Been thanked: 10 times

Re: Moving Average Calculation not correct on multiple data stream chart

Postby PD Quig » 03 Nov 2018

Hi MAZINGUER,

That was one of the thoughts that I had--and tried--before posting. It makes no difference. Here's with intrabarpersist:
MA_calculation_with_intrabarpersist.png
(16 KiB) Downloaded 967 times
And here's without:
MA_calculation_without_intrabarpersist.png
(16.04 KiB) Downloaded 967 times
I appreciate your comment.

-pdq

MAZINGUER
Posts: 75
Joined: 06 Jan 2017
Has thanked: 9 times
Been thanked: 25 times

Re: Moving Average Calculation not correct on multiple data stream chart

Postby MAZINGUER » 03 Nov 2018

PD Quing,
Please, do the same test by removing the word barstatus

PD Quig
Posts: 191
Joined: 27 Apr 2010
Location: San Jose
Has thanked: 67 times
Been thanked: 10 times

Re: Moving Average Calculation not correct on multiple data stream chart

Postby PD Quig » 03 Nov 2018

Code: Select all

{************************************************************************************
variable initialization section
*************************************************************************************}

inputs:
MA_Length (20);

variables:

// intrabarpersist MA (0, data3);
MA (0, data3);


{************************************************************************************
5-min MA calculations
*************************************************************************************}

// if barstatus(3) = 2 then begin

MA = XAverage(close data3, MA_Length);
// end;


{************************************************************************************
diagnostic print section
*************************************************************************************}

once ClearPrintLog;

//if barstatus(3) = 2 then begin

print("MA",
" 5-min bar= ", barnumber data3 + MaxBarsBack:5:0,
" close data3= ", close data3:6:1,
" MA= ", MA:7:2,
" MA[1]= ", MA[1]:7:2,
" MA[2]= ", MA[2]:7:2,
" MA[3]= ", MA[3]:7:2
);
//end;
Commented out barstatus(3) = 2. Different results but still incorrect:

MAZINGUER
Posts: 75
Joined: 06 Jan 2017
Has thanked: 9 times
Been thanked: 25 times

Re: Moving Average Calculation not correct on multiple data stream chart  [SOLVED]

Postby MAZINGUER » 03 Nov 2018

The mistake has to be some nonsense.
Please add the corresponding data flow (data 3) to the end of the declaration and not after the close.
I mean...
MA = XAverage(close, MA_Length) data3 ;

It is also convenient that the data flow with smaller TF is the data1

PD Quig
Posts: 191
Joined: 27 Apr 2010
Location: San Jose
Has thanked: 67 times
Been thanked: 10 times

Re: Moving Average Calculation not correct on multiple data stream chart

Postby PD Quig » 03 Nov 2018

DANG! That did it! I tried several permutations of using "(3)" for data3, etc., without having tried that one. I'll need to do more extensive data analysis, but on first inspection of a random dozen 5-min bars it appears that your solution works.

I've spent way too many hours messing around with what should have been a simple task. You made my weekend! Thanks, MAZINGUER!

-pdq

MAZINGUER
Posts: 75
Joined: 06 Jan 2017
Has thanked: 9 times
Been thanked: 25 times

Re: Moving Average Calculation not correct on multiple data stream chart

Postby MAZINGUER » 03 Nov 2018

:D

PD Quig
Posts: 191
Joined: 27 Apr 2010
Location: San Jose
Has thanked: 67 times
Been thanked: 10 times

Re: Moving Average Calculation not correct on multiple data stream chart

Postby PD Quig » 04 Nov 2018

BTW, MAZINGUER: I forgot to mention yesterday that I was also using the Linear Regression Curve function in my code. The syntax that I used for that was the same as my original syntax for the MA:

LRC = LinearRevValue(close data2, Length, 0);

Strangely enough, this actually works with that function. Your suggested syntax works too:

LRC = LinearRevValue(close, Length, 0) data2;

Since the linear regression curve function produced the expected results with the "data2" declaration inside the parentheses, I likely would never have tried your suggested permutation. It is--as you pointed out yesterday--truly "nonsense." Syntax that works both ways for some functions but not for others is pretty irritating.

Thanks again.

MAZINGUER
Posts: 75
Joined: 06 Jan 2017
Has thanked: 9 times
Been thanked: 25 times

Re: Moving Average Calculation not correct on multiple data stream chart

Postby MAZINGUER » 05 Nov 2018

Hi PD Quig,
That's right ... sometimes it is true that it can work by putting the data where we want the calculations inside the parentheses, but in the end it is a programming error because the data where we want the calculations must go to the end of the statement.
Please read the post 5 in this thread created by the user TJ:

viewtopic.php?t=6929

On the other hand, in your code you use the value of MA in the three previous bars [1], [2] and [3] but I am left with the intrigue of what it means: 7: 2.
Surely it is a programming resource that I do not know and I would appreciate it if you could tell me what it means, as a curiosity and to expand knowledge.
Thank you

PD Quig
Posts: 191
Joined: 27 Apr 2010
Location: San Jose
Has thanked: 67 times
Been thanked: 10 times

Re: Moving Average Calculation not correct on multiple data stream chart

Postby PD Quig » 05 Nov 2018

Glad to share: The :7:2 is simply a specified output format for numeric data. Thus, "MA[1]:7:2" will result in the prior bar's MA value being output 7 digits wide with 2 decimal places.

Thanks for the reference to TJ's post. I had not seen that.

Best,

-pdq

MAZINGUER
Posts: 75
Joined: 06 Jan 2017
Has thanked: 9 times
Been thanked: 25 times

Re: Moving Average Calculation not correct on multiple data stream chart

Postby MAZINGUER » 07 Nov 2018

good detail that I did not know and was curious
Thanks for sharing, PD Quig !
regards


Return to “MultiCharts”