close of data2 issue

Questions about MultiCharts and user contributed studies.
dblend
Posts: 16
Joined: 11 Jul 2011
Been thanked: 1 time

close of data2 issue

Postby dblend » 17 Jan 2012

Hi,

I have written a very basic indicator that calculates the midprice as the average of the bid (data2) and ask price (data3). it then calculates the simple moving average. See code:

Code: Select all

vars: intrabarpersist PxRef(0),
intrabarpersist PxRef1(0),
intrabarpersist BidData(0),
intrabarpersist AskData(0);


inputs: MovAvgLength(360);

BidData = close of data2;
AskData = close of data3;

PxRef = average((BidData + AskData) / 2, MovAvgLength);
PxRef1 = average((close of data2 + close of data3) / 2, MovAvgLength);

plot1(PxRef);
plot2(PxRef1);
The result i get shows 2 moving average lines (red and blue) which are different to each other (see attachment).
My question is why is PxRef different to PxRef1 and which method is the correct method.

p.s. i did try remove the intrabarpersist to all variable and still received the same result.

Thanks for the Help.
Attachments
MovAvg.png
Mov Avg
(307.04 KiB) Downloaded 404 times

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: close of data2 issue

Postby TJ » 17 Jan 2012

Hi,

I have written a very basic indicator that calculates the midprice as the average of the bid (data2) and ask price (data3). it then calculates the simple moving average. See code:

Code: Select all

vars: intrabarpersist PxRef(0),
intrabarpersist PxRef1(0),
intrabarpersist BidData(0),
intrabarpersist AskData(0);


inputs: MovAvgLength(360);

BidData = close of data2;
AskData = close of data3;

PxRef = average((BidData + AskData) / 2, MovAvgLength);
PxRef1 = average((close of data2 + close of data3) / 2, MovAvgLength);

plot1(PxRef);
plot2(PxRef1);
The result i get shows 2 moving average lines (red and blue) which are different to each other (see attachment).
My question is why is PxRef different to PxRef1 and which method is the correct method.

p.s. i did try remove the intrabarpersist to all variable and still received the same result.

Thanks for the Help.
see post #5
viewtopic.php?f=16&t=6929

dblend
Posts: 16
Joined: 11 Jul 2011
Been thanked: 1 time

Re: close of data2 issue

Postby dblend » 17 Jan 2012

Thanks TJ,

How would one code for the scenario of using data2 and data3 in the same moving average.

MA2 = average((close of data2 + close of data3) / 2, Length2);

using

MA2 = Average( close, Length2 ) data2 ;

sptrader
Posts: 742
Joined: 09 Apr 2010
Location: Texas
Has thanked: 483 times
Been thanked: 274 times
Contact:

Re: close of data2 issue

Postby sptrader » 17 Jan 2012

maybe do the averages separately, then add them and smooth them. {might be easier and less confusing}

Code: Select all


MA2 = close data2 ;
MA3 = close data3 ;
Mid = (ma2+ma3)/2;
ave = average(Mid,Len2);

whammer
Posts: 33
Joined: 14 Aug 2010
Has thanked: 8 times
Been thanked: 4 times

Re: close of data2 issue

Postby whammer » 23 Jan 2012

Try using a variable in between.

Var = (close data2+close data3)/2;

var2 = average(Var, Length);


Return to “MultiCharts”