v1.2 strategy upgrade: small-capital fee-aware signals, two-model pipeline, auto deep-research escalation
This commit is contained in:
35
bot.py
35
bot.py
@@ -4,7 +4,16 @@ import json
|
||||
from sqlalchemy import func
|
||||
from config import settings
|
||||
from db import SessionLocal, BotDecision, TradeExecution, CuratedInsight
|
||||
from services import searx_news, ollama_decide, place_order, market_open, positions_snapshot, summarize_news_with_ollama
|
||||
from services import (
|
||||
searx_news,
|
||||
extra_research,
|
||||
summarize_news_with_ollama,
|
||||
strategy_signals,
|
||||
llm_final_decision,
|
||||
place_order,
|
||||
market_open,
|
||||
positions_snapshot,
|
||||
)
|
||||
|
||||
scheduler = BackgroundScheduler(timezone=settings.timezone)
|
||||
|
||||
@@ -45,13 +54,21 @@ def run_cycle():
|
||||
break
|
||||
|
||||
news = searx_news(symbol)
|
||||
decision = ollama_decide(symbol, news)
|
||||
strat = strategy_signals(symbol, news)
|
||||
decision = llm_final_decision(symbol, news, strat)
|
||||
|
||||
# Escalate to deeper research when model asks or confidence weak
|
||||
if decision.get("needs_more_research") or decision.get("confidence", 0) < settings.min_confidence:
|
||||
more = extra_research(symbol, decision.get("research_topics", []))
|
||||
if more:
|
||||
news = news + more
|
||||
decision = llm_final_decision(symbol, news, strat)
|
||||
|
||||
drow = BotDecision(
|
||||
symbol=symbol,
|
||||
action=decision["action"],
|
||||
confidence=decision["confidence"],
|
||||
reason=decision["reason"],
|
||||
reason=f"{decision['reason']} | strat={strat['strategy']} score={strat['score']}",
|
||||
market_context=json.dumps(news)[:60000],
|
||||
order_usd=decision["order_usd"],
|
||||
status="planned",
|
||||
@@ -66,8 +83,14 @@ def run_cycle():
|
||||
)
|
||||
|
||||
if should_trade:
|
||||
notional = min(settings.max_order_usd, decision["order_usd"], settings.max_daily_notional - spent)
|
||||
if notional <= 0:
|
||||
# Fee/slippage-aware cap for tiny bankroll
|
||||
effective_cost = settings.fee_per_trade_usd + (settings.slippage_bps / 10000.0) * decision["order_usd"]
|
||||
notional = min(
|
||||
settings.max_order_usd,
|
||||
decision["order_usd"],
|
||||
settings.max_daily_notional - spent,
|
||||
)
|
||||
if notional <= effective_cost:
|
||||
drow.status = "risk_blocked"
|
||||
db.add(drow)
|
||||
db.commit()
|
||||
@@ -83,7 +106,7 @@ def run_cycle():
|
||||
qty=float((res or {}).get("json", {}).get("qty", 0) or 0),
|
||||
notional=notional,
|
||||
alpaca_order_id=(res or {}).get("json", {}).get("id", ""),
|
||||
raw=json.dumps(res)[:60000],
|
||||
raw=json.dumps({"decision": decision, "strategy": strat, "broker": res})[:60000],
|
||||
))
|
||||
db.commit()
|
||||
if ok:
|
||||
|
||||
Reference in New Issue
Block a user