Announcement

Collapse
No announcement yet.

ema vs. MAStudy string conversion problem

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • ema vs. MAStudy string conversion problem

    I'm using the ema() function and I want to covert the value to a string. I have this code, but I get an error saying "vStr:has no properties."

    var vEMA = ema(10);
    var vTemp = vEMA.getValue(0);
    var vStr = vTemp.toString();

    If howerver, I use the non-efs MA study I receive no error message and the value of the moving average is now stored as a string.
    var vEMA = new MAStudy(10, 0, "Close", MAStudy.EXPONENTIAL);
    var vTemp = vEME.getValue(MAStudy.MA);
    var vStr = vTemp.toString();

    Is there a workaround the efs2 version to convert the value to a string?

  • #2
    Re: ema vs. MAStudy string conversion problem

    rebkwez,

    First, toString() is a method of the Number Object, and I believe relies upon the variable being defined. Try adding "" to the values versus using the toString() method.

    Second, defining these objects like you are is not a very efficient technique, check out the code in the EFS 2 Custom folder.

    Finally, try using the typeof() method to determine the type of the variables before you are getting the errors. This may help you figure out what is going on. For example:


    var vEMA = ema(10);
    var vTemp = vEMA.getValue(0);
    debugPrintln("1st: typeof(vTemp) = "+typeof(vTemp));
    var vStr = vTemp+"";
    debugPrintln("2nd: typeof(vTemp) = "+typeof(vTemp));

    Comment


    • #3
      rebkwez,

      What is happening is with ema it will return "null" until it has enough bars to compute the ema. In your case 10 bars. You can use an if statement to check if vEMA is not null before trying to convert the value to a string.

      You can also use the toFixed(n) method to convert a number to a string with n digits after the decimal. For example:

      var vStr = vTemp.toFixed(2);

      Steve

      Comment

      Working...
      X