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
MovAvg.png (307.04 KiB) Viewed 158 times

User avatar
TJ
Posts: 6572
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 968 times
Been thanked: 1902 times

Re: close of data2 issue

Postby TJ » 17 Jan 2012

dblend wrote: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: 633
Joined: 09 Apr 2010
Location: Colorado
Has thanked: 394 times
Been thanked: 236 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);
These users thanked the author whammer for the post:
JoshM


Return to “MultiCharts”