-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathEClientSocketBaseImpl.h
3448 lines (2869 loc) · 92.1 KB
/
EClientSocketBaseImpl.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef eclientsocketbaseimpl_h__INCLUDED
#define eclientsocketbaseimpl_h__INCLUDED
#include "StdAfx.h"
#include "EClientSocketBase.h"
#include "EWrapper.h"
#include "TwsSocketClientErrors.h"
#include "Contract.h"
#include "Order.h"
#include "OrderState.h"
#include "Execution.h"
#include "ScannerSubscription.h"
#include "CommissionReport.h"
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <assert.h>
/////////////////////////////////////////////////////////////////////////////////
// SOCKET CLIENT VERSION CHANGE LOG : Incremented when the format of incomming
// server responses change
/////////////////////////////////////////////////////////////////////////////////
// constants
// 6 = Added parentId to orderStatus
// 7 = The new execDetails event returned for an order filled status and reqExecDetails
// Also added market depth support.
// 8 = Added 'lastFillPrice' to orderStatus and 'permId' to execDetails
// 9 = Added 'avgCost', 'unrealizedPNL', and 'unrealizedPNL' to updatePortfolio event
// 10 = Added 'serverId' to the 'open order' & 'order status' events.
// We send back all the API open orders upon connection.
// Added new methods reqAllOpenOrders, reqAutoOpenOrders()
// Added FA support - reqExecution has filter.
// - reqAccountUpdates takes acct code.
// 11 = Added permId to openOrder event.
// 12 = Added IgnoreRth, hidden, and discretionaryAmt
// 13 = Added GoodAfterTime
// 14 = always send size on bid/ask/last tick
// 15 = send allocation string with open order
// 16 = can receive account name in account and portfolio updates, and fa params in openOrder
// 17 = can receive liquidation field in exec reports, and notAutoAvailable field in mkt data
// 18 = can receive good till date field in open order messages, and send backfill requests
// 19 = can receive new extended order attributes in OPEN_ORDER
// 20 = expects TWS time string on connection after server version >= 20, and parentId in open order
// 21 = can receive bond contract details.
// 22 = can receive price magnifier in contract details
// 23 = support for scanner
// 24 = can receive volatility order parameters in open order messages
// 25 = can receive HMDS query start and end times
// 26 = can receive option vols in option market data messages
// 27 = can receive delta neutral order type and delta neutral aux price
// 28 = can receive option model computation ticks
// 29 = can receive trail stop limit price in open order and can place them: API 8.91
// 30 = can receive extended bond contract def, new ticks, and trade count in bars
// 31 = can receive EFP extensions to scanner and market data, and combo legs on open orders
// ; can receive RT bars
// 32 = can receive TickType.LAST_TIMESTAMP
// 33 = can receive ScaleNumComponents and ScaleComponentSize is open order messages
// 34 = can receive whatIf orders / order state
// 35 = can receive contId field for Contract objects
// 36 = can receive outsideRth field for Order objects
// 37 = can receive clearingAccount and clearingIntent for Order objects
// 38 = can receive multipier and primaryExchange in portfolio updates
// ; can receive cumQty and avgPrice in execution
// ; can receive fundamental data
// ; can receive underComp for Contract objects
// ; can receive reqId and end marker in contractDetails/bondContractDetails
// ; can receive ScaleInitComponentSize and ScaleSubsComponentSize for Order objects
// 39 = can receive underConId in contractDetails
// 40 = can receive algoStrategy/algoParams in openOrder
// 41 = can receive end marker for openOrder
// ; can receive end marker for account download
// ; can receive end marker for executions download
// 42 = can receive deltaNeutralValidation
// 43 = can receive longName(companyName)
// ; can receive listingExchange
// ; can receive RTVolume tick
// 44 = can receive end market for ticker snapshot
// 45 = can receive notHeld field in openOrder
// 46 = can receive contractMonth, industry, category, subcategory fields in contractDetails
// ; can receive timeZoneId, tradingHours, liquidHours fields in contractDetails
// 47 = can receive gamma, vega, theta, undPrice fields in TICK_OPTION_COMPUTATION
// 48 = can receive exemptCode in openOrder
// 49 = can receive hedgeType and hedgeParam in openOrder
// 50 = can receive optOutSmartRouting field in openOrder
// 51 = can receive smartComboRoutingParams in openOrder
// 52 = can receive deltaNeutralConId, deltaNeutralSettlingFirm, deltaNeutralClearingAccount and deltaNeutralClearingIntent in openOrder
// 53 = can receive orderRef in execution
// 54 = can receive scale order fields (PriceAdjustValue, PriceAdjustInterval, ProfitOffset, AutoReset,
// InitPosition, InitFillQty and RandomPercent) in openOrder
// 55 = can receive orderComboLegs (price) in openOrder
// 56 = can receive trailingPercent in openOrder
// 57 = can receive commissionReport message
// 58 = can receive CUSIP/ISIN/etc. in contractDescription/bondContractDescription
// 59 = can receive evRule, evMultiplier in contractDescription/bondContractDescription/executionDetails
// can receive multiplier in executionDetails
const int CLIENT_VERSION = 59;
const int SERVER_VERSION = 38;
// outgoing msg id's
const int REQ_MKT_DATA = 1;
const int CANCEL_MKT_DATA = 2;
const int PLACE_ORDER = 3;
const int CANCEL_ORDER = 4;
const int REQ_OPEN_ORDERS = 5;
const int REQ_ACCT_DATA = 6;
const int REQ_EXECUTIONS = 7;
const int REQ_IDS = 8;
const int REQ_CONTRACT_DATA = 9;
const int REQ_MKT_DEPTH = 10;
const int CANCEL_MKT_DEPTH = 11;
const int REQ_NEWS_BULLETINS = 12;
const int CANCEL_NEWS_BULLETINS = 13;
const int SET_SERVER_LOGLEVEL = 14;
const int REQ_AUTO_OPEN_ORDERS = 15;
const int REQ_ALL_OPEN_ORDERS = 16;
const int REQ_MANAGED_ACCTS = 17;
const int REQ_FA = 18;
const int REPLACE_FA = 19;
const int REQ_HISTORICAL_DATA = 20;
const int EXERCISE_OPTIONS = 21;
const int REQ_SCANNER_SUBSCRIPTION = 22;
const int CANCEL_SCANNER_SUBSCRIPTION = 23;
const int REQ_SCANNER_PARAMETERS = 24;
const int CANCEL_HISTORICAL_DATA = 25;
const int REQ_CURRENT_TIME = 49;
const int REQ_REAL_TIME_BARS = 50;
const int CANCEL_REAL_TIME_BARS = 51;
const int REQ_FUNDAMENTAL_DATA = 52;
const int CANCEL_FUNDAMENTAL_DATA = 53;
const int REQ_CALC_IMPLIED_VOLAT = 54;
const int REQ_CALC_OPTION_PRICE = 55;
const int CANCEL_CALC_IMPLIED_VOLAT = 56;
const int CANCEL_CALC_OPTION_PRICE = 57;
const int REQ_GLOBAL_CANCEL = 58;
const int REQ_MARKET_DATA_TYPE = 59;
//const int MIN_SERVER_VER_REAL_TIME_BARS = 34;
//const int MIN_SERVER_VER_SCALE_ORDERS = 35;
//const int MIN_SERVER_VER_SNAPSHOT_MKT_DATA = 35;
//const int MIN_SERVER_VER_SSHORT_COMBO_LEGS = 35;
//const int MIN_SERVER_VER_WHAT_IF_ORDERS = 36;
//const int MIN_SERVER_VER_CONTRACT_CONID = 37;
const int MIN_SERVER_VER_PTA_ORDERS = 39;
const int MIN_SERVER_VER_FUNDAMENTAL_DATA = 40;
const int MIN_SERVER_VER_UNDER_COMP = 40;
const int MIN_SERVER_VER_CONTRACT_DATA_CHAIN = 40;
const int MIN_SERVER_VER_SCALE_ORDERS2 = 40;
const int MIN_SERVER_VER_ALGO_ORDERS = 41;
const int MIN_SERVER_VER_EXECUTION_DATA_CHAIN = 42;
const int MIN_SERVER_VER_NOT_HELD = 44;
const int MIN_SERVER_VER_SEC_ID_TYPE = 45;
const int MIN_SERVER_VER_PLACE_ORDER_CONID = 46;
const int MIN_SERVER_VER_REQ_MKT_DATA_CONID = 47;
const int MIN_SERVER_VER_REQ_CALC_IMPLIED_VOLAT = 49;
const int MIN_SERVER_VER_REQ_CALC_OPTION_PRICE = 50;
const int MIN_SERVER_VER_CANCEL_CALC_IMPLIED_VOLAT = 50;
const int MIN_SERVER_VER_CANCEL_CALC_OPTION_PRICE = 50;
const int MIN_SERVER_VER_SSHORTX_OLD = 51;
const int MIN_SERVER_VER_SSHORTX = 52;
const int MIN_SERVER_VER_REQ_GLOBAL_CANCEL = 53;
const int MIN_SERVER_VER_HEDGE_ORDERS = 54;
const int MIN_SERVER_VER_REQ_MARKET_DATA_TYPE = 55;
const int MIN_SERVER_VER_OPT_OUT_SMART_ROUTING = 56;
const int MIN_SERVER_VER_SMART_COMBO_ROUTING_PARAMS = 57;
const int MIN_SERVER_VER_DELTA_NEUTRAL_CONID = 58;
const int MIN_SERVER_VER_SCALE_ORDERS3 = 60;
const int MIN_SERVER_VER_ORDER_COMBO_LEGS_PRICE = 61;
const int MIN_SERVER_VER_TRAILING_PERCENT = 62;
// incoming msg id's
const int TICK_PRICE = 1;
const int TICK_SIZE = 2;
const int ORDER_STATUS = 3;
const int ERR_MSG = 4;
const int OPEN_ORDER = 5;
const int ACCT_VALUE = 6;
const int PORTFOLIO_VALUE = 7;
const int ACCT_UPDATE_TIME = 8;
const int NEXT_VALID_ID = 9;
const int CONTRACT_DATA = 10;
const int EXECUTION_DATA = 11;
const int MARKET_DEPTH = 12;
const int MARKET_DEPTH_L2 = 13;
const int NEWS_BULLETINS = 14;
const int MANAGED_ACCTS = 15;
const int RECEIVE_FA = 16;
const int HISTORICAL_DATA = 17;
const int BOND_CONTRACT_DATA = 18;
const int SCANNER_PARAMETERS = 19;
const int SCANNER_DATA = 20;
const int TICK_OPTION_COMPUTATION = 21;
const int TICK_GENERIC = 45;
const int TICK_STRING = 46;
const int TICK_EFP = 47;
const int CURRENT_TIME = 49;
const int REAL_TIME_BARS = 50;
const int FUNDAMENTAL_DATA = 51;
const int CONTRACT_DATA_END = 52;
const int OPEN_ORDER_END = 53;
const int ACCT_DOWNLOAD_END = 54;
const int EXECUTION_DATA_END = 55;
const int DELTA_NEUTRAL_VALIDATION = 56;
const int TICK_SNAPSHOT_END = 57;
const int MARKET_DATA_TYPE = 58;
const int COMMISSION_REPORT = 59;
// TWS New Bulletins constants
const int NEWS_MSG = 1; // standard IB news bulleting message
const int EXCHANGE_AVAIL_MSG = 2; // control message specifing that an exchange is available for trading
const int EXCHANGE_UNAVAIL_MSG = 3; // control message specifing that an exchange is unavailable for trading
///////////////////////////////////////////////////////////
// helper macroses
#define DECODE_FIELD(x) if (!DecodeField(x, ptr, endPtr)) return 0;
#define DECODE_FIELD_MAX(x) if (!DecodeFieldMax(x, ptr, endPtr)) return 0;
#define ENCODE_FIELD(x) EncodeField(msg, x);
#define ENCODE_FIELD_MAX(x) EncodeFieldMax(msg, x);
///////////////////////////////////////////////////////////
// helper structures
namespace {
struct BarData {
IBString date;
double open;
double high;
double low;
double close;
int volume;
double average;
IBString hasGaps;
int barCount;
};
struct ScanData {
ContractDetails contract;
int rank;
IBString distance;
IBString benchmark;
IBString projection;
IBString legsStr;
};
} // end of anonymous namespace
///////////////////////////////////////////////////////////
// encoders
template<class T>
void EClientSocketBase::EncodeField(std::ostream& os, T value)
{
os << value << '\0';
}
template<>
void EClientSocketBase::EncodeField<bool>(std::ostream& os, bool boolValue)
{
EncodeField<int>(os, boolValue ? 1 : 0);
}
template<>
void EClientSocketBase::EncodeField<double>(std::ostream& os, double doubleValue)
{
char str[128];
snprintf(str, sizeof(str), "%.10g", doubleValue);
EncodeField<const char*>(os, str);
}
///////////////////////////////////////////////////////////
// "max" encoders
void EClientSocketBase::EncodeFieldMax(std::ostream& os, int intValue)
{
if( intValue == INT_MAX) {
EncodeField(os, "");
return;
}
EncodeField(os, intValue);
}
void EClientSocketBase::EncodeFieldMax(std::ostream& os, double doubleValue)
{
if( doubleValue == DBL_MAX) {
EncodeField(os, "");
return;
}
EncodeField(os, doubleValue);
}
///////////////////////////////////////////////////////////
// decoders
bool EClientSocketBase::CheckOffset(const char* ptr, const char* endPtr)
{
assert (ptr && ptr <= endPtr);
return (ptr && ptr < endPtr);
}
const char* EClientSocketBase::FindFieldEnd(const char* ptr, const char* endPtr)
{
return (const char*)memchr(ptr, 0, endPtr - ptr);
}
bool EClientSocketBase::DecodeField(bool& boolValue, const char*& ptr, const char* endPtr)
{
int intValue;
if( !DecodeField(intValue, ptr, endPtr))
return false;
boolValue = (intValue > 0);
return true;
}
bool EClientSocketBase::DecodeField(int& intValue, const char*& ptr, const char* endPtr)
{
if( !CheckOffset(ptr, endPtr))
return false;
const char* fieldBeg = ptr;
const char* fieldEnd = FindFieldEnd(fieldBeg, endPtr);
if( !fieldEnd)
return false;
intValue = atoi(fieldBeg);
ptr = ++fieldEnd;
return true;
}
bool EClientSocketBase::DecodeField(long& longValue, const char*& ptr, const char* endPtr)
{
int intValue;
if( !DecodeField(intValue, ptr, endPtr))
return false;
longValue = intValue;
return true;
}
bool EClientSocketBase::DecodeField(double& doubleValue, const char*& ptr, const char* endPtr)
{
if( !CheckOffset(ptr, endPtr))
return false;
const char* fieldBeg = ptr;
const char* fieldEnd = FindFieldEnd(fieldBeg, endPtr);
if( !fieldEnd)
return false;
doubleValue = atof(fieldBeg);
ptr = ++fieldEnd;
return true;
}
bool EClientSocketBase::DecodeField(IBString& stringValue,
const char*& ptr, const char* endPtr)
{
if( !CheckOffset(ptr, endPtr))
return false;
const char* fieldBeg = ptr;
const char* fieldEnd = FindFieldEnd(ptr, endPtr);
if( !fieldEnd)
return false;
stringValue = fieldBeg; // better way?
ptr = ++fieldEnd;
return true;
}
bool EClientSocketBase::DecodeFieldMax(int& intValue, const char*& ptr, const char* endPtr)
{
IBString stringValue;
if( !DecodeField(stringValue, ptr, endPtr))
return false;
intValue = (IsEmpty(stringValue) ? UNSET_INTEGER : Atoi(stringValue));
return true;
}
bool EClientSocketBase::DecodeFieldMax(long& longValue, const char*& ptr, const char* endPtr)
{
int intValue;
if( !DecodeFieldMax(intValue, ptr, endPtr))
return false;
longValue = intValue;
return true;
}
bool EClientSocketBase::DecodeFieldMax(double& doubleValue, const char*& ptr, const char* endPtr)
{
IBString stringValue;
if( !DecodeField(stringValue, ptr, endPtr))
return false;
doubleValue = (IsEmpty(stringValue) ? UNSET_DOUBLE : Atof(stringValue));
return true;
}
///////////////////////////////////////////////////////////
// static helpers
static const size_t BufferSizeHighMark = 1 * 1024 * 1024; // 1Mb
void EClientSocketBase::CleanupBuffer(BytesVec& buffer, int processed)
{
assert( buffer.empty() || processed <= (int)buffer.size());
if( buffer.empty())
return;
if( processed <= 0)
return;
if( (size_t)processed == buffer.size()) {
if( buffer.capacity() >= BufferSizeHighMark) {
BytesVec().swap(buffer);
}
else {
buffer.clear();
}
}
else {
buffer.erase( buffer.begin(), buffer.begin() + processed);
}
};
///////////////////////////////////////////////////////////
// utility funcs
static IBString errMsg(std::exception e) {
// return the error associated with this exception
return IBString(e.what());
}
#ifdef _MSC_VER
static IBString errMsg(CException *e) {
// return the error associated with this exception
char buf[1024];
e->GetErrorMessage( buf, sizeof buf);
return IBString( buf);
}
#endif
///////////////////////////////////////////////////////////
// member funcs
EClientSocketBase::EClientSocketBase( EWrapper *ptr)
: m_pEWrapper(ptr)
, m_clientId(-1)
, m_connected(false)
, m_serverVersion(0)
{
}
EClientSocketBase::~EClientSocketBase()
{
}
void EClientSocketBase::eConnectBase()
{
}
void EClientSocketBase::eDisconnectBase()
{
Empty(m_TwsTime);
m_serverVersion = 0;
m_connected = false;
m_clientId = -1;
m_outBuffer.clear();
m_inBuffer.clear();
}
int EClientSocketBase::serverVersion()
{
return m_serverVersion;
}
IBString EClientSocketBase::TwsConnectionTime()
{
return m_TwsTime;
}
void EClientSocketBase::reqMktData(TickerId tickerId, const Contract& contract,
const IBString& genericTicks, bool snapshot)
{
// not connected?
if( !m_connected) {
m_pEWrapper->error( tickerId, NOT_CONNECTED.code(), NOT_CONNECTED.msg());
return;
}
// not needed anymore validation
//if( m_serverVersion < MIN_SERVER_VER_SNAPSHOT_MKT_DATA && snapshot) {
// m_pEWrapper->error( tickerId, UPDATE_TWS.code(), UPDATE_TWS.msg() +
// " It does not support snapshot market data requests.");
// return;
//}
if( m_serverVersion < MIN_SERVER_VER_UNDER_COMP) {
if( contract.underComp) {
m_pEWrapper->error( tickerId, UPDATE_TWS.code(), UPDATE_TWS.msg() +
" It does not support delta-neutral orders.");
return;
}
}
if (m_serverVersion < MIN_SERVER_VER_REQ_MKT_DATA_CONID) {
if( contract.conId > 0) {
m_pEWrapper->error( tickerId, UPDATE_TWS.code(), UPDATE_TWS.msg() +
" It does not support conId parameter.");
return;
}
}
std::ostringstream msg;
const int VERSION = 9;
// send req mkt data msg
ENCODE_FIELD( REQ_MKT_DATA);
ENCODE_FIELD( VERSION);
ENCODE_FIELD( tickerId);
// send contract fields
if( m_serverVersion >= MIN_SERVER_VER_REQ_MKT_DATA_CONID) {
ENCODE_FIELD( contract.conId);
}
ENCODE_FIELD( contract.symbol);
ENCODE_FIELD( contract.secType);
ENCODE_FIELD( contract.expiry);
ENCODE_FIELD( contract.strike);
ENCODE_FIELD( contract.right);
ENCODE_FIELD( contract.multiplier); // srv v15 and above
ENCODE_FIELD( contract.exchange);
ENCODE_FIELD( contract.primaryExchange); // srv v14 and above
ENCODE_FIELD( contract.currency);
ENCODE_FIELD( contract.localSymbol); // srv v2 and above
// Send combo legs for BAG requests (srv v8 and above)
if( Compare(contract.secType, "BAG") == 0)
{
const Contract::ComboLegList* const comboLegs = contract.comboLegs.get();
const int comboLegsCount = comboLegs ? comboLegs->size() : 0;
ENCODE_FIELD( comboLegsCount);
if( comboLegsCount > 0) {
for( int i = 0; i < comboLegsCount; ++i) {
const ComboLeg* comboLeg = ((*comboLegs)[i]).get();
assert( comboLeg);
ENCODE_FIELD( comboLeg->conId);
ENCODE_FIELD( comboLeg->ratio);
ENCODE_FIELD( comboLeg->action);
ENCODE_FIELD( comboLeg->exchange);
}
}
}
if( m_serverVersion >= MIN_SERVER_VER_UNDER_COMP) {
if( contract.underComp) {
const UnderComp& underComp = *contract.underComp;
ENCODE_FIELD( true);
ENCODE_FIELD( underComp.conId);
ENCODE_FIELD( underComp.delta);
ENCODE_FIELD( underComp.price);
}
else {
ENCODE_FIELD( false);
}
}
ENCODE_FIELD( genericTicks); // srv v31 and above
ENCODE_FIELD( snapshot); // srv v35 and above
bufferedSend( msg.str());
}
void EClientSocketBase::cancelMktData(TickerId tickerId)
{
// not connected?
if( !m_connected) {
m_pEWrapper->error( tickerId, NOT_CONNECTED.code(), NOT_CONNECTED.msg());
return;
}
std::ostringstream msg;
const int VERSION = 2;
// send cancel mkt data msg
ENCODE_FIELD( CANCEL_MKT_DATA);
ENCODE_FIELD( VERSION);
ENCODE_FIELD( tickerId);
bufferedSend( msg.str());
}
void EClientSocketBase::reqMktDepth( TickerId tickerId, const Contract &contract, int numRows)
{
// not connected?
if( !m_connected) {
m_pEWrapper->error( tickerId, NOT_CONNECTED.code(), NOT_CONNECTED.msg());
return;
}
// Not needed anymore validation
// This feature is only available for versions of TWS >=6
//if( m_serverVersion < 6) {
// m_pEWrapper->error( NO_VALID_ID, UPDATE_TWS.code(), UPDATE_TWS.msg());
// return;
//}
std::ostringstream msg;
const int VERSION = 3;
// send req mkt data msg
ENCODE_FIELD( REQ_MKT_DEPTH);
ENCODE_FIELD( VERSION);
ENCODE_FIELD( tickerId);
// send contract fields
ENCODE_FIELD( contract.symbol);
ENCODE_FIELD( contract.secType);
ENCODE_FIELD( contract.expiry);
ENCODE_FIELD( contract.strike);
ENCODE_FIELD( contract.right);
ENCODE_FIELD( contract.multiplier); // srv v15 and above
ENCODE_FIELD( contract.exchange);
ENCODE_FIELD( contract.currency);
ENCODE_FIELD( contract.localSymbol);
ENCODE_FIELD( numRows); // srv v19 and above
bufferedSend( msg.str());
}
void EClientSocketBase::cancelMktDepth( TickerId tickerId)
{
// not connected?
if( !m_connected) {
m_pEWrapper->error( tickerId, NOT_CONNECTED.code(), NOT_CONNECTED.msg());
return;
}
// Not needed anymore validation
// This feature is only available for versions of TWS >=6
//if( m_serverVersion < 6) {
// m_pEWrapper->error( NO_VALID_ID, UPDATE_TWS.code(), UPDATE_TWS.msg());
// return;
//}
std::ostringstream msg;
const int VERSION = 1;
// send cancel mkt data msg
ENCODE_FIELD( CANCEL_MKT_DEPTH);
ENCODE_FIELD( VERSION);
ENCODE_FIELD( tickerId);
bufferedSend( msg.str());
}
void EClientSocketBase::reqHistoricalData( TickerId tickerId, const Contract &contract,
const IBString &endDateTime, const IBString &durationStr,
const IBString & barSizeSetting, const IBString &whatToShow,
int useRTH, int formatDate)
{
// not connected?
if( !m_connected) {
m_pEWrapper->error( tickerId, NOT_CONNECTED.code(), NOT_CONNECTED.msg());
return;
}
// Not needed anymore validation
//if( m_serverVersion < 16) {
// m_pEWrapper->error(NO_VALID_ID, UPDATE_TWS.code(), UPDATE_TWS.msg());
// return;
//}
std::ostringstream msg;
const int VERSION = 4;
ENCODE_FIELD( REQ_HISTORICAL_DATA);
ENCODE_FIELD( VERSION);
ENCODE_FIELD( tickerId);
// send contract fields
ENCODE_FIELD( contract.symbol);
ENCODE_FIELD( contract.secType);
ENCODE_FIELD( contract.expiry);
ENCODE_FIELD( contract.strike);
ENCODE_FIELD( contract.right);
ENCODE_FIELD( contract.multiplier);
ENCODE_FIELD( contract.exchange);
ENCODE_FIELD( contract.primaryExchange);
ENCODE_FIELD( contract.currency);
ENCODE_FIELD( contract.localSymbol);
ENCODE_FIELD( contract.includeExpired); // srv v31 and above
ENCODE_FIELD( endDateTime); // srv v20 and above
ENCODE_FIELD( barSizeSetting); // srv v20 and above
ENCODE_FIELD( durationStr);
ENCODE_FIELD( useRTH);
ENCODE_FIELD( whatToShow);
ENCODE_FIELD( formatDate); // srv v16 and above
// Send combo legs for BAG requests
if( Compare(contract.secType, "BAG") == 0)
{
const Contract::ComboLegList* const comboLegs = contract.comboLegs.get();
const int comboLegsCount = comboLegs ? comboLegs->size() : 0;
ENCODE_FIELD( comboLegsCount);
if( comboLegsCount > 0) {
for( int i = 0; i < comboLegsCount; ++i) {
const ComboLeg* comboLeg = ((*comboLegs)[i]).get();
assert( comboLeg);
ENCODE_FIELD( comboLeg->conId);
ENCODE_FIELD( comboLeg->ratio);
ENCODE_FIELD( comboLeg->action);
ENCODE_FIELD( comboLeg->exchange);
}
}
}
bufferedSend( msg.str());
}
void EClientSocketBase::cancelHistoricalData(TickerId tickerId)
{
// not connected?
if( !m_connected) {
m_pEWrapper->error( tickerId, NOT_CONNECTED.code(), NOT_CONNECTED.msg());
return;
}
// Not needed anymore validation
//if( m_serverVersion < 24) {
// m_pEWrapper->error( NO_VALID_ID, UPDATE_TWS.code(), UPDATE_TWS.msg() +
// " It does not support historical data query cancellation.");
// return;
//}
std::ostringstream msg;
const int VERSION = 1;
ENCODE_FIELD( CANCEL_HISTORICAL_DATA);
ENCODE_FIELD( VERSION);
ENCODE_FIELD( tickerId);
bufferedSend( msg.str());
}
void EClientSocketBase::reqRealTimeBars(TickerId tickerId, const Contract &contract,
int barSize, const IBString &whatToShow, bool useRTH)
{
// not connected?
if( !m_connected) {
m_pEWrapper->error( tickerId, NOT_CONNECTED.code(), NOT_CONNECTED.msg());
return;
}
// Not needed anymore validation
//if( m_serverVersion < MIN_SERVER_VER_REAL_TIME_BARS) {
// m_pEWrapper->error( NO_VALID_ID, UPDATE_TWS.code(), UPDATE_TWS.msg() +
// " It does not support real time bars.");
// return;
//}
std::ostringstream msg;
const int VERSION = 1;
ENCODE_FIELD( REQ_REAL_TIME_BARS);
ENCODE_FIELD( VERSION);
ENCODE_FIELD( tickerId);
// send contract fields
ENCODE_FIELD( contract.symbol);
ENCODE_FIELD( contract.secType);
ENCODE_FIELD( contract.expiry);
ENCODE_FIELD( contract.strike);
ENCODE_FIELD( contract.right);
ENCODE_FIELD( contract.multiplier);
ENCODE_FIELD( contract.exchange);
ENCODE_FIELD( contract.primaryExchange);
ENCODE_FIELD( contract.currency);
ENCODE_FIELD( contract.localSymbol);
ENCODE_FIELD( barSize);
ENCODE_FIELD( whatToShow);
ENCODE_FIELD( useRTH);
bufferedSend( msg.str());
}
void EClientSocketBase::cancelRealTimeBars(TickerId tickerId)
{
// not connected?
if( !m_connected) {
m_pEWrapper->error( tickerId, NOT_CONNECTED.code(), NOT_CONNECTED.msg());
return;
}
// Not needed anymore validation
//if( m_serverVersion < MIN_SERVER_VER_REAL_TIME_BARS) {
// m_pEWrapper->error( NO_VALID_ID, UPDATE_TWS.code(), UPDATE_TWS.msg() +
// " It does not support realtime bar data query cancellation.");
// return;
//}
std::ostringstream msg;
const int VERSION = 1;
ENCODE_FIELD( CANCEL_REAL_TIME_BARS);
ENCODE_FIELD( VERSION);
ENCODE_FIELD( tickerId);
bufferedSend( msg.str());
}
void EClientSocketBase::reqScannerParameters()
{
// not connected?
if( !m_connected) {
m_pEWrapper->error( NO_VALID_ID, NOT_CONNECTED.code(), NOT_CONNECTED.msg());
return;
}
// Not needed anymore validation
//if( m_serverVersion < 24) {
// m_pEWrapper->error( NO_VALID_ID, UPDATE_TWS.code(), UPDATE_TWS.msg() +
// " It does not support API scanner subscription.");
// return;
//}
std::ostringstream msg;
const int VERSION = 1;
ENCODE_FIELD( REQ_SCANNER_PARAMETERS);
ENCODE_FIELD( VERSION);
bufferedSend( msg.str());
}
void EClientSocketBase::reqScannerSubscription(int tickerId,
const ScannerSubscription& subscription)
{
// not connected?
if( !m_connected) {
m_pEWrapper->error( tickerId, NOT_CONNECTED.code(), NOT_CONNECTED.msg());
return;
}
// Not needed anymore validation
//if( m_serverVersion < 24) {
// m_pEWrapper->error(NO_VALID_ID, UPDATE_TWS.code(), UPDATE_TWS.msg() +
// " It does not support API scanner subscription.");
// return;
//}
std::ostringstream msg;
const int VERSION = 3;
ENCODE_FIELD( REQ_SCANNER_SUBSCRIPTION);
ENCODE_FIELD( VERSION);
ENCODE_FIELD( tickerId);
ENCODE_FIELD_MAX( subscription.numberOfRows);
ENCODE_FIELD( subscription.instrument);
ENCODE_FIELD( subscription.locationCode);
ENCODE_FIELD( subscription.scanCode);
ENCODE_FIELD_MAX( subscription.abovePrice);
ENCODE_FIELD_MAX( subscription.belowPrice);
ENCODE_FIELD_MAX( subscription.aboveVolume);
ENCODE_FIELD_MAX( subscription.marketCapAbove);
ENCODE_FIELD_MAX( subscription.marketCapBelow);
ENCODE_FIELD( subscription.moodyRatingAbove);
ENCODE_FIELD( subscription.moodyRatingBelow);
ENCODE_FIELD( subscription.spRatingAbove);
ENCODE_FIELD( subscription.spRatingBelow);
ENCODE_FIELD( subscription.maturityDateAbove);
ENCODE_FIELD( subscription.maturityDateBelow);
ENCODE_FIELD_MAX( subscription.couponRateAbove);
ENCODE_FIELD_MAX( subscription.couponRateBelow);
ENCODE_FIELD_MAX( subscription.excludeConvertible);
ENCODE_FIELD_MAX( subscription.averageOptionVolumeAbove); // srv v25 and above
ENCODE_FIELD( subscription.scannerSettingPairs); // srv v25 and above
ENCODE_FIELD( subscription.stockTypeFilter); // srv v27 and above
bufferedSend( msg.str());
}
void EClientSocketBase::cancelScannerSubscription(int tickerId)
{
// not connected?
if( !m_connected) {
m_pEWrapper->error( tickerId, NOT_CONNECTED.code(), NOT_CONNECTED.msg());
return;
}
// Not needed anymore validation
//if( m_serverVersion < 24) {
// m_pEWrapper->error(NO_VALID_ID, UPDATE_TWS.code(), UPDATE_TWS.msg() +
// " It does not support API scanner subscription.");
// return;
//}
std::ostringstream msg;
const int VERSION = 1;
ENCODE_FIELD( CANCEL_SCANNER_SUBSCRIPTION);
ENCODE_FIELD( VERSION);
ENCODE_FIELD( tickerId);
bufferedSend( msg.str());
}
void EClientSocketBase::reqFundamentalData(TickerId reqId, const Contract& contract,
const IBString& reportType)
{
// not connected?
if( !m_connected) {
m_pEWrapper->error( reqId, NOT_CONNECTED.code(), NOT_CONNECTED.msg());
return;
}
if( m_serverVersion < MIN_SERVER_VER_FUNDAMENTAL_DATA) {
m_pEWrapper->error(NO_VALID_ID, UPDATE_TWS.code(), UPDATE_TWS.msg() +
" It does not support fundamental data requests.");
return;
}
std::ostringstream msg;
const int VERSION = 1;
ENCODE_FIELD( REQ_FUNDAMENTAL_DATA);
ENCODE_FIELD( VERSION);
ENCODE_FIELD( reqId);
// send contract fields
ENCODE_FIELD( contract.symbol);
ENCODE_FIELD( contract.secType);
ENCODE_FIELD( contract.exchange);
ENCODE_FIELD( contract.primaryExchange);
ENCODE_FIELD( contract.currency);
ENCODE_FIELD( contract.localSymbol);
ENCODE_FIELD( reportType);
bufferedSend( msg.str());
}
void EClientSocketBase::cancelFundamentalData( TickerId reqId)
{
// not connected?
if( !m_connected) {
m_pEWrapper->error( reqId, NOT_CONNECTED.code(), NOT_CONNECTED.msg());
return;
}
if( m_serverVersion < MIN_SERVER_VER_FUNDAMENTAL_DATA) {
m_pEWrapper->error(NO_VALID_ID, UPDATE_TWS.code(), UPDATE_TWS.msg() +
" It does not support fundamental data requests.");
return;
}
std::ostringstream msg;
const int VERSION = 1;
ENCODE_FIELD( CANCEL_FUNDAMENTAL_DATA);
ENCODE_FIELD( VERSION);
ENCODE_FIELD( reqId);
bufferedSend( msg.str());
}
void EClientSocketBase::calculateImpliedVolatility(TickerId reqId, const Contract &contract, double optionPrice, double underPrice) {
// not connected?
if( !m_connected) {
m_pEWrapper->error( NO_VALID_ID, NOT_CONNECTED.code(), NOT_CONNECTED.msg());
return;
}
if (m_serverVersion < MIN_SERVER_VER_REQ_CALC_IMPLIED_VOLAT) {
m_pEWrapper->error( reqId, UPDATE_TWS.code(), UPDATE_TWS.msg() +
" It does not support calculate implied volatility requests.");
return;
}
std::ostringstream msg;
const int VERSION = 1;