Announcement

Collapse
No announcement yet.

Variable has no properties

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

  • Variable has no properties

    The following script stops working when I add .toFixed(2) (the error message states: TypeError: myVar0 has no properties). The same happens when I reassing myVar0 to vOpenTxt even when I declare the variables as global (both before and just after the "function main()" statement:

    var bInit = false;
    var aFPArray = new Array();

    function preMain() {
    setPriceStudy(true);
    var x=0;
    aFPArray[x] = new FunctionParameter( "vInterval", FunctionParameter.String);
    with( aFPArray[x++] ) {
    setName( "Interval" );
    setDefault( );
    }
    }
    var vOpenTxt =null;
    var myVar0;

    function main(vInterval){

    if ( bInit == false ) {
    if (vInterval == null) vInterval = getInterval();
    if(isNaN( vInterval )) vInterval = "\""+vInterval+"\"";
    myStudy0 = sma( 9, inv(eval(vInterval)) );

    // debugPrintln(vInterval);
    vInt = vInterval.toUpperCase()

    bInit = true;
    }
    myVar0 = myStudy0.getValue(0);
    vOpenTxt = myVar0.toFixed(2);

    drawTextRelative(3, myVar0, vOpenTxt , Color.black, Color.RGB(192,192,192), Text.BOLD|Text.LEFT|Text.VCENTER|Text.FRAME , "Eras Demi LTC", 10, "text1");
    return new Array( getSeries(myStudy0));
    }

    /*it also fails with this alternative last 2 lines of code:*/

    myVar0 = myStudy0.getValue(0);

    drawTextRelative(3, myVar0, myVar0.toFixed(2)+ " (9sma)" + "-" + vInt, Color.black, Color.RGB(192,192,192), Text.BOLD|Text.LEFT|Text.VCENTER|Text.FRAME , "Eras Demi LTC", 10, "text1");
    return new Array( getSeries(myStudy0));

    If anyone knows why I would appreciate your thoughts.

    Thanks
    Last edited by waynecd; 03-31-2008, 10:51 AM.

  • #2
    Re: Variable has no properties

    waynecd
    There are a few errors that I can see:
    - you omitted to declare myStudy0 as a global variable
    - before modifying the properties of a variable (in your case by using the toFixed() method) you need to run a null check on that variable eg
    if(myVar0==null) return;
    Once you have done that then you can apply the toFixed() method (see also this thread on the same subject).
    - your return statement is set to return an array but you are returning one item only
    Alex


    Originally posted by waynecd
    The following script stops working when I add .toFixed(2) (the error message states: TypeError: myVar0 has no properties). The same happens when I reassing myVar0 to vOpenTxt even when I declare the variables as global (both before and just after the "function main()" statement:

    var bInit = false;
    var aFPArray = new Array();

    function preMain() {
    setPriceStudy(true);
    var x=0;
    aFPArray[x] = new FunctionParameter( "vInterval", FunctionParameter.String);
    with( aFPArray[x++] ) {
    setName( "Interval" );
    setDefault( );
    }
    }
    var vOpenTxt =null;
    var myVar0;

    function main(vInterval){

    if ( bInit == false ) {
    if (vInterval == null) vInterval = getInterval();
    if(isNaN( vInterval )) vInterval = "\""+vInterval+"\"";
    myStudy0 = sma( 9, inv(eval(vInterval)) );

    // debugPrintln(vInterval);
    vInt = vInterval.toUpperCase()

    bInit = true;
    }
    myVar0 = myStudy0.getValue(0);
    vOpenTxt = myVar0.toFixed(2);

    drawTextRelative(3, myVar0, vOpenTxt , Color.black, Color.RGB(192,192,192), Text.BOLD|Text.LEFT|Text.VCENTER|Text.FRAME , "Eras Demi LTC", 10, "text1");
    return new Array( getSeries(myStudy0));
    }

    /*it also fails with this alternative last 2 lines of code:*/

    myVar0 = myStudy0.getValue(0);

    drawTextRelative(3, myVar0, myVar0.toFixed(2)+ " (9sma)" + "-" + vInt, Color.black, Color.RGB(192,192,192), Text.BOLD|Text.LEFT|Text.VCENTER|Text.FRAME , "Eras Demi LTC", 10, "text1");
    return new Array( getSeries(myStudy0));

    If anyone knows why I would appreciate your thoughts.

    Thanks

    Comment


    • #3
      Thanks Alexis,

      You are a great help.
      With your help and a few other changes it now works.

      I didn't change the "return" statement because nothing else I tried actually plots the sma line. Is there a better way?

      See attached.
      Attached Files
      Last edited by waynecd; 03-31-2008, 11:43 AM.

      Comment


      • #4
        waynecd
        If you are returning only one item then you should use return and not return new Array
        Note that while your formula will work if you are using an intraday interval it will return errors if you run it on a daily (or higher) interval or on a tick chart (ie on an interval that contains a letter). Run the Syntax Check on the script and you will see that it is returning an error in line 24. This is caused in the first place by a syntax error in the method used in the Function Parameter object which should be FunctionParameter.STRING and not FunctionParameter.String (Javascript is a case sensitive language).
        Then when you initialize the study you are using eval() to evaluate the vInterval string. The inv() function requires the parameter to be a string (if the interval contains any letters such as D or 100T, etc) so you should remove the eval().
        Alex


        Originally posted by waynecd
        Thanks Alexis,

        You are a great help.
        With your help and a few other changes it now works.

        I didn't change the "return" statement because nothing else I tried actually plots the sma line. Is there a better way?

        See attached.

        Comment


        • #5
          Alex,

          Thanks your post clears up the "return" issues I had.

          My sloppy work i.e. String vs. STRING.

          Comment


          • #6
            waynecd
            You are welcome
            Alex


            Originally posted by waynecd
            Alex,

            Thanks your post clears up the "return" issues I had.

            My sloppy work i.e. String vs. STRING.

            Comment

            Working...
            X