Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

programmatically check if today is a market open trading day

What might be the simplest way to check if today the U.S. stock exchanges are open for trading?

The prolonged way I am hoping to avoid is by creating routines to parse a feed with calendar info like https://business.nasdaq.com/trade/US-Options/Holiday-Trading-Hours.html

I prefer Ruby, but even a simple URL lookup with curl to yield a true/false, or time until next market open, would be great.

OPTIONAL

To be able to lookup any arbitrary date would be better, and solve this too.
I.e. Even something like curl google.com/search?q="Is the NYSE open on $(date +%Y-%m-%d)" from the shell.

like image 238
Marcos Avatar asked Jan 26 '23 10:01

Marcos


2 Answers

With Python, the easiest way I have found is using the pandas_market_calendars library, which tells you the market days.

import pandas_market_calendars as mcal
nyse = mcal.get_calendar('NYSE')
nyse.valid_days(start_date='2016-12-20', end_date='2017-01-10')

Substitute start_date, end_date for yourself and generate the follow range.

DatetimeIndex(['2016-12-20 00:00:00+00:00', '2016-12-21 00:00:00+00:00',
               '2016-12-22 00:00:00+00:00', '2016-12-23 00:00:00+00:00',
               '2016-12-27 00:00:00+00:00', '2016-12-28 00:00:00+00:00',
               '2016-12-29 00:00:00+00:00', '2016-12-30 00:00:00+00:00',
               '2017-01-03 00:00:00+00:00', '2017-01-04 00:00:00+00:00',
               '2017-01-05 00:00:00+00:00', '2017-01-06 00:00:00+00:00',
               '2017-01-09 00:00:00+00:00', '2017-01-10 00:00:00+00:00'],
              dtype='datetime64[ns, UTC]', freq='C')

Check that the date today is in there.

like image 72
momo668 Avatar answered May 16 '23 07:05

momo668


I'm using Tradier's API to do this in my shell scripts, then using jq to get the status for a particular day.

date="your date"
month=$(date -d $date +%-m)
year=$(date -d $date +%Y)
calendar=$(curl -H "Authorization: Bearer "$tradierApi"" -H "Accept: application/json" "https://production-api.tradier.com/v1/markets/calendar?month="$month"&year="$year)
marketStatus=$(echo $calendar | ./jq-linux64 '.calendar.days.day[] | select(.date == "'$date'") | .status')
like image 20
cwood Avatar answered May 16 '23 08:05

cwood