diff --git a/stock_monitor.py b/stock_monitor.py index 84416b6..63c0d8c 100644 --- a/stock_monitor.py +++ b/stock_monitor.py @@ -2,6 +2,7 @@ import pandas as pd from HTS2 import HTS from dateutil.relativedelta import relativedelta from datetime import datetime, timedelta +import sqlite3 import telegram import time import requests @@ -219,7 +220,7 @@ def format_ma_message(info, market_type): return message -def get_coin_data(symbol, interval=240, to=None, retries=3): +def get_coin_data(symbol, interval=60, to=None, retries=3): for attempt in range(retries): try: #url = "https://api.bithumb.com/v1/candles/minutes/{}?market=KRW-{}&count=3000".format(interval, symbol) @@ -272,8 +273,11 @@ def get_coin_more_data(symbol, interval, bong_count=3000): if data is None: data = get_coin_data(symbol, interval, to.strftime("%Y-%m-%d %H:%M:%S")) else: + p_count = len(data) df = get_coin_data(symbol, interval, to.strftime("%Y-%m-%d %H:%M:%S")) data = pd.concat([data, df], ignore_index=True) + if p_count == len(data): + break time.sleep(0.3) to = to - relativedelta(minutes=interval * 200) @@ -285,6 +289,46 @@ def get_coin_more_data(symbol, interval, bong_count=3000): return data +def get_coin_saved_data(symbol, interval, data): + conn = sqlite3.connect('coins.db') + cursor = conn.cursor() + + for i in range(2, len(data)): + cursor.execute("SELECT * from " + symbol + " where CODE = ? and ymdhms = ? and interval = ?", (symbol, data['datetime'].iloc[-i].strftime('%Y-%m-%d %H:%M:%S'), interval)) + arr = cursor.fetchone() + if not arr: + cursor.execute("INSERT INTO " + symbol + " (interval, CODE, NAME, ymdhms, ymd, hms, close, open, high, low, volume) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", (interval, symbol, KR_COINS[symbol], data['datetime'].iloc[-i].strftime('%Y-%m-%d %H:%M:%S'), data['datetime'].iloc[-i].strftime('%Y%m%d'), data['datetime'].iloc[-i].strftime('%H%M%S'), data['Close'].iloc[-i], data['Open'].iloc[-i], data['High'].iloc[-i], data['Low'].iloc[-i], data['Volume'].iloc[-i])) + else: + break + + cursor.execute("select * from (SELECT Open,Close,High,Low,Volume,ymdhms as datetime from " + symbol + " order by ymdhms desc limit 5000) subquery order by ymdhms") + result = cursor.fetchall() + conn.commit() + cursor.close() + conn.close() + + saved_data = pd.DataFrame(result) + + data = pd.concat([data, saved_data], ignore_index=True) + data = data.set_index('datetime') + data = data.sort_index() + data = data.drop_duplicates(keep='first') + data["datetime"] = data.index + + return + +def get_coin_some_data(symbol, interval): + data = get_coin_data(symbol, interval) + saved_data = get_coin_saved_data(symbol, interval, data) + + data = data.set_index('datetime') + data = data.sort_index() + data = data.drop_duplicates(keep='first') + data["datetime"] = data.index + # 코인 데이터 1500개 봉 가져오기 + + return data + def get_kr_stock_data(symbol, retries=3): for attempt in range(retries): @@ -391,7 +435,7 @@ def monitor_coins(): # 1시간 interval = 60 - data = get_coin_more_data(symbol, interval) + data = get_coin_some_data(symbol, interval) if data is not None and not data.empty: try: @@ -456,5 +500,5 @@ def run_schedule(): if __name__ == "__main__": - run_schedule() - #monitor_coins() + #run_schedule() + monitor_coins()