how to get any previous open market day in YYYYMMDD format?

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #236898 quote
    oliTRoliTR
    Participant
    Average

    Hello,

    let’s consider the monday 3rd of March, 2008 as a current date (= Today ) and also that the market was closed on every saturday and sunday, like on the 1st and 2nd of March, 2008.

    Is there an easy way to code a request which will return 20080229 as the previous open market day ?

    Could this also manage not only the leap years and the regular week days off (as example saturday & sunday) but also any holidays list (as example 25th of december 2023 and 1st of january 2023) ?

    Something like :

    diff=1                                                                   // parameter which gives the increment of lookback in open market days

    previousOpenMarketDay = Today – diff      // would give  : 20080303 – 1 => 20080229 ;   or   20240801 – 1 => 20240731 ;   or   20240102 – 1 => 20231229

    Many thanks

    Oli

    #236905 quote
    JC_BywanJC_Bywan
    Moderator
    Master

    Hello,

    The variable jouravant coded below would give each day what the date was at market open the previous day (beware with 24h-quoted assets like IG cfd combined with an unfortunate timezone difference between market time and local time, which would require a different coding to work for all possible cases).

    The other variable MMDDjouravant transforms it into MMDD format to check it with a drawtext avoiding the 20.2M display

    if opendate<>opendate[1] then
    jouravant=opendate[1]// YYYYMMDD
    MMDDjouravant=jouravant-openyear*10000// transforme en format MMDD pour vérifier l'affichage autrement qu'avec 20.2M dans le drawtext
    endif
    DRAWTEXT("#jouravant# #MMDDjouravant#", barindex, close)
    return close
    #236911 quote
    robertogozzirobertogozzi
    Moderator
    Legend

    The solution suggested by JC_Bywan, is far easier, but since I had already started coding an indicator, I attach it.

    This indicator will return the previous day of any date in the format YYYYMMDD:

    // Prior DAY
    //
    
    // calculates the Day OPf Week of any date YYYYMMDD
    YY   = floor(myDate / 10000)     //Year
    temp = myDate MOD (YY * 10000)
    MM   = floor(temp / 100)         //Month
    DD   = temp MOD (MM * 100)       //Day
    CC   = floor(YY / 100)           //Century
    YC   = YY MOD 100                //Year of the Century (offset from the beginning of the Century)
    
    // Adjust month and year for January and February fo Zeller's Congruence
    tempMM = MM
    IF MM = 1 OR MM = 2 THEN
       MM = MM + 12
       YC = YC - 1
       IF YC = -1 THEN
          YC = 99
          CC = CC - 1
       ENDIF
    ENDIF
    
    // Zeller's Congruence formula
    temp = (((DD + floor((13 * (MM + 1)) / 5) + YC + floor(YC / 4) + floor(CC / 4) - (2 * CC))) MOD 7) + 5
    
    // Adjusting the result to match with standard day of the week (0 = Sunday, 1 = Monday, ...)
    DoW = (temp MOD 7) + 1
    
    //scan bachwards to find the previous day
    MM = tempMM
    While(1)
    DoW = DoW - 1
       DD  = DD - 1
       //when the end of the Month is reached, then it's the end of the previous Month
       IF DD = 0 THEN
          MM = MM - 1
          //when scanned past January, then restart from Month 12 of the previous year
          IF MM < 1 THEN
             MM = 12
             YY = YY - 1
          ENDIF
          //assume 31 as the last day of the previous Month
          DD = 31
          IF (MM = 4) OR (MM = 6) OR (MM = 9) OR (MM = 11) THEN
             DD = 30     //it's 30 on April, June, September and November
          ELSIF (MM = 2) THEN
             DD = 28     //assume 28 on February, unless it's a LeapYear
             IF (YY MOD 4) = 0 THEN
                IF (YY MOD 100) = 0 THEN
                   IF (YY MOD 400) = 0 THEN
                      DD = 29   //it's a Leap Year
                   ENDIF
                ELSE
                   DD = 29      //it's a Leap Year
                ENDIF
             ENDIF
          ENDIF
       ENDIF
       IF Dow < 0 THEN
          Dow = 6
       ENDIF
       IF (DoW > 0) AND (DoW < 6) THEN
          break
       ENDIF
    Wend 
    RETURN ((YY * 10000) + (MM * 100) + DD) AS "Prior Day"

    while JC_Bywan‘s code will always skip holidays, mine will not as it cannot know when it’s not a trading day, apart from Saturdays and Sundays.

    Prior-DAY.itf
    #236969 quote
    oliTRoliTR
    Participant
    Average

    Thanks a lot JC_Bywan and robertogozzi

    your answers help me to make my way which at the moment is:

    .setting up an array with all the days off (weekends or holidays) in YYYYMMDD  format [can be easily generated by excel / however I limited this to 2023 2024 2025]

    .checking in a loop, for each new opendate, if opendate[1] doesn’t match a date in the array. In case it does, then check again with opendate[2] and so on

    if opendate<>opendate[1] // ** new trading day starts **
    dayBefore1=opendate[1]// YYYYMMDD
    d=1
    for i=imax downto 0 do
    if dayBefore1=$dayOff[i] then
    dayBefore1=opendate[1+d]// YYYYMMDD
    d=d+1
    endif
    next
    endif
Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.
ProRealAI ProRealAI New

Stuck on this ProBuilder code?

Describe what this topic is trying to build, in plain English, and ProRealAI writes the ProRealTime™ indicator, screener or system for you.

Available in 7 languages
Try ProRealAI

how to get any previous open market day in YYYYMMDD format?


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
oliTR @olitr Participant
Summary

This topic contains 3 replies,
has 3 voices, and was last updated by oliTRoliTR
2 years ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 08/30/2024
Status: Active
Attachments: 1 files
ProRealCode ProRealCode
Loading...