Upload simulate_v4_1_trailing.py with huggingface_hub
Browse files- simulate_v4_1_trailing.py +120 -0
simulate_v4_1_trailing.py
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import numpy as np
|
| 3 |
+
|
| 4 |
+
def run_simulation():
|
| 5 |
+
print("--- U2Predator V4.1 Trailing Simulator ---")
|
| 6 |
+
df = pd.read_parquet('native_rates_M1.parquet')
|
| 7 |
+
|
| 8 |
+
# Calculate Donchian Channels
|
| 9 |
+
df['dc_fast_h'] = df['high'].rolling(14).max()
|
| 10 |
+
df['dc_fast_l'] = df['low'].rolling(14).min()
|
| 11 |
+
df['dc_fast_mid'] = (df['dc_fast_h'] + df['dc_fast_l']) / 2
|
| 12 |
+
|
| 13 |
+
df['dc_slow_h'] = df['high'].rolling(21).max()
|
| 14 |
+
df['dc_slow_l'] = df['low'].rolling(21).min()
|
| 15 |
+
df['dc_slow_mid'] = (df['dc_slow_h'] + df['dc_slow_l']) / 2
|
| 16 |
+
|
| 17 |
+
df['mid_gap'] = df['dc_fast_mid'] - df['dc_slow_mid']
|
| 18 |
+
|
| 19 |
+
# Shift to prevent lookahead
|
| 20 |
+
df['dc_f_mid_1'] = df['dc_fast_mid'].shift(1)
|
| 21 |
+
df['dc_s_mid_1'] = df['dc_slow_mid'].shift(1)
|
| 22 |
+
|
| 23 |
+
df['mid_upper'] = df[['dc_f_mid_1', 'dc_s_mid_1']].max(axis=1)
|
| 24 |
+
df['mid_lower'] = df[['dc_f_mid_1', 'dc_s_mid_1']].min(axis=1)
|
| 25 |
+
|
| 26 |
+
# Simplified Entry: True Crossover + Expansion
|
| 27 |
+
df['is_bull_cross'] = (df['open'].shift(1) < df['mid_upper']) & (df['close'].shift(1) > df['mid_upper'])
|
| 28 |
+
df['is_bear_cross'] = (df['open'].shift(1) > df['mid_lower']) & (df['close'].shift(1) < df['mid_lower'])
|
| 29 |
+
|
| 30 |
+
df['gap_expansion'] = df['mid_gap'].abs() >= 1.00 # 100 pts
|
| 31 |
+
|
| 32 |
+
# Entry Signals
|
| 33 |
+
df['signal'] = 0
|
| 34 |
+
df.loc[df['is_bull_cross'] & df['gap_expansion'] & (df['mid_gap'] > 0), 'signal'] = 1
|
| 35 |
+
df.loc[df['is_bear_cross'] & df['gap_expansion'] & (df['mid_gap'] < 0), 'signal'] = -1
|
| 36 |
+
|
| 37 |
+
# Simulate Trades
|
| 38 |
+
trades = []
|
| 39 |
+
in_trade = False
|
| 40 |
+
trade_dir = 0
|
| 41 |
+
entry_price = 0
|
| 42 |
+
hard_sl = 0
|
| 43 |
+
locked_sl = 0
|
| 44 |
+
max_float = 0
|
| 45 |
+
|
| 46 |
+
points = 0.01 # Assuming 2 decimal pricing for Gold, 1 point = 0.01 (Wait, MT5 XAUUSD pnt = 0.01)
|
| 47 |
+
|
| 48 |
+
for row in df.itertuples():
|
| 49 |
+
if getattr(row, 'signal') == 1 and not in_trade:
|
| 50 |
+
in_trade = True
|
| 51 |
+
trade_dir = 1
|
| 52 |
+
entry_price = getattr(row, 'open')
|
| 53 |
+
hard_sl = entry_price - 21.00 # 2100 pts SL Mode A
|
| 54 |
+
locked_sl = hard_sl
|
| 55 |
+
max_float = 0
|
| 56 |
+
continue
|
| 57 |
+
|
| 58 |
+
if getattr(row, 'signal') == -1 and not in_trade:
|
| 59 |
+
in_trade = True
|
| 60 |
+
trade_dir = -1
|
| 61 |
+
entry_price = getattr(row, 'open')
|
| 62 |
+
hard_sl = entry_price + 21.00
|
| 63 |
+
locked_sl = hard_sl
|
| 64 |
+
max_float = 0
|
| 65 |
+
continue
|
| 66 |
+
|
| 67 |
+
if in_trade:
|
| 68 |
+
if trade_dir == 1:
|
| 69 |
+
current_high_float = (getattr(row, 'high') - entry_price) / points
|
| 70 |
+
current_low_float = (getattr(row, 'low') - entry_price) / points
|
| 71 |
+
close_float = (getattr(row, 'close') - entry_price) / points
|
| 72 |
+
|
| 73 |
+
max_float = max(max_float, current_high_float)
|
| 74 |
+
|
| 75 |
+
# Check Lock Updates (evaluate HIGH of candle)
|
| 76 |
+
if max_float >= 600:
|
| 77 |
+
locked_sl = max(locked_sl, entry_price + 5.00) # Stage 3 Lock 500pts minimum
|
| 78 |
+
elif max_float >= 450:
|
| 79 |
+
locked_sl = max(locked_sl, entry_price + 3.50) # Stage 2 Lock 350pts
|
| 80 |
+
elif max_float >= 350:
|
| 81 |
+
locked_sl = max(locked_sl, entry_price + 3.00) # Stage 1 Lock 300pts
|
| 82 |
+
|
| 83 |
+
# Check Exit (evaluate LOW of candle against SL)
|
| 84 |
+
if getattr(row, 'low') <= locked_sl:
|
| 85 |
+
pnl = (locked_sl - entry_price) / points
|
| 86 |
+
pnl -= 50 # 50 pts Spread penalty
|
| 87 |
+
trades.append(pnl)
|
| 88 |
+
in_trade = False
|
| 89 |
+
|
| 90 |
+
elif trade_dir == -1:
|
| 91 |
+
current_high_float = (entry_price - getattr(row, 'low')) / points
|
| 92 |
+
current_low_float = (entry_price - getattr(row, 'high')) / points
|
| 93 |
+
|
| 94 |
+
max_float = max(max_float, current_high_float)
|
| 95 |
+
|
| 96 |
+
if max_float >= 600:
|
| 97 |
+
locked_sl = min(locked_sl, entry_price - 5.00)
|
| 98 |
+
elif max_float >= 450:
|
| 99 |
+
locked_sl = min(locked_sl, entry_price - 3.50)
|
| 100 |
+
elif max_float >= 350:
|
| 101 |
+
locked_sl = min(locked_sl, entry_price - 3.00)
|
| 102 |
+
|
| 103 |
+
if getattr(row, 'high') >= locked_sl:
|
| 104 |
+
pnl = (entry_price - locked_sl) / points
|
| 105 |
+
pnl -= 50 # 50 pts Spread penalty
|
| 106 |
+
trades.append(pnl)
|
| 107 |
+
in_trade = False
|
| 108 |
+
|
| 109 |
+
t_df = pd.Series(trades)
|
| 110 |
+
print(f"Total Trades: {len(t_df)}")
|
| 111 |
+
if len(t_df) > 0:
|
| 112 |
+
print(f"Wins: {len(t_df[t_df > 0])}")
|
| 113 |
+
print(f"Losses: {len(t_df[t_df < 0])}")
|
| 114 |
+
print(f"Win Rate: {(len(t_df[t_df > 0]) / len(t_df))*100:.2f}%")
|
| 115 |
+
print(f"Avg Win: {t_df[t_df > 0].mean():.2f} pts")
|
| 116 |
+
print(f"Avg Loss: {t_df[t_df < 0].mean():.2f} pts")
|
| 117 |
+
print(f"Net Profit Expected: {t_df.sum():.2f} pts")
|
| 118 |
+
|
| 119 |
+
if __name__ == '__main__':
|
| 120 |
+
run_simulation()
|