From 951526c508424e8da3d873b86ac023f72b451000 Mon Sep 17 00:00:00 2001 From: Jhonathan Abreu Date: Fri, 23 Feb 2024 15:39:43 -0400 Subject: [PATCH] Minor improvements --- .../CoinbaseBrokerageHistoryProviderTests.cs | 39 ++++++++----------- .../CoinbaseDownloader.cs | 3 +- .../CoinbaseBrokerage.HistoryProvider.cs | 12 ++++-- 3 files changed, 26 insertions(+), 28 deletions(-) diff --git a/QuantConnect.CoinbaseBrokerage.Tests/CoinbaseBrokerageHistoryProviderTests.cs b/QuantConnect.CoinbaseBrokerage.Tests/CoinbaseBrokerageHistoryProviderTests.cs index 3b4a436..48bf68e 100644 --- a/QuantConnect.CoinbaseBrokerage.Tests/CoinbaseBrokerageHistoryProviderTests.cs +++ b/QuantConnect.CoinbaseBrokerage.Tests/CoinbaseBrokerageHistoryProviderTests.cs @@ -32,7 +32,7 @@ namespace QuantConnect.CoinbaseBrokerage.Tests public class CoinbaseBrokerageHistoryProviderTests { [Test, TestCaseSource(nameof(TestParameters))] - public void GetsHistory(Symbol symbol, Resolution resolution, TickType tickType, TimeSpan period, bool shouldBeEmpty, bool unsupported) + public void GetsHistory(Symbol symbol, Resolution resolution, TickType tickType, TimeSpan period, bool unsupported) { var brokerage = new CoinbaseBrokerage( Config.Get("coinbase-url", "wss://advanced-trade-ws.coinbase.com"), @@ -65,12 +65,7 @@ public void GetsHistory(Symbol symbol, Resolution resolution, TickType tickType, return; } - if (shouldBeEmpty) - { - Assert.IsEmpty(history); - return; - } - + Assert.IsNotNull(history); Assert.IsNotEmpty(history); foreach (var bar in history.Cast()) @@ -90,33 +85,33 @@ private static IEnumerable TestParameters var BTCUSDC = Symbol.Create("BTCUSDC", SecurityType.Crypto, Market.Coinbase); // valid parameters - yield return new TestCaseData(BTCUSD, Resolution.Minute, TickType.Trade, TimeSpan.FromDays(5), false, false); - yield return new TestCaseData(BTCUSD, Resolution.Minute, TickType.Trade, Time.OneHour, false, false); - yield return new TestCaseData(BTCUSD, Resolution.Hour, TickType.Trade, Time.OneDay, false, false); - yield return new TestCaseData(BTCUSD, Resolution.Daily, TickType.Trade, TimeSpan.FromDays(15), false, false); + yield return new TestCaseData(BTCUSD, Resolution.Minute, TickType.Trade, TimeSpan.FromDays(5), false); + yield return new TestCaseData(BTCUSD, Resolution.Minute, TickType.Trade, Time.OneHour, false); + yield return new TestCaseData(BTCUSD, Resolution.Hour, TickType.Trade, Time.OneDay, false); + yield return new TestCaseData(BTCUSD, Resolution.Daily, TickType.Trade, TimeSpan.FromDays(15), false); - yield return new TestCaseData(BTCUSDC, Resolution.Minute, TickType.Trade, Time.OneHour, false, false); - yield return new TestCaseData(BTCUSDC, Resolution.Hour, TickType.Trade, Time.OneDay, false, false); + yield return new TestCaseData(BTCUSDC, Resolution.Minute, TickType.Trade, Time.OneHour, false); + yield return new TestCaseData(BTCUSDC, Resolution.Hour, TickType.Trade, Time.OneDay, false); - // invalid period, no error, empty result - yield return new TestCaseData(BTCUSD, Resolution.Daily, TickType.Trade, TimeSpan.FromDays(-15), true, false); + // invalid period + yield return new TestCaseData(BTCUSD, Resolution.Daily, TickType.Trade, TimeSpan.FromDays(-15), true); // quote tick type, null result - yield return new TestCaseData(BTCUSD, Resolution.Daily, TickType.Quote, TimeSpan.FromDays(15), false, true); - yield return new TestCaseData(BTCUSD, Resolution.Daily, TickType.OpenInterest, TimeSpan.FromDays(15), false, true); + yield return new TestCaseData(BTCUSD, Resolution.Daily, TickType.Quote, TimeSpan.FromDays(15), true); + yield return new TestCaseData(BTCUSD, Resolution.Daily, TickType.OpenInterest, TimeSpan.FromDays(15), true); // invalid resolution, null result - yield return new TestCaseData(BTCUSD, Resolution.Tick, TickType.Trade, TimeSpan.FromSeconds(15), false, true); - yield return new TestCaseData(BTCUSD, Resolution.Second, TickType.Trade, Time.OneMinute, false, true); + yield return new TestCaseData(BTCUSD, Resolution.Tick, TickType.Trade, TimeSpan.FromSeconds(15), true); + yield return new TestCaseData(BTCUSD, Resolution.Second, TickType.Trade, Time.OneMinute, true); // invalid symbol, null result - yield return new TestCaseData(Symbol.Create("ABCXYZ", SecurityType.Crypto, Market.Coinbase), Resolution.Daily, TickType.Trade, TimeSpan.FromDays(15), false, true); + yield return new TestCaseData(Symbol.Create("ABCXYZ", SecurityType.Crypto, Market.Coinbase), Resolution.Daily, TickType.Trade, TimeSpan.FromDays(15), true); // invalid security type, null result - yield return new TestCaseData(Symbols.EURGBP, Resolution.Daily, TickType.Trade, TimeSpan.FromDays(15), false, true); + yield return new TestCaseData(Symbols.EURGBP, Resolution.Daily, TickType.Trade, TimeSpan.FromDays(15), true); // invalid market, null result - yield return new TestCaseData(Symbol.Create("BTCUSD", SecurityType.Crypto, Market.Binance), Resolution.Daily, TickType.Trade, TimeSpan.FromDays(15), false, true); + yield return new TestCaseData(Symbol.Create("BTCUSD", SecurityType.Crypto, Market.Binance), Resolution.Daily, TickType.Trade, TimeSpan.FromDays(15), true); } } } diff --git a/QuantConnect.CoinbaseBrokerage.ToolBox/CoinbaseDownloader.cs b/QuantConnect.CoinbaseBrokerage.ToolBox/CoinbaseDownloader.cs index 2f635b8..8deb24d 100644 --- a/QuantConnect.CoinbaseBrokerage.ToolBox/CoinbaseDownloader.cs +++ b/QuantConnect.CoinbaseBrokerage.ToolBox/CoinbaseDownloader.cs @@ -72,8 +72,7 @@ public IEnumerable Get(DataDownloaderGetParameters dataDownloaderGetPa tickType); var brokerage = CreateBrokerage(); - var data = brokerage.GetHistory(historyRequest); - return data; + return brokerage.GetHistory(historyRequest); } /// diff --git a/QuantConnect.CoinbaseBrokerage/CoinbaseBrokerage.HistoryProvider.cs b/QuantConnect.CoinbaseBrokerage/CoinbaseBrokerage.HistoryProvider.cs index 49b139c..b3a11ac 100644 --- a/QuantConnect.CoinbaseBrokerage/CoinbaseBrokerage.HistoryProvider.cs +++ b/QuantConnect.CoinbaseBrokerage/CoinbaseBrokerage.HistoryProvider.cs @@ -20,7 +20,6 @@ using QuantConnect.Data.Market; using System.Collections.Generic; using QuantConnect.CoinbaseBrokerage.Models.Enums; -using System.Linq; namespace QuantConnect.CoinbaseBrokerage { @@ -35,6 +34,7 @@ public partial class CoinbaseBrokerage private bool _loggedCoinbaseSupportsOnlyTradeBars; private bool _loggedUnsupportedAssetForHistory; private bool _loggedUnsupportedResolutionForHistory; + private bool _loggedInvalidTimeRangeForHistory; /// /// Gets the history for the requested security @@ -78,9 +78,13 @@ public override IEnumerable GetHistory(HistoryRequest request) if (request.StartTimeUtc >= request.EndTimeUtc) { - OnMessage(new BrokerageMessageEvent(BrokerageMessageType.Warning, "InvalidDateRange", - "The history request start date must precede the end date, no history returned")); - return Enumerable.Empty(); + if (!_loggedInvalidTimeRangeForHistory) + { + _loggedInvalidTimeRangeForHistory = true; + OnMessage(new BrokerageMessageEvent(BrokerageMessageType.Warning, "InvalidDateRange", + "The history request start date must precede the end date, no history returned")); + } + return null; } Log.Debug($"{nameof(CoinbaseBrokerage)}.{nameof(GetHistory)}: Submitting request: {request.Symbol.Value}: {request.Resolution} {request.StartTimeUtc} UTC -> {request.EndTimeUtc} UTC");