Skip to main content

交易日历

Project description

Trading Calendars(tcalendars)

交易日历,用于交易时间的判断。当前支持的市场有:

  • 中国股票市场(2005年1月1日起)

安装

pip install playwright
playwright install chromium

pip install tcalendars

缓存

  • StockNameCodeHelper.get_stock_code_by_english_name / StockNameCodeHelper.get_stock_info_by_english_name 会通过 Playwright 调用 Yahoo Finance 搜索接口
  • 所有数据缓存文件统一存放在 tcalendars/cache/ 目录下:
    • 交易日历:tcalendars/cache/se_calendar.csvTradingCalendars 会在需要时自动追加更新)
    • 股票名称代码:tcalendars/cache/stock_name_code.csvStockNameCodeHelper 会按天更新)
    • 基金名称代码:tcalendars/cache/fund_name_code.csvFundNameCodeHelper 会按天更新)
    • Yahoo Finance 查询:tcalendars/cache/.yfinance_cache(启动时自动加载到内存缓存)
  • .yfinance_cache 超过 60 天的缓存不会加载到内存(删除该文件可重建缓存)

示例

代码示例

from tcalendars import TradingCalendars

calendar = TradingCalendars()
# 判断2023年1月1日是否为交易日
calendar.is_trading_day('2023-01-01')
# 输出:False

# 获取2023年1月1日至2023年1月5日的所有交易日
calendar.get_trading_days('2023-01-01', '2023-01-05')
# 输出:['2023-01-03', '2023-01-04', '2023-01-05']

# 获取2023年1月1日所在的交易日,如果1月1日不是交易日,则返回后一个交易日
calendar.get_trading_day('2023-01-01')
# 输出:'2023-01-03'

from tcalendars import StockNameCodeHelper

helper = StockNameCodeHelper()

# 根据股票代码获取股票名称
helper.get_stock_name('000001')
# 输出:'平安银行'

# 根据股票名称获取股票代码
helper.get_stock_code('平安银行')
# 输出:'000001'

# 根据股票英文名称获取股票代码
StockNameCodeHelper.get_stock_code_by_english_name('PONY AI')
# 输出:'PONY'

StockNameCodeHelper.get_stock_code_by_english_name("HESAI GROUP")
# 输出:'HESAI'

# 根据股票英文名称获取股票信息
StockNameCodeHelper.get_stock_info_by_english_name("HESAI GROUP")
# 输出:{'exchange': 'NMS', 'shortname': 'Hesai Group', 'quoteType': 'EQUITY', 'symbol': 'HSAI', 'index': 'quotes', 'score': 20006.0, 'typeDisp': '股票', 'longname': 'Hesai Group', 'exchDisp': 'NASDAQ', 'sector': 'Consumer Cyclical', 'sectorDisp': '消費週期性股票', 'industry': 'Auto Parts', 'industryDisp': '汽車零件', 'isYahooFinance': True}

from tcalendars import FundNameCodeHelper

fund_helper = FundNameCodeHelper()

# 根据基金代码获取基金名称
fund_helper.get_fund_name('000001')

# 根据基金名称获取基金代码
fund_helper.get_fund_code('华夏成长混合')

# 查询基金关联份额
fund_helper.query_shares('000001')

# 按关键词搜索基金名称
fund_helper.search_by_keyword('华夏')
# 输出:DataFrame
#   code name
# 0  000001 华夏成长混合
# ...

StockNameCodeHelper.get_stock_info_by_english_name 返回结果示例:

{
  "exchange": "NMS",
  "shortname": "Hesai Group",
  "quoteType": "EQUITY",
  "symbol": "HSAI",
  "index": "quotes",
  "score": 20012,
  "typeDisp": "equity",
  "longname": "Hesai Group",
  "exchDisp": "NASDAQ",
  "sector": "Consumer Cyclical",
  "sectorDisp": "消費週期性股票",
  "industry": "Auto Parts",
  "industryDisp": "汽車零件",
  "isYahooFinance": True
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tcalendars-2.0.0.tar.gz (324.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tcalendars-2.0.0-py3-none-any.whl (327.1 kB view details)

Uploaded Python 3

File details

Details for the file tcalendars-2.0.0.tar.gz.

File metadata

  • Download URL: tcalendars-2.0.0.tar.gz
  • Upload date:
  • Size: 324.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.2

File hashes

Hashes for tcalendars-2.0.0.tar.gz
Algorithm Hash digest
SHA256 1e7082cdf08890e738ecc73895b5daebeb65df24dda24d473695f46f9239879f
MD5 e59d1b8f7609538927e2012e85b5510c
BLAKE2b-256 3f81a8a26538b70312754a88f25d641ceb8f7dd271dd3283d5cfeba60496aedb

See more details on using hashes here.

File details

Details for the file tcalendars-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: tcalendars-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 327.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.2

File hashes

Hashes for tcalendars-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2af6146f8a3e7529b4b59f5fd9f0352c21ec7598f27ccaf1d92074f7426bf263
MD5 f09e7896573aaeb78e630b37b026fd1e
BLAKE2b-256 0e176d4ad39cb514b2c9dfe61e5df9a42158bf3aa6510270a59ed36621283dab

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page