-
Notifications
You must be signed in to change notification settings - Fork 165
/
Copy pathBundle.h
271 lines (193 loc) · 10.1 KB
/
Bundle.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
/************************************************************************
* 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 2012/07/10
#pragma once
#include <map>
#include <OUCommon/Delegate.h>
#include <TFTrading/PortfolioManager.h>
#include <TFTrading/Watch.h>
#include <TFTimeSeries/BarFactory.h>
#include <TFIQFeed/MarketSymbol.h>
#include "NoRiskInterestRateSeries.h"
#include "Binomial.h"
#include "Strike.h"
namespace ou { // One Unified
namespace tf { // TradeFrame
namespace option { // options
class ExpiryBundle {
friend std::ostream& operator<<( std::ostream& os, const ExpiryBundle& );
public:
typedef Instrument::pInstrument_t pInstrument_t;
typedef ou::tf::ProviderInterfaceBase::pProvider_t pProvider_t;
typedef Watch::pWatch_t pWatch_t;
ExpiryBundle(void);
virtual ~ExpiryBundle(void);
typedef FastDelegate1<ou::tf::option::Strike&> OnStrikeWatch_t; // used from the MultiBundle level
ou::Delegate<ou::tf::option::Strike&> OnStrikeWatchOn;
ou::Delegate<ou::tf::option::Strike&> OnStrikeWatchOff;
typedef FastDelegate1<const ou::tf::PriceIVExpiry&> OnAtmIvCalc_t;
ou::Delegate<const ou::tf::PriceIVExpiry&> OnAtmIvCalc;
// void SetUnderlying( pInstrument_t pInstrument, pProvider_t pProvider );
void SetCall( pInstrument_t pInstrument, pProvider_t pDataProvider, pProvider_t pGreekProvider );
void SetPut( pInstrument_t pInstrument, pProvider_t pDataProvider, pProvider_t pGreekProvider );
void SetCall( pInstrument_t pInstrument, pProvider_t pDataProvider );
void SetPut( pInstrument_t pInstrument, pProvider_t pDataProvider );
Call* GetCall( double dblStrike );
Put* GetPut( double dblStrike );
void FindAdjacentStrikes( double dblValue, double& dblLower, double& dblUpper ); // uses <= and >= logic around dblStrike, therefore possibility of dblLower = dblUpper
void UpdateATMWatch( double dblValue );
void SetWatchableOn( double dblStrike ); // each strike is not watchable by default
void SetWatchableOff( double dblStrike );
void SetWatchOn( double dblStrike, bool bForce = false ); // watch only selected call/put at strike, force watchable on
void SetWatchOff( double dblStrike, bool bForce = false ); // forces watchable off when true
void StartWatch( void ); // watch underlying plus all watchable options
void StopWatch( void );
void SaveSeries( const std::string& sPrefix60sec, const std::string& sPrefix86400sec );
void EmitValues( double dblPriceUnderlying );
void SetExpiry( ptime dt ); // utc
void CalcGreeks( double dblUnderlying, double dblVolHistorical, ptime now, ou::tf::LiborFromIQFeed& libor );
protected:
private:
typedef std::map<double,Strike> mapStrikes_t;
typedef mapStrikes_t::iterator mapStrikes_iter_t;
enum EOptionWatchState { EOWSNoWatch, EOWSWatching } m_stateOptionWatch;
ptime m_dtExpiry; // eg, 4pm EST third Fri of month for normal US equity options, in utc
double m_dblUpperTrigger;
double m_dblLowerTrigger;
ou::tf::PriceIVExpirys m_tsAtmIv;
ou::tf::BarFactory m_bfIVUnderlyingCall;
ou::tf::BarFactory m_bfIVUnderlyingPut;
mapStrikes_iter_t m_iterUpper;
mapStrikes_iter_t m_iterMid;
mapStrikes_iter_t m_iterLower;
mapStrikes_t m_mapStrikes;
mapStrikes_iter_t FindStrike( double strike );
mapStrikes_iter_t FindStrikeAuto( double strike ); // Auto insert new strike
void RecalcATMWatch( double dblValue );
void CalcGreeksAtStrike( ptime now, mapStrikes_iter_t iter, ou::tf::option::binomial::structInput& input );
void SaveAtmIv( const std::string& sPrefix, const std::string& sPrefix86400Min );
};
std::ostream& operator<<( std::ostream& os, const ExpiryBundle& );
// =======================================================
class ExpiryBundleWithUnderlying: public ExpiryBundle {
public:
ExpiryBundleWithUnderlying();
~ExpiryBundleWithUnderlying();
typedef Instrument::pInstrument_t pInstrument_t;
typedef ou::tf::ProviderInterfaceBase::pProvider_t pProvider_t;
typedef Watch::pWatch_t pWatch_t;
void SetUnderlying( pInstrument_t pInstrument, pProvider_t pProvider );
void StartWatch();
void StopWatch();
void EmitValues();
void SaveSeries( const std::string& sPrefix60sec, const std::string& sPrefix86400sec );
protected:
private:
pWatch_t m_pwatchUnderlying;
};
// =======================================================
class MultiExpiryBundle {
friend std::ostream& operator<<( std::ostream& os, const MultiExpiryBundle& );
public:
typedef ou::tf::ProviderInterfaceBase::pProvider_t pProvider_t;
typedef ou::tf::Portfolio::pPortfolio_t pPortfolio_t;
typedef ou::tf::Position::pPosition_t pPosition_t;
typedef ou::tf::Instrument::pInstrument_t pInstrument_t;
typedef Watch::pWatch_t pWatch_t;
MultiExpiryBundle( const std::string& sName ): m_sName( sName ) {};
virtual ~MultiExpiryBundle( void );
const std::string& Name( void ) { return m_sName; };
pPortfolio_t& Portfolio( void ) { return m_pPortfolio; };
void SetWatchUnderlying( pInstrument_t& pInstrument, pProvider_t& pProvider );
pWatch_t GetWatchUnderlying( void ) { return m_pWatchUnderlying; };
// the references are void when the map has insertions or deletions
bool ExpiryBundleExists( boost::gregorian::date );
ExpiryBundle& GetExpiryBundle( boost::gregorian::date );
//ExpiryBundle& CreateExpiryBundle( boost::gregorian::date );
ExpiryBundle& CreateExpiryBundle( boost::posix_time::ptime dt ); // date portion used for map, full ptime for expiry calcs
void StartWatch( void );
void StopWatch( void );
void CalcIV( ptime dtNow /*utc*/, ou::tf::LiborFromIQFeed& libor );
void SaveData( const std::string& sPrefixSession, const std::string& sPrefix86400sec );
void AssignOption( pInstrument_t pInstrument, pProvider_t pDataProvider, pProvider_t pGreekProvider );
void AddOnStrikeWatchOn( ExpiryBundle::OnStrikeWatch_t );
void RemoveOnStrikeWatchOn( ExpiryBundle::OnStrikeWatch_t );
void AddOnStrikeWatchOff( ExpiryBundle::OnStrikeWatch_t );
void RemoveOnStrikeWatchOff( ExpiryBundle::OnStrikeWatch_t );
void AddOnAtmIv( ExpiryBundle::OnAtmIvCalc_t );
void RemoveOnAtmIv( ExpiryBundle::OnAtmIvCalc_t );
protected:
private:
typedef std::map<boost::gregorian::date,ExpiryBundle> mapExpiryBundles_t;
std::string m_sName;
pPortfolio_t m_pPortfolio; // summary portfolio for all things related to symbols in this Bundle
pWatch_t m_pWatchUnderlying;
mapExpiryBundles_t m_mapExpiryBundles;
void HandleUnderlyingQuote( const ou::tf::Quote& quote );
void HandleUnderlyingTrade( const ou::tf::Trade& trade ) {};
};
std::ostream& operator<<( std::ostream& os, const MultiExpiryBundle& );
// PopulateMultiExpiryBundle used like:
// m_listIQFeedSymbols.SelectOptionsByUnderlying( sName, PopulateMultiExpiryBundle( *m_pBundle, m_pData1Provider, pNull ) );
struct PopulateMultiExpiryBundle {
typedef ou::tf::Instrument::pInstrument_t pInstrument_t;
typedef ou::tf::ProviderInterfaceBase::pProvider_t pProvider_t;
PopulateMultiExpiryBundle(
ou::tf::option::MultiExpiryBundle& bundle, pProvider_t pDataProvider_, pProvider_t pGreekProvider_ )
: meb( bundle ), pDataProvider( pDataProvider_ ), pGreekProvider( pGreekProvider_ )
{}
void operator()( const ou::tf::iqfeed::MarketSymbol::TableRowDef& trd ) {
//assert( trd.sUnderlying == meb.Name() );
assert( ( ou::tf::iqfeed::ESecurityType::IEOption == trd.sc )
|| ( ou::tf::iqfeed::ESecurityType::FOption == trd.sc )
);
// to trade, when is the IB contract number acquired?
// and when does the symbol get registered, so it can be traded?
//boost::gregorian::date dateTrdExpiry( trd.nYear, trd.nMonth, trd.nDay - 1 ); // IQFeed dates are on Saturday
boost::gregorian::date dateTrdExpiry( trd.nYear, trd.nMonth, trd.nDay ); // fix IQFeed dates elsewhere
if ( meb.ExpiryBundleExists( dateTrdExpiry ) ) {
pInstrument_t pInstrument;
std::string side;
side = trd.eOptionSide;
std::stringstream ss;
ss << trd.sUnderlying << " " << dateTrdExpiry << " " << side << " " << trd.dblStrike;
switch ( trd.sc ) {
case ou::tf::iqfeed::ESecurityType::IEOption:
pInstrument.reset(
new ou::tf::Instrument(
ss.str(), ou::tf::InstrumentType::Option, "SMART",
dateTrdExpiry.year(), dateTrdExpiry.month(), dateTrdExpiry.day(),
// meb.GetWatchUnderlying()->GetInstrument(),
trd.eOptionSide, trd.dblStrike ) );
break;
case ou::tf::iqfeed::ESecurityType::FOption:
pInstrument.reset(
new ou::tf::Instrument(
ss.str(), ou::tf::InstrumentType::FuturesOption, "SMART",
dateTrdExpiry.year(), dateTrdExpiry.month(), dateTrdExpiry.day(),
// meb.GetWatchUnderlying()->GetInstrument(),
trd.eOptionSide, trd.dblStrike ) );
break;
}
pInstrument->SetAlternateName( ou::tf::Instrument::eidProvider_t::EProviderIQF, trd.sSymbol );
meb.AssignOption( pInstrument, pDataProvider, pGreekProvider );
}
}
ou::tf::option::MultiExpiryBundle& meb;
pProvider_t pDataProvider;
pProvider_t pGreekProvider;
};
} // namespace option
} // namespace tf
} // namespace ou