pull-minute-k/get_min_data.py

55 lines
1.6 KiB
Python
Raw Normal View History

2025-05-04 18:32:59 +08:00
from Ashare import *
from trade_day import *
2025-05-03 23:06:31 +08:00
import os
import time
import csv
2025-05-04 18:32:59 +08:00
date = time.strftime("%Y-%m-%d", time.localtime())
date = "2025-04-30"
save_path = os.path.split(os.path.realpath(__file__))[0]
save_path = os.path.join(save_path, date)
# 是否已经获取了数据
file_count = 0
if os.path.exists(save_path):
for root, dirs, files in os.walk(save_path):
file_count += len(files)
if file_count > 5000:
print("already pulled taday's data!")
exit()
# 今天是否是交易日
ok, trade_day = is_trade_day(date)
if not ok or not trade_day:
print("taday is not trade day, skip")
exit()
# 获取数据
2025-05-03 23:06:31 +08:00
stock_codes = {}
2025-05-04 18:32:59 +08:00
stock_list_file = os.path.join(os.path.split(os.path.realpath(__file__))[0], 'table-r.txt')
with open(stock_list_file, 'r', encoding='utf-8') as file:
2025-05-03 23:06:31 +08:00
csv_reader = csv.DictReader(file)
for row in csv_reader:
code = row["code"]
if code.startswith("sh") or code.startswith("sz"):
stock_codes[code] = row["name"]
2025-05-04 18:32:59 +08:00
if not os.path.exists(save_path):
os.makedirs(save_path)
2025-05-03 23:06:31 +08:00
for c, n in stock_codes.items():
print(f"getting stock:{c} name:{n}")
file_name = str(c) + "-" + str(n) + ".txt"
file_name = file_name.replace("*", "#")
2025-05-04 18:32:59 +08:00
file_name = os.path.join(save_path, file_name)
2025-05-03 23:06:31 +08:00
if os.path.exists(file_name):
print(f"file:{file_name} already exist, skip")
continue
try:
df = get_price(c, frequency='1m', count=240)
ctx = df.to_string()
except:
print("get price error")
ctx = ""
with open(file_name, "w", encoding="utf-8") as outfile:
outfile.write(ctx)
# time.sleep(0.01)