-
Notifications
You must be signed in to change notification settings - Fork 156
/
Order.h
310 lines (272 loc) · 15.4 KB
/
Order.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
/************************************************************************
* 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. *
************************************************************************/
// should provider be included? No, this allows an order routing process to select an
// an appropriate provider based upon other criteria
// the provider will be associated later for Execution evaluation
#pragma once
#include <string>
#include <memory>
#include <stdexcept>
#include <OUCommon/Delegate.h>
#include "KeyTypes.h"
#include "Execution.h"
#include "Instrument.h"
namespace ou { // One Unified
namespace tf { // TradeFrame
class OrderManager;
class Order {
friend class OrderManager;
public:
using idOrder_t = keytypes::idOrder_t;
using idPosition_t = keytypes::idPosition_t;
using pInstrument_t = Instrument::pInstrument_t;
using idInstrument_t = keytypes::idInstrument_t;
using pOrder_t = std::shared_ptr<Order>;
using pOrder_ref = const pOrder_t&;
using quantity_t = std::uint32_t;
using idExecution_t = std::uint32_t;
struct TableRowDef {
template<class A>
void Fields( A& a ) {
ou::db::Field( a, "orderid", idOrder );
ou::db::Field( a, "positionid", idPosition );
ou::db::Field( a, "instrumentid", idInstrument );
ou::db::Field( a, "reference", sReference );
ou::db::Field( a, "description", sDescription );
ou::db::Field( a, "timeinforce", eTimeInForce );
ou::db::Field( a, "goodtilldate", dtGoodTillDate );
ou::db::Field( a, "goodaftertime", dtGoodAfterTime );
ou::db::Field( a, "parentid", idParent );
ou::db::Field( a, "transmit", bTransmit );
ou::db::Field( a, "outsiderth", bOutsideRTH );
ou::db::Field( a, "orderstatus", eOrderStatus );
ou::db::Field( a, "ordertype", eOrderType );
ou::db::Field( a, "orderside", eOrderSide );
ou::db::Field( a, "price1", dblPrice1 );
ou::db::Field( a, "price2", dblPrice2 );
ou::db::Field( a, "signalprice", dblSignalPrice );
ou::db::Field( a, "quantityordered", nOrderQuantity );
ou::db::Field( a, "quantityremaining", nQuantityRemaining );
ou::db::Field( a, "quantityfilled", nQuantityFilled );
ou::db::Field( a, "quantitypaired", nQuantityPaired ); // used for pairing profitable trades
ou::db::Field( a, "averagefillprice", dblAverageFillPrice );
ou::db::Field( a, "commission", dblCommission );
ou::db::Field( a, "datetimecreated", dtOrderCreated );
ou::db::Field( a, "datetimesubmitted", dtOrderSubmitted );
ou::db::Field( a, "datetimeclosed", dtOrderClosed );
}
//"create index idx_orders_positionid on orders( positionid );",
idOrder_t idOrder;
idPosition_t idPosition;
idInstrument_t idInstrument;
std::string sReference; // typically, unique order id from broker
std::string sDescription;
ETimeInForce eTimeInForce;
ptime dtGoodTillDate;
ptime dtGoodAfterTime;
idOrder_t idParent;
bool bTransmit;
bool bOutsideRTH;
OrderStatus::EOrderStatus eOrderStatus;
OrderType::EOrderType eOrderType;
OrderSide::EOrderSide eOrderSide;
double dblPrice1; // for limit
double dblPrice2; // for stop
double dblSignalPrice; // mark at which algorithm requested order
quantity_t nOrderQuantity;
quantity_t nQuantityRemaining;
quantity_t nQuantityFilled;
quantity_t nQuantityPaired;
double dblAverageFillPrice; // excludes commission
double dblCommission;
ptime dtOrderCreated;
ptime dtOrderSubmitted;
ptime dtOrderClosed;
TableRowDef() // default constructor
: idOrder( 0 ), idPosition( 0 ),
eTimeInForce( ou::tf::ETimeInForce::Day ),
dtGoodTillDate( boost::date_time::not_a_date_time ), dtGoodAfterTime( boost::date_time::not_a_date_time ),
idParent( 0 ), bTransmit( true ), bOutsideRTH( false ),
eOrderStatus( OrderStatus::Created ), eOrderType( OrderType::Unknown ), eOrderSide( OrderSide::Unknown ),
dblPrice1( 0.0 ), dblPrice2( 0.0 ), dblSignalPrice( 0.0 ),
nOrderQuantity( 0 ), nQuantityRemaining( 0 ), nQuantityFilled( 0 ), nQuantityPaired( 0 ),
dblAverageFillPrice( 0.0 ), dblCommission( 0.0 ) {};
TableRowDef( // market order
idPosition_t idPosition_, idInstrument_t idInstrument_, OrderType::EOrderType eOrderType_, OrderSide::EOrderSide eOrderSide_,
quantity_t nOrderQuantity_, ptime dtOrderSubmitted_ )
: idOrder( 0 ), idPosition( idPosition_ ), idInstrument( idInstrument_ ),
eTimeInForce( ou::tf::ETimeInForce::Day ),
dtGoodTillDate( boost::date_time::not_a_date_time ), dtGoodAfterTime( boost::date_time::not_a_date_time ),
idParent( 0 ), bTransmit( true ), bOutsideRTH( false ),
eOrderStatus( OrderStatus::Created ),
eOrderType( eOrderType_ ), eOrderSide( eOrderSide_ ), dblPrice1( 0.0 ), dblPrice2( 0.0 ), dblSignalPrice( 0.0 ),
nOrderQuantity( nOrderQuantity_ ), nQuantityRemaining( nOrderQuantity_ ), nQuantityFilled( 0 ), nQuantityPaired( 0 ),
dblAverageFillPrice( 0 ), dblCommission( 0 ), dtOrderCreated( boost::date_time::not_a_date_time ),
dtOrderSubmitted( dtOrderSubmitted_ ), dtOrderClosed( boost::date_time::not_a_date_time ) {};
TableRowDef( // limit or stop
idPosition_t idPosition_, idInstrument_t idInstrument_, OrderType::EOrderType eOrderType_, OrderSide::EOrderSide eOrderSide_,
quantity_t nOrderQuantity_, double dblPrice1_, ptime dtOrderSubmitted_ )
: idOrder( 0 ), idPosition( idPosition_ ), idInstrument( idInstrument_ ),
eTimeInForce( ou::tf::ETimeInForce::Day ),
dtGoodTillDate( boost::date_time::not_a_date_time ), dtGoodAfterTime( boost::date_time::not_a_date_time ),
idParent( 0 ), bTransmit( true ), bOutsideRTH( false ),
eOrderStatus( OrderStatus::Created ),
eOrderType( eOrderType_ ), eOrderSide( eOrderSide_ ), dblPrice1( dblPrice1_ ), dblPrice2( 0.0 ), dblSignalPrice( 0.0 ),
nOrderQuantity( nOrderQuantity_ ), nQuantityRemaining( nOrderQuantity_ ), nQuantityFilled( 0 ), nQuantityPaired( 0 ),
dblAverageFillPrice( 0 ), dblCommission( 0 ), dtOrderCreated( boost::date_time::not_a_date_time ),
dtOrderSubmitted( dtOrderSubmitted_ ), dtOrderClosed( boost::date_time::not_a_date_time ) {};
TableRowDef( // limit and stop
idPosition_t idPosition_, idInstrument_t idInstrument_, OrderType::EOrderType eOrderType_, OrderSide::EOrderSide eOrderSide_,
quantity_t nOrderQuantity_, double dblPrice1_, double dblPrice2_, ptime dtOrderSubmitted_ )
: idOrder( 0 ), idPosition( idPosition_ ), idInstrument( idInstrument_ ),
eTimeInForce( ou::tf::ETimeInForce::Day ),
dtGoodTillDate( boost::date_time::not_a_date_time ), dtGoodAfterTime( boost::date_time::not_a_date_time ),
idParent( 0 ), bTransmit( true ), bOutsideRTH( false ),
eOrderStatus( OrderStatus::Created ),
eOrderType( eOrderType_ ), eOrderSide( eOrderSide_ ), dblPrice1( dblPrice1_ ), dblPrice2( dblPrice2_ ), dblSignalPrice( 0.0 ),
nOrderQuantity( nOrderQuantity_ ), nQuantityRemaining( nOrderQuantity_ ), nQuantityFilled( 0 ), nQuantityPaired( 0 ),
dblAverageFillPrice( 0 ), dblCommission( 0 ), dtOrderCreated( boost::date_time::not_a_date_time ),
dtOrderSubmitted( dtOrderSubmitted_ ), dtOrderClosed( boost::date_time::not_a_date_time ) {};
TableRowDef( // complete row provided
idOrder_t idOrder_, idPosition_t idPosition_, idInstrument_t idInstrument_, std::string sDescription_,
OrderStatus::EOrderStatus eOrderStatus_, OrderType::EOrderType eOrderType_, OrderSide::EOrderSide eOrderSide_,
double dblPrice1_, double dblPrice2_, double dblSignalPrice_,
quantity_t nOrderQuantity_, quantity_t nQuantityRemaining_, quantity_t nQuantityFilled_,
double dblAverageFillPrice_, double dblCommission_,
ptime dtOrderCreated_, ptime dtOrderSubmitted_, ptime dtOrderClosed_ )
: idOrder( idOrder_ ), idPosition( idPosition_ ), idInstrument( idInstrument_ ), sDescription( sDescription_ ),
eOrderStatus( eOrderStatus_ ), eOrderType( eOrderType_ ),
eTimeInForce( ou::tf::ETimeInForce::Day ),
dtGoodTillDate( boost::date_time::not_a_date_time ), dtGoodAfterTime( boost::date_time::not_a_date_time ),
idParent( 0 ), bTransmit( true ), bOutsideRTH( false ),
eOrderSide( eOrderSide_ ),
dblPrice1( dblPrice1_ ), dblPrice2( dblPrice2_ ), dblSignalPrice( dblSignalPrice_ ),
nOrderQuantity( nOrderQuantity_ ), nQuantityRemaining( nQuantityRemaining_ ), nQuantityFilled( nQuantityFilled_ ), nQuantityPaired( 0 ),
dblAverageFillPrice( dblAverageFillPrice_ ), dblCommission( dblCommission_ ),
dtOrderCreated( dtOrderCreated_ ), dtOrderSubmitted( dtOrderSubmitted_ ), dtOrderClosed( dtOrderClosed_ ) {};
};
struct TableCreateDef: TableRowDef {
template<class A>
void Fields( A& a ) {
TableRowDef::Fields( a );
ou::db::Key( a, "orderid" );
ou::db::Constraint( a, "positionid", tablenames::sPosition, "positionid" );
ou::db::Constraint( a, "instrumentid", tablenames::sInstrument, "instrumentid" );
}
};
Order() = delete;
Order( // market
Instrument::pInstrument_cref instrument,
OrderType::EOrderType eOrderType,
OrderSide::EOrderSide eOrderSide,
quantity_t nOrderQuantity,
idPosition_t idPosition = 0,
ptime dtOrderSubmitted = not_a_date_time
);
Order( // limit or stop
Instrument::pInstrument_cref instrument,
OrderType::EOrderType eOrderType,
OrderSide::EOrderSide eOrderSide,
quantity_t nOrderQuantity,
double dblPrice1,
idPosition_t idPosition = 0,
ptime dtOrderSubmitted = not_a_date_time
);
Order( // limit and stop
Instrument::pInstrument_cref instrument,
OrderType::EOrderType eOrderType,
OrderSide::EOrderSide eOrderSide,
quantity_t nOrderQuantity,
double dblPrice1,
double dblPrice2,
idPosition_t idPosition = 0,
ptime dtOrderSubmitted = not_a_date_time
);
Order( const TableRowDef& row, pInstrument_t& pInstrument );
~Order();
void SetParentOrderId( idOrder_t idParent ) { m_row.idParent = idParent; }
idOrder_t GetParentOrderId() const { return m_row.idParent; }
void SetReference( const std::string& sReference ) { m_row.sReference = sReference; }
const std::string& GetReference() const { return m_row.sReference; }
void SetOutsideRTH( bool bOutsideRTH ) { m_row.bOutsideRTH = bOutsideRTH; }; // need to persist to db
bool GetOutsideRTH() const { return m_row.bOutsideRTH; };
void SetTimeInForce( ou::tf::ETimeInForce tif ) { m_row.eTimeInForce = tif; } // need to persist to db
ou::tf::ETimeInForce GetTimeInForce() const { return m_row.eTimeInForce; }
void SetTransmit( bool bTransmit ) { m_row.bTransmit = bTransmit; } // need to persist to db
bool GetTransmit() const { return m_row.bTransmit; }
void SetGoodTillDate( ptime dtGTD ) { m_row.dtGoodTillDate = dtGTD; } // need to persist to db
ptime GetGoodTillDate() const { return m_row.dtGoodTillDate; }
void SetGoodAfterTime( ptime dtGAT ) { m_row.dtGoodAfterTime = dtGAT; } // need to persist to db
ptime GetGoodAfterTime() const { return m_row.dtGoodAfterTime; }
Instrument::pInstrument_t GetInstrument() const {
if ( NULL == m_pInstrument.get() ) {
throw std::runtime_error( "Order::GetInstrument: no instrument defined" );
}
return m_pInstrument;
};
const char *GetOrderSideName() const { return OrderSide::Name[ m_row.eOrderSide ]; };
OrderType::EOrderType GetOrderType() const { return m_row.eOrderType; };
OrderSide::EOrderSide GetOrderSide() const { return m_row.eOrderSide; };
double GetPrice1() const { return m_row.dblPrice1; }; // need to validate this on creation
void SetPrice1( double dblPrice ) { m_row.dblPrice1 = dblPrice; } // prepares for UpdatePrice
double GetPrice2() const { return m_row.dblPrice2; };
void SetPrice2( double dblPrice ) { m_row.dblPrice2 = dblPrice; } // prepares for UpdatePrice
double GetAverageFillPrice() const { return m_row.dblAverageFillPrice; };
idOrder_t GetOrderId() const { assert( 0 != m_row.idOrder ); return m_row.idOrder; };
idExecution_t GetNextExecutionId() { return ++m_nNextExecutionId; };
void SetSendingToProvider();
OrderStatus::EOrderStatus ReportExecution( const Execution &exec ); // called from OrderManager
void SetCommission( double dblCommission );
double GetCommission() const{ return m_row.dblCommission; };
void ActOnError( OrderError::EOrderError eError );
quantity_t GetQuanRemaining() const { return m_row.nQuantityRemaining; };
quantity_t GetQuanOrdered() const { return m_row.nOrderQuantity; };
quantity_t GetQuanFilled() const { return m_row.nQuantityFilled; };
void SetSignalPrice( double dblSignalPrice ) { m_row.dblSignalPrice = dblSignalPrice; };
double GetSignalPrice() const { return m_row.dblSignalPrice; };
void SetDescription( const std::string& sDescription ) { m_row.sDescription = sDescription; }
const std::string& GetDescription() const { return m_row.sDescription; }
const ptime &GetDateTimeOrderCreated() const {
assert( not_a_date_time != m_row.dtOrderCreated ); // is this a valid test?
return m_row.dtOrderCreated;
};
const ptime &GetDateTimeOrderSubmitted() const {
assert( not_a_date_time != m_row.dtOrderSubmitted ); // is this a valid test?
return m_row.dtOrderSubmitted;
};
const ptime &GetDateTimeOrderFilled() const {
assert( not_a_date_time != m_row.dtOrderClosed ); // is this a valid test?
return m_row.dtOrderClosed;
};
double GetIncrementalCommission() const { return m_dblIncrementalCommission; };
void MarkAsCancelled(); // called from OrderManager
ou::Delegate<const std::pair<const Order&, const Execution&>& > OnExecution;
ou::Delegate<const Order&> OnOrderCancelled;
ou::Delegate<const Order&> OnPartialFill; // on intermediate fills only
ou::Delegate<const Order&> OnOrderFilled; // on final fill
ou::Delegate<const Order&> OnCommission;
OrderStatus::EOrderStatus OrderStatus() const { return m_row.eOrderStatus; }
const TableRowDef& GetRow() const { return m_row; };
protected:
Instrument::pInstrument_t m_pInstrument;
idExecution_t m_nNextExecutionId; // when is this used?
// statistics and status
double m_dblPriceXQuantity; // used for calculating average price
void ConstructOrder();
void SetOrderId( idOrder_t ); // use OrderManager to construct orders
private:
TableRowDef m_row;
double m_dblIncrementalCommission; // Something for the Position Manager as commission is updated for the order.
};
} // namespace tf
} // namespace ou