Add real-time market truth layer
This commit is contained in:
@@ -11,6 +11,7 @@ if str(ROOT) not in sys.path:
|
||||
sys.path.insert(0, str(ROOT))
|
||||
|
||||
from src.services.execution import AccountRiskState, ExecutionService
|
||||
from src.services.market_state import MarketStateService, get_market_truth_supervisor
|
||||
|
||||
|
||||
def build_account_risk_state_from_env(env=None) -> AccountRiskState:
|
||||
@@ -31,7 +32,9 @@ def build_account_risk_state_from_env(env=None) -> AccountRiskState:
|
||||
def main(env=None) -> int:
|
||||
env = os.environ if env is None else env
|
||||
output_json = _get_bool(env, "EXECUTION_OUTPUT_JSON", False)
|
||||
service = ExecutionService()
|
||||
service = ExecutionService(
|
||||
market_state_service=MarketStateService(market_truth_supervisor=get_market_truth_supervisor())
|
||||
)
|
||||
signal_id = 1
|
||||
execution_mode = "paper"
|
||||
account_state = build_account_risk_state_from_env(env=env)
|
||||
|
||||
@@ -12,6 +12,7 @@ if str(ROOT) not in sys.path:
|
||||
|
||||
from scripts.run_execution import build_account_risk_state_from_env
|
||||
from src.services.execution import ExecutionService
|
||||
from src.services.market_state import MarketStateService, get_market_truth_supervisor
|
||||
|
||||
|
||||
def build_execution_mode_from_env(env=None) -> str:
|
||||
@@ -28,7 +29,9 @@ def main(env=None) -> int:
|
||||
risk_pct = _get_float(env, "EXECUTION_RISK_PCT", 0.01)
|
||||
confirm_ticket = _get_bool(env, "EXECUTION_CONFIRM_TICKET", False)
|
||||
confirmed_by = str(env.get("EXECUTION_CONFIRMED_BY") or "local_operator")
|
||||
service = ExecutionService()
|
||||
service = ExecutionService(
|
||||
market_state_service=MarketStateService(market_truth_supervisor=get_market_truth_supervisor())
|
||||
)
|
||||
account_state = build_account_risk_state_from_env(env=env)
|
||||
try:
|
||||
ticket = service.create_execution_ticket(
|
||||
|
||||
@@ -12,7 +12,11 @@ if str(ROOT) not in sys.path:
|
||||
sys.path.insert(0, str(ROOT))
|
||||
|
||||
from scripts.run_execution import build_account_risk_state_from_env
|
||||
from src.services.market_state import DEFAULT_MAX_CANDLE_AGE_SECONDS, MarketStateService
|
||||
from src.services.market_state import (
|
||||
DEFAULT_MAX_CANDLE_AGE_SECONDS,
|
||||
MarketStateService,
|
||||
get_market_truth_supervisor,
|
||||
)
|
||||
from src.services.risk import RiskContext, RiskGovernorService
|
||||
|
||||
|
||||
@@ -22,7 +26,7 @@ def main(env=None) -> int:
|
||||
instrument_id = _get_int(env, "RISK_INSTRUMENT_ID", 1)
|
||||
timeframe = str(env.get("RISK_TIMEFRAME") or "1m")
|
||||
execution_mode = str(env.get("RISK_EXECUTION_MODE") or "paper")
|
||||
service = MarketStateService()
|
||||
service = MarketStateService(market_truth_supervisor=get_market_truth_supervisor())
|
||||
try:
|
||||
market_state = service.build_market_state(
|
||||
instrument_id=instrument_id,
|
||||
|
||||
@@ -11,6 +11,7 @@ if str(ROOT) not in sys.path:
|
||||
sys.path.insert(0, str(ROOT))
|
||||
|
||||
from scripts.run_trading_plans import DEFAULT_TRADING_PLAN_SYMBOLS
|
||||
from src.api.trading_runtime import run_trading_watch
|
||||
from src.services.trade_plan_candidates import SetupWatcherService
|
||||
|
||||
|
||||
@@ -49,17 +50,16 @@ def main(env=None) -> int:
|
||||
if not instrument:
|
||||
candidates.append({"symbol": symbol, "passed": False, "error": "instrument_not_found"})
|
||||
continue
|
||||
candidate = service.build_candidate(
|
||||
candidate = run_trading_watch(
|
||||
instrument_id=int(instrument["id"]),
|
||||
setup_tf=timeframe_set["setup_tf"],
|
||||
trigger_tf=timeframe_set["trigger_tf"],
|
||||
)
|
||||
candidate_id = service.persist_candidate(candidate)
|
||||
candidates.append(
|
||||
build_candidate_result(
|
||||
symbol=symbol,
|
||||
instrument_id=int(instrument["id"]),
|
||||
candidate_id=candidate_id,
|
||||
candidate_id=int((candidate or {}).get("id") or 0),
|
||||
candidate=candidate,
|
||||
)
|
||||
)
|
||||
@@ -102,7 +102,18 @@ def build_candidate_result(symbol: str, instrument_id: int, candidate_id: int, c
|
||||
"candidate_id": 0,
|
||||
"candidate_rows_written": 0,
|
||||
}
|
||||
confirmation_state = dict(candidate.confirmation_state or {})
|
||||
if isinstance(candidate, dict) and candidate.get("candidate_generated") is False:
|
||||
return {
|
||||
"symbol": symbol,
|
||||
"instrument_id": instrument_id,
|
||||
"passed": True,
|
||||
"candidate_generated": False,
|
||||
"reason": candidate.get("status") or "market_truth_blocked",
|
||||
"reason_codes": list(candidate.get("reason_codes") or []),
|
||||
"candidate_id": 0,
|
||||
"candidate_rows_written": 0,
|
||||
}
|
||||
confirmation_state = dict((candidate or {}).get("confirmation_state") or {})
|
||||
return {
|
||||
"symbol": symbol,
|
||||
"instrument_id": instrument_id,
|
||||
@@ -110,11 +121,11 @@ def build_candidate_result(symbol: str, instrument_id: int, candidate_id: int, c
|
||||
"candidate_generated": True,
|
||||
"candidate_id": candidate_id,
|
||||
"candidate_rows_written": 1 if candidate_id else 0,
|
||||
"trading_plan_id": candidate.trading_plan_id,
|
||||
"model_code": candidate.model_code,
|
||||
"status": candidate.status,
|
||||
"trading_plan_id": candidate.get("trading_plan_id"),
|
||||
"model_code": candidate.get("model_code"),
|
||||
"status": candidate.get("status"),
|
||||
"executable": bool(confirmation_state.get("executable")),
|
||||
"missing_confirmations": list(candidate.missing_confirmations),
|
||||
"missing_confirmations": list(candidate.get("missing_conditions") or candidate.get("missing_confirmations") or []),
|
||||
"reason_codes": list(confirmation_state.get("reason_codes") or []),
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user