Announcement

Collapse
No announcement yet.

Error: no properties of an array

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

  • Error: no properties of an array

    My following formula script gives syntax error:

    ======
    function preMain()
    {
    setStudyTitle("ndMA_SL");
    setCursorLabelName("ndMA_SL",0);
    setDefaultBarStyle(PS_SOLID,0);
    setDefaultBarFgColor(Color.red,0);
    setDefaultBarThickness(1,0);
    }
    function main()
    {
    var LX = new Array(0,0,0,0);
    LX = call("ndMA_L_3pt2[3].efs",0.01);
    return LX[3];
    }
    ===========

    Syntax error is "LX has no properties". Note that function
    ("ndMA_L_3pt2[3].efs" returns an array of 4 elements. If I replace the last statement with

    "return LX"

    then my script working fine, returning 4 values. I, however, want my script to return only the fourth elememt of the LX array.

    Please tell me what to do and how to fix this error. Thanks.

    Name: dzung nguyen
    Email: [email protected]

  • #2
    Post the code for ndMA_L_3pt2[3].efs because the problem is in the line that calls it. Meanwhile you can try something like this:

    var LX = new Array(0,0,0,0);
    var ndMA_L_3pt2 = call("ndMA_L_3pt2.efs",0.01);

    for (i=0; i<4; i++) {
    LX[i] = ndMA_L_3pt2[i]
    }

    I commented out: LX = call("ndMA_L_3pt2[3].efs",0.01);
    and replaced it with OHLC values. Since you only want the last element this returns LX[3] which is the close.

    PHP Code:
    function preMain() {
    setStudyTitle("ndMA_SL");
    setCursorLabelName("ndMA_SL",0);
    setDefaultBarStyle(PS_SOLID,0);
    setDefaultBarFgColor(Color.red,0);
    setDefaultBarThickness(1,0);
    }

    function 
    main() {
    var 
    LX = new Array(0,0,0,0);

    // LX = call("ndMA_L_3pt2[3].efs",0.01);
    LX[0] = open();
    LX[1] = high();
    LX[2] = low();
    LX[3] = close();

    return 
    LX[3];

    Comment


    • #3
      Hi,


      My guess is that:

      call("ndMA_L_3pt2[3].efs",0.01);

      Doesn't return 4 elements for each bar on the chart. I suspect that on the oldest bars it might return no elements or at least less than 3.

      Do a check for LX's = null and LX length's.

      If (LX == null)
      return;
      if (LX.length < 4)
      return;

      and see if that doesn't fix it.

      G
      Garth

      Comment


      • #4
        Hi xygeek and Garth,

        Thanks for your suggestions.
        I should define the problem more clearly as follows. I include 2 efs: TestCall.efs GetOHLC.efs

        GetOHLC.efs:

        function preMain()
        {
        setStudyTitle("GetOHLC");
        }

        function main(D)
        {
        if (D == null) D = 0;
        if (D > 0) return "D must be negative (for a previous bar) or 0 (for current bar)";
        var O = open(D);
        var H = high(D);
        var L = low(D);
        var C = close(D);
        return new Array (O,H,L,C);
        }

        TestCall

        function preMain() {
        setStudyTitle("TestCall");
        }

        function main() {
        var LX = new Array(0,0,0,0);
        LX = call("GetOHLC.efs",-2);
        return LX[3];
        }

        In the above example, TestCall supposes to return LX[3], Close value of the bar 2 interval ago. But What I got is a syntax error "LX has no properties".

        If I replace the last statement of TestCall.efs with

        return LX

        then TestCall is working fine (ie no error) and returns Open, High, Low and Close of the bar 2 intervals ago. So surely, LX is an array consting of 4 elements. But I cannot return one of its element (LX[1] or LX[2] or ...)

        Dzung

        Comment


        • #5
          Hello Dzung,

          You just need to perform a null check right after "LX = call();" and you'll be set.

          PHP Code:
          function main() {
              var 
          LX = new Array(0,0,0,0);
              
          LX call("GetOHLC.efs",-2);
              if (
          LX == null) return;              // null check
              
          return LX[3];

          The problem is that when the formula begins processing, it starts on the oldest bar in your chart. At that point, bar -2, or two bars prior don't exist. Therefore your call() statement sets LX equal to null and generates the error. By performing the null check, you ensure that nothing happens until you have valid data for your LX array.
          Jason K.
          Project Manager
          eSignal - an Interactive Data company

          EFS KnowledgeBase
          JavaScript for EFS Video Series
          EFS Beginner Tutorial Series
          EFS Glossary
          Custom EFS Development Policy

          New User Orientation

          Comment


          • #6
            Jason,

            Exactly but I would also add, as I stated originally, I would also test for the array length. When practicing good programming, you should always verify that a return from a call is the size you think it should be. In the case of OHLC it may not be so important, but the fact that originally they were calling a function called "ndMA_L_3pt2[3].efs", makes me think that they may be filling an array with MA values - so the return may not be null, but also may not have 4 elements in it. Also consider what would happen if eSignal modifed OHLC in some manner.

            In general if you do things like:

            return LX[3];
            y = LX[3];
            or even
            if (LX[3] > 0){

            You should verify at some prior time that LX has at least a length of 4.

            if (LX.length < 4)
            return;

            Garth
            Garth

            Comment


            • #7
              Hi Jason,

              It's fixed. Thanks.

              Dzung

              Comment

              Working...
              X