# Variable Moving Average Calculation  [SOLVED]

Questions about MultiCharts and user contributed studies.
earmarques
Posts: 13
Joined: 07 Dec 2013
Has thanked: 1 time
Been thanked: 1 time

### Variable Moving Average Calculation

Hi, I've tried to create a indicator for the VMA but without success, using the below formula:

Code: Select all

`VMA = (0.78*(volatility index) * close) + (1-0.078 * volatility index)*yesterday’s VMA`

For the volatility index I've choosed the CMO of length 9, function below:

Code: Select all

`Input: LENGTH(Numeric);CMO =100*((C-C[LENGTH])/ (SUMMATION(ABSVALUE((C-C[1])),LENGTH)));`

Then the complete formula for my VMA is:

Code: Select all

`VMA = (0.78*CMO(9) * close) + (1-0.078 * CMO(9))*VMA[1]`

This looks wrong? I put this formula and Multicharts64 return big numbers, far from a moving average. I put this formula in excel with the values of CMO(9) and don't return big numbers. Can anyone help me please?

And sorry by the bad english!

TJ
Posts: 7150
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 987 times
Been thanked: 2040 times

### Re: Variable Moving Average Calculation

earmarques wrote:::
And sorry by the bad english!

No apology needed for bad english; we are from all over the world.
These users thanked the author TJ for the post:
earmarques

TJ
Posts: 7150
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 987 times
Been thanked: 2040 times

### Re: Variable Moving Average Calculation

you can debug by breaking down all the variables and
printing out each value:

eg.

VMA1 = (0.78*(volatility index) * close)
VMA2 = (1-0.078 * volatility index)*yesterday’s VMA

plot CMO
plot VMA1
plot VMA2

plot VMA1+VMA2

earmarques
Posts: 13
Joined: 07 Dec 2013
Has thanked: 1 time
Been thanked: 1 time

### Re: Variable Moving Average Calculation

Yes, but the problem arrives when I reference the VMA[1]. When I change this to Close the number isn't so bigger. And I can't do this test without sum the two formulas:

Code: Select all

`VMA1 = (0.78*(volatility index) * close)VMA2 = (1-0.078 * volatility index)*VMA[1]<-------this part must be calculatedplot CMOplot VMA1plot VMA2plot VMA1+VMA2`

This is why this debug is so dificult. When I replace VMA[1] by Close is ok, but this variable must be calculated.

earmarques
Posts: 13
Joined: 07 Dec 2013
Has thanked: 1 time
Been thanked: 1 time

### Re: Variable Moving Average Calculation  [SOLVED]

T, thanks for the help. I made a search for this indy and found this:

Code: Select all

`Inputs: Price(c), Period(9), CMOPeriod(9); Variables: VI(0), factor(0), VMA(0);factor = 2/(Period+1);VI = AbsValue(CMO(Price, CMOPeriod)/100);if CurrentBar <= 1 then begin     VMA = Price; end else begin      VMA = (factor*VI*Price) + ((1-factor*VI)*VMA[1]); end;plot1(vma);`

It's perfect!
Thanks.

TJ
Posts: 7150
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 987 times
Been thanked: 2040 times

### Re: Variable Moving Average Calculation

Good. Thanks for letting us know.

TJ
Posts: 7150
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 987 times
Been thanked: 2040 times

### Re: Variable Moving Average Calculation

An easier way would be to initialize the VMA to Close.

after the first bar, VMA will be determined by your code.
This method works if you are only referencing one bar back;

Code: Select all

`Inputs: Price(c), Period(9), CMOPeriod(9); Variables: VI(0), factor(0), VMA( CLOSE );factor = 2/(Period+1);VI = AbsValue(CMO(Price, CMOPeriod)/100);VMA = (factor*VI*Price) + ((1-factor*VI)*VMA[1]); plot1(vma);`

filippo.milano
Posts: 42
Joined: 02 Jan 2015

### Re: Variable Moving Average Calculation

Sorry, but it's not working tis code...
Could you please post the correct code for VMA and also for CMO function code?