Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 32 additions & 27 deletions application/cycle_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from quant_platform_kit.common.runtime_reports import persist_runtime_report
from quant_platform_kit.strategy_lifecycle.performance_monitor import try_record_platform_execution
from decision_mapper import is_execution_authority_valid
from runtime_logging import RuntimeLogContext, emit_runtime_log
from runtime_support import finalize_notification_delivery

Expand All @@ -20,7 +21,8 @@ def execute_strategy_cycle(
load_cycle_state,
append_trend_pool_source_logs,
capture_market_snapshot,
compute_portfolio_allocation,
execute_bnb_fuel_top_up,
resolve_strategy_plan,
build_balance_snapshot,
maybe_reset_daily_state,
maybe_rebase_daily_state_for_balance_change,
Expand Down Expand Up @@ -77,22 +79,38 @@ def execute_strategy_cycle(
btc_snapshot = market_snapshot["btc_snapshot"]
trend_indicators = market_snapshot["trend_indicators"]

allocation = compute_portfolio_allocation(
strategy_plan = resolve_strategy_plan(
runtime,
state,
runtime_trend_universe,
balances,
trend_indicators,
btc_snapshot,
prices,
balances,
u_total,
fuel_val,
state,
trend_indicators,
btc_snapshot,
allow_new_trend_entries=allow_new_trend_entries,
allow_pool_refresh=not trend_pool_resolution["degraded"],
)
allocation = strategy_plan["allocation"]
execution_authority = strategy_plan.get("execution_authority")
total_equity = allocation["total_equity"]
trend_val_equity = allocation["trend_val"]

report["total_equity_usdt"] = total_equity
report["trend_equity_usdt"] = trend_val_equity
if not is_execution_authority_valid(execution_authority):
report["status"] = "aborted"
report.setdefault("gating_summary", {})["missing_execution_authority"] = 1
return report

u_total, fuel_val = execute_bnb_fuel_top_up(
runtime,
report,
market_snapshot,
log_buffer,
execution_authority=execution_authority,
)

now_utc = runtime.now_utc
today_utc = now_utc.strftime("%Y-%m-%d")
Expand Down Expand Up @@ -127,6 +145,7 @@ def execute_strategy_cycle(
trend_daily_pnl,
circuit_breaker_pct,
log_buffer,
execution_authority=execution_authority,
):
return report

Expand All @@ -145,29 +164,14 @@ def execute_strategy_cycle(
today_id_str,
allow_new_trend_entries,
allow_pool_refresh=not trend_pool_resolution["degraded"],
strategy_plan=strategy_plan,
execution_authority=execution_authority,
)

post_trade_allocation = compute_portfolio_allocation(
runtime,
runtime_trend_universe,
balances,
prices,
u_total,
fuel_val,
state,
trend_indicators,
btc_snapshot,
)
total_equity = post_trade_allocation["total_equity"]
trend_val_equity = post_trade_allocation["trend_val"]

report["total_equity_usdt"] = total_equity
report["trend_equity_usdt"] = trend_val_equity

btc_target_ratio = post_trade_allocation["btc_target_ratio"]
dca_usdt_pool = post_trade_allocation["dca_usdt_pool"]
dca_val = post_trade_allocation["dca_val"]
btc_base_order_usdt = post_trade_allocation["btc_base_order_usdt"]
btc_target_ratio = allocation["btc_target_ratio"]
dca_usdt_pool = allocation["dca_usdt_pool"]
dca_val = allocation["dca_val"]
btc_base_order_usdt = allocation["btc_base_order_usdt"]
_, trend_daily_pnl = compute_daily_pnls(state, total_equity, trend_val_equity)

u_total = execute_btc_dca_cycle(
Expand All @@ -185,6 +189,7 @@ def execute_strategy_cycle(
btc_base_order_usdt,
today_id_str,
log_buffer,
execution_authority=execution_authority,
)

manage_usdt_earn_buffer_runtime(
Expand Down
18 changes: 18 additions & 0 deletions application/execution_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from __future__ import annotations

from decision_mapper import is_execution_authority_valid
from runtime_support import record_gating_event


Expand Down Expand Up @@ -106,6 +107,7 @@ def run_daily_circuit_breaker(
circuit_breaker_pct,
log_buffer,
*,
execution_authority=None,
format_qty_fn,
runtime_notify_fn,
ensure_asset_available_fn,
Expand All @@ -115,6 +117,8 @@ def run_daily_circuit_breaker(
build_balance_snapshot_fn,
translate_fn,
):
if not is_execution_authority_valid(execution_authority):
return False
if trend_daily_pnl > circuit_breaker_pct:
return False

Expand Down Expand Up @@ -199,6 +203,7 @@ def execute_trend_sells(
log_buffer,
today_id_str,
*,
execution_authority=None,
should_skip_duplicate_trend_action_fn,
append_log_fn,
translate_fn,
Expand All @@ -211,6 +216,8 @@ def execute_trend_sells(
runtime_set_trade_state_fn,
runtime_notify_fn,
):
if not is_execution_authority_valid(execution_authority):
return u_total
for symbol, config in runtime_trend_universe.items():
curr_price = prices[symbol]
sell_reason = str(sell_reasons.get(symbol, "")).strip()
Expand Down Expand Up @@ -296,6 +303,7 @@ def execute_trend_buys(
log_buffer,
today_id_str,
*,
execution_authority=None,
should_skip_duplicate_trend_action_fn,
append_log_fn,
translate_fn,
Expand All @@ -308,6 +316,8 @@ def execute_trend_buys(
runtime_set_trade_state_fn,
runtime_notify_fn,
):
if not is_execution_authority_valid(execution_authority):
return u_total
for symbol in eligible_buy_symbols:
curr_price = prices[symbol]
candidate_meta = selected_candidates[symbol]
Expand Down Expand Up @@ -421,13 +431,16 @@ def execute_trend_rotation(
allow_new_trend_entries,
allow_pool_refresh,
*,
execution_authority=None,
resolve_strategy_plan,
append_rotation_summary,
execute_trend_sells,
execute_trend_buys,
append_trend_symbol_status,
official_trend_pool_symbols,
):
if not is_execution_authority_valid(execution_authority):
return u_total
strategy_plan = resolve_strategy_plan(
state,
runtime_trend_universe,
Expand Down Expand Up @@ -481,6 +494,7 @@ def execute_trend_rotation(
u_total,
log_buffer,
today_id_str,
execution_authority=execution_authority,
)

post_sell_plan = resolve_strategy_plan(
Expand Down Expand Up @@ -520,6 +534,7 @@ def execute_trend_rotation(
u_total,
log_buffer,
today_id_str,
execution_authority=execution_authority,
)
append_trend_symbol_status(
log_buffer,
Expand Down Expand Up @@ -567,6 +582,7 @@ def execute_btc_dca_cycle(
today_id_str,
log_buffer,
*,
execution_authority=None,
append_log_fn,
translate_fn,
format_qty_fn,
Expand All @@ -576,6 +592,8 @@ def execute_btc_dca_cycle(
runtime_notify_fn,
runtime_set_trade_state_fn,
):
if not is_execution_authority_valid(execution_authority):
return u_total
if dca_usdt_pool <= 10 and dca_val <= 10:
record_gating_event(
report,
Expand Down
Loading