22 #ifndef STRATEGIES_NOISE_HPP
23 #define STRATEGIES_NOISE_HPP
25 #include "data_feed/receiver.hpp"
26 #include "order_entry/client.hpp"
27 #include "maths/probability.hpp"
28 #include <nlohmann/json.hpp>
49 asio::steady_timer timer;
55 std::atomic<bool> is_running;
60 enum class OrderType : int {Market, Limit, Cancel};
62 std::discrete_distribution<int> order_distribution;
64 enum LimitType :
int {Cross, Inside, AtBest, Outside};
66 std::discrete_distribution<int> limit_distribution;
68 double size_market_mean;
70 double size_market_std;
72 double size_limit_mean;
74 double size_limit_std;
95 case LimitType::Cross: {
97 case OrderEntry::Side::Sell: {
100 size(size_limit_mean, size_limit_std),
105 case OrderEntry::Side::Buy: {
108 size(size_limit_mean, size_limit_std),
116 case LimitType::Inside: {
120 if (bid >= std::numeric_limits<OrderEntry::Price>::max() - 1)
121 bid = std::numeric_limits<OrderEntry::Price>::max();
128 if (ask <= std::numeric_limits<OrderEntry::Price>::min() + 1)
129 ask = std::numeric_limits<OrderEntry::Price>::min();
134 if (bid >= ask)
return;
138 size(size_limit_mean, size_limit_std),
143 case LimitType::AtBest: {
145 case OrderEntry::Side::Sell: {
148 size(size_limit_mean, size_limit_std),
154 case OrderEntry::Side::Buy: {
157 size(size_limit_mean, size_limit_std),
165 case LimitType::Outside: {
167 auto noise = outside();
169 case OrderEntry::Side::Sell: {
173 if (price >= std::numeric_limits<OrderEntry::Price>::max() - noise)
174 price = std::numeric_limits<OrderEntry::Price>::max();
179 size(size_limit_mean, size_limit_std),
184 case OrderEntry::Side::Buy: {
188 if (price <= std::numeric_limits<OrderEntry::Price>::min() + noise)
189 price = std::numeric_limits<OrderEntry::Price>::min();
194 size(size_limit_mean, size_limit_std),
206 inline void start_strategy() {
207 timer.expires_after(std::chrono::milliseconds(sleep_time));
208 timer.async_wait([
this](
const std::error_code& error) {
210 std::cout <<
"Noise::start_strategy - " << error << std::endl;
223 case OrderType::Market: {
225 OrderEntry::Messages::ORDER_PRICE_MARKET,
226 size(size_market_mean, size_market_std),
231 case OrderType::Limit: {
235 case OrderType::Cancel: {
251 asio::io_context& feed_context,
252 asio::io_context& context,
253 nlohmann::json options
257 asio::ip::make_address(options[
"data_feed"][
"listen"].get<std::string>()),
258 asio::ip::make_address(options[
"data_feed"][
"group"].get<std::string>()),
259 options[
"data_feed"][
"port"].get<uint16_t>(),
264 options[
"order_entry"][
"host"].get<std::string>(),
265 std::to_string(options[
"order_entry"][
"port"].get<uint16_t>())
268 sleep_time(options[
"strategy"][
"sleep_time"].get<uint32_t>()),
269 P_act(options[
"strategy"][
"P_act"].get<double>()),
272 options[
"strategy"][
"order_distribution"][
"P_market"].get<
double>(),
273 options[
"strategy"][
"order_distribution"][
"P_limit"].get<double>(),
274 options[
"strategy"][
"order_distribution"][
"P_cancel"].get<
double>()
277 options[
"strategy"][
"limit_distribution"][
"P_cross"].get<
double>(),
278 options[
"strategy"][
"limit_distribution"][
"P_inside"].get<double>(),
279 options[
"strategy"][
"limit_distribution"][
"P_best"].get<
double>(),
280 options[
"strategy"][
"limit_distribution"][
"P_outside"].get<double>()
282 size_market_mean(options[
"strategy"][
"size_market_mean"].get<double>() + 2),
283 size_market_std(options[
"strategy"][
"size_market_std"].get<double>()),
284 size_limit_mean(options[
"strategy"][
"size_limit_mean"].get<double>() + 2),
285 size_limit_std(options[
"strategy"][
"size_limit_std"].get<double>()),
286 x_min_outside(options[
"strategy"][
"x_min_outside"].get<double>()),
287 beta_exp(options[
"strategy"][
"beta_exp"].get<double>()) {
301 std::cerr <<
"received start of session when already running" << std::endl;
316 if (not is_running) {
317 std::cerr <<
"received end of session when not running" << std::endl;
356 #endif // STRATEGIES_NOISE_HPP
T lognormal(T mean, T stddev)
Sample a number from a log-normal distribution.
Definition: probability.hpp:85
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
Noise(asio::io_context &feed_context, asio::io_context &context, nlohmann::json options)
Initialize the strategy.
Definition: noise.hpp:250
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::AddOrder &message)
Handle an add order message.
Definition: noise.hpp:337
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::StartOfSession &message)
Handle a start of session message.
Definition: noise.hpp:299
A message that indicates the start of a trading session.
Definition: messages.hpp:460
void did_receive(DataFeedReceiver *receiver, const DataFeed::Messages::Clear &message)
Handle a clear book message.
Definition: noise.hpp:330
T uniform_int(T min, T max)
Sample a number from an integer-valued uniform distribution.
Definition: probability.hpp:49
std::mt19937 generator(getpid())
the global generator for the probability module.
void did_receive(DataFeedReceiver *receiver, const DataFeed::Messages::Trade &message)
Handle a trade message.
Definition: noise.hpp:351
void did_receive(DataFeedReceiver *receiver, const DataFeed::Messages::DeleteOrder &message)
Handle a delete order message.
Definition: noise.hpp:344
void did_receive(DataFeedReceiver *receiver, const DataFeed::Messages::EndOfSession &message)
Handle an end of session message.
Definition: noise.hpp:315
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
T power_law(T constant, T exponent)
Sample a number sample from a power law distribution.
Definition: probability.hpp:97
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
The noise trader strategy logic.
Definition: noise.hpp:40
uint64_t Price
A type for order prices.
Definition: messages.hpp:136
Side
The side of an order.
Definition: messages.hpp:71
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