Announcement

Collapse
No announcement yet.

Symbol Specific Rollover Dates

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

  • Symbol Specific Rollover Dates

    It can be useful during strategy design/backtest to include rollover dates (or any other dates come to that) for specific symbols when using continuous contracts ...

    1) Create a text file SymbolRollOverDates.txt in \FormulaOutput with each line starting with a unique symbol name followed by the sequence year,month,day,hour,minute

    e.g. GB 1!-DT,2005,8,31,17,30,2005,11,30,17,30 ... etc

    2) Execute the following function once say on each BARSTATE_ALLBARS to load the data corresponding to the current chart symbol:

    PHP Code:
    function readRollOverDates() {

        var 
    fstr "SymbolRollOverDates.txt";
        var 
    fptr = new File(fstr); 
        var 
    str "";  
        var 
    sets 0;
        var 
    setlen 5;
        
        if(
    fptr.exists() ){
            
    fptr.open("rt"); 
            while(!
    fptr.eof()) {  
                
    str fptr.readln();
                var 
    strarray str.split(",");
                if (
    strarray[0] == getSymbol()) {
                    
    sets Math.floor((strarray.length 1)/setlen);
                    break; 
                }
                
    str "";
            }
            
    fptr.close();  
        }  
        
        if (
    str != "" && sets 0) {
            
    RollOverTime = new Array(sets);
            for (
    i=0i<RollOverTime.length; ++i) {
                
    RollOverTime[i] = new Array(setlen);
            }
            var 
    strarray str.split(",");
            var 
    1;
            for (var 
    i=0i<sets; ++i) {
                for (
    j=0j<setlen; ++jRollOverTime[i][j] = strarray[k+j];
                
    += setlen;
            }
        }
        else {
            
    RollOverTime = new Array(1);
            
    RollOverTime[0] = new Array(setlen);
        }

        return;

    3) Execute the following function say on each BARSTATE_NEWBAR and if true, close all open positions. Note that this only works with Daily & Intraday charts and hour & minute are only tested with Intraday charts:

    PHP Code:
    function isRollOver() {
        for (
    i=0i<RollOverTime.length; ++i) {
            if (
    getYear() == RollOverTime[i][0] && getMonth() == RollOverTime[i][1] && getDay() == RollOverTime[i][2]) {
                if (
    isDaily()) return true;
                else if (
    isIntraday() && getHour() >= RollOverTime[i][3] && getMinute() >= RollOverTime[i][4]) return true;
            }
        }
        return 
    false;

    Paul Williams
    Strategy & Applications
    www.futurenets.co.uk

  • #2
    slightly more sophisticated version of isRollOver() to avoid problems with varying time intervals.

    PHP Code:
    function isRollOver() {
        var 
    vYear getYear();
        var 
    vMonth getMonth();
        var 
    vDay getDay();
        if (
    isDaily()) {
            for (
    i=0i<vRollOverTime.length; ++i) {
                if (
    vYear == vRollOverTime[i][0] && vMonth == vRollOverTime[i][1] && vDay == vRollOverTime[i][2]) return true;
            }
        }
        else if (
    isIntraday()) {
            var 
    vHour getHour();
            var 
    vMinute getMinute();
            for (
    i=0i<vRollOverTime.length; ++i) {
                var 
    rolloverdate = new Date(vRollOverTime[i][0],vRollOverTime[i][1]-1,vRollOverTime[i][2],vRollOverTime[i][3],vRollOverTime[i][4]);
                var 
    rolloverms rolloverdate.valueOf();
                var 
    bardate = new Date(vYear,vMonth-1,vDay,vHour,vMinute);
                var 
    barstartms bardate.valueOf();
                var 
    barfinishms barstartms + (getInterval()*60000);
                if (
    rolloverms >= barstartms && rolloverms barfinishms) return true;
            }
        }
        return 
    false;

    Paul Williams
    Strategy & Applications
    www.futurenets.co.uk

    Comment


    • #3
      ... and a more generalized version requiring SymbolDates.txt file in \FormulaOutput folder with each line starting with a unique symbol name followed by the sequence datetype,year,month,day,hour,minute

      e.g. GB 1!-DT,1,2005,8,31,17,30,1,2005,11,30,17,30 ... etc

      here datetype = 1 is assumed to be a rollover date type. create your own defintions of datetype = 2,3,4, etc.

      Execute the following function once say on each BARSTATE_ALLBARS to load the data corresponding to the current chart symbol

      PHP Code:
      var vSymbolDates;

      function 
      readSymbolDates() {

          var 
      fstr "SymbolDates.txt";
          var 
      fptr = new File(fstr); 
          var 
      str "";  
          var 
      sets 0;
          var 
      setlen 6;
          
          if(
      fptr.exists() ){
              
      fptr.open("rt"); 
              while(!
      fptr.eof()) {  
                  
      str fptr.readln();
                  var 
      strarray str.split(",");
                  if (
      strarray[0] == getSymbol()) {
                      
      sets Math.floor((strarray.length 1)/setlen);
                      break; 
                  }
                  
      str "";
              }
              
      fptr.close();  
          }  
          
          if (
      str != "" && sets 0) {    
              
      vSymbolDates = new Array(sets);
              for (
      i=0i<vSymbolDates.length; ++i) {
                  
      vSymbolDates[i] = new Array(setlen);
              }
              var 
      strarray str.split(",");
              var 
      1;
              for (var 
      i=0i<sets; ++i) {
                  for (
      j=0j<setlen; ++jvSymbolDates[i][j] = strarray[k+j];
                  
      += setlen;
              }
          }
          else {
              
      vSymbolDates = new Array(1);
              
      vSymbolDates[0] = new Array(setlen);
          }

          return;

      Execute the following function say on each BARSTATE_NEWBAR e.g. isSymbolDate(1)) and if true, close all open positions since this is a rollover date type.

      PHP Code:
      function isSymbolDate(datetype) {
          
      //datetype = 1 is a rollover date
          
      var vYear getYear();
          var 
      vMonth getMonth();
          var 
      vDay getDay();
          if (
      isDaily()) {
              for (
      i=0i<vSymbolDates.length; ++i) {
                  if (
      vSymbolDates[i][0] == datetype) {
                      if (
      vYear == vSymbolDates[i][1] && vMonth == vSymbolDates[i][2] && vDay == vSymbolDates[i][3]) return true;
                  }
              }
          }
          else if (
      isIntraday()) {
              var 
      vHour getHour();
              var 
      vMinute getMinute();
              for (
      i=0i<vSymbolDates.length; ++i) {
                  if (
      vSymbolDates[i][0] == datetype) {
                      var 
      vSymdate = new Date(vSymbolDates[i][1],vSymbolDates[i][2]-1,vSymbolDates[i][3],vSymbolDates[i][4],vSymbolDates[i][5]);
                      var 
      vSymdatems vSymdate.valueOf();
                      var 
      vBardate = new Date(vYear,vMonth-1,vDay,vHour,vMinute);
                      var 
      vBarstartms vBardate.valueOf();
                      var 
      vBarfinishms vBarstartms + (getInterval()*60000);
                      if (
      vSymdatems >= vBarstartms && vSymdatems vBarfinishms) return true;
                  }
              }
          }
          return 
      false;

      Paul Williams
      Strategy & Applications
      www.futurenets.co.uk

      Comment

      Working...
      X