Announcement

Collapse
No announcement yet.

EFS to sort Date and Time

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

  • EFS to sort Date and Time

    I developed this as part of a project I was working on and thought it may come in handy to someone. It is an efs that simulates the input of dates and times from a file (using an array in the file) and using the Javascript .sort functionallity sorts the array by the date and time. I had used several methods to sort these fields but invariably, one or two lines would be out of order. This method turned out to be the simplest and most reliable method.

    If you install on your machine you can run it and the output can be examined using the trace utility button on the screen visable after the efs has been run.





    PHP Code:
    /***********************************************
    Key Words:   new Array() length split function else if x++ getBarState() == BARSTATE_ALLBARS trace debugPrintln 

    Title:  Sort Date and Time.efs
    By:         Steve Hare
    Email:      [email][email protected][/email]
    Incept:     9/7/2004
    Version:    1.0.0

    ==============================================
    Fix History:

    1.0.0  9/7/2004 -   Initial Release
                              
    ==============================================
    Description:   

    This was written to demonstrate the use of a sort function for date and time. Those steps marked as required may
    are required, however, the specifics may be changed to match your needs.

    Dislaimer: For educational purposes only! Obviously, no guarantees 
    whatsoever and use at your own risk.
    ***************************************************/

    function Create_Data_Array(){
     var 
    tmp = new Array();
     var 
    0;
     
    tmp[x++] = ("long,8:40:12,9/2/2003");
     
    tmp[x++] = ("long,11:41:12,9/6/2004");
     
    tmp[x++] = ("long,10:41:12,9/5/2004");
     
    tmp[x++] = ("long,10:31:12,9/6/2004");
     
    tmp[x++] = ("long,11:21:12,9/4/2004");
     
    tmp[x++] = ("long,10:11:12,9/6/2004");
     
    tmp[x++] = ("long,9:41:12,9/6/2004");
     
    tmp[x++] = ("long,8:41:12,9/9/2004");
     
    tmp[x++] = ("long,13:41:12,9/6/2004");
     
    tmp[x++] = ("long,11:41:12,8/17/2004");
     
    tmp[x++] = ("long,10:41:12,9/2/2004");
     
    tmp[x++] = ("long,9:41:12,9/2/2004");
     
    tmp[x++] = ("long,8:41:12,9/2/2004");
     
    tmp[x++] = ("long,8:41:12,9/2/03");
     
    tmp[x++] = ("long,8:41:12,9/2/2003");
     
    trace(" ");
     
    trace("Raw data ");
     
    trace(" ");
     for (
    x=0;x<tmp.length;x++){
      
    trace("tmp["+x+"] = "+tmp[x]);
      var 
    nObj = new Object;
      var 
    tmp[x].split(",");
      
    nObj.trade_action1 b[0];//string
      
    nObj.trade_time1 b[1];//string
      
    nObj.trade_date1 b[2];//string
      
    t_lines[x] = nObj;
      
    nObj null;
     }
     
    trace(" ");
     
    trace(" ");

    }

    function 
    Sort_trade_date1(a,b) {
     var 
    p;var q;var f1 null;var f2 null;
     
    a.trade_date1.split("/");  // date is mm/dd/yyyy, so p[0] is month, p[1] is day, p[2] is 4 digit year 
     
    if (p[0]*1<10)p[0] = "0"+p[0];
     if (
    p[1]*1<10)p[1] = "0"+p[1];
     if (
    p[2]*1<100)p[2] = "20"+p[2];
     
    f1=p[2]+p[0]+p[1];
     
    a.trade_time1.split(":");
     if (
    q[0]*1<10)q[0] = "0"+q[0];
     if (
    q[1]*1<10)q[1] = "0"+q[1];
     if (
    q[2]*1<10)q[2] = "0"+q[2];
     
    f1=f1+q[0]+q[1]+q[2];

     
    trace ("p = "+p+"  q = "+q+" f1 = "+f1);

     
    b.trade_date1.split("/");
     if (
    p[0]*1<10)p[0] = "0"+p[0];
     if (
    p[1]*1<10)p[1] = "0"+p[1];
     if (
    p[2]*1<100)p[2] = "20"+p[2];
     
    f2=p[2]+p[0]+p[1];
     
    b.trade_time1.split(":");
     if (
    q[0]*1<10)q[0] = "0"+q[0];
     if (
    q[1]*1<10)q[1] = "0"+q[1];
     if (
    q[2]*1<10)q[2] = "0"+q[2];
     
    f2=f2+q[0]+q[1]+q[2];

     if (
    f1 == f2) return 0;
     else if (
    f1 f2) return 1
     
    else return -1;
    }

    var 
    t_lines = new Array();
     
    function 
    preMain(){
     
    }
    function 
    main(){

     if (
    getBarState() == BARSTATE_ALLBARS) {//true once during initial load and subsequent reload
      
    Create_Data_Array();
      
    trace(" ");
      
    trace("Sort_trade_date1 ");
      
    trace(" ");
      
    trace(" ");
      
    trace("Watch here where the .sort is doing it's thing:");
      
    trace(" ");

      
    t_lines.sort(Sort_trade_date1);
      
    trace(" ");
      
    trace("Now here is the sorted time and date:");
      
    trace(" ");
      for (
    x=0;x<t_lines.length;x++){
       
    trace("t_lines["+x+"] = "+t_lines[x].trade_action1+","+t_lines[x].trade_time1+","+t_lines[x].trade_date1);
      }
     }
    }

    //###### trace section required to be copied complete #####
    var traceon true;// previous tracefile is overwritten if this is true
    //var traceon = false;// previous tracefile is added to if this is false
    var nStudy "Sort Date and Time.txt";// -  name to be used to name tracefile and button
    var tracefile = new File(nStudy);
    function 
    trace(data1){//if (getCurrentBarIndex() < -13)return;
     
    debugPrintln(data1);//comment this line out if you do not want the output echoed to the formula output window

     
    if (traceon){//first use is true, overwrites previous trace file
      
    traceon false;
      
    tracefile.open("wt+");tracefile.close();//opens and overwrites previous file
     
    }
     
    tracefile.open("at+");
     
    tracefile.writeln(data1);
     
    tracefile.close();
     if (
    trace.caller.name!="preMain" && trace.caller.name!="postMain")tButton();//call to the button that allows opening text file
    }
    function 
    tButton(){
    drawTextPixel(5010, (" click here to see the "+nStudy+" file " "@URL=C:\/Program\ Files\/eSignal\/FormulaOutput\/"+nStudy),  Color.rednullText.RELATIVETOLEFT Text.RELATIVETOBOTTOM|Text.FRAME"Comic Sans MS"13,"trace11");
    }
    //####### section required to be copied complete ####### 

  • #2
    Here is a copy of the file
    Attached Files

    Comment

    Working...
    X