-
Notifications
You must be signed in to change notification settings - Fork 156
/
BuildInstrument.cpp
254 lines (206 loc) · 8.86 KB
/
BuildInstrument.cpp
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
252
253
254
/************************************************************************
* Copyright(c) 2021, One Unified. All rights reserved. *
* email: info@oneunified.net *
* *
* This file is provided as is WITHOUT ANY WARRANTY *
* without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
* *
* This software may not be used nor distributed without proper license *
* agreement. *
* *
* See the file LICENSE.txt for redistribution information. *
************************************************************************/
/*
* File: BuildInstrument.cpp
* Author: raymond@burkholder.net
* Project: TFTrading
* Created: Sept 20, 2021, 21:52
*/
#include <boost/log/trivial.hpp>
#include <TFIQFeed/BuildInstrument.h>
#include <TFTrading/Watch.h>
#include <TFTrading/InstrumentManager.h>
#include "BuildInstrument.h"
namespace ou { // One Unified
namespace tf { // TradeFrame
using pWatch_t = Watch::pWatch_t;
// basic instrument
BuildInstrument::BuildInstrument( pProviderIQFeed_t pIQFeed )
: m_pIQ( std::move( pIQFeed ) )
, m_bDeleteIterator( false )
{
assert( m_pIQ );
}
// instrument with contract
BuildInstrument::BuildInstrument( pProviderIQFeed_t pIQFeed, pProviderIBTWS_t pIB )
: m_pIQ( std::move( pIQFeed ) )
, m_pIB( std::move( pIB ) )
, m_bDeleteIterator( false )
{
assert( m_pIQ );
assert( m_pIB );
}
void BuildInstrument::Queue( const std::string& sIQFeedSymbol, fInstrument_t&& fInstrument ) {
pInstrument_t pInstrument;
ou::tf::InstrumentManager& im( ou::tf::InstrumentManager::GlobalInstance() );
pInstrument = im.LoadInstrument( ou::tf::keytypes::EProviderIQF, sIQFeedSymbol );
if ( pInstrument ) { // skip the build
//BOOST_LOG_TRIVIAL(debug) << "BuildInstrument::Build existing: " << pInstrument->GetInstrumentName();
if ( m_pIB ) {
m_pIB->Sync( pInstrument );
}
fInstrument( pInstrument, false );
}
else { // build a new instrument
//BOOST_LOG_TRIVIAL(debug) << "BuildInstrument::Build new: " << sIQFeedSymbol;
{
std::lock_guard<std::mutex> lock( m_mutexMap );
m_mapSymbol.emplace( std::make_pair( sIQFeedSymbol, std::move( fInstrument ) ) );
}
Update();
}
}
// queue this to be deleted on next run through
// prevents a deadlock on delete of watch as the deletion of AcquireFundamentals
// happens within the HandleFundamentals delegate
void BuildInstrument::Update( mapInProgress_t::iterator iter ) {
Update();
std::lock_guard<std::mutex> lock( m_mutexMap );
m_bDeleteIterator = true;
m_iterToDelete = iter;
}
void BuildInstrument::Update() {
bool bDoBuild( false );
mapInProgress_t::iterator iterInProgress;
{
std::lock_guard<std::mutex> lock( m_mutexMap );
if ( m_bDeleteIterator ) {
m_bDeleteIterator = false;
m_mapInProgress.erase( m_iterToDelete );
}
// 5 may not be important any more as IB manages queue
// but why is queue not being replenished?
if ( 5 > m_mapInProgress.size() ) { // 5 is set in m_pIB
if ( 0 != m_mapSymbol.size() ) {
mapSymbol_t::iterator iterSymbol = m_mapSymbol.begin();
{
auto& [ sIQFeedSymbol_, fInstrument_ ] = *iterSymbol;
const std::string sIQFeedSymbol( sIQFeedSymbol_ );
auto result = m_mapInProgress.emplace( std::make_pair( sIQFeedSymbol, InProgress( std::move( fInstrument_ ) ) ) );
assert( result.second );
iterInProgress = result.first;
bDoBuild = true;
}
//BOOST_LOG_TRIVIAL(debug) << "BuildInstrument::Update erase " << iterSymbol->first;
m_mapSymbol.erase( iterSymbol );
}
}
}
if ( bDoBuild ) Build( iterInProgress );
}
void BuildInstrument::Build( mapInProgress_t::iterator iterInProgress ) {
auto& [ sIQFeedSymbol, ip ] = *iterInProgress;
pInstrument_t pInstrument;
// temporary instrument solely for obtaining fundamental data with which to build real instrument
//pInstrument = ou::tf::iqfeed::BuildInstrument( "Acquire-" + sIQFeedSymbol, trd );
pInstrument = std::make_shared<ou::tf::Instrument>( "Acquire-" + sIQFeedSymbol ); // just enough to obtain fundamentals
pInstrument->SetAlternateName( ou::tf::Instrument::eidProvider_t::EProviderIQF, sIQFeedSymbol );
pWatch_t pWatch = std::make_shared<ou::tf::Watch>( pInstrument, m_pIQ );
//BOOST_LOG_TRIVIAL(debug)
// << "BuildInstrument: "
// << sIQFeedSymbol
// ;
AcquireFundamentals::pAcquireFundamentals_t pAcquireFundamentals
= std::make_shared<AcquireFundamentals>(
std::move( pWatch ),
[this,iterInProgress]( pWatch_t pWatchOld ) { // async call once fundamentals arrive
//BOOST_LOG_TRIVIAL(debug) << "AcquireFundamentals_done enter: " << iterInProgress->first;
const ou::tf::Watch::Fundamentals& fundamentals( pWatchOld->GetFundamentals() );
pInstrument_t pInstrument
= ou::tf::iqfeed::BuildInstrument( fundamentals );
std::string sWaiting;
if ( 0 < m_mapInProgress.size() ) {
for ( mapInProgress_t::value_type& vt: m_mapInProgress ) {
sWaiting += "," + vt.first;
}
}
BOOST_LOG_TRIVIAL(info)
<< "BuildInstrument start: "
<< pInstrument->GetInstrumentName() << ","
<< fundamentals.sExchangeRoot << ","
<< m_mapInProgress.size() << ","
<< m_mapSymbol.size()
//<< "," << iterInProgress->first
//<< "," << sWaiting
;
if ( m_pIB ) {
std::string sName;
switch ( pInstrument->GetInstrumentType() ) {
case InstrumentType::Option:
sName = ou::tf::iqfeed::MarketSymbol::OptionBaseName( fundamentals );
break;
default:
sName = ( 0 == fundamentals.sExchangeRoot.size() ) ? fundamentals.sSymbolName : fundamentals.sExchangeRoot;
break;
};
m_pIB->RequestContractDetails(
sName, // needs to be the IB base name
pInstrument, // this is a filled-in, prepared instrument
[this,iterInProgress]( const ou::tf::ib::TWS::ContractDetails& details, pInstrument_t& pInstrument ){
//BOOST_LOG_TRIVIAL(debug) << "BuildInstrument::Build contract: " << pInstrument->GetInstrumentName();
assert( 0 != pInstrument->GetContract() );
m_pIB->Sync( pInstrument );
//ou::tf::InstrumentManager& im( ou::tf::InstrumentManager::GlobalInstance() );
//im.Register( pInstrument ); // is a CallAfter required, or can this run in a thread?
iterInProgress->second.fInstrument( pInstrument, true );
},
[this,iterInProgress]( bool bStatus ) {
if ( bStatus ) {
//BOOST_LOG_TRIVIAL(debug)
// << "BuildInstrument::Build done: "
// << m_mapSymbol.size() << ","
// << m_mapInProgress.size() << ","
// << iterInProgress->first
//;
}
else {
BOOST_LOG_TRIVIAL(warning)
<< "BuildInstrument fail: "
<< iterInProgress->first
<< "," << m_mapSymbol.size()
<< "," << m_mapInProgress.size()
;
iterInProgress->second.fInstrument( nullptr, false );
}
{
std::lock_guard<std::mutex> lock( m_mutexMap );
m_mapInProgress.erase( iterInProgress );
}
Update(); // this should replenish queue
}
);
}
else {
//ou::tf::InstrumentManager& im( ou::tf::InstrumentManager::GlobalInstance() );
//im.Register( pInstrument ); // is a CallAfter required, or can this run in a thread?
iterInProgress->second.fInstrument( pInstrument, true );
pInstrument.reset();
Update( iterInProgress );
}
//BOOST_LOG_TRIVIAL(debug) << "AcquireFundamentals_done exit: " << iterInProgress->first;
}
);
iterInProgress->second.pAcquireFundamentals = pAcquireFundamentals;
pAcquireFundamentals->Start();
}
void BuildInstrument::Clear() {
std::lock_guard<std::mutex> lock( m_mutexMap );
assert( 0 == m_mapInProgress.size() );
m_mapSymbol.clear();
}
bool BuildInstrument::Active() {
return !( m_mapSymbol.empty() && m_mapInProgress.empty() );
}
} // namespace tf
} // namespace ou