59 lines
2.1 KiB
Python
59 lines
2.1 KiB
Python
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 + " (interval 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 + "(interval, 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 interval=?", (close, open, high, low, volume, symbol, ymdhms, interval))
|
|
else:
|
|
cursor.execute("INSERT INTO " + symbol + " (interval, 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:
|
|
print(symbol)
|
|
|
|
# 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()
|