August 9, 2008

Division by zero in MetaStock

    Today I tried to create a custom indicator in Metastock. I thought it will be fun & easy since I have a good programming background (Java, C/C++, Delphi, etc.) . I was terrribly wrong !

  Well, everything was cool, the small formula editor didn't bother me too much, until I came to the IF function. The synthax for IF according to the Metastock Help is: 

 if( condition, value when true, value when false) ;

I was a little disappointed about this approach but I was ready to get over it and continue my work. The big problem occured when I tried to display the value of a division and of course I had to take the necessary precautions in order not to divide by zero:

     if ( denominator <> 0 , numerator/denominator, 0) ;

Guess what ? .... although my denominator was checked, I was still getting a nice "Division by zero " error and not once but more than 100 times. Moreover, the indicator seemed to be properly displayed which made the finding of the error even more painful. 

After a while I realized that the parameters of any function have to be evaluated before being delivered to the function. So,  in a statement like : 

if( condition, expression_1, expression_2)  

both expression_1 and expression_2 will be evaluated no matter what the condition says.

You have to admit : This is really crap !

Did anybody hear about WealthLab?