How to calculate the difference in trading days within a date vector?

46 views Asked by At

I have a date vector containing trading days. I would like to subtract N trading days from this vector, or find out the number of trading days between two dates in this vector. For instance, I want to calculate the number of trading days between the current date and the listing date, or an upcoming significant event date. Is there a method or function available in DolphinDB to perform such calculations?

2

There are 2 answers

0
Hanwei Tang On

If my understanding is correct, set can help you:

numOfTradingDaysBetween = set(beginDate..endDate) & set(your_trading_day_vector)
0
jinwandalaohu On

To subtract N trading days, you can utilize the temporalAdd function. Here's an example:

date=[2023.01.01, 2023.01.02, 2023.01.03, 2023.01.04]
temporalAdd(date, -4,`CFFEX)

To calculate the number of trading days between two dates in a vector, define custom logic to handle the internal offsets within the vector. Here's an example:

date = getMarketCalendar(`SSE, 2023.01.01, 2023.03.01)
d1 = 2023.02.15 
d2 = 2023.02.28
def date_diff(date, d1, d2){
    return binsrch(date, d2) - binsrch(date, d1)
}
date_diff(date, d1, d2)