-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrain_runtime.py
More file actions
1480 lines (1331 loc) · 60.2 KB
/
Copy pathbrain_runtime.py
File metadata and controls
1480 lines (1331 loc) · 60.2 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import annotations
from dataclasses import asdict, dataclass
import hashlib
import json
import threading
import time
from typing import Any, Literal
from urllib import error as urlerror, request as urlrequest
from llm_roles import build_role_context, default_role_team_response, evaluate_role_team, llm_provider_enabled
BrainDecisionValue = Literal["LONG", "SHORT", "WAIT", "BLOCKED"]
BrainSignal = Literal["LONG", "SHORT", "NEUTRAL"]
# ==================================================
# brain_runtime.py
# ==================================================
# AGENT BRAIN / LEARNING DECISION RUNTIME
# ==================================================
@dataclass(frozen=True)
class BrainReport:
agent_name: str
function: str
signal: BrainSignal
score: int
reads: str
message: str
conflict: bool = False
blocking: bool = False
details: dict[str, Any] | None = None
def to_dict(self) -> dict[str, Any]:
return asdict(self)
@dataclass(frozen=True)
class BrainMemoryMatch:
pattern_key: str
count: int
win_rate: float | None
avg_r: float | None
entry_offset_in_box: float | None
def to_dict(self) -> dict[str, Any]:
return asdict(self)
@dataclass(frozen=True)
class BrainTradeCandidate:
symbol: str
decision: Literal["LONG", "SHORT"]
side: Literal["long", "short"]
entry_price: float
sl_price: float
tp_price: float
entry_zone_low: float
entry_zone_high: float
entry_method: str
target_method: str
signal_candle_time: int
confirmation_candle_time: int
pattern_key: str
confidence: float
score: int
reason: str
features: dict[str, Any]
def to_dict(self) -> dict[str, Any]:
return asdict(self)
# --------------------------------------------------
# LLM ASYNC WORKER STATE
# --------------------------------------------------
_LLM_AUDIT_LOCK = threading.Lock()
_LLM_AUDIT_STATE: dict[str, Any] = {
"running": False,
"active_key": None,
"active_hash": None,
"started_at": None,
"last_hash_by_key": {},
"last_result_by_key": {},
}
# --------------------------------------------------
# VALUE ACCESS
# --------------------------------------------------
def _value(candle: Any, key: str, default: float = 0.0) -> float:
try:
if isinstance(candle, dict):
return float(candle.get(key, default))
return float(getattr(candle, key, default))
except (TypeError, ValueError):
return float(default)
def _timestamp(candle: Any, default: int = 0) -> int:
try:
if isinstance(candle, dict):
return int(candle.get("timestamp", default))
return int(getattr(candle, "timestamp", default))
except (TypeError, ValueError):
return int(default)
def _last(candles: list[Any]) -> Any | None:
return candles[-1] if candles else None
def _safe_float(value: Any, fallback: float = 0.0) -> float:
try:
return float(value)
except (TypeError, ValueError):
return float(fallback)
def _round_price(value: float) -> float:
return round(float(value), 8)
# --------------------------------------------------
# AGENT SCOREBOARD
# --------------------------------------------------
def _agent_weight(name: str) -> float:
text = name.lower()
if "bos" in text or "choch" in text:
return 1.25
if "box" in text:
return 1.25
if "hh" in text or "swing" in text:
return 1.0
if "triple" in text:
return 0.9
if "macd" in text:
return 0.85
if "hma" in text:
return 0.8
if "sma" in text:
return 0.8
if "mfi" in text:
return 0.75
if "rsi" in text:
return 0.8
if "vwap" in text:
return 0.85
if "breakout" in text or "fakeout" in text:
return 1.05
if "volume" in text:
return 0.75
if "volatility" in text:
return 0.55
if "risk" in text:
return 0.5
return 1.0
def _report_quality_weight(report: dict[str, Any]) -> float:
details = report.get("details") if isinstance(report.get("details"), dict) else {}
profile = details.get("quality_profile") if isinstance(details.get("quality_profile"), dict) else {}
quality = str(profile.get("quality", "OK")).upper()
reliability = max(0.0, min(100.0, _safe_float(profile.get("reliability_score"), report.get("score", 50.0))))
if bool(report.get("blocking", False)) or quality == "BLOCK":
return 0.0
if quality == "OFFLINE":
return 0.0
if quality == "WEAK":
return max(0.35, reliability / 140.0)
if quality == "STRONG":
return 1.12
return max(0.65, min(1.05, reliability / 75.0))
def _direction_scores(agent_board: dict[str, Any]) -> dict[str, Any]:
reports = agent_board.get("reports") or []
long_score = 0.0
short_score = 0.0
long_count = 0
short_count = 0
neutral_count = 0
long_weight_sum = 0.0
short_weight_sum = 0.0
weak_count = 0
offline_count = 0
blocking_count = 0
parts: list[str] = []
for report in reports:
signal = str(report.get("signal", "NEUTRAL")).upper()
score = max(0.0, min(100.0, _safe_float(report.get("score"), 0.0)))
details = report.get("details") if isinstance(report.get("details"), dict) else {}
profile = details.get("quality_profile") if isinstance(details.get("quality_profile"), dict) else {}
quality = str(profile.get("quality", "OK")).upper()
if quality == "WEAK":
weak_count += 1
if quality == "OFFLINE":
offline_count += 1
if bool(report.get("blocking", False)):
blocking_count += 1
weight = _agent_weight(str(report.get("agent_name", ""))) * _report_quality_weight(report)
adjusted = score * weight
if signal == "LONG" and adjusted > 0:
long_score += adjusted
long_weight_sum += weight
long_count += 1
elif signal == "SHORT" and adjusted > 0:
short_score += adjusted
short_weight_sum += weight
short_count += 1
else:
neutral_count += 1
parts.append(f"{report.get('agent_name', '-')}: {signal} {int(score)} q={quality} w={round(weight, 2)}")
return {
"long_score": round(long_score, 3),
"short_score": round(short_score, 3),
"long_weight_sum": round(long_weight_sum, 6),
"short_weight_sum": round(short_weight_sum, 6),
"long_count": long_count,
"short_count": short_count,
"neutral_count": neutral_count,
"long_weight_sum": round(long_weight_sum, 6),
"short_weight_sum": round(short_weight_sum, 6),
"weak_count": weak_count,
"offline_count": offline_count,
"blocking_count": blocking_count,
"data_quality_penalty": min(12, weak_count * 2),
"reads": " | ".join(parts),
"conflict": long_count > 0 and short_count > 0,
}
def _pattern_key(agent_board: dict[str, Any]) -> str:
items: list[str] = []
for report in agent_board.get("reports") or []:
name = str(report.get("agent_name", "agent")).lower()
short_name = (
name.replace(" agent", "")
.replace(" / ", "_")
.replace(" ", "_")
.replace("_/_", "_")
)
signal = str(report.get("signal", "NEUTRAL")).upper()
score_bucket = int(_safe_float(report.get("score"), 0.0) // 10 * 10)
items.append(f"{short_name}={signal}:{score_bucket}")
return "|".join(items)
# --------------------------------------------------
# MEMORY MATCHING
# --------------------------------------------------
def _memory_match(memory_state: dict[str, Any] | None, pattern_key: str) -> BrainMemoryMatch:
if not memory_state:
return BrainMemoryMatch(pattern_key, 0, None, None, None)
records = memory_state.get("completed_trades", []) or []
matches: list[dict[str, Any]] = []
offsets: list[float] = []
for record in records:
source = str(record.get("source") or "paper_trade_close").lower()
if "replay" in source:
continue
setup = record.get("setup", {}) if isinstance(record, dict) else {}
features = setup.get("features", {}) if isinstance(setup, dict) else {}
strategy = str(features.get("strategy") or "").lower()
if "replay" in strategy:
continue
if features.get("brain_pattern_key") != pattern_key:
continue
matches.append(record)
offset = features.get("brain_entry_offset_in_box")
if offset is not None and _safe_float(record.get("result_r"), 0.0) > 0:
offsets.append(max(0.0, min(1.0, _safe_float(offset, 0.5))))
count = len(matches)
if count == 0:
return BrainMemoryMatch(pattern_key, 0, None, None, None)
wins = sum(1 for item in matches if _safe_float(item.get("result_r"), 0.0) > 0)
sum_r = sum(_safe_float(item.get("result_r"), 0.0) for item in matches)
return BrainMemoryMatch(
pattern_key=pattern_key,
count=count,
win_rate=round(wins / count, 3),
avg_r=round(sum_r / count, 3),
entry_offset_in_box=round(sum(offsets) / len(offsets), 3) if offsets else None,
)
def _memory_score_adjustment(match: BrainMemoryMatch, min_count: int) -> int:
if match.count < max(1, min_count) or match.win_rate is None or match.avg_r is None:
return 0
win_component = int((match.win_rate - 0.5) * 30)
r_component = int(max(-1.0, min(1.5, match.avg_r)) * 12)
return max(-20, min(22, win_component + r_component))
# --------------------------------------------------
# REPLAY RULE WEIGHTING
# --------------------------------------------------
def _rule_pattern_matches(rule: dict[str, Any], pattern_key: str) -> bool:
rule_pattern = str(rule.get("pattern_key") or "")
rule_key = str(rule.get("key") or "")
if rule_pattern and rule_pattern == pattern_key:
return True
if rule_key == pattern_key:
return True
return f"pattern={pattern_key}" in rule_key
def _replay_rule_weight(pattern_key: str, config: dict[str, Any], symbol: str | None = None) -> dict[str, Any]:
# Replay is no longer part of the active decision path; keep the old helper inert for stored configs.
return {"enabled": False, "matched": False, "adjustment": 0, "quality": "OFF", "reason": "Replay entfernt: Live-Analyse nutzt keine Replay-Regeln"}
if not bool(config.get("replay_rule_weight_enabled", False)):
return {"enabled": False, "matched": False, "adjustment": 0, "quality": "OFF", "reason": "replay_rule_weight_enabled=false"}
rules = config.get("replay_rule_weight_rules", []) or []
if not isinstance(rules, list):
return {"enabled": True, "matched": False, "adjustment": 0, "quality": "INVALID", "reason": "replay_rule_weight_rules ist keine Liste"}
active_symbol = str(symbol or "").replace(".P", "").split(":", 1)[0].strip().upper()
default_scope = str(config.get("replay_rule_scope", "asset")).lower()
min_count = int(config.get("replay_rule_weight_min_count", 2))
good_bonus = int(config.get("replay_rule_good_bonus", 8))
bad_penalty = int(config.get("replay_rule_bad_penalty", -14))
max_abs = max(0, int(config.get("replay_rule_max_abs_adjustment", 18)))
skipped_asset = 0
for rule in rules:
if not isinstance(rule, dict):
continue
scope = str(rule.get("scope") or default_scope or "asset").lower()
rule_symbol = str(rule.get("symbol") or "").replace(".P", "").split(":", 1)[0].strip().upper()
if scope == "asset" and active_symbol and rule_symbol and rule_symbol != active_symbol:
skipped_asset += 1
continue
if not _rule_pattern_matches(rule, pattern_key):
continue
count = int(rule.get("count") or 0)
if count < min_count:
return {"enabled": True, "matched": True, "adjustment": 0, "quality": str(rule.get("quality", "WATCH")), "count": count, "symbol": rule_symbol, "scope": scope, "key": rule.get("key", pattern_key), "reason": "Replay-Regel unter Mindestanzahl"}
quality = str(rule.get("quality", "WATCH")).upper()
if quality == "GOOD":
adjustment = good_bonus
elif quality == "BAD":
adjustment = bad_penalty
else:
adjustment = 0
adjustment = max(-max_abs, min(max_abs, int(adjustment)))
return {
"enabled": True,
"matched": True,
"adjustment": adjustment,
"quality": quality,
"count": count,
"symbol": rule_symbol,
"scope": scope,
"key": rule.get("key", pattern_key),
"pattern_key": rule.get("pattern_key", pattern_key),
"win_rate": rule.get("win_rate"),
"avg_r": rule.get("avg_r"),
"reason": "Asset-gebundene Replay-Regel angewendet" if scope == "asset" and adjustment else "Replay-Regelgewicht angewendet" if adjustment else "Replay-Regel nur Watch/Neutral",
}
reason = "Keine passende Replay-Regel"
if skipped_asset:
reason = f"Keine passende Replay-Regel für {active_symbol}; {skipped_asset} Regel(n) anderes Asset"
return {"enabled": True, "matched": False, "adjustment": 0, "quality": "NO_MATCH", "symbol": active_symbol, "scope": default_scope, "reason": reason}
# --------------------------------------------------
# STRUCTURE SELECTION
# --------------------------------------------------
def _boxes_for_direction(indicator_data: dict[str, Any], decision: str) -> list[dict[str, Any]]:
wanted = "rising" if decision == "LONG" else "falling"
boxes = [box for box in indicator_data.get("boxes", []) if str(box.get("direction")) == wanted]
boxes.sort(key=lambda box: int(box.get("start_timestamp", 0)))
return boxes
def _box_bounds(box: dict[str, Any]) -> tuple[float, float]:
top = _safe_float(box.get("top"), 0.0)
bottom = _safe_float(box.get("bottom"), 0.0)
return min(top, bottom), max(top, bottom)
def _entry_from_box(decision: str, box: dict[str, Any], match: BrainMemoryMatch, config: dict[str, Any]) -> tuple[float, float, float, float]:
low, high = _box_bounds(box)
width = max(high - low, 1e-12)
default_offset = _safe_float(config.get("brain_entry_box_offset", 0.35), 0.35)
memory_offset = match.entry_offset_in_box
offset = memory_offset if memory_offset is not None else default_offset
offset = max(0.1, min(0.9, offset))
if decision == "LONG":
entry = low + (width * offset)
else:
entry = high - (width * offset)
offset = (entry - low) / width
return _round_price(entry), _round_price(low), _round_price(high), round(offset, 4)
def _fallback_entry(candles: list[Any], decision: str) -> tuple[float, float, float, float]:
last = _last(candles)
if last is None:
return 0.0, 0.0, 0.0, 0.5
close = _value(last, "close")
high = _value(last, "high")
low = _value(last, "low")
if high <= low:
high = close * 1.0005
low = close * 0.9995
return _round_price(close), _round_price(low), _round_price(high), 0.5
def _target_from_labels(decision: str, entry: float, indicator_data: dict[str, Any]) -> tuple[float | None, str]:
labels = indicator_data.get("labels") or []
candidates: list[float] = []
for label in labels:
text = str(label.get("text", ""))
price = _safe_float(label.get("price"), 0.0)
if decision == "LONG" and text in ("HH", "LH") and price > entry:
candidates.append(price)
if decision == "SHORT" and text in ("HL", "LL") and 0 < price < entry:
candidates.append(price)
if decision == "LONG" and candidates:
return _round_price(min(candidates)), "nearest_swing_label_high"
if decision == "SHORT" and candidates:
return _round_price(max(candidates)), "nearest_swing_label_low"
return None, "missing_swing_label_target"
def _target_from_recent_range(decision: str, entry: float, candles: list[Any], config: dict[str, Any]) -> tuple[float | None, str]:
lookback = max(10, int(config.get("brain_target_lookback_candles", 36)))
window = candles[-min(len(candles), lookback):]
if not window:
return None, "missing_recent_range"
if decision == "LONG":
target = max(_value(candle, "high") for candle in window)
return (_round_price(target), "local_visible_high") if target > entry else (None, "local_high_not_above_entry")
target = min(_value(candle, "low") for candle in window)
return (_round_price(target), "local_visible_low") if 0 < target < entry else (None, "local_low_not_below_entry")
def _target_from_reward_risk(decision: str, entry: float, sl: float, rr: float) -> float | None:
risk = abs(entry - sl)
if risk <= 0 or rr <= 0:
return None
if decision == "LONG":
return _round_price(entry + risk * rr)
return _round_price(entry - risk * rr)
def _planned_quantity(symbol: str, entry: float, config: dict[str, Any]) -> float | None:
if entry <= 0:
return None
symbol_cfg = config.get("trade_sizes_by_symbol", {}) or {}
per_symbol = symbol_cfg.get(symbol) if isinstance(symbol_cfg, dict) else None
size_cfg = per_symbol if isinstance(per_symbol, dict) else config
mode = str(size_cfg.get("mode", config.get("trade_size_mode", "usd"))).lower()
if mode == "asset":
quantity = _safe_float(size_cfg.get("asset", config.get("trade_size_asset")), 0.0)
return quantity if quantity > 0 else None
notional = _safe_float(size_cfg.get("usd", config.get("trade_size_usd")), 0.0)
return (notional / entry) if notional > 0 else None
def _candidate_value_preview(
symbol: str,
entry: float,
sl: float,
tp: float,
config: dict[str, Any],
) -> dict[str, Any]:
quantity = _planned_quantity(symbol, entry, config)
if quantity is None or quantity <= 0:
return {"known": False, "reason": "missing_planned_quantity"}
reward = abs(tp - entry)
fee_rate = _safe_float(config.get("estimated_taker_fee_rate", 0.0006), 0.0006)
min_profit_by_symbol = config.get("min_net_profit_fraction_by_symbol", {}) or {}
symbol_min_profit = min_profit_by_symbol.get(symbol) if isinstance(min_profit_by_symbol, dict) else None
min_net_profit_fraction = _safe_float(
symbol_min_profit if symbol_min_profit is not None else config.get("min_net_profit_fraction", 0.001),
0.001,
)
entry_notional = entry * quantity
risk = abs(entry - sl)
risk_usd = risk * quantity
gross_profit = reward * quantity
estimated_fees = (entry * quantity + tp * quantity) * fee_rate
fee_to_risk_fraction = estimated_fees / risk_usd if risk_usd > 0 else None
max_fee_to_risk_fraction = _safe_float(config.get("max_fee_to_risk_fraction", 0.25), 0.25)
net_profit = gross_profit - estimated_fees
net_profit_fraction = net_profit / entry_notional if entry_notional > 0 else 0.0
fee_risk_blocked = (
max_fee_to_risk_fraction > 0
and fee_to_risk_fraction is not None
and fee_to_risk_fraction > max_fee_to_risk_fraction
)
net_profit_blocked = min_net_profit_fraction > 0 and net_profit_fraction < min_net_profit_fraction
reason = "net_profit_ok"
if fee_risk_blocked:
reason = "fee_to_risk_too_high"
elif net_profit_blocked:
reason = "net_profit_too_small"
return {
"known": True,
"planned_quantity_asset": round(quantity, 8),
"entry_notional_usd": round(entry_notional, 8),
"risk_usd": round(risk_usd, 8),
"gross_profit_usd": round(gross_profit, 8),
"estimated_fees_usd": round(estimated_fees, 8),
"fee_to_risk_fraction": round(fee_to_risk_fraction, 8) if fee_to_risk_fraction is not None else None,
"max_fee_to_risk_fraction": max_fee_to_risk_fraction,
"net_profit_usd": round(net_profit, 8),
"net_profit_fraction": round(net_profit_fraction, 8),
"min_net_profit_fraction": min_net_profit_fraction,
"trade_allowed": not (fee_risk_blocked or net_profit_blocked),
"reason": reason,
}
def _ceo_trade_gate(agent_board: dict[str, Any], signal: str, config: dict[str, Any]) -> dict[str, Any]:
if not bool(config.get("brain_require_ceo_quality_for_trade", True)):
return {"allowed": True, "reason": "brain_require_ceo_quality_for_trade=false"}
ceo = agent_board.get("ceo") if isinstance(agent_board.get("ceo"), dict) else {}
if not ceo:
return {"allowed": False, "reason": "CEO-Report fehlt."}
ceo_signal = str(ceo.get("signal", "NEUTRAL")).upper()
details = ceo.get("details") if isinstance(ceo.get("details"), dict) else {}
decision = str(details.get("decision", "")).upper()
grade = str(details.get("decision_grade", "")).upper()
final_quality = _safe_float(details.get("final_quality_score", ceo.get("score", 0)), 0.0)
role_alignment = _safe_float(details.get("role_alignment_score", details.get("quality_score", 0)), 0.0)
min_quality = _safe_float(config.get("brain_ceo_min_final_quality", 55), 55)
min_alignment = _safe_float(config.get("brain_ceo_min_alignment_score", 45), 45)
wanted_decision = "LONG_BIAS" if signal == "LONG" else "SHORT_BIAS" if signal == "SHORT" else ""
allowed = (
signal in ("LONG", "SHORT")
and ceo_signal == signal
and (not decision or decision == wanted_decision)
and (not grade or grade in {"A", "B"})
and final_quality >= min_quality
and role_alignment >= min_alignment
and not bool(ceo.get("blocking", False))
)
if allowed:
reason = "CEO bestätigt Richtung und Qualität."
elif ceo_signal != signal:
reason = f"CEO bestätigt {signal} nicht; CEO-Signal {ceo_signal}."
elif grade and grade not in {"A", "B"}:
reason = f"CEO-Qualitätsgrad {grade} ist für Trade-Plan zu schwach."
elif final_quality < min_quality:
reason = f"CEO-Finalqualität {round(final_quality, 2)} unter Minimum {round(min_quality, 2)}."
elif role_alignment < min_alignment:
reason = f"CEO-Rollen-Ausrichtung {round(role_alignment, 2)} unter Minimum {round(min_alignment, 2)}."
else:
reason = "CEO gibt keine saubere Trade-Freigabe."
return {
"allowed": allowed,
"reason": reason,
"ceo_signal": ceo_signal,
"ceo_decision": decision,
"decision_grade": grade,
"final_quality_score": final_quality,
"role_alignment_score": role_alignment,
"min_final_quality": min_quality,
"min_alignment_score": min_alignment,
}
def _apply_timeframe_target_cap(
decision: str,
entry: float,
sl: float,
tp: float,
target_method: str,
config: dict[str, Any],
) -> tuple[float, str]:
if not bool(config.get("brain_cap_target_to_max_rr", True)):
return _round_price(tp), target_method
risk = abs(entry - sl)
reward = abs(tp - entry)
if risk <= 0 or reward <= 0:
return _round_price(tp), target_method
configured_rr = _safe_float(config.get("reward_risk", 1.5), 1.5)
max_rr = _safe_float(config.get("brain_max_target_rr", configured_rr), configured_rr)
if max_rr <= 0:
return _round_price(tp), target_method
target_rr = reward / risk
if target_rr <= max_rr:
return _round_price(tp), target_method
capped_tp = _target_from_reward_risk(decision, entry, sl, max_rr)
if capped_tp is None:
return _round_price(tp), target_method
return capped_tp, f"{target_method}_capped_to_{round(max_rr, 3)}r"
def _true_range(candle: Any, previous_close: float | None) -> float:
high = _value(candle, "high")
low = _value(candle, "low")
if previous_close is None:
return max(high - low, 0.0)
return max(high - low, abs(high - previous_close), abs(low - previous_close), 0.0)
def _atr_value(candles: list[Any], period: int) -> float | None:
if not candles:
return None
safe_period = max(1, int(period))
previous_close: float | None = None
ranges: list[float] = []
for candle in candles:
ranges.append(_true_range(candle, previous_close))
previous_close = _value(candle, "close")
window = ranges[-min(len(ranges), safe_period):]
if not window:
return None
atr = sum(window) / len(window)
return atr if atr > 0 else None
def _structure_stop_price(decision: str, entry: float, zone_low: float, zone_high: float, candles: list[Any], config: dict[str, Any]) -> float:
lookback = max(2, int(config.get("brain_stop_lookback_candles", 8)))
window = candles[-min(len(candles), lookback):]
buffer_fraction = _safe_float(config.get("stop_loss_buffer_percent", 0.0), 0.0) / 100.0
buffer = entry * buffer_fraction
if decision == "LONG":
recent_low = min((_value(candle, "low") for candle in window), default=zone_low)
return _round_price(min(zone_low, recent_low) - buffer)
recent_high = max((_value(candle, "high") for candle in window), default=zone_high)
return _round_price(max(zone_high, recent_high) + buffer)
def _atr_stop_price(decision: str, entry: float, candles: list[Any], config: dict[str, Any]) -> float | None:
period = max(1, int(config.get("stop_loss_atr_period", 14)))
multiplier = max(0.0, _safe_float(config.get("stop_loss_atr_multiplier", 1.5), 1.5))
atr = _atr_value(candles, period)
if atr is None or multiplier <= 0:
return None
distance = atr * multiplier
if decision == "LONG":
return _round_price(entry - distance)
return _round_price(entry + distance)
def _stop_price(decision: str, entry: float, zone_low: float, zone_high: float, candles: list[Any], config: dict[str, Any]) -> tuple[float, str]:
mode = str(config.get("stop_loss_mode", "structure")).lower()
if mode == "atr":
atr_stop = _atr_stop_price(decision, entry, candles, config)
if atr_stop is not None:
return atr_stop, "atr_stop"
return _structure_stop_price(decision, entry, zone_low, zone_high, candles, config), "structure_stop_atr_fallback"
if mode == "fixed_percent":
stop_fraction = _safe_float(config.get("stop_loss_percent", 0.25), 0.25) / 100.0
if stop_fraction > 0:
if decision == "LONG":
return _round_price(entry * (1.0 - stop_fraction)), "fixed_percent_stop"
return _round_price(entry * (1.0 + stop_fraction)), "fixed_percent_stop"
return _structure_stop_price(decision, entry, zone_low, zone_high, candles, config), "structure_stop"
# --------------------------------------------------
# LLM AUDIT LAYER
# --------------------------------------------------
def _llm_audit_response(
enabled: bool,
provider: str,
model: str,
verdict: str,
message: str,
confidence_note: str = "-",
risk_note: str = "-",
conflict_note: str = "-",
advice: str = "-",
block_hint: bool = False,
) -> dict[str, Any]:
return {
"enabled": enabled,
"provider": provider,
"model": model,
"role": "local_trade_auditor",
"verdict": verdict,
"confidence_note": confidence_note,
"risk_note": risk_note,
"conflict_note": conflict_note,
"advice": advice,
"block_hint": block_hint,
"message": message,
}
def _llm_audit_context(
agent_board: dict[str, Any],
scan: dict[str, Any],
match: BrainMemoryMatch,
decision: str | None,
candidate: dict[str, Any] | None,
economic_gate: dict[str, Any] | None,
) -> dict[str, Any]:
reports = []
for report in agent_board.get("reports", [])[:12]:
reports.append(
{
"agent_name": str(report.get("agent_name", "-")),
"signal": str(report.get("signal", "NEUTRAL")),
"score": int(_safe_float(report.get("score"), 0.0)),
"conflict": bool(report.get("conflict", False)),
"blocking": bool(report.get("blocking", False)),
}
)
scores = _direction_scores(agent_board)
gate = economic_gate or {}
return {
"symbol": str(agent_board.get("symbol", scan.get("symbol", "-"))),
"timeframe_seconds": int(_safe_float(agent_board.get("timeframe_seconds", 0), 0.0)),
"decision": str(decision or "WAIT"),
"agent_scores": scores,
"agent_reports": reports,
"memory_match": {
"count": match.count,
"win_rate": match.win_rate,
"avg_r": match.avg_r,
},
"candidate_present": candidate is not None,
"economic_gate": {
"present": bool(economic_gate),
"trade_allowed": bool(gate.get("trade_allowed", False)) if economic_gate else None,
"reason": str(gate.get("reason", "")) if economic_gate else "",
},
"scan": {
"setup_found": bool(scan.get("setup_found", False)),
"side": str(scan.get("side", "")),
"reason": str(scan.get("reason", "")),
},
}
def _llm_system_prompt() -> str:
return (
"Du bist ein lokaler Trade-Auditor für ein Paper-Trading-System. "
"Antworte immer auf Deutsch. "
"Verwende keine chinesischen, japanischen oder koreanischen Schriftzeichen. "
"Antworte nur mit einem einzelnen JSON-Objekt ohne Markdown, ohne Codeblock und ohne Zusatztext. "
"Du darfst keine Entry-, Stop-Loss-, Take-Profit- oder Positionsgrößen setzen. "
"Du darfst das Economic Gate nicht umgehen."
)
def _llm_prompt(context: dict[str, Any], config: dict[str, Any]) -> str:
max_chars = max(500, int(_safe_float(config.get("ollama_max_prompt_chars", 4000), 4000.0)))
payload = json.dumps(context, ensure_ascii=False, separators=(",", ":"))[:max_chars]
return (
"Erzeuge ein deutsches Audit für den folgenden reduzierten Bot-Kontext. "
"Pflichtformat: JSON mit exakt diesen Schluesseln: "
"verdict, confidence_note, risk_note, conflict_note, advice, block_hint. "
"Erlaubte verdict Werte: OK, WARN, BLOCK_HINT, NO_DATA, ERROR. "
"Alle Textfelder muessen kurz, sachlich und deutsch sein. "
"Wenn keine passende Aussage möglich ist, nutze '-' als Textwert. "
"Beispiel: {\"verdict\":\"WARN\",\"confidence_note\":\"-\",\"risk_note\":\"Risiko prüfen.\",\"conflict_note\":\"-\",\"advice\":\"Abwarten.\",\"block_hint\":false}. "
f"Audit-Kontext: {payload}"
)
def _ollama_generate(prompt: str, config: dict[str, Any]) -> str:
base_url = str(config.get("ollama_base_url", "http://127.0.0.1:11434")).rstrip("/")
model = str(config.get("ollama_model", "qwen2.5:3b"))
timeout = max(1.0, min(300.0, _safe_float(config.get("ollama_timeout_seconds", 60), 60.0)))
temperature = max(0.0, min(1.0, _safe_float(config.get("ollama_temperature", 0.0), 0.0)))
payload = {
"model": model,
"system": _llm_system_prompt(),
"prompt": prompt,
"stream": False,
"format": "json",
"options": {"temperature": temperature},
}
request = urlrequest.Request(
f"{base_url}/api/generate",
data=json.dumps(payload).encode("utf-8"),
headers={"Content-Type": "application/json"},
method="POST",
)
with urlrequest.urlopen(request, timeout=timeout) as response:
raw = response.read().decode("utf-8")
data = json.loads(raw)
return str(data.get("response", ""))
def _contains_disallowed_llm_script(value: Any) -> bool:
text = str(value or "")
return any(
("\u3040" <= char <= "\u30ff")
or ("\u3400" <= char <= "\u4dbf")
or ("\u4e00" <= char <= "\u9fff")
or ("\uf900" <= char <= "\ufaff")
or ("\uac00" <= char <= "\ud7af")
for char in text
)
def _clean_llm_text_value(value: Any, fallback: str = "-", max_chars: int = 240) -> str:
if value is None or isinstance(value, (dict, list, tuple, set)):
return fallback
text = str(value).replace("\r", " ").replace("\n", " ").strip()
text = " ".join(text.split())
text = text.strip(" `\"',;")
if not text or text in {"-", "--", "---", "–", "—", "null", "None", "none"}:
return fallback
if not any(char.isalnum() for char in text):
return fallback
if _contains_disallowed_llm_script(text):
return fallback
return text[:max_chars]
def _llm_text_field(parsed: dict[str, Any], key: str, fallback: str = "-") -> str:
return _clean_llm_text_value(parsed.get(key, fallback), fallback=fallback)
def _parse_llm_audit(raw_text: str, config: dict[str, Any]) -> dict[str, Any]:
provider = "ollama"
model = str(config.get("ollama_model", "qwen2.5:3b"))
allowed_verdicts = {"OK", "WARN", "BLOCK_HINT", "NO_DATA", "ERROR"}
try:
parsed = json.loads(raw_text)
except (TypeError, json.JSONDecodeError):
return _llm_audit_response(
True,
provider,
model,
"WARN",
"Ollama-Antwort war kein valides JSON; Pipeline läuft deterministisch weiter.",
advice=_clean_llm_text_value(raw_text),
)
if not isinstance(parsed, dict):
return _llm_audit_response(
True,
provider,
model,
"WARN",
"Ollama-Antwort war kein JSON-Objekt; Pipeline läuft deterministisch weiter.",
advice="Ollama muss ein einzelnes JSON-Objekt liefern.",
)
checked_fields = ["confidence_note", "risk_note", "conflict_note", "advice"]
if any(_contains_disallowed_llm_script(parsed.get(field, "")) for field in checked_fields):
return _llm_audit_response(
True,
provider,
model,
"ERROR",
"Ollama-Antwort verworfen: Antwortsprache ist nicht Deutsch; Pipeline läuft deterministisch weiter.",
risk_note="Ollama-Antwort enthielt nicht erlaubte Schriftzeichen und wurde nicht angezeigt.",
conflict_note="-",
advice="Modell erneut laufen lassen oder Prompt/Modell prüfen.",
block_hint=False,
)
verdict = str(parsed.get("verdict", "WARN")).upper()
if verdict not in allowed_verdicts:
verdict = "WARN"
block_hint = bool(parsed.get("block_hint", False)) and bool(config.get("ollama_block_hint_enabled", False))
return _llm_audit_response(
True,
provider,
model,
verdict,
"Ollama-Audit abgeschlossen; Ergebnis ist nur Hinweis, keine Trade-Freigabe.",
confidence_note=_llm_text_field(parsed, "confidence_note"),
risk_note=_llm_text_field(parsed, "risk_note"),
conflict_note=_llm_text_field(parsed, "conflict_note"),
advice=_llm_text_field(parsed, "advice"),
block_hint=block_hint,
)
def _llm_audit_key(context: dict[str, Any]) -> str:
return str(context.get("symbol") or "global")
def _llm_audit_hash(context: dict[str, Any]) -> str:
payload = json.dumps(context, ensure_ascii=False, sort_keys=True, separators=(",", ":"))
return hashlib.sha256(payload.encode("utf-8")).hexdigest()
def _llm_async_status_response(config: dict[str, Any], message: str, risk_note: str, advice: str) -> dict[str, Any]:
result = default_role_team_response(
config,
"NO_DATA",
message,
enabled=True,
)
result["risk_note"] = risk_note
result["advice"] = advice
return result
def _llm_hard_timeout_seconds(config: dict[str, Any]) -> float:
provider = str(config.get("llm_provider", "ollama")).lower()
if provider == "openai":
return max(1.0, min(120.0, _safe_float(config.get("openai_timeout_seconds", config.get("llm_role_timeout_seconds", 30)), 30.0)))
return max(1.0, min(300.0, _safe_float(config.get("ollama_timeout_seconds", config.get("llm_role_timeout_seconds", 120)), 120.0)))
def _llm_soft_timeout_seconds(config: dict[str, Any]) -> float:
hard = _llm_hard_timeout_seconds(config)
return max(10.0, min(60.0, hard * 0.5))
def _llm_running_response(config: dict[str, Any], elapsed: float, has_previous_result: bool) -> dict[str, Any]:
soft = _llm_soft_timeout_seconds(config)
hard = _llm_hard_timeout_seconds(config)
duration = max(0.001, round(elapsed, 3))
if elapsed >= soft:
message = f"LLM-Rollenteam laeuft noch seit {duration:.1f}s; Soft-Timeout {soft:.0f}s ueberschritten. Es wird weiter auf das Modell gewartet."
risk_note = "LLM ist langsam, aber nicht hart abgebrochen; Trading-Pipeline laeuft deterministisch weiter."
advice = f"Warten auf Modellantwort bis maximal {hard:.0f}s Hard-Timeout."
else:
message = (
"LLM-Rollenteam verarbeitet noch den vorherigen Kontext; neuer Kontext wird erst nach Abschluss im naechsten Tick uebergeben."
if has_previous_result
else "LLM-Rollenteam verarbeitet den ersten Kontext im Hintergrund."
)
risk_note = "LLM-Worker ist beschaeftigt; Trading-Pipeline laeuft deterministisch weiter."
advice = f"Warten auf Modellantwort. Soft-Warnung ab {soft:.0f}s, Hard-Abbruch bei {hard:.0f}s."
result = _llm_async_status_response(config, message, risk_note, advice)
result["verdict"] = "RUNNING"
result["duration_seconds"] = duration
result["role_duration_seconds"] = 0
result["judge_duration_seconds"] = 0
result["soft_timeout_seconds"] = soft
result["hard_timeout_seconds"] = hard
result["timing"] = {
"total_seconds": duration,
"roles_seconds": 0,
"judge_seconds": 0,
"role_count": 0,
"soft_timeout_seconds": soft,
"hard_timeout_seconds": hard,
}
return result
def _run_ollama_audit_worker(context_key: str, context_hash: str, context: dict[str, Any], config: dict[str, Any]) -> None:
started = time.perf_counter()
try:
result = evaluate_role_team(context, config)
except (OSError, TimeoutError, ValueError, RuntimeError, json.JSONDecodeError, urlerror.URLError) as exc:
detail = str(exc).strip() or type(exc).__name__
provider = str(config.get("llm_provider", "ollama")).lower()
provider_label = "Ollama" if provider == "ollama" else "OpenAI"
result = default_role_team_response(
config,
"ERROR",
f"{provider_label}/LLM-Rollenteam fehlgeschlagen: {detail}; Pipeline laeuft deterministisch weiter.",
enabled=True,
)
result["risk_note"] = detail
result["duration_seconds"] = max(0.001, round(time.perf_counter() - started, 3))
result["role_duration_seconds"] = 0
result["judge_duration_seconds"] = 0
result["timing"] = {
"total_seconds": result["duration_seconds"],
"roles_seconds": 0,
"judge_seconds": 0,
"role_count": 0,
}
result["advice"] = (
"Ollama Dienst, Modell und Timeout pruefen."
if provider == "ollama"
else "OpenAI API Connection testen und Billing/Guthaben, Key, Projekt und Modell pruefen."
)
with _LLM_AUDIT_LOCK:
_LLM_AUDIT_STATE["running"] = False
_LLM_AUDIT_STATE["active_key"] = None
_LLM_AUDIT_STATE["active_hash"] = None
_LLM_AUDIT_STATE["started_at"] = None
_LLM_AUDIT_STATE.setdefault("last_result_by_key", {})[context_key] = result
if result.get("verdict") != "ERROR":
_LLM_AUDIT_STATE.setdefault("last_hash_by_key", {})[context_key] = context_hash
def _submit_llm_audit_async(context: dict[str, Any], config: dict[str, Any]) -> dict[str, Any]:
context_key = _llm_audit_key(context)
context_hash = _llm_audit_hash(context)
with _LLM_AUDIT_LOCK:
last_hash = (_LLM_AUDIT_STATE.get("last_hash_by_key") or {}).get(context_key)
last_result = (_LLM_AUDIT_STATE.get("last_result_by_key") or {}).get(context_key)
if _LLM_AUDIT_STATE.get("running"):
started_at = _LLM_AUDIT_STATE.get("started_at")