-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbacktest.py
More file actions
245 lines (211 loc) · 8.17 KB
/
Copy pathbacktest.py
File metadata and controls
245 lines (211 loc) · 8.17 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
import os
import sys
from pathlib import Path
# Make sibling modules importable. JAGRESMAN_HOME overrides default.
sys.path.insert(0, os.getenv("JAGRESMAN_HOME") or str(Path(__file__).resolve().parent))
import pandas as pd
import requests
# ==========================================
# CONFIG BACKTEST
# ==========================================
TOTAL_MODAL = 100
RESIKO_PERSEN = 1.0
LEVERAGE = 5
PAIRS = ["BTCUSDT", "ETHUSDT", "SOLUSDT", "BNBUSDT", "SUIUSDT"]
# Mode: "scalping" (15m) atau "swing" (4h)
TRADING_MODE = "scalping"
if TRADING_MODE == "scalping":
TIMEFRAME = "15m"
ATR_MULT = 1.5
RSI_LONG_MIN, RSI_LONG_MAX = 45, 65
RSI_SHORT_MIN, RSI_SHORT_MAX = 35, 55
else:
TIMEFRAME = "4h"
ATR_MULT = 2.5
RSI_LONG_MIN, RSI_LONG_MAX = 50, 70
RSI_SHORT_MIN, RSI_SHORT_MAX = 30, 50
# ==========================================
# DATA
# ==========================================
def get_binance_data(sym, interval, limit=1000):
url = "https://api.binance.com/api/v3/klines"
params = {"symbol": sym, "interval": interval, "limit": limit}
try:
res = requests.get(url, params=params, timeout=10)
data = res.json()
if isinstance(data, dict):
return None
df = pd.DataFrame(data, columns=['ts','o','h','l','c','v','ct','qa','nt','tb','tq','i'])
df[['o','h','l','c','v']] = df[['o','h','l','c','v']].astype(float)
df['ts'] = pd.to_datetime(df['ts'], unit='ms')
return df
except:
return None
# ==========================================
# BACKTEST ENGINE
# ==========================================
def backtest(symbol: str) -> dict:
df = get_binance_data(symbol, TIMEFRAME, limit=1000)
if df is None or len(df) < 200:
return None
# Indicators
df['ema_9'] = df['c'].ewm(span=9, adjust=False).mean()
df['ema_21'] = df['c'].ewm(span=21, adjust=False).mean()
df['ema_200'] = df['c'].ewm(span=200, adjust=False).mean()
delta = df['c'].diff()
gain = delta.where(delta > 0, 0).rolling(14).mean()
loss = (-delta.where(delta < 0, 0)).rolling(14).mean()
df['rsi'] = 100 - (100 / (1 + (gain / loss)))
df['atr'] = (df['h'] - df['l']).rolling(14).mean()
df['vol_ma'] = df['v'].rolling(20).mean()
df['cross_up'] = (df['ema_9'] > df['ema_21']) & (df['ema_9'].shift(1) <= df['ema_21'].shift(1))
df['cross_down'] = (df['ema_9'] < df['ema_21']) & (df['ema_9'].shift(1) >= df['ema_21'].shift(1))
# Backtest
trades = []
modal = TOTAL_MODAL
peak_modal = TOTAL_MODAL
for i in range(210, len(df)-1):
curr = df.iloc[i]
price = curr['c']
candle_range = curr['h'] - curr['l']
is_solid = candle_range > 0 and (abs(curr['c'] - curr['o']) >= candle_range * 0.5)
vol_up = curr['v'] > df.iloc[i-1]['v']
is_bullish = curr['c'] > curr['o']
signal = None
entry = sl = tp = None
# LONG
if (price > curr['ema_200'] and curr['cross_up'] and
RSI_LONG_MIN < curr['rsi'] < RSI_LONG_MAX and
is_solid and vol_up):
signal = "LONG"
entry = price
sl = entry - (curr['atr'] * ATR_MULT)
tp = entry + (entry - sl)
# SHORT
elif (price < curr['ema_200'] and curr['cross_down'] and
RSI_SHORT_MIN < curr['rsi'] < RSI_SHORT_MAX and
is_solid and not is_bullish and vol_up):
signal = "SHORT"
entry = price
sl = entry + (curr['atr'] * ATR_MULT)
tp = entry - (sl - entry)
if not signal:
continue
# Simulasi exit — cek candle berikutnya sampai kena SL/TP
result = "RUNNING"
exit_price = None
for j in range(i+1, min(i+50, len(df))):
future = df.iloc[j]
if signal == "LONG":
if future['l'] <= sl:
result = "LOSS"
exit_price = sl
break
elif future['h'] >= tp:
result = "WIN"
exit_price = tp
break
else: # SHORT
if future['h'] >= sl:
result = "LOSS"
exit_price = sl
break
elif future['l'] <= tp:
result = "WIN"
exit_price = tp
break
if result == "RUNNING":
continue
# Kalkulasi PnL
jarak_sl_pct = abs(entry - sl) / entry * 100
resiko_usd = modal * (RESIKO_PERSEN / 100)
resiko_usd / (jarak_sl_pct / 100) if jarak_sl_pct > 0 else 0
if result == "WIN":
pnl = resiko_usd * LEVERAGE
else:
pnl = -resiko_usd
modal += pnl
if modal > peak_modal:
peak_modal = modal
trades.append({
"symbol": symbol,
"signal": signal,
"entry": round(entry, 4),
"exit": round(exit_price, 4),
"result": result,
"pnl": round(pnl, 2),
"modal": round(modal, 2),
"date": curr['ts'].strftime("%Y-%m-%d %H:%M"),
})
if not trades:
return None
wins = [t for t in trades if t['result'] == "WIN"]
losses = [t for t in trades if t['result'] == "LOSS"]
total_profit = sum(t['pnl'] for t in wins)
total_loss = sum(t['pnl'] for t in losses)
net_pnl = total_profit + total_loss
win_rate = (len(wins) / len(trades) * 100) if trades else 0
max_dd = round(((peak_modal - modal) / peak_modal * 100), 2) if peak_modal > 0 else 0
return {
"symbol": symbol,
"total_trades": len(trades),
"wins": len(wins),
"losses": len(losses),
"win_rate": round(win_rate, 2),
"total_profit": round(total_profit, 2),
"total_loss": round(total_loss, 2),
"net_pnl": round(net_pnl, 2),
"final_modal": round(modal, 2),
"max_drawdown": max_dd,
"best_trade": round(max((t['pnl'] for t in trades), default=0), 2),
"worst_trade": round(min((t['pnl'] for t in trades), default=0), 2),
"trades": trades[-5:], # 5 trade terakhir
}
# ==========================================
# MAIN
# ==========================================
def run_backtest():
print(f"\n{'='*50}")
print(" GODZILLA BACKTEST ENGINE")
print(f" Mode: {TRADING_MODE.upper()} | TF: {TIMEFRAME}")
print(f" Modal: ${TOTAL_MODAL} | Risk: {RESIKO_PERSEN}%")
print(f" Leverage: {LEVERAGE}x")
print(f"{'='*50}\n")
all_results = []
for pair in PAIRS:
print(f"Backtesting {pair}...")
result = backtest(pair)
if not result:
print(f" {pair}: Data tidak cukup\n")
continue
all_results.append(result)
emoji = "✅" if result['net_pnl'] > 0 else "❌"
print(f"\n{emoji} {result['symbol']}")
print(f" Total Trade : {result['total_trades']}")
print(f" Win/Loss : {result['wins']}/{result['losses']}")
print(f" Win Rate : {result['win_rate']}%")
print(f" Total Profit: ${result['total_profit']}")
print(f" Total Loss : ${result['total_loss']}")
print(f" Net PnL : ${result['net_pnl']}")
print(f" Final Modal : ${result['final_modal']}")
print(f" Max DD : {result['max_drawdown']}%")
print(f" Best Trade : ${result['best_trade']}")
print(f" Worst Trade : ${result['worst_trade']}")
# Summary semua pairs
if all_results:
total_trades = sum(r['total_trades'] for r in all_results)
total_wins = sum(r['wins'] for r in all_results)
total_net = sum(r['net_pnl'] for r in all_results)
avg_wr = sum(r['win_rate'] for r in all_results) / len(all_results)
print(f"\n{'='*50}")
print(" SUMMARY SEMUA PAIRS")
print(f"{'='*50}")
print(f" Total Trade : {total_trades}")
print(f" Total Win : {total_wins}")
print(f" Avg Win Rate: {avg_wr:.2f}%")
print(f" Total Net : ${total_net:.2f}")
print(f" Modal Awal : ${TOTAL_MODAL}")
print(f" ROI : {(total_net/TOTAL_MODAL*100):.2f}%")
print(f"{'='*50}\n")
if __name__ == "__main__":
run_backtest()