add stat files.

This commit is contained in:
2024-10-09 11:46:30 +08:00
parent 50bbcd7ca5
commit d1089007bb
21 changed files with 6499 additions and 412 deletions

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,62 @@
import pymysql
import pandas as pd
from futu import *
import logging
from config import db_config # 引用config.py中的数据库配置
# 设置日志
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s - %(message)s')
logger = logging.getLogger()
# 数据库连接函数
def connect_to_db():
return pymysql.connect(**db_config)
# 从sp300表中获取所有股票代码
def fetch_sp300_codes(connection):
query = "SELECT code FROM sp300"
return pd.read_sql(query, connection)
# 获取市场快照并保存到 CSV 文件
def get_market_snapshot_and_save_to_csv(stock_codes, output_file):
quote_ctx = OpenQuoteContext(host='127.0.0.1', port=11111)
ret, data = quote_ctx.get_market_snapshot(stock_codes)
if ret == RET_OK:
logger.info(f"Successfully fetched market snapshot for {len(stock_codes)} codes.")
# 将数据写入CSV文件
data.to_csv(output_file, index=False)
logger.info(f"Snapshot data saved to {output_file}")
else:
logger.error(f"Error fetching market snapshot: {data}")
quote_ctx.close()
# 主函数
def main():
try:
# 连接数据库
connection = connect_to_db()
# 从 sp300 表中获取所有的股票代码
sp300_codes_df = fetch_sp300_codes(connection)
# 提取股票代码列表
stock_codes = sp300_codes_df['code'].tolist()
if not stock_codes:
logger.warning("No stock codes found in sp300 table.")
return
# 获取市场快照并保存到 CSV 文件
output_file = "market_snapshot.csv"
get_market_snapshot_and_save_to_csv(stock_codes, output_file)
except Exception as e:
logger.error(f"An error occurred: {e}")
finally:
if connection:
connection.close()
if __name__ == "__main__":
main()

View File

@ -1,15 +1,28 @@
import yfinance as yf
import pandas as pd
code = 'KDP'
# 获取AAPL的股票数据
stock = yf.Ticker("AAPL")
stock = yf.Ticker(code)
# 获取过去十年的日K线数据前复权
hist_data = stock.history(period="10y", auto_adjust=True)
print (hist_data['Close'].resample('Y').last().pct_change)
# 打印数据前几行
print(hist_data.head())
# 保存到CSV文件
hist_data.to_csv(f"{code}_10year_data_adjust.csv")
# 获取过去十年的日K线数据不复权
hist_data = stock.history(period="10y", auto_adjust=False)
# 打印数据前几行
print(hist_data.head())
# 保存到CSV文件
hist_data.to_csv("AAPL_10year_data.csv")
hist_data.to_csv(f"{code}_10year_data.csv")