-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathMarketDataService.cpp
More file actions
251 lines (178 loc) · 10.3 KB
/
MarketDataService.cpp
File metadata and controls
251 lines (178 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
Copyright (C) 2021 Mike Kipnis
This file is part of DistributedATS, a free-software/open-source project
that integrates QuickFIX and LiquiBook over DDS. This project simplifies
the process of having multiple FIX gateways communicating with multiple
matching engines in realtime.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "MarketDataService.h"
#include <BasicDomainParticipant.h>
#include <iostream>
#include <quickfix/FixValues.h>
#include <MarketDataRequestPubSubTypes.hpp>
#include <MarketDataIncrementalRefreshPubSubTypes.hpp>
#include <MarketDataSnapshotFullRefreshPubSubTypes.hpp>
#include "MarketDataIncrementalRefreshDataReaderListenerImpl.h"
#include <log4cxx/logger.h>
#include <log4cxx/basicconfigurator.h>
#include <thread>
#include <chrono>
#include <MarketDataSnapshotFullRefreshLogger.hpp>
#include <LoggerHelper.h>
namespace DistributedATS {
MarketDataService::MarketDataService( std::shared_ptr<distributed_ats_utils::basic_domain_participant> basic_domain_participant,
const FIX::DatabaseConnectionID& dbConnectionID) :
_basic_domain_participant_ptr( basic_domain_participant )
{
_incrementalRefreshMapPtr = std::make_shared<IncrementalRefreshMap>();
m_sqliteConnection.reset( new DistributedATS::SQLiteConnection(dbConnectionID) );
_markat_data_request_queue_ptr = std::make_shared<MarketDataRequestQueue>();
std::atomic_init(&_is_running, true);
_service_thread = std::thread(&MarketDataService::service, this);
initialize();
}
MarketDataService::~MarketDataService() {
std::atomic_init(&_is_running, false);
_service_thread.join();
}
void MarketDataService::initialize()
{
DistributedATS::SQLiteQuery query("select i.instrument_name, m.market_name, json_extract(hp.properties,\"$.open\") as open_price " \
" from " \
" hist_price hp, " \
" instrument i, " \
" market m, " \
" instrument_market_map imm " \
" where hp.instrument_name=i.instrument_name and " \
" imm.instrument_name=i.instrument_name and " \
" m.market_name=imm.market_name and " \
" hp.business_date=(select max(business_date) from hist_price where instrument_name=i.instrument_name)");
LOG4CXX_INFO(logger, "Populating hist stats");
m_sqliteConnection->execute(query);
for ( int instrument_index =0; instrument_index<query.rows(); instrument_index++)
{
std::string symbol = query.getValue(instrument_index,0);
std::string market = query.getValue(instrument_index,1);
int last_trade_price = std::atoi(query.getValue(instrument_index,2).c_str());
DistributedATS_MarketDataIncrementalRefresh::NoMDEntries last_price_entry;
last_price_entry.MDUpdateAction(FIX::MDUpdateAction_NEW);
last_price_entry.MDEntryType(FIX::MDEntryType_OPENING_PRICE);
last_price_entry.MDEntryPx(last_trade_price);
last_price_entry.TimeInForce(1);
_incrementalRefreshMapPtr->emplace(Instrument( market, symbol),
std::list<DistributedATS_MarketDataIncrementalRefresh::NoMDEntries> { last_price_entry } );
}
}
void MarketDataService::createMarketDataRequestListener()
{
DistributedATS_MarketDataRequest::MarketDataRequest marketDataRequest;
_market_data_request_topic_tuple = _basic_domain_participant_ptr->make_topic
<
DistributedATS_MarketDataRequest::MarketDataRequestPubSubType,
DistributedATS_MarketDataRequest::MarketDataRequest>
( MARKET_DATA_REQUEST_TOPIC_NAME );
std::string filter_data_service = "DATS_DestinationUser = %0";
_market_data_request_data_reader_tuple = _basic_domain_participant_ptr->make_data_reader_tuple(_market_data_request_topic_tuple,
new DistributedATS::MarketDataRequestDataReaderListenerImpl ( _markat_data_request_queue_ptr ),
"FILTERED_MARKET_DATA_REQUEST", filter_data_service, { _basic_domain_participant_ptr->get_participant_name() });
//_market_data_request_data_reader_tuple = _basic_domain_participant_ptr->make_data_reader_tuple(_market_data_request_topic_tuple, new DistributedATS::MarketDataRequestDataReaderListenerImpl ( data_service_name, _markat_data_request_queue_ptr ) );
}
void MarketDataService::createMarketDataIncrementalRefreshListener()
{
_market_data_incremental_refresh_topic_tuple = _basic_domain_participant_ptr->make_topic
< DistributedATS_MarketDataIncrementalRefresh::MarketDataIncrementalRefreshPubSubType,
DistributedATS_MarketDataIncrementalRefresh::MarketDataIncrementalRefresh>
( MARKET_DATA_INCREMENTAL_REFRESH_TOPIC_NAME );
_market_data_incremental_refresh_data_reader_tuple = _basic_domain_participant_ptr->make_data_reader_tuple(_market_data_incremental_refresh_topic_tuple,
new DistributedATS::MarketDataIncrementalRefreshDataReaderListenerImpl(_incrementalRefreshMapPtr));
}
void MarketDataService::createMarketDataFullRefreshDataWriter()
{
_market_data_snapshot_full_refresh_topic_tuple = _basic_domain_participant_ptr->make_topic
< DistributedATS_MarketDataSnapshotFullRefresh::MarketDataSnapshotFullRefreshPubSubType,
DistributedATS_MarketDataSnapshotFullRefresh::MarketDataSnapshotFullRefresh>
( MARKET_DATA_SNAPSHOT_FULL_REFRESH_TOPIC_NAME );
_market_data_shapshot_full_refresh_dw =
_basic_domain_participant_ptr->make_data_writer( _market_data_snapshot_full_refresh_topic_tuple );
}
bool MarketDataService::processMarketDataRequest( const MarketDataRequestPtr& marketDataRequestPtr )
{
std::cout << ">>>> Received MarketDataRequest : " << marketDataRequestPtr->DATS_Source() << ":"
<< marketDataRequestPtr->DATS_Destination()
<< ":" << marketDataRequestPtr->DATS_SourceUser() << std::endl;
for ( int symbol_index = 0; symbol_index<marketDataRequestPtr->c_NoRelatedSym().size(); ++symbol_index )
{
std::cout << "Symbol to send full snapshot : " << marketDataRequestPtr->c_NoRelatedSym()[symbol_index].Symbol() << std::endl;
DistributedATS_MarketDataSnapshotFullRefresh::MarketDataSnapshotFullRefresh marketDataSnapshotFullRefresh;
marketDataSnapshotFullRefresh.fix_header().BeginString(marketDataRequestPtr->fix_header().BeginString());
marketDataSnapshotFullRefresh.fix_header().MsgType("W");
marketDataSnapshotFullRefresh.DATS_Source(marketDataRequestPtr->DATS_Destination());
marketDataSnapshotFullRefresh.DATS_Destination(marketDataRequestPtr->DATS_Source());
marketDataSnapshotFullRefresh.DATS_DestinationUser(marketDataRequestPtr->DATS_SourceUser());
if ( populateMarketDataSnapshotFullRefresh( DistributedATS::Instrument(
marketDataRequestPtr->c_NoRelatedSym()[symbol_index].SecurityExchange(),
marketDataRequestPtr->c_NoRelatedSym()[symbol_index].Symbol() ), marketDataSnapshotFullRefresh ) )
{
std::cout << "Publishing Full Market Data Snapshot : " << marketDataSnapshotFullRefresh.Symbol() << std::endl;
int ret = _market_data_shapshot_full_refresh_dw->write(&marketDataSnapshotFullRefresh);
if (!ret) {
LOG4CXX_ERROR(logger, "Market Data Snapshot Data Write returned : " << ret );
}
}
}
return true;
}
int MarketDataService::service (void)
{
while(_is_running)
{
std::shared_ptr<DistributedATS_MarketDataRequest::MarketDataRequest> market_data_request;
if ( _markat_data_request_queue_ptr->pop( market_data_request ) )
{
processMarketDataRequest(market_data_request);
} else {
std::this_thread::sleep_for(std::chrono::duration<long double, std::milli>(1000));
}
};
return 0;
}
bool MarketDataService::populateMarketDataSnapshotFullRefresh( const Instrument& instrument,
DistributedATS_MarketDataSnapshotFullRefresh::MarketDataSnapshotFullRefresh& marketDataSnapshotFullRefresh ) const
{
marketDataSnapshotFullRefresh.Symbol( instrument.symbol );
marketDataSnapshotFullRefresh.SecurityExchange( instrument.marketName );
auto mdEntryList = _incrementalRefreshMapPtr->find( instrument );
if ( mdEntryList == _incrementalRefreshMapPtr->end() )
return false;
marketDataSnapshotFullRefresh.c_NoMDEntries().resize( mdEntryList->second.size());
auto md_entry_index = 0;
for ( const auto& mdEntry : mdEntryList->second )
{
marketDataSnapshotFullRefresh.c_NoMDEntries()[md_entry_index].MDEntryType(mdEntry.MDEntryType());
marketDataSnapshotFullRefresh.c_NoMDEntries()[md_entry_index].MDEntryPx(mdEntry.MDEntryPx());
marketDataSnapshotFullRefresh.c_NoMDEntries()[md_entry_index].MDEntrySize(mdEntry.MDEntrySize());
marketDataSnapshotFullRefresh.c_NoMDEntries()[md_entry_index].TimeInForce(0);
md_entry_index++;
}
LoggerHelper::log_debug<
std::stringstream, MarketDataSnapshotFullRefreshLogger,
DistributedATS_MarketDataSnapshotFullRefresh::MarketDataSnapshotFullRefresh>
(logger, marketDataSnapshotFullRefresh, "MarketDataSnapshotFullRefresh");
return true;
}
} /* namespace DistributedATS */