Files
quantconnect--lean/Algorithm.CSharp/SpreadExecutionModelRegressionAlgorithm.cs
T
Ronit Jain 15066ae5e1 Feature improve regression tests (#6245)
* add data count properties

* 'add history count property

* assert data counts

* update missing override

* consider override/virtual cases

* implement data count

* add message handler for regression tests

* use regression test message handler

* set algorithm manager for regression test message handler

* update data count

* check if stats are present, check if algo manager is not null

* update

* add c# algo

* make same as c# algo

* use new line

* logic shifted to RegressionTestMessageHandler

* cleanup

* auto cleanup

* skip non deterministic data count

* change data count

* use inheritance

* improve stats

* update couht

* add sma indicator to c# and customSMA to python

* call base method before executing further

* skip test

* revert to original

* add duplicate sma

* skip regression test
2022-03-15 16:51:15 -03:00

143 lines
6.3 KiB
C#

/*
* 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 QuantConnect.Orders;
using QuantConnect.Interfaces;
using System.Collections.Generic;
using QuantConnect.Algorithm.Framework.Alphas;
using QuantConnect.Algorithm.Framework.Execution;
using QuantConnect.Algorithm.Framework.Portfolio;
using QuantConnect.Algorithm.Framework.Selection;
namespace QuantConnect.Algorithm.CSharp
{
/// <summary>
/// Regression algorithm for the SpreadExecutionModel.
/// This algorithm shows how the execution model works to
/// submit orders only when the price is on desirably tight spread.
/// </summary>
/// <meta name="tag" content="using data" />
/// <meta name="tag" content="using quantconnect" />
/// <meta name="tag" content="trading and orders" />
public class SpreadExecutionModelRegressionAlgorithm : QCAlgorithm, IRegressionAlgorithmDefinition
{
/// <summary>
/// Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.
/// </summary>
public override void Initialize()
{
SetStartDate(2013, 10, 7);
SetEndDate(2013, 10, 11);
SetUniverseSelection(new ManualUniverseSelectionModel(
QuantConnect.Symbol.Create("AIG", SecurityType.Equity, Market.USA),
QuantConnect.Symbol.Create("BAC", SecurityType.Equity, Market.USA),
QuantConnect.Symbol.Create("IBM", SecurityType.Equity, Market.USA),
QuantConnect.Symbol.Create("SPY", SecurityType.Equity, Market.USA)));
// using hourly rsi to generate more insights
SetAlpha(new RsiAlphaModel(14, Resolution.Hour));
SetPortfolioConstruction(new EqualWeightingPortfolioConstructionModel());
SetExecution(new SpreadExecutionModel());
InsightsGenerated += OnInsightsGenerated;
}
private void OnInsightsGenerated(IAlgorithm algorithm, GeneratedInsightsCollection eventdata)
{
Log($"{Time}: {string.Join(", ", eventdata)}");
}
/// <summary>
/// Order fill event handler. On an order fill update the resulting information is passed to this method.
/// </summary>
/// <param name="orderEvent">Order event details containing details of the evemts</param>
/// <remarks>This method can be called asynchronously and so should only be used by seasoned C# experts. Ensure you use proper locks on thread-unsafe objects</remarks>
public override void OnOrderEvent(OrderEvent orderEvent)
{
Debug($"Purchased Stock: {orderEvent.Symbol}");
}
/// <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 => true;
/// <summary>
/// This is used by the regression test system to indicate which languages this algorithm is written in.
/// </summary>
public Language[] Languages { get; } = { Language.CSharp, Language.Python };
/// <summary>
/// Data Points count of all timeslices of algorithm
/// </summary>
public long DataPoints => 15643;
/// </summary>
/// Data Points count of the algorithm history
/// </summary>
public int AlgorithmHistoryDataPoints => 56;
/// <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 Trades", "20"},
{"Average Win", "0.62%"},
{"Average Loss", "-0.19%"},
{"Compounding Annual Return", "399.082%"},
{"Drawdown", "1.500%"},
{"Expectancy", "1.396"},
{"Net Profit", "2.077%"},
{"Sharpe Ratio", "10.497"},
{"Probabilistic Sharpe Ratio", "70.088%"},
{"Loss Rate", "44%"},
{"Win Rate", "56%"},
{"Profit-Loss Ratio", "3.31"},
{"Alpha", "0.533"},
{"Beta", "1.115"},
{"Annual Standard Deviation", "0.261"},
{"Annual Variance", "0.068"},
{"Information Ratio", "8.85"},
{"Tracking Error", "0.086"},
{"Treynor Ratio", "2.461"},
{"Total Fees", "$41.65"},
{"Estimated Strategy Capacity", "$2100000.00"},
{"Lowest Capacity Asset", "IBM R735QTJ8XC9X"},
{"Fitness Score", "0.999"},
{"Kelly Criterion Estimate", "34.359"},
{"Kelly Criterion Probability Value", "0.442"},
{"Sortino Ratio", "79228162514264337593543950335"},
{"Return Over Maximum Drawdown", "437.491"},
{"Portfolio Turnover", "1.083"},
{"Total Insights Generated", "5"},
{"Total Insights Closed", "3"},
{"Total Insights Analysis Completed", "3"},
{"Long Insight Count", "3"},
{"Short Insight Count", "2"},
{"Long/Short Ratio", "150.0%"},
{"Estimated Monthly Alpha Value", "$801912.7740"},
{"Total Accumulated Estimated Alpha Value", "$129197.0580"},
{"Mean Population Estimated Insight Value", "$43065.6860"},
{"Mean Population Direction", "100%"},
{"Mean Population Magnitude", "0%"},
{"Rolling Averaged Population Direction", "100%"},
{"Rolling Averaged Population Magnitude", "0%"},
{"OrderListHash", "ac25829306d9fb41b6fc8eebf438c505"}
};
}
}