-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathInstrumentFile.cpp
264 lines (222 loc) · 9.7 KB
/
InstrumentFile.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
255
256
257
258
259
260
261
262
263
264
/************************************************************************
* Copyright(c) 2009, One Unified. All rights reserved. *
* email: [email protected] *
* *
* 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. *
************************************************************************/
#include "StdAfx.h"
// 2012/10/21 Deprecated, no longer use db4
#include <string>
#include <stdexcept>
#include <cassert>
#include <TFTrading/TradingEnumerations.h>
#include "IQFeedInstrumentFile.h"
namespace ou { // One Unified
namespace tf { // TradeFrame
CInstrumentFile::CInstrumentFile(void)
: pRecord( NULL ),
m_bOpen( false ),
m_pdbSymbols( NULL ),
m_pdbIxSymbols_Market( NULL ), m_pdbcIxSymbols_Market( NULL ),
m_pdbIxSymbols_Underlying( NULL ), m_pdbcIxSymbols_Underlying( NULL )
{
}
CInstrumentFile::~CInstrumentFile(void) {
}
void CInstrumentFile::OpenIQFSymbols( const std::string& sDbFileName ) {
// CBerkeleyDBEnvManagerSingleton EnvMgr;
CBerkeleyDBEnvManager& dbMgr = CBerkeleyDBEnvManager::Instance();
DbEnv *pDbEnv = dbMgr.GetDbEnv();
// open/create main symbol table
m_pdbSymbols = new Db( pDbEnv, 0 );
m_pdbSymbols->open( NULL, sDbFileName.c_str(), "IQFSymbols", DB_BTREE, DB_CREATE, 0 );
// open/create the market index
m_pdbIxSymbols_Market = new Db( pDbEnv, 0 );
m_pdbIxSymbols_Market->set_flags( DB_DUPSORT );
m_pdbIxSymbols_Market->open( NULL, sDbFileName.c_str(), "IxIQFSymbols_Market", DB_BTREE, DB_CREATE, 0 );
// associate the index with the main table
m_pdbSymbols->associate( NULL, m_pdbIxSymbols_Market, &CInstrumentFile::GetMarketName, 0 );
// open/create the underlying index
m_pdbIxSymbols_Underlying = new Db( pDbEnv, 0 );
m_pdbIxSymbols_Underlying->set_flags( DB_DUPSORT );
m_pdbIxSymbols_Underlying->open( NULL, sDbFileName.c_str(), "IxIQFSymbols_Underlying", DB_BTREE, DB_CREATE, 0 );
// associate the index with the main table
m_pdbSymbols->associate( NULL, m_pdbIxSymbols_Underlying, &CInstrumentFile::GetUnderlyingName, 0 );
m_bOpen = true;
}
void CInstrumentFile::CloseIQFSymbols() {
m_bOpen = false;
m_pdbIxSymbols_Underlying->close(0);
m_pdbIxSymbols_Market->close(0);
m_pdbSymbols->close(0);
}
void CInstrumentFile::GetRecord( const std::string& sName, structSymbolRecord* pRec ) {
// set key parameters
Dbt k;
k.set_data( (void*) sName.c_str() );
k.set_size( sName.size() );
// set record parameters
Dbt v;
v.set_flags( DB_DBT_USERMEM );
v.set_ulen( sizeof( structSymbolRecord ) );
v.set_size( sizeof( structSymbolRecord ) );
v.set_data( pRec );
int ret;
try {
ret = m_pdbSymbols->get( 0, &k ,&v, 0 );
}
catch ( DbException e ) {
std::cout << "CInstrumentFile::CreateInstrumentFromIQFeed " << e.what() << std::endl;
}
catch ( ... ) {
std::cout << "CInstrumentFile::CreateInstrumentFromIQFeed bummer error" << std::endl;
}
if ( DB_NOTFOUND == ret ) {
throw std::out_of_range( "CInstrumentFile::CreateInstrumentFromIQFeed symbol not found" );
}
if ( 0 != ret ) {
throw std::runtime_error( "CInstrumentFile::CreateInstrumentFromIQFeed had bad error" );
}
}
Instrument::pInstrument_t CInstrumentFile::CreateInstrumentFromIQFeed(const std::string& sIQFeedSymbolName ) {
structSymbolRecord rec;
GetRecord( sIQFeedSymbolName, &rec );
std::string sExchange( rec.line + rec.ix[2], rec.cnt[2] );
switch ( rec.eInstrumentType ) {
case InstrumentType::Stock: {
Instrument::pInstrument_t pInstrument( new Instrument( sIQFeedSymbolName, InstrumentType::Stock, sExchange ) );
return pInstrument;
}
break;
case InstrumentType::Option: {
const char *p = rec.line + rec.ix[1];
const char *e = strchr( p, ' ' );
u_int32_t len = e - p;
std::string sUnderlying( rec.line + rec.ix[1], len );
if ( 0 == len ) {
throw std::out_of_range( "CInstrumentFile::CreateInstrumentFromIQFeed underlying symbol length is 0" );
}
Instrument::pInstrument_t pUnderlying( CreateInstrumentFromIQFeed( sUnderlying ) );
Instrument::pInstrument_t pInstrument( new Instrument( sIQFeedSymbolName,
(InstrumentType::EInstrumentTypes) rec.eInstrumentType,
sExchange,
rec.nYear, rec.nMonth,
pUnderlying,
(OptionSide::enumOptionSide) rec.nOptionSide,
rec.fltStrike ) );
return pInstrument;
}
break;
case InstrumentType::Future: {
Instrument::pInstrument_t pInstrument(
new Instrument( sIQFeedSymbolName, (InstrumentType::EInstrumentTypes) rec.eInstrumentType, sExchange, rec.nYear, rec.nMonth ) );
return pInstrument;
}
break;
default:
throw std::out_of_range( "CInstrumentFile::CreateInstrumentFromIQFeed: Unknown instrument type" );
}
}
Instrument::pInstrument_t CInstrumentFile::CreateInstrumentFromIQFeed(const std::string& sIQFeedSymbolName, Instrument::pInstrument_t pUnderlying ) {
structSymbolRecord rec;
GetRecord( sIQFeedSymbolName, &rec );
std::string sExchange( rec.line + rec.ix[2], rec.cnt[2] );
switch ( rec.eInstrumentType ) {
case InstrumentType::Option: {
const char *p = rec.line + rec.ix[1];
const char *e = strchr( p, ' ' );
u_int32_t len = e - p;
std::string sUnderlying( rec.line + rec.ix[1], len );
Instrument::pInstrument_t pInstrument( new Instrument( sIQFeedSymbolName,
(InstrumentType::EInstrumentTypes) rec.eInstrumentType,
sExchange,
rec.nYear, rec.nMonth, rec.nDay,
pUnderlying,
(OptionSide::enumOptionSide) rec.nOptionSide,
rec.fltStrike ) );
return pInstrument;
}
break;
default:
throw std::out_of_range( "CInstrumentFile::CreateInstrumentFromIQFeed: used for symbols with underlying only" );
}
}
// Sets SubIndex (static function) for Exchange
int CInstrumentFile::GetMarketName( Db *secondary, const Dbt* pKey, const Dbt* data, Dbt* secKey ) {
structSymbolRecord *dbIxRecord = (structSymbolRecord *) data->get_data();
char *p = dbIxRecord->line + dbIxRecord->ix[structSymbolRecord::IXExchange]; // get at the 'exchange' string
unsigned long l = dbIxRecord->lenExchangeKey; // set the key and its length
secKey->set_data( p );
secKey->set_size( l );
return 0;
}
// Sets SubIndex (static function) for Underlying
int CInstrumentFile::GetUnderlyingName( Db *secondary, const Dbt* pKey, const Dbt* data, Dbt* secKey ) {
structSymbolRecord *dbIxRecord = (structSymbolRecord *) data->get_data();
char *p;
size_t len;
bool bUseIndex = true;
if ( InstrumentType::Option == dbIxRecord->eInstrumentType ) { // OPRA option
p = dbIxRecord->line + dbIxRecord->ix[structSymbolRecord::IXDesc]; // start of description
char *e = strchr( p, ' ' ); // find the trailing blank
len = e - p;
if ( 0 != len ) bUseIndex = false;
}
if ( bUseIndex ) { // by default, use record's symbol
p = dbIxRecord->line;
len = dbIxRecord->cnt[0];
}
secKey->set_data( p );
secKey->set_size( len );
return 0;
}
void CInstrumentFile::SetSearchExchange( const char* szExchange ) {
m_szSearchKey = szExchange;
m_lenSearchKey = strlen( szExchange );
m_dbtKey.set_data( (void*) szExchange );
m_dbtKey.set_size( m_lenSearchKey );
int ret = m_pdbIxSymbols_Market->cursor( NULL, &m_pdbcIxSymbols_Market, 0 );
if ( 0 != ret ) throw std::runtime_error( "CInstrumentFile::SetSearchExchange has problems" );
}
void CInstrumentFile::SetSearchUnderlying( const char* szUnderlying ) {
m_szSearchKey = szUnderlying;
m_lenSearchKey = strlen( szUnderlying );
m_dbtKey.set_data( (void*) szUnderlying );
m_dbtKey.set_size( m_lenSearchKey );
int ret = m_pdbIxSymbols_Underlying->cursor( NULL, &m_pdbcIxSymbols_Underlying, 0 );
if ( 0 != ret ) throw std::runtime_error( "CInstrumentFile::SetSearchUnderlying has problems" );
}
structSymbolRecord* CInstrumentFile::RetrieveSymbolRecordByExchange( u_int32_t flags ) {
// return value NULL when data not found
// flags = DB_SET: find first record
// DB_NEXT_DUP: retrieve next record for same exchange
structSymbolRecord* p = NULL;
assert( NULL != m_pdbcIxSymbols_Market );
int result = m_pdbcIxSymbols_Market->get( &m_dbtKey, &m_dbtData, flags );
// DB_NOTFOUND = -30988
if ( 0 == result ) {
p = (structSymbolRecord *) m_dbtData.get_data();
}
return p;
}
structSymbolRecord* CInstrumentFile::RetrieveSymbolRecordByUnderlying( u_int32_t flags ) {
// return value NULL when data not found
// flags = DB_SET: find first record
// DB_NEXT_DUP: retrieve next record for same exchange
structSymbolRecord* p = NULL;
assert( NULL != m_pdbcIxSymbols_Underlying );
int result = m_pdbcIxSymbols_Underlying->get( &m_dbtKey, &m_dbtData, flags );
if ( 0 == result ) {
p = (structSymbolRecord *) m_dbtData.get_data();
}
return p;
}
} // namespace tf
} // namespace ou