Developing a Simple C++ Scalper Using FAST/FIX: Step-by-Step Guide
Hello, friends! Today, I'll show you how to build your first trading scalper bot in C++ using the FAST/FIX protocols. Many believe that algorithmic trading is something extremely complex, accessible only to elite programmers with hedge fund experience. But let's be honest: you can start small, especially if you already have access to an exchange via FAST/FIX.
What Do We Need?
Before we dive into the code, let's prepare our toolkit:
- C++ compiler (GCC, Clang, or MSVC)
- Library for working with FIX/FAST (e.g., OnixS C++ FIX Engine)
- A convenient IDE (Visual Studio, CLion, or your favorite)
- A cup of strong coffee (optional, but highly recommended)
Understanding FIX/FAST Protocols
If you already have access to an exchange via FAST/FIX, you probably know that:
FIX (Financial Information eXchange) is a standard protocol for exchanging financial information. Imagine telegrams like "BUY BTCUSD QTY=1 PRICE=50000", but in a more formalized format.
FAST (FIX Adapted for Streaming) is a "turbo version" of FIX, optimized for high-speed data transfer. If FIX is regular mail, FAST is pneumatic mail on steroids.
Our Scalper's Architecture
Any self-respecting scalper should have the following components:
- Exchange connection module – our bridge to the world of big money
- Market data processing module – our "eyes and ears"
- Decision-making module – our "brain"
- Order management module – our "hands"
- Risk management module – our "self-preservation instinct"
Let's sketch the skeleton of our future bot:
#include <iostream>
#include <string>
#include <onixs/fix/engine.h> // Presumed library for FIX
class SimpleScalper {
private:
// Connection parameters
std::string senderCompID;
std::string targetCompID;
std::string fixVersion;
// Trading parameters
double stopLossPercentage = 0.05; // 5% stop-loss
double profitTargetPercentage = 0.1; // 10% profit target
double tradeAmount = 0.1; // Position size
// Trading state
bool inPosition = false;
double entryPrice = 0.0;
// FIX connection
onixs::fix::Session* session;
public:
SimpleScalper(const std::string& sender, const std::string& target, const std::string& version)
: senderCompID(sender), targetCompID(target), fixVersion(version) {
// Initialize FIX connection
initializeFixConnection();
}
// Other methods...
};
Connecting to the Exchange
First, we need to establish a connection to the exchange. This is like a first handshake – it's important not to mess it up:
void SimpleScalper::initializeFixConnection() {
// Create session configuration
onixs::fix::SessionConfig config;
config.setSenderCompID(senderCompID);
config.setTargetCompID(targetCompID);
config.setFixVersion(fixVersion);
// Set connection parameters
config.setHost("exchange.hostname.com");
config.setPort(9823); // Exchange FIX port
// Create and initialize session
session = new onixs::fix::Session(config);
// Register event handlers
session->setMessageHandler(std::bind(&SimpleScalper::onMessage, this, std::placeholders::_1));
// Establish connection
if (!session->connect()) {
std::cerr << "Failed to connect to the exchange!" << std::endl;
} else {
std::cout << "Connection established!" << std::endl;
}
}
Market Data Processing and Decision Making
For simplicity, let's implement an RSI indicator and basic entry/exit logic:
double calculateRSI(const std::vector<double>& prices, int period = 14) {
if (prices.size() < period + 1) return 50.0;
double sumGain = 0.0, sumLoss = 0.0;
for (size_t i = prices.size() - period; i < prices.size() - 1; ++i) {
double change = prices[i + 1] - prices[i];
if (change > 0) sumGain += change;
else sumLoss -= change;
}
if (sumLoss == 0) return 100.0;
double rs = sumGain / sumLoss;
return 100.0 - (100.0 / (1.0 + rs));
}
Sending Orders to the Exchange
When our strategy says "buy" or "sell", we need to send the corresponding order to the exchange. It's like placing an order at a restaurant – you need to be precise:
void SimpleScalper::sendBuyOrder(double amount, double price) {
std::cout << "Sending BUY order: " << amount << " @ " << price << std::endl;
// Here we form and send a FIX NewOrderSingle
// ...
}
Handling Exchange Responses
void SimpleScalper::onMessage(const onixs::fix::Message& message) {
std::string msgType = message.getField(onixs::fix::Tag::MsgType);
if (msgType == "8") { // ExecutionReport
// Handle execution report
} else if (msgType == "W") { // MarketDataSnapshotFullRefresh
// Handle market data
} else if (msgType == "3") { // Reject
std::string reason = message.getField(onixs::fix::Tag::Text);
std::cout << "Order rejected: " << reason << std::endl;
}
}
Testing and Debugging
Before running our bot with real money, let's test it on historical data or in simulation mode. This is like a pilot training on a simulator before a real flight:
void testScalperOnHistoricalData() {
SimpleScalper scalper("TESTER", "EXCHANGE", "FIX.4.4");
std::vector<double> historicalPrices = loadHistoricalData("BTCUSD_1min_2023.csv");
for (const auto& price : historicalPrices) {
// emulate market data
// scalper.processMarketData(...);
}
// scalper.printPerformanceStats();
}
Conclusion
That's it! We've built a simple yet functional C++ scalper using FAST/FIX protocols. Of course, this is just the beginning. Real HFT systems are much more complex and optimized, but even such a simple bot can be useful for a beginner algorithmic trader.
Remember, trading on financial markets involves risks, and algorithmic trading adds technical risks on top. Always test your strategies on historical data and in simulation mode before going live.
And most importantly – keep learning and improving your bot. Markets change, and your algorithms must adapt with them.
Happy trading, and may your scalper always be profitable!
Citation
@software{soloviov2024scalpercppfastfix,
author = {Soloviov, Eugen},
title = {Developing a Simple C++ Scalper Using FAST/FIX: Step-by-Step Guide},
year = {2025},
url = {https://marketmaker.cc/en/blog/post/scalper-cpp-fast-fix},
version = {0.1.0},
description = {A step-by-step guide to building a C++ trading scalper bot using FAST/FIX protocols.}
}
MarketMaker.cc Team
Investigación Cuantitativa y Estrategia