Programming Challenge

Discussion in 'Trading Software' started by nitro, Feb 27, 2007.

  1. swandro

    swandro

    Nitro

    Actually Sunday = 1, Monday = 2 etc. By default anyway. So I have assumed Friday = 6.

    I have tidied my routine up a little bit. If the first day of the month is after Friday (i.. Saturday, because Sunday is considered BEFORE Friday), I add 3 weeks instead of 2.

    Dim ExtraDays As Integer

    If Weekday(DateSerial(Year(Now), Month(Now), 1)) > 5 Then
    ExtraDays = 21
    Else
    ExtraDays = 14
    End If

    Debug.Print DateAdd("d", ExtraDays + (6 - Weekday(DateSerial(Year(Now), Month(Now), 1))), DateSerial(Year(Now), Month(Now), 1))
     
    #21     Feb 28, 2007
  2. swandro

    swandro

    On the subject of calculating Easter, the following Excel worksheet formula is published in Walkenbach's Formula book.

    Even Walkenbach cannot explain how it works!!

    The 2007 is mentioned twice and is the year for which the calculation is required. It gives 8th April 2007 for this year, Easter Sunday, so it appears to work.

    =DOLLAR(("4/" & 2007)/7 + MOD(19*MOD(2007,19)-7,30)*14%,0)*7-6
     
    #22     Feb 28, 2007
  3. sprstpd

    sprstpd

    Ruby:

    Code:
    require 'Date'
    
    def thirdFriday( date )
      (date.wday == Date:: DAYS[ 'friday' ]) and
        (date.cweek - Date.new( date.year, date.mon, 1 ).cweek == 2)
    end
    
     
    #23     Feb 28, 2007
  4. MGJ

    MGJ

    You have misunderstood. To "calculate whether a given day D is the 3rd Friday of the month", employ the single line

    return( (dayOfWeek(D) == FRIDAY) && (dayOfMonth(D) >= 15) && (dayOfMonth(D) <= 21) ) ;
     
    #24     Feb 28, 2007
  5. nitro

    nitro

    Ah!!!

    Does that really work? I guess so. That would be elegant.

    nitro
     
    #25     Feb 28, 2007
  6. nitro

    nitro

    That's nice because it would work under Unix too.

    nitro
     
    #26     Feb 28, 2007
  7. edil

    edil

    This is what I have been using in VB (returns 3rd friday of the curr month):

    debug.Print (7 - (Weekday(Dateserial(Year(Now),Month(Now),1)) Mod 7) + 14)

    basically transforms Weekday result to 1=fri, 2=thu, 3=wed ... 7=sat
    then simply adds 14 to it
     
    #27     Feb 28, 2007
  8. nitro

    nitro

    This wins. No if statements. Pure math. Fits on one line.

    Nice!

    nitro
     
    #28     Feb 28, 2007
  9. Nitro getting people to do his work for him is what's really clever :)
     
    #29     Mar 13, 2007