-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathOrder_Combo.cpp
211 lines (184 loc) · 5.89 KB
/
Order_Combo.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
/************************************************************************
* Copyright(c) 2023, 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: Order_Combo.cpp
* Author: [email protected]
* Project: TFTrading
* Created: March 22, 2023 13:38:52
*/
/*
Goal:
* collect orders for a combo, monitor for execution, notify on completion
* https://interactivebrokers.github.io/tws-api/classIBApi_1_1Order.html
* add WaitForSpreadStats to stabilize
Note:
* IB supports OPT, STK, FUT, not FOP
* https://interactivebrokers.github.io/tws-api/basic_orders.html#combolimit
* https://interactivebrokers.github.io/tws-api/order_submission.html#order_attach
* therefore submit FuturesOptions as individual orders for now
* but give it a try sometime, once integration with Position is available
For Later:
* https://interactivebrokers.github.io/tws-api/oca.html#oca_types One Cancels All
*/
#include "Order_Combo.hpp"
namespace ou { // One Unified
namespace tf { // TradeFrame
// m_mo is MonitorOrder
OrderCombo_TrackLeg::OrderCombo_TrackLeg( pPosition_t pPosition_, uint32_t nQuantity_, ou::tf::OrderSide::EOrderSide side_, fLegDone_t&& fLegDone_ )
: state( State::leg_add )
, m_nQuantity( nQuantity_ ), m_side( side_ ), m_fLegDone( std::move( fLegDone_ ) )
, m_mo( pPosition_ )
{}
OrderCombo_TrackLeg::OrderCombo_TrackLeg( pPosition_t pPosition_, fLegDone_t&& fLegDone_ )
: state( State::leg_close )
, m_nQuantity {}, m_fLegDone( std::move( fLegDone_ ) )
, m_mo( pPosition_ )
{}
void OrderCombo_TrackLeg::Submit() {
switch ( state ) {
case State::leg_add:
m_mo.PlaceOrder( m_nQuantity, m_side );
break;
case State::leg_close:
m_mo.ClosePosition();
break;
default:
assert( false );
}
state = State::active;
}
bool OrderCombo_TrackLeg::Tick( ptime dt ) {
bool bDone( true ); // done once legs have transitioned away from 'active'
m_mo.Tick( dt );
switch ( state ) {
case State::leg_add:
case State::leg_close:
break;
case State::active:
if ( m_mo.IsActive() ) {
bDone = false;
}
else {
state = State::done;
if ( m_fLegDone ) m_fLegDone();
}
break;
case State::done:
break;
} // switch( state )
return bDone;
}
void OrderCombo_TrackLeg::Cancel() {
m_mo.CancelOrder();
}
// ==================
OrderCombo::OrderCombo()
: m_state( EState::bare )
, m_fComboDone( nullptr )
{
}
OrderCombo::~OrderCombo() {
m_vTrack.clear();
m_fComboDone = nullptr;
}
void OrderCombo::AddLeg( pPosition_t pPosition, uint32_t nOrderQuantity, ou::tf::OrderSide::EOrderSide side, fLegDone_t&& fLegDone ) {
switch ( m_state ) {
case EState::bare:
m_state = EState::loading;
// fall through
case EState::loading: {
// implicit place order
OrderCombo_TrackLeg& track( m_vTrack.emplace_back( std::move( OrderCombo_TrackLeg( std::move( pPosition), nOrderQuantity, side, std::move( fLegDone ) ) ) ) );
}
break;
case EState::placing:
case EState::monitoring:
case EState::done:
assert( false );
break;
default:
assert( false );
break;
}
}
void OrderCombo::CloseLeg( pPosition_t pPosition, fLegDone_t&& fLegDone ) {
switch ( m_state ) {
case EState::bare:
m_state = EState::loading;
// fall through
case EState::loading: {
// implicit close leg
OrderCombo_TrackLeg& track( m_vTrack.emplace_back( std::move( OrderCombo_TrackLeg( std::move( pPosition), std::move( fLegDone ) ) ) ) );
}
break;
case EState::placing:
case EState::monitoring:
case EState::done:
assert( false );
break;
default:
assert( false );
break;
}
}
void OrderCombo::Submit( fComboDone_t&& fComboDone ) {
if ( EState::bare == m_state ) {
std::cout << "OrderCombo::Submit: no legs found" << std::endl;
fComboDone();
}
else {
assert( EState::loading == m_state );
m_state = EState::placing;
m_fComboDone = std::move( fComboDone );
for ( vTrack_t::value_type& entry: m_vTrack ) {
entry.Submit();
}
m_state = EState::monitoring;
}
}
void OrderCombo::Tick( ptime dt ) {
switch ( m_state ) {
case EState::cancel: // need a tick or two to finish
case EState::monitoring: {
bool bDone( true );
for ( vTrack_t::value_type& entry: m_vTrack ) {
bDone &= entry.Tick( dt );
}
if ( bDone ) {
if ( m_fComboDone ) m_fComboDone();
m_state = EState::done;
}
}
break;
default:
break;
}
}
// may have a timing issue with the Active transition in MonitorOrder on each Tick
void OrderCombo::Cancel( fComboDone_t&& fComboDone ) {
switch ( m_state ) {
case EState::monitoring:
m_state = EState::cancel;
m_fComboDone = std::move( fComboDone ); // overwrite previous flavour -
for ( vTrack_t::value_type& vt: m_vTrack ) {
vt.Cancel(); // TODO: this needs to force the completion
}
break;
default:
std::cout << "OrderCombo::Cancel is out of state" << std::endl;
break;
}
}
} // namespace tf
} // namespace ou