Tomorrow's date
Forums › ProRealTime English forum › ProBuilder support › Tomorrow's date
- This topic has 29 replies, 4 voices, and was last updated 5 years ago by dburgh.
Tagged: Business days, date, date math
-
-
03/26/2019 at 1:00 PM #9472203/26/2019 at 1:52 PM #94725
You can find the last day of the current month and check if it’s beyond:
12345678910111213141516DayMax = 31If Month = 4 or Month = 6 or Month = 9 ThenDayMax = 30EndifIf Month = 2 thenDayMax = 28If year mod 4 = 0 thenIf year mod 100 = 0 thenIf year mod 400 = 0 thenDayMax = 29EndifElseDayMax = 29EndifEndifEndif1 user thanked author for this post.
03/26/2019 at 1:57 PM #9472603/26/2019 at 1:58 PM #9472803/26/2019 at 2:13 PM #94729Current Year is YEAR
Current Month is MONTH
Current Day is DAY.
If you need to extract the day from a date, say 20190326, then you’ll have to divide it by 1000000 (it’s a common number for Prt), then round it to the lowest integer, then multiply it by 1000000, then compute the difference between the date and the number you have got:
1234x = datey = round((x / 1000000) - 0.5)z = y * 1000000d = date - zd is the DAY number.
03/26/2019 at 2:19 PM #94730This is a link to my code to compute business days in a month https://www.prorealcode.com/topic/trading-in-giorni-specifici/#post-64933
“Giornoferiale” means business day.
03/26/2019 at 2:20 PM #94731IF OpenMonth <> OpenMonth[1] THEN //Se il mese della barra attuale è diverso da quello della barra precedente significa che siamo alla prima barra del mese…BusinessDay = 0 //… per cui dobbiamo ricominciare da 0 a contare i giorni lavorativiENDIFIF OpenDayOfWeek >= 1 AND OpenDayOfWeek <= 5 AND IntraDayBarIndex = 0 THEN //Se siamo tra lunedì (1) e venerdì (5) ed è la prima barra del giorno…BusinessDay = BusinessDay + 1 //… significa che è iniziato un nuovo giorno lavorativo.ENDIFBut how can I create a function that returns 3 when it checks today’s date 20190326?Using the above would give me the current businessday count for current date. Not sure how to get businessday count for last day of the month. if I had that value can do simple subtraction.03/26/2019 at 2:55 PM #94733Sorry, my previous code was a divion (then multiplication) by 100, not 1000000!
Just modify my previous DATE code:
Get the DAY1234x = date //x = 20190326y = round((x / 100) - 0.5) //y = 201903.00z = y * 100 //z = 20190300d = x - z //d = 26into Get MONTH:
Get the MONTH1234x = date //x = 20190326y = round((x / 100) - 0.5) //y = 201903.00z = round((y / 100) - 0.5) //z = 2019.0000m = y - z //m = 303/26/2019 at 3:40 PM #94736It is not making sense. How can I combine what you’re providing to return the number of business days left in a month? Can you show me a function that takes today’s date 20160326 and returns 3. The same function should take tomorrow’s date and return 2.
03/26/2019 at 4:10 PM #94737day of the week number can be retrieved with: (Replace myYear,myMonth and myDay with YYYY, MM, DD number format. In this example it basically returns the day of the week of the current day.), I think that we could associate the snippets from Roberto to this one to know the last business day.
1234567891011121314//find the day number with a datemyYear=YearmyMonth=MonthmyDay=Dayif myMonth >= 3 thenD = (((23*myMonth)/9) + myDay + 4 + myYear + (myYear/4) - (myYear/100) + (myYear/400) - 2) mod 7elsez = myYear - 1D = (((23*myMonth)/9) + myDay + 4 + myYear + (z/4) - (z/100) + (z/400) ) mod 7endifreturn d03/26/2019 at 4:15 PM #94738There’s no built-in function, by combining the above codes (extracting from dates) and business day math you can achieve that, but it takes a loop, since being 3 does not mean there have been 3 busniss days. PRT does not support easy date math (like eXcel does).
You’ll have to start a loop from the day you want, then go backwords counting only those days whose dayofweek is between 1 and 5 to find out how many business day there were BEFORE that day.
It’s not as straightfoward!
03/26/2019 at 4:49 PM #94739I cannot understand how to replace your ‘OpenDayOfWeek’ in the businessday logic.
Is there a way to get the day of the week from the date?
My temporary idea in pseudo code
x = date
DayMax = getDayMax(x)
businessdayleft = 0
for count = 1 to DayMax
new date = x + count
if dayNumber(newdate) > DayMax then break
if getDayOfWeek(newdate) is business then increment bussinessdaysleft by 1
// basically I need a dayNumber and dayOfWeek function unless I’m missing something?
03/27/2019 at 12:36 PM #9477203/27/2019 at 1:31 PM #94776I’m working on it.
03/27/2019 at 4:43 PM #94795OpenDayOfWeek returns:
1=Monday, … 5=Friday, so if any OpenDayOfWeek is within the range 1-5 it’s a business day (unless there’s some local holiday), so you can detect how many business days there are in each month by tallying from day 1 to the last day and skipping those ourside that range.
Anyway, I attach 4 functions (they are actually indicators, but the behaviour is the same as in other languages, they may require parameters as their input and return some data):
- DayMax (input: Month,Year returns: MaxDay)
- IsLeapYear (input: Year returns: 1=leap year, 0=no leap year)
- UnpackDate (input: Date formatted as YYYYMMDD, as PRT does returns: Day,Month,Year)
- GetDayOfWeek (input: Date formatted as YYYYMMDD, as PRT does returns: 0=Sunday,1=Monday,….,6=Saturday)
I attach both .ITF files to be imported into ProBuilder as well as .TXT files:
DayMax123456789101112//input: MONTH,YEAR//requires indicator ISLEAPYEAR//MaxDay = 31If MyMonth = 4 or MyMonth = 6 or MyMonth = 9 or MyMonth = 11 ThenMaxDay = 30EndifIf MyMonth = 2 thenx = CALL "IsLeapYear"[MyYear]MaxDay = 28 + xEndifRETURN MaxDayIsLeapYear123456789101112//input: YEARLeapYear = 0If MyYear mod 4 = 0 thenIf MyYear mod 100 = 0 thenIf MyYear mod 400 = 0 thenLeapYear = 1EndifElseLeapYear = 1EndifEndifRETURN LeapYearUnpackDate1234567891011121314//input: DATEd = 0m = 0y = 0// example:x = MyDate //x = 20190326w = round((x / 100) - 0.5) //w = 201903.00z = w * 100 //z = 20190300d = x - z //d = 26//y = round((w / 100) - 0.5) //z = 2019.0000m = w - (y * 100) //m = 3//RETURN d,m,yGetDayOfWeek1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950//input: DATE//requires indicators UNPACKDATE and ISLEAPYEAR//// https://www.wikihow.it/Calcolare-il-Giorno-della-Settimana (method 1)//MyDay, MyMonth, MyYear = CALL "UnpackDate"[MyDate]IF MyMonth = 1 OR MyMonth = 10 THENMvalue = 0ELSIF MyMonth = 4 OR MyMonth = 7 THENMvalue = 6ELSIF MyMonth = 9 OR MyMonth = 12 THENMvalue = 5ELSIF MyMonth = 5 THENMvalue = 1ELSIF MyMonth = 6 THENMvalue = 4ELSIF MyMonth = 8 THENMvalue = 2ENDIFd = MyDay + MvalueWHILE d > 6d = d - 7WENDy = MyYear MOD 100Century = MyYear - yCentury = (Century MOD 400) / 100IF Century = 1 THENCentury = 5ELSIF Century = 2 THENCentury = 3ELSIF Century = 3 THENCentury = 1ENDIFz = yWHILE y > 27y = y - 28WENDy = y + round((z / 4) - 0.5) + CenturyIF MyMonth < 3 THENx = CALL "IsLeapYear"[MyYear]y = y - xENDIFd = d + yWHILE d > 6d = d - 7WENDIF d = 0 THENd = 7ENDIFRETURN d - 1 //0=Sunday, 1=Monday...... 6=SaturdayEdit: The above GetDayOfWeek function does not work fine, it has been replaced by a new version (see post https://www.prorealcode.com/topic/tomorrows-date/page/2/#post-95272)
-
AuthorPosts
Find exclusive trading pro-tools on