how to get any previous open market day in YYYYMMDD format?
Forums › ProRealTime English forum › ProOrder support › how to get any previous open market day in YYYYMMDD format?
- This topic has 3 replies, 3 voices, and was last updated 2 months ago by oliTR.
-
-
08/30/2024 at 1:26 PM #236898
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
08/30/2024 at 3:40 PM #236905Hello,
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
123456if opendate<>opendate[1] thenjouravant=opendate[1]// YYYYMMDDMMDDjouravant=jouravant-openyear*10000// transforme en format MMDD pour vérifier l'affichage autrement qu'avec 20.2M dans le drawtextendifDRAWTEXT("#jouravant# #MMDDjouravant#", barindex, close)return close08/30/2024 at 5:16 PM #236911The 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:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566// Prior DAY//// calculates the Day OPf Week of any date YYYYMMDDYY = floor(myDate / 10000) //Yeartemp = myDate MOD (YY * 10000)MM = floor(temp / 100) //MonthDD = temp MOD (MM * 100) //DayCC = floor(YY / 100) //CenturyYC = 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 CongruencetempMM = MMIF MM = 1 OR MM = 2 THENMM = MM + 12YC = YC - 1IF YC = -1 THENYC = 99CC = CC - 1ENDIFENDIF// Zeller's Congruence formulatemp = (((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 dayMM = tempMMWhile(1)DoW = DoW - 1DD = DD - 1//when the end of the Month is reached, then it's the end of the previous MonthIF DD = 0 THENMM = MM - 1//when scanned past January, then restart from Month 12 of the previous yearIF MM < 1 THENMM = 12YY = YY - 1ENDIF//assume 31 as the last day of the previous MonthDD = 31IF (MM = 4) OR (MM = 6) OR (MM = 9) OR (MM = 11) THENDD = 30 //it's 30 on April, June, September and NovemberELSIF (MM = 2) THENDD = 28 //assume 28 on February, unless it's a LeapYearIF (YY MOD 4) = 0 THENIF (YY MOD 100) = 0 THENIF (YY MOD 400) = 0 THENDD = 29 //it's a Leap YearENDIFELSEDD = 29 //it's a Leap YearENDIFENDIFENDIFENDIFIF Dow < 0 THENDow = 6ENDIFIF (DoW > 0) AND (DoW < 6) THENbreakENDIFWendRETURN ((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.
09/01/2024 at 10:28 AM #236969Thanks 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
12345678910if opendate<>opendate[1] // ** new trading day starts **dayBefore1=opendate[1]// YYYYMMDDd=1for i=imax downto 0 doif dayBefore1=$dayOff[i] thendayBefore1=opendate[1+d]// YYYYMMDDd=d+1endifnextendif -
AuthorPosts
Find exclusive trading pro-tools on