Forex market hours are different from regular trading hours. NYSE is open for trading from 9:30am to 4pm local time, but Forex sessions are:
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"
I am attempting to get Forex trading hours with the following code:
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
"""
How do I get 'pandas_market_calendars' to output Forex session from 8am to 5pm instead?
Maybe I should use some other library or code?
Any advice is appreciated.
I am not sure I understood your goal but I suggest the following idea: You will have a dataframe with columns dedicated to each opening and closing time for each market. If it could be useful in the future, I suggest to give a look at the pytz library in case you wish to have the same data adapted to your timezone or a specified one.
I hope the output style helps, I used the dates as index and dedicated a column for each opening or closing hour.
And I would like to add that Forex markets operate 24 hours a day, five days a week, and they don't have centralized exchanges like stock markets. Instead, Forex trading occurs over-the-counter (OTC) with a network of banks and brokers. As a result, Forex trading doesn't have a fixed exchange schedule or a centralized calendar like traditional stock markets. To put it simple, whenever you use an online service of your bank to change, for example, EUR to USD you are in fact trading forex.. This wouldn't be possible out of market hours if you instead wanted to sell or buy a stock.