-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathChain.h
753 lines (667 loc) · 25.8 KB
/
Chain.h
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
/************************************************************************
* Copyright(c) 2019, 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. *
************************************************************************/
/*
* File: Chain.h
* Author: [email protected]
* Project: TFOptions
* Created: June 23, 2019, 9:49 PM
*/
#ifndef CHAIN_H
#define CHAIN_H
#include <memory>
#include <cassert>
#include <iostream>
#include <algorithm>
#include <functional>
#include <map>
#include <string>
#include <stdexcept>
#include <TFTimeSeries/DatedDatum.h>
// use this for light weight strike calculations and name lookups
namespace ou { // One Unified
namespace tf { // TradeFrame
namespace option { // options
namespace chain {
struct OptionName { // inherit to add addtiional fields
std::string sIQFeedSymbolName;
OptionName() {}
virtual ~OptionName() {}
OptionName( const std::string& sName_ )
: sIQFeedSymbolName( sName_ ) {}
OptionName( const std::string&& sName_ )
: sIQFeedSymbolName( std::move( sName_ ) ) {}
OptionName( const OptionName& rhs )
: sIQFeedSymbolName( rhs.sIQFeedSymbolName ) {}
OptionName( const OptionName&& rhs )
: sIQFeedSymbolName( std::move( rhs.sIQFeedSymbolName ) ) {}
};
template<typename Option>
struct Strike {
Option call;
Option put;
Strike() {}
Strike( Option&& call_, Option&& put_ )
: call( std::move( call_ ) ),
put( std::move( put_ ) )
{}
Strike( const Strike& rhs )
: call( rhs.call ),
put( rhs.put )
{}
Strike( const Strike&& rhs )
: call( std::move( rhs.call ) ),
put( std::move( rhs.put ) )
{}
};
}
template<typename Option>
class Chain {
public:
using option_t = Option;
using strike_t = chain::Strike<option_t>;
using fStrike_t = std::function<void( double, const strike_t& )>;
Chain() {}
Chain( Chain&& rhs ) {
m_mapChain = std::move( rhs.m_mapChain );
}
virtual ~Chain() {};
struct exception_strike_not_found: public std::runtime_error {
exception_strike_not_found( const char* ch ): std::runtime_error( ch ) {}
};
struct exception_at_start_of_chain: public exception_strike_not_found {
exception_at_start_of_chain( const char* ch ): exception_strike_not_found( ch ) {}
};
Option& SetIQFeedNameCall( double strike, const std::string& sIQFeedSymbolName );
Option& SetIQFeedNamePut( double strike, const std::string& sIQFeedSymbolName );
const std::string GetIQFeedNameCall( double strike ) const;
const std::string GetIQFeedNamePut( double strike ) const;
void Erase( double strike );
const chain::Strike<Option>& GetExistingStrike( double strike ) const;
//const chain::Strike<Option>& GetStrike( double strike ) const;
chain::Strike<Option>& GetStrike( double strike );
double Put_Itm( double ) const ;
double Put_ItmAtm( double ) const;
double Put_Atm( double ) const;
double Put_OtmAtm( double ) const;
double Put_Otm( double ) const;
double Call_Itm( double ) const;
double Call_ItmAtm( double ) const;
double Call_Atm( double ) const;
double Call_OtmAtm( double ) const;
double Call_Otm( double ) const;
double Atm( double ) const;
// returns 0, 1, 2 strikes found
// needs exact match on strikeSource
int AdjacentStrikes( double strikeSource, double& strikeLower, double& strikeUpper ) const;
void Strikes( fStrike_t&& fStrike ) const {
for ( const typename mapChain_t::value_type& vt: m_mapChain ) {
fStrike( vt.first, vt.second );
}
}
using mapChain_t = std::map<double, strike_t>;
struct TrackATM { // inspired by TFOptions/Bundle.cpp
using PriceIV = ou::tf::PriceIV;
using fIvATM_t = std::function<void( const PriceIV& )>;
using iterStrike_t = typename mapChain_t::iterator;
using fWatch_t = std::function<void( strike_t& )>;
mapChain_t& m_mapStrike;
iterStrike_t m_iterUpper;
iterStrike_t m_iterMid;
iterStrike_t m_iterLower;
double m_dblhysteresisUpper;
double m_dblhysteresisLower;
fWatch_t m_fWatchOn;
fWatch_t m_fWatchOff;
fIvATM_t m_fIvATM;
enum EOptionWatchState { EOWSNoWatch, EOWSWatching } m_stateOptionWatch;
TrackATM( mapChain_t& mapChain, fWatch_t&& fWatchOn, fWatch_t&& fWatchOff, fIvATM_t&& fIvATM )
: m_mapStrike( mapChain )
, m_fWatchOn( std::move( fWatchOn ) )
, m_fWatchOff( std::move( fWatchOff ) )
, m_fIvATM( std::move( fIvATM ) )
, m_dblhysteresisUpper {}, m_dblhysteresisLower {}
, m_iterUpper( m_mapStrike.end() )
, m_iterMid( m_mapStrike.end() )
, m_iterLower( m_mapStrike.end() )
, m_stateOptionWatch( EOWSNoWatch )
{
assert( m_fWatchOn );
assert( m_fWatchOff );
assert( m_fIvATM );
}
~TrackATM() {
m_fIvATM = nullptr;
m_fWatchOn = nullptr;
m_fWatchOff = nullptr;
}
void Shift( const double price ) { // called by Track( price )
// uses a 25% edge hysterisis level to force recalc of three containing options
// ie when underlying is within 25% of upper strike or within 25% of lower strike
// uses a 50% hysterisis level to select new set of three containing options
// ie underlying has to be within +/- 50% of mid strike to choose midstrike and corresponding upper/lower strikes
iterStrike_t iterUpper;
iterStrike_t iterLower;
iterUpper = m_mapStrike.lower_bound( price );
if ( m_mapStrike.end() == iterUpper ) {
//std::cout << "TrackATM::Shift: no upper strike available" << std::endl; // stay in no watch state
m_stateOptionWatch = EOWSNoWatch;
}
else {
iterLower = iterUpper;
if ( m_mapStrike.begin() == iterLower ) {
//std::cout << "TrackATM::Shift: no lower strike available" << std::endl; // stay in no watch state
m_stateOptionWatch = EOWSNoWatch;
}
else {
--iterLower;
double dblMidPoint = ( iterUpper->first + iterLower->first ) * 0.5;
if ( price >= dblMidPoint ) { // third strike is above
m_iterUpper = iterUpper;
++m_iterUpper;
if ( m_mapStrike.end() == m_iterUpper ) {
//std::cout << "TrackATM::Shift: no upper upper strike available" << std::endl; // stay in no watch state
m_stateOptionWatch = EOWSNoWatch;
}
else {
m_iterMid = iterUpper;
m_iterLower = iterLower;
m_stateOptionWatch = EOWSWatching;
}
}
else { // third strike is below
m_iterLower = iterLower;
if ( m_mapStrike.begin() == m_iterLower ) {
//std::cout << "TrackATM::Shift: no lower lower strike available" << std::endl; // stay in no watch state
m_stateOptionWatch = EOWSNoWatch;
}
else {
--m_iterLower;
m_iterMid = iterLower;
m_iterUpper = iterUpper;
m_stateOptionWatch = EOWSWatching;
}
}
if ( EOWSWatching == m_stateOptionWatch ) {
m_dblhysteresisUpper = m_iterUpper->first - ( m_iterUpper->first - m_iterMid->first ) * 0.25;
m_dblhysteresisLower = m_iterLower->first + ( m_iterMid->first - m_iterLower->first ) * 0.25;
//std::cout << m_dblhysteresisLower << " < " << price << " < " << m_dblhysteresisUpper << std::endl;
}
else {
// reset values?
}
}
}
} // Shift( price )
void Track( const double price ) {
switch ( m_stateOptionWatch ) {
case EOWSNoWatch:
Shift( price );
if ( EOWSWatching == m_stateOptionWatch ) { // m_stateOptionWatch updated in Shift
m_fWatchOn( m_iterUpper->second );
m_fWatchOn( m_iterMid->second );
m_fWatchOn( m_iterLower->second );
}
break;
case EOWSWatching:
if ( ( price > m_dblhysteresisUpper ) || ( price < m_dblhysteresisLower ) ) {
iterStrike_t iterUpper( m_iterUpper );
iterStrike_t iterMid( m_iterMid );
iterStrike_t iterLower( m_iterLower );
Shift( price );
if ( EOWSWatching == m_stateOptionWatch ) { // by setting on before off allows continuity of capture
m_fWatchOn( m_iterUpper->second );
m_fWatchOn( m_iterMid->second );
m_fWatchOn( m_iterLower->second );
}
m_fWatchOff( iterUpper->second );
m_fWatchOff( iterMid->second );
m_fWatchOff( iterLower->second );
}
break;
}
} // Track( price )
void IV( ptime dtNow, const double dblUnderlying ) { // was CalcIvAtm in "IvAtm.h"
double dblIvCall {};
double dblIvPut {};
Track( dblUnderlying );
switch ( m_stateOptionWatch ) {
case EOWSWatching:
if ( dblUnderlying == m_iterMid->first ) {
dblIvCall = m_iterMid->second.call.pOption->ImpliedVolatility();
dblIvPut = m_iterMid->second.put.pOption->ImpliedVolatility();
}
else {
if ( dblUnderlying > m_iterMid->first ) { // linear interpolation
double ratio = ( dblUnderlying - m_iterMid->first ) / ( m_iterUpper->first - m_iterMid->first );
double iv1, iv2;
iv1 = m_iterMid->second.call.pOption->ImpliedVolatility();
iv2 = m_iterUpper->second.call.pOption->ImpliedVolatility();
dblIvCall = iv1 + ( iv2 - iv1 ) * ratio;
iv1 = m_iterMid->second.put.pOption->ImpliedVolatility();
iv2 = m_iterUpper->second.put.pOption->ImpliedVolatility();
dblIvPut = iv1 + ( iv2 - iv1 ) * ratio;
}
else { // linear interpolation
double ratio = ( dblUnderlying - m_iterLower->first ) / ( m_iterMid->first - m_iterLower->first );
double iv1, iv2;
iv1 = m_iterLower->second.call.pOption->ImpliedVolatility();
iv2 = m_iterMid->second.call.pOption->ImpliedVolatility();
dblIvCall = iv1 + ( iv2 - iv1 ) * ratio;
iv1 = m_iterLower->second.put.pOption->ImpliedVolatility();
iv2 = m_iterMid->second.put.pOption->ImpliedVolatility();
dblIvPut = iv1 + ( iv2 - iv1 ) * ratio;
}
}
{
PriceIV ivATM( dtNow, dblUnderlying, dblIvCall, dblIvPut);
m_fIvATM( ivATM );
}
break;
default:
break;
}
}
}; // struct TrackATM
using pTrackATM_t = std::unique_ptr<TrackATM>;
pTrackATM_t Factory_TrackATM(
typename TrackATM::fWatch_t&& fWatchOn,
typename TrackATM::fWatch_t&& fWatchOff,
typename TrackATM::fIvATM_t&& fIvATM
) {
return std::make_unique<TrackATM>(
m_mapChain, std::move( fWatchOn ), std::move( fWatchOff ), std::move( fIvATM )
);
}
typename mapChain_t::size_type Size() const { return m_mapChain.size(); }
size_t EmitValues() const;
size_t EmitSummary() const;
void Test( double price );
protected:
typename mapChain_t::const_iterator FindStrike( const double strike ) const;
typename mapChain_t::iterator FindStrike( const double strike );
private:
mapChain_t m_mapChain;
};
// methods:
template<typename Option>
double Chain<Option>::Put_Itm( double value ) const { // price < strike
typename mapChain_t::const_iterator iter = std::upper_bound(
m_mapChain.begin(), m_mapChain.end(), value,
[](double value, const typename mapChain_t::value_type& vt)->bool{ return value < vt.first; } );
if ( m_mapChain.end() == iter ) throw exception_strike_not_found( "Put_Itm not found" );
return iter->first;
}
template<typename Option>
double Chain<Option>::Put_ItmAtm( double value ) const { // price <= strike
typename mapChain_t::const_iterator iter = std::lower_bound(
m_mapChain.begin(), m_mapChain.end(), value,
[](const typename mapChain_t::value_type& vt, double value)->bool{ return vt.first < value; } );
if ( m_mapChain.end() == iter ) throw exception_strike_not_found( "Put_ItmAtm not found" );
return iter->first;
}
template<typename Option>
double Chain<Option>::Put_Atm( double value ) const { // closest strike (use itm vs otm)
double atm {};
typename mapChain_t::const_iterator iter1 = std::lower_bound(
m_mapChain.begin(), m_mapChain.end(), value,
[](const typename mapChain_t::value_type& vt, double value)->bool{ return vt.first < value; } );
if ( m_mapChain.end() == iter1 ) throw exception_strike_not_found( "Put_Atm not found" );
if ( value == iter1->first ) {
atm = value;
}
else {
if ( m_mapChain.begin() == iter1 ) {
atm = value;
}
else {
typename mapChain_t::const_iterator iter2 = iter1;
iter2--;
if ( ( iter1->first - value ) < ( value - iter2->first ) ) {
atm = iter1->first;
}
else {
atm = iter2->first;
}
}
}
assert( 0.0 != atm );
return atm;
}
template<typename Option>
double Chain<Option>::Put_OtmAtm( double value ) const { // price >= strike
typename mapChain_t::const_iterator iter = std::lower_bound(
m_mapChain.begin(), m_mapChain.end(), value,
[](const typename mapChain_t::value_type& vt, double value)->bool{ return vt.first < value; } );
if ( m_mapChain.end() == iter ) throw exception_strike_not_found( "Put_OtmAtm not found" );
if ( value == iter->first ) {
// atm
}
else {
if ( m_mapChain.begin() == iter ) {
throw exception_at_start_of_chain( "Put_OtmAtm at begin of chain" );
}
else {
iter--; // strike will be OTM
}
}
return iter->first;
}
template<typename Option>
double Chain<Option>::Put_Otm( double value ) const { // price > strike
typename mapChain_t::const_iterator iter = std::lower_bound(
m_mapChain.begin(), m_mapChain.end(), value,
[](const typename mapChain_t::value_type& vt, double value)->bool{ return vt.first < value; } );
if ( m_mapChain.end() == iter ) throw exception_strike_not_found( "Put_Otm not found" );
if ( m_mapChain.begin() == iter ) {
throw exception_at_start_of_chain( "Put_Otm at begin of chain" );
}
else {
iter--; // strike will be OTM
}
return iter->first;
}
template<typename Option>
double Chain<Option>::Call_Itm( double value ) const { // price > strike
typename mapChain_t::const_iterator iter = std::lower_bound(
m_mapChain.begin(), m_mapChain.end(), value,
[](const typename mapChain_t::value_type& vt, double value)->bool{ return vt.first < value; } );
if ( m_mapChain.end() == iter ) throw exception_strike_not_found( "Call_Itm not found" );
if ( m_mapChain.begin() == iter ) {
throw exception_at_start_of_chain( "Call_Itm at begin of chain" );
}
else {
iter--;
}
return iter->first;
}
template<typename Option>
double Chain<Option>::Call_ItmAtm( double value ) const { // price >= strike
typename mapChain_t::const_iterator iter = std::lower_bound(
m_mapChain.begin(), m_mapChain.end(), value,
[](const typename mapChain_t::value_type& vt, double value)->bool{ return vt.first < value; } );
if ( m_mapChain.end() == iter ) throw exception_strike_not_found( "Call_ItmAtm not found" );
if ( value == iter->first ) {
// atm
}
else {
if ( m_mapChain.begin() == iter ) {
throw exception_at_start_of_chain( "Call_ItmAtm at begin of chain" );
}
else {
iter--; // strike will be Itm
}
}
return iter->first;
}
template<typename Option>
double Chain<Option>::Call_Atm( double value ) const { // closest strike (use itm vs otm)
double atm {};
typename mapChain_t::const_iterator iter1 = std::lower_bound(
m_mapChain.begin(), m_mapChain.end(), value,
[](const typename mapChain_t::value_type& vt, double value)->bool{ return vt.first < value; } );
if ( m_mapChain.end() == iter1 ) throw exception_strike_not_found( "Call_Atm not found" );
if ( value == iter1->first ) {
atm = value;
}
else {
if ( m_mapChain.begin() == iter1 ) {
atm = value;
}
else {
typename mapChain_t::const_iterator iter2 = iter1;
iter2--;
if ( ( iter1->first - value ) < ( value - iter2->first ) ) {
atm = iter1->first;
}
else {
atm = iter2->first;
}
}
}
assert( 0.0 != atm );
return atm;
}
template<typename Option>
double Chain<Option>::Call_OtmAtm( double value ) const { // price <= strike
typename mapChain_t::const_iterator iter = std::lower_bound(
m_mapChain.begin(), m_mapChain.end(), value,
[](const typename mapChain_t::value_type& vt, double value)->bool{ return vt.first < value; } );
if ( m_mapChain.end() == iter ) throw exception_strike_not_found( "Call_OtmAtm not found" );
return iter->first;
}
template<typename Option>
double Chain<Option>::Call_Otm( double value ) const { // price < strike
typename mapChain_t::const_iterator iter = std::upper_bound(
m_mapChain.begin(), m_mapChain.end(), value,
[](double value, const typename mapChain_t::value_type& vt)->bool{ return value < vt.first; } );
if ( m_mapChain.end() == iter ) throw exception_strike_not_found( "Call_Otm not found" );
return iter->first;
}
template<typename Option>
double Chain<Option>::Atm( double value ) const { // closest strike (use itm vs otm)
// https://en.cppreference.com/w/cpp/algorithm/lower_bound std::map::lower_bound preferred
//typename mapChain_t::const_iterator iter1 = std::lower_bound(
// [](const typename mapChain_t::value_type& vt, double value)->bool{ return vt.first < value; } );
double atm( 0.0 );
typename mapChain_t::const_iterator iterStrikeCandidate = m_mapChain.lower_bound( value );
if ( m_mapChain.end() == iterStrikeCandidate ) throw exception_strike_not_found( "Atm not found" );
if ( value == iterStrikeCandidate->first ) {
atm = value;
}
else {
if ( m_mapChain.begin() == iterStrikeCandidate ) {
atm = value;
}
else {
typename mapChain_t::const_iterator iterStrikeLower( iterStrikeCandidate );
iterStrikeLower--;
if ( ( iterStrikeCandidate->first - value ) < ( value - iterStrikeLower->first ) ) {
atm = iterStrikeCandidate->first;
}
else {
atm = iterStrikeLower->first;
}
}
}
assert( 0.0 != atm );
return atm;
}
template<typename Option>
int Chain<Option>::AdjacentStrikes( double strikeSource, double& strikeLower, double& strikeUpper ) const {
strikeLower = strikeUpper = 0.0;
int nReturn {};
typename mapChain_t::const_iterator iterSource = m_mapChain.find( strikeSource );
if ( m_mapChain.end() != iterSource ) {
typename mapChain_t::const_iterator iterLower = iterSource;
if ( m_mapChain.begin() != iterLower ) {
iterLower--;
strikeLower = iterLower->first;
nReturn++;
}
typename mapChain_t::const_iterator iterUpper = iterSource;
iterUpper++;
if ( m_mapChain.end() != iterUpper ) {
strikeUpper = iterUpper->first;
nReturn++;
}
}
return nReturn;
}
template<typename Option>
Option& Chain<Option>::SetIQFeedNameCall( double dblStrike, const std::string& sIQFeedSymbolName ) {
typename mapChain_t::iterator iter = m_mapChain.find( dblStrike );
if ( m_mapChain.end() == iter ) {
auto result = m_mapChain.emplace( typename mapChain_t::value_type( dblStrike, strike_t() ) );
assert( result.second );
iter = result.first;
}
if ( iter->second.call.sIQFeedSymbolName.empty() ) {
iter->second.call.sIQFeedSymbolName = sIQFeedSymbolName;
}
else {
std::cout
<< "Chain<Option>::SetIQFeedNameCall duplicate existing: "
<< iter->second.call.sIQFeedSymbolName
<< ", new "
<< sIQFeedSymbolName
<< ", skipped"
<< std::endl;
throw std::runtime_error( "duplicate call" );
// maybe throw an exception and let caller handle it: ignore or not
//assert( iter->second.call.sIQFeedSymbolName == sIQFeedSymbolName );
}
return iter->second.call;
}
template<typename Option>
Option& Chain<Option>::SetIQFeedNamePut( double dblStrike, const std::string& sIQFeedSymbolName ) {
typename mapChain_t::iterator iter = m_mapChain.find( dblStrike );
if ( m_mapChain.end() == iter ) {
auto result = m_mapChain.emplace( typename mapChain_t::value_type( dblStrike, strike_t() ) );
assert( result.second );
iter = result.first;
}
if ( iter->second.put.sIQFeedSymbolName.empty() ) {
iter->second.put.sIQFeedSymbolName = sIQFeedSymbolName;
}
else {
std::cout
<< "Chain<Option>::SetIQFeedNamePut duplicate existing: "
<< iter->second.put.sIQFeedSymbolName
<< ", new "
<< sIQFeedSymbolName
<< ", skipped"
<< std::endl;
throw std::runtime_error( "duplicate put" );
// maybe throw an exception and let caller handle it: ignore or not
//assert( iter->second.put.sIQFeedSymbolName == sIQFeedSymbolName );
}
return iter->second.put;
}
template<typename Option>
const std::string Chain<Option>::GetIQFeedNameCall( double dblStrike ) const {
typename mapChain_t::const_iterator iter = FindStrike( dblStrike );
return iter->second.call.sIQFeedSymbolName;
}
template<typename Option>
const std::string Chain<Option>::GetIQFeedNamePut( double dblStrike ) const {
typename mapChain_t::const_iterator iter = FindStrike( dblStrike );
return iter->second.put.sIQFeedSymbolName;
}
template<typename Option>
void Chain<Option>::Erase( double dblStrike ) {
typename mapChain_t::const_iterator iter = FindStrike( dblStrike );
m_mapChain.erase( iter );
}
template<typename Option>
const chain::Strike<Option>& Chain<Option>::GetExistingStrike( double dblStrike ) const { // this one doesn't make much sense
typename mapChain_t::const_iterator citer = m_mapChain.find( dblStrike );
if ( m_mapChain.end() == citer ) {
throw exception_strike_not_found( "Chain::GetExistingStrike const: no strike" );
}
return citer->second;
}
//template<typename Option>
//const chain::Strike<Option>& Chain<Option>::GetStrike( double dblStrike ) const { // this one doesn't make much sense
// typename mapChain_t::iterator iter = m_mapChain.find( dblStrike );
// if ( m_mapChain.end() == iter ) {
// iter = m_mapChain.insert( m_mapChain.begin(), std::move( typename mapChain_t::value_type( dblStrike, strike_t() ) ) );
// }
// return iter->second;
//}
template<typename Option>
chain::Strike<Option>& Chain<Option>::GetStrike( double dblStrike ) {
typename mapChain_t::iterator iter = m_mapChain.find( dblStrike );
if ( m_mapChain.end() == iter ) {
auto result = m_mapChain.emplace( typename mapChain_t::value_type( dblStrike, strike_t() ) );
assert( result.second );
iter = result.first;
}
return iter->second;
}
// const iterator
template<typename Option>
typename Chain<Option>::mapChain_t::const_iterator Chain<Option>::FindStrike( const double strike ) const {
typename mapChain_t::const_iterator iter = m_mapChain.find( strike );
if ( m_mapChain.end() == iter ) {
throw exception_strike_not_found( "Chain::FindStrike const: no strike" );
}
return iter;
}
// regular iterator
template<typename Option>
typename Chain<Option>::mapChain_t::iterator Chain<Option>::FindStrike( const double strike ) {
typename mapChain_t::iterator iter = m_mapChain.find( strike );
if ( m_mapChain.end() == iter ) {
std::cout
<< "Chain::FindStrike error: "
<< "strike " << strike
<< ", m_mapChain size=" << m_mapChain.size()
<< std::endl;
throw exception_strike_not_found( "Chain::FindStrike: no strike" );
}
return iter;
}
template<typename Option>
size_t Chain<Option>::EmitValues() const { // TODO: supply output stream
size_t cnt {};
std::for_each( m_mapChain.begin(), m_mapChain.end(), [&cnt](const typename mapChain_t::value_type& vt){
cnt++;
std::cout
<< vt.first << ": "
<< vt.second.call.sIQFeedSymbolName
<< ", "
<< vt.second.put.sIQFeedSymbolName
<< std::endl;
});
return cnt;
}
template<typename Option>
size_t Chain<Option>::EmitSummary() const { // TODO: supply output stream
size_t nStrikes {};
size_t nCalls {};
size_t nPuts {};
std::for_each( m_mapChain.begin(), m_mapChain.end(), [ &nStrikes, &nCalls, &nPuts](const typename mapChain_t::value_type& vt){
nStrikes++;
if ( 0 != vt.second.call.sIQFeedSymbolName.size() ) nCalls++;
if ( 0 != vt.second.put.sIQFeedSymbolName.size() ) nPuts++;
});
std::cout
<< " #strikes=" << nStrikes
<< ", #calls=" << nCalls
<< ", #puts=" << nPuts
<< std::endl;
return ( nCalls + nPuts );
}
template<typename Option>
void Chain<Option>::Test( double price ) { // TODO: supply output stream
try {
std::cout << "Put_Itm: " << Put_Itm( price ) << std::endl;
std::cout << "Put_ItmAtm: " << Put_ItmAtm( price ) << std::endl;
std::cout << "Put_Atm: " << Put_Atm( price ) << std::endl;
std::cout << "Put_OtmAtm: " << Put_OtmAtm( price ) << std::endl;
std::cout << "Put_Otm: " << Put_Otm( price ) << std::endl;
std::cout << "Call_Itm: " << Call_Itm( price ) << std::endl;
std::cout << "Call_ItmAtm: " << Call_ItmAtm( price ) << std::endl;
std::cout << "Call_Atm: " << Call_Atm( price ) << std::endl;
std::cout << "Call_OtmAtm: " << Call_OtmAtm( price ) << std::endl;
std::cout << "Call_Otm: " << Call_Otm( price ) << std::endl;
}
catch ( std::runtime_error& e ) {
std::cout << "Chain::Test runtime error: " << e.what() << std::endl;
}
}
} // namespace option
} // namespace tf
} // namespace ou
#endif /* CHAIN_H */