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.

2.4 KB
Raw Blame
#pragma once
// IntradayOIChart — minute-resolution OI history for a chosen strike's CE
// and PE legs. Subscribes to two `oi:history:<broker>:<token>:<window>`
// topics while visible (showEvent/hideEvent); the hub scheduler re-requests
// them on the topic TTL (OISnapshotter flushes once per minute), so the chart
// owns no refresh timer (D3).
//
// Data shape per topic: `QVector<OISample>` ordered ascending by ts_minute.
// `OISample` carries oi, ltp, vol, iv — this chart shows oi only; the LTP
// could be added as a secondary axis in a later phase.

#include "services/options/OptionChainTypes.h"

#include <QChartView>
#include <QEvent>
#include <QPointer>
#include <QString>

class QDateTimeAxis;
class QLineSeries;
class QValueAxis;

namespace fincept::screens::fno {

class IntradayOIChart : public QChartView {
    Q_OBJECT
  public:
    explicit IntradayOIChart(QWidget* parent = nullptr);
    ~IntradayOIChart() override;

    /// Switch the chart to a new (broker, ce_token, pe_token) target.
    /// Unsubscribes any prior topics, kicks off a forced refresh, and
    /// subsequent OISnapshotter publishes feed the curve.
    void set_subscription(const QString& broker_id, qint64 ce_token, qint64 pe_token,
                          const QString& window = QStringLiteral("1d"));

    /// Detach all subscriptions and clear the chart.
    void clear_subscription();

  protected:
    void showEvent(QShowEvent* e) override;
    void hideEvent(QHideEvent* e) override;
    void changeEvent(QEvent* event) override;

  private:
    void retranslateUi();
    void on_history(qint64 token, const QVariant& v);
    void replot();
    /// Subscribe to / drop the current CE+PE history topics on the hub. The pair is
    /// only live while the chart is visible; set_subscription() just records the
    /// target and attaches immediately when already shown.
    void attach_topics();
    void detach_topics();

    QChart* chart_ = nullptr;
    QLineSeries* ce_series_ = nullptr;
    QLineSeries* pe_series_ = nullptr;
    QDateTimeAxis* axis_x_ = nullptr;
    QValueAxis* axis_y_ = nullptr;

    QString broker_id_;
    qint64 ce_token_ = 0;
    qint64 pe_token_ = 0;
    QString window_ = QStringLiteral("1d");
    QString ce_topic_, pe_topic_;
    bool attached_ = false;

    QVector<fincept::services::options::OISample> ce_samples_;
    QVector<fincept::services::options::OISample> pe_samples_;
};

} // namespace fincept::screens::fno