Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Reset() method #8410

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Common/Data/Consolidators/BaseTimelessConsolidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,11 @@ public virtual void Dispose()
DataConsolidatedHandler = null;
}

/// <summary>
/// Resets the consolidator
/// </summary>
public abstract void Reset();

/// <summary>
/// Scans this consolidator to see if it should emit a bar due to time passing
/// </summary>
Expand Down
10 changes: 10 additions & 0 deletions Common/Data/Consolidators/ClassicRenkoConsolidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,16 @@ public ClassicRenkoConsolidator(decimal barSize,
_evenBars = evenBars;
}

/// <summary>
/// Resets the ClassicRenkoConsolidator
/// </summary>
public override void Reset()
{
_lastCloseValue = null;
CurrentBar = null;
Consolidated = null;
}

/// <summary>
/// Updates the current RangeBar being created with the given data.
/// Additionally, if it's the case, it consolidates the current RangeBar
Expand Down
9 changes: 8 additions & 1 deletion Common/Data/Consolidators/DataConsolidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void Update(IBaseData data)
/// </summary>
public IBaseData Consolidated
{
get; private set;
get; protected set;
Marinovsky marked this conversation as resolved.
Show resolved Hide resolved
}

/// <summary>
Expand Down Expand Up @@ -108,6 +108,13 @@ protected virtual void OnDataConsolidated(IBaseData consolidated)
Consolidated = consolidated;
}

/// <summary>
/// Resets the consolidator
/// </summary>
public virtual void Reset()
{
}
Marinovsky marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
/// <filterpriority>2</filterpriority>
public void Dispose()
Expand Down
5 changes: 5 additions & 0 deletions Common/Data/Consolidators/IDataConsolidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ public interface IDataConsolidator : IDisposable
/// <param name="currentLocalTime">The current time in the local time zone (same as <see cref="BaseData.Time"/>)</param>
void Scan(DateTime currentLocalTime);

/// <summary>
/// Resets the consolidator
/// </summary>
void Reset();

/// <summary>
/// Event handler that fires when a new piece of data is produced
/// </summary>
Expand Down
9 changes: 9 additions & 0 deletions Common/Data/Consolidators/IdentityDataConsolidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,14 @@ public override void Update(T data)
public override void Scan(DateTime currentLocalTime)
{
}

/// <summary>
/// Resets the consolidator
/// </summary>
public override void Reset()
{
_last = default(T);
Marinovsky marked this conversation as resolved.
Show resolved Hide resolved
Consolidated = null;
}
}
}
11 changes: 11 additions & 0 deletions Common/Data/Consolidators/MarketHourAwareConsolidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,17 @@ public void Dispose()
Consolidator.Dispose();
}

/// <summary>
/// Resets the consolidator
/// </summary>
public void Reset()
{
_useStrictEndTime = false;
ExchangeHours = null;
DataTimeZone = null;
Consolidator.Reset();
}

/// <summary>
/// Perform late initialization based on the datas symbol
/// </summary>
Expand Down
14 changes: 14 additions & 0 deletions Common/Data/Consolidators/PeriodCountConsolidatorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,20 @@ public override void Scan(DateTime currentLocalTime)
}
}

/// <summary>
/// Resets the consolidator
/// </summary>
public override void Reset()
{
_securityIdentifier = null;
_securityIdentifierIsSet = false;
_currentCount = 0;
_workingBar = null;
_lastEmit = null;
_validateTimeSpan = false;
Consolidated = null;
}

/// <summary>
/// Returns true if this consolidator is time-based, false otherwise
/// </summary>
Expand Down
12 changes: 12 additions & 0 deletions Common/Data/Consolidators/RangeConsolidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,18 @@ public RangeConsolidator(int range,
_firstTick = true;
}

/// <summary>
/// Resets the consolidator
/// </summary>
public override void Reset()
{
CurrentBar = null;
Consolidated = null;
_firstTick = true;
_minimumPriceVariation = 0m;
RangeSize = 0m;
}

/// <summary>
/// Updates the current RangeBar being created with the given data.
/// Additionally, if it's the case, it consolidates the current RangeBar
Expand Down
17 changes: 17 additions & 0 deletions Common/Data/Consolidators/RenkoConsolidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,23 @@ public void Dispose()
_dataConsolidatedHandler = null;
}

/// <summary>
/// Resets the consolidator
/// </summary>
public void Reset()
{
_firstTick = true;
_lastWicko = null;
_currentBar = null;
_consolidated = null;
CloseOn = default;
CloseRate = default;
HighRate = default;
LowRate = default;
OpenOn = default;
OpenRate = default;
}

/// <summary>
/// Event invocator for the DataConsolidated event. This should be invoked
/// by derived classes when they have consolidated a new piece of data.
Expand Down
9 changes: 9 additions & 0 deletions Common/Data/Consolidators/SequentialConsolidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,5 +141,14 @@ public void Dispose()
Second.Dispose();
DataConsolidated = null;
}

/// <summary>
/// Resets the consolidator
/// </summary>
public void Reset()
{
First.Reset();
Second.Reset();
}
}
}
9 changes: 9 additions & 0 deletions Common/Data/Consolidators/VolumeRenkoConsolidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,15 @@ public override void Scan(DateTime currentLocalTime)
{
}

/// <summary>
/// Resets the consolidator
/// </summary>
public override void Reset()
{
_currentBar = null;
Consolidated = null;
}

/// <summary>
/// Event invocator for the DataConsolidated event. This should be invoked
/// by derived classes when they have consolidated a new piece of data.
Expand Down
8 changes: 8 additions & 0 deletions Common/Python/DataConsolidatorPythonWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,5 +109,13 @@ public void Update(IBaseData data)
public void Dispose()
{
}

/// <summary>
/// Resets the consolidator
/// </summary>
public void Reset()
{
InvokeMethod(nameof(Reset));
}
}
}
7 changes: 7 additions & 0 deletions Common/Python/PythonConsolidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,12 @@ public void OnDataConsolidated(PyObject consolidator, IBaseData data)
{
DataConsolidated?.Invoke(consolidator, data);
}

/// <summary>
/// Resets the consolidator
/// </summary>
public virtual void Reset()
{
}
}
}
98 changes: 98 additions & 0 deletions Tests/Common/Data/BaseConsolidatorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* 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 System.Collections.Generic;
using Microsoft.FSharp.Core;
using NUnit.Framework;
using Python.Runtime;
using QuantConnect.Data;
using QuantConnect.Data.Consolidators;
using QuantConnect.Data.Market;
using QuantConnect.Indicators;

namespace QuantConnect.Tests.Common.Data
{
[TestFixture]
public abstract class BaseConsolidatorTests
{
protected abstract IDataConsolidator CreateConsolidator();

protected virtual void AssertConsolidator(IDataConsolidator consolidator, IDataConsolidator previousConsolidator = null)
{
if (previousConsolidator == null)
{
Assert.IsNull(consolidator.Consolidated);
}
else
{
Assert.AreEqual(previousConsolidator.Consolidated?.Value, consolidator.Consolidated?.Value);
}
}

protected virtual dynamic GetTestValues()
Marinovsky marked this conversation as resolved.
Show resolved Hide resolved
{
var time = new DateTime(2016, 1, 1);
return new List<IndicatorDataPoint>()
{
new IndicatorDataPoint(time, 1.38687m),
new IndicatorDataPoint(time.AddSeconds(1), 1.38687m),
new IndicatorDataPoint(time.AddSeconds(2), 1.38688m),
new IndicatorDataPoint(time.AddSeconds(3), 1.38687m),
new IndicatorDataPoint(time.AddSeconds(4), 1.38686m),
new IndicatorDataPoint(time.AddSeconds(5), 1.38685m),
new IndicatorDataPoint(time.AddSeconds(6), 1.38683m),
new IndicatorDataPoint(time.AddSeconds(7), 1.38682m),
new IndicatorDataPoint(time.AddSeconds(8), 1.38682m),
new IndicatorDataPoint(time.AddSeconds(9), 1.38684m),
new IndicatorDataPoint(time.AddSeconds(10), 1.38682m),
new IndicatorDataPoint(time.AddSeconds(11), 1.38680m),
new IndicatorDataPoint(time.AddSeconds(12), 1.38681m),
new IndicatorDataPoint(time.AddSeconds(13), 1.38686m),
new IndicatorDataPoint(time.AddSeconds(14), 1.38688m),
};
}

[Test]
public void ResetWorksAsExpected()
{
// Test Renko bar consistency amongst three consolidators starting at different times

var time = new DateTime(2016, 1, 1);
var testValues = GetTestValues();


var consolidator = CreateConsolidator();
foreach (var data in testValues)
{
consolidator.Update(data);
}

var beforeResetConsolidator = consolidator;
Marinovsky marked this conversation as resolved.
Show resolved Hide resolved

consolidator.Reset();
AssertConsolidator(consolidator);

foreach (var data in testValues)
{
consolidator.Update(data);
}

AssertConsolidator(consolidator, beforeResetConsolidator);

consolidator.Dispose();
}
}
}
27 changes: 26 additions & 1 deletion Tests/Common/Data/BaseDataConsolidatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

using System;
using System.Collections.Generic;
using NUnit.Framework;
using QuantConnect.Data.Consolidators;
using QuantConnect.Data.Market;
Expand All @@ -23,7 +24,7 @@
namespace QuantConnect.Tests.Common.Data
{
[TestFixture]
public class BaseDataConsolidatorTests
public class BaseDataConsolidatorTests: BaseConsolidatorTests
{
[Test]
public void AggregatesTickToNewTradeBarProperly()
Expand Down Expand Up @@ -331,5 +332,29 @@ public void RegisterIndicator(IndicatorBase<IndicatorDataPoint> indicator, IData
indicator.Update(consolidated.EndTime, consolidated.Value);
};
}

protected override IDataConsolidator CreateConsolidator()
{
return new BaseDataConsolidator(4);
}

protected override dynamic GetTestValues()
{
var time = new DateTime(2015, 04, 13, 8, 31, 0);
return new List<TradeBar>()
{
new TradeBar(){ Time = time, Period = Time.OneMinute, Symbol = Symbols.SPY, High = 10 },
new TradeBar(){ Time = time.AddMinutes(1), Period = Time.OneMinute, Symbol = Symbols.SPY, High = 12, Close = 5 },
new TradeBar(){ Time = time.AddMinutes(2), Period = Time.OneMinute, Symbol = Symbols.SPY, High = 10, Close = 7 },
new TradeBar(){ Time = time.AddMinutes(3), Period = Time.OneMinute, Symbol = Symbols.SPY, High = 5, Close = 2 },
new TradeBar(){ Time = time.AddMinutes(4), Period = Time.OneMinute, Symbol = Symbols.SPY, High = 15 , Close = 2 },
new TradeBar(){ Time = time.AddMinutes(5), Period = Time.OneMinute, Symbol = Symbols.SPY, High = 20 , Close = 2 },
new TradeBar(){ Time = time.AddMinutes(6), Period = Time.OneMinute, Symbol = Symbols.SPY, High = 18 , Close = 8 },
new TradeBar(){ Time = time.AddMinutes(7), Period = Time.OneMinute, Symbol = Symbols.SPY, High = 12 , Close = 4 },
new TradeBar(){ Time = time.AddMinutes(8), Period = Time.OneMinute, Symbol = Symbols.SPY, High = 25 , Close = 5 },
new TradeBar(){ Time = time.AddMinutes(9), Period = Time.OneMinute, Symbol = Symbols.SPY, High = 30 , Close = 4 },
new TradeBar(){ Time = time.AddMinutes(10), Period = Time.OneMinute, Symbol = Symbols.SPY, High = 26 , Close = 7 },
};
}
}
}
2 changes: 1 addition & 1 deletion Tests/Common/Data/ClassicRangeConsolidatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace QuantConnect.Tests.Common.Data
{
public class ClassicRangeConsolidatorTests : RangeConsolidatorTests
{
protected override RangeConsolidator CreateConsolidator(int range)
protected override RangeConsolidator CreateRangeConsolidator(int range)
{
return new ClassicRangeConsolidator(range, x => x.Value, x => 10m);
}
Expand Down
7 changes: 6 additions & 1 deletion Tests/Common/Data/ClassicRenkoConsolidatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
namespace QuantConnect.Tests.Common.Data
{
[TestFixture]
public class ClassicRenkoConsolidatorTests
public class ClassicRenkoConsolidatorTests: BaseConsolidatorTests
{
[Test]
public void ClassicOutputTypeIsRenkoBar()
Expand Down Expand Up @@ -279,5 +279,10 @@ def getConsolidator():
}
}
}

protected override IDataConsolidator CreateConsolidator()
{
return new ClassicRenkoConsolidator(0.0001m);
}
}
}
Loading
Loading