init
This commit is contained in:
104
simulation.py
104
simulation.py
@@ -24,7 +24,7 @@ class Simulation:
|
||||
if len(data) < 7:
|
||||
return None
|
||||
current_data = data.iloc[-1]
|
||||
if current_data.get('buy_point', 0) == 1:
|
||||
if current_data.get('point', 0) == 1:
|
||||
return {
|
||||
'alert': True,
|
||||
'details': f"매수신호: {current_data.get('buy_signal', 'unknown')}"
|
||||
@@ -51,7 +51,7 @@ class Simulation:
|
||||
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)
|
||||
data = self.monitor.check_point(symbol, data, simulation=True)
|
||||
print(f"데이터 기간: {data.index[0]} ~ {data.index[-1]}")
|
||||
print(f"총 데이터 수: {len(data)}")
|
||||
bottom_start = pd.Timestamp('2025-06-22')
|
||||
@@ -88,7 +88,7 @@ class Simulation:
|
||||
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]
|
||||
bottom_alerts = bottom_data[bottom_data['point'] == 1]
|
||||
alerts = [(idx, row['Close']) for idx, row in bottom_alerts.iterrows()]
|
||||
print(f"저점 기간 매수 신호 수: {len(alerts)}")
|
||||
if alerts:
|
||||
@@ -100,18 +100,18 @@ class Simulation:
|
||||
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)
|
||||
data = self.monitor.check_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:
|
||||
if data['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')])
|
||||
ma_signals = len(data[(data['point'] == 1) & (data['buy_signal'] == 'movingaverage')])
|
||||
dev40_signals = len(data[(data['point'] == 1) & (data['buy_signal'] == 'deviation40')])
|
||||
dev240_signals = len(data[(data['point'] == 1) & (data['buy_signal'] == 'deviation240')])
|
||||
dev1440_signals = len(data[(data['point'] == 1) & (data['buy_signal'] == 'deviation1440')])
|
||||
print(f" - MA 신호: {ma_signals}")
|
||||
print(f" - Dev40 신호: {dev40_signals}")
|
||||
print(f" - Dev240 신호: {dev240_signals}")
|
||||
@@ -236,70 +236,70 @@ class Simulation:
|
||||
|
||||
# 매수 포인트를 신호 유형별로 다르게 표시 (수직선 포함)
|
||||
# 이동평균선 기반 매수 포인트
|
||||
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:
|
||||
ma_points = data[(data['point'] == 1) & (data['buy_signal'] == 'movingaverage')]
|
||||
scatter_ma_points = None
|
||||
if len(ma_points) > 0:
|
||||
scatter_ma_points = ax1.scatter(ma_points.index, ma_points['Close'], color='red', s=150, zorder=10, label='MA 매수 포인트', marker='o')
|
||||
for time in ma_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'],
|
||||
dev40_points = data[(data['point'] == 1) & (data['buy_signal'] == 'deviation40')]
|
||||
scatter_dev40_points = None
|
||||
if len(dev40_points) > 0:
|
||||
scatter_dev40_points = ax1.scatter(dev40_points.index, dev40_points['Close'],
|
||||
facecolors='none', edgecolors='red', linestyle='--',
|
||||
linewidth=2, s=200, zorder=10, label='Dev40 매수 포인트')
|
||||
for time in dev40_buy_points.index:
|
||||
for time in dev40_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'],
|
||||
dev240_points = data[(data['point'] == 1) & (data['buy_signal'] == 'deviation240')]
|
||||
scatter_dev240_points = None
|
||||
if len(dev240_points) > 0:
|
||||
scatter_dev240_points = ax1.scatter(dev240_points.index, dev240_points['Close'],
|
||||
facecolors='none', edgecolors='blue', linestyle='--',
|
||||
linewidth=2, s=200, zorder=10, label='Dev240 매수 포인트')
|
||||
for time in dev240_buy_points.index:
|
||||
for time in dev240_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'],
|
||||
dev1440_points = data[(data['point'] == 1) & (data['buy_signal'] == 'deviation1440')]
|
||||
scatter_dev1440_points = None
|
||||
if len(dev1440_points) > 0:
|
||||
scatter_dev1440_points = ax1.scatter(dev1440_points.index, dev1440_points['Close'],
|
||||
facecolors='none', edgecolors='purple', linestyle='--',
|
||||
linewidth=2, s=200, zorder=10, label='Dev1440 매수 포인트')
|
||||
for time in dev1440_buy_points.index:
|
||||
for time in dev1440_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)
|
||||
if scatter_ma_points is not None:
|
||||
cursor = mplcursors.cursor(scatter_ma_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)
|
||||
if scatter_dev40_points is not None:
|
||||
cursor_dev40 = mplcursors.cursor(scatter_dev40_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)
|
||||
if scatter_dev240_points is not None:
|
||||
cursor_dev240 = mplcursors.cursor(scatter_dev240_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)
|
||||
if scatter_dev1440_points is not None:
|
||||
cursor_dev1440 = mplcursors.cursor(scatter_dev1440_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}'
|
||||
))
|
||||
@@ -340,14 +340,14 @@ class Simulation:
|
||||
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)
|
||||
if scatter_ma_points is not None:
|
||||
plot_lines.append(scatter_ma_points)
|
||||
if scatter_dev40_points is not None:
|
||||
plot_lines.append(scatter_dev40_points)
|
||||
if scatter_dev240_points is not None:
|
||||
plot_lines.append(scatter_dev240_points)
|
||||
if scatter_dev1440_points is not None:
|
||||
plot_lines.append(scatter_dev1440_points)
|
||||
|
||||
for leg_handle, orig in zip(legend_handles[:len(plot_lines)], plot_lines):
|
||||
leg_handle.set_picker(True)
|
||||
@@ -436,7 +436,7 @@ class Simulation:
|
||||
plt.tight_layout()
|
||||
|
||||
print("그래프를 표시합니다...")
|
||||
print(f"매수 포인트 수: MA={len(ma_buy_points)}, Dev40={len(dev40_buy_points)}, Dev240={len(dev240_buy_points)}")
|
||||
print(f"매수 포인트 수: MA={len(ma_points)}, Dev40={len(dev40_points)}, Dev240={len(dev240_points)}")
|
||||
|
||||
# -------- 확대/축소 및 이동 기능 --------
|
||||
press = {}
|
||||
@@ -502,12 +502,12 @@ if __name__ == "__main__":
|
||||
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')])
|
||||
data = sim.monitor.check_point(symbol, data, simulation=True)
|
||||
total_buy_signals = len(data[data['point'] == 1])
|
||||
ma_signals = len(data[(data['point'] == 1) & (data['buy_signal'] == 'movingaverage')])
|
||||
dev40_signals = len(data[(data['point'] == 1) & (data['buy_signal'] == 'deviation40')])
|
||||
dev240_signals = len(data[(data['point'] == 1) & (data['buy_signal'] == 'deviation240')])
|
||||
dev1440_signals = len(data[(data['point'] == 1) & (data['buy_signal'] == 'deviation1440')])
|
||||
print(f"총 매수 신호: {total_buy_signals}")
|
||||
print(f" - MA 신호: {ma_signals}")
|
||||
print(f" - Dev40 신호: {dev40_signals}")
|
||||
|
||||
Reference in New Issue
Block a user