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.

10.5 KB
Raw Blame
#include "trading/PaperMarkService.h"

#include "core/logging/Logger.h"
#include "storage/sqlite/Database.h"
#include "trading/AccountDataStream.h"
#include "trading/AccountManager.h"
#include "trading/DataStreamManager.h"
#include "trading/OrderMatcher.h"
#include "trading/PaperTrading.h"
#include "trading/websocket/FyersTickTypes.h"

#include <QDate>
#include <QDateTime>
#include <QStringList>
#include <QTimeZone>
#include <QTimer>

namespace fincept::trading {

namespace {
const QString TAG = QStringLiteral("PaperMark");
const QString kConsumer = QStringLiteral("paper-mark");
// Re-evaluate tracked accounts/positions on this cadence (cheap: a few accounts,
// a handful of positions each). Fills also trigger an immediate resync.
constexpr int kResyncMs = 4000;
// Persist marked prices to SQLite at most once per this window per burst of
// ticks — one UPDATE per symbol per window instead of one per tick.
constexpr int kFlushMs = 1000;

// DAY-order semantics for Indian-equity (INR) portfolios: an order from an earlier
// IST session has expired — pt_settle_intraday() cancels those — so it must not be
// resurrected into the matcher at launch. Other currencies (crypto / US) are
// good-till-cancelled and always resume.
bool pm_is_expired_day_order(const PtPortfolio& pf, const PtOrder& o) {
    if (pf.currency.compare(QLatin1String("INR"), Qt::CaseInsensitive) != 0)
        return false;
    QDateTime created = QDateTime::fromString(o.created_at, Qt::ISODate);
    if (!created.isValid())
        return false;
    created.setTimeZone(QTimeZone::UTC); // timestamps are stored as UTC
    const QDate created_ist = created.addSecs(330 * 60).date();
    const QDate today_ist = QDateTime::currentDateTimeUtc().addSecs(330 * 60).date();
    return created_ist < today_ist;
}
} // namespace

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

PaperMarkService::PaperMarkService() = default;

void PaperMarkService::start() {
    if (started_)
        return;
    started_ = true;

    // A fill changes positions/orders. The callback can fire on a worker thread
    // (e.g. square-off runs in QtConcurrent), so marshal to this object's thread
    // before touching the bindings / emitting.
    fill_cb_id_ = OrderMatcher::instance().on_order_fill([this](const OrderFillEvent&) {
        QMetaObject::invokeMethod(
            this,
            [this]() {
                resync();
                for (auto it = bound_.cbegin(); it != bound_.cend(); ++it)
                    emit portfolio_changed(it->portfolio_id);
            },
            Qt::QueuedConnection);
    });

    // Resume matching of resting limit / stop orders that were persisted before this
    // launch. Nothing ever re-registered them with the matcher after a restart
    // (load_orders() had no caller; only the Equity screen re-added its own focused
    // account's), so they sat "pending" in the DB — holding margin — and never
    // filled. Idempotent: OrderMatcher::add_order() dedupes by order id.
    for (const auto& pf : pt_list_portfolios()) {
        QVector<PtOrder> resting;
        for (const auto& o : pt_get_orders(pf.id, QStringLiteral("pending"))) {
            if (!pm_is_expired_day_order(pf, o))
                resting.push_back(o);
        }
        OrderMatcher::instance().load_orders(resting);
    }

    resync_timer_ = new QTimer(this);
    resync_timer_->setInterval(kResyncMs);
    connect(resync_timer_, &QTimer::timeout, this, &PaperMarkService::resync);
    resync_timer_->start();

    resync();
    LOG_INFO(TAG, "Paper mark-to-market service started");
}

void PaperMarkService::resync() {
    auto& dsm = DataStreamManager::instance();
    QSet<QString> should_track; // account_ids that currently warrant a binding

    for (const auto& acct : AccountManager::instance().active_accounts()) {
        if (acct.trading_mode != QLatin1String("paper") || acct.paper_portfolio_id.isEmpty())
            continue;

        const auto positions = pt_get_positions(acct.paper_portfolio_id);
        QStringList syms;
        QVector<PosLeg> legs;
        for (const auto& p : positions) {
            if (p.quantity == 0.0 || p.symbol.isEmpty())
                continue;
            syms << p.symbol;
            const auto leg = fyers_parse_option(p.symbol);
            if (leg.valid)
                legs.push_back({leg.underlying, leg.is_call, p.symbol});
        }
        // Resting limit / stop orders need ticks too: without their symbols on the
        // stream they could only ever fill while a screen happened to feed the
        // matcher. (Market orders fill immediately and never rest.)
        for (const auto& o : pt_get_orders(acct.paper_portfolio_id, QStringLiteral("pending"))) {
            if (o.order_type == QLatin1String("market") || o.symbol.isEmpty())
                continue;
            syms << o.symbol;
            const auto leg = fyers_parse_option(o.symbol);
            if (leg.valid)
                legs.push_back({leg.underlying, leg.is_call, o.symbol});
        }
        if (syms.isEmpty())
            continue; // nothing to mark — don't force a stream up for an idle portfolio

        should_track.insert(acct.account_id);
        auto& b = bound_[acct.account_id];
        b.portfolio_id = acct.paper_portfolio_id;
        b.exact = QSet<QString>(syms.cbegin(), syms.cend());
        b.legs = legs;

        auto* stream = dsm.stream_for(acct.account_id);
        if (!stream)
            continue;

        // (Re)bind quote_updated. Reconnect when the stream object changed — a
        // token refresh runs restart_stream(), which destroys + recreates it; the
        // stored connection would be dead and marking would silently stall.
        if (b.stream != stream || !b.conn) {
            if (b.conn)
                QObject::disconnect(b.conn);
            const QString pid = acct.paper_portfolio_id;
            b.conn = connect(stream, &AccountDataStream::quote_updated, this,
                             [this, pid](const QString& aid, const QString& sym, const BrokerQuote& q) {
                                 on_quote(aid, pid, sym, q);
                             });
            b.stream = stream;
            b.symbols.clear(); // force a fresh subscribe on the new stream
        }

        const QSet<QString> want(syms.cbegin(), syms.cend());
        if (b.symbols != want) {
            stream->subscribe_symbols(kConsumer, syms);
            b.symbols = want;
        }
        dsm.start_stream(acct.account_id);
    }

    // Drop bindings for accounts that no longer hold positions / are no longer
    // active paper, releasing their stream subscription.
    for (auto it = bound_.begin(); it != bound_.end();) {
        if (!should_track.contains(it.key())) {
            if (it->conn)
                QObject::disconnect(it->conn);
            if (auto* s = dsm.stream_for(it.key()))
                s->unsubscribe_consumer(kConsumer);
            it = bound_.erase(it);
        } else {
            ++it;
        }
    }
}

void PaperMarkService::on_quote(const QString& account_id, const QString& portfolio_id, const QString& symbol,
                                const BrokerQuote& quote) {
    if (quote.ltp <= 0.0)
        return; // garbage/zero tick — the repo guard would drop it anyway; skip the work
    auto it = bound_.constFind(account_id);
    if (it == bound_.constEnd())
        return;

    // Reconcile the live quote symbol to a stored position symbol.
    QString pos_sym;
    if (it->exact.contains(symbol)) {
        pos_sym = symbol; // exact match (equities + brokers that normalise identically)
    } else {
        // Option fallback: the live spelling ("…C<strike>") differs from the
        // position spelling ("…<strike>CE"). Match by (underlying, side, strike).
        const auto tick = fyers_parse_option(symbol);
        if (tick.valid && tick.strike > 0.0) {
            const QString strike_str = QString::number(qRound64(tick.strike));
            for (const auto& leg : it->legs) {
                if (leg.is_call != tick.is_call || leg.underlying != tick.underlying)
                    continue;
                QString core = leg.symbol.section(QLatin1Char(':'), -1);
                if (core.endsWith(QLatin1String("CE")) || core.endsWith(QLatin1String("PE")))
                    core.chop(2);
                if (core.endsWith(strike_str)) {
                    pos_sym = leg.symbol;
                    break;
                }
            }
        }
    }
    if (pos_sym.isEmpty())
        return;

    // Coalesce the SQLite write; the in-memory matcher sees every tick.
    pending_[portfolio_id][pos_sym] = quote.ltp;
    if (!flush_armed_) {
        flush_armed_ = true;
        QTimer::singleShot(kFlushMs, this, [this]() { flush_prices(); });
    }

    PriceData pd;
    pd.last = quote.ltp;
    pd.bid = quote.bid;
    pd.ask = quote.ask;
    pd.timestamp = quote.timestamp;
    OrderMatcher::instance().check_orders(pos_sym, pd, portfolio_id);
    OrderMatcher::instance().check_sl_tp_triggers(portfolio_id, pos_sym, quote.ltp);
}

void PaperMarkService::flush_prices() {
    flush_armed_ = false;
    if (pending_.isEmpty())
        return;
    const auto snapshot = pending_;
    pending_.clear();

    // One transaction for the whole flush. Each pt_update_position_price() is a
    // bare UPDATE, so without this the 1 s timer issued one implicit commit per
    // position — 40 commits/second on a 40-position book, every second the
    // market is open.
    auto& db = fincept::Database::instance();
    const bool batched = db.begin_transaction().is_ok();
    if (!batched)
        LOG_WARN(TAG, "flush_prices: could not open a transaction — falling back to one commit per position");

    QStringList marked;
    marked.reserve(snapshot.size());
    for (auto pit = snapshot.cbegin(); pit != snapshot.cend(); ++pit) {
        const QString& pid = pit.key();
        for (auto sit = pit.value().cbegin(); sit != pit.value().cend(); ++sit)
            pt_update_position_price(pid, sit.key(), sit.value()); // repo guards price <= 0
        marked.append(pid);
    }

    if (batched) {
        auto c = db.commit();
        if (c.is_err()) {
            LOG_ERROR(TAG, QString("flush_prices commit failed: %1").arg(QString::fromStdString(c.error())));
            db.rollback();
            return; // nothing landed — don't tell listeners marks are fresh
        }
    }

    // Emit only after the commit, so a slot that re-reads pt_positions sees the
    // marks it was just told about.
    for (const auto& pid : marked)
        emit prices_marked(pid);
}

} // namespace fincept::trading