Create risk_engine.py
Browse files- risk_engine.py +81 -0
risk_engine.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
from typing import Dict, Any
|
| 3 |
+
from config import (
|
| 4 |
+
MAX_RISK_PER_TRADE,
|
| 5 |
+
HIGH_VOLATILITY_THRESHOLD,
|
| 6 |
+
REDUCED_RISK_FACTOR,
|
| 7 |
+
)
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def compute_stop_distance(atr: float, multiplier: float = 2.0) -> float:
|
| 11 |
+
return atr * multiplier
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def compute_position_size(
|
| 15 |
+
account_equity: float,
|
| 16 |
+
entry_price: float,
|
| 17 |
+
stop_distance: float,
|
| 18 |
+
risk_fraction: float = MAX_RISK_PER_TRADE,
|
| 19 |
+
) -> float:
|
| 20 |
+
if stop_distance <= 0 or entry_price <= 0:
|
| 21 |
+
return 0.0
|
| 22 |
+
dollar_risk = account_equity * risk_fraction
|
| 23 |
+
units = dollar_risk / stop_distance
|
| 24 |
+
notional = units * entry_price
|
| 25 |
+
return notional
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def compute_risk_fraction(
|
| 29 |
+
vol_ratio: float,
|
| 30 |
+
regime_score: float,
|
| 31 |
+
base_risk: float = MAX_RISK_PER_TRADE,
|
| 32 |
+
) -> float:
|
| 33 |
+
risk = base_risk
|
| 34 |
+
|
| 35 |
+
if vol_ratio > HIGH_VOLATILITY_THRESHOLD:
|
| 36 |
+
risk *= REDUCED_RISK_FACTOR
|
| 37 |
+
|
| 38 |
+
if regime_score < 0.4:
|
| 39 |
+
risk *= REDUCED_RISK_FACTOR
|
| 40 |
+
elif regime_score < 0.6:
|
| 41 |
+
risk *= 0.75
|
| 42 |
+
|
| 43 |
+
return float(np.clip(risk, 0.001, base_risk))
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def evaluate_risk(
|
| 47 |
+
df_last_close: float,
|
| 48 |
+
atr: float,
|
| 49 |
+
atr_pct: float,
|
| 50 |
+
regime_score: float,
|
| 51 |
+
vol_ratio: float,
|
| 52 |
+
account_equity: float = 10000.0,
|
| 53 |
+
stop_multiplier: float = 2.0,
|
| 54 |
+
) -> Dict[str, Any]:
|
| 55 |
+
stop_distance = compute_stop_distance(atr, stop_multiplier)
|
| 56 |
+
risk_fraction = compute_risk_fraction(vol_ratio, regime_score)
|
| 57 |
+
position_notional = compute_position_size(
|
| 58 |
+
account_equity, df_last_close, stop_distance, risk_fraction
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
stop_price_long = df_last_close - stop_distance
|
| 62 |
+
stop_price_short = df_last_close + stop_distance
|
| 63 |
+
risk_reward_target = stop_distance * 2.0
|
| 64 |
+
|
| 65 |
+
target_long = df_last_close + risk_reward_target
|
| 66 |
+
target_short = df_last_close - risk_reward_target
|
| 67 |
+
|
| 68 |
+
return {
|
| 69 |
+
"entry_price": df_last_close,
|
| 70 |
+
"atr": atr,
|
| 71 |
+
"atr_pct": atr_pct,
|
| 72 |
+
"stop_distance": stop_distance,
|
| 73 |
+
"stop_price_long": stop_price_long,
|
| 74 |
+
"stop_price_short": stop_price_short,
|
| 75 |
+
"target_long": target_long,
|
| 76 |
+
"target_short": target_short,
|
| 77 |
+
"risk_fraction": risk_fraction,
|
| 78 |
+
"position_notional": position_notional,
|
| 79 |
+
"vol_ratio": vol_ratio,
|
| 80 |
+
"regime_score": regime_score,
|
| 81 |
+
}
|