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.

6.6 KB
Raw Blame
#pragma once
// Unified Trading — routes orders to live broker or paper trading engine

#include "trading/BrokerRegistry.h"
#include "trading/TradingTypes.h"

#include <QMutex>
#include <QThread>
#include <QTimer>

#include <atomic>
#include <functional>
#include <optional>

namespace fincept::trading {

class UnifiedTrading : public QObject {
    Q_OBJECT
  public:
    static UnifiedTrading& instance();

    // Session management (legacy single-session API — kept for backward compat)
    TradingSession init_session(const QString& broker, const QString& mode, const QString& paper_portfolio_id = "");
    std::optional<TradingSession> get_session() const;
    TradingSession switch_mode(const QString& mode);

    // Order routing (legacy — uses session_)
    UnifiedOrderResponse place_order(const UnifiedOrder& order);
    UnifiedOrderResponse cancel_order(const QString& order_id);

    // True when `account_id` is in PAPER mode AND that PAPER mode runs on the broker's own
    // paper venue (e.g. Alpaca paper-api) instead of the local pt_* simulator: the broker
    // offers native paper and the account's stored credentials are positively its paper
    // environment (IBroker::is_paper_environment). Every order/cancel/modify/close below
    // follows this same decision, and the UI mode tag stays "paper".
    // Fails CLOSED: live keys, an unidentifiable environment or a broker that has not opted
    // in all answer false and keep the local simulator, so a "paper" ticket can never reach a
    // real exchange. Reads credentials from secure storage — call from user-action paths,
    // not per tick.
    bool uses_native_paper(const QString& account_id) const;

    // Account-aware order routing (new — uses AccountManager for credentials)
    UnifiedOrderResponse place_order(const QString& account_id, const UnifiedOrder& order);
    UnifiedOrderResponse cancel_order(const QString& account_id, const QString& order_id);
    UnifiedOrderResponse modify_order(const QString& account_id, const QString& order_id,
                                      const QJsonObject& modifications);

    // Broadcast order to multiple accounts simultaneously
    // Each account gets an independent order — different credentials, different fills.
    // Returns one result per account. Safe to call from background thread.
    struct BroadcastResult {
        QString account_id;
        QString display_name;
        UnifiedOrderResponse response;
    };
    QVector<BroadcastResult> broadcast_order(const QStringList& account_ids, const UnifiedOrder& order);

    // --- Phase 1: Bulk operations (OpenAlgo bridge) ---
    ApiResponse<CancelAllResult> cancel_all_orders(const QString& account_id);
    ApiResponse<CloseAllResult> close_all_positions(const QString& account_id);
    ApiResponse<OrderPlaceResponse> close_position(const QString& account_id, const QString& symbol,
                                                   const QString& exchange, const QString& product_type = {});
    ApiResponse<SmartOrderResult> place_smart_order(const QString& account_id, const SmartOrder& order);
    ApiResponse<QVector<BrokerQuote>> get_multi_quotes(const QString& account_id,
                                                       const QVector<QPair<QString, QString>>& symbols);
    ApiResponse<MarketDepth> get_market_depth(const QString& account_id, const QString& symbol,
                                              const QString& exchange);

    // --- Basket & Split orders ---
    // Both run asynchronously on a background thread (orders are placed
    // sequentially in batches/chunks with sleeps between) and deliver their
    // result back on the caller's thread via the supplied callback.
    void place_basket_orders(const QString& account_id, const BasketOrderRequest& basket,
                             std::function<void(const BasketOrderResult&)> callback);
    void place_split_orders(const QString& account_id, const SplitOrderRequest& request,
                            std::function<void(const SplitOrderResult&)> callback);

    // Quantity-freeze-aware placement (Phase 3 §17). If the order quantity is
    // within the freeze limit for (symbol, exchange) — or no limit is set — this
    // places a single normal order and delivers a one-element SplitOrderResult.
    // If it exceeds the limit, it auto-splits into chunks of `freeze_limit` via
    // place_split_orders. Asynchronous in BOTH cases (background thread +
    // callback) so callers get a uniform contract regardless of whether a split
    // actually happened. Use this instead of place_order when you want
    // transparent freeze handling; use place_order for the strict sync path.
    void place_order_auto_split(const QString& account_id, const UnifiedOrder& order,
                                std::function<void(const SplitOrderResult&)> callback);

    // --- Basket unwind (partial-failure recovery) ---
    // A multi-leg basket is NOT atomic: legs go out one by one, so a failure part
    // way leaves the placed legs as naked exposure (e.g. the short leg of a spread
    // without its hedge). unwind_basket_legs() undoes the legs that were placed:
    // for each successful leg it reads the broker's order book (truth, not our
    // request), cancels any unfilled remainder, re-reads the book, then sends an
    // opposite-side MARKET order for exactly the FILLED quantity in the same
    // product, through place_order() (validation + risk gate + rate limit).
    // Never reverses a leg whose fill it cannot verify — it reports it instead.
    // BLOCKING (broker round-trips): call from a worker thread.
    struct UnwindLegResult {
        QString symbol;
        QString order_id;
        bool ok = false;
        QString action; // what was done, e.g. "cancelled remainder; reversed 50"
        QString error;
    };
    QVector<UnwindLegResult> unwind_basket_legs(const QString& account_id,
                                                const QVector<BasketOrderResult::OrderResult>& legs);

    UnifiedTrading(const UnifiedTrading&) = delete;
    UnifiedTrading& operator=(const UnifiedTrading&) = delete;

  private:
    UnifiedTrading() = default;

    UnifiedOrderResponse place_paper_order(const TradingSession& session, const UnifiedOrder& order);
    UnifiedOrderResponse place_live_order(const TradingSession& session, const UnifiedOrder& order);

    // Account-aware helpers
    UnifiedOrderResponse place_paper_order_for_account(const QString& account_id, const UnifiedOrder& order);
    UnifiedOrderResponse place_live_order_for_account(const QString& account_id, const UnifiedOrder& order);

    std::optional<TradingSession> session_;
    mutable QMutex mutex_;
};

} // namespace fincept::trading