Files
QTradeProgram/Sqbase/qorderlistmanager.h
2025-08-15 15:56:40 +08:00

88 lines
2.4 KiB
C++

#ifndef QORDERLISTMANAGER_H
#define QORDERLISTMANAGER_H
#include <QObject>
#include <QMap>
#include <QMutex>
#include <QSharedPointer>
#include <QDateTime>
struct OrderBookItem {
double price = 0.0;
double volume = 0.0;
int orderCount = 0;
QString orderId;
bool isValid() const {
return price > 0 && volume > 0 && !orderId.isEmpty();
}
bool operator==(const OrderBookItem& other) const {
return orderId == other.orderId;
}
};
class QOrderListManager : public QObject
{
Q_OBJECT
public:
enum OrderType {
BidOrder, // 买单
AskOrder // 卖单
};
Q_ENUM(OrderType)
static QOrderListManager* instance();
static void destroy();
// 订单簿操作
bool addOrder(OrderType type, const OrderBookItem& order);
bool updateOrder(OrderType type, const QString& orderId, const OrderBookItem& newData);
bool removeOrder(OrderType type, const QString& orderId);
QSharedPointer<OrderBookItem> getOrder(OrderType type, const QString& orderId) const;
// 获取订单簿数据
QMap<double, QList<QSharedPointer<OrderBookItem>>> getBidOrders() const;
QMap<double, QList<QSharedPointer<OrderBookItem>>> getAskOrders() const;
// 聚合数据
QMap<double, double> getBidPriceLevels() const; // 价格 -> 总量
QMap<double, double> getAskPriceLevels() const;
// 统计信息
double getTotalBidVolume() const;
double getTotalAskVolume() const;
double getBestBidPrice() const;
double getBestAskPrice() const;
signals:
void orderAdded(OrderType type, const OrderBookItem& order);
void orderUpdated(OrderType type, const QString& orderId);
void orderRemoved(OrderType type, const QString& orderId);
void orderBookChanged();
private:
explicit QOrderListManager(QObject* parent = nullptr);
~QOrderListManager();
void sortOrders(OrderType type);
void calculatePriceLevels();
static QBasicAtomicPointer<QOrderListManager> s_instance;
static QMutex s_mutex;
// 买单和卖单分别存储,按价格排序
QMap<double, QList<QSharedPointer<OrderBookItem>>> m_bidOrders;
QMap<double, QList<QSharedPointer<OrderBookItem>>> m_askOrders;
// 价格层级聚合数据
QMap<double, double> m_bidPriceLevels;
QMap<double, double> m_askPriceLevels;
mutable QMutex m_orderMutex;
Q_DISABLE_COPY(QOrderListManager)
};
#endif // QORDERLISTMANAGER_H