mirror of
https://github.com/Xe138/AI-Trader.git
synced 2026-04-01 17:17:24 -04:00
Streamline the data preparation workflow by having get_daily_price.py automatically invoke merge_jsonl.py after fetching all stock prices. Changes: - Modified get_daily_price.py to call merge_jsonl.py automatically - Updated entrypoint.sh to remove redundant merge_jsonl.py call - Updated main.sh to remove redundant merge_jsonl.py call - Fixed import order for linting compliance Benefits: - Single command now handles both fetching and merging - Ensures merged.jsonl is always created after price updates - Simplifies Docker container startup process - Prevents missing merged.jsonl errors in production
60 lines
2.4 KiB
Python
60 lines
2.4 KiB
Python
import json
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
|
|
import requests
|
|
from dotenv import load_dotenv
|
|
|
|
load_dotenv()
|
|
|
|
|
|
all_nasdaq_100_symbols = [
|
|
"NVDA", "MSFT", "AAPL", "GOOG", "GOOGL", "AMZN", "META", "AVGO", "TSLA",
|
|
"NFLX", "PLTR", "COST", "ASML", "AMD", "CSCO", "AZN", "TMUS", "MU", "LIN",
|
|
"PEP", "SHOP", "APP", "INTU", "AMAT", "LRCX", "PDD", "QCOM", "ARM", "INTC",
|
|
"BKNG", "AMGN", "TXN", "ISRG", "GILD", "KLAC", "PANW", "ADBE", "HON",
|
|
"CRWD", "CEG", "ADI", "ADP", "DASH", "CMCSA", "VRTX", "MELI", "SBUX",
|
|
"CDNS", "ORLY", "SNPS", "MSTR", "MDLZ", "ABNB", "MRVL", "CTAS", "TRI",
|
|
"MAR", "MNST", "CSX", "ADSK", "PYPL", "FTNT", "AEP", "WDAY", "REGN", "ROP",
|
|
"NXPI", "DDOG", "AXON", "ROST", "IDXX", "EA", "PCAR", "FAST", "EXC", "TTWO",
|
|
"XEL", "ZS", "PAYX", "WBD", "BKR", "CPRT", "CCEP", "FANG", "TEAM", "CHTR",
|
|
"KDP", "MCHP", "GEHC", "VRSK", "CTSH", "CSGP", "KHC", "ODFL", "DXCM", "TTD",
|
|
"ON", "BIIB", "LULU", "CDW", "GFS"
|
|
]
|
|
|
|
def get_daily_price(SYMBOL: str):
|
|
FUNCTION = "TIME_SERIES_DAILY"
|
|
OUTPUTSIZE = 'compact'
|
|
APIKEY = os.getenv("ALPHAADVANTAGE_API_KEY")
|
|
url = f'https://www.alphavantage.co/query?function={FUNCTION}&symbol={SYMBOL}&outputsize={OUTPUTSIZE}&apikey={APIKEY}'
|
|
r = requests.get(url)
|
|
data = r.json()
|
|
if data.get('Note') is not None or data.get('Information') is not None:
|
|
print(f"⚠️ {SYMBOL}: API rate limit or error - {data.get('Note') or data.get('Information')}")
|
|
return
|
|
print(f"✓ Fetched {SYMBOL}")
|
|
with open(f'./daily_prices_{SYMBOL}.json', 'w', encoding='utf-8') as f:
|
|
json.dump(data, f, ensure_ascii=False, indent=4)
|
|
if SYMBOL == "QQQ":
|
|
with open(f'./Adaily_prices_{SYMBOL}.json', 'w', encoding='utf-8') as f:
|
|
json.dump(data, f, ensure_ascii=False, indent=4)
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
for symbol in all_nasdaq_100_symbols:
|
|
get_daily_price(symbol)
|
|
|
|
get_daily_price("QQQ")
|
|
|
|
# Automatically run merge after fetching
|
|
print("\n📦 Merging price data...")
|
|
try:
|
|
script_dir = os.path.dirname(os.path.abspath(__file__))
|
|
merge_script = os.path.join(script_dir, "merge_jsonl.py")
|
|
subprocess.run([sys.executable, merge_script], check=True)
|
|
print("✅ Price data merged successfully")
|
|
except Exception as e:
|
|
print(f"⚠️ Failed to merge data: {e}")
|
|
print(" Please run 'python merge_jsonl.py' manually") |