bill-gates/datasource/gateway.py
2025-07-09 15:30:52 +08:00

32 lines
895 B
Python

import tushare as ts
import pandas as pd
from datetime import datetime, timedelta
# Read config from config.yaml
def read_config():
import yaml
with open('config/config.yaml', 'r') as file:
config = yaml.safe_load(file)
# Ensure the config has the required keys
if 'gateway' not in config or 'token' not in config['gateway']:
raise KeyError("Missing required config key: gateway.token")
return config
def main():
config = read_config()
token = config['gateway']['token']
# Initialize Tushare with the token
ts.set_token(token)
pro = ts.pro_api()
# Get the last trading day
last_trading_day = (datetime.now() - timedelta(days=1)).strftime('%Y%m%d')
# Fetch stock data for the last trading day
df = pro.daily(trade_date=last_trading_day)
# Print the DataFrame
print(df)
if __name__ == "__main__":
main()