# Coding a function to calculate volatility

Questions about MultiCharts and user contributed studies. brianhclo
Posts: 31
Joined: 31 Mar 2014
Has thanked: 8 times

### Coding a function to calculate volatility

HI MC Experts. I am new to MC, I would like to code a function but ran into some problems.

What I need: I would like to calculate the Standard Deviation of overnight returns on a 1 minute chart. i.e. For 3 days, Vol = Stdev( ln(Open(today)/Close(t-1)), ln(Open(t-1)/Close(t-2)), ln(Open(t-2)/Close(t-3)) ) * sqrt(260)

I took the code from VolatilityStdDev and clone it to make the following. It compiles OK however I get an error "Message: Error in study: Logarithm error." when plotting the function on a 1 minute chart.

Can someone point out the mistakes I made?

Code: Select all

```inputs: NumDays( numericsimple ) ; variables: var0( SquareRoot( 260 ) ) , j(0); Array: dayClose(0), dayOpen(0); for j=0 to NumDays-1 begin dayClose[j] = closeD(j); dayOpen[j] = OpenD(j); end; BL_CO_VOL = var0 * StandardDev( Log( OpenD(0) / dayClose ), NumDays, 1 ) ;``` Posts: 39
Joined: 08 Nov 2013
Has thanked: 11 times
Been thanked: 18 times

### Re: Coding a function to calculate volatility

Corrections to the declaration of Input and Variable code and changing 'BL_CO_VOL' to Value1 allowed me to compile the study
Inputs: NumDays(0);

Vars: var0(SquareRoot( 260 )) , j(0);

Array:
dayClose(0),
dayOpen(0);

for j=0 to NumDays-1 begin
dayClose[j] = closeD(j);
dayOpen[j] = OpenD(j);
end;

Value1 = var0 * StandardDev( Log( OpenD(0) / dayClose ), NumDays, 1 ) ;

I have no idea if the study is going to perform as expected, however brianhclo
Posts: 31
Joined: 31 Mar 2014
Has thanked: 8 times

### Re: Coding a function to calculate volatility

Thanks for looking into this.

My code compiles too, I dont think the changes solve the problem. Posts: 39
Joined: 08 Nov 2013
Has thanked: 11 times
Been thanked: 18 times

### Re: Coding a function to calculate volatility

The StandardDev function does not like the value of Log( OpenD(0) / dayClose)

Code: Select all

``` BL_CO_VOL = var0 * StandardDev( Log( OpenD(0) / dayClose ), NumDays, 1 ) ; ```
Put a double forward slash // in front of BL_CO_VOL and using the last bar on the chart as an example investigate these in the output window:

If LastBarOnChart then Print(OpenD(0));

If LastBarOnChart then Print(dayClose );

If LastBarOnChart then Print(OpenD(0) / dayClose );

If LastBarOnChart then Print(Log( OpenD(0) / dayClose) ); brianhclo
Posts: 31
Joined: 31 Mar 2014
Has thanked: 8 times

### Re: Coding a function to calculate volatility

I am able to get a value of Log(OpenD(0)/dayClose), they output just fine.

Anyone has any ideas?

The StandardDev function does not like the value of Log( OpenD(0) / dayClose)

Code: Select all

``` BL_CO_VOL = var0 * StandardDev( Log( OpenD(0) / dayClose ), NumDays, 1 ) ; ```
Put a double forward slash // in front of BL_CO_VOL and using the last bar on the chart as an example investigate these in the output window:

If LastBarOnChart then Print(OpenD(0));

If LastBarOnChart then Print(dayClose );

If LastBarOnChart then Print(OpenD(0) / dayClose );

If LastBarOnChart then Print(Log( OpenD(0) / dayClose) );

TJ
Posts: 7191
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 990 times
Been thanked: 2049 times

### Re: Coding a function to calculate volatility

I am able to get a value of Log(OpenD(0)/dayClose), they output just fine.
Anyone has any ideas?
::
KISS

Do one thing at a time... do not lump all the calculations in one indecipherable strand.

try this...

Code: Select all

``` sqrt260 = SquareRoot( 260 ); logz = Log( OpenD(0) / dayClose ); BL_CO_VOL = sqrt260 * StandardDev( Logz, NumDays, 1 ) ; ```

TJ
Posts: 7191
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 990 times
Been thanked: 2049 times

### Re: Coding a function to calculate volatility

ps. make sure you do not have any bad data.

eg. dayClose <= 0. Smarty
Posts: 29
Joined: 05 Nov 2013
Has thanked: 11 times
Been thanked: 1 time

### Re: Coding a function to calculate volatility

The error is regardless, even if the execution does not pass from the logarithm calculation instruction. The error is serious, please check support Zheka
Posts: 191
Joined: 13 Jan 2016
Has thanked: 8 times
Been thanked: 44 times

### Re: Coding a function to calculate volatility

The error is not "regardless". Follow TJ's advice.

1) StandardDev needs a "series" as input. Just Log() is not, but a variable - in MC - is.
2) calculate Log if dayClose>0.

I am not 100% clear why 1) passes by the compiler and why 2) is not handled by closeD() under the hood, but thats the way it is.
Last edited by Zheka on 13 Feb 2018, edited 1 time in total. Smarty
Posts: 29
Joined: 05 Nov 2013
Has thanked: 11 times
Been thanked: 1 time

### Re: Coding a function to calculate volatility

No .... sorry Zheka ... it's sure.

After this anomaly, I try to put "print" everywhere, but it's no possible to do not even apply step-by-step debugging.

It was a numberseries in input at a function, whose first occurrence had zero value (the oldest). it was useless that I have filled with controls around to avoid values below zero. I also tried to write a function NaturalLog, simply delegating any call to Log(x) ... Nothing to do.

I can assure that execution don't pass by that instruction it is sufficient that the log is present in the function. It's an error of interpreter.

it's an anomaly diffcult to reproduce, I try to upload a code in next days. I hope somebody of support check about it.

Ciao Zheka, hi for all, Sabino. Zheka
Posts: 191
Joined: 13 Jan 2016
Has thanked: 8 times
Been thanked: 44 times

### Re: Coding a function to calculate volatility

Code: Select all

```Inputs: NumDays(0); Vars: var0(SquareRoot( 260 )) , j(0); Array: dayClose(0), dayOpen(0); for j=0 to NumDays-1 begin dayClose[j] = CloseD(j); dayOpen[j] = OpenD(j); end; if dayClose>0 then begin value2=Log( OpenD(0) /dayClose); Value1 = var0 * StandardDev(value2, NumDays, 1 ) ; end; plot1(value1);```

This works for me. Smarty
Posts: 29
Joined: 05 Nov 2013
Has thanked: 11 times
Been thanked: 1 time

### Re: Coding a function to calculate volatility

for me too .... but it's no my situation .... soon I will post a complete example ... may be you can see what I am not able ...  Smarty
Posts: 29
Joined: 05 Nov 2013
Has thanked: 11 times
Been thanked: 1 time

### Re: Coding a function to calculate volatility

I have isolated the problem on the interpreter, which also generates the error on the logarithm. I will open post as "anomaly of interpreter".

This is indicator:

Code: Select all

```inputs: calcMode(0), dataSeries1(1), dataSeries2(2); vars: xSeries(0), ySeries(0), calc(0); xSeries = Close of data(dataSeries1); ySeries = Close of data(dataSeries2); if calcMode = 1 then calc = fun(calcMode,xSeries,ySeries) else if calcMode = 2 then calc = fun(calcMode,xSeries,ySeries) else if calcMode = 3 then calc = fun(calcMode,xSeries,ySeries) else if calcMode = 4 then calc = fun(calcMode,xSeries,ySeries) else if calcMode = 5 then calc = fun(calcMode,xSeries,ySeries) else if calcMode = 6 then calc = fun(calcMode,xSeries,ySeries) else if calcMode = 7 then calc = fun(calcMode,xSeries,ySeries) else if calcMode = 8 then calc = fun(calcMode,xSeries,ySeries); if LastBarOnChart then Print(ELDateToString(date),", fun ... calcMode: ",calcMode,", calc: ",calc); Plot3(calc,"calcolo"); ```
And this is the function:

Code: Select all

```inputs: calcMode(numericsimple), xSeries(numericseries), ySeries(numericseries); vars: double supVal(0.0), double infVal(0.0), value(0); supVal = xSeries; infVal = ySeries; If infVal <> 0 then value = supVal/infVal; if LastBarOnChart = true then Print("fun: calcMode: ",calcMode); fun = value;```
final effect is this:

Code: Select all

```fun: calcMode: 4.00 02/23/2018, switch_fun ... calcMode: 4.00, calc: 4.80 fun: calcMode: 4.00 fun: calcMode: 4.00 fun: calcMode: 4.00 fun: calcMode: 4.00 fun: calcMode: 4.00 fun: calcMode: 4.00 fun: calcMode: 4.00 ```
the execution flow is completely indifferent to the condition "if then else", then executes all the branches of the condition. Same effect also using switch construct.

I hope support can solve fastly.

Regards, Smarty

TJ
Posts: 7191
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 990 times
Been thanked: 2049 times

### Re: Coding a function to calculate volatility

GIGO Smarty
Posts: 29
Joined: 05 Nov 2013
Has thanked: 11 times
Been thanked: 1 time

### Re: Coding a function to calculate volatility

Hi TJ ... of course it's no question of intelligence of the indicator calculation.

I have isolated everything in easy instructions to make easy the understand that the flow is wrong, no matter what do indicator.

Of course ...

Ciao

TJ
Posts: 7191
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 990 times
Been thanked: 2049 times

### Re: Coding a function to calculate volatility

Hi TJ ... of course it's no question of intelligence of the indicator calculation.

I have isolated everything in easy instructions to make easy the understand that the flow is wrong, no matter what do indicator.

Of course ...

Ciao

BUT... do you know the value of your variables at every stage?
If you haven't done an audit, how can you say it is a compiler problem? Smarty
Posts: 29
Joined: 05 Nov 2013
Has thanked: 11 times
Been thanked: 1 time

### Re: Coding a function to calculate volatility

No ... compiling is without error ...

The sample is simple .... very much simple ... and now I write a more simpe version ...