增加趋势回调

This commit is contained in:
dekun
2026-05-23 11:33:01 +08:00
parent fc8f9b70da
commit 4439bedcb7
12 changed files with 1419 additions and 194 deletions
+52 -2
View File
@@ -1954,6 +1954,7 @@ def insert_trade_record(
exchange_trade_id=None,
key_signal_type=None,
entry_reason=None,
trend_plan_id=None,
):
hold_minutes = calc_hold_minutes(hold_seconds)
open_ts = opened_at or app_now_str()
@@ -1964,12 +1965,13 @@ def insert_trade_record(
snap_sl = initial_stop_loss if initial_stop_loss not in (None, "") else stop_loss
er = (entry_reason or "").strip() or entry_reason_from_key_signal(kst) or ""
conn.execute(
"INSERT INTO trade_records (symbol,monitor_type,key_signal_type,direction,trigger_price,stop_loss,initial_stop_loss,take_profit,margin_capital,leverage,pnl_amount,hold_seconds,trade_style,risk_amount,planned_rr,actual_rr,hold_minutes,opened_at,opened_at_ms,closed_at,closed_at_ms,result,miss_reason,exchange_trade_id,entry_reason) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
"INSERT INTO trade_records (symbol,monitor_type,key_signal_type,direction,trigger_price,stop_loss,initial_stop_loss,take_profit,margin_capital,leverage,pnl_amount,hold_seconds,trade_style,risk_amount,planned_rr,actual_rr,hold_minutes,opened_at,opened_at_ms,closed_at,closed_at_ms,result,miss_reason,exchange_trade_id,entry_reason,trend_plan_id) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
(
symbol, monitor_type, kst, direction, trigger_price, snap_sl, snap_sl, take_profit,
margin_capital, leverage, pnl_amount, hold_seconds,
trade_style, risk_amount, planned_rr, actual_rr, hold_minutes,
open_ts, open_ts_ms, close_ts, close_ts_ms, result, miss_reason, exchange_trade_id, er or None
open_ts, open_ts_ms, close_ts, close_ts_ms, result, miss_reason, exchange_trade_id, er or None,
trend_plan_id,
)
)
@@ -2462,6 +2464,41 @@ def replace_active_monitor_tpsl_on_exchange(order_row, stop_loss, take_profit):
_okx_place_tp_sl_orders(ex_sym, direction, float(pos_amt), float(stop_loss), float(take_profit))
def _okx_place_stop_loss_only(exchange_symbol, direction, stop_loss):
"""OKX 永续:仅挂止损(趋势回调),止盈由程序监控。"""
ensure_markets_loaded()
pos_amt = get_live_position_contracts(exchange_symbol, direction)
if pos_amt is None or float(pos_amt) <= 0:
raise RuntimeError("交易所当前无持仓,无法挂止损")
cancel_okx_swap_open_orders(exchange_symbol)
close_side = "sell" if direction == "long" else "buy"
amt = float(exchange.amount_to_precision(exchange_symbol, float(pos_amt)))
params = build_okx_order_params(direction, reduce_only=True)
params["stopLoss"] = {
"triggerPrice": _okx_algo_trigger_price_str(exchange_symbol, stop_loss),
"type": "market",
}
exchange.create_order(exchange_symbol, "market", close_side, amt, None, params)
def calc_trend_manual_breakeven_stop(direction, entry_price, offset_pct=None):
try:
e = float(entry_price)
pct = float(
offset_pct
if offset_pct is not None
else float(os.getenv("TREND_PULLBACK_MANUAL_BREAKEVEN_OFFSET_PCT", "0.3"))
)
except (TypeError, ValueError):
return None
if e <= 0:
return None
direction = (direction or "long").strip().lower()
if direction == "short":
return e * (1.0 - pct / 100.0)
return e * (1.0 + pct / 100.0)
def extract_trade_price_from_order(order):
if not order:
return None
@@ -4069,6 +4106,11 @@ def background_task():
check_fib_key_monitors()
check_key_monitors()
check_order_monitors()
cfg = app.extensions.get("strategy_trend_cfg")
if cfg:
from strategy_trend_register import check_trend_pullback_plans
check_trend_pullback_plans(cfg)
except:
pass
time.sleep(MONITOR_POLL_SECONDS)
@@ -4199,6 +4241,12 @@ def render_main_page(page="trade"):
strategy_extra = strategy_page_template_vars(
conn, page, default_risk_percent=float(RISK_PERCENT)
)
if page == "strategy_trend":
cfg = app.extensions.get("strategy_trend_cfg")
if cfg:
from strategy_trend_register import load_trend_page_context
strategy_extra.update(load_trend_page_context(conn, request, cfg))
conn.close()
return render_template(
"index.html",
@@ -5971,8 +6019,10 @@ def strategy_roll_page():
from strategy_register import install_strategy_trading
from strategy_trend_register import install_strategy_trend
install_strategy_trading(app, _REPO_ROOT, app_module=sys.modules[__name__])
install_strategy_trend(app, _REPO_ROOT, app_module=sys.modules[__name__])
# 启动
+1 -1
View File
@@ -353,7 +353,7 @@
</div>
</div>
{% elif page == 'strategy_trend' %}
{% include 'strategy_trend_disabled_panel.html' %}
{% include 'strategy_trend_panel.html' %}
{% elif page == 'strategy_roll' %}
{% include 'strategy_roll_panel.html' %}
{% endif %}