From 50d9ea25f45ef9f7e69448132dc1bb012072a70f Mon Sep 17 00:00:00 2001 From: Martin-Molinero Date: Wed, 1 Nov 2023 20:14:42 -0300 Subject: [PATCH] Revert "Bug websocket session refresh (#19)" (#20) This reverts commit 77dc3e9f651c92f21c81043b27aef5a80be63fa4. --- .github/workflows/gh-actions.yml | 12 +-- .../TradierBrokerageDataQueueHandlerTests.cs | 81 ------------------- .../TradierBrokerageTests.cs | 2 +- .../TradierBrokerage.DataQueueHandler.cs | 19 +---- .../TradierStreamSession.cs | 10 +++ 5 files changed, 15 insertions(+), 109 deletions(-) delete mode 100644 QuantConnect.TradierBrokerage.Tests/TradierBrokerageDataQueueHandlerTests.cs diff --git a/.github/workflows/gh-actions.yml b/.github/workflows/gh-actions.yml index fec53be..3d1d5a0 100644 --- a/.github/workflows/gh-actions.yml +++ b/.github/workflows/gh-actions.yml @@ -14,16 +14,10 @@ jobs: QC_JOB_USER_ID: ${{ secrets.JOB_USER_ID }} QC_API_ACCESS_TOKEN: ${{ secrets.API_ACCESS_TOKEN }} QC_JOB_ORGANIZATION_ID: ${{ secrets.JOB_ORGANIZATION_ID }} + container: + image: quantconnect/lean:foundation steps: - - name: Checkout - uses: actions/checkout@v2 - - name: Free space - run: df -h && rm -rf /opt/hostedtoolcache* && df -h - - - uses: addnab/docker-run-action@v3 - with: - image: quantconnect/lean:foundation - options: --workdir /__w/Lean.Brokerages.Tradier/Lean.Brokerages.Tradier -v /home/runner/work:/__w -e QC_TRADIER_ENVIRONMENT=${{ secrets.QC_TRADIER_ENVIRONMENT }} -e QC_TRADIER_ACCESS_TOKEN=${{ secrets.QC_TRADIER_ACCESS_TOKEN }} -e QC_TRADIER_ACCOUNT_ID=${{ secrets.QC_TRADIER_ACCOUNT_ID }} -e QC_JOB_USER_ID=${{ secrets.JOB_USER_ID }} -e QC_API_ACCESS_TOKEN=${{ secrets.API_ACCESS_TOKEN }} -e QC_JOB_ORGANIZATION_ID=${{ secrets.JOB_ORGANIZATION_ID }} + - uses: actions/checkout@v2 - name: Checkout Lean Same Branch id: lean-same-branch diff --git a/QuantConnect.TradierBrokerage.Tests/TradierBrokerageDataQueueHandlerTests.cs b/QuantConnect.TradierBrokerage.Tests/TradierBrokerageDataQueueHandlerTests.cs deleted file mode 100644 index 7f5f510..0000000 --- a/QuantConnect.TradierBrokerage.Tests/TradierBrokerageDataQueueHandlerTests.cs +++ /dev/null @@ -1,81 +0,0 @@ -/* - * 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 NUnit.Framework; -using System.Threading; -using QuantConnect.Data; -using QuantConnect.Logging; -using QuantConnect.Data.Market; -using QuantConnect.Brokerages.Tradier; - -namespace QuantConnect.Tests.Brokerages.Tradier -{ - [TestFixture] - public partial class TradierBrokerageTests : BrokerageTests - { - private static TestCaseData[] TestParameters - { - get - { - return new[] - { - // valid parameters, for example - new TestCaseData(Symbols.AAPL, Resolution.Tick, false), - }; - } - } - - [Test, TestCaseSource(nameof(TestParameters)), Explicit("Long execution time")] - public void StreamsData(Symbol symbol, Resolution resolution, bool throwsException) - { - var cancelationToken = new CancellationTokenSource(); - - var tradier = (TradierBrokerage)Brokerage; - SubscriptionDataConfig[] configs; - if (resolution == Resolution.Tick) - { - var tradeConfig = new SubscriptionDataConfig(GetSubscriptionDataConfig(symbol, resolution), tickType: TickType.Trade); - var quoteConfig = new SubscriptionDataConfig(GetSubscriptionDataConfig(symbol, resolution), tickType: TickType.Quote); - configs = new[] { tradeConfig, quoteConfig }; - } - else - { - configs = new[] { GetSubscriptionDataConfig(symbol, resolution), - GetSubscriptionDataConfig(symbol, resolution) }; - } - - foreach (var config in configs) - { - ProcessFeed(tradier.Subscribe(config, (s, e) => { }), - cancelationToken, - (baseData) => { - if (baseData != null) { Log.Trace($"{baseData}"); } - }); - } - - // long runtime so we assert the session refresh and data stream is not interrupted - Thread.Sleep(1000 * 15 * 60); - - foreach (var config in configs) - { - tradier.Unsubscribe(config); - } - - Thread.Sleep(1000); - - cancelationToken.Cancel(); - } - } -} diff --git a/QuantConnect.TradierBrokerage.Tests/TradierBrokerageTests.cs b/QuantConnect.TradierBrokerage.Tests/TradierBrokerageTests.cs index ccbfe75..c2c6082 100644 --- a/QuantConnect.TradierBrokerage.Tests/TradierBrokerageTests.cs +++ b/QuantConnect.TradierBrokerage.Tests/TradierBrokerageTests.cs @@ -29,7 +29,7 @@ namespace QuantConnect.Tests.Brokerages.Tradier { - public partial class TradierBrokerageTests : BrokerageTests + public class TradierBrokerageTests : BrokerageTests { /// /// Provides the data required to test each order type in various cases diff --git a/QuantConnect.TradierBrokerage/TradierBrokerage.DataQueueHandler.cs b/QuantConnect.TradierBrokerage/TradierBrokerage.DataQueueHandler.cs index 29b886c..ecb53ee 100644 --- a/QuantConnect.TradierBrokerage/TradierBrokerage.DataQueueHandler.cs +++ b/QuantConnect.TradierBrokerage/TradierBrokerage.DataQueueHandler.cs @@ -27,7 +27,6 @@ using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; -using System.Threading.Tasks; namespace QuantConnect.Brokerages.Tradier { @@ -38,9 +37,6 @@ public partial class TradierBrokerage { #region IDataQueueHandler implementation - private readonly static TimeSpan SessionTimeout = TimeSpan.FromMinutes(4.8); - private bool _sessionExpired; - private const string WebSocketUrl = "wss://ws.tradier.com/v1/markets/events"; private object _streamSessionLock = new (); @@ -270,23 +266,10 @@ private TradierStreamSession GetStreamSession() { lock (_streamSessionLock) { - if (_streamSession == null || _sessionExpired) + if (_streamSession == null || !_streamSession.IsValid) { var request = new RestRequest("markets/events/session", Method.POST); _streamSession = Execute(request, TradierApiRequestType.Data, "stream"); - - // clear expired state, we just refreshed it - _sessionExpired = false; - - // after timeout, expire the session and refresh subscriptions, which will refresh token - Task.Delay(SessionTimeout).ContinueWith(_ => - { - lock (_streamSessionLock) - { - _sessionExpired = true; - _subscribeProcedure.Set(); - } - }); } return _streamSession; diff --git a/QuantConnect.TradierBrokerage/TradierStreamSession.cs b/QuantConnect.TradierBrokerage/TradierStreamSession.cs index 042cc8e..bfb220d 100644 --- a/QuantConnect.TradierBrokerage/TradierStreamSession.cs +++ b/QuantConnect.TradierBrokerage/TradierStreamSession.cs @@ -14,6 +14,8 @@ * */ +using System; + namespace QuantConnect.Brokerages.Tradier { /// @@ -21,6 +23,9 @@ namespace QuantConnect.Brokerages.Tradier /// public class TradierStreamSession { + private readonly static TimeSpan LifeSpan = TimeSpan.FromMinutes(4.9); + private readonly DateTime _createdTime = DateTime.UtcNow; + /// /// Trading Stream: Session Id /// @@ -30,5 +35,10 @@ public class TradierStreamSession /// Trading Stream: Stream URL /// public string Url; + + /// + /// Determines if this session Id is valid + /// + public bool IsValid => DateTime.UtcNow - _createdTime < LifeSpan; } }