-
Notifications
You must be signed in to change notification settings - Fork 154
/
Aggregate.cpp
214 lines (180 loc) · 7.17 KB
/
Aggregate.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
/************************************************************************
* 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: Aggregate.cpp
* Author: raymond@burkholder.net
* Project: TFOptions
* Created on May 29, 2021, 20:09
*/
#include <stdexcept>
#include "Aggregate.h"
namespace ou { // One Unified
namespace tf { // TradeFrame
namespace option { // options
Aggregate::Aggregate(
pWatch_t pWatchUnderlying
)
: m_pWatchUnderlying( pWatchUnderlying )
{
}
void Aggregate::LoadChains( fGatherOptions_t&& fGatherOptions ) {
std::cout << "Aggregate::LoadChains" << std::endl;
fGatherOptions(
m_pWatchUnderlying->GetInstrument()->GetInstrumentName( ou::tf::Instrument::eidProvider_t::EProviderIQF ),
[this]( pOption_t pOption ){
mapChains_iterator_t iterChains;
const std::string& sInstrumentName( pOption->GetInstrumentName() );
const std::string& sIQFeedSymbolName( pOption->GetInstrument()->GetInstrumentName( ou::tf::Instrument::eidProvider_t::EProviderIQF ) );
{ // find existing expiry, or create new one
chain_t chain;
const boost::gregorian::date expiry( pOption->GetExpiry() );
iterChains = m_mapChains.find( expiry ); // see if expiry date exists
if ( m_mapChains.end() == iterChains ) { // insert new expiry set if not
std::cout
<< "Aggregate chain: "
<< ou::tf::Instrument::BuildDate( expiry )
<< "," << sIQFeedSymbolName
<< "," << sInstrumentName
<< std::endl;
iterChains = m_mapChains.insert(
m_mapChains.begin(),
mapChains_t::value_type( expiry, std::move( chain ) )
);
}
}
{ // populate new call or put, no test for pre-existance
//std::cout << " option: " << row.sSymbol << std::endl;
chain_t& chain( iterChains->second );
chain_t::strike_t& strike( chain.GetStrike( pOption->GetStrike() ) );
try {
switch ( pOption->GetOptionSide() ) {
case ou::tf::OptionSide::Call:
try {
chain.SetIQFeedNameCall( pOption->GetStrike(), sIQFeedSymbolName );
strike.call.pOption = pOption;
}
catch ( std::runtime_error& e ) {
// don't do anything with the duplicate
}
break;
case ou::tf::OptionSide::Put:
try {
chain.SetIQFeedNamePut( pOption->GetStrike(), sIQFeedSymbolName );
strike.put.pOption = pOption;
}
catch( std::runtime_error& e ) {
// don't do anything with the duplicate
}
break;
}
}
catch ( std::runtime_error& e ) {
std::cout << "LoadChains::fGatherOptions error" << std::endl;
}
}
}
);
}
void Aggregate::FilterChains() {
assert( 0 < m_mapChains.size() );
std::vector<boost::gregorian::date> vChainsToBeRemoved;
size_t nStrikesSum {};
for ( const mapChains_t::value_type& vt: m_mapChains ) { // only use chains where all calls/puts available
size_t nStrikesTotal {};
size_t nStrikesMatch {};
struct MisMatched {
double strike;
std::string side;
MisMatched( double strike_, const std::string& side_ ): strike( strike_ ), side( std::move( side_ ) ) {}
};
std::vector<MisMatched> vMisMatch;
vMisMatch.reserve( 10 );
vt.second.Strikes(
[&nStrikesTotal,&nStrikesMatch,&vMisMatch]( double strike, const chain_t::strike_t& options ){
nStrikesTotal++;
if ( options.call.sIQFeedSymbolName.empty() || options.put.sIQFeedSymbolName.empty() ) {
if ( options.call.sIQFeedSymbolName.empty() ) {
vMisMatch.emplace_back( MisMatched( strike, "C" ) );
}
if ( options.put.sIQFeedSymbolName.empty() ) {
vMisMatch.emplace_back( MisMatched( strike, "P" ) );
}
}
else {
nStrikesMatch++;
}
} );
const std::string sStrikeDate( ou::tf::Instrument::BuildDate( vt.first ) );
bool bChainAdded( true );
if ( nStrikesTotal == nStrikesMatch ) {
nStrikesSum += nStrikesTotal;
std::cout << "chain " << sStrikeDate << " added with " << nStrikesTotal << " strikes" << std::endl;
}
else {
if ( 0 == nStrikesMatch ) {
std::cout << "chain " << sStrikeDate << " skipped with " << nStrikesMatch << '/' << nStrikesTotal << " strikes" << std::endl;
vChainsToBeRemoved.push_back( vt.first );
bChainAdded = false;
}
else {
std::cout
<< "chain " << sStrikeDate << " added " << nStrikesMatch << " strikes without";
for ( const MisMatched& entry: vMisMatch ) {
std::cout << " " << entry.side << entry.strike;
const_cast<chain_t&>( vt.second ).Erase( entry.strike );
}
std::cout << std::endl;
assert( 0 < vt.second.Size() );
}
}
}
assert( vChainsToBeRemoved.size() != m_mapChains.size() );
for ( auto date: vChainsToBeRemoved ) { // remove chains with incomplete info
mapChains_t::iterator iter = m_mapChains.find( date );
m_mapChains.erase( iter );
}
size_t nAverageStrikes = nStrikesSum / m_mapChains.size();
std::cout << "chain size average: " << nAverageStrikes << std::endl;
}
void Aggregate::WalkChains( fDate_t&& fDate ) const {
for ( const mapChains_t::value_type& vt: m_mapChains ) {
fDate( vt.first );
}
}
void Aggregate::WalkChains( fOption_t&& fOption ) const {
for ( const mapChains_t::value_type& vt: m_mapChains ) {
const chain_t& chain( vt.second );
chain.Strikes(
[ fOption ]( double strike, const chain_t::strike_t& options ){
if ( options.call.pOption ) fOption( options.call.pOption );
if ( options.put.pOption ) fOption( options.put.pOption );
});
}
}
void Aggregate::WalkChain( boost::gregorian::date date, fOption_t&& fOption ) const {
mapChains_t::const_iterator iter = m_mapChains.find( date );
if ( m_mapChains.end() == iter ) {
throw std::runtime_error( "Aggregate::WalkChain: date not found" );
}
else {
iter->second.Strikes(
[ fOption = std::move( fOption ) ]( double strike, const chain_t::strike_t& options ){
fOption( options.call.pOption );
fOption( options.put.pOption );
} );
}
}
} // namespace option
} // namespace tf
} // namespace ou