-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSampleDialogSettingOrder.cpp
More file actions
4707 lines (4210 loc) · 137 KB
/
SampleDialogSettingOrder.cpp
File metadata and controls
4707 lines (4210 loc) · 137 KB
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
#include "stdafx.h"
#include "SampleDialogSettingOrder.h"
//#include "StockMarketSorter.h"
#include "ExtensionSample.h"
#include "ExtensionDerivedConfig.h"
extern ExtensionSample theApp;
extern ExtensionDerivedConfig* extensionConfig;
enum OrderStatusValues : unsigned char
{
OSE_UNACKNOWLEDGED,
OSE_PENDING,
OSE_PARTIALLY_FILLED,
OSE_FILLED,//Order Dead
OSE_UNFILLED,//Order Dead
OSE_REJECTED,
OSE_Count
};
class OrderObserver : public Observer
{
public:
OrderObserver(Order* const& order, ListBoxOrder* const& listBoxOrder, const bool& inThread):
m_order(order),
m_listBoxOrder(listBoxOrder),
m_sizePending(0),
m_sizeExecuted(0),
m_serverId(0),
m_replacedByClientId(0),
m_replacedBySendId(0),
m_timeCancelSent(0),
m_timeDead(0),
m_status(OSE_Count),
m_rejectCode(0),
m_rejectText(nullptr),
m_inThread(inThread)
{
AddObserver();
m_listBoxOrder->AddItem(this);
// m_listBoxOrder->AddString((const char*)this);
// m_listBoxOrder->RepaintCount();
Update();
if(m_status == OSE_Count)m_status = CalculateOrderStatus();
}
virtual ~OrderObserver();
void SetInThread(const bool& inThread)
{
if(inThread != m_inThread)
{
if(inThread)
{
if(!m_order->isDead())
{
m_order->AddInThreadObserver(this);
}
m_order->RemoveObserver(this);
}
else
{
if(!m_order->isDead())
{
m_order->AddObserver(this);
}
m_order->RemoveInThreadObserver(this);
}
m_inThread = inThread;
}
}
void RemoveObserver()
{
if(m_inThread)
{
m_order->RemoveInThreadObserver(this);
}
else
{
m_order->RemoveObserver(this);
}
}
void AddObserver()
{
if(!m_order->isDead())
{
if(m_inThread)
{
m_order->AddInThreadObserver(this);
}
else
{
m_order->AddObserver(this);
}
}
}
Order* GetOrder() const{return m_order;}
bool isDead() const{return m_status > OSE_PARTIALLY_FILLED;}
const bool& isIso() const{return m_order->isIso();}
const bool& isReplacingNatively() const{return m_order->isReplacingNatively();}
#ifndef TAKION_NO_OPTIONS
char isCallPut() const{return m_order->GetOptionBlock() ? m_order->isCall() ? 'C' : 'P' : '\0';}
const unsigned __int64& GetOptionBlock() const{return m_order->GetOptionBlock();}
unsigned char GetSecurityType() const{return m_order->GetSecurityType();}
#endif
const unsigned int& GetClientId() const{return m_order->GetClientId();}
const Money& GetClosedPnl() const{return m_closedPnl;}
const Money& GetBpUsed() const{return m_bpUsed;}
const Price& GetExecutedPrice() const{return m_executedPrice;}
const unsigned int& GetSizePending() const{return m_sizePending;}
const unsigned int& GetSizeExecuted() const{return m_sizeExecuted;}
const unsigned int& GetServerId() const{return m_serverId;}
const unsigned int& GetReplacedByClientId() const{return m_replacedByClientId;}
const unsigned int& GetReplacedBySendId() const{return m_replacedBySendId;}
const unsigned int& GetTimeCancelSent() const{return m_timeCancelSent;}
const unsigned int& GetTimeDead() const{return m_timeDead;}
const unsigned char& GetStatus() const{return m_status;}//OrderStatusEnum
const unsigned short& GetRejectCode() const{return m_rejectCode;}
const char* GetRejectText() const{return m_rejectText;}
unsigned char CalculateOrderStatus() const;
//When the order is updated in the worker thread, we need to LockInquiry when we paint in the main thread, and LockModification when we change the values in the worker thread.
bool LockInquiry() const{return m_paintLock.LockInquiry();}
void LockInquiryWait() const{m_paintLock.LockInquiryWait();}
void UnlockInquiry() const{m_paintLock.UnlockInquiry();}
bool LockModification() const{return m_paintLock.LockModification();}
void UnlockModification() const{m_paintLock.UnlockModification();}
protected:
virtual void Notify(const Message* message, const Observable* from, const Message* info) override;
//InThread
void UpdateInThread();
void PostRepaintMessageFromWorkerThread(unsigned short startColumn, unsigned short endColumn) const;
TakionLockMultithread m_paintLock;//When the order is updated in the worker thread, we need to LockInquiry when we paint in the main thread, and LockModification when we change the values in the worker thread.
//
bool Update();
Order* m_order;
ListBoxOrder* m_listBoxOrder;
Money m_closedPnl;
Money m_bpUsed;
Price m_executedPrice;
unsigned int m_sizePending;
unsigned int m_sizeExecuted;
unsigned int m_serverId;
unsigned int m_replacedByClientId;
unsigned int m_replacedBySendId;
unsigned int m_timeCancelSent;
unsigned int m_timeDead;
unsigned char m_status;//OrderStatusEnum
unsigned short m_rejectCode;
bool isRejectTextChanged(const char* rejectText) const;
bool UpdateRejectText(const char* rejectText);
char* m_rejectText;
bool m_inThread;
private:
OrderObserver(const OrderObserver& other){}
OrderObserver& operator=(const OrderObserver& other){return *this;}
};
class LbOrderHorizontalScroller : public LbHorizontalScroller
{
public:
LbOrderHorizontalScroller(ListBoxOrder* listBox);
virtual void AddColumns() override;
protected:
ListBoxOrder* m_listBoxOrder;
};
class TMsgSampleOrderClientId : public TMsgCustomExternal
{
public:
unsigned int m_clientId;
protected:
TMsgSampleOrderClientId(const unsigned int& clientId, const unsigned short& tipe, const unsigned short& length):
TMsgCustomExternal(tipe, length),
m_clientId(clientId)
{}
};
class TMsgSampleNewOrderCreated : public TMsgSampleOrderClientId
{
public:
TMsgSampleNewOrderCreated(const unsigned int& clientId):
TMsgSampleOrderClientId(clientId, NEW_ORDER_CREATED, sizeof(TMsgSampleNewOrderCreated))
{}
};
class TMsgSampleNewOrderSendOrderDone : public TMsgSampleOrderClientId
{
public:
TMsgSampleNewOrderSendOrderDone(const unsigned int& sendId):
TMsgSampleOrderClientId(sendId, NEW_ORDER_SEND_ORDER_DONE, sizeof(TMsgSampleNewOrderSendOrderDone))
{}
};
class TMsgSampleOrderDeleted : public TMsgSampleOrderClientId
{
public:
TMsgSampleOrderDeleted(const unsigned int& clientId) :
TMsgSampleOrderClientId(clientId, ORDER_DELETED, sizeof(TMsgSampleOrderDeleted))
{}
};
class TMsgSampleOrderTotalsChanged : public TMsgCustomExternal
{
public:
TMsgSampleOrderTotalsChanged(const Money& closedPnlDiff, const Money& bpUsedDiff, const unsigned int& sizePendingDiff, const unsigned int& sizeExecutedDiff):
TMsgCustomExternal(ORDER_TOTALS_CHANGED, sizeof(TMsgSampleOrderTotalsChanged)),
m_closedPnlDiff(closedPnlDiff),
m_bpUsedDiff(bpUsedDiff),
m_sizePendingDiff(sizePendingDiff),
m_sizeExecutedDiff(sizeExecutedDiff)
{}
Money m_closedPnlDiff;
Money m_bpUsedDiff;
unsigned int m_sizePendingDiff;
unsigned int m_sizeExecutedDiff;
};
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
OrderObserver::~OrderObserver()
{
if(m_rejectText)delete[] m_rejectText;
}
bool OrderObserver::isRejectTextChanged(const char* rejectText) const
{
return rejectText != m_rejectText && (!rejectText || !m_rejectText || strcmp(m_rejectText, rejectText) != 0);
/*
if(rejectText == m_rejectText)
{
return false;
}
if(rejectText && m_rejectText)
{
return strcmp(m_rejectText, rejectText) != 0;
}
return true;
*/
}
bool OrderObserver::UpdateRejectText(const char* rejectText)
{
bool rejectTextChanged = false;
if(rejectText)
{
if(m_rejectText)
{
if(strcmp(m_rejectText, rejectText))
{
unsigned int len = (unsigned int)strlen(m_rejectText);
unsigned int lenOther = (unsigned int)strlen(rejectText);
if(len != lenOther)
{
delete[] m_rejectText;
m_rejectText = new char[lenOther + 1];
}
strcpy_s(m_rejectText, lenOther + 1, rejectText);
rejectTextChanged = true;
}
}
else
{
unsigned int lenOther = (unsigned int)strlen(rejectText);
m_rejectText = new char[lenOther + 1];
strcpy_s(m_rejectText, lenOther + 1, rejectText);
rejectTextChanged = true;
}
}
else if(m_rejectText)
{
delete[] m_rejectText;
m_rejectText = nullptr;
rejectTextChanged = true;
}
return rejectTextChanged;
}
unsigned char OrderObserver::CalculateOrderStatus() const
{
return !m_order->GetServerId() ? OSE_UNACKNOWLEDGED:
m_order->GetPendingQuantity() ? m_order->GetFilledQuantity() ? OSE_PARTIALLY_FILLED : OSE_PENDING:
m_order->GetFilledQuantity() ? OSE_FILLED : m_order->GetRejectedQuantity() ? OSE_REJECTED : OSE_UNFILLED;
}
void OrderObserver::PostRepaintMessageFromWorkerThread(unsigned short startColumn, unsigned short endColumn) const
{
/*
if(m_ordinal != 0xFFFFFFFF)
{
m_owner->PostMessage(WM_USER + 444, m_ordinal, ((unsigned int)startColumn << 16) | endColumn);
}
*/
if(startColumn <= endColumn)
{
m_listBoxOrder->PostMessage(WM_USER + 444, m_order->GetClientId(), ((unsigned int)startColumn << 16) | endColumn);
}
}
void OrderObserver::UpdateInThread()
{
if(m_inThread)
{
const Money closedPnl = m_order->GetClosedPnl();
const Money bpUsed = m_order->GetBpUsed();
const Price executedPrice = m_order->GetAveragePrice();
const unsigned int sizePending = m_order->GetPendingQuantity();
const unsigned int sizeExecuted = m_order->GetFilledQuantity();
const unsigned int serverId = m_order->GetServerId();
const unsigned int timeCancelSent = m_order->GetTimeCancelSent();
const unsigned int timeDead = m_order->GetTimeDead();
const unsigned int rejectCode = m_order->GetRejectCode();
const unsigned int replacedByClientId = m_order->GetReplacedByClientId();
const unsigned int replacedBySendId = m_order->GetReplacedBySendId();
const bool rejected = m_order->GetRejectedQuantity() != 0;
const char* rejectText = m_order->GetRejectText();
// const bool rejectTextChanged = UpdateRejectText(m_order->GetRejectText());
const unsigned char status = rejected ? OSE_REJECTED:
!serverId ? OSE_UNACKNOWLEDGED:
sizePending ? sizeExecuted ? OSE_PARTIALLY_FILLED : OSE_PENDING:
sizeExecuted ? OSE_FILLED : OSE_UNFILLED;
const bool statusChanged = status != m_status;
const bool serverIdChanged = serverId != m_serverId;
const bool replacedByClientIdChanged = replacedByClientId != m_replacedByClientId;
const bool replacedBySendIdChanged = replacedBySendId != m_replacedBySendId;
const bool executedPriceChanged = executedPrice != m_executedPrice;
const bool sizeExecutedChanged = sizeExecuted != m_sizeExecuted;
const bool timeCancelSentChanged = timeCancelSent != m_timeCancelSent;
const bool timeDeadChanged = timeDead != m_timeDead;
const bool rejectCodeChanged = rejectCode != m_rejectCode;
const bool rejectTextChanged = isRejectTextChanged(rejectText);
// const bool closedPnlChanged = closedPnl != m_closedPnl;
const Money closedPnlDiff = closedPnl - m_closedPnl;
// const bool bpUsedChanged = bpUsed != m_bpUsed;
const Money bpUsedDiff = bpUsed - m_bpUsed;
// const bool sizePendingChanged = sizePending != m_sizePending;
const unsigned int sizePendingDiff = m_sizePending - sizePending;
// const bool sizeExecutedChanged = sizeExecuted != m_sizeExecuted;
const unsigned int sizeExecutedDiff = sizeExecuted - m_sizeExecuted;
bool totalsChanged = false;
unsigned short firstColumnUpdated = 0xFFFF;
unsigned short lastColumnUpdated = 0;
const bool unlock = LockModification();
if(statusChanged)
{
m_status = status;
firstColumnUpdated = 0;
lastColumnUpdated = 0xFFFF;
}
if(serverIdChanged)
{
m_serverId = serverId;
if(OCI_SERVER_ID < firstColumnUpdated)firstColumnUpdated = OCI_SERVER_ID;
if(OCI_SERVER_ID > lastColumnUpdated)lastColumnUpdated = OCI_SERVER_ID;
}
if(replacedByClientIdChanged)
{
m_replacedByClientId = replacedByClientId;
if(OCI_REPLACED_BY_CLIENT_ID < firstColumnUpdated)firstColumnUpdated = OCI_REPLACED_BY_CLIENT_ID;
if(OCI_REPLACED_BY_CLIENT_ID > lastColumnUpdated)lastColumnUpdated = OCI_REPLACED_BY_CLIENT_ID;
}
if(replacedBySendIdChanged)
{
m_replacedBySendId = replacedBySendId;
if(OCI_REPLACED_BY_SEND_ID < firstColumnUpdated)firstColumnUpdated = OCI_REPLACED_BY_SEND_ID;
if(OCI_REPLACED_BY_SEND_ID > lastColumnUpdated)lastColumnUpdated = OCI_REPLACED_BY_SEND_ID;
}
if(!closedPnlDiff.isZero())
{
totalsChanged = true;
// m_listBoxOrder->UpdateClosedPnl(m_closedPnl, closedPnl);
m_closedPnl = closedPnl;
if(OCI_CLOSED_PNL < firstColumnUpdated)firstColumnUpdated = OCI_CLOSED_PNL;
if(OCI_CLOSED_PNL > lastColumnUpdated)lastColumnUpdated = OCI_CLOSED_PNL;
}
if(!bpUsedDiff.isZero())
{
totalsChanged = true;
// m_listBoxOrder->UpdateBpUsed(m_bpUsed, bpUsed);
m_bpUsed = bpUsed;
if(OCI_BP_USED < firstColumnUpdated)firstColumnUpdated = OCI_BP_USED;
if(OCI_BP_USED > lastColumnUpdated)lastColumnUpdated = OCI_BP_USED;
}
if(executedPriceChanged)
{
totalsChanged = true;
m_executedPrice = executedPrice;
if(OCI_EXECUTED_PRICE < firstColumnUpdated)firstColumnUpdated = OCI_EXECUTED_PRICE;
if(OCI_EXECUTED_PRICE > lastColumnUpdated)lastColumnUpdated = OCI_EXECUTED_PRICE;
}
if(sizePendingDiff)
{
totalsChanged = true;
// m_listBoxOrder->UpdateSizePending(m_sizePending, sizePending);
m_sizePending = sizePending;
if(OCI_PENDING_SIZE < firstColumnUpdated)firstColumnUpdated = OCI_PENDING_SIZE;
if(OCI_PENDING_SIZE > lastColumnUpdated)lastColumnUpdated = OCI_PENDING_SIZE;
}
if(sizeExecutedDiff)
{
totalsChanged = true;
// m_listBoxOrder->UpdateSizeExecuted(m_sizeExecuted, sizeExecuted);
m_sizeExecuted = sizeExecuted;
if(OCI_EXECUTED_SIZE < firstColumnUpdated)firstColumnUpdated = OCI_EXECUTED_SIZE;
if(OCI_EXECUTED_SIZE > lastColumnUpdated)lastColumnUpdated = OCI_EXECUTED_SIZE;
}
if(timeCancelSentChanged)
{
m_timeCancelSent = timeCancelSent;
if(OCI_TIME_CANCEL_SENT < firstColumnUpdated)firstColumnUpdated = OCI_TIME_CANCEL_SENT;
if(OCI_TIME_CANCEL_SENT > lastColumnUpdated)lastColumnUpdated = OCI_TIME_CANCEL_SENT;
}
if(timeDeadChanged)
{
m_timeDead = timeDead;
if(OCI_TIME_DEAD < firstColumnUpdated)firstColumnUpdated = OCI_TIME_DEAD;
if(OCI_TIME_DEAD > lastColumnUpdated)lastColumnUpdated = OCI_TIME_DEAD;
}
if(rejectCodeChanged)
{
m_rejectCode = rejectCode;
if(OCI_REJECT_CODE < firstColumnUpdated)firstColumnUpdated = OCI_REJECT_CODE;
if(OCI_REJECT_CODE > lastColumnUpdated)lastColumnUpdated = OCI_REJECT_CODE;
}
if(rejectTextChanged)
{
UpdateRejectText(rejectText);
if(OCI_REJECT_TEXT < firstColumnUpdated)firstColumnUpdated = OCI_REJECT_TEXT;
if(OCI_REJECT_TEXT > lastColumnUpdated)lastColumnUpdated = OCI_REJECT_TEXT;
}
if(unlock)UnlockModification();
//Can't repaint from a thread. Post Windows Message
PostRepaintMessageFromWorkerThread(firstColumnUpdated, lastColumnUpdated);
//Write custom message to main thread to change the totals
if(totalsChanged)
{
TD_WriteCustomExternalMessageToMainThread(&TMsgSampleOrderTotalsChanged(closedPnlDiff, bpUsedDiff, sizePendingDiff, sizeExecutedDiff));
}
}
}
bool OrderObserver::Update()//returns true if order status changed. If returns true, then we need to check if the order needs to be removed from the list box because it no longer belongs there.
{
if(m_order->LockInquiry())//have to lock because the order may be being modified in a worker thread
{
//Remember the values that can change. Other values don't change and can be safely taken from the order object itself at any time even if the order is being modified in a different thread.
const Money closedPnl = m_order->GetClosedPnl();
const Money bpUsed = m_order->GetBpUsed();
const Price executedPrice = m_order->GetAveragePrice();
const unsigned int sizePending = m_order->GetPendingQuantity();
const unsigned int sizeExecuted = m_order->GetFilledQuantity();
const unsigned int serverId = m_order->GetServerId();
const unsigned int timeCancelSent = m_order->GetTimeCancelSent();
const unsigned int timeDead = m_order->GetTimeDead();
const unsigned int rejectCode = m_order->GetRejectCode();
const unsigned int replacedByClientId = m_order->GetReplacedByClientId();
const unsigned int replacedBySendId = m_order->GetReplacedBySendId();
bool rejected = m_order->GetRejectedQuantity() != 0;
const bool rejectTextChanged = UpdateRejectText(m_order->GetRejectText());
//Unlock as soon as you don't need to access the order data.
m_order->UnlockInquiry();
int found = m_listBoxOrder->FindOrderByClientId(m_order->GetClientId());
unsigned char status = rejected ? OSE_REJECTED:
!serverId ? OSE_UNACKNOWLEDGED:
sizePending ? sizeExecuted ? OSE_PARTIALLY_FILLED : OSE_PENDING:
sizeExecuted ? OSE_FILLED : OSE_UNFILLED;
bool statusChanged = m_status != status;
if(statusChanged)
{
m_status = status;
m_listBoxOrder->InvalidateItem(found);//the whole row needs to be repainted because it's color changed
}
LbHorizontalScroller* scroller = m_listBoxOrder->GetHorizontalScroller();
if(serverId != m_serverId)
{
m_serverId = serverId;
if(!statusChanged && scroller)scroller->InvalidateItemColumn(found, OCI_SERVER_ID);
}
if(replacedByClientId != m_replacedByClientId)
{
m_replacedByClientId = replacedByClientId;
if(scroller)scroller->InvalidateItemColumn(found, OCI_REPLACED_BY_CLIENT_ID);
}
if(replacedBySendId != m_replacedBySendId)
{
m_replacedBySendId = replacedBySendId;
if(scroller)scroller->InvalidateItemColumn(found, OCI_REPLACED_BY_SEND_ID);
}
if(closedPnl != m_closedPnl)
{
m_listBoxOrder->UpdateClosedPnl(m_closedPnl, closedPnl);
m_closedPnl = closedPnl;
if(!statusChanged && scroller)scroller->InvalidateItemColumn(found, OCI_CLOSED_PNL);
}
if(bpUsed != m_bpUsed)
{
m_listBoxOrder->UpdateBpUsed(m_bpUsed, bpUsed);
m_bpUsed = bpUsed;
if(!statusChanged && scroller)scroller->InvalidateItemColumn(found, OCI_BP_USED);
}
if(executedPrice != m_executedPrice)
{
m_executedPrice = executedPrice;
if(!statusChanged && scroller)scroller->InvalidateItemColumn(found, OCI_EXECUTED_PRICE);
}
if(sizePending != m_sizePending)
{
m_listBoxOrder->UpdateSizePending(m_sizePending, sizePending);
m_sizePending = sizePending;
if(!statusChanged && scroller)scroller->InvalidateItemColumn(found, OCI_PENDING_SIZE);
}
if(sizeExecuted != m_sizeExecuted)
{
m_listBoxOrder->UpdateSizeExecuted(m_sizeExecuted, sizeExecuted);
m_sizeExecuted = sizeExecuted;
if(!statusChanged && scroller)scroller->InvalidateItemColumn(found, OCI_EXECUTED_SIZE);
}
if(timeCancelSent != m_timeCancelSent)
{
m_timeCancelSent = timeCancelSent;
if(!statusChanged && scroller)scroller->InvalidateItemColumn(found, OCI_TIME_CANCEL_SENT);
}
if(timeDead != m_timeDead)
{
m_timeDead = timeDead;
if(!statusChanged && scroller)scroller->InvalidateItemColumn(found, OCI_TIME_DEAD);
}
if(rejectCode != m_rejectCode)
{
m_rejectCode = rejectCode;
if(!statusChanged && scroller)scroller->InvalidateItemColumn(found, OCI_REJECT_CODE);
}
if(rejectTextChanged)
{
if(!statusChanged && scroller)scroller->InvalidateItemColumn(found, OCI_REJECT_TEXT);
}
return statusChanged;
}
//else - LockInquiry failed which means that the order is being updated and a new notification will come soon and the function will be called again.
return false;
}
void OrderObserver::Notify(const Message* message, const Observable* from, const Message* info)
{
if(m_inThread)
{
switch(message->m_type)
{
/*
case TS_ORDER://New Order in thread. You get this message from Account
{
const TMsgReqOrder* msg = (const TMsgReqOrder*)message;
// UpdateInThread();
}
break;
*/
case TS_ORDER_CANCEL_REQ://Order is requested to cancel
{
const TMsgReqOrderCancel* msg = (const TMsgReqOrderCancel*)message;
const Order* orderBeingCanceled = this->m_order;
UpdateInThread();
}
break;
case TS_ORDER_REPLACE_REQ://Order is requested to replace
{
const TMsgReqOrderReplace* msg = (const TMsgReqOrderReplace*)message;
if(info && info->m_type == TM_UPDATED_ORDER)//sanity check
{
const TMsgUpdatedOrder* msgReplacingOrder = (const TMsgUpdatedOrder*)info;
const Order* replacingOrder = msgReplacingOrder->m_order;
const Order* orderBeingReplaced = this->m_order;
UpdateInThread();
}
}
break;
case TS_ORDER_ACK:
{
const TMsgOrderAck* msg = (const TMsgOrderAck*)message;
UpdateInThread();
}
break;
case TS_ORDER_UPDATE:
{
const TMsgOrderUpdate* msg = (const TMsgOrderUpdate*)message;
UpdateInThread();
}
break;
case TS_ORDER_REJECT:
{
const TMsgOrderReject* msg = (const TMsgOrderReject*)message;
UpdateInThread();
}
break;
case TS_ORDER_REPORT:
{
const TMsgOrderReport* msg = (const TMsgOrderReport*)message;
if(info && info->m_type == TM_NEW_EXECUTION)//make sure the info is of the type we expect it to be
{
const Execution* execution = ((TMsgNewExecution*)info)->m_execution;
const Order* order = execution->GetOrder();
const unsigned __int64 userId = order->GetUserId();
const Position* position = order->GetPosition();
const Account* account = position->GetAccount();
}
UpdateInThread();
}
break;
// case TS_ORDER_CANCEL_REQ:
// case TS_ORDER_REPLACE_REQ:
case TS_ORDER_CANCEL_ACK:
{
const TMsgOrderCancelAck* msg = (const TMsgOrderCancelAck*)message;
UpdateInThread();
}
break;
case TS_ORDER_KILL:
{
const TMsgOrderKill* msg = (const TMsgOrderKill*)message;
UpdateInThread();
}
break;
case TS_ORDER_REPLACE_ACK:
{
const TMsgOrderReplaceAck* msg = (const TMsgOrderReplaceAck*)message;
UpdateInThread();
}
break;
case TS_ORDER_REPLACED://You will get 2 of this - one fro replaced order and one for replacing order
{
const TMsgOrderReplaceDone* msg = (const TMsgOrderReplaceDone*)message;
UpdateInThread();
}
break;
case TS_CANCEL_REJECT:
{
const TMsgCancelReject* msg = (const TMsgCancelReject*)message;
UpdateInThread();
}
break;
}
}
else
{
if(m_order->isDeleted())
{
m_listBoxOrder->RemoveRow(this);
}
else
{
//'this' object can get destroyed before we exit the function
if(m_order->GetOrderState() == TU_OS_DEAD)
{
m_order->RemoveObserver(this);
}
if(Update())
{
if(!m_listBoxOrder->GetOwnerDialog()->OrderObserverStatusBelongs(this))
{
m_listBoxOrder->RemoveRow(this);
}
}
}
}
}
LbOrderHorizontalScroller::LbOrderHorizontalScroller(ListBoxOrder* listBox):
LbHorizontalScroller(listBox),
m_listBoxOrder(listBox)
{
}
void LbOrderHorizontalScroller::AddColumns()
{
const int itemHeight = m_listBox->GetMyItemHeight();
const int doubleHeight = itemHeight + itemHeight;
const int tripleHeight = doubleHeight + itemHeight;
const int fourHeight = tripleHeight + itemHeight;
const int fiveHeight = fourHeight + itemHeight;
const int sixHeight = fiveHeight + itemHeight;
AddColumn("Symbol", OCI_SYMBOL, fiveHeight, DT_LEFT, DT_LEFT, DT_RIGHT, "Symbol");//, 0xFFFFFFFF, itemHeight, itemHeight);
#ifndef TAKION_NO_OPTIONS
AddColumn("C/P", OCI_OPTION_CALLPUT, doubleHeight, DT_CENTER, DT_CENTER, DT_RIGHT, "Option Call (C) or Put (P)");
AddColumn("Expiration", OCI_OPTION_EXPIRATION, fourHeight, DT_RIGHT, DT_CENTER, 0xFFFFFFFF, "Option Expiration Date");
AddColumn("Strike", OCI_OPTION_STRIKE_PRICE, fourHeight, DT_RIGHT, DT_RIGHT, 0xFFFFFFFF, "Option Strike Price");
#endif
AddColumn("ClientId", OCI_CLIENT_ID, fourHeight, DT_RIGHT, DT_RIGHT, 0xFFFFFFFF, "Client Id");
AddColumn("Side", OCI_SIDE, doubleHeight, DT_CENTER, DT_CENTER, 0xFFFFFFFF, "Side");
AddColumn("$Limit", OCI_LIMIT_PRICE, fourHeight, DT_RIGHT, DT_RIGHT, 0xFFFFFFFF, "Limit Price");
AddColumn("$Stop", OCI_STOP_PRICE, fourHeight, DT_RIGHT, DT_RIGHT, 0xFFFFFFFF, "Stop Price");
AddColumn("Size", OCI_SIZE, fourHeight, DT_RIGHT, DT_RIGHT, 0xFFFFFFFF, "Order Size");
AddColumn("Venue", OCI_VENUE, tripleHeight, DT_RIGHT, DT_RIGHT, 0xFFFFFFFF, "Venue");
AddColumn("Route", OCI_ROUTING, tripleHeight, DT_CENTER, DT_CENTER, 0xFFFFFFFF, "Routing");
AddColumn("TIF", OCI_TIF, sixHeight, DT_CENTER, DT_CENTER, 0xFFFFFFFF, "Time in Force");
AddColumn("Created", OCI_TIME_CREATED, fiveHeight, DT_RIGHT, DT_CENTER, 0xFFFFFFFF, "Time Created");
AddColumn("ServerId", OCI_SERVER_ID, fourHeight, DT_RIGHT, DT_RIGHT, 0xFFFFFFFF, "Server Id");
AddColumn("$Exec", OCI_EXECUTED_PRICE, fourHeight, DT_RIGHT, DT_RIGHT, 0xFFFFFFFF, "Executed Price");
AddColumn("Pending", OCI_PENDING_SIZE, fourHeight, DT_RIGHT, DT_RIGHT, DT_RIGHT, "Pending Size");
AddColumn("Executed", OCI_EXECUTED_SIZE, fourHeight, DT_RIGHT, DT_RIGHT, DT_RIGHT, "Executed Size");
AddColumn("Visible", OCI_VISIBLE_SIZE, fourHeight, DT_RIGHT, DT_RIGHT, 0xFFFFFFFF, "Visible Size");
AddColumn("Status", OCI_STATUS, fourHeight, DT_LEFT, DT_LEFT, 0xFFFFFFFF, "Status", 0xFFFFFFFF, 0xFFFFFFFF, 5, 5, 5);
AddColumn("CancelSent", OCI_TIME_CANCEL_SENT, fiveHeight, DT_RIGHT, DT_CENTER, 0xFFFFFFFF, "Cancel Sent");
AddColumn("Dead", OCI_TIME_DEAD, fiveHeight, DT_RIGHT, DT_CENTER, 0xFFFFFFFF, "Time Dead");
AddColumn("Replaced", OCI_REPLACED_CLIENT_ID, fourHeight, DT_RIGHT, DT_RIGHT, 0xFFFFFFFF, "Client Id of the Order that was replaced by this Order");
AddColumn("ByCl", OCI_REPLACED_BY_CLIENT_ID, fourHeight, DT_RIGHT, DT_RIGHT, 0xFFFFFFFF, "Client Id of the Order that replaced this Order");
AddColumn("BySend", OCI_REPLACED_BY_SEND_ID, fourHeight, DT_RIGHT, DT_RIGHT, 0xFFFFFFFF, "Send Id of the Order(s) that replaced this Order");
AddColumn("ClosedP&L", OCI_CLOSED_PNL, fiveHeight, DT_RIGHT, DT_RIGHT, DT_RIGHT, "Closed P&&L");
AddColumn("BP Used", OCI_BP_USED, fiveHeight, DT_RIGHT, DT_RIGHT, DT_RIGHT, "Buying Power used by the Order");
AddColumn("Rej", OCI_REJECT_CODE, doubleHeight, DT_CENTER, DT_CENTER, 0xFFFFFFFF, "Reject Code");
AddColumn("Reject Text", OCI_REJECT_TEXT, sixHeight, DT_LEFT, DT_LEFT, 0xFFFFFFFF, "Reject Text");
AddColumn("SendId", OCI_SEND_ID, fourHeight, DT_RIGHT, DT_RIGHT, 0xFFFFFFFF, "Client Id returned by the SendOrder function");
AddColumn("ParentId", OCI_PARENT_ID, fourHeight, DT_RIGHT, DT_RIGHT, 0xFFFFFFFF, "Parent Id");
AddColumn("iso", OCI_ISO, doubleHeight, DT_CENTER, DT_RIGHT, DT_RIGHT, "Inter Market Sweep Order");
AddColumn("RN", OCI_REPLACING_NATIVELY, doubleHeight, DT_CENTER, DT_RIGHT, DT_RIGHT, "Replacing Natively");
}
int ListBoxOrder::Compare(const void* item1, const void* item2) const
{
const unsigned int& clientId1 = ((const OrderObserver*)item1)->GetClientId();
const unsigned int& clientId2 = ((const OrderObserver*)item2)->GetClientId();
return clientId1 > clientId2 ? -1 : clientId1 < clientId2 ? 1 : 0;
}
void ListBoxOrder::SetInThread(const bool inThread)
{
if(inThread != m_inThread)
{
m_inThread = inThread;
OrderObserver* stock;
for(unsigned int i = 0, end = m_itemCount; i < end; ++i)
{
stock = (OrderObserver*)GetItemDataPtr(i);
stock->SetInThread(m_inThread);
}
}
}
int ListBoxOrder::FindOrderByClientId(const unsigned int clientIdToFind) const//search in the sorted collection using break in half
{
const unsigned int count = GetItemCount();
if(!count)
{
return -1;
}
/*if unsorted list box - linear search
if((GetStyle() & LBS_SORT) == 0 || m_doNotSort)
{
for(int i = 0; i < count; ++i)
{
if(((const OrderObserver*)GetItemDataPtr(i))->GetClientId() == clientIdToFind)
{
return i;
}
}
return -1;
}
*/
int low = 0;
int high = count - 1;
int middle;
unsigned int itemClientId;
while(high >= low)
{
middle = (low + high) >> 1;
itemClientId = ((const OrderObserver*)GetItemDataPtr(middle))->GetClientId();
if(itemClientId < clientIdToFind)
{
high = middle - 1;
}
else if(itemClientId > clientIdToFind)
{
low = middle + 1;
}
else
{
/*
if(middle > 0)//case when duplicates are possible; get the topmost item out of all duplicates
{
for(--middle; middle >= 0; --middle)
{
if(((const OrderObserver*)GetItemDataPtr(middle))->GetClientId() != clientIdToFind)
{
break;
}
}
return middle + 1;
}
*/
return middle;
}
}
return -1;
}
void ListBoxOrder::LogCancelError(unsigned char errorCode, const char* prefix)
{
if(errorCode != CE_OK)
{
std::string errorMessage;
TU_GetCancelErrorMessage(errorCode, errorMessage);
std::string extensionPrefix("ExtSample Order Cancel ");
if(prefix)
{
extensionPrefix += ' ';
extensionPrefix += prefix;
}
TD_Log(errorMessage.c_str(), extensionPrefix.c_str());
}
}
void ListBoxOrder::LogOrderError(unsigned char errorCode, const char* prefix)
{
if(errorCode != OE_OK)
{
std::string errorMessage;
TU_GetOrderErrorMessage(errorCode, errorMessage);
std::string extensionPrefix("ExtSample Order");
if(prefix)
{
extensionPrefix += ' ';
extensionPrefix += prefix;
}
TD_Log(errorMessage.c_str(), extensionPrefix.c_str());
}
}
//////////////////
BEGIN_MESSAGE_MAP(ListBoxOrder, ColumnedListBox)
ON_MESSAGE(WM_USER + 444, OnItemRepaint)
END_MESSAGE_MAP()
LRESULT ListBoxOrder::OnItemRepaint(WPARAM clientId, LPARAM columns)
{
int ordinal = FindOrderByClientId((unsigned int)clientId);
// InvalidateItem(ordinal);
InvalidateItemColumnRange(ordinal, (unsigned short)(columns >> 16), (unsigned short)columns);
return 0;
}
void ListBoxOrder::AddItem(const OrderObserver* item)
{
AddString((const char*)item);
RepaintCount();
if(item->isIso())
{
UpdateIsoCount(0, 1);
}
if(item->isReplacingNatively())
{
UpdateReplacingNativelyCount(0, 1);
}
#ifndef TAKION_NO_OPTIONS
if(item->isCallPut())
{
UpdateOptionCount(0, 1);
}
#endif
}
void ListBoxOrder::RemoveItemAt(int found, const OrderObserver* item)
{
UpdateSizePending(item->GetSizePending(), 0);
UpdateSizeExecuted(item->GetSizeExecuted(), 0);
if(item->isIso())
{
UpdateIsoCount(1, 0);
}
if(item->isReplacingNatively())
{
UpdateReplacingNativelyCount(1, 0);
}
#ifndef TAKION_NO_OPTIONS
if(item->isCallPut())
{
UpdateOptionCount(1, 0);
}
#endif
UpdateClosedPnl(item->GetClosedPnl(), Money::moneyZero);//TU_GetMoneyZero());
UpdateBpUsed(item->GetBpUsed(), Money::moneyZero);//TU_GetMoneyZero());
DeleteString(found);
RepaintCount();
}
bool ListBoxOrder::RemoveRowAt(int found)
{
if(found >= 0 && (unsigned int)found < m_itemCount)//GetCount())
{
RemoveItemAt(found, (const OrderObserver*)GetItemDataPtr(found));
return true;
}
return false;
}
bool ListBoxOrder::RemoveRow(const OrderObserver* item)
{
return RemoveOrderByClientId(item->GetClientId());
}
bool ListBoxOrder::RemoveOrder(const Order* order)
{
return RemoveOrderByClientId(order->GetClientId());
}
bool ListBoxOrder::RemoveOrderByClientId(unsigned int clientId)
{
const int found = FindOrderByClientId(clientId);
if(found >= 0)
{
RemoveRowAt(found);
return true;
}
return false;
}
void ListBoxOrder::ResetTotals()
{
m_owner->RepaintCount();
if(!m_closedPnl.isZero())
{
m_closedPnl.SetZero();
m_owner->RepaintClosedPnl();//RepaintClosedPnl();
}
if(!m_bpUsed.isZero())
{
m_bpUsed.SetZero();
m_owner->RepaintBpUsed();//RepaintOpenPnl();
}
if(m_sizePending)
{
m_sizePending = 0;
m_owner->RepaintSizePending();
}
if(m_sizeExecuted)
{
m_sizeExecuted = 0;
m_owner->RepaintSizeExecuted();
}
if(m_isoCount)
{
m_isoCount = 0;
m_owner->RepaintIsoCount();
}
if(m_replacingNativelyCount)
{
m_replacingNativelyCount = 0;
m_owner->RepaintReplacingNativelyCount();
}
#ifndef TAKION_NO_OPTIONS
if(m_optionCount)
{
m_optionCount = 0;
m_owner->RepaintOptionCount();
}
#endif
}
#ifndef TAKION_NO_OPTIONS
void ListBoxOrder::RepaintOptionCount() const
{
m_owner->RepaintOptionCount();
}