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.
C++ ai-agentsalgorithmic-tradingbloomberg-terminalcppfinancefinancial-marketsfintechgood-first-issueinvestmentinvestment-researchmachine-learningopensourcepythonqtquantitative-financestock-markettrading
#include "screens/dashboard/widgets/OrderBookMiniWidget.h"
#include "trading/OrderThreadPool.h"
#include "core/logging/Logger.h"
#include "datahub/DataHub.h"
#include "datahub/DataHubMetaTypes.h"
#include "trading/AccountManager.h"
#include "trading/BrokerTopic.h"
#include "trading/DataStreamManager.h"
#include "trading/UnifiedTrading.h"
#include "ui/theme/Theme.h"
#include <QComboBox>
#include <QDialog>
#include <QDialogButtonBox>
#include <QFormLayout>
#include <QHeaderView>
#include <QJsonObject>
#include <QLabel>
#include <QMessageBox>
#include <QPointer>
#include <QPushButton>
#include <QtConcurrent>
namespace fincept::screens::widgets {
OrderBookMiniWidget::OrderBookMiniWidget(const QJsonObject& cfg, QWidget* parent)
: BaseWidget(tr("WORKING ORDERS"), parent) {
auto* vl = content_layout();
vl->setContentsMargins(8, 6, 8, 6);
vl->setSpacing(4);
header_hint_ = new QLabel("—");
vl->addWidget(header_hint_);
table_ = new QTableWidget(0, 6, this);
table_->setHorizontalHeaderLabels({tr("Symbol"), tr("Side"), tr("Qty"), tr("Price"), tr("Status"), ""});
table_->verticalHeader()->setVisible(false);
table_->setEditTriggers(QAbstractItemView::NoEditTriggers);
table_->setSelectionMode(QAbstractItemView::NoSelection);
table_->setShowGrid(false);
table_->horizontalHeader()->setStretchLastSection(false);
table_->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch);
table_->horizontalHeader()->setSectionResizeMode(1, QHeaderView::ResizeToContents);
table_->horizontalHeader()->setSectionResizeMode(2, QHeaderView::ResizeToContents);
table_->horizontalHeader()->setSectionResizeMode(3, QHeaderView::ResizeToContents);
table_->horizontalHeader()->setSectionResizeMode(4, QHeaderView::ResizeToContents);
table_->horizontalHeader()->setSectionResizeMode(5, QHeaderView::Fixed);
table_->setColumnWidth(5, 52);
vl->addWidget(table_, 1);
// Title-bar refresh = "retry". It used to be wired to nothing, so the 20 s
// "No data yet - click refresh to retry" prompt pointed at a dead button.
// The account stream polls on its own cadence, so the only thing a retry can
// do is (re)start that stream, show the loading state again, and ask the hub.
connect(this, &BaseWidget::refresh_requested, this, [this]() {
if (broker_id_.isEmpty() || account_id_.isEmpty())
return;
ensure_stream_running();
if (table_->rowCount() == 0)
set_loading(true);
datahub::DataHub::instance().request(
trading::broker_topic(broker_id_, account_id_, QStringLiteral("orders")), /*force=*/true);
});
set_configurable(true);
apply_styles();
apply_config(cfg);
}
QJsonObject OrderBookMiniWidget::config() const {
QJsonObject o;
if (!account_id_.isEmpty())
o.insert("account_id", account_id_);
return o;
}
void OrderBookMiniWidget::apply_config(const QJsonObject& cfg) {
account_id_ = cfg.value("account_id").toString();
if (account_id_.isEmpty())
account_id_ = resolve_account_id();
broker_id_.clear();
if (!account_id_.isEmpty()) {
const auto acct = trading::AccountManager::instance().get_account(account_id_);
broker_id_ = acct.broker_id;
header_hint_->setText(acct.display_name.isEmpty() ? account_id_ : acct.display_name);
} else {
header_hint_->setText(tr("No active account — click gear to configure"));
}
if (isVisible() && !broker_id_.isEmpty() && !account_id_.isEmpty()) {
ensure_stream_running();
hub_resubscribe();
}
}
QString OrderBookMiniWidget::resolve_account_id() const {
const auto active = trading::AccountManager::instance().active_accounts();
return active.isEmpty() ? QString() : active.first().account_id;
}
void OrderBookMiniWidget::ensure_stream_running() {
auto& mgr = trading::DataStreamManager::instance();
if (!mgr.has_stream(account_id_))
mgr.start_stream(account_id_);
}
bool OrderBookMiniWidget::is_working(const QString& status) {
const QString s = status.toUpper();
return s != "COMPLETE" && s != "COMPLETED" && s != "FILLED" && s != "CANCELLED" && s != "CANCELED" &&
s != "REJECTED";
}
void OrderBookMiniWidget::hub_resubscribe() {
auto& hub = datahub::DataHub::instance();
hub.unsubscribe(this);
if (broker_id_.isEmpty() || account_id_.isEmpty())
return;
const QString topic = trading::broker_topic(broker_id_, account_id_, QStringLiteral("orders"));
if (table_->rowCount() == 0)
set_loading(true); // see OpenPositionsWidget::hub_resubscribe
hub.subscribe(this, topic, [this](const QVariant& v) {
if (!v.canConvert<QVector<trading::BrokerOrderInfo>>())
return;
populate(v.value<QVector<trading::BrokerOrderInfo>>());
});
hub_active_ = true;
}
void OrderBookMiniWidget::hub_unsubscribe_all() {
datahub::DataHub::instance().unsubscribe(this);
hub_active_ = false;
}
void OrderBookMiniWidget::showEvent(QShowEvent* e) {
BaseWidget::showEvent(e);
if (!broker_id_.isEmpty() && !account_id_.isEmpty()) {
ensure_stream_running();
if (!hub_active_)
hub_resubscribe();
}
}
void OrderBookMiniWidget::hideEvent(QHideEvent* e) {
BaseWidget::hideEvent(e);
if (hub_active_)
hub_unsubscribe_all();
}
void OrderBookMiniWidget::populate(const QVector<trading::BrokerOrderInfo>& rows) {
QVector<trading::BrokerOrderInfo> working;
working.reserve(rows.size());
for (const auto& o : rows) {
if (is_working(o.status))
working.append(o);
}
// Cell items and cancel buttons are reused between pushes instead of being
// re-created for every row on every push. Switching between the empty-state
// message and order rows starts from clean cells (this also drops row 0's
// stale cells / cancel button that used to linger under the message span).
const bool want_empty = working.isEmpty();
if (want_empty != showing_empty_) {
table_->clearContents();
showing_empty_ = want_empty;
}
auto cell = [this](int row, int col) {
QTableWidgetItem* item = table_->item(row, col);
if (!item) {
item = new QTableWidgetItem;
table_->setItem(row, col, item);
}
return item;
};
// Empty state — an account with nothing working used to render a blank
// grid that is indistinguishable from "still loading" / "broken".
table_->clearSpans();
if (want_empty) {
table_->setRowCount(1);
auto* msg = cell(0, 0);
msg->setText(tr("No working orders"));
msg->setTextAlignment(Qt::AlignCenter);
msg->setForeground(QColor(ui::colors::TEXT_TERTIARY()));
table_->setSpan(0, 0, 1, table_->columnCount());
set_loading(false);
return;
}
table_->setRowCount(static_cast<int>(working.size()));
const QColor buy_color(ui::colors::POSITIVE());
const QColor sell_color(ui::colors::NEGATIVE());
for (int i = 0; i < working.size(); ++i) {
const auto& o = working[i];
cell(i, 0)->setText(o.symbol);
auto* side = cell(i, 1);
side->setText(o.side);
side->setForeground(o.side.compare("BUY", Qt::CaseInsensitive) == 0 ? buy_color : sell_color);
auto* qty = cell(i, 2);
qty->setText(QString::number(o.quantity, 'f', 0));
qty->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter);
auto* price = cell(i, 3);
price->setText(o.price > 0 ? QString::number(o.price, 'f', 2) : tr("MKT"));
price->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter);
cell(i, 4)->setText(o.status);
// Styling comes from the table-level stylesheet (see apply_styles) via
// the `obCancel` dynamic property — a per-row setStyleSheet here meant
// one full CSS reparse per working order on every order-book push.
auto* cancel_btn = qobject_cast<QPushButton*>(table_->cellWidget(i, 5));
if (!cancel_btn) {
cancel_btn = new QPushButton("×", table_);
cancel_btn->setProperty("obCancel", true);
cancel_btn->setCursor(Qt::PointingHandCursor);
cancel_btn->setFixedHeight(18);
// The row's order id is read at click time — the button is reused.
connect(cancel_btn, &QPushButton::clicked, this,
[this, cancel_btn]() { cancel_order(cancel_btn->property("order_id").toString()); });
table_->setCellWidget(i, 5, cancel_btn);
}
if (cancel_btn->property("order_id").toString() != o.order_id) {
cancel_btn->setProperty("order_id", o.order_id);
cancel_btn->setToolTip(tr("Cancel order %1").arg(o.order_id));
cancel_btn->setAccessibleName(tr("Cancel order %1").arg(o.order_id));
}
}
set_loading(false);
}
void OrderBookMiniWidget::cancel_order(const QString& order_id) {
if (account_id_.isEmpty() || order_id.isEmpty())
return;
if (QMessageBox::question(this, tr("Cancel Order"), tr("Cancel order %1?").arg(order_id),
QMessageBox::Yes | QMessageBox::No) != QMessageBox::Yes)
return;
// BrokerHttp blocking trap (P1) — always wrap live broker calls in QtConcurrent::run.
// Runs on the dedicated order pool so a busy global pool can't delay it, and
// completes even if this widget is closed meanwhile (the user confirmed it).
const QString acct_id = account_id_;
(void)QtConcurrent::run(&trading::order_thread_pool(), [acct_id, order_id]() {
trading::UnifiedTrading::instance().cancel_order(acct_id, order_id);
// Order stream will publish the updated order state via the hub;
// populate() repaints automatically.
});
}
QDialog* OrderBookMiniWidget::make_config_dialog(QWidget* parent) {
auto* dlg = new QDialog(parent);
dlg->setWindowTitle(tr("Configure — Working Orders"));
auto* form = new QFormLayout(dlg);
auto* combo = new QComboBox(dlg);
const auto accts = trading::AccountManager::instance().list_accounts();
for (const auto& a : accts) {
const QString label = a.display_name.isEmpty() ? (a.broker_id + " — " + a.account_id) : a.display_name;
combo->addItem(label, a.account_id);
if (a.account_id == account_id_)
combo->setCurrentIndex(combo->count() - 1);
}
form->addRow(tr("Broker account"), combo);
auto* buttons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, dlg);
form->addRow(buttons);
connect(buttons, &QDialogButtonBox::accepted, dlg, [this, dlg, combo]() {
const QString picked = combo->currentData().toString();
if (!picked.isEmpty() && picked != account_id_) {
QJsonObject cfg;
cfg.insert("account_id", picked);
apply_config(cfg);
emit config_changed(cfg);
}
dlg->accept();
});
connect(buttons, &QDialogButtonBox::rejected, dlg, &QDialog::reject);
return dlg;
}
void OrderBookMiniWidget::on_theme_changed() {
apply_styles();
}
void OrderBookMiniWidget::apply_styles() {
header_hint_->setStyleSheet(
QString("color:%1;font-size:9px;background:transparent;padding:2px 0;").arg(ui::colors::TEXT_TERTIARY()));
table_->setStyleSheet(
QString("QTableWidget{background:transparent;color:%1;gridline-color:%2;font-size:10px;border:none;}"
"QHeaderView::section{background:%3;color:%4;border:none;border-bottom:1px solid %2;"
"padding:2px 4px;font-size:9px;font-weight:bold;}"
"QTableWidget::item{padding:2px 4px;}"
"QPushButton[obCancel=\"true\"]{color:%4;background:transparent;border:1px solid %2;"
"border-radius:2px;font-size:11px;font-weight:bold;}"
"QPushButton[obCancel=\"true\"]:hover{color:%5;border-color:%5;}")
.arg(ui::colors::TEXT_PRIMARY(), ui::colors::BORDER_DIM(), ui::colors::BG_RAISED(),
ui::colors::TEXT_TERTIARY(), ui::colors::NEGATIVE()));
}
void OrderBookMiniWidget::retranslateUi() {
BaseWidget::retranslateUi();
set_title(tr("WORKING ORDERS"));
// Re-apply the actual strings (the old body called hub_resubscribe(),
// which rebuilds subscriptions and translates nothing).
if (table_)
table_->setHorizontalHeaderLabels({tr("Symbol"), tr("Side"), tr("Qty"), tr("Price"), tr("Status"), QString()});
if (header_hint_ && account_id_.isEmpty())
header_hint_->setText(tr("No active account — click gear to configure"));
}
} // namespace fincept::screens::widgets