RayMelius Claude Opus 4.6 commited on
Commit
1c7dab6
Β·
1 Parent(s): 44c2214

Update README, hybrid default, dynamic strategy switch, CLRH source, regime MDF

Browse files

- README rewritten with full Clearing House, RL agent, and MDF regime docs
- CH_AI_STRATEGY defaults to hybrid, switchable via POST /ch/api/strategy
- Source label CLEARINGHOUSE β†’ CLRH
- MDF snapshots include embedded technical indicators

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

Files changed (1) hide show
  1. README.md +208 -83
README.md CHANGED
@@ -11,7 +11,7 @@ app_port: 7860
11
  # StockEx – Kafka-based Stock Exchange Simulator
12
 
13
  A real-time stock exchange simulation built with **Apache Kafka**, **Python**, and **Flask**.
14
- Includes a FIX 4.4 order gateway, live matching engine, SSE-streamed dashboard, and candlestick charts.
15
 
16
  πŸ”— **Live demo:** [huggingface.co/spaces/RayMelius/StockEx](https://huggingface.co/spaces/RayMelius/StockEx)
17
  πŸ“¦ **Source:** [github.com/Bonum/StockEx](https://github.com/Bonum/StockEx)
@@ -21,103 +21,171 @@ Includes a FIX 4.4 order gateway, live matching engine, SSE-streamed dashboard,
21
  ## Architecture Overview
22
 
23
  ```
24
- β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
25
- β”‚ Single Docker Container β”‚
26
- β”‚ β”‚
27
- β”‚ nginx :7860 (reverse proxy) β”‚
28
- β”‚ / β†’ Dashboard :5000 β”‚
29
- β”‚ /fix/ β†’ FIX UI :5002 β”‚
30
- β”‚ β”‚
31
- β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
32
- β”‚ β”‚ MD Feeder β”‚ β”‚ FIX OEG β”‚ β”‚ FIX UI Client β”‚ β”‚
33
- β”‚ β”‚ (simulator) β”‚ β”‚ :5001 β”‚ β”‚ Flask :5002 β”‚ β”‚
34
- β”‚ β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
35
- β”‚ β”‚ β”‚ β”‚ FIX 4.4 β”‚
36
- β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚
37
- β”‚ β–Ό β–Ό β”‚ β”‚
38
- β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚
39
- β”‚ β”‚ Apache Kafka (KRaft) β”‚ β”‚ β”‚
40
- β”‚ β”‚ orders β”‚ trades β”‚ snapshots β”‚ control β”‚ β”‚ β”‚
41
- β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚
42
- β”‚ β”‚ β”‚ β”‚
43
- β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚
44
- β”‚ β–Ό β”‚ β”‚
45
- β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚
46
- β”‚ β”‚ Matcher β”‚ β”‚ Dashboard Flask :5000 β”‚β—„β”€β”€β”˜ β”‚
47
- β”‚ β”‚ Flask:6000 β”‚ β”‚ SSE + REST API β”‚ β”‚
48
- β”‚ β”‚ SQLite DB β”‚ β”‚ SQLite OHLCV history β”‚ β”‚
49
- β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
50
- β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
 
 
51
  ```
52
 
53
  ---
54
 
55
- ## Message Flow
56
 
57
- ```mermaid
58
- flowchart TB
59
- Browser("🌐 Browser")
 
 
 
 
 
 
 
 
60
 
61
- subgraph Container["Docker Container"]
62
- nginx["nginx :7860"]
63
 
64
- subgraph Services["Python Services"]
65
- Dashboard["Dashboard\n:5000"]
66
- Matcher["Matcher\n:6000"]
67
- MDF["MD Feeder"]
68
- FIXOEG["FIX OEG\n:5001"]
69
- FIXUI["FIX UI Client\n:5002"]
70
- end
71
 
72
- subgraph KafkaTopics["Apache Kafka (KRaft)"]
73
- Ko[("orders")]
74
- Kt[("trades")]
75
- Ks[("snapshots")]
76
- Kc[("control")]
77
- end
78
 
79
- DB[("SQLite")]
80
- end
 
 
 
81
 
82
- Browser -- "HTTP / SSE" --> nginx
83
- nginx -- "/" --> Dashboard
84
- nginx -- "/fix/" --> FIXUI
85
 
86
- FIXUI -- "FIX 4.4\nNewOrderSingle" --> FIXOEG
87
- FIXOEG -- "order" --> Ko
88
- FIXOEG -- "ExecReport" --> FIXUI
89
 
90
- MDF -- "synthetic orders\n& snapshots" --> Ko
91
- MDF -- "BBO snapshots" --> Ks
 
 
 
92
 
93
- Dashboard -- "start/stop\nsuspend/resume" --> Kc
94
- Kc -- "control signal" --> MDF
95
 
96
- Ko -- "limit / market orders" --> Matcher
97
- Matcher -- "fills" --> Kt
98
- Matcher -- "BBO updates" --> Ks
99
- Matcher --- DB
100
 
101
- Ko --> Dashboard
102
- Kt --> Dashboard
103
- Ks --> Dashboard
104
- Kt -- "OHLCV\n1-min buckets" --> DB
 
 
 
 
 
 
 
 
 
 
 
 
 
105
 
106
- Dashboard -- "SSE stream\n(orders/trades/snapshots)" --> Browser
107
  ```
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
 
109
  ---
110
 
111
- ## Services
112
 
113
- | Service | Port | Description |
 
 
 
 
114
  |---|---|---|
115
- | **nginx** | 7860 | Reverse proxy β€” single public port |
116
- | **Dashboard** | 5000 | Flask app, SSE streaming, session control, OHLCV history |
117
- | **Matcher** | 6000 | Price-time priority matching engine, REST API, SQLite persistence |
118
- | **MD Feeder** | β€” | Synthetic market data generator; responds to start/stop/suspend/resume |
119
- | **FIX OEG** | 5001 | FIX 4.4 acceptor β€” translates FIX messages to Kafka orders |
120
- | **FIX UI Client** | 5002 | Browser UI for sending FIX orders and viewing execution reports |
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
121
 
122
  ---
123
 
@@ -132,11 +200,25 @@ flowchart TB
132
  - **Start / End of Day** β€” resets opening prices, starts/stops MD simulation
133
  - **Suspend / Resume** β€” pauses order generation without ending the session
134
  - **Order Management** β€” cancel and amend resting orders from the dashboard
 
 
135
  - **FIX UI Client** β€” send NewOrderSingle via FIX 4.4, view execution reports at `/fix/`
136
  - **Mobile Responsive** β€” single-column layout on phones and tablets
137
 
138
  ---
139
 
 
 
 
 
 
 
 
 
 
 
 
 
140
  ## Securities
141
 
142
  | Symbol | Name | Start Price |
@@ -146,11 +228,11 @@ flowchart TB
146
  | EXAE | Athens Exchange Group | €42.05 |
147
  | QUEST | Quest Holdings | €12.60 |
148
  | NBG | National Bank of Greece | €18.05 |
149
- | ATTIKA | Attika Bank | €3.95 |
150
  | INTKA | Intertech | €3.95 |
151
- | LAMDA | Lamda Development | €3.95 |
152
  | AEG | AEG | €3.95 |
153
  | AAAK | AAAK | €3.95 |
 
154
 
155
  ---
156
 
@@ -158,11 +240,42 @@ flowchart TB
158
 
159
  - **Apache Kafka 3.7** (KRaft mode β€” no ZooKeeper)
160
  - **Python 3.11** Β· Flask 2.2 Β· kafka-python 2.0
 
161
  - **QuickFIX** (FIX 4.4 protocol, compiled from C++)
162
- - **SQLite** β€” matcher order/trade persistence + OHLCV history
163
  - **Canvas 2D API** β€” candlestick charts rendered client-side
164
  - **Server-Sent Events** β€” real-time push to browser (no WebSocket)
165
  - **nginx** β€” reverse proxy with `sub_filter` URL rewriting for `/fix/`
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
166
 
167
  ---
168
 
@@ -234,11 +347,13 @@ docker-compose up
234
 
235
  | URL | Service |
236
  |---|---|
237
- | http://localhost:5000 | Trading Dashboard |
238
- | http://localhost:5001 | FIX OEG (TCP, not HTTP) |
239
  | http://localhost:5002 | FIX UI Client |
240
  | http://localhost:6000 | Matcher REST API |
241
 
 
 
242
  ---
243
 
244
  ### Option 4 β€” Local without Docker
@@ -248,7 +363,7 @@ docker-compose up
248
  ```bash
249
  git clone https://github.com/Bonum/StockEx.git
250
  cd StockEx
251
- pip install kafka-python Flask requests quickfix
252
 
253
  # In separate terminals:
254
  export PYTHONPATH=$(pwd)
@@ -258,6 +373,7 @@ export MATCHER_URL=http://localhost:6000
258
  python matcher/matcher.py # terminal 1
259
  python md_feeder/mdf_simulator.py # terminal 2
260
  python dashboard/dashboard.py # terminal 3
 
261
  ```
262
 
263
  Then open http://localhost:5000.
@@ -281,13 +397,22 @@ GitHub Actions workflows: `.github/workflows/ci.yml` Β· `.github/workflows/deplo
281
  ```
282
  StockEx/
283
  β”œβ”€β”€ matcher/ # Matching engine + SQLite persistence
284
- β”œβ”€β”€ md_feeder/ # Synthetic market data generator
285
  β”œβ”€β”€ dashboard/ # Flask dashboard + SSE + OHLCV history
286
  β”‚ └── templates/ # Single-page trading UI
 
 
 
 
 
 
 
287
  β”œβ”€β”€ fix_oeg/ # FIX 4.4 Order Entry Gateway
288
  β”œβ”€β”€ fix-ui-client/ # FIX browser UI
 
289
  β”œβ”€β”€ shared/ # Shared config + Kafka utils
290
  β”œβ”€β”€ shared_data/ # securities.txt (symbol list + prices)
 
291
  β”œβ”€β”€ Dockerfile # HuggingFace / single-container build
292
  β”œβ”€β”€ docker-compose.yml # Local multi-container dev setup
293
  β”œβ”€β”€ entrypoint.sh # Container startup (Kafka β†’ services β†’ nginx)
 
11
  # StockEx – Kafka-based Stock Exchange Simulator
12
 
13
  A real-time stock exchange simulation built with **Apache Kafka**, **Python**, and **Flask**.
14
+ Includes a FIX 4.4 order gateway, live matching engine, SSE-streamed dashboard, AI-powered clearing house members, and candlestick charts.
15
 
16
  πŸ”— **Live demo:** [huggingface.co/spaces/RayMelius/StockEx](https://huggingface.co/spaces/RayMelius/StockEx)
17
  πŸ“¦ **Source:** [github.com/Bonum/StockEx](https://github.com/Bonum/StockEx)
 
21
  ## Architecture Overview
22
 
23
  ```
24
+ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
25
+ β”‚ Single Docker Container β”‚
26
+ β”‚ β”‚
27
+ β”‚ nginx :7860 (reverse proxy) β”‚
28
+ β”‚ / β†’ Dashboard :5000 β”‚
29
+ β”‚ /ch/ β†’ Clearing House :5004 β”‚
30
+ β”‚ /fix/ β†’ FIX UI :5002 β”‚
31
+ β”‚ /frontend/ β†’ Order Entry :5003 β”‚
32
+ β”‚ β”‚
33
+ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
34
+ β”‚ β”‚ MD Feeder β”‚ β”‚ FIX OEG β”‚ β”‚ FIX UI β”‚ β”‚ AI Analystβ”‚ β”‚
35
+ β”‚ β”‚ (simulator) β”‚ β”‚ :5001 β”‚ β”‚ :5002 β”‚ β”‚ β”‚ β”‚
36
+ β”‚ β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜ β”‚
37
+ β”‚ β”‚ β”‚ β”‚ FIX 4.4 β”‚ β”‚
38
+ β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚ β”‚
39
+ β”‚ β–Ό β–Ό β”‚ β”‚ β”‚
40
+ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚
41
+ β”‚ β”‚ Apache Kafka (KRaft) β”‚ β”‚ β”‚
42
+ β”‚ β”‚ orders β”‚ trades β”‚ snapshots β”‚ control β”‚ ai_insights β”‚ β”‚ β”‚
43
+ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚
44
+ β”‚ β”‚ β”‚ β”‚
45
+ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚
46
+ β”‚ β–Ό β”‚ β”‚
47
+ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚
48
+ β”‚ β”‚ Matcher β”‚ β”‚ Dashboard :5000 β”‚ β”‚ Clearing House β”‚ β”‚ β”‚
49
+ β”‚ β”‚ Flask:6000 β”‚ β”‚ SSE + REST API β”‚ β”‚ :5004 β”‚β—„β”˜ β”‚
50
+ β”‚ β”‚ SQLite DB β”‚ β”‚ SQLite OHLCV β”‚ β”‚ AI Members β”‚ β”‚
51
+ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
52
+ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
53
  ```
54
 
55
  ---
56
 
57
+ ## Services
58
 
59
+ | Service | Port | Description |
60
+ |---|---|---|
61
+ | **nginx** | 7860 | Reverse proxy β€” single public port |
62
+ | **Dashboard** | 5000 | Flask app, SSE streaming, session control, OHLCV history, AI insights |
63
+ | **Matcher** | 6000 | Price-time priority matching engine, REST API, SQLite persistence |
64
+ | **MD Feeder** | β€” | Regime-based synthetic market data generator with technical indicators |
65
+ | **Clearing House** | 5004 | 10 AI-driven trading members with RL/LLM strategies |
66
+ | **AI Analyst** | β€” | LLM market commentary (Groq β†’ HF β†’ Ollama fallback) |
67
+ | **FIX OEG** | 5001 | FIX 4.4 acceptor β€” translates FIX messages to Kafka orders |
68
+ | **FIX UI Client** | 5002 | Browser UI for sending FIX orders and viewing execution reports |
69
+ | **Frontend** | 5003 | Simple order entry form |
70
 
71
+ ---
 
72
 
73
+ ## Clearing House
 
 
 
 
 
 
74
 
75
+ The Clearing House simulates **10 trading members** (USR01–USR10) that autonomously trade securities using AI strategies.
 
 
 
 
 
76
 
77
+ ### Member Details
78
+ - Starting capital: **€100,000** each
79
+ - Daily obligation: **20 securities** (quantity sum) per trading day
80
+ - Password = username (e.g. USR01 logs in with password "USR01")
81
+ - End-of-day settlement triggered by Dashboard session end
82
 
83
+ ### AI Trading Strategies
 
 
84
 
85
+ Members use one of three strategy modes, switchable dynamically via `POST /ch/api/strategy`:
 
 
86
 
87
+ | Strategy | `CH_AI_STRATEGY` | Description |
88
+ |---|---|---|
89
+ | **Hybrid** (default) | `hybrid` | USR01–05 use RL, USR06–10 use LLM |
90
+ | **RL only** | `rl` | All members use the RL neural network |
91
+ | **LLM only** | `llm` | All members use LLM (Groq β†’ HF β†’ Ollama) |
92
 
93
+ Every strategy falls back to rule-based trading if the primary method fails.
 
94
 
95
+ ### RL Agent β€” Neural Network Details
 
 
 
96
 
97
+ The RL strategy uses [Adilbai/stock-trading-rl-agent](https://huggingface.co/Adilbai/stock-trading-rl-agent), a **PPO (Proximal Policy Optimization)** model trained with Stable-Baselines3.
98
+
99
+ | Property | Value |
100
+ |---|---|
101
+ | **Algorithm** | PPO (Proximal Policy Optimization) |
102
+ | **Policy** | MlpPolicy (Multi-Layer Perceptron) |
103
+ | **Model size** | ~2.5 MB (`final_model.zip`) |
104
+ | **Scaler** | ~50 KB (`scaler.pkl`, StandardScaler) |
105
+ | **Observation space** | 3,008 dimensions (60 bars Γ— 50 features + 8 portfolio) |
106
+ | **Action space** | 2D: action type (Hold/Buy/Sell) + position size (0–1) |
107
+ | **Training steps** | 500,000 |
108
+ | **Learning rate** | 0.0003 |
109
+ | **Gamma** | 0.99 |
110
+ | **License** | MIT (free, runs locally, no API keys needed) |
111
+ | **Stored at** | Downloaded to `/app/data/rl_model/` on first use via HuggingFace Hub |
112
+
113
+ **How the RL agent decides:**
114
 
 
115
  ```
116
+ Kafka trades β†’ Per-symbol OHLCV bars (60-bar rolling window)
117
+ ↓
118
+ Technical indicators (50 features per bar):
119
+ SMA(5,10,20,50), EMA(12,26), MACD, Signal, Histogram,
120
+ RSI(14), Bollinger Bands (upper/lower/width/position),
121
+ Volatility(20), Price changes, H/L ratio, Volume ratio,
122
+ + lagged features at 1,2,3,5,10 bars
123
+ ↓
124
+ Scaler normalizes β†’ 3,000 market features
125
+ + 8 portfolio features (capital, position, net worth, etc.)
126
+ ↓
127
+ PPO neural network forward pass β†’ [action_type, position_size]
128
+ ↓
129
+ action_type: 0=Hold, 1=Buy, 2=Sell
130
+ position_size: fraction of capital (Buy) or holdings (Sell)
131
+ ```
132
+
133
+ The RL model is a pre-trained neural network, **not** an LLM β€” it runs a single forward pass through the policy network (~milliseconds) rather than generating text. No API keys or internet required at inference time.
134
+
135
+ ### LLM Strategy
136
+
137
+ Uses the same fallback chain as the AI Analyst:
138
+ - **Groq** (preferred on HF Spaces) β†’ `GROQ_API_KEY` required
139
+ - **HuggingFace Inference** β†’ `HF_TOKEN` required
140
+ - **Ollama** (preferred locally) β†’ `OLLAMA_HOST` required
141
+
142
+ Sends a text prompt with member state + market BBOs, expects JSON response with symbol/side/quantity/price.
143
+
144
+ ### Clearing House UI
145
+
146
+ - **Leaderboard** at `/ch/` β€” rankings, P&L, obligation status
147
+ - **Portfolio** at `/ch/portfolio` β€” holdings, trades, AI decisions (login required)
148
+ - **API**: `/ch/api/leaderboard`, `/ch/api/config`, `/ch/api/strategy`
149
 
150
  ---
151
 
152
+ ## Market Data Feeder β€” Regime-Based Simulation
153
 
154
+ The MD Feeder generates synthetic orders using a **regime-driven price dynamics engine** that produces meaningful technical indicator signals.
155
+
156
+ ### Price Regimes
157
+
158
+ | Regime | Behavior | Effect on Indicators |
159
  |---|---|---|
160
+ | **trending_up** | Positive drift + momentum | MACD > 0, RSI rising, price above SMA |
161
+ | **trending_down** | Negative drift + momentum | MACD < 0, RSI falling, price below SMA |
162
+ | **mean_revert** | Pulls toward start price | RSI oscillates around 50, BB position ~0.5 |
163
+ | **volatile** | Wide swings, expanded spread | BB width expands, RSI spikes, high volatility |
164
+ | **calm** | Tight range, narrow spread | BB width contracts, RSI stable, low volatility |
165
+
166
+ Regimes auto-rotate every 15–50 ticks. When price deviates significantly from start, the engine biases toward mean reversion to prevent runaway prices.
167
+
168
+ ### Snapshots with Embedded Indicators
169
+
170
+ Each market data snapshot includes computed indicators:
171
+
172
+ ```json
173
+ {
174
+ "symbol": "ALPHA",
175
+ "best_bid": 24.85,
176
+ "best_ask": 25.05,
177
+ "indicators": {
178
+ "sma_5": 24.92,
179
+ "sma_20": 24.78,
180
+ "ema_12": 24.88,
181
+ "ema_26": 24.75,
182
+ "macd": 0.13,
183
+ "rsi_14": 62.5,
184
+ "bb_pos": 0.72,
185
+ "regime": "trending_up"
186
+ }
187
+ }
188
+ ```
189
 
190
  ---
191
 
 
200
  - **Start / End of Day** β€” resets opening prices, starts/stops MD simulation
201
  - **Suspend / Resume** β€” pauses order generation without ending the session
202
  - **Order Management** β€” cancel and amend resting orders from the dashboard
203
+ - **Clearing House** β€” 10 AI members trading with RL/LLM strategies, leaderboard, portfolio UI
204
+ - **AI Analyst** β€” LLM-generated market commentary with provider switching
205
  - **FIX UI Client** β€” send NewOrderSingle via FIX 4.4, view execution reports at `/fix/`
206
  - **Mobile Responsive** β€” single-column layout on phones and tablets
207
 
208
  ---
209
 
210
+ ## Kafka Topics
211
+
212
+ | Topic | Description |
213
+ |---|---|
214
+ | `orders` | Limit orders from all sources (MDF, FIX, Clearing House, Frontend) |
215
+ | `trades` | Executed trades from the Matcher |
216
+ | `snapshots` | BBO snapshots with technical indicators from MD Feeder |
217
+ | `control` | Session control signals (start/stop/suspend/resume) |
218
+ | `ai_insights` | AI Analyst market commentary |
219
+
220
+ ---
221
+
222
  ## Securities
223
 
224
  | Symbol | Name | Start Price |
 
228
  | EXAE | Athens Exchange Group | €42.05 |
229
  | QUEST | Quest Holdings | €12.60 |
230
  | NBG | National Bank of Greece | €18.05 |
231
+ | ATTIK | Attika Bank | €3.95 |
232
  | INTKA | Intertech | €3.95 |
 
233
  | AEG | AEG | €3.95 |
234
  | AAAK | AAAK | €3.95 |
235
+ | EUROB | Eurobank | €3.95 |
236
 
237
  ---
238
 
 
240
 
241
  - **Apache Kafka 3.7** (KRaft mode β€” no ZooKeeper)
242
  - **Python 3.11** Β· Flask 2.2 Β· kafka-python 2.0
243
+ - **Stable-Baselines3** + PyTorch β€” RL trading agent (PPO)
244
  - **QuickFIX** (FIX 4.4 protocol, compiled from C++)
245
+ - **SQLite** β€” matcher order/trade persistence + OHLCV history + clearing house DB
246
  - **Canvas 2D API** β€” candlestick charts rendered client-side
247
  - **Server-Sent Events** β€” real-time push to browser (no WebSocket)
248
  - **nginx** β€” reverse proxy with `sub_filter` URL rewriting for `/fix/`
249
+ - **LLM integrations** β€” Groq, HuggingFace Inference, Ollama (local)
250
+
251
+ ---
252
+
253
+ ## Environment Variables
254
+
255
+ ### Core
256
+ | Variable | Default | Description |
257
+ |---|---|---|
258
+ | `KAFKA_BOOTSTRAP` | `kafka:9092` | Kafka broker address |
259
+ | `MATCHER_URL` | `http://matcher:6000` | Matcher service URL |
260
+ | `SECURITIES_FILE` | `/app/data/securities.txt` | Securities definition file |
261
+
262
+ ### Clearing House AI
263
+ | Variable | Default | Description |
264
+ |---|---|---|
265
+ | `CH_AI_STRATEGY` | `hybrid` | Trading strategy: `llm`, `rl`, or `hybrid` |
266
+ | `CH_AI_INTERVAL` | `45` | Seconds between AI trading cycles |
267
+ | `CH_RL_MODEL_REPO` | `Adilbai/stock-trading-rl-agent` | HuggingFace model repo for RL |
268
+ | `CH_RL_MIN_BARS` | `30` | Minimum price bars before RL starts |
269
+
270
+ ### LLM Providers
271
+ | Variable | Default | Description |
272
+ |---|---|---|
273
+ | `GROQ_API_KEY` | β€” | Groq API key (free tier available) |
274
+ | `GROQ_MODEL` | `llama-3.1-8b-instant` | Groq model name |
275
+ | `HF_TOKEN` | β€” | HuggingFace API token |
276
+ | `HF_MODEL` | `RayMelius/stockex-ch-trader` | HuggingFace model for CH trader |
277
+ | `OLLAMA_HOST` | β€” | Ollama server URL (e.g. `http://localhost:11434`) |
278
+ | `OLLAMA_MODEL` | `llama3.1:8b` | Ollama model name |
279
 
280
  ---
281
 
 
347
 
348
  | URL | Service |
349
  |---|---|
350
+ | http://localhost:5005 | Trading Dashboard |
351
+ | http://localhost:5004 | Clearing House |
352
  | http://localhost:5002 | FIX UI Client |
353
  | http://localhost:6000 | Matcher REST API |
354
 
355
+ Set `CH_AI_STRATEGY=rl` in `.env` or `docker-compose.yml` to use the RL agent for all members.
356
+
357
  ---
358
 
359
  ### Option 4 β€” Local without Docker
 
363
  ```bash
364
  git clone https://github.com/Bonum/StockEx.git
365
  cd StockEx
366
+ pip install kafka-python Flask requests quickfix stable-baselines3 huggingface_hub pandas scikit-learn
367
 
368
  # In separate terminals:
369
  export PYTHONPATH=$(pwd)
 
373
  python matcher/matcher.py # terminal 1
374
  python md_feeder/mdf_simulator.py # terminal 2
375
  python dashboard/dashboard.py # terminal 3
376
+ cd clearing_house && python app.py # terminal 4
377
  ```
378
 
379
  Then open http://localhost:5000.
 
397
  ```
398
  StockEx/
399
  β”œβ”€β”€ matcher/ # Matching engine + SQLite persistence
400
+ β”œβ”€β”€ md_feeder/ # Regime-based synthetic market data generator
401
  β”œβ”€β”€ dashboard/ # Flask dashboard + SSE + OHLCV history
402
  β”‚ └── templates/ # Single-page trading UI
403
+ β”œβ”€β”€ clearing_house/ # AI trading members (RL + LLM strategies)
404
+ β”‚ β”œβ”€β”€ app.py # Flask app, REST API, SSE
405
+ β”‚ β”œβ”€β”€ ch_ai_trader.py # Strategy dispatcher + LLM integration
406
+ β”‚ β”œβ”€β”€ ch_rl_trader.py # RL agent (PPO neural network)
407
+ β”‚ β”œβ”€β”€ ch_database.py # SQLite: members, trades, settlements
408
+ β”‚ └── templates/ # Leaderboard + portfolio UI
409
+ β”œβ”€β”€ ai_analyst/ # LLM market commentary service
410
  β”œβ”€β”€ fix_oeg/ # FIX 4.4 Order Entry Gateway
411
  β”œβ”€β”€ fix-ui-client/ # FIX browser UI
412
+ β”œβ”€β”€ frontend/ # Simple order entry form
413
  β”œβ”€β”€ shared/ # Shared config + Kafka utils
414
  β”œβ”€β”€ shared_data/ # securities.txt (symbol list + prices)
415
+ β”œβ”€β”€ notebooks/ # Fine-tuning notebooks
416
  β”œβ”€β”€ Dockerfile # HuggingFace / single-container build
417
  β”œβ”€β”€ docker-compose.yml # Local multi-container dev setup
418
  β”œβ”€β”€ entrypoint.sh # Container startup (Kafka β†’ services β†’ nginx)