/*
* 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.Market;
using QuantConnect.Interfaces;
using QuantConnect.Indicators;
using QuantConnect.Data;
namespace QuantConnect.Algorithm.CSharp
{
///
/// Regression test to check custom indicators warms up properly
/// when one of them define WarmUpPeriod parameter and the other doesn't
///
public class CustomWarmUpPeriodIndicatorAlgorithm : QCAlgorithm, IRegressionAlgorithmDefinition
{
private CSMANotWarmUp _customNotWarmUp;
private CSMAWithWarmUp _customWarmUp;
private SimpleMovingAverage _customNotInherit;
private SimpleMovingAverage _duplicateSMA;
public override void Initialize()
{
SetStartDate(2013, 10, 7);
SetEndDate(2013, 10, 11);
AddEquity("SPY", Resolution.Second);
// Create two custom indicators, where one of them defines WarmUpPeriod parameter
_customNotWarmUp = new CSMANotWarmUp("_customNotWarmUp", 60);
_customWarmUp = new CSMAWithWarmUp("_customWarmUp", 60);
_customNotInherit = new SimpleMovingAverage("_customNotInherit", 60);
// using 2nd SMA to match counterpart python algorithm ( CustomSMA + csharpIndicator )
// so that AlgorithmHistoryDataPoints are the same in both
_duplicateSMA = new SimpleMovingAverage("_duplicateSMA", 60);
// Register the daily data of "SPY" to automatically update both indicators
RegisterIndicator("SPY", _customWarmUp, Resolution.Minute);
RegisterIndicator("SPY", _customNotWarmUp, Resolution.Minute);
RegisterIndicator("SPY", _customNotInherit, Resolution.Minute);
RegisterIndicator("SPY", _duplicateSMA, Resolution.Minute);
// Warm up _customWarmUp indicator
WarmUpIndicator("SPY", _customWarmUp, Resolution.Minute);
// Check _customWarmUp indicator has already been warmed up with the requested data
if (!_customWarmUp.IsReady)
{
throw new Exception("_customWarmUp indicator was expected to be ready");
}
if (_customWarmUp.Samples != 60)
{
throw new Exception("_customWarmUp indicator was expected to have processed 60 datapoints already");
}
// Try to warm up _customNotWarmUp indicator. It's expected from LEAN to skip the warm up process
// because this indicator doesn't implement IIndicatorWarmUpPeriodProvider
WarmUpIndicator("SPY", _customNotWarmUp, Resolution.Minute);
// Check _customNotWarmUp indicator is not ready, because the warm up process was skipped
if (_customNotWarmUp.IsReady)
{
throw new Exception("_customNotWarmUp indicator wasn't expected to be warmed up");
}
WarmUpIndicator("SPY", _customNotInherit, Resolution.Minute);
// Check _customWarmUp indicator has already been warmed up with the requested data
if (!_customNotInherit.IsReady)
{
throw new Exception("_customNotInherit indicator was expected to be ready");
}
if (_customNotInherit.Samples != 60)
{
throw new Exception("_customNotInherit indicator was expected to have processed 60 datapoints already");
}
WarmUpIndicator("SPY", _duplicateSMA, Resolution.Minute);
// Check _customWarmUp indicator has already been warmed up with the requested data
if (!_duplicateSMA.IsReady)
{
throw new Exception("_duplicateSMA indicator was expected to be ready");
}
if (_duplicateSMA.Samples != 60)
{
throw new Exception("_duplicateSMA indicator was expected to have processed 60 datapoints already");
}
}
public void OnData(TradeBars data)
{
if (!Portfolio.Invested)
{
SetHoldings("SPY", 1);
}
if (Time.Second == 0)
{
// Compute the difference between the indicators values
var diff = Math.Abs(_customNotWarmUp.Current.Value - _customWarmUp.Current.Value);
diff += Math.Abs(_customNotInherit.Current.Value - _customNotWarmUp.Current.Value);
diff += Math.Abs(_customNotInherit.Current.Value - _customWarmUp.Current.Value);
diff += Math.Abs(_duplicateSMA.Current.Value - _customWarmUp.Current.Value);
diff += Math.Abs(_duplicateSMA.Current.Value - _customNotWarmUp.Current.Value);
diff += Math.Abs(_duplicateSMA.Current.Value - _customNotInherit.Current.Value);
// Check _customNotWarmUp indicator is ready when the number of samples is bigger than its period
if (_customNotWarmUp.IsReady != (_customNotWarmUp.Samples >= 60))
{
throw new Exception("_customNotWarmUp indicator was expected to be ready when the number of samples were bigger that its WarmUpPeriod parameter");
}
// Check their values are the same when both are ready
if (diff > 1e-10m && _customNotWarmUp.IsReady && _customWarmUp.IsReady)
{
throw new Exception($"The values of the indicators are not the same. The difference is {diff}");
}
}
}
///
/// Custom implementation of SimpleMovingAverage.
/// Represents the traditional simple moving average indicator (SMA) without WarmUpPeriod parameter defined
///
private class CSMANotWarmUp : IndicatorBase
{
private Queue _queue;
private int _period;
public CSMANotWarmUp(string name, int period)
: base(name)
{
_queue = new Queue();
_period = period;
}
public override bool IsReady => _queue.Count == _period;
protected override decimal ComputeNextValue(IBaseData input)
{
_queue.Enqueue(input);
if (_queue.Count > _period)
{
_queue.Dequeue();
}
var items = (_queue.ToArray());
var sum = 0m;
Array.ForEach(items, i => sum += i.Value);
return sum / _queue.Count;
}
}
///
/// Custom implementation of SimpleMovingAverage.
/// Represents the traditional simple moving average indicator (SMA) with WarmUpPeriod defined
///
private class CSMAWithWarmUp : CSMANotWarmUp, IIndicatorWarmUpPeriodProvider
{
public CSMAWithWarmUp(string name, int period)
: base(name, period)
{
WarmUpPeriod = period;
}
public int WarmUpPeriod { get; private set; }
}
///
/// This is used by the regression test system to indicate if the open source Lean repository has the required data to run this algorithm.
///
public bool CanRunLocally { get; } = true;
///
/// This is used by the regression test system to indicate which languages this algorithm is written in.
///
public Language[] Languages { get; } = { Language.CSharp, Language.Python };
///
/// Data Points count of all timeslices of algorithm
///
public long DataPoints => 234043;
///
/// Data Points count of the algorithm history
///
public int AlgorithmHistoryDataPoints => 360;
///
/// This is used by the regression test system to indicate what the expected statistics are from running the algorithm
///
public Dictionary ExpectedStatistics => new Dictionary
{
{"Total Trades", "1"},
{"Average Win", "0%"},
{"Average Loss", "0%"},
{"Compounding Annual Return", "272.157%"},
{"Drawdown", "2.200%"},
{"Expectancy", "0"},
{"Net Profit", "1.694%"},
{"Sharpe Ratio", "8.897"},
{"Probabilistic Sharpe Ratio", "67.609%"},
{"Loss Rate", "0%"},
{"Win Rate", "0%"},
{"Profit-Loss Ratio", "0"},
{"Alpha", "-0.003"},
{"Beta", "0.998"},
{"Annual Standard Deviation", "0.222"},
{"Annual Variance", "0.049"},
{"Information Ratio", "-14.534"},
{"Tracking Error", "0.001"},
{"Treynor Ratio", "1.98"},
{"Total Fees", "$3.45"},
{"Estimated Strategy Capacity", "$310000000.00"},
{"Lowest Capacity Asset", "SPY R735QTJ8XC9X"},
{"Fitness Score", "0.246"},
{"Kelly Criterion Estimate", "0"},
{"Kelly Criterion Probability Value", "0"},
{"Sortino Ratio", "9.761"},
{"Return Over Maximum Drawdown", "107.509"},
{"Portfolio Turnover", "0.249"},
{"Total Insights Generated", "0"},
{"Total Insights Closed", "0"},
{"Total Insights Analysis Completed", "0"},
{"Long Insight Count", "0"},
{"Short Insight Count", "0"},
{"Long/Short Ratio", "100%"},
{"Estimated Monthly Alpha Value", "$0"},
{"Total Accumulated Estimated Alpha Value", "$0"},
{"Mean Population Estimated Insight Value", "$0"},
{"Mean Population Direction", "0%"},
{"Mean Population Magnitude", "0%"},
{"Rolling Averaged Population Direction", "0%"},
{"Rolling Averaged Population Magnitude", "0%"},
{"OrderListHash", "e10039d74166b161f3ea2851a5e85843"}
};
}
}