Repository navigation
Expand file tree
/
Copy pathtraderspi.cpp
More file actions
4414 lines (3825 loc) · 126 KB
/
Copy pathtraderspi.cpp
File metadata and controls
4414 lines (3825 loc) · 126 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 "traderspi.h"
#include "xTrader.h"
extern HANDLE g_hEvent;
void CtpTdSpi::OnFrontConnected()
{
if (g_Dlg && g_Dlg->m_bTdClosed){ ClrAllVecs(); }
if (strlen(m_sAtCode)) { ReqAuth(PROD_INFO,m_sAtCode); }
else { ReqUserLogin(); }
}
CtpTdSpi::~CtpTdSpi()
{
ClrAllVecs();
}
void CtpTdSpi::ReqAuth(TThostFtdcProductInfoType prodInf,TThostFtdcAuthCodeType AuthCode)
{
CThostFtdcReqAuthenticateField req;
ZeroMemory(&req, sizeof(req));
strcpy(req.BrokerID, m_sBkrId);
strcpy(req.UserID, m_sUid);
strcpy(req.UserProductInfo, prodInf);
strcpy(req.AuthCode,AuthCode);
m_pApi->ReqAuthenticate(&req,++m_iReqId);
}
//如果需要认证 就在成功后继续登录
void CtpTdSpi::OnRspAuthenticate(CThostFtdcRspAuthenticateField *pRspAuthenticateField, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
if (pRspInfo) { memcpy(&m_RspMsg,pRspInfo,sizeof(CThostFtdcRspInfoField)); }
if(bIsLast && !IsErrorRspInfo(pRspInfo)) { ReqUserLogin(); }
}
void CtpTdSpi::ReqUserLogin()
{
CThostFtdcReqUserLoginField req;
ZeroMemory(&req, sizeof(req));
strcpy(req.BrokerID, m_sBkrId);
strcpy(req.UserID, m_sUid);
strcpy(req.Password, m_sPwd);
if (strlen(m_sDymPwd)) { strcpy(req.OneTimePassword,m_sDymPwd); }
strcpy(req.UserProductInfo,PROD_INFO);
int iRet = m_pApi->ReqUserLogin(&req, ++m_iReqId);
}
#define TIME_NULL "--:--:--"
void CtpTdSpi::OnRspUserLogin(CThostFtdcRspUserLoginField *pRspUserLogin,
CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
if ( pRspInfo){memcpy(&m_RspMsg,pRspInfo,sizeof(CThostFtdcRspInfoField));}
if (!IsErrorRspInfo(pRspInfo) && pRspUserLogin )
{
// 保存会话参数
m_iFrtId = pRspUserLogin->FrontID;
m_iSesId = pRspUserLogin->SessionID;
strcpy(m_sTdday,pRspUserLogin->TradingDay);
m_iOrdRef = atoi(pRspUserLogin->MaxOrderRef);
SYSTEMTIME curTime;
::GetLocalTime(&curTime);
CTime tc(curTime);
int i=0,iHour[5],iMin[5],iSec[5];
TThostFtdcTimeType ExhTime[5];
strcpy(ExhTime[0],pRspUserLogin->SHFETime);
strcpy(ExhTime[1],pRspUserLogin->DCETime);
strcpy(ExhTime[2],pRspUserLogin->CZCETime);
strcpy(ExhTime[3],pRspUserLogin->FFEXTime);
strcpy(ExhTime[4],pRspUserLogin->INETime);
for (i=0;i<5;i++)
{
if (!strcmp(ExhTime[i],TIME_NULL))
{iHour[i]=curTime.wHour; iMin[i]=curTime.wMinute; iSec[i]=curTime.wSecond;}
else
{ sscanf(ExhTime[i], "%d:%d:%d", &iHour[i], &iMin[i], &iSec[i]); }
m_tsEXnLocal[i] = CTime(curTime.wYear,curTime.wMonth,curTime.wDay,iHour[i],iMin[i],iSec[i])-tc;
}
if (g_Dlg) { ::PostMessage(g_Dlg->m_hWnd,WM_TDRECON_MSG,0,0); }
}
if(bIsLast) SetEvent(g_hEvent);
}
const char* CtpTdSpi::GetTradingDay()
{
//登录才能准确获取
return m_sTdday;
}
void CtpTdSpi::ReqUserLogout()
{
CThostFtdcUserLogoutField req;
ZeroMemory(&req, sizeof(req));
strcpy(req.BrokerID, m_sBkrId);
strcpy(req.UserID, m_sUid);
m_pApi->ReqUserLogout(&req, ++m_iReqId);
}
///登出请求响应
void CtpTdSpi::OnRspUserLogout(CThostFtdcUserLogoutField *pUserLogout, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
if( !IsErrorRspInfo(pRspInfo) && pUserLogout)
{ }
if(bIsLast) SetEvent(g_hEvent);
}
void CtpTdSpi::ReqSetInfConfirm()
{
CThostFtdcSettlementInfoConfirmField req;
ZeroMemory(&req, sizeof(req));
strcpy(req.BrokerID, m_sBkrId);
strncpy(req.InvestorID, m_sUid,sizeof(TThostFtdcInvestorIDType));
m_pApi->ReqSettlementInfoConfirm(&req, ++m_iReqId);
}
void CtpTdSpi::OnRspSettlementInfoConfirm(
CThostFtdcSettlementInfoConfirmField *pSettlementInfoConfirm,
CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
if( !IsErrorRspInfo(pRspInfo) && pSettlementInfoConfirm)
{
}
else
{
if (pRspInfo)
{
memcpy(&m_RspMsg,pRspInfo,sizeof(CThostFtdcRspInfoField));
ShowCbErrs(pRspInfo->ErrorMsg);
}
}
if(bIsLast) SetEvent(g_hEvent);
}
void CtpTdSpi::ReqQryNotice()
{
CThostFtdcQryNoticeField req;
ZeroMemory(&req, sizeof(req));
strcpy(req.BrokerID, m_sBkrId);
while (true)
{
int iRet = m_pApi->ReqQryNotice(&req, ++m_iReqId);
if (!IsFlowControl(iRet)){ break; }
else
{ Sleep(FLOWCTL_TO); }
}
}
void CtpTdSpi::OnRspQryNotice(CThostFtdcNoticeField *pNotice, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
if (!IsErrorRspInfo(pRspInfo) && pNotice){ m_BkrNtVec.push_back(*pNotice); }
else
{
if (pRspInfo)
{
memcpy(&m_RspMsg,pRspInfo,sizeof(CThostFtdcRspInfoField));
ShowCbErrs(pRspInfo->ErrorMsg);
}
}
if (bIsLast)
{
if (g_Dlg){ SendNotifyMessage(g_Dlg->m_hWnd,WM_QRYBKRNT_MSG,0,0); }
SetEvent(g_hEvent);
}
}
void CtpTdSpi::ReqQryTdNotice()
{
CThostFtdcQryTradingNoticeField req;
ZeroMemory(&req, sizeof(req));
strcpy(req.BrokerID, m_sBkrId);
strncpy(req.InvestorID, m_sUid,sizeof(TThostFtdcInvestorIDType));
while (true)
{
int iRet = m_pApi->ReqQryTradingNotice(&req, ++m_iReqId);
if (!IsFlowControl(iRet)){ break; }
else
{ Sleep(FLOWCTL_TO); }
}
}
void CtpTdSpi::OnRspQryTradingNotice(CThostFtdcTradingNoticeField *pTradingNotice, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
if (!IsErrorRspInfo(pRspInfo) && pTradingNotice){ m_TdNoticeVec.push_back(*pTradingNotice); }
else
{
if (pRspInfo)
{
memcpy(&m_RspMsg,pRspInfo,sizeof(CThostFtdcRspInfoField));
ShowCbErrs(pRspInfo->ErrorMsg);
}
}
if(bIsLast)
{
if (g_Dlg){ SendNotifyMessage(g_Dlg->m_hWnd,WM_QRYTDNT_MSG,0,0); }
SetEvent(g_hEvent);
}
}
void CtpTdSpi::ReqQrySetInfConfirm()
{
CThostFtdcQrySettlementInfoConfirmField req;
ZeroMemory(&req, sizeof(req));
strcpy(req.BrokerID, m_sBkrId);
strncpy(req.InvestorID, m_sUid,sizeof(TThostFtdcInvestorIDType));
while (true)
{
int iRet = m_pApi->ReqQrySettlementInfoConfirm(&req,++m_iReqId);
if (!IsFlowControl(iRet)){ break; }
else
{ Sleep(FLOWCTL_TO); }
}
}
void CtpTdSpi::OnRspQrySettlementInfoConfirm(CThostFtdcSettlementInfoConfirmField *pSettlementInfoConfirm, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
if (!IsErrorRspInfo(pRspInfo) && pSettlementInfoConfirm)
{
//
}
else
{
if (pRspInfo)
{
memcpy(&m_RspMsg,pRspInfo,sizeof(CThostFtdcRspInfoField));
ShowCbErrs(pRspInfo->ErrorMsg);
}
}
if(bIsLast) SetEvent(g_hEvent);
}
void CtpTdSpi::ReqQrySetInf(TThostFtdcDateType TdDay)
{
CThostFtdcQrySettlementInfoField req;
ZeroMemory(&req, sizeof(req));
strcpy(req.BrokerID, m_sBkrId);
strncpy(req.InvestorID, m_sUid,sizeof(TThostFtdcInvestorIDType));
strcpy(req.TradingDay,TdDay);
while (true)
{
int iRet = m_pApi->ReqQrySettlementInfo(&req,++m_iReqId);
if (!IsFlowControl(iRet)){ break; }
else
{ Sleep(FLOWCTL_TO); }
}
}
void CtpTdSpi::OnRspQrySettlementInfo(CThostFtdcSettlementInfoField *pSettlementInfo, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
if (!IsErrorRspInfo(pRspInfo) && pSettlementInfo){ m_StmiVec.push_back(*pSettlementInfo); }
else
{
if (pRspInfo)
{
memcpy(&m_RspMsg,pRspInfo,sizeof(CThostFtdcRspInfoField));
ShowCbErrs(pRspInfo->ErrorMsg);
}
}
if(bIsLast)
{
if (g_Dlg){ SendNotifyMessage(HWND_BROADCAST,WM_QRYSMI_MSG,0,0); }
else
{ SetEvent(g_hEvent); }
}
}
void CtpTdSpi::ReqQryInst(TThostFtdcInstrumentIDType instId)
{
CThostFtdcQryInstrumentField req;
ZeroMemory(&req, sizeof(req));
if (instId != NULL){ strcpy(req.InstrumentID, instId); }
while (true)
{
int iRet = m_pApi->ReqQryInstrument(&req, ++m_iReqId);
if (!IsFlowControl(iRet)){ break; }
else
{ Sleep(FLOWCTL_TO); }
}
}
void CtpTdSpi::OnRspQryInstrument(CThostFtdcInstrumentField *pInstrument,
CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
if ( !IsErrorRspInfo(pRspInfo) && pInstrument)
{
if(((pInstrument->ProductClass ==THOST_FTDC_PC_Futures) ||(pInstrument->ProductClass ==THOST_FTDC_PC_Options)||
(pInstrument->ProductClass ==THOST_FTDC_PC_SpotOption) /*||(pInstrument->ProductClass ==THOST_FTDC_PC_Combination)*/)&&
(pInstrument->IsTrading))
{
CThostFtdcInstrumentFieldEx infEx;
ZeroMemory(&infEx,sizeof(CThostFtdcInstrumentFieldEx));
memcpy(&infEx,pInstrument,sizeof(CThostFtdcInstrumentField));
FiltInfEx(infEx);
string strkey = pInstrument->InstrumentID;
MIT_if iter = m_InsinfMap.find(strkey);
if(iter == m_InsinfMap.end()){ m_InsinfMap.insert(make_pair(strkey,infEx)); }
else
{ iter->second = infEx; }
}
}
else
{
if (pRspInfo)
{
memcpy(&m_RspMsg,pRspInfo,sizeof(CThostFtdcRspInfoField));
ShowCbErrs(pRspInfo->ErrorMsg);
}
}
if(bIsLast)
{
CAppCfgs& s = g_s;
sort(s.m_InsinfVec.begin(),s.m_InsinfVec.end(),cmpInst);
ClearMap(m_FeeRateMap); ClearMap(m_MgrFtMap);
SetEvent(g_hEvent);
}
}
void CtpTdSpi::ReqQryTdAcc()
{
CThostFtdcQryTradingAccountField req;
ZeroMemory(&req, sizeof(req));
strcpy(req.BrokerID, m_sBkrId);
strncpy(req.InvestorID, m_sUid,sizeof(TThostFtdcInvestorIDType));
while (true)
{
int iRet = m_pApi->ReqQryTradingAccount(&req, ++m_iReqId);
if (!IsFlowControl(iRet)){ break; }
else
{ Sleep(FLOWCTL_TO); }
}
}
//如果有担保 信用账户 可能需要vec来接收多账户信息
void CtpTdSpi::OnRspQryTradingAccount(
CThostFtdcTradingAccountField *pTradingAccount,
CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
if (!IsErrorRspInfo(pRspInfo) && pTradingAccount)
{ memcpy(&m_TdAcc,pTradingAccount,sizeof(CThostFtdcTradingAccountField)); }
else
{
if (pRspInfo)
{
memcpy(&m_RspMsg,pRspInfo,sizeof(CThostFtdcRspInfoField));
ShowCbErrs(pRspInfo->ErrorMsg);
}
}
if(bIsLast)
{
if (g_Dlg)
{ SendNotifyMessage(g_Dlg->m_hWnd,WM_QRYACC_MSG,0,0); }
//else
{ SetEvent(g_hEvent); }
}
}
//INSTRUMENT_ID设成部分字段,例如IF10,就能查出所有IF10打头的头寸
void CtpTdSpi::ReqQryInvPos(TThostFtdcInstrumentIDType instId)
{
CThostFtdcQryInvestorPositionField req;
ZeroMemory(&req, sizeof(req));
strcpy(req.BrokerID, m_sBkrId);
strncpy(req.InvestorID, m_sUid,sizeof(TThostFtdcInvestorIDType));
if (instId!=NULL)
{strcpy(req.InstrumentID, instId);}
while (true)
{
int iRet = m_pApi->ReqQryInvestorPosition(&req, ++m_iReqId);
if (!IsFlowControl(iRet)){ break; }
else
{ Sleep(FLOWCTL_TO); }
}
}
void CtpTdSpi::OnRspQryInvestorPosition(CThostFtdcInvestorPositionField *pInvestorPosition,
CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
if( !IsErrorRspInfo(pRspInfo) && pInvestorPosition )
{ JoinTdYdPos(pInvestorPosition); }
else
{
if (pRspInfo)
{
memcpy(&m_RspMsg,pRspInfo,sizeof(CThostFtdcRspInfoField));
ShowCbErrs(pRspInfo->ErrorMsg);
}
}
if(bIsLast) SetEvent(g_hEvent);
}
void CtpTdSpi::JoinTdYdPos(const CThostFtdcInvestorPositionField *pInv)
{
if (pInv->Position >0)
{
VIT_pos vpos = m_InvPosVec.begin();
for (vpos;vpos != m_InvPosVec.end();vpos++)
{
if (!strcmp(vpos->InstrumentID,pInv->InstrumentID) && (vpos->PosiDirection==pInv->PosiDirection))
{
vpos->YdPosition += pInv->YdPosition;
vpos->Position += pInv->Position;
vpos->LongFrozen += pInv->LongFrozen;
vpos->ShortFrozen += pInv->ShortFrozen;
vpos->LongFrozenAmount += pInv->LongFrozenAmount;
vpos->ShortFrozenAmount += pInv->ShortFrozenAmount;
vpos->OpenVolume += pInv->OpenVolume;
vpos->CloseVolume += pInv->CloseVolume;
vpos->OpenAmount += pInv->OpenAmount;
vpos->CloseAmount += pInv->CloseAmount;
vpos->PositionCost += pInv->PositionCost;
vpos->PreMargin += pInv->PreMargin;
vpos->UseMargin += pInv->UseMargin;
vpos->FrozenMargin += pInv->FrozenMargin;
vpos->FrozenCash += pInv->FrozenCash;
vpos->FrozenCommission += pInv->FrozenCommission;
vpos->CashIn += pInv->CashIn;
vpos->Commission += pInv->Commission;
vpos->CloseProfit += pInv->CloseProfit;
vpos->PositionProfit += pInv->PositionProfit;
vpos->OpenCost += pInv->OpenCost;
vpos->CombPosition += pInv->CombPosition;
vpos->CombLongFrozen += pInv->CombLongFrozen;
vpos->CombShortFrozen += pInv->CombShortFrozen;
vpos->CloseProfitByDate += pInv->CloseProfitByDate;
vpos->CloseProfitByTrade += pInv->CloseProfitByTrade;
vpos->TodayPosition += pInv->TodayPosition;
vpos->StrikeFrozen += pInv->StrikeFrozen;
vpos->StrikeFrozenAmount += pInv->StrikeFrozenAmount;
vpos->AbandonFrozen += pInv->AbandonFrozen;
break;
}
}
if (vpos == m_InvPosVec.end()){ m_InvPosVec.push_back(*pInv); }
}
//else if (pInv->Position == 0)
//{
// //持仓量为0的表示清过仓 但里面记录了YdPos的原始值
//}
}
//INSTRUMENT_ID设成部分字段,例如IF10,就能查出所有IF10打头的头寸
void CtpTdSpi::ReqQryInvPosEx(TThostFtdcInstrumentIDType instId)
{
CThostFtdcQryInvestorPositionDetailField req;
ZeroMemory(&req, sizeof(req));
strcpy(req.BrokerID, m_sBkrId);
strncpy(req.InvestorID, m_sUid,sizeof(TThostFtdcInvestorIDType));
if (instId!=NULL) {strcpy(req.InstrumentID, instId);}
while (true)
{
int iRet = m_pApi->ReqQryInvestorPositionDetail(&req, ++m_iReqId);
if (!IsFlowControl(iRet)){ break; }
else
{ Sleep(FLOWCTL_TO); }
}
}
void CtpTdSpi::OnRspQryInvestorPositionDetail(CThostFtdcInvestorPositionDetailField *pInvestorPositionDetail,
CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
if( !IsErrorRspInfo(pRspInfo) && pInvestorPositionDetail)
{
}
else
{
if (pRspInfo)
{
memcpy(&m_RspMsg,pRspInfo,sizeof(CThostFtdcRspInfoField));
ShowCbErrs(pRspInfo->ErrorMsg);
}
}
if(bIsLast) SetEvent(g_hEvent);
}
void CtpTdSpi::ReqQryInvPosCombEx(TThostFtdcInstrumentIDType instId)
{
CThostFtdcQryInvestorPositionCombineDetailField req;
ZeroMemory(&req, sizeof(req));
strcpy(req.BrokerID, m_sBkrId);
strncpy(req.InvestorID, m_sUid,sizeof(TThostFtdcInvestorIDType));
if (instId!=NULL) {strcpy(req.CombInstrumentID, instId);}
while (true)
{
int iRet = m_pApi->ReqQryInvestorPositionCombineDetail(&req, ++m_iReqId);
if (!IsFlowControl(iRet)){ break; }
else
{ Sleep(FLOWCTL_TO); }
}
}
void CtpTdSpi::OnRspQryInvestorPositionCombineDetail(CThostFtdcInvestorPositionCombineDetailField *pInvestorPositionDetail,
CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
if( !IsErrorRspInfo(pRspInfo) && pInvestorPositionDetail)
{
}
else
{
if (pRspInfo)
{
memcpy(&m_RspMsg,pRspInfo,sizeof(CThostFtdcRspInfoField));
ShowCbErrs(pRspInfo->ErrorMsg);
}
}
if(bIsLast) SetEvent(g_hEvent);
}
///请求查询交易所
void CtpTdSpi::ReqQryExh(TThostFtdcExchangeIDType ExhId)
{
CThostFtdcQryExchangeField req;
if (ExhId!=NULL) { strcpy(req.ExchangeID,ExhId); }
while (true)
{
int iRet = m_pApi->ReqQryExchange(&req, ++m_iReqId);
if (!IsFlowControl(iRet)){ break; }
else
{ Sleep(FLOWCTL_TO); }
}
}
///请求查询交易所响应
void CtpTdSpi::OnRspQryExchange(CThostFtdcExchangeField *pExchange, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
if( !IsErrorRspInfo(pRspInfo) && pExchange){ m_ExhVec.push_back(*pExchange); }
else
{
if (pRspInfo)
{
memcpy(&m_RspMsg,pRspInfo,sizeof(CThostFtdcRspInfoField));
ShowCbErrs(pRspInfo->ErrorMsg);
}
}
if(bIsLast)
{
//SendNotifyMessage(HWND_BROADCAST,WM_QRYEXH_MSG,0,0);
SetEvent(g_hEvent);
}
}
///请求查询行情
void CtpTdSpi::ReqQryDepthMD(TThostFtdcInstrumentIDType instId)
{
CThostFtdcQryDepthMarketDataField req;
ZeroMemory(&req,sizeof(req));
if (instId != NULL){ strcpy(req.InstrumentID,instId); }
while (true)
{
int iRet = m_pApi->ReqQryDepthMarketData(&req, ++m_iReqId);
if (!IsFlowControl(iRet)){ break; }
else
{ Sleep(FLOWCTL_TO); }
}
}
///请求查询行情响应
void CtpTdSpi::OnRspQryDepthMarketData(CThostFtdcDepthMarketDataField *pDepthMarketData, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
if( !IsErrorRspInfo(pRspInfo) && pDepthMarketData)
{
string strkey = pDepthMarketData->InstrumentID;
MIT_md iter = m_DepthMdMap.find(strkey);
if(iter == m_DepthMdMap.end())
{ m_DepthMdMap.insert(make_pair(strkey,*pDepthMarketData)); }
else
{ iter->second = *pDepthMarketData; }
}
else
{
if (pRspInfo)
{
memcpy(&m_RspMsg,pRspInfo,sizeof(CThostFtdcRspInfoField));
ShowCbErrs(pRspInfo->ErrorMsg);
}
}
if(bIsLast) { SetEvent(g_hEvent); }
}
///请求查询报单
void CtpTdSpi::ReqQryOrd(TThostFtdcInstrumentIDType instId,TThostFtdcExchangeIDType ExhId,TThostFtdcOrderSysIDType ordSysId,
TThostFtdcTimeType tmStart,TThostFtdcTimeType tmEnd)
{
CThostFtdcQryOrderField req;
ZeroMemory(&req,sizeof(req));
strcpy(req.BrokerID, m_sBkrId);
strncpy(req.InvestorID, m_sUid,sizeof(TThostFtdcInvestorIDType));
if(ExhId !=NULL) strcpy(req.ExchangeID,ExhId);
if(instId !=NULL) strcpy(req.InstrumentID,instId);
if(ordSysId !=NULL) strcpy(req.OrderSysID,ordSysId);
if(tmStart !=NULL) strcpy(req.InsertTimeStart,tmStart);
if(tmEnd !=NULL) strcpy(req.InsertTimeEnd,tmEnd);
while (true)
{
int iRet = m_pApi->ReqQryOrder(&req, ++m_iReqId);
if (!IsFlowControl(iRet)){ break; }
else
{ Sleep(FLOWCTL_TO); }
}
}
///请求查询报单响应
void CtpTdSpi::OnRspQryOrder(CThostFtdcOrderField *pOrder, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
if( !IsErrorRspInfo(pRspInfo) && pOrder )
{
if (!IsCondOrd(pOrder))
{
CORDFEx order(pOrder,GetTdAvgPx(pOrder->BrokerOrderSeq));
VIT_ord vod = find(m_orderVec.begin(), m_orderVec.end(), order);
if (vod!=m_orderVec.end()){ *vod = order; }
else
{ m_orderVec.push_back(order); }
//掉线重查时此处需要修正
if(JgCancelStat(&order)){ m_onRoadVec.push_back(order); }
}
else
{
PARKEDEX pkEx;
OrdConvter(*pOrder,pkEx,ORD2PARKED);
if (pOrder->ContingentCondition > THOST_FTDC_CC_ParkedOrder) //条件单
{ pkEx.ordType = COND_SERVER; }
else if (pOrder->ContingentCondition == THOST_FTDC_CC_Touch)
{ pkEx.ordType = TOUCH_ZS; }
else if (pOrder->ContingentCondition == THOST_FTDC_CC_TouchProfit)
{ pkEx.ordType = TOUCH_ZY; }
VIT_pk vpk = m_pkExVec.begin();
for (vpk;vpk!=m_pkExVec.end();vpk++)
{
if (!strcmp(vpk->OrderSysID,pOrder->OrderSysID))
{
*vpk = pkEx;
break;
}
}
if(vpk == m_pkExVec.end()) { m_pkExVec.push_back(pkEx); }
}
}
else
{
if (pRspInfo)
{
memcpy(&m_RspMsg,pRspInfo,sizeof(CThostFtdcRspInfoField));
ShowCbErrs(pRspInfo->ErrorMsg);
}
}
if(bIsLast)
{
sort(m_orderVec.begin(),m_orderVec.end(),cmpOrder);
SetEvent(g_hEvent);
}
}
///请求查询成交
void CtpTdSpi::ReqQryTd(TThostFtdcInstrumentIDType instId,TThostFtdcExchangeIDType ExhId,TThostFtdcTradeIDType tdId,
TThostFtdcTimeType tmStart,TThostFtdcTimeType tmEnd)
{
CThostFtdcQryTradeField req;
ZeroMemory(&req,sizeof(req));
strcpy(req.BrokerID, m_sBkrId);
strncpy(req.InvestorID, m_sUid,sizeof(TThostFtdcInvestorIDType));
if (ExhId !=NULL)strcpy(req.ExchangeID,ExhId);
if (instId !=NULL)strcpy(req.InstrumentID,instId);
if (tdId !=NULL)strcpy(req.TradeID,tdId);
if (tmStart !=NULL)strcpy(req.TradeTimeStart,tmStart);
if (tmEnd !=NULL)strcpy(req.TradeTimeEnd,tmEnd);
while (true)
{
int iRet = m_pApi->ReqQryTrade(&req, ++m_iReqId);
if (!IsFlowControl(iRet)){ break; }
else
{ Sleep(FLOWCTL_TO); }
}
}
///请求查询成交响应
void CtpTdSpi::OnRspQryTrade(CThostFtdcTradeField *pTrade, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
if( !IsErrorRspInfo(pRspInfo) && pTrade )
{
CTDFEx td(pTrade,GetTdFee(pTrade));
VIT_td vtd = find(m_tradeVec.begin(), m_tradeVec.end(), td);
if (vtd!=m_tradeVec.end()){ *vtd = td; }
else
{ m_tradeVec.push_back(td); }
}
else
{
if (pRspInfo)
{
memcpy(&m_RspMsg,pRspInfo,sizeof(CThostFtdcRspInfoField));
ShowCbErrs(pRspInfo->ErrorMsg);
}
}
if(bIsLast)
{
sort(m_tradeVec.begin(),m_tradeVec.end(),cmpTrade);
SetEvent(g_hEvent);
}
}
UINT CtpTdSpi::TmOutAutoDellThd(LPVOID pParam)
{
PTOAUTODELP pAutoDel = static_cast<PTOAUTODELP>(pParam);
CtpTdSpi* td = static_cast<CtpTdSpi*>(pAutoDel->td);
if (pAutoDel)
{
Sleep(1000*pAutoDel->uTmOut+1);
//唯一定位执行撤单立即返回
if (strlen(pAutoDel->ordSysId))
{
//条件单
VIT_pk vpk=td->m_pkExVec.begin();
for(vpk;vpk!=td->m_pkExVec.end();vpk++)
{
if (!strcmp(vpk->OrderSysID,pAutoDel->ordSysId) &&
!strcmp(vpk->park.ExchangeID,pAutoDel->ExhId))
{
if (vpk->park.Status == THOST_FTDC_OST_NotTouched)
{ td->ReqOrdCancel(pAutoDel->ExhId,pAutoDel->ordSysId); }
break;
}
}
}
else
{
//普通单
VIT_ord vord = td->m_orderVec.begin();
for (vord;vord!=td->m_orderVec.end();vord++)
{
if (vord->Ord.FrontID == pAutoDel->iFrtId &&
vord->Ord.SessionID == pAutoDel->iSesId &&
!strcmp(vord->Ord.OrderRef,pAutoDel->sRef) &&
!strcmp(vord->Ord.InstrumentID,pAutoDel->instId))
{
if (vord->Ord.OrderStatus == THOST_FTDC_OST_PartTradedQueueing ||
vord->Ord.OrderStatus == THOST_FTDC_OST_NoTradeQueueing)
{ td->ReqOrdCancel(pAutoDel->iFrtId,pAutoDel->iSesId,pAutoDel->sRef,pAutoDel->instId); }
break;
}
}
}
DEL(pAutoDel);
}
return 0;
}
//////////////////////////////////////////////////
void CtpTdSpi::AutoDelLocOrd(const CThostFtdcInputOrderField *pInp)
{
CAppCfgs& s = g_s;
//if (s.m_bOrdTmOut)
{
PTOAUTODELP pAutoDel = new TOAUTODELP(this,s.m_uOrdToSec,m_iFrtId,m_iSesId,
pInp->OrderRef,pInp->InstrumentID);
AfxBeginThread((AFX_THREADPROC)TmOutAutoDellThd,pAutoDel);
}
}
void CtpTdSpi::AutoDelNoLocOrd(const CThostFtdcOrderField *pOrder)
{
CAppCfgs& s = g_s;
/////////////////////其它终端报单也监测////////////////////////
if (/*s.m_bOrdTmOut &&*/
(strcmp(pOrder->IPAddress,s.m_sLocIp) ||
strcmp(pOrder->MacAddress,s.m_sLocMac) ||
strcmp(pOrder->UserProductInfo,PROD_INFO)) &&
(pOrder->OrderStatus == THOST_FTDC_OST_Unknown ||
pOrder->OrderStatus == THOST_FTDC_OST_PartTradedQueueing ||
pOrder->OrderStatus == THOST_FTDC_OST_NoTradeQueueing))
{
PTOAUTODELP pAutoDel = new TOAUTODELP(this,s.m_uOrdToSec,pOrder->FrontID,
pOrder->SessionID,pOrder->OrderRef,pOrder->InstrumentID);
AfxBeginThread((AFX_THREADPROC)TmOutAutoDellThd,pAutoDel);
}
}
//动态止损只能是本地单,普通止损可为服务器或本地条件单
void CtpTdSpi::StopAfterTd(const TThostFtdcPriceType dStopPx,bool bDym)
{
}
void CtpTdSpi::ReqOrds(CThostFtdcInputOrderField* pInp)
{
CAppCfgs& s = g_s;
strcpy(pInp->BrokerID, m_sBkrId);
strncpy(pInp->InvestorID, m_sUid,sizeof(TThostFtdcInvestorIDType));
sprintf(pInp->OrderRef, "%012d", ++m_iOrdRef);
pInp->ForceCloseReason = THOST_FTDC_FCC_NotForceClose;
pInp->IsAutoSuspend = 0;
pInp->UserForceClose = 0;
strcpy(pInp->IPAddress,s.m_sLocIp);
strcpy(pInp->MacAddress,s.m_sLocMac);
m_pApi->ReqOrderInsert(pInp, ++m_iReqId);
//////////////////////////////////////////////////
AutoDelLocOrd(pInp);
}
void CtpTdSpi::ReqOrdLimit(TThostFtdcInstrumentIDType instId,TThostFtdcDirectionType dir,
TThostFtdcOffsetFlagType kpp,TThostFtdcPriceType price,TThostFtdcVolumeType vol)
{
CThostFtdcInputOrderField req;
ZeroMemory(&req, sizeof(req));
strcpy(req.InstrumentID, instId);
req.OrderPriceType = THOST_FTDC_OPT_LimitPrice;
req.Direction = dir;
req.CombOffsetFlag[0] = kpp;
req.CombHedgeFlag[0] = THOST_FTDC_HF_Speculation;
req.LimitPrice = price;
req.VolumeTotalOriginal = vol;
req.TimeCondition = THOST_FTDC_TC_GFD;
req.VolumeCondition = THOST_FTDC_VC_AV;
req.ContingentCondition = THOST_FTDC_CC_Immediately;
ReqOrds(&req);
}
///市价单
void CtpTdSpi::ReqOrdAny(TThostFtdcInstrumentIDType instId,TThostFtdcDirectionType dir, TThostFtdcOffsetFlagType kpp,TThostFtdcVolumeType vol)
{
CThostFtdcInputOrderField req;
ZeroMemory(&req, sizeof(req));
strcpy(req.InstrumentID, instId);
req.OrderPriceType = THOST_FTDC_OPT_AnyPrice;
req.CombHedgeFlag[0] = THOST_FTDC_HF_Speculation;
req.Direction = dir;
req.CombOffsetFlag[0] = kpp;
req.VolumeTotalOriginal = vol;
req.TimeCondition = THOST_FTDC_TC_IOC;
req.VolumeCondition = THOST_FTDC_VC_AV;
req.ContingentCondition = THOST_FTDC_CC_Immediately;
ReqOrds(&req);
}
//中金所高级市价单
void CtpTdSpi::ReqOrdAnyCfx(TThostFtdcInstrumentIDType instId,TThostFtdcDirectionType dir,TThostFtdcOffsetFlagType kpp,
TThostFtdcVolumeType vol,TThostFtdcOrderPriceTypeType OrdPxType,TThostFtdcTimeConditionType tmCond)
{
CThostFtdcInputOrderField req;
ZeroMemory(&req, sizeof(req));
strcpy(req.InstrumentID, instId);
req.CombHedgeFlag[0] = THOST_FTDC_HF_Speculation;
req.OrderPriceType = OrdPxType;
req.Direction = dir;
req.CombOffsetFlag[0] = kpp;
req.VolumeTotalOriginal = vol;
req.TimeCondition = tmCond;
req.VolumeCondition = THOST_FTDC_VC_AV;
req.ContingentCondition = THOST_FTDC_CC_Immediately;
ReqOrds(&req);
}
void CtpTdSpi::ReqOrdCond(TThostFtdcInstrumentIDType instId,TThostFtdcDirectionType dir, TThostFtdcOffsetFlagType kpp,TThostFtdcPriceType price,
TThostFtdcVolumeType vol,TThostFtdcPriceType stopPx,TThostFtdcContingentConditionType conType)
{
CThostFtdcInputOrderField req;
ZeroMemory(&req, sizeof(req));
strcpy(req.InstrumentID, instId);
req.CombHedgeFlag[0] = THOST_FTDC_HF_Speculation;
req.OrderPriceType = THOST_FTDC_OPT_LimitPrice;
req.Direction = dir;
req.CombOffsetFlag[0] = kpp;
req.LimitPrice = price;
req.VolumeTotalOriginal = vol;
req.TimeCondition = THOST_FTDC_TC_GFD;
req.VolumeCondition = THOST_FTDC_VC_AV;
req.ContingentCondition = conType;
req.StopPrice = stopPx;
ReqOrds(&req);
}
//大商所止损止盈 最新>/<触发价时触发新报单,市价或限价
//ContingentCondition:Touch/TouchProfit
void CtpTdSpi::ReqOrdTouch(TThostFtdcInstrumentIDType instId,TThostFtdcDirectionType dir, TThostFtdcOffsetFlagType kpp,
TThostFtdcPriceType price,TThostFtdcVolumeType vol,TThostFtdcPriceType stopPx,TThostFtdcContingentConditionType conType)
{
CThostFtdcInputOrderField req;
ZeroMemory(&req, sizeof(req));
strcpy(req.InstrumentID, instId);
TThostFtdcOrderPriceTypeType tPx = (price<NEARZERO)?THOST_FTDC_OPT_AnyPrice:THOST_FTDC_OPT_LimitPrice;
req.CombHedgeFlag[0] = THOST_FTDC_HF_Speculation;
req.OrderPriceType = tPx;
req.Direction = dir;
req.CombOffsetFlag[0] = kpp;
req.LimitPrice = price;
req.VolumeTotalOriginal = vol;
req.TimeCondition = (tPx==THOST_FTDC_OPT_AnyPrice)?THOST_FTDC_TC_IOC:THOST_FTDC_TC_GFD;
req.VolumeCondition = THOST_FTDC_VC_AV;
req.ContingentCondition = conType;
req.StopPrice = stopPx;
ReqOrds(&req);
}
//FAK(Fill And Kill)指令就是将报单的有效期设为THOST_FTDC_TC_IOC,同时,成交量类型设为THOST_FTDC_VC_AV,即任意数量;
//FOK(Fill Or Kill)指令是将报单的有效期类型设置为THOST_FTDC_TC_IOC,同时将成交量类型设置为THOST_FTDC_VC_CV,即全部数量.
//此外,在FAK指令下,还可指定最小成交量,即在指定价位、满足最小成交数量以上成交,剩余订单被系统撤销,否则被系统全部撤销.此种状况下,
//有效期类型设置为THOST_FTDC_TC_IOC,数量条件设为THOST_FTDC_VC_MV,同时设定MinVolume字段.
void CtpTdSpi::ReqOrdFAOK(TThostFtdcInstrumentIDType instId,TThostFtdcDirectionType dir,TThostFtdcOffsetFlagType kpp,
TThostFtdcPriceType price,TThostFtdcVolumeType vol,TThostFtdcVolumeConditionType volconType,TThostFtdcVolumeType minVol)
{
CThostFtdcInputOrderField req;
ZeroMemory(&req, sizeof(req));
strcpy(req.InstrumentID, instId);
req.CombHedgeFlag[0] = THOST_FTDC_HF_Speculation;
req.OrderPriceType = THOST_FTDC_OPT_LimitPrice;
req.Direction = dir;
req.CombOffsetFlag[0] = kpp;
req.LimitPrice = price;
req.VolumeTotalOriginal = vol;
req.TimeCondition = THOST_FTDC_TC_IOC;
req.VolumeCondition = volconType;
req.MinVolume = minVol;
req.ContingentCondition = THOST_FTDC_CC_Immediately;
ReqOrds(&req);
}
void CtpTdSpi::OnRspOrderInsert(CThostFtdcInputOrderField *pInputOrder,
CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
CXTraderDlg* pDlg = g_Dlg;
if( IsErrorRspInfo(pRspInfo) || (!pInputOrder))