-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquantmind.py
More file actions
131 lines (106 loc) · 3.83 KB
/
Copy pathquantmind.py
File metadata and controls
131 lines (106 loc) · 3.83 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
# QuantMind - Systematic Trading Thinking Framework
# Not a bot, a discipline
from typing import Callable, Optional
from enum import Enum
class MarketRegime(Enum):
TRENDING_UP = "trending_up"
TRENDING_DOWN = "trending_down"
RANGING = "ranging"
VOLATILE = "volatile"
class SignalStrength(Enum):
STRONG_BUY = 5
BUY = 4
NEUTRAL = 3
SELL = 2
STRONG_SELL = 1
class MarketAnalyzer:
"""
Multi-factor market analysis framework.
Factors:
- Price action (trend, support/resistance)
- Momentum (RSI, MACD)
- Sentiment (volume, breadth)
- Macro context
"""
def __init__(self):
self.regime = MarketRegime.RANGING
self.factors = {}
def analyze(self, price_data: dict) -> dict:
"""Analyze market and return regime + signals."""
trend = self._detect_trend(price_data)
momentum = self._calc_momentum(price_data)
regime = self._classify_regime(trend, momentum)
self.regime = regime
return {
"regime": regime.value,
"trend": trend,
"momentum": momentum,
"signal": self._generate_signal(trend, momentum, regime),
"confidence": 0.75
}
def _detect_trend(self, data: dict) -> str:
# Simplified trend detection
prices = data.get("prices", [])
if len(prices) < 2:
return "neutral"
if prices[-1] > prices[0]:
return "up"
return "down"
def _calc_momentum(self, data: dict) -> float:
# Simplified RSI-like calculation
return 50.0 # Neutral
def _classify_regime(self, trend: str, momentum: float):
if trend == "up" and momentum > 55:
return MarketRegime.TRENDING_UP
if trend == "down" and momentum < 45:
return MarketRegime.TRENDING_DOWN
return MarketRegime.RANGING
def _generate_signal(self, trend: str, momentum: float, regime: MarketRegime) -> dict:
if regime == MarketRegime.TRENDING_UP:
return {"direction": "buy", "strength": SignalStrength.BUY.value}
if regime == MarketRegime.TRENDING_DOWN:
return {"direction": "sell", "strength": SignalStrength.SELL.value}
return {"direction": "hold", "strength": SignalStrength.NEUTRAL.value}
class RiskManager:
"""
Position sizing and risk control.
Rules:
- Never risk more than 2% per trade
- Maximum 5 positions at once
- Stop loss at 1.5x ATR
"""
def __init__(self, max_risk_pct: float = 0.02, max_positions: int = 5):
self.max_risk_pct = max_risk_pct
self.max_positions = max_positions
self.positions = []
def calculate_size(self, account_value: float, entry: float, stop: float) -> float:
"""Calculate position size based on risk."""
risk_amount = account_value * self.max_risk_pct
risk_per_share = abs(entry - stop)
if risk_per_share == 0:
return 0
return risk_amount / risk_per_share
def can_open(self) -> bool:
return len(self.positions) < self.max_positions
class TradingJournal:
"""Track all trades for learning and improvement."""
def __init__(self):
self.trades = []
def record(self, trade: dict):
trade["id"] = len(self.trades) + 1
self.trades.append(trade)
def summary(self) -> dict:
if not self.trades:
return {"total": 0, "win_rate": 0}
wins = sum(1 for t in self.trades if t.get("pnl", 0) > 0)
return {
"total": len(self.trades),
"wins": wins,
"losses": len(self.trades) - wins,
"win_rate": wins / len(self.trades)
}
if __name__ == "__main__":
analyzer = MarketAnalyzer()
data = {"prices": [100, 102, 101, 103, 105]}
result = analyzer.analyze(data)
print("Market:", result)