-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_agent_eth.py
More file actions
323 lines (263 loc) · 10.7 KB
/
Copy pathtest_agent_eth.py
File metadata and controls
323 lines (263 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
"""
test_agent_eth.py - Test your trained Ethereum trading bot
This script:
1. Loads your trained model
2. Runs it on test data (data it's never seen)
3. Records every trade it makes
4. Saves trade history to CSV
5. Shows equity curve
"""
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from stable_baselines3 import PPO
from stable_baselines3.common.vec_env import DummyVecEnv
from indicators import load_and_preprocess_data
from trading_env_eth import EthereumTradingEnv
def run_one_episode(model, vec_env, deterministic=True):
"""
Run the model through one complete episode
Returns:
--------
equity_curve : list
Equity at each step
closed_trades : list
List of all closed trades with details
"""
obs = vec_env.reset()
equity_curve = []
closed_trades = []
step_count = 0
while True:
step_count += 1
action, _ = model.predict(obs, deterministic=deterministic)
step_out = vec_env.step(action)
# Handle both gym APIs
if len(step_out) == 4:
obs, rewards, dones, infos = step_out
done = bool(dones[0])
else:
obs, rewards, terminated, truncated, infos = step_out
done = bool(terminated[0] or truncated[0])
equity_curve.append(vec_env.get_attr("equity_usd")[0])
# Record closed trades
trade_info = vec_env.get_attr("last_trade_info")[0]
if isinstance(trade_info, dict) and trade_info.get("event") == "CLOSE":
closed_trades.append(trade_info)
if done:
break
return equity_curve, closed_trades
def analyze_trades(trades_df):
"""Calculate and print trading statistics"""
if len(trades_df) == 0:
print("No trades to analyze!")
return
print("\n" + "="*70)
print(" TRADE ANALYSIS")
print("="*70)
# Basic stats
print(f"\nTotal trades: {len(trades_df)}")
# Win/loss analysis
winning_trades = trades_df[trades_df['net_dollars'] > 0]
losing_trades = trades_df[trades_df['net_dollars'] < 0]
breakeven_trades = trades_df[trades_df['net_dollars'] == 0]
print(f"\nWin/Loss breakdown:")
print(f" Winning trades: {len(winning_trades):3d} ({len(winning_trades)/len(trades_df)*100:.1f}%)")
print(f" Losing trades: {len(losing_trades):3d} ({len(losing_trades)/len(trades_df)*100:.1f}%)")
print(f" Breakeven trades: {len(breakeven_trades):3d} ({len(breakeven_trades)/len(trades_df)*100:.1f}%)")
# Profit statistics
if len(winning_trades) > 0:
avg_win = winning_trades['net_dollars'].mean()
max_win = winning_trades['net_dollars'].max()
print(f"\nWinning trades:")
print(f" Average win: ${avg_win:.2f}")
print(f" Largest win: ${max_win:.2f}")
if len(losing_trades) > 0:
avg_loss = losing_trades['net_dollars'].mean()
max_loss = losing_trades['net_dollars'].min()
print(f"\nLosing trades:")
print(f" Average loss: ${avg_loss:.2f}")
print(f" Largest loss: ${max_loss:.2f}")
# Overall P&L
total_pnl = trades_df['net_dollars'].sum()
print(f"\nTotal P&L: ${total_pnl:+,.2f}")
# Profit factor
if len(losing_trades) > 0 and losing_trades['net_dollars'].sum() != 0:
profit_factor = winning_trades['net_dollars'].sum() / abs(losing_trades['net_dollars'].sum())
print(f"Profit factor: {profit_factor:.2f} (>1 is profitable)")
# Time in trade
avg_time = trades_df['time_in_trade'].mean()
print(f"\nAverage time in trade: {avg_time:.1f} hours ({avg_time/24:.1f} days)")
# Position analysis
long_trades = trades_df[trades_df['position'] == 1]
short_trades = trades_df[trades_df['position'] == -1]
print(f"\nPosition breakdown:")
print(f" Long positions: {len(long_trades):3d} | P&L: ${long_trades['net_dollars'].sum():+,.2f}")
print(f" Short positions: {len(short_trades):3d} | P&L: ${short_trades['net_dollars'].sum():+,.2f}")
# Close reason breakdown
print(f"\nHow trades ended:")
for reason in trades_df['reason'].unique():
count = len(trades_df[trades_df['reason'] == reason])
pnl = trades_df[trades_df['reason'] == reason]['net_dollars'].sum()
print(f" {reason:20s}: {count:3d} trades | ${pnl:+,.2f}")
def main():
print("="*70)
print(" ETHEREUM TRADING BOT - TESTING")
print("="*70)
print()
# ========================================
# 1. Load test data
# ========================================
print("STEP 1: Loading test data...")
print("-" * 70)
# You can test on either the training data's test split, or the separate test file
file_path = "ETHUSD_Candlestick_1_Hour_2023_2025.csv" # Separate test data
# OR use: file_path = "ETHUSD_Candlestick_1_Hour_2020_2023.csv" # Same as training
df, feature_cols = load_and_preprocess_data(file_path)
# If using training data, take the test portion
# split_idx = int(len(df) * 0.8)
# test_df = df.iloc[split_idx:].copy()
# If using separate test file, use all of it
test_df = df.copy()
print(f"Test data: {len(test_df):,} hours ({len(test_df)/24:.1f} days)")
print()
# ========================================
# 2. Create test environment
# ========================================
print("STEP 2: Creating test environment...")
print("-" * 70)
# Must match training parameters!
SL_OPTS = [20, 50, 100, 150]
TP_OPTS = [30, 75, 150, 225]
WIN = 30
test_env = EthereumTradingEnv(
df=test_df,
window_size=WIN,
sl_options=SL_OPTS,
tp_options=TP_OPTS,
spread_pct=0.1,
commission_dollars=0.5,
max_slippage_pct=0.05,
position_size_eth=0.1,
random_start=False, # Start from beginning
episode_max_steps=None, # Run through all data
feature_columns=feature_cols,
hold_reward_weight=0.0,
open_penalty_dollars=0.0,
time_penalty_dollars=0.0,
unrealized_delta_weight=0.0
)
vec_test_env = DummyVecEnv([lambda: test_env])
print("✓ Test environment created")
print()
# ========================================
# 3. Load trained model
# ========================================
print("STEP 3: Loading trained model...")
print("-" * 70)
try:
model = PPO.load("model_ethereum_best", env=vec_test_env)
print("✓ Model loaded: model_ethereum_best.zip")
except FileNotFoundError:
print("❌ Error: model_ethereum_best.zip not found!")
print(" Did you run train_agent_eth.py first?")
return
print()
# ========================================
# 4. Run the test
# ========================================
print("STEP 4: Running bot on test data...")
print("-" * 70)
print("This may take a few minutes...\n")
equity_curve, closed_trades = run_one_episode(model, vec_test_env, deterministic=True)
print(f"✓ Test complete!")
print(f" Steps simulated: {len(equity_curve):,}")
print(f" Trades executed: {len(closed_trades)}")
print()
# ========================================
# 5. Save trade history
# ========================================
print("STEP 5: Saving trade history...")
print("-" * 70)
if closed_trades:
trades_df = pd.DataFrame(closed_trades)
out_csv = "ethereum_trade_history.csv"
trades_df.to_csv(out_csv, index=False)
print(f"✓ Trade history saved to: {out_csv}")
print(f" Columns: {list(trades_df.columns)}")
# Show sample trades
print(f"\nFirst 5 trades:")
print(trades_df[['step', 'position', 'entry_price', 'exit_price',
'net_dollars', 'reason']].head().to_string(index=False))
else:
print("❌ No trades recorded!")
print(" The bot stayed flat the entire time.")
trades_df = pd.DataFrame()
print()
# ========================================
# 6. Calculate statistics
# ========================================
if len(trades_df) > 0:
analyze_trades(trades_df)
print()
# ========================================
# 7. Plot equity curve
# ========================================
print("STEP 7: Generating equity curve...")
print("-" * 70)
plt.figure(figsize=(14, 8))
# Equity curve
plt.subplot(2, 1, 1)
plt.plot(equity_curve, label="Equity", linewidth=2, color='#2E86AB')
plt.axhline(y=10000, color='gray', linestyle='--', alpha=0.5, label='Starting equity')
plt.title("Ethereum Trading Bot - Equity Curve (Test Data)",
fontsize=14, fontweight='bold')
plt.xlabel("Steps (hours)")
plt.ylabel("Equity ($)")
plt.legend(loc='best')
plt.grid(True, alpha=0.3)
# Add final stats on plot
final_equity = equity_curve[-1]
pnl = final_equity - 10000
roi = (final_equity / 10000 - 1) * 100
stats_text = f"Final: ${final_equity:,.2f}\nP&L: ${pnl:+,.2f}\nROI: {roi:+.2f}%"
plt.text(0.02, 0.98, stats_text, transform=plt.gca().transAxes,
fontsize=10, verticalalignment='top',
bbox=dict(boxstyle='round', facecolor='wheat', alpha=0.5))
# Drawdown
plt.subplot(2, 1, 2)
equity_array = np.array(equity_curve)
running_max = np.maximum.accumulate(equity_array)
drawdown = (equity_array - running_max) / running_max * 100
plt.fill_between(range(len(drawdown)), drawdown, 0,
alpha=0.3, color='red', label='Drawdown')
plt.plot(drawdown, color='red', linewidth=1)
plt.title("Drawdown (% from peak equity)", fontsize=12, fontweight='bold')
plt.xlabel("Steps (hours)")
plt.ylabel("Drawdown (%)")
plt.grid(True, alpha=0.3)
max_dd = drawdown.min()
plt.text(0.02, 0.02, f"Max Drawdown: {max_dd:.2f}%",
transform=plt.gca().transAxes,
fontsize=10, verticalalignment='bottom',
bbox=dict(boxstyle='round', facecolor='wheat', alpha=0.5))
plt.tight_layout()
plt.savefig('ethereum_test_results.png', dpi=150, bbox_inches='tight')
print("✓ Plot saved as: ethereum_test_results.png")
plt.show()
print()
print("="*70)
print(" TESTING COMPLETE!")
print("="*70)
print()
print("Files created:")
print(" - ethereum_trade_history.csv (all trades)")
print(" - ethereum_test_results.png (equity curve)")
print()
print("Next steps:")
print(" - Open ethereum_trade_history.csv in Excel to analyze")
print(" - Look for patterns in winning vs losing trades")
print(" - Consider adjusting parameters if results are poor")
print()
if __name__ == "__main__":
main()