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.

4.9 KB
Raw Blame
#include "services/portfolio/PortfolioAnalyticsService.h"

#include "core/logging/Logger.h"
#include "python/PythonRunner.h"

#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>

namespace fincept::services {

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

static QJsonArray portfolio_analytics_to_json_array(const QList<double>& xs) {
    QJsonArray a;
    for (double x : xs)
        a.append(x);
    return a;
}

void PortfolioAnalyticsService::run_quantstats(const QStringList& symbols, const QList<double>& weights,
                                               double risk_free_rate, const QString& benchmark, AnalyticsCallback cb) {
    QJsonObject args;
    args["symbols"] = QJsonArray::fromStringList(symbols);
    args["weights"] = portfolio_analytics_to_json_array(weights);
    args["risk_free_rate"] = risk_free_rate;
    if (!benchmark.isEmpty())
        args["benchmark"] = benchmark;
    // quantstats_analysis.py takes its input on stdin ONLY. Handing it the JSON as
    // argv (as this used to) left it blocked in sys.stdin.read() on a pipe nobody
    // ever closed until the 5-minute watchdog killed it.
    run_script(QStringLiteral("quantstats_analysis"),
               QString::fromUtf8(QJsonDocument(args).toJson(QJsonDocument::Compact)), std::move(cb),
               /*via_stdin=*/true);
}

void PortfolioAnalyticsService::run_monte_carlo(const QStringList& symbols, const QList<double>& weights,
                                                int num_simulations, AnalyticsCallback cb) {
    QJsonObject args;
    args["symbols"] = QJsonArray::fromStringList(symbols);
    args["weights"] = portfolio_analytics_to_json_array(weights);
    args["num_simulations"] = num_simulations;
    run_script(QStringLiteral("quantstats_monte_carlo"),
               QString::fromUtf8(QJsonDocument(args).toJson(QJsonDocument::Compact)), std::move(cb),
               /*via_stdin=*/true); // stdin-only script, see run_quantstats()
}

void PortfolioAnalyticsService::optimize_weights(const QString& args_json, AnalyticsCallback cb) {
    run_script(QStringLiteral("optimize_portfolio_weights"), args_json, std::move(cb));
}

void PortfolioAnalyticsService::run_ffn(const QStringList& symbols, const QJsonObject& weights_by_symbol,
                                        double risk_free_rate, AnalyticsCallback cb) {
    QJsonObject args;
    args["symbols"] = QJsonArray::fromStringList(symbols);
    args["weights"] = weights_by_symbol;
    args["risk_free_rate"] = risk_free_rate;
    run_script(QStringLiteral("ffn_analysis"), QString::fromUtf8(QJsonDocument(args).toJson(QJsonDocument::Compact)),
               std::move(cb), /*via_stdin=*/true); // stdin-only script, see run_quantstats()
}

void PortfolioAnalyticsService::run_script(const QString& script, const QString& args_json, AnalyticsCallback cb,
                                           bool via_stdin) {
    // PythonRunner::run() resolves scripts_dir/<name> verbatim — the name must
    // include the ".py" extension (as e.g. the yfinance_data.py daemon path
    // does). These analytics callers pass the bare stem, so normalise here;
    // otherwise the existence check fails with "Script not found".
    const QString script_file = script.endsWith(QLatin1String(".py")) ? script : script + ".py";
    fincept::python::PythonRunner::instance().run(
        script_file, via_stdin ? QStringList{} : QStringList{args_json},
        [cb = std::move(cb), script = script_file](const fincept::python::PythonResult& result) {
            AnalyticsResult out;
            if (!result.success || result.output.trimmed().isEmpty()) {
                out.error =
                    result.error.isEmpty() ? QStringLiteral("%1 returned empty output").arg(script) : result.error;
                LOG_ERROR("PortfolioAnalyticsService",
                          QStringLiteral("%1 failed: %2").arg(script, out.error.left(300)));
                if (cb)
                    cb(out);
                return;
            }
            const auto doc = QJsonDocument::fromJson(result.output.trimmed().toUtf8());
            if (!doc.isObject()) {
                out.error = QStringLiteral("%1 returned malformed JSON").arg(script);
                LOG_ERROR("PortfolioAnalyticsService",
                          QStringLiteral("%1: bad JSON: %2").arg(script, result.output.left(200)));
                if (cb)
                    cb(out);
                return;
            }
            const QJsonObject root = doc.object();
            if (root.contains("error")) {
                out.error = root.value("error").toString();
                if (cb)
                    cb(out);
                return;
            }
            out.success = true;
            out.data = root;
            if (cb)
                cb(out);
        },
        /*on_line=*/{}, via_stdin ? args_json.toUtf8() : QByteArray());
}

} // namespace fincept::services