Announcement

Collapse
No announcement yet.

debugPrintln returns NaN

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

  • debugPrintln returns NaN

    I am using callFunction to call a modified ATR function that works great when added to a bar chart. It shows up in its own window under the price data.

    var AvgVol

    function preMain() {
    setPriceStudy(true);
    setColorPriceBars(true);
    setDefaultBarFgColor(Color.black);
    }
    function main(nInputLength,nOffset,nNumberBars) {
    if (nInputLength == null) {
    nInputLength = 70;
    }
    if (nOffset == null) {
    nOffSet = -1;
    }
    if (nNumberBars == null) {
    nNumberBars = 19;
    }

    AvgVol = callFunction("Trendstat/TCMATR.efs", "main", 20) ;
    debugPrintln("------------------");
    debugPrintln("AvgVol:" + AvgVol);

    }


    This is the separate TCMATR function that is being called. Tested it by itself and it works fine. Called from the Main function up above, I get NaN. Searched the EFS web-based stuff and came up empty on this error code.


    /* TCMATR.efs */
    /**********/
    var AryTTR = null;
    var iCount = 0;


    function preMain() {
    setPriceStudy(false);
    setCursorLabelName("TCM - ATR",0);
    debugClear()

    }

    function main(nInputPeriod){
    // the nInputPeriod can't less than 2
    if (nInputPeriod == null){
    nInputPeriod = 20;
    }

    var vState = getBarState();
    if(vState == BARSTATE_ALLBARS){
    iCount = 0;
    }

    if (AryTTR == null){
    AryTTR = new Array(nInputPeriod);
    }

    var dH = high(-1);
    var dL = low(-1);
    var dC = close(-2);

    var ATRHigh = Math.max(dH,dC);
    var ATRLow = Math.min(dL,dC);
    var TodayTrueRange = ATRHigh - ATRLow ;

    if (iCount < nInputPeriod){
    AryTTR.shift();
    AryTTR.push(TodayTrueRange);
    iCount++;
    return;
    }
    else{
    var PATR = ref(-1); // Previous ATR
    if (PATR == null){
    var iSum=0;
    for(i=0;i<AryTTR.length;i++){
    iSum = iSum + AryTTR[i];
    }
    return iSum/nInputPeriod;
    }
    else{
    var CATR = (PATR*(nInputPeriod-1)+TodayTrueRange)/nInputPeriod;
    //debugPrintln("------------------------");
    //debugPrintln("nInputPeriod:" + nInputPeriod);
    //debugPrintln("TodayTrueRange:" + TodayTrueRange);
    //debugPrintln("PATR:" + PATR);
    //debugPrintln("CATR:" + CATR);
    return CATR;
    }
    }

    }

    Thanks
    Mon_Trader

  • #2
    It means...

    Your call to ...

    callFunction("Trendstat/TCMATR.efs", "main", 20) ;

    could not be executed. There is something wrong with your call function or the location of this file. You might try moving it into the folder where you are running this current EFS file.

    Brad
    Brad Matheny
    eSignal Solution Provider since 2000

    Comment


    • #3
      NaN = Not a number.

      There are a number of reasons this could happen, Brad has identifed one.

      Which print statement caused the NaN?

      Garth
      Garth

      Comment


      • #4
        Hello Mon_Trader,

        Try the formulas below. I believe they fix your problem. I saw a couple of problems actually.

        The main problem has to do with the ref(-1) statement. Your TCMATR.efs formula returns a valid result when used by itself. But when you call this formula through callFunction(), ref(-1) isn't going to return a valid number. It is accessing data that the formula returns to the chart. But since it is being called from another formula, its result is being returned to that formula and not the chart. The end result was that the calc for CATR was returning NaN, or "not a number." The solution is to make CATR, PATR global variables and instead of using ref(-1) to get the previous ATR, just check for a new bar and set PATR = CATR before calculating the new CATR for the new bar.

        The other problem had to do with the order of process for updating your AryTTR array. This was a minor problem and you wouldn't notice much until you ran the formulas in real time. The solution here is to make TodayTrueRange global as well and only perform the pop/unshift routine at the instance of a new bar. I like to use the pop/unshift routine vs. shift/push so that array element 0 always represents the current bar. That way, on each calc of TodayTrueRange, all you have to do is set AryTTR[0] = TodayTrueRange.

        Try these out and let us know if they work out for you.

        Mon_Trader1.efs
        TCMATR.efs
        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


        • #5
          Jason scores again

          Joson,

          I wish I had your knowledge of Java Script. You nailed it again. I took your revised code and used callFunction to go to it for my revised ATR and it worked. It's allowed me to move forward a great deal in my research.

          Thanks so much,

          Mon_Trader

          Comment

          Working...
          X