2
3

More than 3 years have passed since last update.

日経先物のSQ日を取得(毎月の第二金曜日)

Last updated at Posted at 2020-05-22

今回は完全にメモ用である。過去から第二金曜日の一覧を取得したいと思う。
こちらのサイトを参考にさせていただいた。

import calendar
import datetime

def get_day_of_nth_dow(year, month, nth, dow):
    '''dow: Monday(0) - Sunday(6)'''
    if nth < 1 or dow < 0 or dow > 6:
        return None

    first_dow, n = calendar.monthrange(year, month)
    day = 7 * (nth - 1) + (dow - first_dow) % 7 + 1

    return day if day <= n else None


#実行
dates = []

for year in range(2000, 2021):
    for month in range(1, 13):
        day = get_day_of_nth_dow(year, month, 2, 4)
        dates.append(["{}-{}-{}".format(year, month, day)])
dates = pd.DataFrame(dates, columns=["date"])
dates["date"] = pd.to_datetime(dates["date"])
dates

出力

date
242 2020-03-13
243 2020-04-10
244 2020-05-08
245 2020-06-12
246 2020-07-10
247 2020-08-14
248 2020-09-11
249 2020-10-09
250 2020-11-13
251 2020-12-11

結論

過去の日経先物のSQ日が一瞬で計算できる。(祝日の例外は無視しているが、、、

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3