WLD 전용 BB MTF 전략 및 HTML 시뮬 최적화
- strategy.py, candle_features.py, rule_discovery.py로 다봉 BB·캔들 규칙 탐색 - simulation_1h.py: discover 명령, 기본 BB vs 탐색 규칙 자동 선택, Plotly Y축 줌 - mtf_bb.py, downloader/monitor 정리, 다코인 파일 제거 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
566
monitor.py
566
monitor.py
@@ -1,34 +1,40 @@
|
||||
import pandas as pd
|
||||
from HTS2 import HTS
|
||||
from dateutil.relativedelta import relativedelta
|
||||
from datetime import datetime, timedelta
|
||||
from datetime import datetime
|
||||
import sqlite3
|
||||
import telegram
|
||||
import time
|
||||
|
||||
try:
|
||||
import telegram
|
||||
except ImportError:
|
||||
telegram = None # type: ignore
|
||||
import requests
|
||||
import json
|
||||
import asyncio
|
||||
from multiprocessing import Pool
|
||||
import FinanceDataReader as fdr
|
||||
import numpy as np
|
||||
import os
|
||||
|
||||
from config import *
|
||||
from HTS2 import HTS
|
||||
import strategy
|
||||
|
||||
class Monitor(HTS):
|
||||
"""자산(코인/주식/ETF) 모니터링 및 매수 실행 클래스"""
|
||||
"""WLD 코인 모니터링 및 매매 실행."""
|
||||
|
||||
last_signal = None
|
||||
cooldown_file = None
|
||||
|
||||
def __init__(self, cooldown_file='coins_buy_time.json') -> None:
|
||||
self.hts = HTS()
|
||||
HTS.__init__(self)
|
||||
# 최근 매수 신호 저장용(파일은 [신규] 포맷으로 저장)
|
||||
self.last_signal: dict[str, str] = {}
|
||||
if cooldown_file is not None:
|
||||
self.cooldown_file = cooldown_file
|
||||
self.buy_cooldown = self._load_buy_cooldown()
|
||||
else:
|
||||
self.cooldown_file = None
|
||||
self.buy_cooldown = {}
|
||||
|
||||
# ------------- Persistence -------------
|
||||
def _load_buy_cooldown(self) -> dict:
|
||||
@@ -106,13 +112,12 @@ class Monitor(HTS):
|
||||
|
||||
# ------------- Telegram -------------
|
||||
def _send_coin_msg(self, text: str) -> None:
|
||||
if telegram is None:
|
||||
print(f"[telegram skip] {text}")
|
||||
return
|
||||
coin_client = telegram.Bot(token=COIN_TELEGRAM_BOT_TOKEN)
|
||||
asyncio.run(coin_client.send_message(chat_id=COIN_TELEGRAM_CHAT_ID, text=text))
|
||||
|
||||
def _send_stock_msg(self, text: str) -> None:
|
||||
stock_client = telegram.Bot(token=STOCK_TELEGRAM_BOT_TOKEN)
|
||||
asyncio.run(stock_client.send_message(chat_id=STOCK_TELEGRAM_CHAT_ID, text=text))
|
||||
|
||||
def sendMsg(self, msg):
|
||||
try:
|
||||
pool = Pool(12)
|
||||
@@ -133,18 +138,6 @@ class Monitor(HTS):
|
||||
pool = Pool(12)
|
||||
pool.map(self._send_coin_msg, [payload])
|
||||
|
||||
def send_stock_telegram_message(self, message_list: list[str], header: str) -> None:
|
||||
payload = header + "\n"
|
||||
for i, message in enumerate(message_list):
|
||||
payload += message + "\n"
|
||||
if i + 1 % 20 == 0:
|
||||
pool = Pool(12)
|
||||
pool.map(self._send_stock_msg, [payload])
|
||||
payload = ''
|
||||
if len(message_list) % 20 != 0:
|
||||
pool = Pool(12)
|
||||
pool.map(self._send_stock_msg, [payload])
|
||||
|
||||
# ------------- Indicators -------------
|
||||
def normalize_data(self, data: pd.DataFrame) -> pd.DataFrame:
|
||||
columns_to_normalize = ['Open', 'High', 'Low', 'Close', 'Volume']
|
||||
@@ -224,236 +217,169 @@ class Monitor(HTS):
|
||||
|
||||
return data
|
||||
|
||||
# ------------- Strategy -------------
|
||||
def buy_sell_ticker_1h(self, symbol: str, data: pd.DataFrame, balances=None, is_inverse: bool = False) -> bool:
|
||||
# ------------- Strategy (strategy.py에 구현) -------------
|
||||
def annotate_signals(self, symbol: str, data: pd.DataFrame, simulation: bool | None = None) -> pd.DataFrame:
|
||||
"""strategy.annotate_signals에 위임."""
|
||||
return strategy.annotate_signals(
|
||||
symbol, data, simulation=simulation, config=strategy.ACTIVE_CONFIG
|
||||
)
|
||||
|
||||
def _is_in_cooldown(self, symbol: str, side: str) -> bool:
|
||||
"""매수/매도 쿨다운 여부."""
|
||||
if self.cooldown_file is None:
|
||||
return False
|
||||
last_dt = self.buy_cooldown.get(symbol, {}).get(side, {}).get("datetime")
|
||||
if not last_dt:
|
||||
return False
|
||||
limit = BUY_COOLDOWN_SEC if side == "buy" else SELL_COOLDOWN_SEC
|
||||
elapsed = (datetime.now() - last_dt).total_seconds()
|
||||
if elapsed < limit:
|
||||
print(f"{symbol}: {side} 쿨다운 중 (남은 시간: {limit - elapsed:.0f}초)")
|
||||
return True
|
||||
return False
|
||||
|
||||
def _record_trade(self, symbol: str, side: str, signal: str) -> None:
|
||||
"""매매 기록 저장."""
|
||||
if self.cooldown_file is None:
|
||||
return
|
||||
current_time = datetime.now()
|
||||
self.last_signal[symbol] = signal
|
||||
self.buy_cooldown.setdefault(symbol, {})[side] = {
|
||||
"datetime": current_time,
|
||||
"signal": signal,
|
||||
}
|
||||
self._save_buy_cooldown()
|
||||
|
||||
def execute_trade_signal(
|
||||
self,
|
||||
symbol: str,
|
||||
trade: strategy.TradeSignal,
|
||||
balances: dict | None = None,
|
||||
) -> bool:
|
||||
"""TradeSignal 1건에 대해 현물 매수 또는 매도를 실행합니다."""
|
||||
try:
|
||||
# 신호 생성 및 최신 포인트 확인
|
||||
data = self.annotate_signals(symbol, data)
|
||||
if data['point'].iloc[-1] != 1:
|
||||
return False
|
||||
coin_name = KR_COINS.get(symbol, symbol)
|
||||
signal_name = trade.signal
|
||||
close = trade.close
|
||||
|
||||
if is_inverse:
|
||||
# BUY_MINUTE_LIMIT 이내라면 매수하지 않음
|
||||
current_time = datetime.now()
|
||||
last_buy_dt = self.buy_cooldown.get(symbol, {}).get('sell', {}).get('datetime')
|
||||
if last_buy_dt:
|
||||
time_diff = current_time - last_buy_dt
|
||||
if time_diff.total_seconds() < BUY_MINUTE_LIMIT:
|
||||
print(f"{symbol}: 매수 금지 중 (남은 시간: {BUY_MINUTE_LIMIT - time_diff.total_seconds():.0f}초)")
|
||||
return False
|
||||
|
||||
# 인버스 데이터: 매수 신호를 매도로 처리 (fall_6p, deviation40 만 허용)
|
||||
# 허용된 인버스 매도 신호만 처리
|
||||
last_signal = str(data['signal'].iloc[-1]) if 'signal' in data.columns else ''
|
||||
if last_signal not in ['fall_6p', 'deviation40']:
|
||||
if trade.action == "sell":
|
||||
if self._is_in_cooldown(symbol, "sell"):
|
||||
return False
|
||||
available_balance = 0
|
||||
try:
|
||||
if balances and symbol in balances:
|
||||
available_balance = float(balances[symbol].get('balance', 0))
|
||||
except Exception:
|
||||
available_balance = 0
|
||||
if available_balance <= 0:
|
||||
available = 0.0
|
||||
if balances and symbol in balances:
|
||||
available = float(balances[symbol].get("balance", 0))
|
||||
if available <= 0:
|
||||
print(f"{symbol}: 매도 신호({signal_name}) — 보유 없음, 스킵")
|
||||
return False
|
||||
sell_amount = available_balance * 0.7
|
||||
_ = self.hts.sellCoinMarket(symbol, 0, sell_amount)
|
||||
if self.cooldown_file is not None:
|
||||
try:
|
||||
self.last_signal[symbol] = str(data['signal'].iloc[-1])
|
||||
except Exception:
|
||||
self.last_signal[symbol] = ''
|
||||
self.buy_cooldown.setdefault(symbol, {})['sell'] = {'datetime': current_time, 'signal': str(data['signal'].iloc[-1])}
|
||||
self._save_buy_cooldown()
|
||||
|
||||
print(f"{KR_COINS[symbol]} ({symbol}) [{data['signal'].iloc[-1]} 매도], 현재가: {data['Close'].iloc[-1]:.4f}")
|
||||
self.sendMsg("[KRW-COIN]\n" + f"• 매도 [COIN] {KR_COINS[symbol]} ({symbol}): {data['signal'].iloc[-1]} ({'₩'}{data['Close'].iloc[-1]:.4f})")
|
||||
sell_amount = available * strategy.get_sell_ratio(symbol, signal_name)
|
||||
if sell_amount <= 0:
|
||||
return False
|
||||
self.sellCoinMarket(symbol, 0, sell_amount)
|
||||
self._record_trade(symbol, "sell", signal_name)
|
||||
print(f"{coin_name} ({symbol}) [매도 {signal_name}] ₩{close:.4f}, 수량 {sell_amount:.6f}")
|
||||
self.sendMsg(
|
||||
f"[KRW-COIN]\n• 매도 {coin_name} ({symbol}): {signal_name} ₩{close:.4f}"
|
||||
)
|
||||
return True
|
||||
|
||||
else:
|
||||
check_5_week_lowest = False
|
||||
|
||||
# BUY_MINUTE_LIMIT 이내라면 매수하지 않음
|
||||
current_time = datetime.now()
|
||||
last_buy_dt = self.buy_cooldown.get(symbol, {}).get('buy', {}).get('datetime')
|
||||
if last_buy_dt:
|
||||
time_diff = current_time - last_buy_dt
|
||||
if time_diff.total_seconds() < BUY_MINUTE_LIMIT:
|
||||
print(f"{symbol}: 매수 금지 중 (남은 시간: {BUY_MINUTE_LIMIT - time_diff.total_seconds():.0f}초)")
|
||||
return False
|
||||
|
||||
try:
|
||||
# 5주봉이 20주봉이나 40주봉보다 아래에 있는지 체크
|
||||
# Convert hourly data to week-based rolling periods (5, 20, 40 weeks)
|
||||
hours_in_week = 24 * 7 # 168 hours
|
||||
period_5w = 5 * hours_in_week # 840 hours
|
||||
period_20w = 20 * hours_in_week # 3,360 hours
|
||||
period_40w = 40 * hours_in_week # 6,720 hours
|
||||
|
||||
if len(data) >= period_40w:
|
||||
wma5 = data['Close'].rolling(window=period_5w).mean().iloc[-1]
|
||||
wma20 = data['Close'].rolling(window=period_20w).mean().iloc[-1]
|
||||
wma40 = data['Close'].rolling(window=period_40w).mean().iloc[-1]
|
||||
|
||||
# 5-week MA is the lowest among 5, 20, 40 week MAs
|
||||
if (wma5 < wma20) and (wma5 < wma40):
|
||||
check_5_week_lowest = True
|
||||
|
||||
except Exception:
|
||||
# Ignore errors in MA calculation so as not to block trading logic
|
||||
pass
|
||||
|
||||
# 체크: fall_6p
|
||||
buy_amount = 5100
|
||||
current_time = datetime.now()
|
||||
if data['signal'].iloc[-1] == 'fall_6p':
|
||||
if data['Close'].iloc[-1] > 100:
|
||||
buy_amount = 300000
|
||||
else:
|
||||
buy_amount = 150000
|
||||
elif data['signal'].iloc[-1] == 'movingaverage':
|
||||
buy_amount = 10000
|
||||
elif data['signal'].iloc[-1] == 'deviation40':
|
||||
buy_amount = 30000
|
||||
elif data['signal'].iloc[-1] == 'deviation240':
|
||||
buy_amount = 7000
|
||||
elif data['signal'].iloc[-1] == 'deviation1440':
|
||||
if symbol in ['BONK', 'PEPE', 'TON']:
|
||||
buy_amount = 50000
|
||||
else:
|
||||
buy_amount = 70000
|
||||
|
||||
if data['signal'].iloc[-1] in ['movingaverage', 'deviation40', 'deviation240', 'deviation1440']:
|
||||
if check_5_week_lowest:
|
||||
buy_amount *= 2
|
||||
|
||||
# 매수를 진행함
|
||||
buy_amount = self.hts.buyCoinMarket(symbol, buy_amount)
|
||||
|
||||
# 최근 매수 신호를 함께 기록하여 [신규] 포맷으로 저장
|
||||
if self.cooldown_file is not None:
|
||||
try:
|
||||
self.last_signal[symbol] = str(data['signal'].iloc[-1])
|
||||
except Exception:
|
||||
self.last_signal[symbol] = ''
|
||||
self.buy_cooldown.setdefault(symbol, {})['buy'] = {'datetime': current_time, 'signal': str(data['signal'].iloc[-1])}
|
||||
|
||||
# 매수를 저장함
|
||||
self._save_buy_cooldown()
|
||||
|
||||
print(f"{KR_COINS[symbol]} ({symbol}) [{data['signal'].iloc[-1]}], 현재가: {data['Close'].iloc[-1]:.4f}, {int(BUY_MINUTE_LIMIT/60)}분간 매수 금지 시작")
|
||||
self.sendMsg("{}".format(self.format_message(symbol, KR_COINS[symbol], data['Close'].iloc[-1], data['signal'].iloc[-1], buy_amount)))
|
||||
if self._is_in_cooldown(symbol, "buy"):
|
||||
return False
|
||||
buy_amount = strategy.get_buy_amount(
|
||||
symbol, signal_name, close, trend=trade.trend
|
||||
)
|
||||
if strategy.should_double_buy(symbol, signal_name, pd.DataFrame()):
|
||||
buy_amount *= 2
|
||||
executed = self.buyCoinMarket(symbol, buy_amount)
|
||||
self._record_trade(symbol, "buy", signal_name)
|
||||
print(
|
||||
f"{coin_name} ({symbol}) [매수 {signal_name}] ₩{close:.4f} "
|
||||
f"({buy_amount} KRW, 추세={trade.trend})"
|
||||
)
|
||||
self.sendMsg(
|
||||
self.format_message(
|
||||
symbol, coin_name, close, signal_name, executed or buy_amount
|
||||
)
|
||||
)
|
||||
return True
|
||||
except Exception as e:
|
||||
print(f"Error buying {symbol}: {str(e)}")
|
||||
print(f"Error trading {symbol}: {str(e)}")
|
||||
return False
|
||||
return True
|
||||
|
||||
def annotate_signals(self, symbol: str, data: pd.DataFrame, simulation: bool | None = None) -> pd.DataFrame:
|
||||
data = data.copy()
|
||||
data['signal'] = ''
|
||||
data['point'] = 0
|
||||
if data['point'].iloc[-1] != 1:
|
||||
for i in range(1, len(data)):
|
||||
if all(data[f'MA{n}'].iloc[i] < data['MA720'].iloc[i] for n in [5, 20, 40, 120, 200, 240]) and \
|
||||
all(data[f'MA{n}'].iloc[i] > data[f'MA{n}'].iloc[i - 1] for n in [5, 20, 40, 120, 200, 240]) and \
|
||||
data['MA720'].iloc[i] < data['MA1440'].iloc[i]:
|
||||
data.at[data.index[i], 'signal'] = 'movingaverage'
|
||||
data.at[data.index[i], 'point'] = 1
|
||||
if not simulation and data['point'][-3:].sum() > 0:
|
||||
data.at[data.index[-1], 'signal'] = 'movingaverage'
|
||||
data.at[data.index[-1], 'point'] = 1
|
||||
def process_wld_mtf(self, symbol: str, balances: dict | None = None) -> None:
|
||||
"""
|
||||
WLD MTF: 모든 봉 BB 상태 비교 후 정책에 따라 매수/매도.
|
||||
|
||||
if data['Deviation40'].iloc[i - 1] < data['Deviation40'].iloc[i] and data['Deviation40'].iloc[i - 1] <= 90:
|
||||
data.at[data.index[i], 'signal'] = 'deviation40'
|
||||
data.at[data.index[i], 'point'] = 1
|
||||
if not simulation and data['point'][-3:].sum() > 0:
|
||||
data.at[data.index[-1], 'signal'] = 'deviation40'
|
||||
data.at[data.index[-1], 'point'] = 1
|
||||
mtf_bb_policy.json 이 있으면 해당 정책, 없으면 ACTIVE_MTF_POLICY 사용.
|
||||
"""
|
||||
from mtf_bb import load_frames_from_db, load_policy, print_latest_states
|
||||
|
||||
if symbol not in ['BONK']:
|
||||
if symbol in ['TRX']:
|
||||
if data['Deviation240'].iloc[i - 1] < data['Deviation240'].iloc[i] and data['Deviation240'].iloc[i - 1] <= 98:
|
||||
data.at[data.index[i], 'signal'] = 'deviation240'
|
||||
data.at[data.index[i], 'point'] = 1
|
||||
if not simulation and data['point'][-3:].sum() > 0:
|
||||
data.at[data.index[-1], 'signal'] = 'deviation240'
|
||||
data.at[data.index[-1], 'point'] = 1
|
||||
else:
|
||||
if data['Deviation240'].iloc[i - 1] < data['Deviation240'].iloc[i] and data['Deviation240'].iloc[i - 1] <= 90:
|
||||
data.at[data.index[i], 'signal'] = 'deviation240'
|
||||
data.at[data.index[i], 'point'] = 1
|
||||
if not simulation and data['point'][-3:].sum() > 0:
|
||||
data.at[data.index[-1], 'signal'] = 'deviation240'
|
||||
data.at[data.index[-1], 'point'] = 1
|
||||
try:
|
||||
frames = load_frames_from_db(self, symbol)
|
||||
if not frames:
|
||||
print(f"Data for {symbol}: 로드된 봉 없음.")
|
||||
return
|
||||
|
||||
if symbol in ['TON']:
|
||||
if data['Deviation1440'].iloc[i - 1] < data['Deviation1440'].iloc[i] and data['Deviation1440'].iloc[i - 1] <= 89:
|
||||
data.at[data.index[i], 'signal'] = 'deviation1440'
|
||||
data.at[data.index[i], 'point'] = 1
|
||||
if not simulation and data['point'][-3:].sum() > 0:
|
||||
data.at[data.index[-1], 'signal'] = 'deviation1440'
|
||||
data.at[data.index[-1], 'point'] = 1
|
||||
elif symbol in ['XRP']:
|
||||
if data['Deviation1440'].iloc[i - 1] < data['Deviation1440'].iloc[i] and data['Deviation1440'].iloc[i - 1] <= 90:
|
||||
data.at[data.index[i], 'signal'] = 'deviation1440'
|
||||
data.at[data.index[i], 'point'] = 1
|
||||
if not simulation and data['point'][-3:].sum() > 0:
|
||||
data.at[data.index[-1], 'signal'] = 'deviation1440'
|
||||
data.at[data.index[-1], 'point'] = 1
|
||||
elif symbol in ['BONK']:
|
||||
if data['Deviation1440'].iloc[i - 1] < data['Deviation1440'].iloc[i] and data['Deviation1440'].iloc[i - 1] <= 76:
|
||||
data.at[data.index[i], 'signal'] = 'deviation1440'
|
||||
data.at[data.index[i], 'point'] = 1
|
||||
if not simulation and data['point'][-3:].sum() > 0:
|
||||
data.at[data.index[-1], 'signal'] = 'deviation1440'
|
||||
data.at[data.index[-1], 'point'] = 1
|
||||
else:
|
||||
if data['Deviation1440'].iloc[i - 1] < data['Deviation1440'].iloc[i] and data['Deviation1440'].iloc[i - 1] <= 80:
|
||||
data.at[data.index[i], 'signal'] = 'deviation1440'
|
||||
data.at[data.index[i], 'point'] = 1
|
||||
if not simulation and data['point'][-3:].sum() > 0:
|
||||
data.at[data.index[-1], 'signal'] = 'deviation1440'
|
||||
data.at[data.index[-1], 'point'] = 1
|
||||
df_1d = frames.get(TREND_INTERVAL_1D)
|
||||
df_1h = frames.get(TREND_INTERVAL_1H)
|
||||
if df_1d is None or df_1d.empty:
|
||||
df_1d = frames.get(ENTRY_INTERVAL)
|
||||
if df_1h is None or df_1h.empty:
|
||||
df_1h = frames.get(ENTRY_INTERVAL)
|
||||
|
||||
# Deviation720 상향 돌파 매수 (92, 93)
|
||||
try:
|
||||
prev_d720 = data['Deviation720'].iloc[i - 1]
|
||||
curr_d720 = data['Deviation720'].iloc[i]
|
||||
# 92 상향 돌파
|
||||
if prev_d720 < 92 and curr_d720 >= 92:
|
||||
data.at[data.index[i], 'signal'] = 'Deviation720'
|
||||
data.at[data.index[i], 'point'] = 1
|
||||
if not simulation and data['point'][-3:].sum() > 0:
|
||||
data.at[data.index[-1], 'signal'] = 'Deviation720'
|
||||
data.at[data.index[-1], 'point'] = 1
|
||||
# 93 상향 돌파
|
||||
if prev_d720 < 93 and curr_d720 >= 93:
|
||||
data.at[data.index[i], 'signal'] = 'Deviation720'
|
||||
data.at[data.index[i], 'point'] = 1
|
||||
if not simulation and data['point'][-3:].sum() > 0:
|
||||
data.at[data.index[-1], 'signal'] = 'Deviation720'
|
||||
data.at[data.index[-1], 'point'] = 1
|
||||
except Exception:
|
||||
pass
|
||||
policy = load_policy() or strategy.ACTIVE_MTF_POLICY
|
||||
cfg = strategy.ACTIVE_CONFIG
|
||||
print_latest_states(frames, cfg)
|
||||
print(
|
||||
f"MTF 정책: {policy.name} | "
|
||||
f"매수={policy.buy_interval}분 | 매도={policy.sell_interval}분 | "
|
||||
f"확인={list(policy.buy_confirm_intervals)}"
|
||||
)
|
||||
|
||||
try:
|
||||
prev_low = data['Low'].iloc[i - 1]
|
||||
curr_close = data['Close'].iloc[i]
|
||||
curr_low = data['Low'].iloc[i]
|
||||
cond_close_drop = curr_close <= prev_low * 0.94
|
||||
cond_low_drop = curr_low <= prev_low * 0.94
|
||||
if cond_close_drop or cond_low_drop:
|
||||
data.at[data.index[i], 'signal'] = 'fall_6p'
|
||||
data.at[data.index[i], 'point'] = 1
|
||||
if not simulation and data['point'][-3:].sum() > 0:
|
||||
data.at[data.index[-1], 'signal'] = 'fall_6p'
|
||||
data.at[data.index[-1], 'point'] = 1
|
||||
except Exception:
|
||||
pass
|
||||
return data
|
||||
trend = strategy.get_trend(df_1d, df_1h)
|
||||
print(f"{symbol} 추세: {trend}")
|
||||
|
||||
entry = frames.get(ENTRY_INTERVAL)
|
||||
trade = strategy.evaluate(
|
||||
symbol,
|
||||
entry if entry is not None else frames[policy.buy_interval],
|
||||
df_1h,
|
||||
df_1d,
|
||||
config=cfg,
|
||||
frames=frames,
|
||||
policy=policy,
|
||||
)
|
||||
if trade is None:
|
||||
return
|
||||
self.execute_trade_signal(symbol, trade, balances=balances)
|
||||
except Exception as e:
|
||||
print(f"Error processing {symbol}: {str(e)}")
|
||||
|
||||
def process_symbol(
|
||||
self,
|
||||
symbol: str,
|
||||
interval: int | None = None,
|
||||
balances: dict | None = None,
|
||||
use_inverse: bool = False,
|
||||
) -> None:
|
||||
"""하위 호환: MTF 전략으로 위임 (use_inverse 무시)."""
|
||||
self.process_wld_mtf(symbol, balances=balances)
|
||||
|
||||
def load_balances_dict(self) -> dict:
|
||||
"""getBalances() 결과를 currency 키 dict로 변환."""
|
||||
tmps = self.getBalances()
|
||||
balances = {}
|
||||
for tmp in tmps:
|
||||
balances[tmp["currency"]] = {
|
||||
"balance": float(tmp["balance"]),
|
||||
"avg_buy_price": float(tmp["avg_buy_price"]),
|
||||
}
|
||||
return balances
|
||||
|
||||
# ------------- Formatting -------------
|
||||
def format_message(self, symbol: str, symbol_name: str, close: float, signal: str, buy_amount: float) -> str:
|
||||
message = f"[매수] {symbol_name} ({symbol}): "
|
||||
def format_message(
|
||||
self, symbol: str, symbol_name: str, close: float, signal: str, buy_amount: float
|
||||
) -> str:
|
||||
message = f"[매수] {symbol_name} ({symbol}) [{signal}]: "
|
||||
|
||||
if int(close) >= 100:
|
||||
message += f"₩{close}"
|
||||
@@ -472,12 +398,6 @@ class Monitor(HTS):
|
||||
message += f"[{signal}]"
|
||||
return message
|
||||
|
||||
def format_ma_message(self, info: dict, market_type: str) -> str:
|
||||
prefix = '상승 ' if info.get('alert') else ''
|
||||
message = prefix + f"[{market_type}] {info['name']} ({info['symbol']}) "
|
||||
message += f"{'$' if market_type == 'US' else '₩'}({info['price']:.4f}) \n"
|
||||
return message
|
||||
|
||||
# ------------- Data fetch -------------
|
||||
def get_coin_data(self, symbol: str, interval: int = 60, to: str | None = None, retries: int = 3) -> pd.DataFrame | None:
|
||||
for attempt in range(retries):
|
||||
@@ -520,96 +440,146 @@ class Monitor(HTS):
|
||||
continue
|
||||
return None
|
||||
|
||||
def get_coin_more_data(self, symbol: str, interval: int, bong_count: int = 3000) -> pd.DataFrame:
|
||||
def get_coin_more_data(
|
||||
self,
|
||||
symbol: str,
|
||||
interval: int,
|
||||
bong_count: int = 3000,
|
||||
verbose: bool = False,
|
||||
) -> pd.DataFrame:
|
||||
"""
|
||||
빗썸 API를 반복 호출해 bong_count개까지 과거 봉을 수집합니다.
|
||||
|
||||
Args:
|
||||
verbose: True면 수집 진행 상황을 출력합니다.
|
||||
"""
|
||||
to = datetime.now()
|
||||
data: pd.DataFrame | None = None
|
||||
step = 0
|
||||
while data is None or len(data) < bong_count:
|
||||
step += 1
|
||||
if data is None:
|
||||
data = self.get_coin_data(symbol, interval, to.strftime("%Y-%m-%d %H:%M:%S"))
|
||||
chunk = self.get_coin_data(symbol, interval, to.strftime("%Y-%m-%d %H:%M:%S"))
|
||||
data = chunk
|
||||
else:
|
||||
previous_count = len(data)
|
||||
df = self.get_coin_data(symbol, interval, to.strftime("%Y-%m-%d %H:%M:%S"))
|
||||
data = pd.concat([data, df], ignore_index=True)
|
||||
if previous_count == len(data):
|
||||
if df is not None and not df.empty:
|
||||
data = pd.concat([data, df], ignore_index=True)
|
||||
if df is None or df.empty or previous_count == len(data):
|
||||
if verbose:
|
||||
print(f" API 추가 데이터 없음 (수집 {len(data)}봉)")
|
||||
break
|
||||
if verbose and (step == 1 or step % 5 == 0 or len(data) >= bong_count):
|
||||
label = "일봉" if interval >= 1440 else f"{interval}분"
|
||||
print(f" [{label}] 요청 {step}회 — 누적 {len(data)}/{bong_count}봉")
|
||||
time.sleep(0.3)
|
||||
to = to - relativedelta(minutes=interval * 200)
|
||||
data = data.set_index('datetime')
|
||||
if data is None or data.empty:
|
||||
return pd.DataFrame()
|
||||
data = data.set_index("datetime")
|
||||
data = data.sort_index()
|
||||
data = data.drop_duplicates(keep='first')
|
||||
data = data.drop_duplicates(keep="first")
|
||||
data["datetime"] = data.index
|
||||
return data
|
||||
|
||||
def get_coin_saved_data(self, symbol: str, interval: int, data: pd.DataFrame) -> pd.DataFrame:
|
||||
conn = sqlite3.connect('coins.db')
|
||||
def get_coin_saved_data(
|
||||
self, symbol: str, interval: int, data: pd.DataFrame, db_path: str = "coins.db"
|
||||
) -> pd.DataFrame:
|
||||
"""
|
||||
coins.db에서 저장된 봉을 읽고, API로 받은 최신 봉을 DB에 반영합니다.
|
||||
|
||||
downloader.py로 미리 적재해 두면 장기 MA 계산에 유리합니다.
|
||||
"""
|
||||
conn = sqlite3.connect(db_path)
|
||||
cursor = conn.cursor()
|
||||
table_name = f"{symbol}_{interval}"
|
||||
cursor.execute(
|
||||
f"CREATE TABLE IF NOT EXISTS {table_name} "
|
||||
"(CODE text, NAME text, ymdhms datetime, ymd text, hms text, "
|
||||
"Close REAL, Open REAL, High REAL, Low REAL, Volume REAL)"
|
||||
)
|
||||
cursor.execute(
|
||||
f"CREATE INDEX IF NOT EXISTS {table_name}_idx ON {table_name}(CODE, ymdhms)"
|
||||
)
|
||||
|
||||
for i in range(1, len(data)):
|
||||
cursor.execute("SELECT * from {}_{} where CODE = ? and ymdhms = ?".format(symbol, str(interval)), (symbol, data['datetime'].iloc[-i].strftime('%Y-%m-%d %H:%M:%S')),)
|
||||
arr = cursor.fetchone()
|
||||
if not arr:
|
||||
ymdhms = data["datetime"].iloc[-i].strftime("%Y-%m-%d %H:%M:%S")
|
||||
cursor.execute(
|
||||
f"SELECT 1 FROM {table_name} WHERE CODE = ? AND ymdhms = ?",
|
||||
(symbol, ymdhms),
|
||||
)
|
||||
if not cursor.fetchone():
|
||||
cursor.execute(
|
||||
"INSERT INTO {}_{} (CODE, NAME, ymdhms, ymd, hms, close, open, high, low, volume) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)".format(symbol, interval),
|
||||
f"INSERT INTO {table_name} "
|
||||
"(CODE, NAME, ymdhms, ymd, hms, Close, Open, High, Low, Volume) "
|
||||
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
|
||||
(
|
||||
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],
|
||||
ymdhms,
|
||||
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 {}_{} order by ymdhms desc limit 7000) subquery order by datetime".format(symbol, str(interval)))
|
||||
|
||||
cursor.execute(
|
||||
f"SELECT Open, Close, High, Low, Volume, ymdhms AS datetime "
|
||||
f"FROM (SELECT Open, Close, High, Low, Volume, ymdhms "
|
||||
f"FROM {table_name} ORDER BY ymdhms DESC LIMIT 7000) "
|
||||
f"ORDER BY datetime"
|
||||
)
|
||||
result = cursor.fetchall()
|
||||
conn.commit()
|
||||
cursor.close()
|
||||
conn.close()
|
||||
df = pd.DataFrame(result)
|
||||
df.columns = ['Open', 'Close', 'High', 'Low', 'Volume', 'datetime']
|
||||
df = df.set_index('datetime')
|
||||
|
||||
if not result:
|
||||
return pd.DataFrame(
|
||||
columns=["Open", "Close", "High", "Low", "Volume", "datetime"]
|
||||
)
|
||||
|
||||
df = pd.DataFrame(
|
||||
result, columns=["Open", "Close", "High", "Low", "Volume", "datetime"]
|
||||
)
|
||||
df = df.set_index("datetime")
|
||||
df = df.sort_index()
|
||||
df['datetime'] = df.index
|
||||
df["datetime"] = df.index
|
||||
return df
|
||||
|
||||
def get_coin_some_data(self, symbol: str, interval: int) -> pd.DataFrame:
|
||||
"""
|
||||
WLD 시세: API 최신 봉 + coins.db 과거 봉 + 1분봉 최신 1개를 합칩니다.
|
||||
|
||||
DB가 비어 있으면 API·1분봉만 사용합니다. 과거 적재는 downloader.py 실행.
|
||||
"""
|
||||
data = self.get_coin_data(symbol, interval)
|
||||
if data is None or data.empty:
|
||||
return pd.DataFrame()
|
||||
|
||||
data_1 = self.get_coin_data(symbol, interval=1)
|
||||
data_1.at[data_1.index[-1], 'Volume'] = data_1['Volume'].iloc[-1] * 60
|
||||
if data_1 is not None and not data_1.empty:
|
||||
data_1 = data_1.copy()
|
||||
data_1.at[data_1.index[-1], "Volume"] = data_1["Volume"].iloc[-1] * 60
|
||||
|
||||
saved_data = self.get_coin_saved_data(symbol, interval, data)
|
||||
data = pd.concat([data, saved_data, data_1.iloc[[-1]]], ignore_index=True)
|
||||
data['datetime'] = pd.to_datetime(data['datetime'], format='%Y-%m-%d %H:%M:%S')
|
||||
data = data.set_index('datetime')
|
||||
data = data.sort_index()
|
||||
data = data.drop_duplicates(keep='first')
|
||||
data["datetime"] = data.index
|
||||
return data
|
||||
parts = [data]
|
||||
if saved_data is not None and not saved_data.empty:
|
||||
parts.append(saved_data)
|
||||
if data_1 is not None and not data_1.empty:
|
||||
parts.append(data_1.iloc[[-1]])
|
||||
|
||||
def get_kr_stock_data(self, symbol: str, retries: int = 3) -> pd.DataFrame | None:
|
||||
for attempt in range(retries):
|
||||
try:
|
||||
end = datetime.now()
|
||||
start = end - timedelta(days=300)
|
||||
data = fdr.DataReader(symbol, start.strftime('%Y-%m-%d'), end.strftime('%Y-%m-%d'))
|
||||
if not data.empty:
|
||||
data = data.rename(columns={
|
||||
'Open': 'Open',
|
||||
'High': 'High',
|
||||
'Low': 'Low',
|
||||
'Close': 'Close',
|
||||
'Volume': 'Volume',
|
||||
})
|
||||
return data
|
||||
print(f"No data received for {symbol}, attempt {attempt + 1}")
|
||||
time.sleep(2)
|
||||
except Exception as e:
|
||||
print(f"Attempt {attempt + 1} failed for {symbol}: {str(e)}")
|
||||
if attempt < retries - 1:
|
||||
time.sleep(5)
|
||||
continue
|
||||
return None
|
||||
merged = pd.concat(parts, ignore_index=True)
|
||||
merged["datetime"] = pd.to_datetime(merged["datetime"], format="%Y-%m-%d %H:%M:%S")
|
||||
merged = merged.set_index("datetime")
|
||||
merged = merged.sort_index()
|
||||
merged = merged.drop_duplicates(keep="first")
|
||||
merged["datetime"] = merged.index
|
||||
return merged
|
||||
|
||||
Reference in New Issue
Block a user