chore: import upstream snapshot with attribution

This commit is contained in:
wehub-resource-sync
2026-07-13 13:02:50 +08:00
commit 0fc60fdcb1
5008 changed files with 910633 additions and 0 deletions
+63
View File
@@ -0,0 +1,63 @@
/*
* 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.UniverseSelection;
using QuantConnect.Python;
namespace QuantConnect.Algorithm.Framework.Alphas
{
/// <summary>
/// Provides a base class for alpha models.
/// </summary>
public class AlphaModel : BasePythonWrapper<AlphaModel>, IAlphaModel, INamedModel
{
/// <summary>
/// Defines a name for a framework model
/// </summary>
public virtual string Name { get; set; }
/// <summary>
/// Initialize new <see cref="AlphaModel"/>
/// </summary>
public AlphaModel()
{
Name = Guid.NewGuid().ToString();
}
/// <summary>
/// Updates this alpha model with the latest data from the algorithm.
/// This is called each time the algorithm receives data for subscribed securities
/// </summary>
/// <param name="algorithm">The algorithm instance</param>
/// <param name="data">The new data available</param>
/// <returns>The new insights generated</returns>
public virtual IEnumerable<Insight> Update(QCAlgorithm algorithm, Slice data)
{
throw new NotImplementedException("Types deriving from 'AlphaModel' must implement the 'IEnumerable<Insight> Update(QCAlgorithm, Slice) method.");
}
/// <summary>
/// Event fired each time the we add/remove securities from the data feed
/// </summary>
/// <param name="algorithm">The algorithm instance that experienced the change in securities</param>
/// <param name="changes">The security additions and removals from the algorithm</param>
public virtual void OnSecuritiesChanged(QCAlgorithm algorithm, SecurityChanges changes)
{
}
}
}
+37
View File
@@ -0,0 +1,37 @@
/*
* 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.
*/
namespace QuantConnect.Algorithm.Framework.Alphas
{
/// <summary>
/// Provides extension methods for alpha models
/// </summary>
public static class AlphaModelExtensions
{
/// <summary>
/// Gets the name of the alpha model
/// </summary>
public static string GetModelName(this IAlphaModel model)
{
var namedModel = model as INamedModel;
if (namedModel != null)
{
return namedModel.Name;
}
return model.GetType().Name;
}
}
}
@@ -0,0 +1,95 @@
/*
* 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 Python.Runtime;
using QuantConnect.Data;
using QuantConnect.Data.UniverseSelection;
using System;
using System.Collections.Generic;
using QuantConnect.Python;
namespace QuantConnect.Algorithm.Framework.Alphas
{
/// <summary>
/// Provides an implementation of <see cref="IAlphaModel"/> that wraps a <see cref="PyObject"/> object
/// </summary>
public class AlphaModelPythonWrapper : AlphaModel
{
/// <summary>
/// Defines a name for a framework model
/// </summary>
public override string Name
{
get
{
using (Py.GIL())
{
// if the model defines a Name property then use that
if (HasAttr(nameof(Name)))
{
return GetProperty<string>(nameof(Name));
}
// if the model does not define a name property, use the python type name
return GetProperty(" __class__").GetAttr("__name__").GetAndDispose<string>();
}
}
}
/// <summary>
/// Constructor for initialising the <see cref="IAlphaModel"/> class with wrapped <see cref="PyObject"/> object
/// </summary>
/// <param name="model">>Model that generates alpha</param>
public AlphaModelPythonWrapper(PyObject model)
{
SetPythonInstance(model, false);
foreach (var attributeName in new[] { "Update", "OnSecuritiesChanged" })
{
if (!HasAttr(attributeName))
{
throw new NotImplementedException($"IAlphaModel.{attributeName} must be implemented. Please implement this missing method on {model.GetPythonType()}");
}
}
var methodName = nameof(SetPythonInstance);
if (HasAttr(methodName))
{
InvokeMethod(methodName, model);
}
}
/// <summary>
/// Updates this alpha model with the latest data from the algorithm.
/// This is called each time the algorithm receives data for subscribed securities
/// </summary>
/// <param name="algorithm">The algorithm instance</param>
/// <param name="data">The new data available</param>
/// <returns>The new insights generated</returns>
public override IEnumerable<Insight> Update(QCAlgorithm algorithm, Slice data)
{
return InvokeMethodAndEnumerate<Insight>(nameof(Update), algorithm, data);
}
/// <summary>
/// Event fired each time the we add/remove securities from the data feed
/// </summary>
/// <param name="algorithm">The algorithm instance that experienced the change in securities</param>
/// <param name="changes">The security additions and removals from the algorithm</param>
public override void OnSecuritiesChanged(QCAlgorithm algorithm, SecurityChanges changes)
{
InvokeVoidMethod(nameof(OnSecuritiesChanged), algorithm, changes);
}
}
}
+126
View File
@@ -0,0 +1,126 @@
/*
* 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 Python.Runtime;
using QuantConnect.Data;
using QuantConnect.Data.UniverseSelection;
using QuantConnect.Util;
namespace QuantConnect.Algorithm.Framework.Alphas
{
/// <summary>
/// Provides an implementation of <see cref="IAlphaModel"/> that combines multiple alpha
/// models into a single alpha model and properly sets each insights 'SourceModel' property.
/// </summary>
public class CompositeAlphaModel : AlphaModel
{
private readonly List<IAlphaModel> _alphaModels = new List<IAlphaModel>();
/// <summary>
/// Initializes a new instance of the <see cref="CompositeAlphaModel"/> class
/// </summary>
/// <param name="alphaModels">The individual alpha models defining this composite model</param>
public CompositeAlphaModel(params IAlphaModel[] alphaModels)
{
if (alphaModels.IsNullOrEmpty())
{
throw new ArgumentException("Must specify at least 1 alpha model for the CompositeAlphaModel");
}
_alphaModels.AddRange(alphaModels);
}
/// <summary>
/// Initializes a new instance of the <see cref="CompositeAlphaModel"/> class
/// </summary>
/// <param name="alphaModels">The individual alpha models defining this composite model</param>
public CompositeAlphaModel(params PyObject[] alphaModels)
{
if (alphaModels.IsNullOrEmpty())
{
throw new ArgumentException("Must specify at least 1 alpha model for the CompositeAlphaModel");
}
foreach (var pyAlphaModel in alphaModels)
{
AddAlpha(pyAlphaModel);
}
}
/// <summary>
/// Updates this alpha model with the latest data from the algorithm.
/// This is called each time the algorithm receives data for subscribed securities.
/// This method patches this call through the each of the wrapped models.
/// </summary>
/// <param name="algorithm">The algorithm instance</param>
/// <param name="data">The new data available</param>
/// <returns>The new insights generated</returns>
public override IEnumerable<Insight> Update(QCAlgorithm algorithm, Slice data)
{
foreach (var model in _alphaModels)
{
var name = model.GetModelName();
foreach (var insight in model.Update(algorithm, data))
{
if (string.IsNullOrEmpty(insight.SourceModel))
{
// set the source model name if not already set
insight.SourceModel = name;
}
yield return insight;
}
}
}
/// <summary>
/// Event fired each time the we add/remove securities from the data feed.
/// This method patches this call through the each of the wrapped models.
/// </summary>
/// <param name="algorithm">The algorithm instance that experienced the change in securities</param>
/// <param name="changes">The security additions and removals from the algorithm</param>
public override void OnSecuritiesChanged(QCAlgorithm algorithm, SecurityChanges changes)
{
foreach (var model in _alphaModels)
{
model.OnSecuritiesChanged(algorithm, changes);
}
}
/// <summary>
/// Adds a new <see cref="AlphaModel"/>
/// </summary>
/// <param name="alphaModel">The alpha model to add</param>
public void AddAlpha(IAlphaModel alphaModel)
{
_alphaModels.Add(alphaModel);
}
/// <summary>
/// Adds a new <see cref="AlphaModel"/>
/// </summary>
/// <param name="pyAlphaModel">The alpha model to add</param>
public void AddAlpha(PyObject pyAlphaModel)
{
var alphaModel = PythonUtil.CreateInstanceOrWrapper<IAlphaModel>(
pyAlphaModel,
py => new AlphaModelPythonWrapper(py)
);
_alphaModels.Add(alphaModel);
}
}
}
+35
View File
@@ -0,0 +1,35 @@
/*
* 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.Collections.Generic;
using QuantConnect.Data;
namespace QuantConnect.Algorithm.Framework.Alphas
{
/// <summary>
/// Algorithm framework model that produces insights
/// </summary>
public interface IAlphaModel : INotifiedSecurityChanges
{
/// <summary>
/// Updates this alpha model with the latest data from the algorithm.
/// This is called each time the algorithm receives data for subscribed securities
/// </summary>
/// <param name="algorithm">The algorithm instance</param>
/// <param name="data">The new data available</param>
/// <returns>The new insights generated</returns>
IEnumerable<Insight> Update(QCAlgorithm algorithm, Slice data);
}
}
+31
View File
@@ -0,0 +1,31 @@
/*
* 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.
*/
namespace QuantConnect.Algorithm.Framework.Alphas
{
/// <summary>
/// Provides a marker interface allowing models to define their own names.
/// If not specified, the framework will use the model's type name.
/// Implementation of this is not required unless you plan on running multiple models
/// of the same type w/ different parameters.
/// </summary>
public interface INamedModel
{
/// <summary>
/// Defines a name for a framework model
/// </summary>
string Name { get; }
}
}
+39
View File
@@ -0,0 +1,39 @@
/*
* 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.Collections.Generic;
using System.Linq;
using QuantConnect.Data;
namespace QuantConnect.Algorithm.Framework.Alphas
{
/// <summary>
/// Provides a null implementation of an alpha model
/// </summary>
public class NullAlphaModel : AlphaModel
{
/// <summary>
/// Updates this alpha model with the latest data from the algorithm.
/// This is called each time the algorithm receives data for subscribed securities
/// </summary>
/// <param name="algorithm">The algorithm instance</param>
/// <param name="data">The new data available</param>
/// <returns>The new insights generated</returns>
public override IEnumerable<Insight> Update(QCAlgorithm algorithm, Slice data)
{
return Enumerable.Empty<Insight>();
}
}
}
+26
View File
@@ -0,0 +1,26 @@
# 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 *
class NullAlphaModel(AlphaModel):
'''Provides a null implementation of an alpha model'''
def update(self, algorithm, data):
''' Determines an insight for each security based on it's current MACD signal
Args:
algorithm: The algorithm instance
data: The new data available
Returns:
The new insights generated'''
return []