外汇市场时间与常规交易时间“不同”。纽约证券交易所于当地时间上午 9:30 至下午 4 点开放交易,但外汇交易时段为:
Forex "NYSE" New York 8am to 5pm timezone: "America/New_York"
Forex "ASX" Sydney 7am to 4pm timezone: "Australia/Sydney"
Forex "JPX" Tokyo 9am to 6pm timezone: "Asia/Tokyo"
Forex "LSE" London 8am to 4pm timezone: "Europe/London"
我正在尝试使用以下代码获取外汇交易时间:
import pytz
import pandas_market_calendars as mcal
nyse = mcal.get_calendar("NYSE")
market_open_and_close_times = nyse.schedule(start_date="2023-11-05", end_date="2023-11-11")
market_open_and_close_times['market_open'] = [t.tz_convert(pytz.timezone("America/New_York")) for t in market_open_and_close_times['market_open']]
market_open_and_close_times['market_close'] = [t.tz_convert(pytz.timezone("America/New_York")) for t in market_open_and_close_times['market_close']]
print(type(market_open_and_close_times))
print(market_open_and_close_times)
"""
# Result:
<class 'pandas.core.frame.DataFrame'>
market_open market_close
2023-11-06 2023-11-06 09:30:00-05:00 2023-11-06 16:00:00-05:00
2023-11-07 2023-11-07 09:30:00-05:00 2023-11-07 16:00:00-05:00
2023-11-08 2023-11-08 09:30:00-05:00 2023-11-08 16:00:00-05:00
2023-11-09 2023-11-09 09:30:00-05:00 2023-11-09 16:00:00-05:00
2023-11-10 2023-11-10 09:30:00-05:00 2023-11-10 16:00:00-05:00
"""
如何将“pandas_market_calendars”改为输出
从上午 8 点到下午 5 点的外汇交易时段? 也许我应该使用其他库或代码?
如有任何建议,我们将不胜感激。
库,以防您希望将相同的数据适应您的时区(尽管在这种情况下您可能必须将数据更改为,也许, pd.Timestamp()).
import pandas as pd
import pytz # NOT USED BELOW
def get_forex_trading_hours(start_date, end_date):
forex_sessions = {
"NYSE": ("America/New_York", "8:00", "17:00"),
"ASX": ("Australia/Sydney", "7:00", "16:00"),
"JPX": ("Asia/Tokyo", "9:00", "18:00"),
"LSE": ("Europe/London", "8:00", "16:00")
}
trading_hours = pd.DataFrame(index=pd.date_range(start=start_date, end=end_date, freq="D"))
for market, (timezone, open_time, close_time) in forex_sessions.items():
start_time = pd.to_datetime(open_time).time()
end_time = pd.to_datetime(close_time).time()
trading_hours[f"{market}_open"] = start_time
trading_hours[f"{market}_close"] = end_time
return trading_hours
# Example usage
start_date = "2023-11-05"
end_date = "2023-11-11"
forex_trading_hours = get_forex_trading_hours(start_date, end_date)
print(forex_trading_hours)