Announcement

Collapse
No announcement yet.

TF expiration calculation

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

  • TF expiration calculation

    Has anyone seen any other code to determine the expiry code for futures?

    I made this up and wonder if I have it close to right. I looked up the Thursday before the 3rd Friday for each expiration month, made and array, and then figured out if this month is expiration and then is it before or after expiration.

    Comments appreciated, thanks.

    // 2009 expiry 1 2 3 4 5 6 7 8 9 10 11 12
    var MM = new Array( 3, 3, 3, 6, 6, 6, 9, 9, 9,12,12,12,13)
    var DD = new Array(12,12,12,11,11,11,10,10,10,11,11,11,99)
    var init = 0.0;
    var d = new Date();
    debugClear();
    var exp = "";

    function main() {

    if ( init == 0 ) {
    exp = ""+d.getFullYear()+MM[d.getMonth()];//basic logic
    if ( d.getMonth() == MM[d.getMonth()] && d.getDate() < MM[d.getMonth()] )//before expiry
    exp = ""+d.getFullYear()+MM[d.getMonth()];
    if ( d.getMonth() == MM[d.getMonth()] && d.getDate() >= MM[d.getMonth()] )//after expiry
    exp = ""+d.getFullYear()+MM[d.getMonth()+1];
    if ( d.getMonth() == 12 && d.getDate() >= MM[d.getMonth()] )//december rule
    exp = ""+(d.getFullYear()+1)+MM[0];
    debugPrintln(exp)
    init = 1;
    }
    return;}

  • #2
    I found this interesting and a way to learn to code with dates.
    Here is my attempt. As far as my testing goes it works fine.
    The efs assumes that futures roll over on the third Thursday of every quarter as defined by your MM array.
    Attached Files

    Comment


    • #3
      Here is some really fancy code that doesnt need the DD array that i found in TWSLink sample for eSignal
      var d = new Date();
      var MM = new Array( 3, 3, 3, 6, 6, 6, 9, 9, 9,12,12,12,13)
      var expiry_date = 0 ;

      function main{
      get_expiry_date(Month-1, d.getFullYear())
      debugPrintln(MM[d.getMonth()]+"/" + Third_Friday + "/"+d.getFullYear())
      return;}


      function get_expiry_date(month,year) {
      First_day_of_Month = new Date(year,month,01);
      Weekday = First_day_of_Month.getDay();
      if (5 - Weekday >= 0)
      Third_Friday = 20 - Weekday - 8;// - 8 from the third friday is thursday roll over
      else
      Third_Friday = 21 - 8;
      expiry_date = new Date(year,month,Third_Friday);
      return Third_Friday;
      }

      Comment


      • #4
        Based on your new code Futures Expiration is the Thursday a week before the third Friday of a quarterly expiration month. So I modified my code to reflect that. See attached.

        Note: fwiw, the code you posted is a sketch of the idea. Variables and logic are not defined. Neither uses the DD array.

        If you tell me how or where you want the output (i.e., to a file, output window, etc.) I'll try to add it to the code.
        Attached Files

        Comment


        • #5
          waynecd

          What i was trying to do was write a standa lone rountine to calculate the rollover date, which is the 2nd Thursday before the Thrid Friday.

          I would then use this expiration date to fill in the order i send to the broker.

          The only flaw I saw last night was how the routine handles dates after the rollover in December,

          if ( d.getDate() > Third_Friday && d.getMonth() == 11 ){// after dec expiry
          get_expiry_date(3, d.getFullYear()+1); }//next years March expiry

          seems to take of that, will do some testing over the weekend tho.

          Comment


          • #6
            Just curious,

            Standalone using partial efs code?

            Where would the standalone code work?

            Comment


            • #7
              waynecd

              When I write code I try and get sections working before I add them into the main program.

              This way I can better control mistakes.

              Once I get the code to generate the rollover date, I add the code in to the main program.

              Then it is on to the next section.

              Comment


              • #8
                waynecd

                x=(Math.floor((d.getMonth()-0.1)/3)+1)*3;

                Will generate the month of the next expiration, still needs some help for the last half of an expiry month, but does away with the MM array.

                Comment


                • #9
                  try this.
                  it outputs to output window when menu item "debug" is true
                  for real time instead of entering dates in menu just comment out the line
                  "d.setFullYear(DateY,(DateM-1),DateD);"
                  Attached Files
                  Last edited by waynecd; 11-02-2008, 05:56 AM.

                  Comment


                  • #10
                    Thisi s what I ended with, seems to work in the past just fine and I ran it for every day of 2008 and it looks good there too....

                    if(getBarState()==BARSTATE_NEWBAR){
                    YY = year(0); MM = month(0); DD = day(0);
                    Month=(Math.floor((MM-0.1)/3)+1)*3;//0-11 ==> 3, 6, 9 or 12

                    get_expiry_date(Month, YY);//basic logic to find Rollover Thursday

                    if ( DD > Third_Friday && MM == Month ) //after expiry in Mar Jun and Sept
                    Month = Month + 3;//Increment Month to next expiry

                    if ( Month <= 9 ) MM = "0" + Month; else MM = Month;

                    if ( DD > Third_Friday && MM == 11 ){// after Dec expiry
                    MM = "03"; //next March
                    YY = YY + 1 ; }//next year

                    exp = YY + "" + MM ;
                    }

                    function get_expiry_date( Month, Year ) {
                    First_day_of_Month = new Date( Year, Month - 1, 01 );
                    Weekday = First_day_of_Month.getDay();
                    if ( 5 - Weekday >= 0 )
                    Third_Friday = 20 - Weekday - 8;
                    else
                    Third_Friday = 21 - 8;
                    return Third_Friday;
                    }
                    Last edited by dloomis; 11-02-2008, 02:08 PM.

                    Comment

                    Working...
                    X