Skip to content

Commit eb8043e

Browse files
Fix problem with IsSubscriptionValidForConsolidator() method (#8754)
* Fix problem with IsSubscriptionValidForConsolidator() method * Add regression algorithms using TickQuoteBarConsolidator * Fix the method IsSubscriptionValidForConsolidator() * Refactor IsSubscriptionValidForConsolidator method * Resolve review comments * Add new regression algo and update unit tests * Update TickTradeBarConsolidatorWithQuoteTickTypeRegressionAlgorithm.cs --------- Co-authored-by: Martin-Molinero <martin.molinero1@gmail.com>
1 parent 7dad4de commit eb8043e

7 files changed

Lines changed: 653 additions & 230 deletions
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3+
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*
15+
*/
16+
17+
using QuantConnect.Data;
18+
using QuantConnect.Data.Consolidators;
19+
20+
namespace QuantConnect.Algorithm.CSharp
21+
{
22+
/// <summary>
23+
/// This algorithm tests the functionality of the TickQuoteBarConsolidator with tick data.
24+
/// The SubscriptionManager.AddConsolidator method uses a null TickType since none is specified.
25+
/// It checks if data consolidation occurs as expected for the given time period. If consolidation does not happen, a RegressionTestException is thrown.
26+
/// </summary>
27+
public class TickQuoteBarConsolidatorWithDefaultTickTypeRegressionAlgorithm : TickQuoteBarConsolidatorWithTickTypeRegressionAlgorithm
28+
{
29+
protected override void AddConsolidator(TickQuoteBarConsolidator consolidator)
30+
{
31+
SubscriptionManager.AddConsolidator(GoldFuture.Mapped, consolidator);
32+
}
33+
}
34+
}
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/*
2+
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3+
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*
15+
*/
16+
17+
using System;
18+
using System.Collections.Generic;
19+
using QuantConnect.Data;
20+
using QuantConnect.Data.Consolidators;
21+
using QuantConnect.Data.Market;
22+
using QuantConnect.Interfaces;
23+
using QuantConnect.Securities.Future;
24+
25+
namespace QuantConnect.Algorithm.CSharp
26+
{
27+
/// <summary>
28+
/// This algorithm tests the functionality of the TickQuoteBarConsolidator with tick data.
29+
/// The SubscriptionManager.AddConsolidator method uses a Quote TickType
30+
/// It checks if data consolidation occurs as expected for the given time period. If consolidation does not happen, a RegressionTestException is thrown.
31+
/// </summary>
32+
public class TickQuoteBarConsolidatorWithTickTypeRegressionAlgorithm : QCAlgorithm, IRegressionAlgorithmDefinition
33+
{
34+
private Dictionary<Symbol, TickQuoteBarConsolidator> _consolidators = new Dictionary<Symbol, TickQuoteBarConsolidator>();
35+
private bool _itWasConsolidated;
36+
protected Future GoldFuture { get; set; }
37+
public override void Initialize()
38+
{
39+
SetStartDate(2013, 10, 7);
40+
SetEndDate(2013, 10, 9);
41+
42+
GoldFuture = AddFuture("GC", Resolution.Tick, Market.COMEX);
43+
GoldFuture.SetFilter(0, 180);
44+
}
45+
46+
private void OnConsolidated(object sender, QuoteBar bar)
47+
{
48+
_itWasConsolidated = true;
49+
}
50+
51+
public override void OnData(Slice slice)
52+
{
53+
if (!_consolidators.ContainsKey(GoldFuture.Mapped))
54+
{
55+
var consolidator = new TickQuoteBarConsolidator(TimeSpan.FromSeconds(10));
56+
consolidator.DataConsolidated += OnConsolidated;
57+
AddConsolidator(consolidator);
58+
_consolidators[GoldFuture.Mapped] = consolidator;
59+
}
60+
}
61+
62+
protected virtual void AddConsolidator(TickQuoteBarConsolidator consolidator)
63+
{
64+
SubscriptionManager.AddConsolidator(GoldFuture.Mapped, consolidator, TickType.Quote);
65+
}
66+
67+
public override void OnEndOfAlgorithm()
68+
{
69+
if (!_itWasConsolidated)
70+
{
71+
throw new RegressionTestException("TickQuoteBarConsolidator did not consolidate any data.");
72+
}
73+
}
74+
75+
/// <summary>
76+
/// This is used by the regression test system to indicate if the open source Lean repository has the required data to run this algorithm.
77+
/// </summary>
78+
public bool CanRunLocally { get; } = true;
79+
80+
/// <summary>
81+
/// This is used by the regression test system to indicate which languages this algorithm is written in.
82+
/// </summary>
83+
public List<Language> Languages { get; } = new() { Language.CSharp };
84+
85+
/// <summary>
86+
/// Data Points count of all timeslices of algorithm
87+
/// </summary>
88+
public long DataPoints => 1082920;
89+
90+
/// <summary>
91+
/// Data Points count of the algorithm history
92+
/// </summary>
93+
public int AlgorithmHistoryDataPoints => 0;
94+
95+
/// <summary>
96+
/// Final status of the algorithm
97+
/// </summary>
98+
public AlgorithmStatus AlgorithmStatus => AlgorithmStatus.Completed;
99+
100+
/// <summary>
101+
/// This is used by the regression test system to indicate what the expected statistics are from running the algorithm
102+
/// </summary>
103+
public Dictionary<string, string> ExpectedStatistics => new Dictionary<string, string>
104+
{
105+
{"Total Orders", "0"},
106+
{"Average Win", "0%"},
107+
{"Average Loss", "0%"},
108+
{"Compounding Annual Return", "0%"},
109+
{"Drawdown", "0%"},
110+
{"Expectancy", "0"},
111+
{"Start Equity", "100000"},
112+
{"End Equity", "100000"},
113+
{"Net Profit", "0%"},
114+
{"Sharpe Ratio", "0"},
115+
{"Sortino Ratio", "0"},
116+
{"Probabilistic Sharpe Ratio", "0%"},
117+
{"Loss Rate", "0%"},
118+
{"Win Rate", "0%"},
119+
{"Profit-Loss Ratio", "0"},
120+
{"Alpha", "0"},
121+
{"Beta", "0"},
122+
{"Annual Standard Deviation", "0"},
123+
{"Annual Variance", "0"},
124+
{"Information Ratio", "5.524"},
125+
{"Tracking Error", "0.136"},
126+
{"Treynor Ratio", "0"},
127+
{"Total Fees", "$0.00"},
128+
{"Estimated Strategy Capacity", "$0"},
129+
{"Lowest Capacity Asset", ""},
130+
{"Portfolio Turnover", "0%"},
131+
{"OrderListHash", "d41d8cd98f00b204e9800998ecf8427e"}
132+
};
133+
}
134+
}
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/*
2+
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3+
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*
15+
*/
16+
17+
using System;
18+
using System.Collections.Generic;
19+
using QuantConnect.Data;
20+
using QuantConnect.Data.Consolidators;
21+
using QuantConnect.Data.Market;
22+
using QuantConnect.Interfaces;
23+
using QuantConnect.Securities.Future;
24+
25+
namespace QuantConnect.Algorithm.CSharp
26+
{
27+
/// <summary>
28+
/// This algorithm tests the functionality of the TickConsolidator with tick data.
29+
/// The SubscriptionManager.AddConsolidator method uses a null TickType since none is specified.
30+
/// It checks if data consolidation occurs as expected for the given time period. If consolidation does not happen, a RegressionTestException is thrown.
31+
/// </summary>
32+
public class TickTradeBarConsolidatorWithDefaultTickTypeRegressionAlgorithm : QCAlgorithm, IRegressionAlgorithmDefinition
33+
{
34+
private Dictionary<Symbol, TickConsolidator> _consolidators = new Dictionary<Symbol, TickConsolidator>();
35+
protected bool ItWasConsolidated { get; set; }
36+
protected Future GoldFuture { get; set; }
37+
public override void Initialize()
38+
{
39+
SetStartDate(2013, 10, 7);
40+
SetEndDate(2013, 10, 9);
41+
42+
GoldFuture = AddFuture("GC", Resolution.Tick, Market.COMEX);
43+
GoldFuture.SetFilter(0, 180);
44+
}
45+
46+
private void OnConsolidated(object sender, TradeBar bar)
47+
{
48+
ItWasConsolidated = true;
49+
}
50+
51+
public override void OnData(Slice slice)
52+
{
53+
if (!_consolidators.ContainsKey(GoldFuture.Mapped))
54+
{
55+
var consolidator = new TickConsolidator(TimeSpan.FromSeconds(10));
56+
consolidator.DataConsolidated += OnConsolidated;
57+
AddConsolidator(consolidator);
58+
_consolidators[GoldFuture.Mapped] = consolidator;
59+
}
60+
}
61+
62+
protected virtual void AddConsolidator(TickConsolidator consolidator)
63+
{
64+
SubscriptionManager.AddConsolidator(GoldFuture.Mapped, consolidator);
65+
}
66+
67+
public override void OnEndOfAlgorithm()
68+
{
69+
if (!ItWasConsolidated)
70+
{
71+
throw new RegressionTestException("TickConsolidator did not consolidate any data.");
72+
}
73+
}
74+
75+
/// <summary>
76+
/// This is used by the regression test system to indicate if the open source Lean repository has the required data to run this algorithm.
77+
/// </summary>
78+
public bool CanRunLocally { get; } = true;
79+
80+
/// <summary>
81+
/// This is used by the regression test system to indicate which languages this algorithm is written in.
82+
/// </summary>
83+
public List<Language> Languages { get; } = new() { Language.CSharp };
84+
85+
/// <summary>
86+
/// Data Points count of all timeslices of algorithm
87+
/// </summary>
88+
public long DataPoints => 1082920;
89+
90+
/// <summary>
91+
/// Data Points count of the algorithm history
92+
/// </summary>
93+
public int AlgorithmHistoryDataPoints => 0;
94+
95+
/// <summary>
96+
/// Final status of the algorithm
97+
/// </summary>
98+
public AlgorithmStatus AlgorithmStatus => AlgorithmStatus.Completed;
99+
100+
/// <summary>
101+
/// This is used by the regression test system to indicate what the expected statistics are from running the algorithm
102+
/// </summary>
103+
public Dictionary<string, string> ExpectedStatistics => new Dictionary<string, string>
104+
{
105+
{"Total Orders", "0"},
106+
{"Average Win", "0%"},
107+
{"Average Loss", "0%"},
108+
{"Compounding Annual Return", "0%"},
109+
{"Drawdown", "0%"},
110+
{"Expectancy", "0"},
111+
{"Start Equity", "100000"},
112+
{"End Equity", "100000"},
113+
{"Net Profit", "0%"},
114+
{"Sharpe Ratio", "0"},
115+
{"Sortino Ratio", "0"},
116+
{"Probabilistic Sharpe Ratio", "0%"},
117+
{"Loss Rate", "0%"},
118+
{"Win Rate", "0%"},
119+
{"Profit-Loss Ratio", "0"},
120+
{"Alpha", "0"},
121+
{"Beta", "0"},
122+
{"Annual Standard Deviation", "0"},
123+
{"Annual Variance", "0"},
124+
{"Information Ratio", "5.524"},
125+
{"Tracking Error", "0.136"},
126+
{"Treynor Ratio", "0"},
127+
{"Total Fees", "$0.00"},
128+
{"Estimated Strategy Capacity", "$0"},
129+
{"Lowest Capacity Asset", ""},
130+
{"Portfolio Turnover", "0%"},
131+
{"OrderListHash", "d41d8cd98f00b204e9800998ecf8427e"}
132+
};
133+
}
134+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3+
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*
15+
*/
16+
17+
using QuantConnect.Data;
18+
using QuantConnect.Data.Consolidators;
19+
20+
namespace QuantConnect.Algorithm.CSharp
21+
{
22+
/// <summary>
23+
/// This algorithm tests the functionality of the TickConsolidator with tick data.
24+
/// The SubscriptionManager.AddConsolidator method uses a Quote TickType
25+
/// It checks if data consolidation does not occur when the algorithm is running. If consolidation happens, a RegressionTestException is thrown.
26+
/// </summary>
27+
public class TickTradeBarConsolidatorWithQuoteTickTypeRegressionAlgorithm : TickTradeBarConsolidatorWithDefaultTickTypeRegressionAlgorithm
28+
{
29+
protected override void AddConsolidator(TickConsolidator consolidator)
30+
{
31+
SubscriptionManager.AddConsolidator(GoldFuture.Mapped, consolidator, TickType.Quote);
32+
}
33+
34+
public override void OnEndOfAlgorithm()
35+
{
36+
if (ItWasConsolidated)
37+
{
38+
throw new RegressionTestException("TickConsolidator should not have consolidated Quote ticks.");
39+
}
40+
}
41+
}
42+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3+
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*
15+
*/
16+
17+
using QuantConnect.Data;
18+
using QuantConnect.Data.Consolidators;
19+
namespace QuantConnect.Algorithm.CSharp
20+
{
21+
/// <summary>
22+
/// This algorithm tests the functionality of the TickConsolidator with tick data.
23+
/// The SubscriptionManager.AddConsolidator method uses a Trade TickType
24+
/// It checks if data consolidation occurs as expected for the given time period. If consolidation does not happen, a RegressionTestException is thrown.
25+
/// </summary>
26+
public class TickTradeBarConsolidatorWithTradeTickTypeRegressionAlgorithm : TickTradeBarConsolidatorWithDefaultTickTypeRegressionAlgorithm
27+
{
28+
protected override void AddConsolidator(TickConsolidator consolidator)
29+
{
30+
SubscriptionManager.AddConsolidator(GoldFuture.Mapped, consolidator, TickType.Trade);
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)