why my code somtimes return negative numbers? please help

 

hi friends. I'm new in coding. the following is part of my code, i want to caluculate the volatility in a certain period, i used Log() and Sqrt() to calculate the number. but sometimes, the outcome is negative, but it shall not be negative according to the formula. please help, i want to know why or anything wrong in my code. thanks.

*********

double valueofVolatility5Days(int i)

  double d1close=iClose(NULL,PERIOD_D1,i);

  double d2close=iClose(NULL,PERIOD_D1,i+1);  

  double d3close=iClose(NULL,PERIOD_D1,i+2);  

  double d4close=iClose(NULL,PERIOD_D1,i+3);  

  double d5close=iClose(NULL,PERIOD_D1,i+4);

  double d6close=iClose(NULL,PERIOD_D1,i+5);

  double vol;

     vol= (MathLog(d1close/d2close)+MathLog(d2close/d3close)+MathLog(d3close/d4close)+MathLog(d4close/d5close)+MathLog(d5close/d6close))/5;

  double std;

    std= sqrt(((MathLog(d1close/d2close)-vol)*(MathLog(d1close/d2close)-vol)

            +(MathLog(d2close/d3close)-vol)*(MathLog(d2close/d3close)-vol)

            +(MathLog(d3close/d4close)-vol)*(MathLog(d3close/d4close)-vol)

            +(MathLog(d4close/d5close)-vol)*(MathLog(d4close/d5close)-vol)

            +(MathLog(d5close/d6close)-vol)*(MathLog(d5close/d6close)-vol))/5

            );

  double av;

    av= std/vol/sqrt(0.2);

  return(NormalizeDouble(av,2));

}  

 

Your code is a bit messy and repetitive. Also, use the proper code formatting when creating a thread.


That said, MathLog returns the natural logarithm of a number. The natural logarithm is its logarithm to the base of the constant e. The constant e is approximately equal to  2.718281828459. If any number to the power of 0 is 1, then it's safe to say that if the division of closing prices results in a number lower than 1, the function will return a negative value (e to the power of this value shall result the division). That is why.


At last, if you allow me, try reducing the repetition in your code like this:


bool SetSeries(double& arr[])
{
    return ArraySetAsSeries(arr, true);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double valueofVolatility5Days(int i)
{
    static double close[]; // static array
    static bool check=ArraySetAsSeries(close, true); // set it to series only once

    CopyClose(_Symbol, PERIOD_D1, i, 6, close); // use CopyClose instead of multiple iClose; you should check if this function returns an error

    double log1 = MathLog(close[0]/close[1]); // store the result in a variable to use it multiple times
    double log2 = MathLog(close[1]/close[2]);
    double log3 = MathLog(close[2]/close[3]);
    double log4 = MathLog(close[3]/close[4]);
    double log5 = MathLog(close[4]/close[5]);

    double vol = (log1+log2+log3+log4+log5)/5; // assign the value when you declare a variable, if possible, to make the code cleaner and smaller
    double std = sqrt(((log1-vol)*(log1-vol)+(log2-vol)*(log2-vol)+(log3-vol)*(log3-vol)+(log4-vol)*(log4-vol)+(log5-vol)*(log5-vol))/5); // use the variables stored before

    double av = std/vol/sqrt(0.2);

    return(NormalizeDouble(av, 2));

}


You could also use the MathPow function instead of multiplying every value to get the standard deviation, but that's up to you.

 

thanks Emanuel. very helpful. i will study your suggestion, honestly, i'm not a qualified coder just have interests to it. again, thanks a lot. 

Emanuel Cavalcante Amorim Filho #:

Your code is a bit messy and repetitive. Also, use the proper code formatting when creating a thread.


That said, MathLog returns the natural logarithm of a number. The natural logarithm is its logarithm to the base of the constant e. The constant e is approximately equal to  2.718281828459. If any number to the power of 0 is 1, then it's safe to say that if the division of closing prices results in a number lower than 1, the function will return a negative value (e to the power of this value shall result the division). That is why.


At last, if you allow me, try reducing the repetition in your code like this:



You could also use the MathPow function instead of multiplying every value to get the standard deviation, but that's up to you.

 

ln(1) = 0,ln(e) = 1,ln(0.5) = -0.6931


i got it. my bad math theory. now i know the result of this function is still reliable. 

Reason: