-
Notifications
You must be signed in to change notification settings - Fork 154
/
Chains.h
131 lines (108 loc) · 4.61 KB
/
Chains.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
/************************************************************************
* 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: Chains.h
* Author: raymond@burkholder.net
* Project: TFOptions
* Created on May 29, 2021, 21:51
*/
#pragma once
#include <string>
#include <boost/log/trivial.hpp>
#include <boost/date_time/gregorian/greg_date.hpp>
#include "Exceptions.h"
#include "GatherOptions.h"
namespace ou { // One Unified
namespace tf { // TradeFrame
namespace option { // options
// for reference:
// using fGatherOptions_t = std::function<void(const std::string& sUnderlying, fOption_t&&)>;
template<typename mapChains_t>
typename mapChains_t::iterator GetChain( mapChains_t& map, pOption_t pOption ) { // find existing expiry, or create new one
using chain_t = typename mapChains_t::mapped_type;
using iterator_t = typename mapChains_t::iterator;
chain_t chain; // default chain for insertion into new entry
const boost::gregorian::date date( pOption->GetExpiry() );
iterator_t iterChains = map.find( date ); // see if expiry date exists
if ( map.end() == iterChains ) { // insert new expiry set if not
BOOST_LOG_TRIVIAL(info)
<< "GetChain created: "
<< date.year() << "/"
<< date.month().as_number() << "/"
<< date.day()
<< " with "
<< pOption->GetInstrumentName()
;
iterChains = map.insert(
map.begin(),
typename mapChains_t::value_type( date, std::move( chain ) )
);
}
return iterChains;
}
template<typename chain_t, typename OptionEntry>
OptionEntry* UpdateOption( chain_t& chain, pOption_t pOption ) {
// populate new call or put, no test for pre-existance
//std::cout << " option: " << row.sSymbol << std::endl;
OptionEntry* pOptionEntry {};
try {
switch ( pOption->GetOptionSide() ) {
case ou::tf::OptionSide::Call:
{
OptionEntry& entry( chain.SetIQFeedNameCall( pOption->GetStrike(), pOption->GetInstrument()->GetInstrumentName( ou::tf::Instrument::eidProvider_t::EProviderIQF ) ) );
pOptionEntry = &entry;
}
break;
case ou::tf::OptionSide::Put:
{
OptionEntry& entry( chain.SetIQFeedNamePut( pOption->GetStrike(), pOption->GetInstrument()->GetInstrumentName( ou::tf::Instrument::eidProvider_t::EProviderIQF ) ) );
pOptionEntry = &entry;
}
break;
default:
assert( false );
break;
}
}
catch ( std::runtime_error& e ) {
BOOST_LOG_TRIVIAL(error) << "option::UpdateOption error";
}
return pOptionEntry;
}
template<typename mapChains_t> // used for populating option names into a default chain
void PopulateMap( mapChains_t& map, const std::string& sUnderlying, fGatherOptions_t&& fGatherOptions ) {
fGatherOptions(
sUnderlying,
[&map](pOption_t pOption){ // these are iqfeed based symbol names
using chain_t = typename mapChains_t::mapped_type;
using iterator_t = typename mapChains_t::iterator;
using OptionEntry = typename chain_t::option_t;
iterator_t iterChains = GetChain( map, pOption );
UpdateOption<chain_t,OptionEntry>( iterChains->second, pOption );
});
}
template<typename mapChains_t>
static typename mapChains_t::const_iterator SelectChain( const mapChains_t& mapChains, boost::gregorian::date date, boost::gregorian::days daysToExpiry ) {
typename mapChains_t::const_iterator citerChain = std::find_if( mapChains.begin(), mapChains.end(),
[date,daysToExpiry](const typename mapChains_t::value_type& vt)->bool{
return daysToExpiry <= ( vt.first - date ); // first chain where trading date less than expiry date
} );
if ( mapChains.end() == citerChain ) {
throw ou::tf::option::exception_chain_not_found( "option::SelectChain" );
}
return citerChain;
}
} // namespace option
} // namespace tf
} // namespace ou