Trading Calendar

Trading calendar is a frequently used tool for data analysis, which helps to quickly obtain exchange calendars and perform calculations based on trading calendars. Starting from version 2.00.9/1.30.21, DolphinDB provides built-in trading calendars of more than fifty exchanges. Refer to Release for information on the latest calendars.

This tutorial describes how to use and customize trading calendars in DolphinDB. Specifically: check trading days; perform calculations based on trading calendars; create your own trading calendars; update trading calendars.

1. Use Trading Calendars

The built-in trading calendars can be used for various scenarios.

Note:

  • Starting from version 1.30.23/2.00.11, multiple trading days frequency is supported for functions transFreq, asFreq, and resample with the number specified before the trading calendar identifier in rule.

  • Starting from version 2.00.11.1, trading calendar, specified as "integers + identifiers", can be used and calculated as DURATION data.

1.1. Check Trading Days

You can use function getMarketCalendar(marketName, [startDate], [endDate]) to get trading days of the corresponding exchange in the date range determined by startDate and endDate.

To check the trading days of New York Stock Exchange (XNYS) between 2022.1.1 and 2022.1.10:

getMarketCalendar("XNYS",2022.01.01, 2022.01.10)

// output
[2022.01.03,2022.01.04,2022.01.05,2022.01.06,2022.01.07,2022.01.10]

1.2. Create the DateOffset of Trading Days

To shift a trading day forward or backward, you can use function temporalAdd(date, duration, exchangeId).

Take XNYS for example, we add two trading days to the dates between 2023.1.1 and 2023.1.6:

dates=[2023.01.01, 2023.01.02, 2023.01.03, 2023.01.04, 2023.01.05, 2023.01.06]
temporalAdd(dates,2,"XNYS")

// output
[2023.01.04,2023.01.04,2023.01.05,2023.01.06,2023.01.09,2023.01.10]

Or you can also use the following script if you use version 2.00.11.1 or higher. For detailed usage of trading calendar as DURATION type, refer to section "Use Trading Days as DURATION Type".

dates=[2023.01.01, 2023.01.02, 2023.01.03, 2023.01.04, 2023.01.05, 2023.01.06]
temporalAdd(dates, 2XNYS)

//output
[2023.01.04,2023.01.04,2023.01.05,2023.01.06,2023.01.09,2023.01.10]

1.3. Obtain the Closest Trading Day

You can get the closest trading day of a certain day with function transFreq(X,rule).

For example, specify parameter rule as XNYS. We can get the closest trading days of each date between 2023.1.1 and 2023.1.6:

dates=[2023.01.01, 2023.01.02, 2023.01.03, 2023.01.04, 2023.01.05, 2023.01.06]
dates.transFreq("XNYS")

// output
[2022.12.30,2022.12.30,2023.01.03,2023.01.04,2023.01.05,2023.01.06]

1.4. Data Sampling Based on Trading Days

You can choose functions asFreq(X,rule) or resample(X,rule,func) to sample data on trading days. The only difference of the two lies in whether data can be aggregated.

Function asFreq(X,rule) will return the result by trading days. If there are multiple records in the same trading day, only the first value will be taken. If there is no data in a trading day, it will be filled with NULL.

The following example obtains the stock prices of XNYS in trading days from 2022.12.30 to 2023.1.6:

timestampv = [2022.12.30T23:00:00.000,2023.01.01T00:00:00.000,2023.01.03T00:10:00.000,2023.01.03T00:20:00.000,2023.01.04T00:20:00.000,2023.01.04T00:30:00.000,2023.01.06T00:40:00.000]
close = [100.10, 100.10, 100.10, 78.89, 88.99, 88.67, 78.78]
s=indexedSeries(timestampv, close)
s.asFreq("XNYS")

// output
           #0                 
           ------
2022.12.30|100.10
2023.01.03|100.10
2023.01.04|88.99 
2023.01.05|                   
2023.01.06|78.78

Function resample(X,rule,func) will return the aggregated result of data sampled by trading days.

In the following example, we obtain the closing prices of XNYS stocks in trading days from 2022.12.30 to 2023.1.6:

timestampv = [2022.12.30T23:00:00.000,2023.01.01T00:00:00.000,2023.01.03T00:10:00.000,2023.01.03T00:20:00.000,2023.01.04T00:20:00.000,2023.01.04T00:30:00.000,2023.01.06T00:40:00.000]
close = [100.10, 100.10, 100.10, 78.89, 88.99, 88.67, 78.78]
s=indexedSeries(timestampv, close)
s.resample("XNYS", last)

// output
           #0                 
           ------
2022.12.30|100.10
2023.01.03|78.89
2023.01.04|88.67 
2023.01.05|                   
2023.01.06|78.78

1.5. Use Trading Days as DURATION Type (Only for Server 200)

Starting from version 2.00.11.1, trading calendar, specified as "integers + identifiers", can be used as DURATION data.

1.5.1. Convert Trading Days to DURATION Type

Trading days of DURATION type can be specified by converting a string of trading calendar identifier with theduration function.

Take XNYS for example, we can convert string "2XNYS" to DURATION type and query the average closing price every two trading days with interval specified:

y = duration("2XNYS")
date = [2022.12.30, 2023.01.03, 2023.01.04, 2023.01.05, 2023.01.06]
close = [100.10, 78.89, 88.99, 88.67, 78.78]
t = table(date, close)
select avg(close) from t group by interval(date, y, "prev")

// output
   | interval_date | avg_close 
---|---------------|-----------
 0 | 2022.12.30    | 89.495    
 1 | 2023.01.04    | 88.83     
 2 | 2023.01.06    | 78.78     

1.5.2. Trading Days as Windows for wj

The window parameter of wj now can be specified as trading calendar identifiers.

The following example performs the window join operation on "t1" and "t2" and obtain the average closing price over each window [-2XNYS:0XNYS]:

t1 = table(2023.01.03 2023.01.06 as date)
date = [2022.12.30, 2023.01.03, 2023.01.04, 2023.01.05, 2023.01.06]
close = [100.10, 78.89, 88.99, 88.67, 78.78]
t2 = table(date, close)
wj(t1, t2, -2XNYS:0XNYS, <avg(close)>, `date);

// output
   | date       | avg_close         
---|------------|-------------------
 0 | 2023.01.03 | 89.495            
 1 | 2023.01.06 | 85.48 

1.5.3. Trading Days as Sliding Windows

The trading days can be used for measuring sliding windows for the moving, time-based moving, twindow, and tmovingWindowData functions.

Moving Functions

Take msum as an example, we obtain the sum of closing prices of an XNYS stock every two trading days:

date = [2022.12.30, 2023.01.03, 2023.01.04, 2023.01.05, 2023.01.06]
close = [100.10, 78.89, 88.99, 88.67, 78.78]
X1 = indexedSeries(date, close)
msum(X1, window=2XNYS)

// output
           #0                 
           ------
2022.12.30|100.1
2023.01.03|178.99
2023.01.04|167.88
2023.01.05|177.66
2023.01.06|167.45

Time-based Moving Functions

Take tmavg for example, we obtain the average closing prices of an XNYS stock every two trading days:

date = [2022.12.30, 2023.01.03, 2023.01.04, 2023.01.05, 2023.01.06]
close = [100.10, 78.89, 88.99, 88.67, 78.78]
t = table(date, close)
select tmavg(date, close, 2XNYS) from t

// output
   | tmavg_date         
---|------------
 0 | 100.1              
 1 | 89.495
 2 | 83.94
 3 | 88.83
 4 | 83.725

Function twindow

The following example calculates the average closing price of an XNYS stock over each window [-1XNYS:2XNYS]:

date = [2022.12.30, 2023.01.03, 2023.01.04, 2023.01.05, 2023.01.06]
close = [100.10, 78.89, 88.99, 88.67, 78.78]
t = table(date, close)
select twindow(avg, close, date, -1XNYS:2XNYS) from t

// output
   | twindow_avg       
---|-------------------
 0 | 89.327 
 1 | 89.163 
 2 | 83.833
 3 | 85.48
 4 | 83.725

Function tmovingWindowData

The following example returns an array vector where each row indicates the closing prices of each window (i.e., two trading days).

date = [2022.12.30, 2023.01.03, 2023.01.04, 2023.01.05, 2023.01.06]
close = [100.10, 78.89, 88.99, 88.67, 78.78]
tmovingWindowData(date, close, 2XNYS)

// output
[[100.1],[100.1, 78.89],[78.89, 88.99],[88.99, 88.67],[88.67, 78.78]]

1.5.4. Shift Elements Based on Trading Days

Use functions move and tmove to shift elements based on trading days.

Function move

The following example shifts the closing prices of an XNYS stock to the right for two trading days:

date = [2022.12.30, 2023.01.03, 2023.01.04, 2023.01.05, 2023.01.06]
close = [100.10, 78.89, 88.99, 88.67, 78.78]
X1 = indexedSeries(date, close)
move(X1, 2XNYS)

// output
          #0                 
           ------
2022.12.30|
2023.01.03|
2023.01.04|100.1
2023.01.05|78.89
2023.01.06|88.99

Function tmove

The following example obtains the closing prices from two trading days before the current day.

date = [2022.12.30, 2023.01.03, 2023.01.04, 2023.01.05, 2023.01.06]
close = [100.10, 78.89, 88.99, 88.67, 78.78]
t = table(date, close)
select *, tmove(date, close, 2XNYS) from t

// output
      | date       | close | tmove_date
------|------------|-------|-----------
 0    | 2022.12.30 | 100.1 |       
 1    | 2023.01.03 | 78.89 |       
 2    | 2023.01.04 | 88.99 | 100.1 
 3    | 2023.01.05 | 88.67 | 78.89 
 4    | 2023.01.06 | 78.78 | 88.99

2. Customize Trading Calendars

DolphinDB also allows administrators to customize trading calendars with built-in functions.

Note: Since version 1.30.23/2.00.11, naming the trading calendar identifier with digits is no longer permitted. It must consist of four uppercase letters and cannot be the same as the file name in marketHolidayDir.

2.1. Add a New Trading Calendar

Suppose there is an exchange named "XDDB", function addMarketHoliday(marketName, holiday) can be used to add a new XDDB calendar. A XDDB.csv file will be added to the /marketHoliday/ directory. Weekends are recognized as holidays in DolphinDB by default, therefore, only weekday holidays need to be filled in the file.

Once a new trading calendar has been generated, functions such as getMarketCalendar can be used directly based on the new calendar:

//set 2023.01.03 (Tue.) and 2023.01.04 (Wed.) as holidays
holiday = 2023.01.03 2023.01.04  
//user login
login(`admin,`123456)
//generate a trading calendar
addMarketHoliday("XDDB",holiday)

//get the trading days of the new calendar in a date range
getMarketCalendar("XDDB",2023.01.01, 2023.01.10)
//output
[2023.01.02,2023.01.05,2023.01.06,2023.01.09,2023.01.10]

temporalAdd(2023.01.01,2,"XDDB")
//output
2023.01.05

Note: The newly added trading calendar is only valid on the current node. Execute function addMarketHoliday on other nodes for it to take effect on those nodes.

2.2. Update the Trading Calendar

If you want to update the existing calendar of XDDB exchange, function updateMarketHoliday(marketName, holiday) can be used to reset the holidays.

Note: The file will be overwritten. The original holidays will be replaced with the holidays specified by this function.

The following example resets the dates 2023.03.07 and 2023.03.08 as holidays for the XDDB calendar. Check the next trading day after 2022.01.01 with function temporalAdd:

//set 2023.03.07 (Tue.) and 2023.03.08 (Wed.) as holiday
updateMarketHoliday("XDDB",2023.03.07 2023.03.08)

//the original holidays 2023.01.03 and 2023.01.04 are no longer holidays
getMarketCalendar("XDDB",2023.01.01, 2023.01.10)
//output
[2023.01.02,2023.01.03,2023.01.04,2023.01.05,2023.01.06,2023.01.09,2023.01.10]

//As holidays, 2023.03.07 and 2023.03.08 are not included in the trading calendar
getMarketCalendar("XDDB",2023.03.01, 2023.03.10)
//output
[2023.03.01,2023.03.02,2023.03.03,2023.03.06,2023.03.09,2023.03.10]

3. Calendar Support

All exchange calendars supported are listed below.

Note that calendars are updated according to the holidays announced on the official website of each exchange and the local governments. Refer to Release for information on the latest calendars.

  • Major Stock Exchanges
ISO CodeExchangeCountryExchange WebsiteCSV File PathStarting from
AIXKAstana International ExchangeKazakhstanhttps://aix.kz/trading/trading-calendar/marketHoliday/AIXK.csv2017
ASEXAthens Stock ExchangeGreecehttps://www.athexgroup.gr/market-alternative-holidaysmarketHoliday/ASEX.csv2004
BVMFBMF BovespaBrazilhttps://www.b3.com.br/en_us/solutions/platforms/puma-trading-system/for-members-and-traders/trading-calendar/holidays/marketHoliday/BVMF.csv2004
CCFXChina Finacial Futures ExchangeChinahttp://www.cffex.com.cn/jyrl/marketHoliday/CCFX.csv2007
CMESChicago Mercantile ExchangeUSAhttps://www.cmegroup.com/tools-information/holiday-calendar.html#cmeGlobexmarketHoliday/CMES.csv2004
CZCEZhengzhou Commodity ExchangeChinahttp://www.czce.com.cn/cn/jysj/jyyl/H770313index_1.htmmarketHoliday/CZCE.csv1991
XDCEDalian Commodity ExchangeChinahttp://big5.dce.com.cn:1980/SuniT/www.dce.com.cn/DCE/TradingClearing/Exchange%20Notice/1516085/index.htmlmarketHoliday/XDCE.csv1994
IEPAICE USUShttps://www.theice.com/holiday-hours?utm_source=website&utm_medium=search&utm_campaign=spotlightmarketHoliday/IEPA.csv2004
XINEShanghai International Energey ExchangeChinahttps://www.ine.cn/en/news/notice/6598.htmlmarketHoliday/XINE.csv2017
SHFEShanghai Futures ExchangeChinahttps://www.shfe.com.cn/bourseService/businessdata/calendar/marketHoliday/SHFE.csv1992
XAMSEuronext AmsterdamNetherlandshttps://www.euronext.com/en/trade/trading-hours-holidaysmarketHoliday/XAMS.csv2004
XASXAustrialian Securities ExchangeAustraliahttps://www2.asx.com.au/markets/market-resources/asx-24-trading-calendarmarketHoliday/XASX.csv2004
XBKKStock Exchange of ThailandThailandhttps://www.set.or.th/en/about/event-calendar/holiday?year=2023marketHoliday/XBKK.csv2004
XBOGColombia Securities ExchangeColombiahttps://www.bvc.com.co/non-business-market-daysmarketHoliday/XBOG.csv2004
XBOMBombay Stock ExchangeIndiahttps://www.bseindia.com/static/markets/marketinfo/listholi.aspxmarketHoliday/XBOM.csv2004
XBRUEuronext BrusselsBelgiumhttps://www.euronext.com/en/trade/trading-hours-holidays#:~:text=Calendar%20of%20business%20days%202023%20%20%20Euronext:%20%20Closed%20%2012%20more%20rows%20marketHoliday/XBRU.csv2004
XBSEBucharest Stock ExchangeRomaniahttps://www.bvb.ro/TradingAndStatistics/TradingSessionSchedulemarketHoliday/XBSE.csv2004
XBUDBudapest Stock ExchangeHungaryhttps://www.bse.hu/Products-and-Services/Trading-information/trading-calendar-2023marketHoliday/XBUD.csv2004
XBUEBuenos Aires Stock ExchangeArgentina marketHoliday/XBUE.csv2004
XCBFCBOE FuturesUSAhttps://www.cboe.com/about/hours/us-futures/marketHoliday/XCBF.csv2004
XCSECopenhagen Stock ExchangeDenmarkhttps://www.nasdaqomxnordic.com/tradinghours/marketHoliday/XCSE.csv2004
XDUBIrish Stock ExchangeIrelandhttps://www.euronext.com/en/trade/trading-hours-holidaysmarketHoliday/XDUB.csv2004
XETRXetraGermanyhttps://www.xetra.com/xetra-en/newsroom/trading-calendarmarketHoliday/XETR.csv2004
XFRAFrankfurt Stock ExchangeGermanyhttps://www.boerse-frankfurt.de/en/know-how/trading-calendarmarketHoliday/XFRA.csv2004
XHELHelsinki Stock ExchangeFinlandhttps://www.nasdaqomxnordic.com/tradinghours/XHELmarketHoliday/XHEL.csv2004
XHKGHong Kong ExchangesHong Kong, Chinahttps://www.hkex.com.hk/News/HKEX-Calendar?sc_lang=zh-HK&defaultdate=2023-02-01marketHoliday/XHKG.csv2004
XICEIceland Stock ExchangeIcelandhttps://www.nasdaqomxnordic.com/tradinghours/marketHoliday/XICE.csv2004
XIDXIndonesia Stock ExchangeIndonesiahttps://idx.co.id/en/about-idx/trading-holiday/marketHoliday/XIDX.csv2004
XISTIstanbul Stock ExchangeTürkiyehttps://borsaistanbul.com/en/sayfa/3631/official-holidaysmarketHoliday/XIST.csv2004
XJSEJohannesburg Stock ExchangeSouth Africahttps://www.jse.co.za/marketHoliday/XJSE.csv2004
XKARPakistan Stock ExchangePakistanhttps://www.psx.com.pk/psx/exchange/general/calendar-holidaysmarketHoliday/XKAR.csv2004
XKLSMalaysia Stock ExchangeMalaysiahttps://www.bursamalaysia.com/about_bursa/about_us/calendarmarketHoliday/XKLS.csv2004
XKRXKorea ExchangeRepublic of Koreahttp://global.krx.co.kr/contents/GLB/05/0501/0501110000/GLB0501110000.jspmarketHoliday/XKRX.csv2004
XLIMLima Stock ExchangePeru marketHoliday/XLIM.csv2004
XLISEuronext LisbonPortugalhttps://www.euronext.com/en/trade/trading-hours-holidaysmarketHoliday/XLIS.csv2004
XLONLondon Stock ExchangeEnglandhttps://www.londonstockexchange.com/securities-trading/trading-access/business-daysmarketHoliday/XLON.csv2004
XMADEuronext LisbonPortugalhttps://www.euronext.com/en/trade/trading-hours-holidaysmarketHoliday/XMAD.csv2004
XMEXMexican Stock ExchangeMexicohttps://www.bmv.com.mx/en/bmv-group/holiday-schedulemarketHoliday/XMEX.csv2004
XMILBorsa ItalianaItalyhttps://www.borsaitaliana.it/borsaitaliana/calendario-e-orari-di-negoziazione/calendario-borsa-orari-di-negoziazione.en.htmmarketHoliday/XMIL.csv2004
XMOSMoscow ExchangeRussiahttps://www.moex.com/en/tradingcalendar/marketHoliday/XMOS.csv2004
XNYSNew York Stock ExchangeUSAhttps://www.nyse.com/markets/hours-calendarsmarketHoliday/XNYS.csv2004
XNZENew Zealand ExchangenNew Zealandhttps://www.nzx.com/services/nzx-trading/hours-boardsmarketHoliday/XNZE.csv2004
XOSLOslo Stock ExchangeNorwayhttps://www.euronext.com/en/trade/trading-hours-holidaysmarketHoliday/XOSL.csv2004
XPAREuronext ParisFrancehttps://www.euronext.com/en/trade/trading-hours-holidaysmarketHoliday/XPAR.csv2004
XPHSPhilippine Stock ExchangePhilippineshttps://www.pse.com.ph/investing-at-pse/#investing2marketHoliday/XPHS.csv2004
XPRAPrague Stock ExchangeCzech Republichttps://www.pse.cz/en/trading/trading-information/trading-calendarmarketHoliday/XPRA.csv2004
XSESSingapore ExchangeSingaporehttps://www.mom.gov.sg/employment-practices/public-holidaysmarketHoliday/XSES.csv2004
XSGOSantiago Stock ExchangeChilehttps://www.euronext.com/en/trade/trading-hours-holidaysmarketHoliday/XSGO.csv2004
XSHEShenzhen Stock ExchangeChinahttp://www.szse.cn/disclosure/index.htmlmarketHoliday/XSHE.csv1992
XSHGShanghai Stock ExchangeChinahttp://www.sse.com.cn/market/view/marketHoliday/XSHG.csv1991
XSTOStockholm Stock ExchangeSwedenhttps://www.nasdaqomxnordic.com/tradinghours/marketHoliday/XSTO.csv2004
XSWXSIX Swiss ExchangeSwitzerlandhttps://www.six-group.com/en/products-services/the-swiss-stock-exchange/market-data/news-tools/trading-currency-holiday-calendar.html#/marketHoliday/XSWX.csv2004
XTAITaiwan Stock Exchange CorpTaiwan, Chinahttps://www.twse.com.tw/en/holidaySchedule/holidaySchedulemarketHoliday/XTAI.csv2004
XTKSTokyo Stock ExchangeJapanhttps://www.jpx.co.jp/english/corporate/about-jpx/calendar/marketHoliday/XTKS.csv2004
XTSEToronto Stock ExchangeCanadahttps://www.tsx.com/trading/calendars-and-trading-hours/calendarmarketHoliday/XTSE.csv2004
XWARPoland Stock ExchangePoland marketHoliday/XWAR.csv2004
XWBOWiener BorseAustriahttps://www.wienerborse.at/en/trading/trading-information/trading-calendar/marketHoliday/XWBO.csv2004