#include <iostream> // Often needed for production log/error handling, kept for completeness but not strictly part of signal calc
// Enum to represent the type of trading signal generated.
enum class SignalType {
NONE, // No signal generated.
BUY, // Signal to buy.
SELL // Signal to sell.
};
// Structure to hold the necessary market data for the signal calculation.
// In a real HFT system, this would likely be a complex snapshot of the order book.
struct MarketData {
double bidPrice; // Best Bid Price
double askPrice; // Best Ask Price
long bidVolume; // Volume at Best Bid
long askVolume; // Volume at Best Ask
// For a more robust signal, you might include multiple price levels,
// total depth, number of orders, or a timestamp.
};
// A simple High-Frequency Trading signal based on Order Book Imbalance (OBI)
// of the Best Bid and Offer volumes.
// This is a basic example; real HFT signals are often much more complex,
// incorporating more data, more complex algorithms, and machine learning.
class OrderBookImbalanceSignal {
public:
// Constructor to initialize the thresholds for generating BUY/SELL signals.
// These thresholds define how significant the imbalance must be.
// Example: buyThreshold = 1.5 means BUY if bidVolume >= 1.5 * askVolume.
// Example: sellThreshold = 0.66 means SELL if bidVolume <= 0.66 * askVolume
// (equivalently, askVolume >= 1.5 * bidVolume).
OrderBookImbalanceSignal(double buyThreshold = 1.5, double sellThreshold = 0.66)
: buyThreshold_(buyThreshold), sellThreshold_(sellThreshold) {
// Basic validation for thresholds. Add more sophisticated checks as needed.
if (buyThreshold_ <= 1.0 || sellThreshold_ >= 1.0 || sellThreshold_ <= 0.0) {
// In a real system, log an error or throw an exception.
// For this example, we'll just print a warning and ensure valid range.
std::cerr << "Warning: Invalid OBI thresholds provided. Adjusting." << std::endl;
if (buyThreshold_ <= 1.0) buyThreshold_ = 1.5;
if (sellThreshold_ >= 1.0 || sellThreshold_ <= 0.0) sellThreshold_ = 0.66; // Ensure it's between 0 and 1 (exclusive of 1)
}
}
// Generates a trading signal based on the provided current market data.
// This method is designed to be called upon receiving new market data (e.g., a tick update or order book update).
// The core logic is computationally minimal to ensure low latency.
SignalType generateSignal(const MarketData& data) {
// Avoid division by zero or attempting to compute ratio with zero volume on either side.
// In low-liquidity situations, this might often return NONE.
if (data.bidVolume <= 0 || data.askVolume <= 0) {
return SignalType::NONE;
}
// Calculate the Order Book Imbalance ratio: Bid Volume / Ask Volume
// Cast to double to ensure floating-point division.
double imbalanceRatio = static_cast<double>(data.bidVolume) / data.askVolume;
// Apply the defined thresholds to generate a signal.
if (imbalanceRatio >= buyThreshold_) {
return SignalType::BUY; // Bullish imbalance (more buy pressure)
} else if (imbalanceRatio <= sellThreshold_) {
return SignalType::SELL; // Bearish imbalance (more sell pressure)
} else {
return SignalType::NONE; // Imbalance is within the neutral range
}
}
private:
double buyThreshold_; // Threshold for generating a BUY signal (ratio >= threshold)
double sellThreshold_; // Threshold for generating a SELL signal (ratio <= threshold)
};
/*
// Example Usage (Optional - for demonstration purposes, not part of the required output)
int main() {
// Create a signal instance with custom thresholds
OrderBookImbalanceSignal hftSignal(2.0, 0.5);
// Simulate receiving market data updates
MarketData data1 = {100.0, 100.1, 500, 200}; // Bid volume significantly higher
MarketData data2 = {100.0, 100.1, 200, 500}; // Ask volume significantly higher
MarketData data3 = {100.0, 100.1, 300, 350}; // Relatively balanced
MarketData data4 = {100.0, 100.1, 0, 100}; // Zero bid volume
MarketData data5 = {100.0, 100.1, 100, 0}; // Zero ask volume
std::cout << "Data 1 (500/200=2.5) Signal: ";
switch (hftSignal.generateSignal(data1)) {
case SignalType::BUY: std::cout << "BUY"; break;
case SignalType::SELL: std::cout << "SELL"; break;
case SignalType::NONE: std::cout << "NONE"; break;
}
std::cout << std::endl;
std::cout << "Data 2 (200/500=0.4) Signal: ";
switch (hftSignal.generateSignal(data2)) {
case SignalType::BUY: std::cout << "BUY"; break;
case SignalType::SELL: std::cout << "SELL"; break;
case SignalType::NONE: std::cout << "NONE"; break;
}
std::cout << std::endl;
std::cout << "Data 3 (300/350=0.85) Signal: ";
switch (hftSignal.generateSignal(data3)) {
case SignalType::BUY: std::cout << "BUY"; break;
case SignalType::SELL: std::cout << "SELL"; break;
case SignalType::NONE: std::cout << "NONE"; break;
}
std::cout << std::endl;
std::cout << "Data 4 (0/100) Signal: ";
switch (hftSignal.generateSignal(data4)) {
case SignalType::BUY: std::cout << "BUY"; break;
case SignalType::SELL: std::cout << "SELL"; break;
case SignalType::NONE: std::cout << "NONE"; break;
}
std::cout << std::endl;
std::cout << "Data 5 (100/0) Signal: ";
switch (hftSignal.generateSignal(data5)) {
case SignalType::BUY: std::cout << "BUY"; break;
case SignalType::SELL: std::cout << "SELL"; break;
case SignalType::NONE: std::cout << "NONE"; break;
}
std::cout << std::endl;
return 0;
}
*/
Brief Description:
This C++ code provides a basic High-Frequency Trading (HFT) signal generator based on Order Book Imbalance (OBI) using the volumes at the Best Bid and Best Ask prices. It calculates the ratio of Bid Volume to Ask Volume and generates a BUY signal if the ratio exceeds a defined buyThreshold (indicating strong buy-side pressure) or a SELL signal if the ratio falls below a sellThreshold (indicating strong sell-side pressure). The logic is kept simple and computationally efficient, making it suitable for quickly processing live market data updates typical in HFT environments. This is a simplified example; real-world HFT signals are considerably more complex and often involve deeper order book analysis, historical data, and machine learning.