-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Expand file tree
/
Copy pathQCAlgorithm.Framework.Python.cs
More file actions
178 lines (164 loc) · 6.92 KB
/
Copy pathQCAlgorithm.Framework.Python.cs
File metadata and controls
178 lines (164 loc) · 6.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
* 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.Algorithm.Framework.Alphas;
using QuantConnect.Algorithm.Framework.Execution;
using QuantConnect.Algorithm.Framework.Portfolio;
using QuantConnect.Algorithm.Framework.Risk;
using QuantConnect.Algorithm.Framework.Selection;
using QuantConnect.Util;
namespace QuantConnect.Algorithm
{
public partial class QCAlgorithm
{
/// <summary>
/// Sets the alpha model
/// </summary>
/// <param name="alpha">Model that generates alpha</param>
[DocumentationAttribute(AlgorithmFramework)]
public void SetAlpha(PyObject alpha)
{
// None is accepted as the null model, e.g. to disable a model a template set up
if (alpha is null || alpha.IsNone())
{
SetAlpha(new NullAlphaModel());
return;
}
Alpha = PythonUtil.CreateInstanceOrWrapper<IAlphaModel>(
alpha,
py => new AlphaModelPythonWrapper(py)
);
}
/// <summary>
/// Adds a new alpha model
/// </summary>
/// <param name="alpha">Model that generates alpha to add</param>
[DocumentationAttribute(AlgorithmFramework)]
public void AddAlpha(PyObject alpha)
{
var model = PythonUtil.CreateInstanceOrWrapper<IAlphaModel>(
alpha,
py => new AlphaModelPythonWrapper(py)
);
AddAlpha(model);
}
/// <summary>
/// Sets the execution model
/// </summary>
/// <param name="execution">Model defining how to execute trades to reach a portfolio target</param>
[DocumentationAttribute(AlgorithmFramework)]
[DocumentationAttribute(TradingAndOrders)]
public void SetExecution(PyObject execution)
{
// None is accepted as the null model, e.g. to disable a model a template set up
if (execution is null || execution.IsNone())
{
SetExecution(new NullExecutionModel());
return;
}
Execution = PythonUtil.CreateInstanceOrWrapper<IExecutionModel>(
execution,
py => new ExecutionModelPythonWrapper(py)
);
}
/// <summary>
/// Sets the portfolio construction model
/// </summary>
/// <param name="portfolioConstruction">Model defining how to build a portfolio from alphas</param>
[DocumentationAttribute(AlgorithmFramework)]
[DocumentationAttribute(TradingAndOrders)]
public void SetPortfolioConstruction(PyObject portfolioConstruction)
{
// None is accepted as the null model, e.g. to disable a model a template set up
if (portfolioConstruction is null || portfolioConstruction.IsNone())
{
SetPortfolioConstruction(new NullPortfolioConstructionModel());
return;
}
PortfolioConstruction = PythonUtil.CreateInstanceOrWrapper<IPortfolioConstructionModel>(
portfolioConstruction,
py => new PortfolioConstructionModelPythonWrapper(py)
);
}
/// <summary>
/// Sets the universe selection model
/// </summary>
/// <param name="universeSelection">Model defining universes for the algorithm</param>
[DocumentationAttribute(AlgorithmFramework)]
[DocumentationAttribute(Universes)]
public void SetUniverseSelection(PyObject universeSelection)
{
// None is accepted as the null model, e.g. to disable a model a template set up
if (universeSelection is null || universeSelection.IsNone())
{
SetUniverseSelection(new NullUniverseSelectionModel());
return;
}
UniverseSelection = PythonUtil.CreateInstanceOrWrapper<IUniverseSelectionModel>(
universeSelection,
py => new UniverseSelectionModelPythonWrapper(py)
);
}
/// <summary>
/// Adds a new universe selection model
/// </summary>
/// <param name="universeSelection">Model defining universes for the algorithm to add</param>
[DocumentationAttribute(AlgorithmFramework)]
[DocumentationAttribute(Universes)]
public void AddUniverseSelection(PyObject universeSelection)
{
var model = PythonUtil.CreateInstanceOrWrapper<IUniverseSelectionModel>(
universeSelection,
py => new UniverseSelectionModelPythonWrapper(py)
);
AddUniverseSelection(model);
}
/// <summary>
/// Sets the risk management model
/// </summary>
/// <param name="riskManagement">Model defining how risk is managed</param>
[DocumentationAttribute(AlgorithmFramework)]
[DocumentationAttribute(TradingAndOrders)]
public void SetRiskManagement(PyObject riskManagement)
{
// None is accepted as the null model, e.g. to disable a model a template set up.
// Without this, set_risk_management(None) failed with
// "IRiskManagementModel must be fully implemented. Please implement these missing methods on NoneType: ManageRisk"
if (riskManagement is null || riskManagement.IsNone())
{
SetRiskManagement(new NullRiskManagementModel());
return;
}
RiskManagement = PythonUtil.CreateInstanceOrWrapper<IRiskManagementModel>(
riskManagement,
py => new RiskManagementModelPythonWrapper(py)
);
}
/// <summary>
/// Adds a new risk management model
/// </summary>
/// <param name="riskManagement">Model defining how risk is managed to add</param>
[DocumentationAttribute(AlgorithmFramework)]
[DocumentationAttribute(TradingAndOrders)]
public void AddRiskManagement(PyObject riskManagement)
{
var model = PythonUtil.CreateInstanceOrWrapper<IRiskManagementModel>(
riskManagement,
py => new RiskManagementModelPythonWrapper(py)
);
AddRiskManagement(model);
}
}
}