-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix: several deprecate href * feat: use max available date by subs * feat: request parameters const names * feat: Execute History Request Parallel * refactor: synchronize request in API class * feat: develop StopWatch Wrapper * remove: RestClient huge Timeout value * fix: GenerateDateRangesWithInterval extension * refactor: ExecuteRequestParallelAsync test:feat: GetHistoryRequestWithLongRange * refactor: ExecuteRequestParallelAsync * refactor: ExecuteRequestParallelAsync if scope * Revert "feat: use max available date by subs" This reverts commit a1819dc. * refactor: use Parallel.ForEachAsync instead of semaphore and task to get history data * remove: NullDisposable * feat: use max available date by subs * feat: request retry if failed in ApiClient * fix: GenerateDateRangesWithInterval refactor: ExecuteRequest * feat: filter of HistoryRequest with ExtendedMarketHours test:feat: amount of bars with different resolution * fix: use Utc time for tick requests remove: extra logs * feat: validate history request on null * remove: not used lib
- Loading branch information
Showing
10 changed files
with
547 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals. | ||
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 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 NUnit.Framework; | ||
using System.Collections.Generic; | ||
|
||
namespace QuantConnect.Lean.DataSource.ThetaData.Tests; | ||
|
||
[TestFixture] | ||
public class ThetaDataAdditionalTests | ||
{ | ||
[Test] | ||
public void GenerateDateRangesWithNinetyDaysInterval() | ||
{ | ||
var intervalDays = 90; | ||
var startDate = new DateTime(2020, 07, 18); | ||
var endDate = new DateTime(2021, 01, 14); | ||
|
||
var expectedRanges = new List<(DateTime startDate, DateTime endDate)> | ||
{ | ||
(new DateTime(2020, 07, 18), new DateTime(2020, 10, 16)), | ||
(new DateTime(2020, 10, 17), new DateTime(2021, 01, 14)), | ||
}; | ||
|
||
var actualRanges = new List<(DateTime startDate, DateTime endDate)>(ThetaDataExtensions.GenerateDateRangesWithInterval(startDate, endDate, intervalDays)); | ||
|
||
Assert.AreEqual(expectedRanges.Count, actualRanges.Count, "The number of ranges should match."); | ||
|
||
for (int i = 0; i < expectedRanges.Count; i++) | ||
{ | ||
Assert.AreEqual(expectedRanges[i].startDate, actualRanges[i].startDate, $"Start date mismatch at index {i}"); | ||
Assert.AreEqual(expectedRanges[i].endDate, actualRanges[i].endDate, $"End date mismatch at index {i}"); | ||
} | ||
} | ||
|
||
[Test] | ||
public void GenerateDateRangesWithOneDayInterval() | ||
{ | ||
var intervalDays = 1; | ||
|
||
var startDate = new DateTime(2024, 07, 26); | ||
var endDate = new DateTime(2024, 07, 30); | ||
|
||
var expectedRanges = new List<(DateTime startDate, DateTime endDate)> | ||
{ | ||
(new DateTime(2024, 07, 26), new DateTime(2024, 07, 27)), | ||
(new DateTime(2024, 07, 28), new DateTime(2024, 07, 29)), | ||
(new DateTime(2024, 07, 30), new DateTime(2024, 07, 30)) | ||
}; | ||
|
||
var actualRanges = new List<(DateTime startDate, DateTime endDate)>(ThetaDataExtensions.GenerateDateRangesWithInterval(startDate, endDate, intervalDays)); | ||
|
||
Assert.AreEqual(expectedRanges.Count, actualRanges.Count, "The number of ranges should match."); | ||
|
||
for (int i = 0; i < expectedRanges.Count; i++) | ||
{ | ||
Assert.AreEqual(expectedRanges[i].startDate, actualRanges[i].startDate, $"Start date mismatch at index {i}"); | ||
Assert.AreEqual(expectedRanges[i].endDate, actualRanges[i].endDate, $"End date mismatch at index {i}"); | ||
} | ||
} | ||
|
||
[Test] | ||
public void GenerateDateRangesWithInterval_ShouldHandleSameStartEndDate() | ||
{ | ||
DateTime startDate = new DateTime(2025, 2, 1); | ||
DateTime endDate = new DateTime(2025, 2, 1); | ||
|
||
var ranges = new List<(DateTime startDate, DateTime endDate)>( | ||
ThetaDataExtensions.GenerateDateRangesWithInterval(startDate, endDate, 1) | ||
); | ||
|
||
Assert.AreEqual(1, ranges.Count, "There should be no date ranges generated."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals. | ||
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 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. | ||
*/ | ||
|
||
namespace QuantConnect.Lean.DataSource.ThetaData.Models.Rest; | ||
|
||
/// <summary> | ||
/// Contains constant values for various request parameters used in API queries. | ||
/// </summary> | ||
public static class RequestParameters | ||
{ | ||
/// <summary> | ||
/// Represents the time interval in milliseconds since midnight Eastern Time (ET). | ||
/// Example values: | ||
/// - 09:30:00 ET = 34_200_000 ms | ||
/// - 16:00:00 ET = 57_600_000 ms | ||
/// </summary> | ||
public const string IntervalInMilliseconds = "ivl"; | ||
|
||
/// <summary> | ||
/// Represents the start date for a query or request. | ||
/// </summary> | ||
public const string StartDate = "start_date"; | ||
|
||
/// <summary> | ||
/// Represents the end date for a query or request. | ||
/// </summary> | ||
public const string EndDate = "end_date"; | ||
} |
67 changes: 67 additions & 0 deletions
67
QuantConnect.ThetaData/Models/Wrappers/StopwatchWrapper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals. | ||
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 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.Diagnostics; | ||
using QuantConnect.Logging; | ||
using QuantConnect.Lean.DataSource.ThetaData.Models.Common; | ||
|
||
namespace QuantConnect.Lean.DataSource.ThetaData.Models.Wrappers; | ||
|
||
/// <summary> | ||
/// A utility class that conditionally starts a stopwatch for measuring execution time | ||
/// when debugging is enabled. Implements <see cref="IDisposable"/> to ensure | ||
/// automatic logging upon completion. | ||
/// </summary> | ||
public class StopwatchWrapper : IDisposable | ||
{ | ||
private readonly Stopwatch? _stopwatch; | ||
private readonly string _message; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="StopwatchWrapper"/> class | ||
/// and starts a stopwatch to measure execution time. | ||
/// </summary> | ||
/// <param name="message">A descriptive message to include in the log output.</param> | ||
private StopwatchWrapper(string message) | ||
{ | ||
_message = message; | ||
_stopwatch = Stopwatch.StartNew(); | ||
} | ||
|
||
/// <summary> | ||
/// Starts a stopwatch if debugging is enabled and returns an appropriate disposable instance. | ||
/// </summary> | ||
/// <param name="message">A descriptive message to include in the log output.</param> | ||
/// <returns> | ||
/// A <see cref="StopwatchWrapper"/> instance if debugging is enabled, | ||
/// otherwise a no-op <see cref="NullDisposable"/> instance. | ||
/// </returns> | ||
public static IDisposable? StartIfEnabled(string message) | ||
{ | ||
return Log.DebuggingEnabled ? new StopwatchWrapper(message) : null; | ||
} | ||
|
||
/// <summary> | ||
/// Stops the stopwatch and logs the elapsed time if debugging is enabled. | ||
/// </summary> | ||
public void Dispose() | ||
{ | ||
if (_stopwatch != null) | ||
{ | ||
_stopwatch.Stop(); | ||
Log.Debug($"{_message} completed in {_stopwatch.ElapsedMilliseconds} ms"); | ||
} | ||
} | ||
} |
Oops, something went wrong.