forked from QuantConnect/Lean.DataSource.DataBento
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataBentoHistoryProviderTests.cs
More file actions
137 lines (120 loc) · 4.98 KB
/
DataBentoHistoryProviderTests.cs
File metadata and controls
137 lines (120 loc) · 4.98 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
/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2026 QuantConnect Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
using System;
using NodaTime;
using NUnit.Framework;
using QuantConnect.Util;
using QuantConnect.Data;
using QuantConnect.Logging;
using QuantConnect.Securities;
using QuantConnect.Data.Market;
using System.Collections.Generic;
using QuantConnect.Configuration;
namespace QuantConnect.Lean.DataSource.DataBento.Tests;
[TestFixture]
public class DataBentoHistoryProviderTests
{
private DataBentoDataProvider _historyDataProvider;
[OneTimeSetUp]
public void OneTimeSetUp()
{
var apiKey = Config.Get("databento-api-key");
if (string.IsNullOrEmpty(apiKey))
{
Assert.Fail("DataBento API key is not set. Please set 'databento-api-key' in the configuration to run these tests.");
}
_historyDataProvider = new DataBentoDataProvider(apiKey);
}
[OneTimeTearDown]
public void OneTimeTearDown()
{
_historyDataProvider?.Dispose();
}
private static IEnumerable<TestCaseData> HistoryTestParameters
{
get
{
var sp500EMiniMarch = Symbol.CreateFuture(Futures.Indices.SP500EMini, Market.CME, new(2026, 03, 20));
// New York DateTimeZone
yield return new TestCaseData(sp500EMiniMarch, Resolution.Hour, TickType.Quote, new DateTime(2026, 1, 23, 9, 0, 0), new DateTime(2026, 1, 27, 16, 0, 0), false).SetArgDisplayNames($"{sp500EMiniMarch.Value}|Quote|Hour|2026/01/23 9:00 - 2026/01/27 16:00");
yield return new TestCaseData(sp500EMiniMarch, Resolution.Daily, TickType.Quote, new DateTime(2026, 1, 23), new DateTime(2026, 1, 27), false).SetArgDisplayNames($"{sp500EMiniMarch.Value}|Quote|Daily|2026/01/23 - 2026/01/27");
yield return new TestCaseData(sp500EMiniMarch, Resolution.Daily, TickType.OpenInterest, new DateTime(2026, 1, 23), new DateTime(2026, 1, 27), false).SetArgDisplayNames($"{sp500EMiniMarch.Value}|OpenInterest|2026/01/26 - 2026/01/26");
}
}
[TestCaseSource(nameof(HistoryTestParameters))]
public void GetsHistory(Symbol symbol, Resolution resolution, TickType tickType, DateTime startDateTime, DateTime endDateTime, bool isNullResult)
{
var historyRequest = CreateHistoryRequest(symbol, resolution, tickType, startDateTime, endDateTime);
var history = _historyDataProvider.GetHistory(historyRequest);
if (isNullResult)
{
Assert.IsNull(history);
}
else
{
Assert.IsNotNull(history);
Assert.IsNotEmpty(history);
var index = 0;
var previousTime = DateTime.MinValue;
foreach (var bar in history)
{
LogBaseData(index, bar);
Assert.IsNotNull(bar);
Assert.IsTrue(bar.Time > previousTime, $"Bar at {bar.Time:o} is not after previous bar at {previousTime:o}");
previousTime = bar.Time;
index += 1;
}
}
}
private void LogBaseData(int index, BaseData data)
{
switch (data)
{
case OpenInterest oi:
Log.Trace($"#{index} DataType: {oi.DataType} {oi.TickType} | " + oi.ToString() + $" Time: {oi.Time}, EndTime: {oi.EndTime}, FF: {oi.IsFillForward}");
break;
case QuoteBar qb:
Log.Trace($"#{index} Data Type: {qb.DataType} | " + qb.ToString() + $" Time: {qb.Time}, EndTime: {qb.EndTime}, FF: {qb.IsFillForward}");
break;
}
}
private static HistoryRequest CreateHistoryRequest(Symbol symbol, Resolution resolution, TickType tickType, DateTime startDateTime, DateTime endDateTime, SecurityExchangeHours exchangeHours = null, DateTimeZone dataTimeZone = null)
{
if (exchangeHours == null)
{
exchangeHours = SecurityExchangeHours.AlwaysOpen(TimeZones.NewYork);
}
if (dataTimeZone == null)
{
dataTimeZone = TimeZones.NewYork;
}
var dataType = LeanData.GetDataType(resolution, tickType);
return new HistoryRequest(
startDateTime,
endDateTime,
dataType,
symbol,
resolution,
exchangeHours,
dataTimeZone,
null,
true,
false,
DataNormalizationMode.Adjusted,
tickType
);
}
}