init
This commit is contained in:
458
simulation.py
Normal file
458
simulation.py
Normal file
@@ -0,0 +1,458 @@
|
||||
from dateutil.relativedelta import relativedelta
|
||||
import pandas as pd
|
||||
import yfinance as yf
|
||||
import matplotlib.pyplot as plt
|
||||
import matplotlib.dates
|
||||
import mplcursors
|
||||
plt.rcParams['font.family'] ='AppleGothic'
|
||||
plt.rcParams['axes.unicode_minus'] =False
|
||||
|
||||
from config import *
|
||||
from monitor import Monitor
|
||||
|
||||
|
||||
class Simulation:
|
||||
def __init__(self) -> None:
|
||||
self.monitor = Monitor()
|
||||
self.INTERVAL_MAP = {
|
||||
60: "60m",
|
||||
240: "4h",
|
||||
}
|
||||
|
||||
def detect_turnaround_signal(self, symbol, data, interval=0, params=None):
|
||||
if len(data) < 7:
|
||||
return None
|
||||
current_data = data.iloc[-1]
|
||||
if current_data.get('buy_point', 0) == 1:
|
||||
return {
|
||||
'alert': True,
|
||||
'details': f"매수신호: {current_data.get('buy_signal', 'unknown')}"
|
||||
}
|
||||
return {'alert': False, 'details': "매수신호 없음"}
|
||||
|
||||
def fetch_price_history(self, symbol: str, interval_minutes: int, days: int = 30) -> pd.DataFrame:
|
||||
if symbol in KR_COINS:
|
||||
bong_count = 3000
|
||||
return self.monitor.get_coin_more_data(symbol, interval_minutes, bong_count=bong_count)
|
||||
if interval_minutes not in self.INTERVAL_MAP:
|
||||
raise ValueError("interval must be 60 or 240")
|
||||
interval_str = self.INTERVAL_MAP[interval_minutes]
|
||||
df = yf.download(
|
||||
tickers=symbol,
|
||||
period=f"{days}d",
|
||||
interval=interval_str,
|
||||
progress=False,
|
||||
)
|
||||
if df.empty:
|
||||
raise RuntimeError("No data fetched. Check symbol or interval support.")
|
||||
return df
|
||||
|
||||
def analyze_bottom_period(self, symbol: str, interval_minutes: int, days: int = 90):
|
||||
data = self.fetch_price_history(symbol, interval_minutes, days)
|
||||
data = self.monitor.calculate_technical_indicators(data)
|
||||
data = self.monitor.check_buy_point(symbol, data, simulation=True)
|
||||
print(f"데이터 기간: {data.index[0]} ~ {data.index[-1]}")
|
||||
print(f"총 데이터 수: {len(data)}")
|
||||
bottom_start = pd.Timestamp('2025-06-22')
|
||||
bottom_end = pd.Timestamp('2025-07-09')
|
||||
bottom_data = data[(data.index >= bottom_start) & (data.index <= bottom_end)]
|
||||
if len(bottom_data) == 0:
|
||||
print("저점 기간 데이터가 없습니다.")
|
||||
return None, []
|
||||
print(f"\n저점 기간 데이터: {bottom_data.index[0]} ~ {bottom_data.index[-1]}")
|
||||
print(f"저점 기간 데이터 수: {len(bottom_data)}")
|
||||
print("\n=== 저점 기간 기술적 지표 분석 ===")
|
||||
min_price = bottom_data['Low'].min()
|
||||
max_price = bottom_data['High'].max()
|
||||
avg_price = bottom_data['Close'].mean()
|
||||
print(f"최저가: {min_price:.4f}")
|
||||
print(f"최고가: {max_price:.4f}")
|
||||
print(f"평균가: {avg_price:.4f}")
|
||||
print(f"가격 변동폭: {((max_price - min_price) / min_price * 100):.2f}%")
|
||||
bb_lower_min = bottom_data['Lower'].min()
|
||||
bb_upper_max = bottom_data['Upper'].max()
|
||||
print(f"\n볼린저 밴드 분석:")
|
||||
print(f"하단 밴드 최저: {bb_lower_min:.4f}")
|
||||
print(f"상단 밴드 최고: {bb_upper_max:.4f}")
|
||||
volume_avg = bottom_data['Volume'].mean()
|
||||
volume_max = bottom_data['Volume'].max()
|
||||
print(f"\n거래량 분석:")
|
||||
print(f"평균 거래량: {volume_avg:.0f}")
|
||||
print(f"최대 거래량: {volume_max:.0f}")
|
||||
actual_bottom_idx = bottom_data['Low'].idxmin()
|
||||
actual_bottom_price = bottom_data.loc[actual_bottom_idx, 'Low']
|
||||
actual_bottom_date = actual_bottom_idx
|
||||
print(f"\n실제 저점:")
|
||||
print(f"날짜: {actual_bottom_date}")
|
||||
print(f"가격: {actual_bottom_price:.4f}")
|
||||
print(f"볼린저 하단 대비: {((actual_bottom_price - bottom_data.loc[actual_bottom_idx, 'Lower']) / bottom_data.loc[actual_bottom_idx, 'Lower'] * 100):.2f}%")
|
||||
print(f"\n=== 매수 신호 분석 ===")
|
||||
bottom_alerts = bottom_data[bottom_data['buy_point'] == 1]
|
||||
alerts = [(idx, row['Close']) for idx, row in bottom_alerts.iterrows()]
|
||||
print(f"저점 기간 매수 신호 수: {len(alerts)}")
|
||||
if alerts:
|
||||
print("매수 신호 발생 시점:")
|
||||
for date, price in alerts:
|
||||
print(f" {date}: {price:.4f}")
|
||||
return bottom_data, alerts
|
||||
|
||||
def run_simulation(self, symbol: str, interval_minutes: int, days: int = 30):
|
||||
data = self.fetch_price_history(symbol, interval_minutes)
|
||||
data = self.monitor.calculate_technical_indicators(data)
|
||||
data = self.monitor.check_buy_point(symbol, data, simulation=True)
|
||||
print(f"데이터 기간: {data.index[0]} ~ {data.index[-1]}")
|
||||
print(f"총 데이터 수: {len(data)}")
|
||||
alerts = []
|
||||
for i in range(len(data)):
|
||||
if data['buy_point'].iloc[i] == 1:
|
||||
alerts.append((data.index[i], data['Close'].iloc[i]))
|
||||
print(f"\n총 매수 신호 수: {len(alerts)}")
|
||||
ma_signals = len(data[(data['buy_point'] == 1) & (data['buy_signal'] == 'movingaverage')])
|
||||
dev40_signals = len(data[(data['buy_point'] == 1) & (data['buy_signal'] == 'deviation40')])
|
||||
dev240_signals = len(data[(data['buy_point'] == 1) & (data['buy_signal'] == 'deviation240')])
|
||||
dev1440_signals = len(data[(data['buy_point'] == 1) & (data['buy_signal'] == 'deviation1440')])
|
||||
print(f" - MA 신호: {ma_signals}")
|
||||
print(f" - Dev40 신호: {dev40_signals}")
|
||||
print(f" - Dev240 신호: {dev240_signals}")
|
||||
print(f" - Dev1440 신호: {dev1440_signals}")
|
||||
fig, (ax1, ax2) = plt.subplots(2, 1, sharex=True, figsize=(15, 8), height_ratios=[3, 1])
|
||||
fig.suptitle(f"{symbol} - 시뮬레이션 {interval_minutes}분봉", fontsize=14)
|
||||
|
||||
# ----------------- 마우스 휠 확대/축소 -----------------
|
||||
def on_scroll(event):
|
||||
# event.button: 'up' -> zoom in, 'down' -> zoom out
|
||||
if event.inaxes not in [ax1, ax2]:
|
||||
return
|
||||
ax = event.inaxes
|
||||
# x 축만 두 축을 동시에 조정
|
||||
cur_xlim = ax1.get_xlim()
|
||||
xdata = event.xdata
|
||||
if xdata is None:
|
||||
return
|
||||
scale_factor = 0.9 if event.button == 'up' else 1/0.9
|
||||
new_width = (cur_xlim[1] - cur_xlim[0]) * scale_factor
|
||||
relx = (cur_xlim[1] - xdata) / (cur_xlim[1] - cur_xlim[0])
|
||||
new_left = xdata - new_width * (1 - relx)
|
||||
new_right = xdata + new_width * relx
|
||||
# 데이터 영역 벗어나지 않도록 클램프
|
||||
xmin, xmax = matplotlib.dates.date2num(data.index[0]), matplotlib.dates.date2num(data.index[-1])
|
||||
if new_left < xmin:
|
||||
new_left = xmin
|
||||
if new_right > xmax:
|
||||
new_right = xmax
|
||||
ax1.set_xlim([new_left, new_right])
|
||||
ax2.set_xlim([new_left, new_right])
|
||||
# y축은 해당 축만 줌
|
||||
cur_ylim = ax.get_ylim()
|
||||
ydata = event.ydata
|
||||
if ydata is not None:
|
||||
new_height = (cur_ylim[1] - cur_ylim[0]) * scale_factor
|
||||
rely = (cur_ylim[1] - ydata) / (cur_ylim[1] - cur_ylim[0])
|
||||
ax.set_ylim([ydata - new_height * (1 - rely), ydata + new_height * rely])
|
||||
ax.figure.canvas.draw_idle()
|
||||
|
||||
fig.canvas.mpl_connect('scroll_event', on_scroll)
|
||||
|
||||
# 캔들스틱 차트 추가 (matplotlib 기본 기능 사용)
|
||||
import matplotlib.dates as mdates
|
||||
|
||||
# 캔들스틱 데이터 준비
|
||||
ohlc_data = []
|
||||
for i, (idx, row) in enumerate(data.iterrows()):
|
||||
ohlc_data.append([
|
||||
mdates.date2num(idx),
|
||||
row['Open'],
|
||||
row['High'],
|
||||
row['Low'],
|
||||
row['Close']
|
||||
])
|
||||
|
||||
# 캔들스틱 그리기 (matplotlib 기본 기능으로 구현)
|
||||
for ohlc in ohlc_data:
|
||||
date, open_price, high, low, close = ohlc
|
||||
|
||||
# 캔들 색상 결정
|
||||
color = 'red' if close >= open_price else 'blue'
|
||||
|
||||
# 캔들 몸통 그리기
|
||||
body_height = abs(close - open_price)
|
||||
body_bottom = min(open_price, close)
|
||||
rect = plt.Rectangle((date - 0.3, body_bottom), 0.6, body_height,
|
||||
facecolor=color, edgecolor='black', alpha=0.7)
|
||||
ax1.add_patch(rect)
|
||||
|
||||
# 캔들 심지 그리기
|
||||
ax1.plot([date, date], [low, high], color='black', linewidth=1)
|
||||
|
||||
# 메인 차트 (가격, 이동평균선, 볼린저 밴드)
|
||||
line_close = ax1.plot(data.index, data["Close"], label="종가", color="black", linewidth=1.5, alpha=0.8)[0]
|
||||
line_ma5 = ax1.plot(data.index, data["MA5"], label="MA5", color="red", linewidth=1)[0]
|
||||
line_ma20 = ax1.plot(data.index, data["MA20"], label="MA20", color="blue", linewidth=1)[0]
|
||||
line_ma40 = ax1.plot(data.index, data["MA40"], label="MA40", color="green", linewidth=1)[0]
|
||||
line_ma120 = ax1.plot(data.index, data["MA120"], label="MA120", color="purple", linewidth=1)[0]
|
||||
line_ma200 = ax1.plot(data.index, data["MA200"], label="MA200", color="brown", linewidth=1)[0]
|
||||
line_ma240 = ax1.plot(data.index, data["MA240"], label="MA240", color="darkred", linewidth=1)[0]
|
||||
line_ma720 = ax1.plot(data.index, data["MA720"], label="MA720", color="cyan", linewidth=1)[0]
|
||||
line_ma1440 = ax1.plot(data.index, data["MA1440"], label="MA1440", color="magenta", linewidth=1)[0]
|
||||
|
||||
# Bollinger Bands
|
||||
line_upper = ax1.plot(data.index, data["Upper"], label="볼린저 Upper", color="grey", linestyle="--", linewidth=1)[0]
|
||||
line_lower = ax1.plot(data.index, data["Lower"], label="볼린저 Lower", color="grey", linestyle="--", linewidth=1)[0]
|
||||
|
||||
ax1.fill_between(data.index, data["Lower"], data["Upper"], color="grey", alpha=0.1)
|
||||
|
||||
# 매수 포인트를 신호 유형별로 다르게 표시 (수직선 포함)
|
||||
# 이동평균선 기반 매수 포인트
|
||||
ma_buy_points = data[(data['buy_point'] == 1) & (data['buy_signal'] == 'movingaverage')]
|
||||
scatter_ma_buy_points = None
|
||||
if len(ma_buy_points) > 0:
|
||||
scatter_ma_buy_points = ax1.scatter(ma_buy_points.index, ma_buy_points['Close'], color='red', s=150, zorder=10, label='MA 매수 포인트', marker='o')
|
||||
for time in ma_buy_points.index:
|
||||
ax1.axvline(x=time, color='red', linestyle='-', alpha=0.5, linewidth=1)
|
||||
|
||||
# Deviation40 기반 매수 포인트
|
||||
dev40_buy_points = data[(data['buy_point'] == 1) & (data['buy_signal'] == 'deviation40')]
|
||||
scatter_dev40_buy_points = None
|
||||
if len(dev40_buy_points) > 0:
|
||||
scatter_dev40_buy_points = ax1.scatter(dev40_buy_points.index, dev40_buy_points['Close'],
|
||||
facecolors='none', edgecolors='red', linestyle='--',
|
||||
linewidth=2, s=200, zorder=10, label='Dev40 매수 포인트')
|
||||
for time in dev40_buy_points.index:
|
||||
ax1.axvline(x=time, color='red', linestyle='--', alpha=0.5, linewidth=1)
|
||||
|
||||
# Deviation240 기반 매수 포인트
|
||||
dev240_buy_points = data[(data['buy_point'] == 1) & (data['buy_signal'] == 'deviation240')]
|
||||
scatter_dev240_buy_points = None
|
||||
if len(dev240_buy_points) > 0:
|
||||
scatter_dev240_buy_points = ax1.scatter(dev240_buy_points.index, dev240_buy_points['Close'],
|
||||
facecolors='none', edgecolors='blue', linestyle='--',
|
||||
linewidth=2, s=200, zorder=10, label='Dev240 매수 포인트')
|
||||
for time in dev240_buy_points.index:
|
||||
ax1.axvline(x=time, color='blue', linestyle='--', alpha=0.5, linewidth=1)
|
||||
|
||||
# Deviation1440 기반 매수 포인트
|
||||
dev1440_buy_points = data[(data['buy_point'] == 1) & (data['buy_signal'] == 'deviation1440')]
|
||||
scatter_dev1440_buy_points = None
|
||||
if len(dev1440_buy_points) > 0:
|
||||
scatter_dev1440_buy_points = ax1.scatter(dev1440_buy_points.index, dev1440_buy_points['Close'],
|
||||
facecolors='none', edgecolors='purple', linestyle='--',
|
||||
linewidth=2, s=200, zorder=10, label='Dev1440 매수 포인트')
|
||||
for time in dev1440_buy_points.index:
|
||||
ax1.axvline(x=time, color='purple', linestyle='--', alpha=0.5, linewidth=1)
|
||||
|
||||
# 마우스 오버 기능 추가 (이동평균선 매수 포인트)
|
||||
if scatter_ma_buy_points is not None:
|
||||
cursor = mplcursors.cursor(scatter_ma_buy_points, hover=True)
|
||||
cursor.connect("add", lambda sel: sel.annotation.set_text(
|
||||
f'MA 매수신호\n날짜: {matplotlib.dates.num2date(sel.target[0]).replace(tzinfo=None).strftime("%Y-%m-%d %H:%M")}\n가격: {sel.target[1]:.2f}'
|
||||
))
|
||||
cursor.connect("remove", lambda sel: sel.annotation.set_visible(False))
|
||||
|
||||
# 마우스 오버 기능 추가 (Deviation40 매수 포인트)
|
||||
if scatter_dev40_buy_points is not None:
|
||||
cursor_dev40 = mplcursors.cursor(scatter_dev40_buy_points, hover=True)
|
||||
cursor_dev40.connect("add", lambda sel: sel.annotation.set_text(
|
||||
f'Dev40 매수신호\n날짜: {matplotlib.dates.num2date(sel.target[0]).replace(tzinfo=None).strftime("%Y-%m-%d %H:%M")}\n가격: {sel.target[1]:.2f}'
|
||||
))
|
||||
cursor_dev40.connect("remove", lambda sel: sel.annotation.set_visible(False))
|
||||
|
||||
# 마우스 오버 기능 추가 (Deviation240 매수 포인트)
|
||||
if scatter_dev240_buy_points is not None:
|
||||
cursor_dev240 = mplcursors.cursor(scatter_dev240_buy_points, hover=True)
|
||||
cursor_dev240.connect("add", lambda sel: sel.annotation.set_text(
|
||||
f'Dev240 매수신호\n날짜: {matplotlib.dates.num2date(sel.target[0]).replace(tzinfo=None).strftime("%Y-%m-%d %H:%M")}\n가격: {sel.target[1]:.2f}'
|
||||
))
|
||||
cursor_dev240.connect("remove", lambda sel: sel.annotation.set_visible(False))
|
||||
|
||||
# 마우스 오버 기능 추가 (Deviation1440 매수 포인트)
|
||||
if scatter_dev1440_buy_points is not None:
|
||||
cursor_dev1440 = mplcursors.cursor(scatter_dev1440_buy_points, hover=True)
|
||||
cursor_dev1440.connect("add", lambda sel: sel.annotation.set_text(
|
||||
f'Dev1440 매수신호\n날짜: {matplotlib.dates.num2date(sel.target[0]).replace(tzinfo=None).strftime("%Y-%m-%d %H:%M")}\n가격: {sel.target[1]:.2f}'
|
||||
))
|
||||
cursor_dev1440.connect("remove", lambda sel: sel.annotation.set_visible(False))
|
||||
|
||||
# 모든 봉에 마우스 오버 기능 추가
|
||||
cursor3 = mplcursors.cursor(line_close, hover=True)
|
||||
cursor3.connect("add", lambda sel: sel.annotation.set_text(
|
||||
f'종가\n날짜: {matplotlib.dates.num2date(sel.target[0]).replace(tzinfo=None).strftime("%Y-%m-%d %H:%M")}\n가격: {sel.target[1]:.2f}'
|
||||
))
|
||||
cursor3.connect("remove", lambda sel: sel.annotation.set_visible(False))
|
||||
|
||||
ax1.set_ylabel("가격 (KRW)")
|
||||
ax1.set_title(f"{symbol} 차트 - 캔들스틱 & 이동평균선", fontsize=14)
|
||||
ax1.grid(True, linestyle='--', alpha=0.3)
|
||||
|
||||
# 홈 버튼 추가 (초기화 기능)
|
||||
home_button = ax1.text(0.02, 0.98, '홈', transform=ax1.transAxes,
|
||||
bbox=dict(boxstyle="round,pad=0.3", facecolor='lightblue', alpha=0.8),
|
||||
fontsize=10, ha='left', va='top', picker=True)
|
||||
|
||||
# --- 범례 생성 및 인터랙티브 토글 ---
|
||||
legend = ax1.legend(loc='upper left', bbox_to_anchor=(0.02, 0.85), fontsize=10)
|
||||
|
||||
# 범례 클릭 시 해당 선 토글 기능
|
||||
lined = {}
|
||||
if hasattr(legend, "legend_handles"):
|
||||
legend_handles = legend.legend_handles
|
||||
elif hasattr(legend, "legendHandles"):
|
||||
legend_handles = legend.legendHandles
|
||||
else:
|
||||
legend_handles = legend.get_lines()
|
||||
plot_lines = [line_close, line_ma5, line_ma20, line_ma40, line_ma120,
|
||||
line_ma200, line_ma240, line_ma720, line_ma1440,
|
||||
line_upper, line_lower]
|
||||
if scatter_ma_buy_points is not None:
|
||||
plot_lines.append(scatter_ma_buy_points)
|
||||
if scatter_dev40_buy_points is not None:
|
||||
plot_lines.append(scatter_dev40_buy_points)
|
||||
if scatter_dev240_buy_points is not None:
|
||||
plot_lines.append(scatter_dev240_buy_points)
|
||||
if scatter_dev1440_buy_points is not None:
|
||||
plot_lines.append(scatter_dev1440_buy_points)
|
||||
|
||||
for leg_handle, orig in zip(legend_handles, plot_lines):
|
||||
leg_handle.set_picker(True)
|
||||
lined[leg_handle] = orig
|
||||
|
||||
def on_pick(event):
|
||||
leg_handle = event.artist
|
||||
if leg_handle == home_button:
|
||||
ax1.set_xlim(matplotlib.dates.date2num(data.index[0]), matplotlib.dates.date2num(data.index[-1]))
|
||||
ax1.set_ylim(data['Low'].min() * 0.99, data['High'].max() * 1.01)
|
||||
ax2.set_xlim(ax1.get_xlim())
|
||||
ax2.set_ylim(
|
||||
data[['Deviation5', 'Deviation20', 'Deviation40', 'Deviation120', 'Deviation200', 'Deviation240', 'Deviation720', 'Deviation1440']].min().min() * 0.95,
|
||||
data[['Deviation5', 'Deviation20', 'Deviation40', 'Deviation120', 'Deviation200', 'Deviation240', 'Deviation720', 'Deviation1440']].max().max() * 1.05,
|
||||
)
|
||||
fig.canvas.draw_idle()
|
||||
return
|
||||
orig = lined.get(leg_handle)
|
||||
if orig is None:
|
||||
return
|
||||
vis = not orig.get_visible()
|
||||
orig.set_visible(vis)
|
||||
leg_handle.set_alpha(1.0 if vis else 0.2)
|
||||
fig.canvas.draw_idle()
|
||||
|
||||
fig.canvas.mpl_connect('pick_event', on_pick)
|
||||
|
||||
# Deviation subplot (이격도 보조지표)
|
||||
line_dev5 = ax2.plot(data.index, data['Deviation5'], color='red', label='Dev5', linewidth=1)[0]
|
||||
line_dev20 = ax2.plot(data.index, data['Deviation20'], color='blue', label='Dev20', linewidth=1)[0]
|
||||
line_dev40 = ax2.plot(data.index, data['Deviation40'], color='green', label='Dev40', linewidth=2)[0]
|
||||
line_dev120 = ax2.plot(data.index, data['Deviation120'], color='purple', label='Dev120', linewidth=1)[0]
|
||||
line_dev200 = ax2.plot(data.index, data['Deviation200'], color='brown', label='Dev200', linewidth=1)[0]
|
||||
line_dev240 = ax2.plot(data.index, data['Deviation240'], color='darkred', label='Dev240', linewidth=2)[0]
|
||||
line_dev720 = ax2.plot(data.index, data['Deviation720'], color='cyan', label='Dev720', linewidth=1)[0]
|
||||
line_dev1440 = ax2.plot(data.index, data['Deviation1440'], color='magenta', label='Dev1440', linewidth=1)[0]
|
||||
|
||||
cursor_dev = mplcursors.cursor([line_dev5, line_dev20, line_dev40, line_dev120, line_dev200, line_dev240, line_dev720, line_dev1440], hover=True)
|
||||
cursor_dev.connect("add", lambda sel: sel.annotation.set_text(
|
||||
f"{sel.artist.get_label()}\n날짜: {matplotlib.dates.num2date(sel.target[0]).replace(tzinfo=None).strftime('%Y-%m-%d %H:%M')}\n값: {sel.target[1]:.2f}"
|
||||
))
|
||||
line_h90 = ax2.axhline(90, color='red', linestyle='--', linewidth=2, label='90', alpha=0.7)
|
||||
line_h95 = ax2.axhline(95, color='green', linestyle='--', linewidth=2, label='95', alpha=0.7)
|
||||
line_h100 = ax2.axhline(100, color='black', linestyle='-', linewidth=1, label='100', alpha=0.5)
|
||||
ax2.set_ylabel('이격도 (%)')
|
||||
ax2.set_title('이격도 보조지표', fontsize=12)
|
||||
legend2 = ax2.legend(loc='upper left', fontsize=9)
|
||||
|
||||
if legend2 is not None:
|
||||
if hasattr(legend2, "legend_handles"):
|
||||
legend2_handles = legend2.legend_handles
|
||||
elif hasattr(legend2, "legendHandles"):
|
||||
legend2_handles = legend2.legendHandles
|
||||
else:
|
||||
legend2_handles = legend2.get_lines()
|
||||
plot_lines2 = [line_dev5, line_dev20, line_dev40, line_dev120, line_dev200, line_dev240, line_dev720, line_dev1440, line_h90, line_h95]
|
||||
for leg_handle in legend2_handles:
|
||||
label = leg_handle.get_label()
|
||||
target_line = next((pl for pl in plot_lines2 if pl.get_label() == label), None)
|
||||
if target_line is not None:
|
||||
leg_handle.set_picker(True)
|
||||
lined[leg_handle] = target_line
|
||||
ax2.grid(True, linestyle='--', alpha=0.3)
|
||||
|
||||
plt.tight_layout()
|
||||
|
||||
print("그래프를 표시합니다...")
|
||||
print(f"매수 포인트 수: MA={len(ma_buy_points)}, Dev40={len(dev40_buy_points)}, Dev240={len(dev240_buy_points)}")
|
||||
|
||||
# -------- 확대/축소 및 이동 기능 --------
|
||||
press = {}
|
||||
|
||||
def on_scroll(event):
|
||||
ax = event.inaxes
|
||||
if ax is None:
|
||||
return
|
||||
x_left, x_right = ax.get_xlim()
|
||||
x_range = (x_right - x_left)
|
||||
if event.button == 'up': # zoom in
|
||||
scale = 0.8
|
||||
elif event.button == 'down': # zoom out
|
||||
scale = 1.25
|
||||
else:
|
||||
scale = 1.0
|
||||
new_range = x_range * scale
|
||||
center = event.xdata if event.xdata is not None else (x_left + x_right) / 2
|
||||
ax.set_xlim(center - new_range / 2, center + new_range / 2)
|
||||
for other_ax in fig.axes:
|
||||
if other_ax is not ax:
|
||||
other_ax.set_xlim(ax.get_xlim())
|
||||
fig.canvas.draw_idle()
|
||||
|
||||
def on_press(event):
|
||||
if event.button == 1 and event.inaxes is not None:
|
||||
press['xpress'] = event.xdata
|
||||
press['axes'] = event.inaxes
|
||||
|
||||
def on_motion(event):
|
||||
if 'xpress' not in press or press.get('axes') is None or event.inaxes is None:
|
||||
return
|
||||
dx = press['xpress'] - event.xdata
|
||||
for ax in fig.axes:
|
||||
x_left, x_right = ax.get_xlim()
|
||||
ax.set_xlim(x_left + dx, x_right + dx)
|
||||
fig.canvas.draw_idle()
|
||||
|
||||
def on_release(event):
|
||||
press.clear()
|
||||
|
||||
fig.canvas.mpl_connect('scroll_event', on_scroll)
|
||||
fig.canvas.mpl_connect('button_press_event', on_press)
|
||||
fig.canvas.mpl_connect('motion_notify_event', on_motion)
|
||||
fig.canvas.mpl_connect('button_release_event', on_release)
|
||||
|
||||
plt.show()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sim = Simulation()
|
||||
interval = 60
|
||||
days = 90
|
||||
target_coins = ['POL']
|
||||
show_graphs = True
|
||||
for symbol in target_coins:
|
||||
print(f"\n=== {symbol} 저점 기간 분석 시작 ===")
|
||||
try:
|
||||
bottom_data, alerts = sim.analyze_bottom_period(symbol, interval, days)
|
||||
print(f"\n=== {symbol} 전체 기간 시뮬레이션 ===")
|
||||
if show_graphs:
|
||||
sim.run_simulation(symbol, interval, days)
|
||||
else:
|
||||
data = sim.fetch_price_history(symbol, interval, days)
|
||||
data = sim.monitor.calculate_technical_indicators(data)
|
||||
data = sim.monitor.check_buy_point(symbol, data, simulation=True)
|
||||
total_buy_signals = len(data[data['buy_point'] == 1])
|
||||
ma_signals = len(data[(data['buy_point'] == 1) & (data['buy_signal'] == 'movingaverage')])
|
||||
dev40_signals = len(data[(data['buy_point'] == 1) & (data['buy_signal'] == 'deviation40')])
|
||||
dev240_signals = len(data[(data['buy_point'] == 1) & (data['buy_signal'] == 'deviation240')])
|
||||
dev1440_signals = len(data[(data['buy_point'] == 1) & (data['buy_signal'] == 'deviation1440')])
|
||||
print(f"총 매수 신호: {total_buy_signals}")
|
||||
print(f" - MA 신호: {ma_signals}")
|
||||
print(f" - Dev40 신호: {dev40_signals}")
|
||||
print(f" - Dev240 신호: {dev240_signals}")
|
||||
print(f" - Dev1440 신호: {dev1440_signals}")
|
||||
except Exception as e:
|
||||
print(f"Error analyzing {symbol}: {str(e)}")
|
||||
Reference in New Issue
Block a user