[英]Using quantlib in python how do i get the number of days between two dates?
[英]How to get a list of days using Python's QuantLib
給定一個日歷,我想獲取給定日期前 4 周前一周的前 2 個工作日(從星期日開始)的列表。
有沒有辦法得到這樣的日子的名單? 我在Python
中使用QuantLib
模塊。
以下是我到目前為止的實現 -
import QuantLib as ql
import pandas as pd
date = ql.Date(2, 7, 2020)
date_prev = date - ql.Period(4, ql.Weeks)
us_calendar = ql.UnitedStates()
print(date)
print(date_prev)
如您所見,上面的代碼只是盲目地從date
減去 4 周,沒有考慮us_calendar
,也沒有提供我正在尋找的那一周的前 2 天列表
任何指針都會非常有幫助。
像這樣的東西:
import QuantLib as ql
calendar = ql.UnitedStates()
start = ql.Date(7,5,2020)
previous = calendar.advance(start, -4, ql.Weeks)
days = [calendar.advance(previous, n, ql.Days) for n in [1,2]]
print(days)
[日期(10,4,2020),日期(13,4,2020)]
我遲到了 2 年以上,但以防萬一它仍然有用,我會做這樣的事情:
date = ql.Date(2, 7, 2020)
# 4 weeks before:
d = date - ql.Period(4, ql.Weeks)
# if needed, go back to the Sunday of that week:
while d.weekday() != ql.Sunday:
d = d - 1
# now get the first two business days:
d1 = us_calendar.advance(d, 1, ql.Days)
d2 = us_calendar.advance(d1, 1, ql.Days)
print([d1, d2])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.