Skip to content

Commit 88226fb

Browse files
committed
历史行情模块
1 parent 8f34acf commit 88226fb

File tree

16 files changed

+176
-75
lines changed

16 files changed

+176
-75
lines changed

QuantBox.XAPI/Callback/BaseApi.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class BaseApi : IDisposable
1616
protected IntPtr Handle = IntPtr.Zero;
1717
protected Queue _Queue;
1818
private string _Path1;
19-
private string _Path2;
19+
//private string _Path2;
2020

2121
protected XCall _XRespone;
2222

QuantBox.XAPI/Callback/XApi.HistoricalData.cs

+15-17
Original file line numberDiff line numberDiff line change
@@ -12,48 +12,46 @@ public partial class XApi
1212
public DelegateOnRspQryHistoricalTicks OnRspQryHistoricalTicks;
1313
public DelegateOnRspQryHistoricalBars OnRspQryHistoricalBars;
1414

15-
public void ReqQryHistoricalTicks(string szInstrument, string szExchange,int datetime1,int datetime2)
15+
public void ReqQryHistoricalTicks(ref HistoricalDataRequestField request)
1616
{
17-
IntPtr szInstrumentPtr = Marshal.StringToHGlobalAnsi(szInstrument);
18-
IntPtr szExchangePtr = Marshal.StringToHGlobalAnsi(szExchange);
17+
IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(HistoricalDataRequestField)));
18+
Marshal.StructureToPtr(request, ptr, false);
1919

2020
proxy.XRequest((byte)RequestType.ReqQryHistoricalTicks, Handle, IntPtr.Zero, 0, 0,
21-
szInstrumentPtr, datetime1, szExchangePtr, datetime2, IntPtr.Zero, 0);
21+
ptr, 0, IntPtr.Zero, 0, IntPtr.Zero, 0);
2222

23-
Marshal.FreeHGlobal(szInstrumentPtr);
24-
Marshal.FreeHGlobal(szExchangePtr);
23+
Marshal.FreeHGlobal(ptr);
2524
}
2625

27-
public void ReqQryHistoricalBars(string szInstrument, string szExchange, int datetime1, int datetime2,long barSize)
26+
public void ReqQryHistoricalBars(ref HistoricalDataRequestField request)
2827
{
29-
IntPtr szInstrumentPtr = Marshal.StringToHGlobalAnsi(szInstrument);
30-
IntPtr szExchangePtr = Marshal.StringToHGlobalAnsi(szExchange);
28+
IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(HistoricalDataRequestField)));
29+
Marshal.StructureToPtr(request, ptr, false);
3130

32-
proxy.XRequest((byte)RequestType.ReqQryHistoricalBars, Handle, IntPtr.Zero, barSize, 0,
33-
szInstrumentPtr, datetime1, szExchangePtr, datetime2, IntPtr.Zero, 0);
31+
proxy.XRequest((byte)RequestType.ReqQryHistoricalBars, Handle, IntPtr.Zero, 0, 0,
32+
ptr, 0, IntPtr.Zero, 0, IntPtr.Zero, 0);
3433

35-
Marshal.FreeHGlobal(szInstrumentPtr);
36-
Marshal.FreeHGlobal(szExchangePtr);
34+
Marshal.FreeHGlobal(ptr);
3735
}
3836

3937

40-
private void _OnRspQryHistoricalTicks(IntPtr ptr1, int size1, double double1)
38+
private void _OnRspQryHistoricalTicks(IntPtr ptr1, int size1, double double1, double double2)
4139
{
4240
if (OnRspQryHistoricalTicks == null)
4341
return;
4442

4543
DepthMarketDataField obj = PInvokeUtility.GetObjectFromIntPtr<DepthMarketDataField>(ptr1);
4644

47-
OnRspQryHistoricalTicks(this, ref obj, size1, double1 != 0);
45+
OnRspQryHistoricalTicks(this, ref obj, size1, double1 != 0,(int)double2);
4846
}
49-
private void _OnRspQryHistoricalBars(IntPtr ptr1, int size1, double double1)
47+
private void _OnRspQryHistoricalBars(IntPtr ptr1, int size1, double double1, double double2)
5048
{
5149
if (OnRspQryHistoricalBars == null)
5250
return;
5351

5452
BarField obj = PInvokeUtility.GetObjectFromIntPtr<BarField>(ptr1);
5553

56-
OnRspQryHistoricalBars(this, ref obj, size1, double1 != 0);
54+
OnRspQryHistoricalBars(this, ref obj, size1, double1 != 0, (int)double2);
5755
}
5856
}
5957
}

QuantBox.XAPI/Callback/XApi.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,10 @@ protected override IntPtr OnRespone(byte type, IntPtr pApi1, IntPtr pApi2, doubl
7474
break;
7575

7676
case ResponeType.OnRspQryHistoricalTicks:
77-
_OnRspQryHistoricalTicks(ptr1, size1, double1);
77+
_OnRspQryHistoricalTicks(ptr1, size1, double1, double2);
7878
break;
7979
case ResponeType.OnRspQryHistoricalBars:
80-
_OnRspQryHistoricalBars(ptr1, size1, double1);
80+
_OnRspQryHistoricalBars(ptr1, size1, double1, double2);
8181
break;
8282

8383
default:

QuantBox.XAPI/Delegate.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ namespace QuantBox.XAPI
2020
public delegate void DelegateOnRtnTrade(object sender, ref TradeField trade);
2121
public delegate void DelegateOnRtnQuote(object sender, ref QuoteField quote);
2222

23-
public delegate void DelegateOnRspQryHistoricalTicks(object sender, ref DepthMarketDataField marketData, int size1, bool bIsLast);
24-
public delegate void DelegateOnRspQryHistoricalBars(object sender, ref BarField bar, int size1, bool bIsLast);
23+
public delegate void DelegateOnRspQryHistoricalTicks(object sender, ref DepthMarketDataField marketData, int size1, bool bIsLast,int pos);
24+
public delegate void DelegateOnRspQryHistoricalBars(object sender, ref BarField bar, int size1, bool bIsLast,int pos);
2525
}

QuantBox.XAPI/Enum.cs

+30-9
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public enum ApiType : byte
9696
Instrument = 32,
9797
};
9898

99-
public enum DataLevelType : byte
99+
public enum DepthLevelType : byte
100100
{
101101
L0,
102102
L1,
@@ -111,13 +111,13 @@ public enum ResumeType : byte
111111
Quick,
112112
};
113113

114-
public enum PutCall : byte
114+
public enum PutCall_ : byte
115115
{
116116
Put,
117117
Call,
118118
};
119119

120-
public enum OrderStatus : byte
120+
public enum OrderStatus_ : byte
121121
{
122122
NotSent,
123123
PendingNew,
@@ -132,13 +132,13 @@ public enum OrderStatus : byte
132132
Replaced,
133133
};
134134

135-
public enum OrderSide : byte
135+
public enum OrderSide_ : byte
136136
{
137137
Buy,
138138
Sell,
139139
};
140140

141-
public enum OrderType : byte
141+
public enum OrderType_ : byte
142142
{
143143
Market,
144144
Stop,
@@ -149,7 +149,7 @@ public enum OrderType : byte
149149
TrailingStopLimit,
150150
};
151151

152-
public enum TimeInForce : byte
152+
public enum TimeInForce_ : byte
153153
{
154154
ATC,
155155
Day,
@@ -163,13 +163,13 @@ public enum TimeInForce : byte
163163
GFS,
164164
};
165165

166-
public enum PositionSide : byte
166+
public enum PositionSide_ : byte
167167
{
168168
Long,
169169
Short,
170170
};
171171

172-
public enum ExecType : byte
172+
public enum ExecType_ : byte
173173
{
174174
ExecNew,
175175
ExecRejected,
@@ -197,7 +197,7 @@ public enum HedgeFlagType : byte
197197
MarketMaker,
198198
};
199199

200-
public enum InstrumentType : byte
200+
public enum InstrumentType_ : byte
201201
{
202202
Stock,
203203
Future,
@@ -210,4 +210,25 @@ public enum InstrumentType : byte
210210
MultiLeg,
211211
Synthetic,
212212
};
213+
214+
public enum BarType_:byte
215+
{
216+
Time,
217+
Tick,
218+
Volume,
219+
Range,
220+
};
221+
222+
public enum DataObjetType_ : byte
223+
{
224+
Tick,
225+
Bid,
226+
Ask,
227+
Trade,
228+
Quote,
229+
Bar,
230+
Level2,
231+
Level2Snapshot,
232+
Level2Update,
233+
};
213234
}

QuantBox.XAPI/PositionFieldEx.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public void AddPosition(PositionField position)
2121
Instrument = position.InstrumentID;
2222
Exchange = position.ExchangeID;
2323

24-
if (position.Side == PositionSide.Long)
24+
if (position.Side == PositionSide_.Long)
2525
{
2626
Long = position;
2727
}

QuantBox.XAPI/Struct.cs

+46-11
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ public struct InstrumentField
388388
/// <summary>
389389
/// 类型
390390
/// </summary>
391-
public InstrumentType Type;
391+
public InstrumentType_ Type;
392392
/// <summary>
393393
/// 合约数量乘数
394394
/// </summary>
@@ -414,7 +414,7 @@ public struct InstrumentField
414414
/// <summary>
415415
/// 期权类型
416416
/// </summary>
417-
public PutCall OptionsType;
417+
public PutCall_ OptionsType;
418418
}
419419

420420
/// <summary>
@@ -544,8 +544,8 @@ public struct QuoteField
544544
public string AskOrderID;
545545
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]
546546
public string BidOrderID;
547-
public OrderStatus Status;
548-
public ExecType ExecType;
547+
public OrderStatus_ Status;
548+
public ExecType_ ExecType;
549549
/// <summary>
550550
/// 错误代码
551551
/// </summary>
@@ -576,20 +576,20 @@ public struct OrderField
576576
/// <summary>
577577
/// 订单类型
578578
/// </summary>
579-
public OrderType Type;
579+
public OrderType_ Type;
580580
/// <summary>
581581
/// 合约代码
582582
/// </summary>
583-
public OrderSide Side;
583+
public OrderSide_ Side;
584584
public double Qty;
585585
public double Price;
586586
public OpenCloseType OpenClose;
587587
public HedgeFlagType HedgeFlag;
588588
public double StopPx;
589-
public TimeInForce TimeInForce;
589+
public TimeInForce_ TimeInForce;
590590

591-
public OrderStatus Status;
592-
public ExecType ExecType;
591+
public OrderStatus_ Status;
592+
public ExecType_ ExecType;
593593
public double LeavesQty;
594594
public double CumQty;
595595
public double AvgPx;
@@ -629,7 +629,7 @@ public struct TradeField
629629
/// <summary>
630630
/// 合约代码
631631
/// </summary>
632-
public OrderSide Side;
632+
public OrderSide_ Side;
633633
public double Qty;
634634
public double Price;
635635
public OpenCloseType OpenClose;
@@ -664,10 +664,45 @@ public struct PositionField
664664
/// <summary>
665665
/// 合约代码
666666
/// </summary>
667-
public PositionSide Side;
667+
public PositionSide_ Side;
668668
public double Position;
669669
public double TdPosition;
670670
public double YdPosition;
671671
public HedgeFlagType HedgeFlag;
672672
}
673+
674+
/// <summary>
675+
/// 合约信息
676+
/// </summary>
677+
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
678+
public struct HistoricalDataRequestField
679+
{
680+
/// <summary>
681+
/// 合约代码
682+
/// </summary>
683+
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]
684+
public string Symbol;
685+
/// <summary>
686+
/// 合约代码
687+
/// </summary>
688+
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 31)]
689+
public string InstrumentID;
690+
/// <summary>
691+
/// 交易所代码
692+
/// </summary>
693+
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 9)]
694+
public string ExchangeID;
695+
696+
public int Date1;
697+
public int Date2;
698+
public int Time1;
699+
public int Time2;
700+
701+
public DataObjetType_ DataType;
702+
public BarType_ BarType;
703+
public int BarSize;
704+
705+
public int RequestId;
706+
public int Count;
707+
}
673708
}

QuantBox_CTP_Trade/TraderApi.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -1663,6 +1663,7 @@ void CTraderApi::OnTrade(TradeField *pTrade)
16631663
pField = new PositionField();
16641664
memset(pField, 0, sizeof(PositionField));
16651665

1666+
strcpy(pField->Symbol, pTrade->InstrumentID);
16661667
strcpy(pField->InstrumentID, pTrade->InstrumentID);
16671668
pField->Side = TradeField_2_PositionSide(pTrade);
16681669
pField->HedgeFlag = TThostFtdcHedgeFlagType_2_HedgeFlagType(pTrade->HedgeFlag);

QuantBox_Esunny_HistoricalData/HistoricalDataApi.cpp

+27-25
Original file line numberDiff line numberDiff line change
@@ -311,8 +311,9 @@ int __cdecl CHistoricalDataApi::OnRspHistoryQuot(struct STKHISDATA *pHisData)
311311
field.Close = item.fClose;
312312
field.Volume = item.fVolume;
313313
field.Turnover = item.fAmount;
314+
314315

315-
XRespone(ResponeType::OnRspQryHistoricalBars, m_msgQueue, this, i >= pHisData->nCount - 1, 0, &field, sizeof(BarField), nullptr, 0, nullptr, 0);
316+
XRespone(ResponeType::OnRspQryHistoricalBars, m_msgQueue, this, i >= pHisData->nCount - 1, i, &field, sizeof(BarField), nullptr, 0, nullptr, 0);
316317
}
317318
return 0;
318319
}
@@ -352,35 +353,36 @@ int __cdecl CHistoricalDataApi::OnRspMarketInfo(struct MarketInfo *pMarketInfo,
352353
return 0;
353354
}
354355

355-
int CHistoricalDataApi::ReqQryHistoricalTicks(const string& szInstrument, const string& szExchange, int datetime1, int datetime2)
356+
int CHistoricalDataApi::ReqQryHistoricalTicks(HistoricalDataRequestField* request)
356357
{
357-
char buf1[20] = {0};
358-
sprintf(buf1, "%d", datetime1);
359-
m_pApi->RequestTrace(szExchange.c_str(), szInstrument.c_str(), buf1);
358+
//char buf1[20] = {0};
359+
//sprintf(buf1, "%d", datetime1);
360+
//m_pApi->RequestTrace(szExchange.c_str(), szInstrument.c_str(), buf1);
360361

361362
return 0;
362363
}
363364

364-
int CHistoricalDataApi::ReqQryHistoricalBars(const string& szInstrument, const string& szExchange, int datetime1, int datetime2, long barSize)
365+
int CHistoricalDataApi::ReqQryHistoricalBars(HistoricalDataRequestField* request)
365366
{
366367
// 传成API可识别的BarSize
367-
int period = 0;
368-
switch (barSize)
369-
{
370-
case 60:
371-
period = 1;
372-
break;
373-
case 60 * 5:
374-
period = 2;
375-
break;
376-
case 3600:
377-
period = 3;
378-
break;
379-
case 86400:
380-
period = 4;
381-
break;
382-
default:
383-
return -100;
384-
}
385-
return m_pApi->RequestHistory(szExchange.c_str(), szInstrument.c_str(), period);
368+
//int period = 0;
369+
//switch (barSize)
370+
//{
371+
//case 60:
372+
// period = 1;
373+
// break;
374+
//case 60 * 5:
375+
// period = 2;
376+
// break;
377+
//case 3600:
378+
// period = 3;
379+
// break;
380+
//case 86400:
381+
// period = 4;
382+
// break;
383+
//default:
384+
// return -100;
385+
//}
386+
//return m_pApi->RequestHistory(szExchange.c_str(), szInstrument.c_str(), period);
387+
return 0;
386388
}

0 commit comments

Comments
 (0)