Feasibility Analysis
Section D: TP/SL feasibility with path-dependent probability heatmaps.
feasibility
TP/SL feasibility analysis: probabilistic hitting of targets vs stops.
hit_matrix(trades, tp_grid, sl_grid, min_sample=5)
Compute probability heatmap: P(hit TP before SL) for grid of TP/SL values.
This is path-dependent: checks if price hits TP before hitting SL within the trade horizon.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trades
|
TradeSet
|
Trade geometry data (must have mfe_path and mae_path) |
required |
tp_grid
|
ndarray
|
Array of take-profit levels (positive %, e.g., [0.5, 1.0, 1.5, ...]) |
required |
sl_grid
|
ndarray
|
Array of stop-loss levels (positive %, e.g., [0.5, 1.0, 1.5, ...]) |
required |
min_sample
|
int
|
Minimum sample size required for valid cell |
5
|
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary containing: - 'tp_grid': array of TP values - 'sl_grid': array of SL values - 'prob_matrix': 2D array of P(TP before SL) [shape: len(tp_grid) x len(sl_grid)] - 'count_matrix': 2D array of sample counts - 'tp_hit_matrix': 2D array of TP hit counts - 'sl_hit_matrix': 2D array of SL hit counts |
Notes
For each trade, we check the paths: - TP hit: first bar where MFE >= tp - SL hit: first bar where -MAE >= sl (i.e., MAE <= -sl) - TP before SL: t_tp < t_sl (or SL never hit)
Source code in signal_analyzer/analysis/feasibility.py
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 | |
ev_proxy(hit_data, slippage=0.0, commission=0.0)
Compute expected value proxy for TP/SL grid.
EV = P(TP) * TP - P(SL) * SL - costs
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hit_data
|
dict
|
Output from hit_matrix |
required |
slippage
|
float
|
Slippage cost per trade (% of entry) |
0.0
|
commission
|
float
|
Commission cost per trade (% of entry) |
0.0
|
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary containing: - 'ev_matrix': 2D array of expected values - 'risk_reward_ratio': 2D array of TP/SL ratios - 'robust_zones': boolean mask where EV is positive and stable |
Source code in signal_analyzer/analysis/feasibility.py
find_best_zones(ev_data, top_n=5)
Find the top N TP/SL zones by expected value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ev_data
|
dict
|
Output from ev_proxy |
required |
top_n
|
int
|
Number of top zones to return |
5
|
Returns:
| Type | Description |
|---|---|
list of dict
|
List of top zones, each containing: - 'tp': take-profit level - 'sl': stop-loss level - 'ev': expected value - 'rr': risk-reward ratio |