chore: import upstream snapshot with attribution
This commit is contained in:
@@ -0,0 +1,168 @@
|
||||
/*
|
||||
* 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.Interfaces;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using QuantConnect.Data.Market;
|
||||
using QuantConnect.Data.UniverseSelection;
|
||||
using QuantConnect.Orders;
|
||||
using QuantConnect.Data;
|
||||
|
||||
namespace QuantConnect.Algorithm.CSharp
|
||||
{
|
||||
/// <summary>
|
||||
/// In this algorithm we demonstrate how to use the coarse fundamental data to
|
||||
/// define a universe as the top dollar volume
|
||||
/// </summary>
|
||||
/// <meta name="tag" content="using data" />
|
||||
/// <meta name="tag" content="universes" />
|
||||
/// <meta name="tag" content="coarse universes" />
|
||||
/// <meta name="tag" content="regression test" />
|
||||
public class CoarseFundamentalTop3Algorithm : QCAlgorithm, IRegressionAlgorithmDefinition
|
||||
{
|
||||
private const int NumberOfSymbols = 3;
|
||||
|
||||
// initialize our changes to nothing
|
||||
private SecurityChanges _changes = SecurityChanges.None;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
UniverseSettings.Resolution = Resolution.Daily;
|
||||
|
||||
SetStartDate(2014, 03, 24);
|
||||
SetEndDate(2014, 04, 07);
|
||||
SetCash(50000);
|
||||
|
||||
// this add universe method accepts a single parameter that is a function that
|
||||
// accepts an IEnumerable<CoarseFundamental> and returns IEnumerable<Symbol>
|
||||
AddUniverse(CoarseSelectionFunction);
|
||||
}
|
||||
|
||||
// sort the data by daily dollar volume and take the top 'NumberOfSymbols'
|
||||
public static IEnumerable<Symbol> CoarseSelectionFunction(IEnumerable<CoarseFundamental> coarse)
|
||||
{
|
||||
// sort descending by daily dollar volume
|
||||
var sortedByDollarVolume = coarse.OrderByDescending(x => x.DollarVolume);
|
||||
|
||||
// take the top entries from our sorted collection
|
||||
var top = sortedByDollarVolume.Take(NumberOfSymbols);
|
||||
|
||||
// we need to return only the symbol objects
|
||||
return top.Select(x => x.Symbol);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
|
||||
/// </summary>
|
||||
/// <param name="data">Slice object keyed by symbol containing the stock data</param>
|
||||
public override void OnData(Slice slice)
|
||||
{
|
||||
Log($"OnData({UtcTime:o}): Keys: {string.Join(", ", slice.Keys.OrderBy(x => x))}");
|
||||
|
||||
// if we have no changes, do nothing
|
||||
if (_changes == SecurityChanges.None) return;
|
||||
|
||||
// liquidate removed securities
|
||||
foreach (var security in _changes.RemovedSecurities)
|
||||
{
|
||||
if (security.Invested)
|
||||
{
|
||||
Liquidate(security.Symbol);
|
||||
}
|
||||
}
|
||||
|
||||
// we want 1/N allocation in each security in our universe
|
||||
foreach (var security in _changes.AddedSecurities)
|
||||
{
|
||||
SetHoldings(security.Symbol, 1m / NumberOfSymbols);
|
||||
}
|
||||
|
||||
_changes = SecurityChanges.None;
|
||||
}
|
||||
|
||||
// this event fires whenever we have changes to our universe
|
||||
public override void OnSecuritiesChanged(SecurityChanges changes)
|
||||
{
|
||||
_changes = changes;
|
||||
Log($"OnSecuritiesChanged({UtcTime:o}):: {changes}");
|
||||
}
|
||||
|
||||
public override void OnOrderEvent(OrderEvent orderEvent)
|
||||
{
|
||||
Log($"OnOrderEvent({UtcTime:o}):: {orderEvent}");
|
||||
}
|
||||
|
||||
/// <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 => 78088;
|
||||
|
||||
/// <summary>
|
||||
/// Data Points count of the algorithm history
|
||||
/// </summary>
|
||||
public int AlgorithmHistoryDataPoints => 0;
|
||||
|
||||
/// <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", "12"},
|
||||
{"Average Win", "0.63%"},
|
||||
{"Average Loss", "-0.49%"},
|
||||
{"Compounding Annual Return", "-35.851%"},
|
||||
{"Drawdown", "2.700%"},
|
||||
{"Expectancy", "-0.542"},
|
||||
{"Start Equity", "50000"},
|
||||
{"End Equity", "49096.01"},
|
||||
{"Net Profit", "-1.808%"},
|
||||
{"Sharpe Ratio", "-1.989"},
|
||||
{"Sortino Ratio", "-3.359"},
|
||||
{"Probabilistic Sharpe Ratio", "23.563%"},
|
||||
{"Loss Rate", "80%"},
|
||||
{"Win Rate", "20%"},
|
||||
{"Profit-Loss Ratio", "1.29"},
|
||||
{"Alpha", "-0.172"},
|
||||
{"Beta", "1.068"},
|
||||
{"Annual Standard Deviation", "0.141"},
|
||||
{"Annual Variance", "0.02"},
|
||||
{"Information Ratio", "-1.865"},
|
||||
{"Tracking Error", "0.096"},
|
||||
{"Treynor Ratio", "-0.263"},
|
||||
{"Total Fees", "$26.72"},
|
||||
{"Estimated Strategy Capacity", "$630000000.00"},
|
||||
{"Lowest Capacity Asset", "FB V6OIPNZEM8V9"},
|
||||
{"Portfolio Turnover", "24.59%"},
|
||||
{"Drawdown Recovery", "6"},
|
||||
{"OrderListHash", "90b57d40d047eedbff7111d2a73a1290"}
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user