-
Notifications
You must be signed in to change notification settings - Fork 165
/
Copy pathPopulateWithIBOptions.cpp
90 lines (75 loc) · 3.55 KB
/
PopulateWithIBOptions.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
/************************************************************************
* Copyright(c) 2013, 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. *
************************************************************************/
// started 2013/07/14 de-templating the original class
#include "PopulateWithIBOptions.h"
namespace ou { // One Unified
namespace tf { // TradeFrame
namespace option { // Option
PopulateOptions::PopulateOptions( ou::tf::DBOps& session, pProvider_t pProvider )
: m_pProvider( pProvider ), m_session( session ), m_bActive( false ), m_cntInstruments( 0 )
{
// assert( session active? );
assert( pProvider->Connected() );
m_contract.currency = "USD";
m_contract.exchange = "SMART";
m_contract.secType = "OPT";
m_pProvider->SetOnSecurityDefinitionNotFoundHandler( MakeDelegate( this, &PopulateOptions::HandleOptionContractNotFound ) );
}
PopulateOptions::~PopulateOptions( void ) {
m_pProvider->SetOnSecurityDefinitionNotFoundHandler( NULL );
}
void PopulateOptions::Populate( const std::string& sUnderlying, boost::gregorian::date dateExpiry, bool bCall, bool bPut ) {
if ( m_bActive ) {
throw std::runtime_error( "already in process" );
}
if ( !bCall && !bPut ) {
throw std::runtime_error( "neither call nor put set" );
}
m_bActive = true;
unsigned int n( 0 ); // start with no options retrieved
m_contract.symbol = sUnderlying;
m_contract.right = "";
if ( bCall && bPut ) {
// leave .right empty to get both
}
else {
if ( bCall ) m_contract.right = "CALL";
if ( bPut ) m_contract.right = "PUT";
}
m_contract.lastTradeDateOrContractMonth = boost::gregorian::to_iso_string( dateExpiry );
namespace ph = std::placeholders;
m_pProvider->RequestContractDetails( // delete any pre-existing first?
m_contract,
std::bind( &PopulateOptions::HandleOptionContractDetails, this, ph::_1, ph::_2 ),
std::bind( &PopulateOptions::HandleOptionContractDetailsDone, this, ph::_1 )
);
}
void PopulateOptions::HandleOptionContractDetails( const ContractDetails& details, pInstrument_t& pInstrument ) {
// pInstrument_t pInstrument = m_pProvider->BuildInstrumentFromContract( details.summary );
// ou::db::QueryFields<Instrument::TableRowDef>::pQueryFields_t pInsert
// = m_session.Insert<Instrument::TableRowDef>( pInstrument->GetRow() );
++m_cntInstruments;
if ( 0 != OnInstrumentBuilt ) OnInstrumentBuilt( pInstrument );
}
void PopulateOptions::HandleOptionContractDetailsDone( bool ) {
if ( 0 != OnPopulateComplete ) OnPopulateComplete( m_cntInstruments );
m_bActive = false;
}
void PopulateOptions::HandleOptionContractNotFound() {
if ( 0 != OnPopulateComplete ) OnPopulateComplete( m_cntInstruments );
m_bActive = false;
}
} // namespace option
} // namespace tf
} // namespace ou