Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/*
* 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 QuantConnect.Data;
using QuantConnect.Data.Consolidators;
using QuantConnect.Indicators;
using QuantConnect.Interfaces;

namespace QuantConnect.Algorithm.CSharp
{
/// <summary>
/// Asserts that indicators registered with a consolidator are automatically warmed up when
/// 'Settings.AutomaticIndicatorWarmUp' is enabled, without requiring a manual history replay
/// </summary>
public class AutomaticIndicatorWarmupConsolidatorRegressionAlgorithm : QCAlgorithm, IRegressionAlgorithmDefinition
{
private Symbol _spy;
private AverageTrueRange _atr;
private RelativeStrengthIndex _rsi;

public override void Initialize()
{
SetStartDate(2013, 10, 08);
SetEndDate(2013, 10, 09);

Settings.AutomaticIndicatorWarmUp = true;
_spy = AddEquity("SPY", Resolution.Minute).Symbol;

// Test case 1: bar indicator registered with a consolidator, previously required a manual history replay
_atr = new AverageTrueRange(10);
RegisterIndicator(_spy, _atr, new TradeBarConsolidator(TimeSpan.FromMinutes(30)));
AssertIsReady(_atr, expected: true);

// Test case 2: data point indicator registered with a consolidator and a selector
_rsi = new RelativeStrengthIndex(14);
RegisterIndicator(_spy, _rsi, new TradeBarConsolidator(TimeSpan.FromMinutes(30)), Field.Close);
AssertIsReady(_rsi, expected: true);

// Test case 3: non time based consolidators cannot be automatically warmed up, the indicator is
// registered but left cold
var renkoRsi = new RelativeStrengthIndex(14);
RegisterIndicator(_spy, renkoRsi, new RenkoConsolidator(1m), Field.Close);
AssertIsReady(renkoRsi, expected: false);

// Test case 4: with the setting disabled nothing is warmed up
Settings.AutomaticIndicatorWarmUp = false;
var notWarmed = new AverageTrueRange(10);
RegisterIndicator(_spy, notWarmed, new TradeBarConsolidator(TimeSpan.FromMinutes(30)));
AssertIsReady(notWarmed, expected: false);
Settings.AutomaticIndicatorWarmUp = true;
}

private static void AssertIsReady(IIndicator indicator, bool expected)
{
if (indicator.IsReady != expected)
{
throw new RegressionTestException($"Expected {indicator.Name} IsReady to be {expected} but was {indicator.IsReady}");
}
}

/// <summary>
/// OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
/// </summary>
/// <param name="slice">Slice object keyed by symbol containing the stock data</param>
public override void OnData(Slice slice)
{
if (!Portfolio.Invested)
{
SetHoldings(_spy, 1);
}
}

/// <summary>
/// This is used by the regression test system to indicate if the open source Lean repository has the required data to run this algorithm.
/// </summary>
public bool CanRunLocally { get; } = true;

/// <summary>
/// This is used by the regression test system to indicate which languages this algorithm is written in.
/// </summary>
public List<Language> Languages { get; } = new() { Language.CSharp, Language.Python };

/// <summary>
/// Data Points count of all timeslices of algorithm
/// </summary>
public long DataPoints => 1582;

/// <summary>
/// Data Points count of the algorithm history
/// </summary>
public int AlgorithmHistoryDataPoints => 1500;

/// <summary>
/// Final status of the algorithm
/// </summary>
public AlgorithmStatus AlgorithmStatus => AlgorithmStatus.Completed;

/// <summary>
/// This is used by the regression test system to indicate what the expected statistics are from running the algorithm
/// </summary>
public Dictionary<string, string> ExpectedStatistics => new Dictionary<string, string>
{
{"Total Orders", "1"},
{"Average Win", "0%"},
{"Average Loss", "0%"},
{"Compounding Annual Return", "0%"},
{"Drawdown", "0%"},
{"Expectancy", "0"},
{"Start Equity", "100000"},
{"End Equity", "98848.47"},
{"Net Profit", "0%"},
{"Sharpe Ratio", "0"},
{"Sortino Ratio", "0"},
{"Probabilistic Sharpe Ratio", "0%"},
{"Loss Rate", "0%"},
{"Win Rate", "0%"},
{"Profit-Loss Ratio", "0"},
{"Alpha", "0"},
{"Beta", "0"},
{"Annual Standard Deviation", "0"},
{"Annual Variance", "0"},
{"Information Ratio", "0"},
{"Tracking Error", "0"},
{"Treynor Ratio", "0"},
{"Total Fees", "$3.44"},
{"Estimated Strategy Capacity", "$31000000.00"},
{"Lowest Capacity Asset", "SPY R735QTJ8XC9X"},
{"Portfolio Turnover", "50.43%"},
{"Drawdown Recovery", "0"},
{"OrderListHash", "00636a25aed88acd2171c6221c747716"}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,12 @@ public override void Initialize()
// force spy for use Raw data mode so that it matches the used when unsubscribed which uses the universe settings
SubscriptionManager.SubscriptionDataConfigService.GetSubscriptionDataConfigs(spy).SetDataNormalizationMode(DataNormalizationMode.Raw);

// Test case: custom IndicatorBase<QuoteBar> indicator using Future subscribed symbol
// Test case: custom IndicatorBase<QuoteBar> indicator using Future subscribed symbol.
// Since 'Settings.AutomaticIndicatorWarmUp' is enabled, registering with a consolidator warms the indicator up
var indicator = new CustomIndicator();
var consolidator = CreateConsolidator(TimeSpan.FromMinutes(2), typeof(QuoteBar));
RegisterIndicator(_symbol, indicator, consolidator);

AssertIndicatorState(indicator, isReady: false);
WarmUpIndicator(_symbol, indicator);
AssertIndicatorState(indicator, isReady: true);

// Test case: SimpleMovingAverage<IndicatorDataPoint> using Future Subscribed symbol (should use TradeBar)
Expand Down Expand Up @@ -151,7 +150,7 @@ protected override decimal ComputeNextValue(QuoteBar input)
/// <summary>
/// Data Points count of the algorithm history
/// </summary>
public int AlgorithmHistoryDataPoints => 85;
public int AlgorithmHistoryDataPoints => 87;

/// <summary>
/// Final status of the algorithm
Expand Down
23 changes: 17 additions & 6 deletions Algorithm.CSharp/AutomaticIndicatorWarmupRegressionAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,30 @@ public override void Initialize()
throw new RegressionTestException("Expected SMA to be warmed up");
}

// Test case 2
// Test case 2: 'RegisterIndicator' respects the automatic indicator warm-up setting
var indicator = new CustomIndicator(10);
RegisterIndicator(_spy, indicator, Resolution.Minute, (Func<IBaseData, decimal>) null);

if (indicator.IsReady)
if (!indicator.IsReady)
{
throw new RegressionTestException("Expected CustomIndicator to be automatically warmed up when registered");
}

// Test case 3: with the setting disabled 'RegisterIndicator' does not warm up, 'WarmUpIndicator' does
Settings.AutomaticIndicatorWarmUp = false;
var notWarmedIndicator = new CustomIndicator(10);
RegisterIndicator(_spy, notWarmedIndicator, Resolution.Minute, (Func<IBaseData, decimal>) null);

if (notWarmedIndicator.IsReady)
{
throw new RegressionTestException("Expected CustomIndicator Not to be warmed up");
}
WarmUpIndicator(_spy, indicator);
if (!indicator.IsReady)
WarmUpIndicator(_spy, notWarmedIndicator);
if (!notWarmedIndicator.IsReady)
{
throw new RegressionTestException("Expected CustomIndicator to be warmed up");
}
Settings.AutomaticIndicatorWarmUp = true;
}

/// <summary>
Expand All @@ -70,7 +81,7 @@ public override void OnData(Slice slice)
var subscription = SubscriptionManager.SubscriptionDataConfigService.GetSubscriptionDataConfigs(_spy).First(config => config.TickType == TickType.Trade);

// we expect 1 consolidator per indicator
if (subscription.Consolidators.Count != 2)
if (subscription.Consolidators.Count != 3)
{
throw new RegressionTestException($"Unexpected consolidator count for subscription: {subscription.Consolidators.Count}");
}
Expand Down Expand Up @@ -113,7 +124,7 @@ protected override decimal ComputeNextValue(IReadOnlyWindow<IndicatorDataPoint>
/// <summary>
/// Data Points count of the algorithm history
/// </summary>
public int AlgorithmHistoryDataPoints => 40;
public int AlgorithmHistoryDataPoints => 60;

/// <summary>
/// Final status of the algorithm
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# 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.

from AlgorithmImports import *

### <summary>
### Asserts that indicators registered with a consolidator are automatically warmed up when
### 'settings.automatic_indicator_warm_up' is enabled, without requiring a manual history replay
### </summary>
class AutomaticIndicatorWarmupConsolidatorRegressionAlgorithm(QCAlgorithm):
def initialize(self):
self.set_start_date(2013, 10, 8)
self.set_end_date(2013, 10, 9)

self.settings.automatic_indicator_warm_up = True
self._spy = self.add_equity("SPY", Resolution.MINUTE).symbol

# Test case 1: bar indicator registered with a consolidator, previously required a manual history replay
self._atr = AverageTrueRange(10)
self.register_indicator(self._spy, self._atr, TradeBarConsolidator(timedelta(minutes=30)))
self.assert_is_ready(self._atr, True)

# Test case 2: data point indicator registered with a consolidator and a selector
self._rsi = RelativeStrengthIndex(14)
self.register_indicator(self._spy, self._rsi, TradeBarConsolidator(timedelta(minutes=30)), lambda bar: bar.close)
self.assert_is_ready(self._rsi, True)

# Test case 3: non time based consolidators cannot be automatically warmed up, the indicator is
# registered but left cold
renko_rsi = RelativeStrengthIndex(14)
self.register_indicator(self._spy, renko_rsi, RenkoConsolidator(1), lambda bar: bar.close)
self.assert_is_ready(renko_rsi, False)

# Test case 4: with the setting disabled nothing is warmed up
self.settings.automatic_indicator_warm_up = False
not_warmed = AverageTrueRange(10)
self.register_indicator(self._spy, not_warmed, TradeBarConsolidator(timedelta(minutes=30)))
self.assert_is_ready(not_warmed, False)
self.settings.automatic_indicator_warm_up = True

def assert_is_ready(self, indicator, expected):
if indicator.is_ready != expected:
raise Exception(f"Expected {indicator.name} is_ready to be {expected} but was {indicator.is_ready}")

def on_data(self, data):
if not self.portfolio.invested:
self.set_holdings(self._spy, 1)
Loading
Loading