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

Postby brianhclo » 09 Nov 2014

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[30](0),
dayOpen[30](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[1] ), NumDays, 1 ) ;

OZ Trade
Posts: 39
Joined: 08 Nov 2013
Has thanked: 11 times
Been thanked: 18 times

Re: Coding a function to calculate volatility

Postby OZ Trade » 09 Nov 2014

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[30](0),
dayOpen[30](0);

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

Value1 = var0 * StandardDev( Log( OpenD(0) / dayClose[1] ), 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

Postby brianhclo » 09 Nov 2014

Thanks for looking into this.

My code compiles too, I dont think the changes solve the problem.

OZ Trade
Posts: 39
Joined: 08 Nov 2013
Has thanked: 11 times
Been thanked: 18 times

Re: Coding a function to calculate volatility

Postby OZ Trade » 09 Nov 2014

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

Code: Select all

BL_CO_VOL = var0 * StandardDev( Log( OpenD(0) / dayClose[1] ), 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[1] );

If LastBarOnChart then Print(OpenD(0) / dayClose[1] );

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

brianhclo
Posts: 31
Joined: 31 Mar 2014
Has thanked: 8 times

Re: Coding a function to calculate volatility

Postby brianhclo » 09 Nov 2014

OZ Trade,

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

Anyone has any ideas?

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

Code: Select all

BL_CO_VOL = var0 * StandardDev( Log( OpenD(0) / dayClose[1] ), 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[1] );

If LastBarOnChart then Print(OpenD(0) / dayClose[1] );

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

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

Re: Coding a function to calculate volatility

Postby TJ » 09 Nov 2014

OZ Trade,
I am able to get a value of Log(OpenD(0)/dayClose[1]), 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[1] ); BL_CO_VOL = sqrt260 * StandardDev( Logz, NumDays, 1 ) ;

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

Re: Coding a function to calculate volatility

Postby TJ » 09 Nov 2014

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

eg. dayClose[1] <= 0.

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

Re: Coding a function to calculate volatility

Postby Smarty » 12 Feb 2018

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: 223
Joined: 13 Jan 2016
Has thanked: 8 times
Been thanked: 53 times

Re: Coding a function to calculate volatility

Postby Zheka » 12 Feb 2018

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[1]>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: 31
Joined: 05 Nov 2013
Has thanked: 11 times
Been thanked: 1 time

Re: Coding a function to calculate volatility

Postby Smarty » 12 Feb 2018

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: 223
Joined: 13 Jan 2016
Has thanked: 8 times
Been thanked: 53 times

Re: Coding a function to calculate volatility

Postby Zheka » 13 Feb 2018

Code: Select all

Inputs: NumDays(0);

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

Array: dayClose[30](0), dayOpen[30](0);

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

if dayClose[1]>0 then
begin
value2=Log( OpenD(0) /dayClose[1]);
Value1 = var0 * StandardDev(value2, NumDays, 1 ) ;
end;

plot1(value1);

This works for me.

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

Re: Coding a function to calculate volatility

Postby Smarty » 13 Feb 2018

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: 31
Joined: 05 Nov 2013
Has thanked: 11 times
Been thanked: 1 time

Re: Coding a function to calculate volatility

Postby Smarty » 26 Feb 2018

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[1] <> 0 then value = supVal/infVal[1];

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

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

Re: Coding a function to calculate volatility

Postby TJ » 26 Feb 2018

GIGO

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

Re: Coding a function to calculate volatility

Postby Smarty » 26 Feb 2018

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

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

Re: Coding a function to calculate volatility

Postby TJ » 26 Feb 2018

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: 31
Joined: 05 Nov 2013
Has thanked: 11 times
Been thanked: 1 time

Re: Coding a function to calculate volatility

Postby Smarty » 26 Feb 2018

No ... compiling is without error ...

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

amw_775
Posts: 21
Joined: 19 Apr 2020
Has thanked: 1 time
Been thanked: 11 times

Re: Coding a function to calculate volatility

Postby amw_775 » 02 Mar 2023

Did you figure this out ?

I was facing the similar error. You need to add a "if condition" to check that the value of whose log you need to take is greater than 0.

For example , if you wanna calculate the Log of Volume:

Code: Select all

var:value1(0); if Volume>0 then value1 = log(Volume);


Return to “MultiCharts”