Announcement

Collapse
No announcement yet.

Please Help with This Small Script

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

  • Please Help with This Small Script

    This script returns "0.#Q".

    What I'm trying to do is take a simple average of the true range and then take a average of the averages. So the script should return an average of a range of ATRs.

    The Donchian Channel is used to get a daily true range on a four hour chart and the spread is added to account for the broker.
    Attached Files

  • #2
    whatever,

    I figured instead of me just answering the question, I would show you how I figure this stuff out when I am stuck.

    I added a function to your code to assist in troubleshooting. You will see that I have it active on lines 56 and 57 but commented out everywhere else. What I suggest is that you run the code, go to the formula output directory and open the Trace satr.txt file. This will be the output from the trace statements that are active in the script. As you go through the troubleshooting steps, comment out the trace statements that you have verified confirm satisfactory operation of that section of code and enable the next line which has a trace statement.

    I left the commented out trace lines without specifics regarding what they are recording. I am leaving that to you to complete.

    PHP Code:
    //added trace function to code 3/27/2004 S. Hare

    //trace("85: open(); "+open());//  colon after line number, semicolon plus a space after description

    //############ global variables required above premain #############
    var tracedata = new Array();
    var 
    traceon true;//tracefile is only created if it is called, this is a flag to create file
    var nStudy "satr";
    var    
    tracefile = new File("Trace "+nStudy+".txt");
    //############ global variables required above premain ############

    var Sum 0SATR 0;

    function 
    preMain() {
        
    setStudyTitle("SATR");
        
    setDefaultBarFgColor(Color.red0);
        
    setDefaultBarFgColor(Color.blue1);
        
    setDefaultBarFgColor(Color.black2);
        
    setDefaultBarFgColor(Color.brown3);
        
    setCursorLabelName("SATR 5"0);
        
    setCursorLabelName("SATR 10"1);
        
    setCursorLabelName("SATR 20"2);
        
    setCursorLabelName("AVG"3);
        
    setPriceStudy(false);
        
        var 
    fp1 = new FunctionParameter("ATRPeriod1"FunctionParameter.NUMBER);
        
    fp1.setName("SATR1 Period "); //Simple ATR Period 1
        
    fp1.setLowerLimit(1);
        
    fp1.setDefault(3);
        
        var 
    fp2 = new FunctionParameter("DCHLength1"FunctionParameter.NUMBER);
        
    fp2.setName("DCH SATR Length"); //6 on a four hour gives a 24hr True Range
        
    fp2.setLowerLimit(1);
        
    fp2.setDefault(6);
        
        var 
    fp3 = new FunctionParameter("ATRPeriod2"FunctionParameter.NUMBER);
        
    fp3.setName("SATR2 Period"); //Simple ATR Period 2
        
    fp3.setLowerLimit(1);
        
    fp3.setDefault(5);
        
        var 
    fp4 = new FunctionParameter("Spread"FunctionParameter.NUMBER);
        
    fp4.setName("Spread");
        
    fp4.setLowerLimit(.0001);
        
    fp4.setDefault(.0004);
        }

    function 
    main(DCHLength1ATRPeriod1ATRPeriod2Spread) {
        var 
    vValue1vValue2;
        
        var 
    avg  0avgSum 0;
        
        if (
    getBarState() == BARSTATE_ALLBARS) {
            
    myDCHstudy1 = new DonchianStudy(DCHLength10);
        }

        
    vValue1 myDCHstudy1.getValue(DonchianStudy.UPPER0, -ATRPeriod2);    trace("56:vValue1;"+vValue1);trace("save");
        
    vValue2 myDCHstudy1.getValue(DonchianStudy.LOWER0, -ATRPeriod2);    trace("57:vValue2"+vValue1);trace("save");


        if (
    vValue1 == null || vValue2 == null) return;
        
        var 
    0;
        var 
    Len1 ATRPeriod2 ATRPeriod1 1;//    trace(":"+);trace("save");
        
    var Len2 ATRPeriod1;    //trace(":"+);trace("save");

        
    SATR = new Array(Len1);//    trace(":"+);trace("save");
        
    Sum = new Array(Len1);    //trace(":"+);trace("save");
        
        
      
    for(0Len1j++){
        for(
    0Len2i++){
            
    Sum[j] += (vValue1[i] + Spread vValue2[i])*10000;//    trace(":"+);trace("save");
            
    }
        
    SATR[j] = Sum[j] / (Len2);    //trace(":"+);trace("save");
        
    Len2 Len2 1;    //trace(":"+);trace("save");
        
    }
        
        var 
    0;
        
    //trace(":"+);trace("save");
        
    for(0Len1k++){
            
    avgSum += SATR[k];//    trace(":"+);trace("save");            
    }        
        
    avg avgSum/(Len1);    //trace(":"+);trace("save");    
        
    return avg;
        }
            
            
    //########### function required to be copied complete ##############
    function trace(data1){//routine that saves the data
        
    if(data1==null || getCurrentBarIndex() <-10){return;}
        
    tracedata.push(data1);//adds datapoint to array
        
    if(data1 == "save"){//section saves data to a file
            
    if (traceon){//first use is true, creates file only if feature is used
                
    traceon false;
                
    tracefile.open("wt+");tracefile.close();//opens and overwrites previous file
            
    }
            
    tracedata.pop();//removes "save" from array
            
    tracefile.open("at+");tracefile.writeln(tracedata);tracefile.close();
            while (
    tracedata.length0){tracedata.pop();}//removes all data from array
        
    }
    }
    //########## function required to be copied complete ################ 
    If you have any problems figuring out what I am doing, repost and I help.
    Attached Files

    Comment


    • #3
      When I try to open the txt file after running the script I get a buffer over run error and eSignal shuts down.

      I tried loading the script and then removing the script and then opening the txt file and I get the same error.

      I do appreciate the help.

      Thank you

      Comment


      • #4
        interesting, what are you using as a text editor?

        Comment


        • #5
          here is the output to the text file on my end

          PHP Code:
          56:vValue1;1108,1108,1108,1108,1108
          57
          :vValue21108,1108,1108,1108,1108
          56
          :vValue1;1108,1108,1108,1108,1108
          57
          :vValue21108,1108,1108,1108,1108
          56
          :vValue1;1107.25,1108,1108,1108,1108
          57
          :vValue21107.25,1108,1108,1108,1108
          56
          :vValue1;1107,1107.25,1108,1108,1108
          57
          :vValue21107,1107.25,1108,1108,1108
          56
          :vValue1;1107,1107,1107.25,1108,1108
          57
          :vValue21107,1107,1107.25,1108,1108
          56
          :vValue1;1106.75,1107,1107,1107.25,1108
          57
          :vValue21106.75,1107,1107,1107.25,1108
          56
          :vValue1;1106.75,1106.75,1107,1107,1107.25
          57
          :vValue21106.75,1106.75,1107,1107,1107.25
          56
          :vValue1;1106.5,1106.75,1106.75,1107,1107
          57
          :vValue21106.5,1106.75,1106.75,1107,1107
          56
          :vValue1;1106.5,1106.5,1106.75,1106.75,1107
          57
          :vValue21106.5,1106.5,1106.75,1106.75,1107
          56
          :vValue1;1106.5,1106.5,1106.5,1106.75,1106.75
          57
          :vValue21106.5,1106.5,1106.5,1106.75,1106.75
          56
          :vValue1;1106.5,1106.5,1106.5,1106.5,1106.75
          57
          :vValue21106.5,1106.5,1106.5,1106.5,1106.75 

          Comment


          • #6
            Okay got it working.

            Here is some output:

            66:SATR;,,
            67:Sum;,,
            66:SATR;,,
            67:Sum;,,
            66:SATR;,,
            67:Sum;,,
            66:SATR;,,
            67:Sum;,,
            66:SATR;,,
            67:Sum;,,
            66:SATR;,,
            67:Sum;,,
            66:SATR;,,
            67:Sum;,,
            66:SATR;,,
            67:Sum;,,
            66:SATR;,,
            67:Sum;,,
            66:SATR;,,
            67:Sum;,,
            66:SATR;,,
            67:Sum;,,

            72:Sum;NaN,,
            72:Sum;NaN,,
            72:Sum;NaN,,
            72:Sum;NaN,NaN,
            72:Sum;NaN,NaN,
            72:Sum;NaN,NaN,
            72:Sum;NaN,NaN,
            72:Sum;NaN,NaN,NaN
            72:Sum;NaN,NaN,NaN
            72:Sum;NaN,NaN,NaN
            72:Sum;NaN,NaN,NaN
            72:Sum;NaN,NaN,NaN this continues for many line

            I am not sure if line 67 and 68 are correct but I am sure that Line 72 is incorrect.


            for(j = 0; j < Len1; j++){
            for(i = 0; i < Len2; i++){
            Sum[j] += (vValue1[i] + Spread - vValue2[i])*10000; trace("72:Sum;"+Sum);trace("save");
            }
            SATR[j] = Sum[j] / (Len2); //trace(":"+);trace("save");
            Len2 = Len2 + 1; //trace(":"+);trace("save");
            }

            I am not sure how to fix it.
            Any suggestions

            Thanks

            Comment


            • #7
              I have added some additional trace statements, I can tell you that there was a problem with how you were defining your arrays. I have not fixed it completely.

              Before I did anything more, your array values were all coming back as NAN, which is not a number. I stuck a little function that I use to define arrays and declared them in your all bars section. You are still getting an NAN periodically in the output though. I have to get going, so I will post the file as I have it now.

              keeping plugging away on it, I think you are very close.

              Regards,
              Attached Files

              Comment


              • #8
                I added two run throughs to fill the Arrays with zeros and it works. It is a bit of a CPU hog initially so be careful.

                Thanks for all the help
                Attached Files

                Comment

                Working...
                X