Announcement

Collapse
No announcement yet.

TypeError: variable has no properties

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

  • TypeError: variable has no properties

    I am performing the null check but it still does not work. I am trying to combine a triangular moving average study with a bar colouring study.

    I am getting the error on line 17 (var vLastTMA =...)

    Any help would be much appreciated.

    function preMain() {
    setPriceStudy(true);
    setStudyTitle("MA Price Bars");
    setCursorLabelName("TMAPB");
    setColorPriceBars(true);
    setDefaultPriceBarColor(Color.black);

    var fp1 = new FunctionParameter("nInputLength", FunctionParameter.NUMBER);
    fp1.setName("Length");
    fp1.setLowerLimit(1);
    fp1.setDefault(15);
    }

    function main(nInputLength) {
    var nMA = triMA(nInputLength, 0);
    var vClose = close(0);
    var vLastTMA = nMA.getValue(MAStudy.MA,-1);

    if(nMA == null || vClose == null || vLastTMA == null) return;

    if((nMA > vClose) && (nMA < vLastTMA)) {
    setPriceBarColor(Color.red);
    }
    if ((nMA < vClose) && (nMA > vLastTMA)) {
    setPriceBarColor(Color.green);
    }

    return nMA;
    }

    function triMA(Length, Offset)
    {
    var MAvg = null;
    var TMA = null;

    var vTMA = 0.0;

    var vMAvg = 0.0;
    var Len1 = 0.0;
    var Len2 = 0.0;
    var Len = 0.0;
    var LenRnd = 0.0;

    Len = Length / 2;
    LenRnd = Math.round(Len);
    if (LenRnd != Len )
    {
    Len1 = LenRnd;
    Len2 = LenRnd;
    }
    if (LenRnd == Len )
    {
    Len1 = LenRnd;
    Len2 = LenRnd + 1;
    }

    if (MAvg == null) MAvg = new MAStudy(Len1, Offset, "OHLC/4", MAStudy.SIMPLE);
    if(MAvg.getValue(MAStudy.MA) == null) return;
    if (TMA == null) TMA = new MAStudy(Len2, 0, MAvg, MAStudy.MA, MAStudy.SIMPLE);
    vTMA = TMA.getValue(MAStudy.MA);

    return (vTMA);

    }
    Last edited by bf2; 01-17-2008, 10:52 PM.

  • #2
    Re: TypeError: variable has no properties

    bf2
    The reason you are getting an error is because the variable nMA is not a series but a single value hence you cannot use the .getValue() method to retrieve its values (see the link to the article in the EFS KnowledgeBase for a description of the Series Object and its methods)
    The simplest solution to your problem is to use the efsInternal() function to call the triMA() function (see the link to the article in the EFS KnowledgeBase for the description and syntax of the function). This will create the series which will allow you to then retrieve its values.
    As a first step replace the following line in your script
    var nMA = triMA(nInputLength, 0);
    with
    var nMA = efsInternal("triMA", nInputLength, 0);
    The variable nMA is now a series so you can use the .getValue() method to retrieve its values. The syntax however is slightly different than the one you used in the line
    var vLastTMA = nMA.getValue(MAStudy.MA,-1);
    which needs to be replaced with
    var vLastTMA = nMA.getValue(-1);
    After that line of code you may also want to add the following line to retrieve the current value of nMA
    var vThisMA = nMA.getValue(0);
    which will retrieve the current value of nMA
    At that point replace nMA with vThisMA in your conditions and the script should work as expected (see enclosed screenshot)
    Alex





    Originally posted by bf2
    I am performing the null check but it still does not work. I am trying to combine a triangular moving average study with a bar colouring study.

    I am getting the error on line 17 (var vLastTMA =...)

    Any help would be much appreciated.

    function preMain() {
    setPriceStudy(true);
    setStudyTitle("MA Price Bars");
    setCursorLabelName("TMAPB");
    setColorPriceBars(true);
    setDefaultPriceBarColor(Color.black);

    var fp1 = new FunctionParameter("nInputLength", FunctionParameter.NUMBER);
    fp1.setName("Length");
    fp1.setLowerLimit(1);
    fp1.setDefault(15);
    }

    function main(nInputLength) {
    var nMA = triMA(nInputLength, 0);
    var vClose = close(0);
    var vLastTMA = nMA.getValue(MAStudy.MA,-1);

    if(nMA == null || vClose == null || vLastTMA == null) return;

    if((nMA > vClose) && (nMA < vLastTMA)) {
    setPriceBarColor(Color.red);
    }
    if ((nMA < vClose) && (nMA > vLastTMA)) {
    setPriceBarColor(Color.green);
    }

    return nMA;
    }

    function triMA(Length, Offset)
    {
    var MAvg = null;
    var TMA = null;

    var vTMA = 0.0;

    var vMAvg = 0.0;
    var Len1 = 0.0;
    var Len2 = 0.0;
    var Len = 0.0;
    var LenRnd = 0.0;

    Len = Length / 2;
    LenRnd = Math.round(Len);
    if (LenRnd != Len )
    {
    Len1 = LenRnd;
    Len2 = LenRnd;
    }
    if (LenRnd == Len )
    {
    Len1 = LenRnd;
    Len2 = LenRnd + 1;
    }

    if (MAvg == null) MAvg = new MAStudy(Len1, Offset, "OHLC/4", MAStudy.SIMPLE);
    if(MAvg.getValue(MAStudy.MA) == null) return;
    if (TMA == null) TMA = new MAStudy(Len2, 0, MAvg, MAStudy.MA, MAStudy.SIMPLE);
    vTMA = TMA.getValue(MAStudy.MA);

    return (vTMA);

    }

    Comment

    Working...
    X