-
Notifications
You must be signed in to change notification settings - Fork 157
/
Portfolio.cpp
278 lines (215 loc) · 10.7 KB
/
Portfolio.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
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
/************************************************************************
* Copyright(c) 2009, One Unified. All rights reserved. *
* *
* 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. *
************************************************************************/
#include <stdexcept>
#include "Portfolio.h"
// 2013/07/25
// * position currency must match portfolio currency
// * currency ratio between from sub-portfolio and portfolio needs to be maintained
// * top level portfolio is always ratio 1
// * first level of sub-portfolios can be alternate currency, and have non one ratio
// * also portfolio type as 'multi-legged position'
// * no further level sub-portfolios can have different currency
// * so.. master portfolio, sub-currency portfolio, regular portfolio
namespace ou { // One Unified
namespace tf { // TradeFrame
//Portfolio::Portfolio( // in memory
// const idPortfolio_t& idPortfolio, EPortfolioType ePortfolioType, currency_t sCurrency, const std::string& sDescription )
//: m_row( idPortfolio, "", "", ePortfolioType, sCurrency, sDescription )
//{
//}
//Portfolio::Portfolio( // master portfolio currency record
// const idPortfolio_t& idPortfolio,
// const idAccountOwner_t& idAccountOwner, currency_t sCurrency,
// const std::string& sDescription )
//: m_row( idPortfolio, idAccountOwner, "", Master, sCurrency, sDescription )
//{
//}
Portfolio::Portfolio( // portfolio record
const idPortfolio_t& idPortfolio, const idAccountOwner_t& idAccountOwner, const idPortfolio_t& idOwner,
EPortfolioType ePortfolioType, currency_t sCurrency, const std::string& sDescription )
: m_row( idPortfolio, idAccountOwner, idOwner, ePortfolioType, sCurrency, sDescription )
{
bool bOk = true;
if ( "" == idPortfolio ) bOk = false;
if ( ( Master == ePortfolioType ) && ( "" != idOwner ) ) bOk = false;
if ( ( "" != idAccountOwner ) && ( Master != ePortfolioType ) && ( "" == idOwner ) ) bOk = false;
if ( !bOk ) {
throw std::runtime_error( "Portfolio::Portfolio parameter problems" );
}
}
Portfolio::Portfolio( const TableRowDef& row )
: m_row( row )
{
m_plCurrent.dblCommissionsPaid = m_row.dblCommissionsPaid;
m_plCurrent.dblRealized = m_row.dblRealizedPL;
m_plCurrent.Sum();
m_plMax = m_plMin = m_plCurrent;
}
Portfolio::~Portfolio(void) {
}
Portfolio::pPosition_t Portfolio::AddPosition( const std::string &sName, pPosition_t pPosition ) {
// prepare to add position to user named map
mapPositions_iter_t iterUser = m_mapPositionsViaUserName.find( sName );
if ( m_mapPositionsViaUserName.end() != iterUser ) {
throw std::runtime_error( "Portfolio::Add1 position " + sName + " already exists" );
}
m_mapPositionsViaUserName.insert( mapPositions_pair_t( sName, pPosition ) );
pPosition->OnUnRealizedPL.Add( MakeDelegate( this, &Portfolio::HandleUnRealizedPL ) );
pPosition->OnExecution.Add( MakeDelegate( this, &Portfolio::HandleExecution ) );
pPosition->OnCommission.Add( MakeDelegate( this, &Portfolio::HandleCommission ) );
return pPosition;
}
void Portfolio::DeletePosition( const std::string& sName ) {
mapPositions_iter_t iterUser = m_mapPositionsViaUserName.find( sName );
if ( m_mapPositionsViaUserName.end() == iterUser ) {
throw std::runtime_error( "Portfolio::Delete1 position does not exist" );
}
iterUser->second->OnCommission.Remove( MakeDelegate( this, &Portfolio::HandleCommission ) );
iterUser->second->OnExecution.Remove( MakeDelegate( this, &Portfolio::HandleExecution ) );
iterUser->second->OnUnRealizedPL.Remove( MakeDelegate( this, &Portfolio::HandleUnRealizedPL ) );
m_mapPositionsViaUserName.erase( iterUser );
}
void Portfolio::RenamePosition( const std::string& sOld, const std::string& sNew ) {
mapPositions_iter_t iter;
iter = m_mapPositionsViaUserName.find( sNew );
if ( m_mapPositionsViaUserName.end() == iter ) {
throw std::runtime_error( "Portfolio::Rename New position already exists" );
}
iter = m_mapPositionsViaUserName.find( sOld );
if ( m_mapPositionsViaUserName.end() == iter ) {
throw std::runtime_error( "Portfolio::Rename Old position does not exist" );
}
pPosition_t pPosition( iter->second );
m_mapPositionsViaUserName.erase( iter );
m_mapPositionsViaUserName.insert( mapPositions_pair_t( sNew, pPosition ) );
}
Portfolio::pPosition_t Portfolio::GetPosition( const std::string& sName ) {
mapPositions_iter_t iter = m_mapPositionsViaUserName.find( sName );
if ( m_mapPositionsViaUserName.end() == iter ) {
throw std::runtime_error( "Portfolio::GetPosition position does not exist" );
}
return iter->second;
}
// not used
void Portfolio::ReCalc( void ) {
m_plCurrent.Zero();
for ( mapPositions_iter_t iter = m_mapPositionsViaUserName.begin(); iter != m_mapPositionsViaUserName.end(); ++iter ) {
m_plCurrent.dblUnRealized += iter->second->GetUnRealizedPL();
m_plCurrent.dblRealized += iter->second->GetRealizedPL();
m_plCurrent.dblCommissionsPaid += iter->second->GetCommissionPaid();
}
m_plCurrent.Sum();
if ( m_plCurrent > m_plMax ) m_plMax = m_plCurrent;
if ( m_plCurrent < m_plMin ) m_plMin = m_plCurrent;
m_row.dblRealizedPL = m_plCurrent.dblRealized;
}
void Portfolio::AddSubPortfolio( pPortfolio_t& pPortfolio ) {
if ( Master == pPortfolio->GetRow().ePortfolioType ) {
//throw std::runtime_error( "Portfolio::AddSubPortfolio: sub-portfolio cannot be a master portfolio" );
std::cout << "Master portfolio found" << std::endl;
}
else {
if ( Master == m_row.ePortfolioType ) {
// if this portfolio is master, no problem
}
else {
// if sub portfolio is currency summary portfolio, and this isn't master, then this needs to be master portfolio
if ( CurrencySummary == pPortfolio->GetRow().ePortfolioType ) {
throw std::runtime_error( "Portfolio::AddSubPortfolio: alternate currency portfolio only attachable to master portfolio" );
}
}
const idPortfolio_t& idSubPortfolio( pPortfolio->GetRow().idPortfolio );
mapPortfolios_iter_t iter = m_mapSubPortfolios.find( idSubPortfolio );
if ( m_mapSubPortfolios.end() != iter ) {
throw std::runtime_error( "Portfolio::AddSubPortfolio portfolio already exists: " + idSubPortfolio );
}
m_mapSubPortfolios[ idSubPortfolio ] = pPortfolio;
pPortfolio->OnCommission.Add( MakeDelegate( this, &Portfolio::HandleCommission ) );
pPortfolio->OnExecution.Add( MakeDelegate( this, &Portfolio::HandleExecution ) );
pPortfolio->OnUnRealizedPL.Add( MakeDelegate( this, &Portfolio::HandleUnRealizedPL ) );
}
}
void Portfolio::RemoveSubPortfolio( const idPortfolio_t& idPortfolio ) {
mapPortfolios_iter_t iter = m_mapSubPortfolios.find( idPortfolio );
if ( m_mapSubPortfolios.end() != iter ) {
throw std::runtime_error( "Portfolio::RemoveSubPortfolio portfolio does not exist: " + idPortfolio );
}
Portfolio* pPortfolio = iter->second.get();
pPortfolio->OnCommission.Add( MakeDelegate( this, &Portfolio::HandleCommission ) );
pPortfolio->OnExecution.Add( MakeDelegate( this, &Portfolio::HandleExecution ) );
pPortfolio->OnUnRealizedPL.Add( MakeDelegate( this, &Portfolio::HandleUnRealizedPL ) );
m_mapSubPortfolios.erase( iter );
}
/*
void Portfolio::SetOwnerPortfolio( const idPortfolio_t& idOwner, pPortfolio_t& pPortfolio ) {
if ( idOwner != m_row.idOwner ) {
throw std::runtime_error( "Portfolio::SetOwnerPortfoio " + idOwner + " does not match " + m_row.idOwner );
}
m_pOwnerPortfolio = pPortfolio;
}
*/
// as positions and portfolios get attached, they should perform an initial update of
// unrealized, realized, & commission (if non-zero)
void Portfolio::HandleUnRealizedPL( const PositionDelta_delegate_t& position ) {
m_plCurrent.dblUnRealized += ( -position.get<1>() + position.get<2>() );
// m_row.db.dblUnRealized = m_plCurrent.dblUnRealized;
m_plCurrent.Sum();
if ( m_plCurrent > m_plMax ) m_plMax.dblUnRealized = m_plCurrent.dblUnRealized;
if ( m_plCurrent < m_plMin ) m_plMin.dblUnRealized = m_plCurrent.dblUnRealized;
// need to propogate up portfolios yet
OnUnRealizedPL( position );
OnUnRealizedPLUpdate( *this );
}
void Portfolio::HandleExecution( const PositionDelta_delegate_t& position ) {
m_row.dblRealizedPL += ( -position.get<1>() + position.get<2>() );
m_plCurrent.dblRealized = m_row.dblRealizedPL;
m_plCurrent.Sum();
if ( m_plCurrent > m_plMax ) m_plMax.dblRealized = m_plCurrent.dblRealized;
if ( m_plCurrent < m_plMin ) m_plMin.dblRealized = m_plCurrent.dblRealized;
// need to propogate up portfolios yet
OnExecution( position );
OnExecutionUpdate( *this );
}
void Portfolio::HandleCommission( const PositionDelta_delegate_t& position ) {
m_row.dblCommissionsPaid += ( -position.get<1>() + position.get<2>() );
m_plCurrent.dblCommissionsPaid = m_row.dblCommissionsPaid;
m_plCurrent.Sum();
if ( m_plCurrent > m_plMax ) m_plMax.dblCommissionsPaid = m_plCurrent.dblCommissionsPaid;
if ( m_plCurrent < m_plMin ) m_plMin.dblCommissionsPaid = m_plCurrent.dblCommissionsPaid;
// need to propogate up portfolios yet
OnCommission( position );
OnCommissionUpdate( *this );
}
std::ostream& operator<<( std::ostream& os, const Portfolio& portfolio ) {
for ( Portfolio::mapPositions_t::const_iterator iter = portfolio.m_mapPositionsViaUserName.begin();
portfolio.m_mapPositionsViaUserName.end() != iter;
++iter ) {
os << iter->second;
}
os << "Portfolio URPL=" << portfolio.m_plCurrent.dblUnRealized
<< ", RPL=" << portfolio.m_plCurrent.dblRealized
<< ", Comm=" << portfolio.m_plCurrent.dblCommissionsPaid
<< "=> PL-C=" << portfolio.m_plCurrent.dblRealized - portfolio.m_plCurrent.dblCommissionsPaid
<< ": Min=" << portfolio.m_plMin.dblNet
<< ", Net=" << portfolio.m_plCurrent.dblNet
<< ", Max=" << portfolio.m_plMax.dblNet
;
return os;
}
void Portfolio::SetActive( bool bActive ) {
if ( bActive != m_row.bActive ) {
m_row.bActive = bActive;
}
}
} // namespace tf
} // namespace ou