22 #ifndef STRATEGIES_MEAN_REVERSION_HPP
23 #define STRATEGIES_MEAN_REVERSION_HPP
25 #include "data_feed/receiver.hpp"
26 #include "order_entry/client.hpp"
27 #include "maths/probability.hpp"
28 #include "maths/exponential_moving_variance.hpp"
29 #include <nlohmann/json.hpp>
50 asio::steady_timer timer;
56 std::atomic<bool> is_running;
66 inline void start_strategy() {
67 timer.expires_after(std::chrono::milliseconds(sleep_time));
68 timer.async_wait([
this](
const std::error_code& error) {
70 std::cout <<
"MeanReversion::start_strategy - " << error << std::endl;
85 double decision_boundary = deviations * midpoint.
get_stddev();
87 if (change >= decision_boundary) {
91 if (best_sell <= std::numeric_limits<OrderEntry::Price>::min() + 1)
return;
93 auto price = best_sell - 1;
97 else if (change <= -decision_boundary) {
101 if (best_buy >= std::numeric_limits<OrderEntry::Price>::max() - 1)
return;
103 auto price = best_buy + 1;
117 asio::io_context& feed_context,
118 asio::io_context& context,
119 nlohmann::json options
123 asio::ip::make_address(options[
"data_feed"][
"listen"].get<std::string>()),
124 asio::ip::make_address(options[
"data_feed"][
"group"].get<std::string>()),
125 options[
"data_feed"][
"port"].get<uint16_t>(),
130 options[
"order_entry"][
"host"].get<std::string>(),
131 std::to_string(options[
"order_entry"][
"port"].get<uint16_t>())
134 sleep_time(options[
"strategy"][
"sleep_time"].get<uint32_t>()),
135 P_act(options[
"strategy"][
"P_act"].get<double>()),
137 size(options[
"strategy"][
"size"].get<
OrderEntry::Quantity>()),
138 deviations(options[
"strategy"][
"deviations"].get<double>()),
139 midpoint(options[
"strategy"][
"weight"].get<double>(), options[
"strategy"][
"average"].get<double>()) {
153 std::cerr <<
"received start of session when already running" << std::endl;
168 if (not is_running) {
169 std::cerr <<
"received end of session when not running" << std::endl;
208 #endif // STRATEGIES_MEAN_REVERSION_HPP
Logic for sending/receiving application messages in a financial market.
Definition: authorizer.hpp:26
Price last_price() const
Return the current price of the asset using last prices.
Definition: limit_order_book.hpp:283
T get_stddev() const
Return the current value of the exponential moving standard deviation .
Definition: exponential_moving_variance.hpp:139
A request that indicates a client is attempting to create a new session.
Definition: messages.hpp:248
Password make_password(std::string password)
Make a password from the given input string.
Definition: messages.hpp:62
A message that indicates a market order matches with a limit order.
Definition: messages.hpp:387
bool has_active_order() const
Return true if the client has an active order.
Definition: client.hpp:267
void send(Args &&...args)
Write a message asynchronously (non-blocking).
Definition: client.hpp:299
Price last_best_sell() const
Return the last best sell price.
Definition: limit_order_book.hpp:262
A message that indicates a limit order was added to the book.
Definition: messages.hpp:332
A request to place a new limit / market order in the book.
Definition: messages.hpp:502
const LOB::LimitOrderBook & get_book()
Return the limit order book for this receiver.
Definition: receiver.hpp:214
void did_receive(DataFeedReceiver *receiver, const DataFeed::Messages::StartOfSession &message)
Handle a start of session message.
Definition: mean_reversion.hpp:151
A message that indicates the end of a trading session.
Definition: messages.hpp:506
A message that indicates to clear all orders in the order book.
Definition: messages.hpp:213
bool boolean()
Return the result of a coin toss.
Definition: probability.hpp:105
void did_receive(DataFeedReceiver *receiver, const DataFeed::Messages::Clear &message)
Handle a clear book message.
Definition: mean_reversion.hpp:182
A message that indicates the start of a trading session.
Definition: messages.hpp:460
void did_receive(DataFeedReceiver *receiver, const DataFeed::Messages::EndOfSession &message)
Handle an end of session message.
Definition: mean_reversion.hpp:167
Price last_best_buy() const
Return the last best buy price.
Definition: limit_order_book.hpp:268
A client for interacting with the direct market access server.
Definition: client.hpp:42
A request to cancel all active orders in the book.
Definition: messages.hpp:917
Username make_username(std::string username)
Make a username from the given input string.
Definition: messages.hpp:46
uint32_t Quantity
A type for order quantities.
Definition: messages.hpp:133
T process(T observation)
Calculate the next average and variance based on observation .
Definition: exponential_moving_variance.hpp:113
void did_receive(DataFeedReceiver *receiver, const DataFeed::Messages::Trade &message)
Handle a trade message.
Definition: mean_reversion.hpp:203
void did_receive(DataFeedReceiver *receiver, const DataFeed::Messages::DeleteOrder &message)
Handle a delete order message.
Definition: mean_reversion.hpp:196
Direct market access trading strategies.
Definition: iceberg_liquidity_consumer.hpp:33
A message that indicates a limit order was added to the book.
Definition: messages.hpp:259
void did_receive(DataFeedReceiver *receiver, const DataFeed::Messages::AddOrder &message)
Handle an add order message.
Definition: mean_reversion.hpp:189
The mean reversion trader strategy logic.
Definition: mean_reversion.hpp:41
MeanReversion(asio::io_context &feed_context, asio::io_context &context, nlohmann::json options)
Initialize the strategy.
Definition: mean_reversion.hpp:116