forked from MeowBot/OpenQuant.API
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataManager.cs
More file actions
302 lines (302 loc) · 11.7 KB
/
Copy pathDataManager.cs
File metadata and controls
302 lines (302 loc) · 11.7 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
using OpenQuant.API.Compression;
using OpenQuant.Config;
using OpenQuant.ObjectMap;
using SmartQuant.Data;
using SmartQuant.Instruments;
using SmartQuant.Providers;
using SmartQuant.Series;
using System;
using System.Collections.Generic;
namespace OpenQuant.API
{
public class DataManager
{
public static BarSeries GetHistoricalBars(Instrument instrument, DateTime begin, DateTime end, BarType barType, long barSize)
{
SmartQuant.Instruments.Instrument instrument2 = Map.OQ_SQ_Instrument[instrument] as SmartQuant.Instruments.Instrument;
if (barSize == 86400L)
{
return new BarSeries(SmartQuant.Instruments.DataManager.GetDailySeries(instrument2, begin, end));
}
return new BarSeries(SmartQuant.Instruments.DataManager.GetBarSeries(instrument2, begin, end, EnumConverter.Convert(barType), barSize));
}
public static BarSeries GetHistoricalBars(Instrument instrument, BarType barType, long barSize)
{
return DataManager.GetHistoricalBars(instrument, DateTime.MinValue, DateTime.MaxValue, barType, barSize);
}
public static BarSeries GetHistoricalBars(Instrument instrument)
{
return DataManager.GetHistoricalBars(instrument, DateTime.MinValue, DateTime.MaxValue);
}
public static BarSeries GetHistoricalBars(Instrument instrument, DateTime begin, DateTime end)
{
SmartQuant.Instruments.Instrument instrument2 = Map.OQ_SQ_Instrument[instrument] as SmartQuant.Instruments.Instrument;
return new BarSeries(SmartQuant.Instruments.DataManager.GetBarSeries(instrument2, begin, end));
}
public static BarSeries GetHistoricalBars(string provider, Instrument instrument, DateTime begin, DateTime end, int size)
{
BarSeries barSeries = new BarSeries();
if (SmartQuant.Providers.ProviderManager.HistoricalDataProviders.Contains(provider))
{
IHistoricalDataProvider provider2 = SmartQuant.Providers.ProviderManager.HistoricalDataProviders[provider];
SmartQuant.Series.BarSeries barSeries2;
if (size == 86400)
{
barSeries2 = SmartQuant.Instruments.DataManager.GetHistoricalDailies(provider2, instrument.instrument, begin, end);
}
else
{
barSeries2 = SmartQuant.Instruments.DataManager.GetHistoricalBars(provider2, instrument.instrument, begin, end, (long)size);
}
foreach (SmartQuant.Data.Bar bar in barSeries2)
{
barSeries.series.Add(bar);
}
}
return barSeries;
}
public static TradeSeries GetHistoricalTrades(string provider, Instrument instrument, DateTime begin, DateTime end)
{
TradeSeries tradeSeries = new TradeSeries();
if (SmartQuant.Providers.ProviderManager.HistoricalDataProviders.Contains(provider))
{
TradeArray historicalTrades = SmartQuant.Instruments.DataManager.GetHistoricalTrades(SmartQuant.Providers.ProviderManager.HistoricalDataProviders[provider], instrument.instrument, begin, end);
foreach (SmartQuant.Data.Trade obj in historicalTrades)
{
tradeSeries.series.Add(obj);
}
}
return tradeSeries;
}
public static QuoteSeries GetHistoricalQuotes(string provider, Instrument instrument, DateTime begin, DateTime end)
{
QuoteSeries quoteSeries = new QuoteSeries();
if (SmartQuant.Providers.ProviderManager.HistoricalDataProviders.Contains(provider))
{
QuoteArray historicalQuotes = SmartQuant.Instruments.DataManager.GetHistoricalQuotes(SmartQuant.Providers.ProviderManager.HistoricalDataProviders[provider], instrument.instrument, begin, end);
foreach (SmartQuant.Data.Quote obj in historicalQuotes)
{
quoteSeries.series.Add(obj);
}
}
return quoteSeries;
}
public static QuoteSeries GetHistoricalQuotes(Instrument instrument, DateTime begin, DateTime end)
{
SmartQuant.Instruments.Instrument instrument2 = Map.OQ_SQ_Instrument[instrument] as SmartQuant.Instruments.Instrument;
return new QuoteSeries(SmartQuant.Instruments.DataManager.GetQuoteArray(instrument2, begin, end));
}
public static TradeSeries GetHistoricalTrades(Instrument instrument, DateTime begin, DateTime end)
{
SmartQuant.Instruments.Instrument instrument2 = Map.OQ_SQ_Instrument[instrument] as SmartQuant.Instruments.Instrument;
return new TradeSeries(SmartQuant.Instruments.DataManager.GetTradeArray(instrument2, begin, end));
}
private static bool SeriesNameToBarTypeSize(string name, out BarType barType, out long barSize)
{
barType = BarType.Range;
barSize = -1L;
string[] array = name.Split(new char[]
{
'.'
});
if (array.Length >= 4 && array[array.Length - 3] == "Bar" && Enum.IsDefined(typeof(BarType), array[array.Length - 2]))
{
barType = (BarType)Enum.Parse(typeof(BarType), array[array.Length - 2]);
if (long.TryParse(array[array.Length - 1], out barSize))
{
return true;
}
}
return false;
}
public static BarSeriesInfo[] GetBarSeriesInfoList(Instrument instrument)
{
List<BarSeriesInfo> list = new List<BarSeriesInfo>();
foreach (IDataSeries dataSeries in SmartQuant.Instruments.DataManager.GetDataSeries(instrument.instrument))
{
BarType barType;
long barSize;
if (DataManager.SeriesNameToBarTypeSize(dataSeries.Name, out barType, out barSize))
{
list.Add(new BarSeriesInfo(barType, barSize));
}
}
return list.ToArray();
}
public static void Add(Instrument instrument, Bar bar)
{
SmartQuant.Instruments.Instrument instrument2 = Map.OQ_SQ_Instrument[instrument] as SmartQuant.Instruments.Instrument;
if (bar.bar.BarType == SmartQuant.Data.BarType.Time && bar.bar.Size == 86400L)
{
Daily daily = new Daily(bar.bar.DateTime, bar.bar.Open, bar.bar.High, bar.bar.Low, bar.bar.Close, bar.bar.Volume, bar.bar.OpenInt);
SmartQuant.Instruments.DataManager.Add(instrument2, daily);
return;
}
SmartQuant.Instruments.DataManager.Add(instrument2, bar.bar);
}
public static void Add(Instrument instrument, DateTime datetime, double open, double high, double low, double close, long volume, long size)
{
DataManager.Add(instrument, new Bar(new SmartQuant.Data.Bar(datetime, open, high, low, close, volume, size)));
}
public static void Add(Instrument instrument, Trade trade)
{
SmartQuant.Instruments.Instrument instrument2 = Map.OQ_SQ_Instrument[instrument] as SmartQuant.Instruments.Instrument;
SmartQuant.Instruments.DataManager.Add(instrument2, trade.trade);
}
public static void Add(Instrument instrument, DateTime datetime, double price, int size)
{
DataManager.Add(instrument, new Trade(new SmartQuant.Data.Trade(datetime, price, size)));
}
public static void Add(Instrument instrument, Quote quote)
{
SmartQuant.Instruments.Instrument instrument2 = Map.OQ_SQ_Instrument[instrument] as SmartQuant.Instruments.Instrument;
SmartQuant.Instruments.DataManager.Add(instrument2, quote.quote);
}
public static void Add(Instrument instrument, DateTime datetime, double bid, int bidsize, double ask, int asksize)
{
DataManager.Add(instrument, new Quote(new SmartQuant.Data.Quote(datetime, bid, bidsize, ask, asksize)));
}
public static void Add(Instrument instrument, OrderBookUpdate update)
{
SmartQuant.Instruments.Instrument instrument2 = Map.OQ_SQ_Instrument[instrument] as SmartQuant.Instruments.Instrument;
SmartQuant.Instruments.DataManager.Add(instrument2, update.marketDepth);
}
public static void Add(Instrument instrument, DateTime datetime, BidAsk side, OrderBookAction action, int position, double price, int size)
{
DataManager.Add(instrument, new OrderBookUpdate(new MarketDepth(datetime, string.Empty, position, EnumConverter.Convert(action), EnumConverter.Convert(side), price, size)));
}
public static void DeleteTradeSeries(Instrument instrument)
{
DataManager.DeleteDataSeries(instrument, new string[]
{
"Trade"
});
}
public static void DeleteQuoteSeries(Instrument instrument)
{
DataManager.DeleteDataSeries(instrument, new string[]
{
"Quote"
});
}
public static void DeleteBarSeries(Instrument instrument, BarType barType, long barSize)
{
DataManager.DeleteDataSeries(instrument, new string[]
{
"Bar",
barType.ToString(),
barSize.ToString()
});
}
public static void DeleteDailySeries(Instrument instrument)
{
DataManager.DeleteDataSeries(instrument, new string[]
{
"Daily"
});
}
private static void DeleteDataSeries(Instrument instrument, params string[] items)
{
string series = string.Format("{0}{1}{2}", instrument.Symbol, '.', string.Join('.'.ToString(), items));
SmartQuant.Instruments.DataManager.DeleteDataSeries(series);
}
public static void DeleteTrade(Instrument instrument, DateTime datetime)
{
DataManager.DeleteDataObject(instrument, datetime, new string[]
{
"Trade"
});
}
public static void DeleteQuote(Instrument instrument, DateTime datetime)
{
DataManager.DeleteDataObject(instrument, datetime, new string[]
{
"Quote"
});
}
public static void DeleteBar(Instrument instrument, DateTime datetime, BarType barType, long barSize)
{
DataManager.DeleteDataObject(instrument, datetime, new string[]
{
"Bar",
barType.ToString(),
barSize.ToString()
});
}
public static void DeleteDaily(Instrument instrument, DateTime date)
{
DataManager.DeleteDataObject(instrument, date.Date, new string[]
{
"Daily"
});
}
private static void DeleteDataObject(Instrument instrument, DateTime datetime, params string[] items)
{
string series = string.Format("{0}{1}{2}", instrument.Symbol, '.', string.Join('.'.ToString(), items));
IDataSeries dataSeries = SmartQuant.Instruments.DataManager.Server.GetDataSeries(series);
if (dataSeries != null)
{
dataSeries.Remove(datetime);
}
}
public static BrokerInfo GetBrokerInfo(string provider, byte route)
{
IExecutionProvider executionProvider = SmartQuant.Providers.ProviderManager.ExecutionProviders[provider];
if (executionProvider == null)
{
throw new ArgumentException(string.Format("Provider {0} does not exist.", provider));
}
if (!executionProvider.IsConnected)
{
throw new ApplicationException(string.Format("Provider {0} is not connected.", provider));
}
if (route != 0 && executionProvider is IMultiRouteExecutionProvider)
{
return new BrokerInfo((executionProvider as IMultiRouteExecutionProvider).GetBrokerInfo(route));
}
return new BrokerInfo(executionProvider.GetBrokerInfo());
}
public static BrokerInfo GetBrokerInfo(string provider)
{
return DataManager.GetBrokerInfo(provider, 0);
}
public static BrokerInfo GetBrokerInfo()
{
return DataManager.GetBrokerInfo(Configuration.Active.ExecutionProvider.Name);
}
public static BarSeries CompressBars(TradeSeries trades, BarType barType, long barSize)
{
return DataManager.CompressBars(new TradeDataEnumerator(trades), barType, 1L, barSize);
}
public static BarSeries CompressBars(QuoteSeries quotes, QuoteData input, BarType barType, long barSize)
{
if (barType == BarType.Range && input == QuoteData.BidAsk)
{
throw new ArgumentException(string.Format("Cannot make range bars from {0}", input));
}
return DataManager.CompressBars(new QuoteDataEnumerator(quotes, input), barType, 1L, barSize);
}
public static BarSeries CompressBars(BarSeries bars, long barSize)
{
if (bars.Count == 0)
{
return new BarSeries();
}
Bar bar = bars[0];
if (bar.Type == BarType.Range)
{
throw new ArgumentException("Cannot compress bars with type Range");
}
return DataManager.CompressBars(new BarDataEnumerator(bars), bar.Type, bar.Size, barSize);
}
private static BarSeries CompressBars(DataEntryEnumerator enumerator, BarType barType, long oldBarSize, long newBarSize)
{
BarCompressor compressor = BarCompressor.GetCompressor(barType, oldBarSize, newBarSize);
return compressor.Compress(enumerator);
}
public static void Flush()
{
SmartQuant.Instruments.DataManager.Server.Flush();
}
}
}