检查今天美国证券交易所是否开放交易的最简单方法是什么?
我希望避免的冗长方法是创建例程来解析带有日历信息的提要,例如 https://business.nasdaq.com/trade/US-Options/Holiday-Trading-Hours.html
我更喜欢 Ruby,但即使使用 curl 进行简单的 URL 查找来产生真/假,或者直到下一个市场开放的时间,也会很棒。
可选
能够查找任意日期会更好,并解决这个问题。
IE。甚至像
curl google.com/search?q="Is the NYSE open on $(date +%Y-%m-%d)"
来自 shell.
使用 Python,我发现最简单的方法是使用 pandas_market_calendars 库,它会告诉您市场日期。
import pandas_market_calendars as mcal
nyse = mcal.get_calendar('NYSE')
nyse.valid_days(start_date='2016-12-20', end_date='2017-01-10')
自己代入start_date, end_date,生成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')
检查今天的日期在那里。
我正在使用 Tradier 的 API 在我的 shell 脚本中执行此操作,然后使用 jq 获取特定日期的状态。
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')
在出现更优雅和可靠的东西之前,我可能会使用
curl -s "https://www.isthemarketopen.com/" |grep -c "** Stock market is closed"
在今天的情况下(耶稣受难日)返回 1——我可以直接在条件代码中使用——但必须定期维护和检查该 URL 的底层 HTML 源可能发生了怎样的变化。
MSN 报价包括一个属性,指示股票列表的市场是开放还是关闭:
https://www.msn.com/en-us/money/stockdetails/nas-aapl/fi-a1mou2?symbol=AAPL&form=PRMFPS
下面是一个使用 Beautiful Soup 解析器获取状态的示例 Python 脚本。
如果你担心他们改变他们的 css,我想正则表达式可能更可靠,等等。
from bs4 import BeautifulSoup
import requests
session = requests.session()
response = session.get('https://www.msn.com/en-us/money/stockdetails/nas-aapl/fi-a1mou2?symbol=AAPL&form=PRMFPS')
if response.status_code == 200:
page = response.text
soup = BeautifulSoup(page, "lxml")
market = soup.find('span', attrs={'class': 'market-status-text'}).text
if market == 'US Markets Closed':
# do stuff
print('closed')
else:
print('open')
# do different stuff
我找到了一个美国市场假期的谷歌日历,它会在接下来的几年里得到合理的更新。
尽管我讨厌添加一个单独的答案,但这是一个比之前脆弱的 URL 页面文本搜索器更好的解决方案(顺便说一句,两周后仍在返回“** 股票市场于 4 月 19 日耶稣受难日关闭/ 19" 而不是简单地在某处显示“今天市场开盘”)。
首先是代码:(带有嵌入式 Bash 的 Ruby)
$marketCal = %x'curl -s "https://www.googleapis.com/calendar/v3/calendars/[email protected]/events?key=[YOUR GOOGLE API KEY]&maxResults=1&timeMin=#{today=`date +%Y-%m-%d`.chomp}T00%3A00%3A01%2B00%3A00&timeMax=#{today}T23%3A59%3A59%2B00%3A00&"'
$isMarketOpenToday = $marketCal !~ /"status":/ # => true when there exists event "id":, etc.
由于存在资源限制,您需要去创建自己的免费 API 密钥 https://console.developers.google.com/apis/credentials?
全局变量$isMarketOpenToday如果今天没有市场假期则返回true,否则返回false。请注意,它不考虑星期几,因此在周末它可能仍会产生 true。因此,如果需要,只需进行另一个简单测试即可。例如。
[[ $(date +%u) < 6 ]] && echo "Weekday, yay!"
编程标准可以改进很多;这里的目标只是用最少的代码行测试 API,很容易从脚本外部的 CLI 检查,无论多么讨厌。
PowerShell:
function isMarketOpen(){
$url = "https://query2.finance.yahoo.com/v7/finance/quote?symbols=MSFT"
try{
$response = Invoke-RestMethod $url
#return $response.quoteResponse.result[0]
if($response.quoteResponse.result[0].marketState -eq 'CLOSED'){
return $false
}else{
return $true
}
}catch{
$_.exception | Write-Warning
return $null
}
}
isMarketOpen
前面的回答提到过,可以使用
pandas-market-calendars
,下面是一个基于纽约证券交易所的简化实现:
import pandas_market_calendars as mcal
from datetime import datetime
def market_is_open(date):
result = mcal.get_calendar("NYSE").schedule(start_date=date, end_date=date)
return result.empty == False
# check if market is open today
is_open = market_is_open(datetime.now().strftime("%Y-%m-%d"))
# check if market was open 2023-04-16 (False)
was_open = market_is_open('2023-04-16')