File size: 5,490 Bytes
b79786e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pandas as pd
import numpy as np

def calculate_adx(df, period=14):
    df['h_l'] = df['high'] - df['low']
    df['h_pc'] = (df['high'] - df['close'].shift(1)).abs()
    df['l_pc'] = (df['low'] - df['close'].shift(1)).abs()
    df['tr'] = df[['h_l', 'h_pc', 'l_pc']].max(axis=1)
    df['+dm'] = np.where((df['high'] - df['high'].shift(1)) > (df['low'].shift(1) - df['low']), np.maximum(df['high'] - df['high'].shift(1), 0), 0)
    df['-dm'] = np.where((df['low'].shift(1) - df['low']) > (df['high'] - df['high'].shift(1)), np.maximum(df['low'].shift(1) - df['low'], 0), 0)
    alpha = 1 / period
    df['tr_smooth'] = df['tr'].ewm(alpha=alpha, adjust=False).mean() * period
    df['+dm_smooth'] = df['+dm'].ewm(alpha=alpha, adjust=False).mean() * period
    df['-dm_smooth'] = df['-dm'].ewm(alpha=alpha, adjust=False).mean() * period
    df['+di'] = 100 * (df['+dm_smooth'] / df['tr_smooth'])
    df['-di'] = 100 * (df['-dm_smooth'] / df['tr_smooth'])
    df['dx'] = 100 * (df['+di'] - df['-di']).abs() / (df['+di'] + df['-di']).replace(0, 1)
    df['adx'] = df['dx'].ewm(alpha=alpha, adjust=False).mean()
    return df

print("Loading 4-Months of tickflow data...")
m1 = pd.read_parquet('tickflow_M1.parquet')
m5 = pd.read_parquet('tickflow_M5.parquet')
m15 = pd.read_parquet('tickflow_M15.parquet')

# Indices are already DatetimeIndex 'dt'
print("Calculating ADX for massive dataset...")
m1 = calculate_adx(m1, 14)
m5 = calculate_adx(m5, 14)
m15 = calculate_adx(m15, 14)

df = m1[['open', 'high', 'low', 'close', 'adx', '+di', '-di']].copy()
df.rename(columns={'adx': 'adx_M1', '+di': 'di_plus_M1', '-di': 'di_minus_M1'}, inplace=True)

m5_cols = m5[['adx', '+di', '-di']].rename(columns={'adx': 'adx_M5', '+di': 'di_plus_M5', '-di': 'di_minus_M5'})
df = df.join(m5_cols, how='left').ffill()

m15_cols = m15[['adx', '+di', '-di']].rename(columns={'adx': 'adx_M15', '+di': 'di_plus_M15', '-di': 'di_minus_M15'})
df = df.join(m15_cols, how='left').ffill()
df.dropna(inplace=True)

print(f"Total Rows Ready: {len(df)}")

t = 18.0
a1 = (df['adx_M1'] >= t).astype(int)
a5 = (df['adx_M5'] >= t).astype(int)
a15 = (df['adx_M15'] >= t).astype(int)

# Correct EA Mapping
df['phase'] = 1
df.loc[(a15==0) & (a5==0) & (a1==1), 'phase'] = 2
df.loc[(a15==0) & (a5==1) & (a1==0), 'phase'] = 3
df.loc[(a15==1) & (a5==0) & (a1==0), 'phase'] = 4
df.loc[(a15==0) & (a5==1) & (a1==1), 'phase'] = 5
df.loc[(a15==1) & (a5==0) & (a1==1), 'phase'] = 6
df.loc[(a15==1) & (a5==1) & (a1==0), 'phase'] = 7
df.loc[(a15==1) & (a5==1) & (a1==1), 'phase'] = 8

df['phase_prev'] = df['phase'].shift(1)
df['phase_transition'] = df['phase'] != df['phase_prev']
df['dmi_dir_m15'] = np.where(df['di_plus_M15'] > df['di_minus_M15'], 1, -1)
df['dmi_dir_m5'] = np.where(df['di_plus_M5'] > df['di_minus_M5'], 1, -1)

results = []
tp_pips = 20.0
sl_pips = 20.0

print("Scanning 64 possible transitions over 4 months...")
for p_prev in range(1, 9):
    for p_curr in range(1, 9):
        if p_prev == p_curr: continue
        
        mask = (df['phase_transition']) & (df['phase_prev'] == p_prev) & (df['phase'] == p_curr)
        indices = np.where(mask)[0]
        if len(indices) < 20: continue # minimum threshold for stats reliability
        
        wins_trend = 0
        wins_fade = 0
        for i in indices:
            if i > len(df) - 60: continue
            
            entry = df['close'].iloc[i]
            # determine the primary active direction
            if p_prev >= 5 or p_curr >= 5:
                dir_mult = df['dmi_dir_m15'].iloc[i]
            elif p_curr in [3, 4] or p_prev in [3, 4]:
                dir_mult = df['dmi_dir_m5'].iloc[i]
            else:
                dir_mult = 1 if df['close'].iloc[i] < df['open'].iloc[i] else -1 # Sideways fade
                
            future_highs = df['high'].iloc[i+1:i+61].values
            future_lows = df['low'].iloc[i+1:i+61].values
            
            # Trend Check
            wt = 0
            if dir_mult == 1:
                for h, l in zip(future_highs, future_lows):
                    if h >= entry + (tp_pips * 0.01): wt=1; break
                    if l <= entry - (sl_pips * 0.01): break
            else:
                for h, l in zip(future_highs, future_lows):
                    if l <= entry - (tp_pips * 0.01): wt=1; break
                    if h >= entry + (sl_pips * 0.01): break
            wins_trend += wt
            
            # Fade Check
            wf = 0
            dir_fade = -dir_mult
            if dir_fade == 1:
                for h, l in zip(future_highs, future_lows):
                    if h >= entry + (tp_pips * 0.01): wf=1; break
                    if l <= entry - (sl_pips * 0.01): break
            else:
                for h, l in zip(future_highs, future_lows):
                    if l <= entry - (tp_pips * 0.01): wf=1; break
                    if h >= entry + (sl_pips * 0.01): break
            wins_fade += wf
            
        count = len(indices)
        wr_trend = (wins_trend / count) * 100
        wr_fade = (wins_fade / count) * 100
        
        results.append({
            'Trans': f'P{p_prev} -> P{p_curr}',
            'Count': count,
            'Trend_WR': round(wr_trend, 1),
            'Fade_WR': round(wr_fade, 1)
        })

res_df = pd.DataFrame(results)
res_df = res_df.sort_values('Count', ascending=False)
with open('out_large_matrix.txt', 'w') as f:
    f.write(res_df.to_string(index=False))
print("Done! Saved to out_large_matrix.txt")