Skip to content

FinceptTerminal is a modern finance application offering advanced market analytics, investment research, and economic data tools, designed for interactive exploration and data-driven decision-making in a user-friendly environment.

43.5 KB
Raw Blame
// src/algo_engine/AlgoEngine.cpp
#include "algo_engine/AlgoEngine.h"

#include "algo_engine/fno/FnoExecution.h"
#include "core/logging/Logger.h"
#include "datahub/DataHub.h"
#include "storage/sqlite/Database.h"
#include "trading/AccountManager.h"
#include "trading/ActionCenter.h"
#include "trading/DataStreamManager.h"
#include "trading/OrderThreadPool.h"
#include "trading/PaperTrading.h"
#include "trading/UnifiedTrading.h"

#include <QCoreApplication>
#include <QDateTime>
#include <QJsonArray>
#include <QJsonDocument>
#include <QMetaObject>
#include <QPointer>
#include <QSqlError>
#include <QSqlQuery>
#include <QUuid>
#include <QtConcurrent>

namespace fincept::algo {

// How long the engine thread gets to acknowledge a stop before the GUI-thread
// watchdog force-detaches the runner.
static constexpr int kAlgoEngineStopConfirmMs = 10000;

AlgoEngine& AlgoEngine::instance() {
    static AlgoEngine s;
    return s;
}

AlgoEngine::AlgoEngine() {
    // Register types crossing the engine-thread → UI-thread queued signals.
    qRegisterMetaType<fincept::algo::AlgoLiveSnapshot>("fincept::algo::AlgoLiveSnapshot");
    qRegisterMetaType<fincept::algo::AlgoMetrics>("fincept::algo::AlgoMetrics");
    qRegisterMetaType<fincept::algo::AlgoTradeRecord>("fincept::algo::AlgoTradeRecord");

    // Create the FnoDataBridge BEFORE moveToThread so it stays on the main thread.
    // Do NOT parent it to `this` — `this` moves to the engine thread, which would
    // carry fno_bridge_ along. Ownership: deleted in dtor.
    fno_bridge_ = new fincept::algo::fno::FnoDataBridge();

    // Semi-Auto approval outcomes. ActionCenter lives on the main thread and this
    // engine on engine_thread_, so these arrive queued — which is what we want:
    // the runner bookkeeping then runs on the runner's own thread.
    connect(&fincept::trading::ActionCenter::instance(), &fincept::trading::ActionCenter::order_approved, this,
            &AlgoEngine::on_approval_approved);
    connect(&fincept::trading::ActionCenter::instance(), &fincept::trading::ActionCenter::order_rejected, this,
            &AlgoEngine::on_approval_rejected);

    engine_thread_.setObjectName(QStringLiteral("AlgoEngineThread"));
    engine_thread_.start();
    moveToThread(&engine_thread_);
}

AlgoEngine::~AlgoEngine() {
    // Process exit: ask runners to stop but do NOT mark rows stopped in the DB -
    // recover_orphaned() resumes deployments that were live at shutdown. (stop_all()
    // marks rows stopped, which is the user's explicit STOP ALL.)
    {
        QMutexLocker lock(&mutex_);
        for (auto* r : std::as_const(runners_)) {
            r->request_stop();
            QMetaObject::invokeMethod(r, &DeploymentRunner::stop, Qt::QueuedConnection);
        }
    }
    engine_thread_.quit();
    engine_thread_.wait(5000);
    delete fno_bridge_;
    fno_bridge_ = nullptr;
}

void AlgoEngine::start_deployment(const services::algo::AlgoDeployment& deployment,
                                  const services::algo::AlgoStrategy& strategy) {
    QMutexLocker lock(&mutex_);
    if (runners_.contains(deployment.id)) {
        lock.unlock(); // never emit under the (non-recursive) lock — a direct slot would deadlock
        emit error_occurred(deployment.id, QStringLiteral("Deployment already running"));
        return;
    }

    auto* runner = new DeploymentRunner(deployment, strategy);
    runner->set_fno_bridge(fno_bridge_);
    runner->moveToThread(&engine_thread_);

    connect(runner, &DeploymentRunner::trade_executed, this, &AlgoEngine::trade_executed);
    connect(runner, &DeploymentRunner::metrics_updated, this, &AlgoEngine::metrics_updated);
    connect(runner, &DeploymentRunner::error_occurred, this, &AlgoEngine::error_occurred);
    connect(runner, &DeploymentRunner::live_update, this, &AlgoEngine::live_update);
    connect(runner, &DeploymentRunner::order_requested, this, &AlgoEngine::on_order_requested);
    connect(runner, &DeploymentRunner::status_changed, this, [this, runner](const QString& id, const QString& status) {
        if (status == "stopped" || status == "error") {
            QMutexLocker lock(&mutex_);
            // Compare by identity: a runner force-detached by verify_stop() is no
            // longer in the map, but it is still THIS object and must be deleted.
            const bool registered = runners_.value(id, nullptr) == runner;
            if (registered)
                runners_.remove(id);
            stopping_.remove(id);
            lock.unlock(); // never emit under the (non-recursive) lock — a direct slot would deadlock
            runner->deleteLater();
            if (registered)
                emit deployment_stopped(id);
        }
        auto& hub = datahub::DataHub::instance();
        hub.publish(QStringLiteral("algo:state:") + id, QVariant::fromValue(status));
    });

    // Publish metrics to DataHub on update
    connect(runner, &DeploymentRunner::metrics_updated, this, [](const QString& dep_id, const AlgoMetrics& m) {
        datahub::DataHub::instance().publish(QStringLiteral("algo:metrics:") + dep_id, QVariant::fromValue(m));
    });
    connect(runner, &DeploymentRunner::trade_executed, this, [](const AlgoTradeRecord& trade) {
        datahub::DataHub::instance().publish(QStringLiteral("algo:trade:") + trade.deployment_id,
                                             QVariant::fromValue(trade));
    });

    runners_.insert(deployment.id, runner);
    lock.unlock();

    // Persist the row BEFORE the runner's queued start() (which UPDATEs status) and
    // before the UI refreshes the Dashboard — both read/expect this row to exist.
    persist_deployment(deployment);

    QMetaObject::invokeMethod(runner, &DeploymentRunner::start, Qt::QueuedConnection);
    emit deployment_started(deployment.id);
    LOG_INFO(
        "AlgoEngine",
        QString("Started deployment %1 for strategy '%2' on %3").arg(deployment.id, strategy.name, deployment.symbol));
}

AlgoStopResult AlgoEngine::stop_deployment(const QString& deployment_id) {
    return stop_deployment_impl(deployment_id, QString());
}

AlgoStopResult AlgoEngine::stop_deployment_impl(const QString& deployment_id, const QString& note) {
    AlgoStopResult res;
    res.deployment_id = deployment_id;

    DeploymentRunner* runner = nullptr;
    bool already_stopping = false;
    {
        QMutexLocker lock(&mutex_);
        runner = runners_.value(deployment_id, nullptr);
        if (runner) {
            already_stopping = stopping_.contains(deployment_id);
            stopping_.insert(deployment_id);
            // 1) Kill flag first - effective immediately on the engine thread.
            runner->request_stop();
            res.runner_found = true;
            res.orders_blocked = true;
            const auto& d = runner->deployment();
            res.strategy_name = d.strategy_name;
            res.symbol = d.symbol.isEmpty() ? d.underlying : d.symbol;
            res.mode = d.mode;
        }
    }

    // 2) Persist now, from this thread: whatever the engine thread does next, the
    //    row will not be resumed by recover_orphaned() after a restart. (A STOP on a
    //    row with no runner - stale, errored, or recovery not run yet - used to do
    //    nothing at all, and that row resumed trading at the next launch.)
    res.db_marked_stopped = mark_stopped_in_db(deployment_id, note);
    if (res.strategy_name.isEmpty()) {
        auto db = fincept::Database::instance().connection();
        QSqlQuery q(db);
        q.prepare(QStringLiteral("SELECT strategy_name, symbol, underlying, mode FROM algo_deployments WHERE id = ?"));
        q.addBindValue(deployment_id);
        if (q.exec() && q.next()) {
            res.strategy_name = q.value(0).toString();
            res.symbol = q.value(1).toString().isEmpty() ? q.value(2).toString() : q.value(1).toString();
            res.mode = q.value(3).toString();
        }
    }

    if (!runner) {
        res.note = QStringLiteral("no runner in this session - row marked stopped");
        LOG_INFO("AlgoEngine", QString("Stop %1: %2").arg(deployment_id, res.note));
        emit deployment_stopped(deployment_id); // refresh the Dashboard
        return res;
    }

    if (already_stopping) {
        res.note = QStringLiteral("stop already in progress");
        return res;
    }

    // 3) Orderly cleanup on the runner's own thread (unsubscribe, persist metrics).
    QMetaObject::invokeMethod(runner, &DeploymentRunner::stop, Qt::QueuedConnection);

    // 4) Verify from the GUI thread - a timer on the engine thread would never fire
    //    if that thread is the one that is stuck.
    if (auto* app = QCoreApplication::instance()) {
        QTimer::singleShot(kAlgoEngineStopConfirmMs, app, [this, deployment_id]() { verify_stop(deployment_id); });
        res.note = QStringLiteral("orders blocked; stop confirmation pending");
    } else {
        res.note = QStringLiteral("orders blocked; application shutting down");
    }
    LOG_INFO("AlgoEngine", QString("Stop requested for %1 (%2)").arg(deployment_id, res.note));
    return res;
}

bool AlgoEngine::mark_stopped_in_db(const QString& deployment_id, const QString& note) {
    auto db = fincept::Database::instance().connection();
    QSqlQuery q(db);
    if (note.isEmpty()) {
        q.prepare(QStringLiteral("UPDATE algo_deployments SET status='stopped', updated_at=datetime('now') WHERE id=?"));
    } else {
        q.prepare(QStringLiteral("UPDATE algo_deployments SET status='stopped', error_message=?, "
                                 "updated_at=datetime('now') WHERE id=?"));
        q.addBindValue(note);
    }
    q.addBindValue(deployment_id);
    if (!q.exec()) {
        LOG_ERROR("AlgoEngine", QString("Could not mark %1 stopped: %2").arg(deployment_id, q.lastError().text()));
        return false;
    }
    return true;
}

void AlgoEngine::verify_stop(const QString& deployment_id) {
    DeploymentRunner* runner = nullptr;
    services::algo::AlgoDeployment d;
    {
        QMutexLocker lock(&mutex_);
        if (!stopping_.contains(deployment_id))
            return; // acknowledged: the status handler cleared it
        runner = runners_.value(deployment_id, nullptr);
        if (!runner) {
            stopping_.remove(deployment_id);
            return;
        }
        // Force-detach: the engine never processed the stop. The kill flag already
        // blocks every order path; take the runner out of the active set so the
        // Dashboard and active_count() stop reporting it as running. It is deleted
        // when (if) its thread wakes up and finally runs stop().
        d = runner->deployment(); // copied under the lock: the object may be deleted later
        runners_.remove(deployment_id);
        stopping_.remove(deployment_id);
    }
    // Cut its market data from this thread so no further ticks are queued to it.
    trading::DataStreamManager::instance().close_quote_feed(QStringLiteral("algo:") + deployment_id,
                                                            d.broker_account_id);
    const QString msg =
        QStringLiteral("The algo engine did not confirm the stop within %1 s. New orders are blocked and the "
                       "runner was detached; %2")
            .arg(kAlgoEngineStopConfirmMs / 1000)
            .arg(d.mode == QLatin1String("live")
                     ? QStringLiteral("this is a LIVE deployment - verify open orders and positions at the broker.")
                     : QStringLiteral("no further paper fills will be recorded."));
    mark_stopped_in_db(deployment_id, msg);
    LOG_ERROR("AlgoEngine", QString("Deployment %1: %2").arg(deployment_id, msg));
    emit error_occurred(deployment_id, msg);
    emit deployment_stopped(deployment_id);
}

QVector<AlgoStopResult> AlgoEngine::stop_all_live(const QString& reason) {
    QStringList ids;
    {
        QMutexLocker lock(&mutex_);
        for (auto it = runners_.constBegin(); it != runners_.constEnd(); ++it)
            if (it.value()->deployment().mode == QLatin1String("live"))
                ids << it.key();
    }
    // Rows that could resume or are mid-start without a runner in this session.
    {
        auto db = fincept::Database::instance().connection();
        QSqlQuery q(db);
        if (q.exec(QStringLiteral("SELECT id FROM algo_deployments WHERE mode='live' "
                                  "AND status IN ('running','starting','error','crashed')"))) {
            while (q.next()) {
                const QString id = q.value(0).toString();
                if (!ids.contains(id))
                    ids << id;
            }
        }
    }
    QVector<AlgoStopResult> out;
    out.reserve(ids.size());
    for (const auto& id : ids)
        out.append(stop_deployment_impl(id, reason));
    LOG_WARN("AlgoEngine", QString("stop_all_live (%1): %2 live deployment(s) stopped").arg(reason).arg(out.size()));
    return out;
}

void AlgoEngine::pause_deployment(const QString& deployment_id) {
    QMutexLocker lock(&mutex_);
    auto* runner = runners_.value(deployment_id, nullptr);
    if (!runner)
        return;
    lock.unlock();
    QMetaObject::invokeMethod(runner, &DeploymentRunner::pause, Qt::QueuedConnection);
}

void AlgoEngine::resume_deployment(const QString& deployment_id) {
    QMutexLocker lock(&mutex_);
    auto* runner = runners_.value(deployment_id, nullptr);
    if (!runner)
        return;
    lock.unlock();
    QMetaObject::invokeMethod(runner, &DeploymentRunner::resume, Qt::QueuedConnection);
}

void AlgoEngine::stop_all() {
    QMutexLocker lock(&mutex_);
    auto ids = runners_.keys();
    lock.unlock();
    for (const auto& id : ids)
        stop_deployment_impl(id, QString());
}

bool AlgoEngine::is_running(const QString& deployment_id) const {
    QMutexLocker lock(&mutex_);
    auto* runner = runners_.value(deployment_id, nullptr);
    return runner && runner->is_running();
}

QStringList AlgoEngine::active_deployment_ids() const {
    QMutexLocker lock(&mutex_);
    return runners_.keys();
}

AlgoMetrics AlgoEngine::metrics(const QString& deployment_id) const {
    QMutexLocker lock(&mutex_);
    auto* runner = runners_.value(deployment_id, nullptr);
    return runner ? runner->metrics() : AlgoMetrics{};
}

int AlgoEngine::active_count() const {
    QMutexLocker lock(&mutex_);
    return runners_.size();
}

void AlgoEngine::on_order_requested(const AlgoOrderSignal& signal) {
    execute_order(signal);
}

void AlgoEngine::on_approval_approved(const QString& pending_id, const QString& broker_order_id) {
    QMutexLocker lock(&mutex_);
    const QString dep_id = approvals_.take(pending_id);
    if (dep_id.isEmpty())
        return; // not one of ours (another screen queued it)
    auto* runner = runners_.value(dep_id, nullptr);
    lock.unlock();
    if (!runner)
        return; // deployment stopped while the order sat in the queue
    LOG_INFO("AlgoEngine", QString("Deployment %1: queued order %2 approved -> broker order %3")
                               .arg(dep_id, pending_id, broker_order_id));
    runner->on_approval_resolved(pending_id, true, broker_order_id, QString());
}

void AlgoEngine::on_approval_rejected(const QString& pending_id, const QString& reason) {
    QMutexLocker lock(&mutex_);
    const QString dep_id = approvals_.take(pending_id);
    if (dep_id.isEmpty())
        return;
    auto* runner = runners_.value(dep_id, nullptr);
    lock.unlock();
    if (!runner)
        return;
    LOG_WARN("AlgoEngine",
             QString("Deployment %1: queued order %2 rejected: %3").arg(dep_id, pending_id, reason));
    runner->on_approval_resolved(pending_id, false, QString(), reason);
}

void AlgoEngine::execute_order(const AlgoOrderSignal& signal) {
    // Last gate before any order leaves the engine: a deployment being stopped (or
    // already gone) places nothing, even if its signal was emitted a moment before
    // the kill flag was set.
    {
        QMutexLocker lock(&mutex_);
        auto* r = runners_.value(signal.deployment_id, nullptr);
        if (!r || r->stop_requested()) {
            lock.unlock();
            LOG_WARN("AlgoEngine", QString("Deployment %1: %2 order DROPPED - deployment is stopping/stopped")
                                       .arg(signal.deployment_id, signal.reason));
            return;
        }
    }
    // Multi-leg F&O basket → dedicated path (paper portfolio / live broker basket).
    if (!signal.legs.isEmpty()) {
        execute_basket(signal);
        return;
    }

    QPointer<AlgoEngine> self = this;
    QString dep_id = signal.deployment_id;
    const double submitted_price = signal.price;
    const double qty = signal.quantity;

    // ── Paper: never touch a real broker ────────────────────────────────────────
    // Simulate an immediate fill at the signal's reference price. This is the hard
    // safety gate: even though a paper deployment carries a real broker account (its
    // data source), its orders are never sent to that broker.
    if (signal.mode != QStringLiteral("live")) {
        const QString sim_id = QStringLiteral("paper-") + QUuid::createUuid().toString(QUuid::WithoutBraces);
        QMetaObject::invokeMethod(
            this,
            [self, dep_id, sim_id, submitted_price, qty]() {
                if (!self)
                    return;
                QMutexLocker lock(&self->mutex_);
                auto* runner = self->runners_.value(dep_id, nullptr);
                if (!runner)
                    return;
                lock.unlock();
                runner->on_order_filled(sim_id, submitted_price, qty);
            },
            Qt::QueuedConnection);
        LOG_INFO("AlgoEngine", QString("Deployment %1: PAPER simulated fill %2 %3 @ %4")
                                   .arg(dep_id, signal.side)
                                   .arg(qty)
                                   .arg(submitted_price));
        return;
    }

    fincept::trading::UnifiedOrder order;
    order.symbol = signal.symbol;
    order.exchange = signal.exchange;
    order.side = (signal.side == "BUY") ? trading::OrderSide::Buy : trading::OrderSide::Sell;
    order.order_type = trading::OrderType::Market;
    order.quantity = signal.quantity;
    if (signal.product_type == "CNC" || signal.product_type == "delivery")
        order.product_type = trading::ProductType::Delivery;
    else if (signal.product_type == "NRML" || signal.product_type == "margin")
        order.product_type = trading::ProductType::Margin;
    else
        order.product_type = trading::ProductType::Intraday;

    const QString account_id = signal.account_id;

    // ── Semi-Auto approval gate ─────────────────────────────────────────────────
    // Every other live-order path opts into this (MCP live tools, workflow order
    // nodes, the equity screen, basket/broadcast dialogs). Without it an account
    // set to "Semi-Auto — require approval" still had algo deployments firing
    // unattended orders on it. Mirrors mcp/tools/LiveTradingTools.cpp and
    // services/workflow/adapters/ServiceBridges.cpp: queue, don't send.
    if (fincept::trading::ActionCenter::instance().should_queue(account_id, QStringLiteral("placeorder"))) {
        const QString pending_id = fincept::trading::ActionCenter::instance().queue_order(
            account_id, QStringLiteral("placeorder"),
            fincept::trading::ActionCenter::serialize_unified_order(order));

        QMutexLocker lock(&mutex_);
        auto* runner = runners_.value(dep_id, nullptr);
        if (pending_id.isEmpty()) {
            lock.unlock();
            LOG_ERROR("AlgoEngine",
                      QString("Deployment %1: Semi-Auto queue failed — order dropped (NOT sent)").arg(dep_id));
            if (runner)
                runner->on_order_rejected(QString(), QStringLiteral("Semi-Auto: failed to queue for approval"));
            emit error_occurred(dep_id, QStringLiteral("Order could not be queued for approval"));
            return;
        }
        approvals_.insert(pending_id, dep_id);
        lock.unlock();
        // Keeps the runner's in-flight guard held until the user acts, so the
        // deployment cannot stack one approval request per tick.
        if (runner)
            runner->on_order_queued_for_approval(pending_id);
        LOG_WARN("AlgoEngine", QString("Deployment %1: account %2 is in Semi-Auto — order queued for approval (%3)")
                                   .arg(dep_id, account_id, pending_id));
        return;
    }

    // Dedicated order executor (never starved by sleeping jobs on the global pool).
    (void)QtConcurrent::run(&fincept::trading::order_thread_pool(), [self, dep_id, account_id, order, submitted_price]() {
        auto response = fincept::trading::UnifiedTrading::instance().place_order(account_id, order);

        if (!self)
            return;
        QMetaObject::invokeMethod(
            self,
            [self, dep_id, response, order, submitted_price]() {
                if (!self)
                    return;
                QMutexLocker lock(&self->mutex_);
                auto* runner = self->runners_.value(dep_id, nullptr);
                if (!runner)
                    return;
                lock.unlock();

                if (response.success) {
                    // Use the signal's reference price as the fill price (market orders
                    // carry no price; the broker's avg-fill isn't returned synchronously).
                    runner->on_order_filled(response.order_id, submitted_price, order.quantity);
                } else {
                    runner->on_order_rejected(response.order_id, response.message);
                }
            },
            Qt::QueuedConnection);
    });
}

QString AlgoEngine::resolve_paper_portfolio_id(const AlgoOrderSignal& signal) {
    // 1) Explicit portfolio stamped on the signal from the deployment.
    if (!signal.paper_portfolio_id.isEmpty())
        return signal.paper_portfolio_id;

    // 2) The deployment account's linked paper portfolio.
    if (!signal.account_id.isEmpty()) {
        const auto acct = fincept::trading::AccountManager::instance().get_account(signal.account_id);
        if (!acct.paper_portfolio_id.isEmpty())
            return acct.paper_portfolio_id;
    }

    // 3) Reuse or create a shared "F&O Paper" portfolio (mirrors the F&O Builder).
    if (auto existing = fincept::trading::pt_find_portfolio(QStringLiteral("F&O Paper"), QStringLiteral("NFO")))
        return existing->id;
    const auto created =
        fincept::trading::pt_create_portfolio(QStringLiteral("F&O Paper"), 1000000.0, QStringLiteral("INR"), 1.0,
                                              QStringLiteral("cross"), 0.001, QStringLiteral("NFO"));
    LOG_INFO("AlgoEngine", QString("Created F&O Paper portfolio %1 for basket execution").arg(created.id));
    return created.id;
}

void AlgoEngine::execute_basket(const AlgoOrderSignal& signal) {
    QPointer<AlgoEngine> self = this;
    const QString dep_id = signal.deployment_id;
    const QVector<AlgoOrderLeg> legs = signal.legs;

    // ── Paper: route every leg to the paper portfolio, NEVER the broker ──────────
    // Hard safety gate: a paper deployment carries a real broker account as its
    // data source, but its basket is simulated against pt_place_order. We never
    // call place_basket_orders here (it routes by ACCOUNT mode and could fire real
    // orders on a live data account).
    if (signal.mode != QStringLiteral("live")) {
        const QString portfolio_id = resolve_paper_portfolio_id(signal);
        if (portfolio_id.isEmpty()) {
            LOG_ERROR("AlgoEngine", QString("Deployment %1: PAPER basket: no portfolio").arg(dep_id));
            return;
        }
        QMetaObject::invokeMethod(
            this,
            [self, dep_id, legs, portfolio_id]() {
                if (!self)
                    return;
                QMutexLocker lock(&self->mutex_);
                auto* runner = self->runners_.value(dep_id, nullptr);
                if (!runner)
                    return;
                lock.unlock();

                // Place each leg; immediate fill at the leg's reference price. If any
                // leg fails, reverse the already-placed legs (reduce-only) and reject
                // the whole basket so the runner records no position (atomic entry).
                QVector<int> placed_ok;
                bool any_fail = false;
                for (int i = 0; i < legs.size(); ++i) {
                    const auto& leg = legs[i];
                    bool ok = true;
                    try {
                        // pt_place_order inserts a pending order; a market order is
                        // filled explicitly at the leg's reference price (same as the
                        // Equity/F&O paper paths — there is no implicit market fill).
                        const auto po = fincept::trading::pt_place_order(
                            portfolio_id, leg.symbol, leg.side.toLower(), QStringLiteral("market"), leg.quantity,
                            leg.price, std::nullopt, false, QStringLiteral("NFO"), QStringLiteral("NRML"));
                        fincept::trading::pt_fill_order(po.id, leg.price);
                    } catch (const std::exception& e) {
                        ok = false;
                        any_fail = true;
                        LOG_ERROR("AlgoEngine", QString("Deployment %1: PAPER leg %2 (%3) failed: %4")
                                                    .arg(dep_id)
                                                    .arg(i)
                                                    .arg(leg.symbol, QString::fromUtf8(e.what())));
                    }
                    if (ok)
                        placed_ok.append(i);
                }

                if (!any_fail) {
                    for (int i = 0; i < legs.size(); ++i)
                        runner->on_leg_filled(i, legs[i], legs[i].price, legs[i].quantity);
                    return;
                }

                // Rollback the legs that did fill, then reject every leg.
                for (int i : std::as_const(placed_ok)) {
                    const auto& leg = legs[i];
                    const QString rev =
                        (leg.side.toLower() == QStringLiteral("buy")) ? QStringLiteral("sell") : QStringLiteral("buy");
                    try {
                        const auto ro = fincept::trading::pt_place_order(
                            portfolio_id, leg.symbol, rev, QStringLiteral("market"), leg.quantity, leg.price,
                            std::nullopt, true, QStringLiteral("NFO"), QStringLiteral("NRML"));
                        fincept::trading::pt_fill_order(ro.id, leg.price);
                    } catch (const std::exception& e) {
                        LOG_ERROR("AlgoEngine", QString("Deployment %1: PAPER rollback leg %2 failed: %3")
                                                    .arg(dep_id)
                                                    .arg(i)
                                                    .arg(QString::fromUtf8(e.what())));
                    }
                }
                for (int i = 0; i < legs.size(); ++i)
                    runner->on_leg_rejected(i, QStringLiteral("paper basket aborted (partial fill)"));
            },
            Qt::QueuedConnection);
        LOG_INFO("AlgoEngine", QString("Deployment %1: PAPER basket %2 legs -> portfolio %3")
                                   .arg(dep_id)
                                   .arg(legs.size())
                                   .arg(portfolio_id));
        return;
    }

    // ── Live: broker basket (ONLY signal.mode == "live") ─────────────────────────
    const QString account_id = signal.account_id;
    const fincept::trading::BasketOrderRequest basket =
        fincept::algo::fno::build_basket_request(legs, signal.product_type);

    fincept::trading::UnifiedTrading::instance().place_basket_orders(
        account_id, basket, [self, dep_id, legs, account_id](const fincept::trading::BasketOrderResult& res) {
            if (!self)
                return;
            QMetaObject::invokeMethod(
                self,
                [self, dep_id, legs, account_id, res]() {
                    if (!self)
                        return;
                    QMutexLocker lock(&self->mutex_);
                    auto* runner = self->runners_.value(dep_id, nullptr);
                    if (!runner)
                        return;
                    lock.unlock();

                    // place_basket_orders reorders (BUY-first) and batches, so map each
                    // leg to its result by symbol rather than by index.
                    auto leg_succeeded = [&res](const QString& sym) -> bool {
                        for (const auto& r : res.results)
                            if (r.symbol == sym)
                                return r.success;
                        return false;
                    };

                    QVector<int> filled_idx;
                    bool any_fail = false;
                    for (int i = 0; i < legs.size(); ++i) {
                        if (leg_succeeded(legs[i].symbol))
                            filled_idx.append(i);
                        else
                            any_fail = true;
                    }

                    if (!any_fail) {
                        // Fills carry no price; mark at the leg's reference LTP (P3.5
                        // replaces this with live leg-quote marks).
                        for (int i = 0; i < legs.size(); ++i)
                            runner->on_leg_filled(i, legs[i], legs[i].price, legs[i].quantity);
                        return;
                    }

                    // Rollback: close the legs that filled, then reject the basket so no
                    // position is recorded, and surface the error.
                    if (!filled_idx.isEmpty()) {
                        QVector<AlgoOrderLeg> reverse;
                        for (int i : std::as_const(filled_idx)) {
                            AlgoOrderLeg rl = legs[i];
                            rl.side =
                                (legs[i].side == QLatin1String("BUY")) ? QStringLiteral("SELL") : QStringLiteral("BUY");
                            reverse.append(rl);
                        }
                        const auto rb = fincept::algo::fno::build_basket_request(reverse, QString());
                        fincept::trading::UnifiedTrading::instance().place_basket_orders(
                            account_id, rb, [dep_id](const fincept::trading::BasketOrderResult&) {
                                LOG_WARN("AlgoEngine", QString("Deployment %1: entry basket rolled back").arg(dep_id));
                            });
                    }
                    for (int i = 0; i < legs.size(); ++i)
                        runner->on_leg_rejected(i, QStringLiteral("live basket aborted (partial fill)"));
                    emit self->error_occurred(dep_id, QStringLiteral("F&O entry basket partially failed; rolled back"));
                },
                Qt::QueuedConnection);
        });
    LOG_INFO("AlgoEngine",
             QString("Deployment %1: LIVE basket %2 legs -> account %3").arg(dep_id).arg(legs.size()).arg(account_id));
}

void AlgoEngine::persist_deployment(const services::algo::AlgoDeployment& d) {
    auto db = fincept::Database::instance().connection();
    QSqlQuery q(db);
    // UPSERT, not INSERT OR REPLACE: recover_orphaned() re-runs start_deployment() — and so
    // this — for every deployment that was live before a restart. REPLACE deletes the
    // existing row and re-inserts it with column defaults, which wiped resolved_legs_json /
    // resolved_expiry (the open F&O basket the runner is about to reattach from that very
    // row) before DeploymentRunner::restore_state_from_db() read it, and reset created_at.
    // The UPDATE branch deliberately leaves those columns (and pid) alone.
    q.prepare(QStringLiteral(
        "INSERT INTO algo_deployments "
        "(id, strategy_id, strategy_name, strategy_kind, symbol, exchange, product_type, "
        " mode, entry_side, backend, broker_id, broker_account_id, paper_portfolio_id, "
        " timeframe, quantity, max_order_value, max_daily_loss, "
        " instrument_type, underlying, status, created_at, updated_at) "
        "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?, datetime('now'), datetime('now')) "
        "ON CONFLICT(id) DO UPDATE SET "
        " strategy_id=excluded.strategy_id, strategy_name=excluded.strategy_name, "
        " strategy_kind=excluded.strategy_kind, symbol=excluded.symbol, exchange=excluded.exchange, "
        " product_type=excluded.product_type, mode=excluded.mode, entry_side=excluded.entry_side, "
        " backend=excluded.backend, broker_id=excluded.broker_id, "
        " broker_account_id=excluded.broker_account_id, paper_portfolio_id=excluded.paper_portfolio_id, "
        " timeframe=excluded.timeframe, quantity=excluded.quantity, max_order_value=excluded.max_order_value, "
        " max_daily_loss=excluded.max_daily_loss, instrument_type=excluded.instrument_type, "
        " underlying=excluded.underlying, status=excluded.status, error_message='', "
        " updated_at=datetime('now')"));
    q.addBindValue(d.id);
    q.addBindValue(d.strategy_id);
    q.addBindValue(d.strategy_name);
    q.addBindValue(d.strategy_kind);
    q.addBindValue(d.symbol);
    q.addBindValue(d.exchange);
    q.addBindValue(d.product_type);
    q.addBindValue(d.mode);
    q.addBindValue(d.entry_side);
    q.addBindValue(d.backend);
    q.addBindValue(d.broker_id);
    q.addBindValue(d.broker_account_id);
    q.addBindValue(d.paper_portfolio_id);
    q.addBindValue(d.timeframe);
    q.addBindValue(d.quantity);
    q.addBindValue(d.max_order_value);
    q.addBindValue(d.max_daily_loss);
    q.addBindValue(d.instrument_type); // F&O: option|future — drives the multi-leg path on resume
    q.addBindValue(d.underlying);      // F&O underlying for chain lookup on resume
    q.addBindValue(QStringLiteral("starting"));
    if (!q.exec())
        LOG_ERROR("AlgoEngine", QString("Failed to persist deployment %1: %2").arg(d.id, q.lastError().text()));
    else
        LOG_INFO("AlgoEngine",
                 QString("Persisted deployment row %1 (%2/%3 on %4)").arg(d.id, d.mode, d.backend, d.symbol));
}

void AlgoEngine::list_deployments() {
    QPointer<AlgoEngine> self = this;
    (void)QtConcurrent::run([self]() {
        auto db = fincept::Database::instance().connection();
        QSqlQuery q(db);
        q.exec(QStringLiteral("SELECT * FROM algo_deployments ORDER BY created_at DESC"));

        QVector<services::algo::AlgoDeployment> deployments;
        while (q.next()) {
            services::algo::AlgoDeployment d;
            d.id = q.value("id").toString();
            d.strategy_id = q.value("strategy_id").toString();
            d.strategy_name = q.value("strategy_name").toString();
            d.symbol = q.value("symbol").toString();
            d.exchange = q.value("exchange").toString();
            d.product_type = q.value("product_type").toString();
            d.mode = q.value("mode").toString();
            d.entry_side = q.value("entry_side").toString();
            d.backend = q.value("backend").toString();
            d.broker_id = q.value("broker_id").toString();
            d.broker_account_id = q.value("broker_account_id").toString();
            d.instrument_type = q.value("instrument_type").toString();
            d.underlying = q.value("underlying").toString();
            d.resolved_expiry = q.value("resolved_expiry").toString();
            d.status = q.value("status").toString();
            d.error_message = q.value("error_message").toString();
            d.timeframe = q.value("timeframe").toString();
            d.quantity = q.value("quantity").toDouble();
            d.max_order_value = q.value("max_order_value").toDouble();
            d.max_daily_loss = q.value("max_daily_loss").toDouble();
            d.created_at = q.value("created_at").toString();
            deployments.append(d);
        }

        // Load live metrics for running deployments
        QSqlQuery mq(db);
        mq.exec(QStringLiteral("SELECT * FROM algo_metrics"));
        QHash<QString, int> metrics_idx;
        while (mq.next()) {
            QString dep_id = mq.value("deployment_id").toString();
            for (int i = 0; i < deployments.size(); ++i) {
                if (deployments[i].id == dep_id) {
                    deployments[i].total_pnl = mq.value("total_pnl").toDouble();
                    deployments[i].unrealized_pnl = mq.value("unrealized_pnl").toDouble();
                    deployments[i].total_trades = mq.value("total_trades").toInt();
                    deployments[i].win_rate = mq.value("win_rate").toDouble();
                    deployments[i].max_drawdown = mq.value("max_drawdown").toDouble();
                    deployments[i].current_price = mq.value("current_price").toDouble();
                    deployments[i].position_qty = mq.value("current_position_qty").toDouble();
                    deployments[i].position_side = mq.value("current_position_side").toString();
                    deployments[i].position_entry = mq.value("current_position_entry").toDouble();
                    break;
                }
            }
        }

        if (!self)
            return;
        QMetaObject::invokeMethod(
            self,
            [self, deployments]() {
                if (!self)
                    return;
                emit self->deployments_loaded(deployments);
            },
            Qt::QueuedConnection);
    });
}

void AlgoEngine::recover_orphaned() {
    // On restart, deployments that were active (or stuck in error from the prior
    // no-data bug) should resume rather than silently die — that's what users
    // expect of a deployed algo. Reload each one + its strategy and restart it.
    auto db = fincept::Database::instance().connection();
    QSqlQuery q(db);
    q.exec(QStringLiteral("SELECT * FROM algo_deployments WHERE status IN ('running','starting','error','crashed')"));

    QVector<services::algo::AlgoDeployment> to_resume;
    while (q.next()) {
        services::algo::AlgoDeployment d;
        d.id = q.value("id").toString();
        d.strategy_id = q.value("strategy_id").toString();
        d.strategy_name = q.value("strategy_name").toString();
        d.strategy_kind = q.value("strategy_kind").toString();
        d.symbol = q.value("symbol").toString();
        d.exchange = q.value("exchange").toString();
        d.product_type = q.value("product_type").toString();
        d.mode = q.value("mode").toString();
        d.entry_side = q.value("entry_side").toString();
        d.backend = q.value("backend").toString();
        d.broker_id = q.value("broker_id").toString();
        d.broker_account_id = q.value("broker_account_id").toString();
        d.paper_portfolio_id = q.value("paper_portfolio_id").toString();
        d.timeframe = q.value("timeframe").toString();
        d.quantity = q.value("quantity").toDouble();
        d.max_order_value = q.value("max_order_value").toDouble();
        d.max_daily_loss = q.value("max_daily_loss").toDouble();
        // F&O: needed so the resumed runner takes the multi-leg path and can
        // reattach its open basket (resolved_legs_json read in restore_state_from_db).
        d.instrument_type = q.value("instrument_type").toString();
        d.underlying = q.value("underlying").toString();
        d.resolved_expiry = q.value("resolved_expiry").toString();
        to_resume.append(d);
    }

    for (const auto& d : to_resume) {
        auto strat = load_strategy(d.strategy_id);
        if (strat.id.isEmpty()) {
            QSqlQuery u(db);
            u.prepare(QStringLiteral("UPDATE algo_deployments SET status='stopped', "
                                     "error_message='Strategy not found on resume' WHERE id=?"));
            u.addBindValue(d.id);
            u.exec();
            LOG_WARN("AlgoEngine",
                     QString("Cannot resume deployment %1: strategy %2 missing").arg(d.id, d.strategy_id));
            continue;
        }
        LOG_INFO("AlgoEngine",
                 QString("Resuming deployment %1 ('%2' on %3) after restart").arg(d.id, strat.name, d.symbol));
        start_deployment(d, strat);
    }
}

bool AlgoEngine::has_active_duplicate(const QString& strategy_id, const QString& symbol, const QString& mode,
                                      const QString& entry_side) const {
    auto db = fincept::Database::instance().connection();
    QSqlQuery q(db);
    q.prepare(QStringLiteral("SELECT COUNT(*) FROM algo_deployments WHERE strategy_id=? AND symbol=? AND mode=? "
                             "AND entry_side=? AND status IN ('running','starting')"));
    q.addBindValue(strategy_id);
    q.addBindValue(symbol);
    q.addBindValue(mode);
    q.addBindValue(entry_side);
    if (q.exec() && q.next())
        return q.value(0).toInt() > 0;
    return false;
}

void AlgoEngine::remove_deployment(const QString& deployment_id) {
    // Stop the runner first if it happens to be live (REMOVE is normally only shown
    // for already-stopped rows, but guard anyway).
    {
        QMutexLocker lock(&mutex_);
        if (auto* r = runners_.value(deployment_id, nullptr)) {
            r->request_stop(); // no order may leave it while the rows are deleted
            lock.unlock();
            QMetaObject::invokeMethod(r, &DeploymentRunner::stop, Qt::QueuedConnection);
        }
    }

    auto db = fincept::Database::instance().connection();
    const char* const stmts[] = {
        "DELETE FROM algo_deployments WHERE id = ?",
        "DELETE FROM algo_metrics WHERE deployment_id = ?",
        "DELETE FROM algo_deployment_heartbeat WHERE deployment_id = ?",
        "DELETE FROM algo_order_signals WHERE deployment_id = ?",
        "DELETE FROM algo_trades WHERE deployment_id = ?",
    };
    for (const char* sql : stmts) {
        QSqlQuery q(db);
        q.prepare(QString::fromLatin1(sql));
        q.addBindValue(deployment_id);
        if (!q.exec())
            LOG_ERROR("AlgoEngine", QString("remove_deployment(%1): %2").arg(deployment_id, q.lastError().text()));
    }
    LOG_INFO("AlgoEngine", QString("Removed deployment %1").arg(deployment_id));
    list_deployments(); // refresh the Dashboard
}

services::algo::AlgoStrategy AlgoEngine::load_strategy(const QString& strategy_id) {
    services::algo::AlgoStrategy s;
    auto db = fincept::Database::instance().connection();
    QSqlQuery q(db);
    q.prepare(QStringLiteral("SELECT * FROM algo_strategies WHERE id = ?"));
    q.addBindValue(strategy_id);
    if (q.exec() && q.next()) {
        s.id = q.value("id").toString();
        s.name = q.value("name").toString();
        s.description = q.value("description").toString();
        s.timeframe = q.value("timeframe").toString();
        s.entry_conditions = QJsonDocument::fromJson(q.value("entry_conditions").toString().toUtf8()).array();
        s.exit_conditions = QJsonDocument::fromJson(q.value("exit_conditions").toString().toUtf8()).array();
        s.entry_logic = q.value("entry_logic").toString();
        s.exit_logic = q.value("exit_logic").toString();
        s.stop_loss = q.value("stop_loss").toDouble();
        s.take_profit = q.value("take_profit").toDouble();
        s.trailing_stop = q.value("trailing_stop").toDouble();
        // F&O strategies: the leg RULES live in legs_json. Without them a deployment
        // resumed after a restart had strategy.legs == [] — resolve_entry_legs() then
        // found no legs and the multi-leg runner could never enter again (silently).
        const QString inst = q.value("instrument_type").toString();
        s.instrument_type = inst.isEmpty() ? QStringLiteral("equity") : inst;
        s.legs = QJsonDocument::fromJson(q.value("legs_json").toString().toUtf8()).array();
    }
    return s;
}

} // namespace fincept::algo