This commit is contained in:
dsyoon
2025-08-06 23:18:56 +09:00
parent 7d5a8eafcb
commit 7135bf71f0
3 changed files with 213 additions and 3 deletions

57
stock_downloader.py Normal file
View File

@@ -0,0 +1,57 @@
from HTS2 import HTS
from dateutil.relativedelta import relativedelta
from datetime import datetime
import sqlite3
from stock_monitor import get_coin_more_data
from config import *
hts = HTS()
def inserData(symbol, interval, data):
conn = sqlite3.connect('coins.db')
cursor = conn.cursor()
# 테이블/키 생성
cursor.execute("CREATE TABLE IF NOT EXISTS " + symbol + " (period text, CODE text, NAME text, ymdhms datetime, ymd text, hms text, close REAL, open REAL, high REAL, low REAL, volume REAL)")
cursor.execute("CREATE INDEX IF NOT EXISTS " + symbol + "_idx on " + symbol + "(CODE, ymdhms)")
for i in range(len(data)):
ymd = data.index[i].strftime('%Y%m%d')
hms = data.index[i].strftime('%H%M%S')
ymdhms = data.index[i].strftime('%Y-%m-%d %H:%M:%S')
open = data.Open.iloc[i]
high = data.High.iloc[i]
low = data.Low.iloc[i]
close = data.Close.iloc[i]
volume = data.Volume.iloc[i]
cursor.execute("SELECT * from " + symbol + " where CODE = ? and ymdhms = ? and interval = ?", (symbol, ymdhms, interval))
arr = cursor.fetchone()
if arr:
cursor.execute("UPDATE " + symbol + " SET close=?, open=?, high=?, low=?, volume=? where CODE=? and ymdhms=? and period=?", (close, open, high, low, volume, symbol, ymdhms, interval))
else:
cursor.execute("INSERT INTO " + symbol + " (period, CODE, NAME, ymdhms, ymd, hms, close, open, high, low, volume) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", (interval, symbol, KR_COINS[symbol], ymdhms, ymd, hms, close, open, high, low, volume))
conn.commit()
cursor.close()
conn.close()
return
def download():
for symbol in KR_COINS:
# 1시간
interval = 60
data = get_coin_more_data(symbol, interval, bong_count=10000)
if data is not None and not data.empty:
try:
inserData(symbol, interval, data)
except Exception as e:
print(f"Error processing data for {symbol}: {str(e)}")
return
if __name__ == "__main__":
download()