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
// src/screens/economics/panels/BeaPanel.cpp
// BEA (Bureau of Economic Analysis) — requires BEA_API_KEY env var.
//
// Uses the frontend integration command:
// fetch <indicator_id> <start_year>-01-01 <end_year>-12-31
// Response: { "success": true,
// "data": [{"date": "2022", "value": 123.4}],
// "metadata": {"indicator": "gdp_growth", "indicator_name": "Real GDP Growth...",
// "country": "United States", "source": "BEA NIPA",
// "table": "T10101", "line": "1"} }
//
// 27 indicators mapped across 5 categories:
// GDP: gdp_growth, nominal_gdp, real_gdp, gdp_deflator, gdp_per_capita, gdi
// Consumption: pce, pce_goods, pce_services, gross_investment, fixed_investment
// Trade: net_exports, exports, imports, current_account
// Income: personal_income, compensation, wages_salaries, disposable_income,
// personal_saving, saving_rate
// Government: govt_spending, federal_spending, defense_spending,
// govt_receipts, personal_taxes, corporate_taxes, gross_saving
#include "screens/economics/panels/BeaPanel.h"
#include "core/logging/Logger.h"
#include "services/economics/EconomicsService.h"
#include <QDate>
#include <QHBoxLayout>
#include <QJsonArray>
#include <QJsonObject>
#include <QLabel>
#include <QSplitter>
#include <QVBoxLayout>
namespace fincept::screens {
namespace {
static constexpr const char* kBeaScript = "bea_data.py";
static constexpr const char* kBeaSourceId = "bea";
static constexpr const char* kBeaColor = "#E65100"; // deep orange
struct BeaIndicatorDef {
QString name;
QString id;
QString unit;
};
// ── GDP & Output ──────────────────────────────────────────────────────────────
static const QList<BeaIndicatorDef> kGdpIndicators = {
{"Real GDP Growth (% Change)", "gdp_growth", "% change"},
{"Nominal GDP (Billions $)", "nominal_gdp", "Billions $"},
{"Real GDP (Chained 2017 $)", "real_gdp", "Billions $"},
{"GDP Price Index", "gdp_deflator", "Index"},
{"GDP Price Change (%)", "gdp_price_change", "% change"},
{"GDP per Capita (Current $)", "gdp_per_capita", "$ per capita"},
{"Gross Domestic Income (Billions $)", "gdi", "Billions $"},
};
// ── Consumption & Investment ──────────────────────────────────────────────────
static const QList<BeaIndicatorDef> kConsumptionIndicators = {
{"Personal Consumption Expenditures", "pce", "Billions $"},
{"PCE — Goods", "pce_goods", "Billions $"},
{"PCE — Services", "pce_services", "Billions $"},
{"Gross Private Domestic Investment", "gross_investment", "Billions $"},
{"Fixed Investment", "fixed_investment", "Billions $"},
};
// ── Trade & External ─────────────────────────────────────────────────────────
static const QList<BeaIndicatorDef> kTradeIndicators = {
{"Net Exports (Billions $)", "net_exports", "Billions $"},
{"Exports of Goods & Services", "exports", "Billions $"},
{"Imports of Goods & Services", "imports", "Billions $"},
{"Current Account Balance", "current_account", "Billions $"},
};
// ── Income & Saving ───────────────────────────────────────────────────────────
static const QList<BeaIndicatorDef> kIncomeIndicators = {
{"Personal Income (Billions $)", "personal_income", "Billions $"},
{"Compensation of Employees", "compensation", "Billions $"},
{"Wages and Salaries", "wages_salaries", "Billions $"},
{"Disposable Personal Income", "disposable_income", "Billions $"},
{"Personal Saving (Billions $)", "personal_saving", "Billions $"},
{"Personal Saving Rate (%)", "saving_rate", "%"},
{"PCE Inflation (% Change)", "pce_inflation", "% change"},
{"Core PCE Inflation (ex F&E)", "core_pce_inflation", "% change"},
};
// ── Government ────────────────────────────────────────────────────────────────
static const QList<BeaIndicatorDef> kGovtIndicators = {
{"Government Spending (Billions $)", "govt_spending", "Billions $"},
{"Federal Government Spending", "federal_spending", "Billions $"},
{"National Defense Spending", "defense_spending", "Billions $"},
{"Government Current Receipts", "govt_receipts", "Billions $"},
{"Personal Current Taxes", "personal_taxes", "Billions $"},
{"Taxes on Corporate Income", "corporate_taxes", "Billions $"},
{"Gross Saving (Billions $)", "gross_saving", "Billions $"},
};
struct BeaCategoryDef {
QString label;
const QList<BeaIndicatorDef>* indicators;
};
static const QList<BeaCategoryDef> kBeaCategories = {
{"GDP & Output", &kGdpIndicators}, {"Consumption & Investment", &kConsumptionIndicators},
{"Trade & External", &kTradeIndicators}, {"Income & Saving", &kIncomeIndicators},
{"Government", &kGovtIndicators},
};
} // namespace
// ── Constructor ───────────────────────────────────────────────────────────────
BeaPanel::BeaPanel(QWidget* parent) : EconPanelBase(kBeaSourceId, kBeaColor, parent) {
// Outer layout: left selector | right base UI
auto* main = new QHBoxLayout(this);
main->setContentsMargins(0, 0, 0, 0);
main->setSpacing(0);
// ── Left: category + indicator selector ──────────────────────────────────
auto* left = new QWidget(this);
left->setFixedWidth(210);
left->setStyleSheet(sidebar_style());
auto* lvl = new QVBoxLayout(left);
lvl->setContentsMargins(0, 0, 0, 0);
lvl->setSpacing(0);
// Category header
auto* cat_hdr = new QWidget(this);
cat_hdr->setStyleSheet(section_hdr_style());
cat_hdr->setFixedHeight(32);
auto* chl = new QHBoxLayout(cat_hdr);
chl->setContentsMargins(8, 0, 8, 0);
category_lbl_ = new QLabel(tr("CATEGORY"));
category_lbl_->setStyleSheet(ctrl_label_style()); // sidebar: outside the panel_style() container
category_combo_ = new QComboBox;
for (const auto& c : kBeaCategories)
category_combo_->addItem(c.label);
category_combo_->setFixedHeight(22);
connect(category_combo_, &QComboBox::currentIndexChanged, this, &BeaPanel::on_category_changed);
chl->addWidget(category_lbl_);
chl->addWidget(category_combo_, 1);
lvl->addWidget(cat_hdr);
// Indicator search
indicator_search_ = new QLineEdit;
indicator_search_->setPlaceholderText(tr("Filter indicators…"));
indicator_search_->setStyleSheet(search_input_style());
indicator_search_->setFixedHeight(28);
connect(indicator_search_, &QLineEdit::textChanged, this, &BeaPanel::on_indicator_filter);
lvl->addWidget(indicator_search_);
// Indicator list
indicator_list_ = new QListWidget;
indicator_list_->setStyleSheet(list_style());
indicator_list_->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
lvl->addWidget(indicator_list_, 1);
main->addWidget(left);
// ── Right: base UI ────────────────────────────────────────────────────────
auto* right = new QWidget(this);
right->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
build_base_ui(right);
main->addWidget(right, 1);
// Populate initial indicator list (GDP category)
on_category_changed(0);
connect(&services::EconomicsService::instance(), &services::EconomicsService::result_ready, this,
&BeaPanel::on_result);
// Localize fixed category-combo item text to the active language.
retranslateUi();
}
void BeaPanel::activate() {
show_empty(tr("Select a category and indicator, then click FETCH\n"
"Requires BEA_API_KEY — free at: www.bea.gov/data/api/register\n"
"All data is US national accounts (NIPA)"));
}
// ── Controls ──────────────────────────────────────────────────────────────────
void BeaPanel::build_controls(QHBoxLayout* thl) {
auto make_lbl = [](const QString& text) {
auto* l = new QLabel(text);
l->setObjectName(QStringLiteral("econCtrlLabel"));
return l;
};
start_input_ = new QLineEdit;
start_input_->setPlaceholderText(tr("Start year"));
start_input_->setText("2000");
start_input_->setFixedHeight(26);
start_input_->setFixedWidth(75);
end_input_ = new QLineEdit;
end_input_->setPlaceholderText(tr("End year"));
end_input_->setText(QString::number(QDate::currentDate().year()));
end_input_->setFixedHeight(26);
end_input_->setFixedWidth(75);
thl->addWidget(from_lbl_ = make_lbl(tr("FROM")));
thl->addWidget(start_input_);
thl->addWidget(to_lbl_ = make_lbl(tr("TO")));
thl->addWidget(end_input_);
// API key notice
notice_lbl_ = new QLabel(tr("Requires BEA_API_KEY"));
notice_lbl_->setStyleSheet(notice_style());
thl->addWidget(notice_lbl_);
}
void BeaPanel::on_category_changed(int index) {
if (index < 0 || index >= kBeaCategories.size())
return;
indicator_list_->clear();
indicator_search_->clear();
current_indicators_.clear();
const auto& inds = *kBeaCategories[index].indicators;
for (const auto& ind : inds) {
current_indicators_.append({ind.name, ind.id, ind.unit});
auto* item = new QListWidgetItem(ind.name + " [" + ind.unit + "]");
item->setData(Qt::UserRole, ind.id);
item->setToolTip(ind.id + " — " + ind.unit);
indicator_list_->addItem(item);
}
if (indicator_list_->count() > 0)
indicator_list_->setCurrentRow(0);
}
void BeaPanel::on_indicator_filter(const QString& text) {
for (int i = 0; i < indicator_list_->count(); ++i) {
auto* item = indicator_list_->item(i);
item->setHidden(!text.isEmpty() && !item->text().contains(text, Qt::CaseInsensitive) &&
!item->data(Qt::UserRole).toString().contains(text, Qt::CaseInsensitive));
}
}
// ── Fetch ─────────────────────────────────────────────────────────────────────
void BeaPanel::on_fetch() {
const auto* sel = indicator_list_->currentItem();
if (!sel) {
show_empty(tr("Select an indicator from the list"));
return;
}
const QString indicator_id = sel->data(Qt::UserRole).toString();
const QString start = start_input_->text().trimmed();
const QString end = end_input_->text().trimmed();
if (start.isEmpty() || end.isEmpty()) {
show_empty(tr("Enter start and end years"));
return;
}
// CLI: fetch <indicator_id> <start_year>-01-01 <end_year>-12-31
show_loading(tr("Fetching BEA: %1…").arg(sel->text().split(" [").first()));
services::EconomicsService::instance().execute(kBeaSourceId, kBeaScript, "fetch",
{indicator_id, start + "-01-01", end + "-12-31"},
"bea_fetch_" + indicator_id);
}
// ── Result ────────────────────────────────────────────────────────────────────
void BeaPanel::on_result(const QString& request_id, const services::EconomicsResult& result) {
if (result.source_id != kBeaSourceId)
return;
if (!request_id.startsWith("bea_fetch_"))
return;
if (!result.success) {
if (result.error.contains("API key") || result.error.contains("UserID") || result.error.contains("register")) {
show_error(tr("BEA API key not configured.\n"
"Set BEA_API_KEY environment variable.\n"
"Free registration at: www.bea.gov/data/api/register"));
} else {
show_error(result.error);
}
return;
}
// Response: { "success": true, "data": [{"date": "2022", "value": 123.4}],
// "metadata": {"indicator_name": "...", "source": "BEA NIPA"} }
QJsonArray rows = result.data["data"].toArray();
if (rows.isEmpty()) {
show_empty(tr("No data returned — check API key and year range"));
return;
}
const QJsonObject meta = result.data["metadata"].toObject();
const QString ind_name = meta["indicator_name"].toString(
indicator_list_->currentItem() ? indicator_list_->currentItem()->text().split(" [").first() : tr("Indicator"));
display(rows, "BEA: " + ind_name);
LOG_INFO("BeaPanel", QString("Displayed %1 data points for %2").arg(rows.size()).arg(request_id));
}
// ── i18n ──────────────────────────────────────────────────────────────────────
void BeaPanel::changeEvent(QEvent* event) {
if (event->type() == QEvent::LanguageChange)
retranslateUi();
EconPanelBase::changeEvent(event);
}
void BeaPanel::retranslateUi() {
if (category_lbl_)
category_lbl_->setText(tr("CATEGORY"));
// Category names are fixed UI labels (order matches kBeaCategories).
if (category_combo_ && category_combo_->count() >= 5) {
category_combo_->setItemText(0, tr("GDP & Output"));
category_combo_->setItemText(1, tr("Consumption & Investment"));
category_combo_->setItemText(2, tr("Trade & External"));
category_combo_->setItemText(3, tr("Income & Saving"));
category_combo_->setItemText(4, tr("Government"));
}
if (indicator_search_)
indicator_search_->setPlaceholderText(tr("Filter indicators…"));
if (from_lbl_)
from_lbl_->setText(tr("FROM"));
if (to_lbl_)
to_lbl_->setText(tr("TO"));
if (start_input_)
start_input_->setPlaceholderText(tr("Start year"));
if (end_input_)
end_input_->setPlaceholderText(tr("End year"));
if (notice_lbl_)
notice_lbl_->setText(tr("Requires BEA_API_KEY"));
EconPanelBase::retranslateUi();
}
} // namespace fincept::screens