OnixS C++ ICE Binary Order Entry Handler 1.1.1
API Documentation
Loading...
Searching...
No Matches
Helpers.h
Go to the documentation of this file.
1/*
2 * Copyright Onix Solutions Limited [OnixS]. All rights reserved.
3 *
4 * This software owned by Onix Solutions Limited [OnixS] and is protected by copyright law
5 * and international copyright treaties.
6 *
7 * Access to and use of the software is governed by the terms of the applicable ONIXS Software
8 * Services Agreement (the Agreement) and Customer end user license agreements granting
9 * a non-assignable, non-transferable and non-exclusive license to use the software
10 * for it's own data processing purposes under the terms defined in the Agreement.
11 *
12 * Except as otherwise granted within the terms of the Agreement, copying or reproduction of any part
13 * of this source code or associated reference material to any other location for further reproduction
14 * or redistribution, and any amendments to this copyright notice, are expressly prohibited.
15 *
16 * Any reproduction or redistribution for sale or hiring of the Software not in accordance with
17 * the terms of the Agreement is a violation of copyright law.
18 */
19
20#pragma once
21
23
25#include "../Common/Options.h"
26
27#include <string>
28#include <sstream>
29#include <iostream>
30#include <cstdio>
31#include <fstream>
32
33#include <thread>
34#include <future>
35#include <functional>
36#include <chrono>
37
38namespace Samples {
40
42{
43 static int32_t newId()
44 {
45 constexpr int64_t limit = (std::numeric_limits<int32_t>::max)();
46 const uint64_t ticks = static_cast<uint64_t>(UtcWatch::now().sinceEpoch());
47 return static_cast<int32_t>((splitmix64(ticks) % limit) + 1);
48 }
49
50private:
51 static uint64_t splitmix64(uint64_t x) noexcept
52 {
53 x ^= 0x726f62696eULL;
54 x += 0x9e3779b97f4a7c15ULL;
55 x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9ULL;
56 x = (x ^ (x >> 27)) * 0x94d049bb133111ebULL;
57 x ^= (x >> 31);
58 return x;
59 }
60};
61
62class Helper
63{
64public:
65 static MessageHolder<IPRequest> createIpRequest(int clientId, const std::string& userId)
66 {
68
69 request->clientId(clientId);
70
71 auto users = request->users(1);
72 users[0].userId(userId);
73
74 return request;
75 }
76
77 static MessageHolder<TraderLogonRequest> createTraderLogonRequest(const std::string& traderId, const std::string& traderPwd)
78 {
80
81 request->originatorUserId(traderId);
82 request->directElectronicAccess(BooleanEnum::True);
83 request->tradingCapacity(TradingCapacityEnum::DEAL);
84 request->liquidityProvision(BooleanEnum::True);
85 request->commodityDerivIndicator(BooleanEnum::False);
86 request->investmentDecision(87657);
87 request->executionDecision(87657);
88 request->clientIdCode(nullOpt);
89 request->mifidId(nullOpt);
90 request->rawData(traderPwd);
91
92 return request;
93 }
94
96 {
98
99 request->originatorUserId(traderId);
100
101 return request;
102 }
103
104 static MessageHolder<NewOrderRequest> createOrder(const std::string& traderId)
105 {
107
108 request->originatorUserId(traderId);
109
110 request->directElectronicAccess(nullOpt);
111 request->tradingCapacity(nullOpt);
112 request->liquidityProvision(nullOpt);
113 request->commodityDerivIndicator(nullOpt);
114 request->investmentDecision(nullOpt);
115 request->executionDecision(nullOpt);
116 request->clientIdCode(nullOpt);
117 request->customerAccountRefId(nullOpt);
118
119 request->side(SideEnum::Buy);
120 request->ordType(OrderTypeEnum::Market);
121 request->timeInForce(TimeInForceEnum::Day);
122
123 request->price(nullOpt);
124 request->orderQty(1000000000);
125 request->symbol(5884693);
126 request->clOrdId(IdGenerator::newId());
127
128
129 request->onBehalfOfSubId("DAU|DAU");
130 request->onBehalfOfLocationId("DAU|DAU");
131 request->onBehalfOfCompId("DAU|DAU");
132 request->clearingFirm(649);
133 request->clearingAccount("TEST");
134 request->selfMatchPreventionId(nullOpt);
135 request->mifidId(10);
136
137 return request;
138 }
139};
140
141template <typename T>
142T fromStr(const std::string & s)
143{
144 std::istringstream ss(s);
145 T result = T();
146 ss >> result;
147 return result;
148}
149
150inline std::shared_ptr<void> setPriorityAndPolicy(Session* session = nullptr)
151{
152#ifdef _WIN32
153 ONIXS_ICEBOE_NAMESPACE::Threading::ThisThread::priority(THREAD_PRIORITY_ABOVE_NORMAL);
154
155 if(session)
156 session->receivingThreadPriority(THREAD_PRIORITY_ABOVE_NORMAL);
157#else
158 const int policy = SCHED_RR;
159 try
160 {
162
163 if(session)
164 session->receivingThreadPolicy(policy);
165 }
166 catch (const std::exception&)
167 {
168 std::cerr << "\nWARNING: Cannot change the scheduling policy to " << policy <<
169 ", it requires root permissions, so you either have to be root or run it with sudo." << std::endl;
170 }
171#endif
172
173 return std::shared_ptr<void>{nullptr, [](void*)
174 {
175#ifndef _WIN32
176 ONIXS_ICEBOE_NAMESPACE::Threading::ThisThread::policy(SCHED_OTHER);
177#endif
178 }};
179}
180
181inline
183 std::ostream& o, size_t numberOfMessages, size_t intervalBetweenSending, size_t warmupInterval, SessionStorageType::Enum storageType)
184{
185 o << "\n\n\n****" << std::endl;
186
187 o << std::endl << "Benchmark Parameters: numberOfMessages=" << numberOfMessages << "; intervalBetweenSending(μSec)=" << intervalBetweenSending
188 << "; warmupInterval(μSec)=" << warmupInterval
189 << "; StorageType=" << SessionStorageType::toString(storageType) << '.' << std::endl;
190}
191
192inline
193std::unique_ptr<Session> createSession(SessionType::Enum sessionType, SessionSettings settings, SessionListener* listener, SessionStorageType::Enum storageType)
194{
195 return std::unique_ptr<Session>{new Session(sessionType, settings, listener, storageType, nullptr, {})};
196}
197
198inline
199std::unique_ptr<Session> createSession(SessionReactor & stack, SessionType::Enum sessionType, SessionSettings settings, SessionListener* listener, SessionStorageType::Enum storageType)
200{
201 return std::unique_ptr<Session>{new Session(stack, sessionType, settings, listener, storageType, nullptr, {})};
202}
203
204inline
205std::unique_ptr<Session> createSession(ServiceFactory & factory, SessionType::Enum sessionType, SessionSettings settings, SessionListener* listener, SessionStorageType::Enum storageType)
206{
207 return std::unique_ptr<Session>{new Session(factory, sessionType, settings, listener, storageType, nullptr, {})};
208}
209
210}
#define ONIXS_ICEBOE_MESSAGING_NAMESPACE
Definition ABI.h:114
Contains the SimpleOpenFramingHeader, the SBE message, and the data buffer.
Ticks sinceEpoch() const noexcept
Definition Time.h:622
Session's network stack reactor interface.
static void policy(int policy)
Sets the scheduling policy for the current thread.
static MessageHolder< IPRequest > createIpRequest(int clientId, const std::string &userId)
Definition Helpers.h:65
static MessageHolder< NewOrderRequest > createOrder(const std::string &traderId)
Definition Helpers.h:104
static MessageHolder< TraderLogoutRequest > createTraderLogoutRequest(const std::string &traderId)
Definition Helpers.h:95
static MessageHolder< TraderLogonRequest > createTraderLogonRequest(const std::string &traderId, const std::string &traderPwd)
Definition Helpers.h:77
std::shared_ptr< void > setPriorityAndPolicy(Session *session=nullptr)
Definition Helpers.h:150
std::unique_ptr< Session > createSession(SessionType::Enum sessionType, SessionSettings settings, SessionListener *listener, SessionStorageType::Enum storageType)
Definition Helpers.h:193
void printBenchmarkSettings(std::ostream &o, size_t numberOfMessages, size_t intervalBetweenSending, size_t warmupInterval, SessionStorageType::Enum storageType)
Definition Helpers.h:182
T fromStr(const std::string &s)
Definition Helpers.h:142
static const char * toString(SessionStorageType::Enum)
static int32_t newId()
Definition Helpers.h:43