1919using QuantConnect . Algorithm . Framework . Portfolio ;
2020using QuantConnect . Algorithm . Framework . Risk ;
2121using QuantConnect . Algorithm . Framework . Selection ;
22+ using QuantConnect . Util ;
2223
2324namespace QuantConnect . Algorithm
2425{
@@ -31,15 +32,10 @@ public partial class QCAlgorithm
3132 [ DocumentationAttribute ( AlgorithmFramework ) ]
3233 public void SetAlpha ( PyObject alpha )
3334 {
34- IAlphaModel model ;
35- if ( alpha . TryConvert ( out model ) )
36- {
37- SetAlpha ( model ) ;
38- }
39- else
40- {
41- Alpha = new AlphaModelPythonWrapper ( alpha ) ;
42- }
35+ Alpha = PythonUtil . CreateInstanceOrWrapper < IAlphaModel > (
36+ alpha ,
37+ py => new AlphaModelPythonWrapper ( py )
38+ ) ;
4339 }
4440
4541 /// <summary>
@@ -49,15 +45,11 @@ public void SetAlpha(PyObject alpha)
4945 [ DocumentationAttribute ( AlgorithmFramework ) ]
5046 public void AddAlpha ( PyObject alpha )
5147 {
52- IAlphaModel model ;
53- if ( alpha . TryConvert ( out model ) )
54- {
55- AddAlpha ( model ) ;
56- }
57- else
58- {
59- AddAlpha ( new AlphaModelPythonWrapper ( alpha ) ) ;
60- }
48+ var model = PythonUtil . CreateInstanceOrWrapper < IAlphaModel > (
49+ alpha ,
50+ py => new AlphaModelPythonWrapper ( py )
51+ ) ;
52+ AddAlpha ( model ) ;
6153 }
6254
6355 /// <summary>
@@ -68,15 +60,10 @@ public void AddAlpha(PyObject alpha)
6860 [ DocumentationAttribute ( TradingAndOrders ) ]
6961 public void SetExecution ( PyObject execution )
7062 {
71- IExecutionModel model ;
72- if ( execution . TryConvert ( out model ) )
73- {
74- SetExecution ( model ) ;
75- }
76- else
77- {
78- Execution = new ExecutionModelPythonWrapper ( execution ) ;
79- }
63+ Execution = PythonUtil . CreateInstanceOrWrapper < IExecutionModel > (
64+ execution ,
65+ py => new ExecutionModelPythonWrapper ( py )
66+ ) ;
8067 }
8168
8269 /// <summary>
@@ -87,15 +74,10 @@ public void SetExecution(PyObject execution)
8774 [ DocumentationAttribute ( TradingAndOrders ) ]
8875 public void SetPortfolioConstruction ( PyObject portfolioConstruction )
8976 {
90- IPortfolioConstructionModel model ;
91- if ( portfolioConstruction . TryConvert ( out model ) )
92- {
93- SetPortfolioConstruction ( model ) ;
94- }
95- else
96- {
97- PortfolioConstruction = new PortfolioConstructionModelPythonWrapper ( portfolioConstruction ) ;
98- }
77+ PortfolioConstruction = PythonUtil . CreateInstanceOrWrapper < IPortfolioConstructionModel > (
78+ portfolioConstruction ,
79+ py => new PortfolioConstructionModelPythonWrapper ( py )
80+ ) ;
9981 }
10082
10183 /// <summary>
@@ -106,12 +88,10 @@ public void SetPortfolioConstruction(PyObject portfolioConstruction)
10688 [ DocumentationAttribute ( Universes ) ]
10789 public void SetUniverseSelection ( PyObject universeSelection )
10890 {
109- IUniverseSelectionModel model ;
110- if ( ! universeSelection . TryConvert ( out model ) )
111- {
112- model = new UniverseSelectionModelPythonWrapper ( universeSelection ) ;
113- }
114- SetUniverseSelection ( model ) ;
91+ UniverseSelection = PythonUtil . CreateInstanceOrWrapper < IUniverseSelectionModel > (
92+ universeSelection ,
93+ py => new UniverseSelectionModelPythonWrapper ( py )
94+ ) ;
11595 }
11696
11797 /// <summary>
@@ -122,11 +102,10 @@ public void SetUniverseSelection(PyObject universeSelection)
122102 [ DocumentationAttribute ( Universes ) ]
123103 public void AddUniverseSelection ( PyObject universeSelection )
124104 {
125- IUniverseSelectionModel model ;
126- if ( ! universeSelection . TryConvert ( out model ) )
127- {
128- model = new UniverseSelectionModelPythonWrapper ( universeSelection ) ;
129- }
105+ var model = PythonUtil . CreateInstanceOrWrapper < IUniverseSelectionModel > (
106+ universeSelection ,
107+ py => new UniverseSelectionModelPythonWrapper ( py )
108+ ) ;
130109 AddUniverseSelection ( model ) ;
131110 }
132111
@@ -138,15 +117,10 @@ public void AddUniverseSelection(PyObject universeSelection)
138117 [ DocumentationAttribute ( TradingAndOrders ) ]
139118 public void SetRiskManagement ( PyObject riskManagement )
140119 {
141- IRiskManagementModel model ;
142- if ( riskManagement . TryConvert ( out model ) )
143- {
144- SetRiskManagement ( model ) ;
145- }
146- else
147- {
148- RiskManagement = new RiskManagementModelPythonWrapper ( riskManagement ) ;
149- }
120+ RiskManagement = PythonUtil . CreateInstanceOrWrapper < IRiskManagementModel > (
121+ riskManagement ,
122+ py => new RiskManagementModelPythonWrapper ( py )
123+ ) ;
150124 }
151125
152126 /// <summary>
@@ -157,11 +131,10 @@ public void SetRiskManagement(PyObject riskManagement)
157131 [ DocumentationAttribute ( TradingAndOrders ) ]
158132 public void AddRiskManagement ( PyObject riskManagement )
159133 {
160- IRiskManagementModel model ;
161- if ( ! riskManagement . TryConvert ( out model ) )
162- {
163- model = new RiskManagementModelPythonWrapper ( riskManagement ) ;
164- }
134+ var model = PythonUtil . CreateInstanceOrWrapper < IRiskManagementModel > (
135+ riskManagement ,
136+ py => new RiskManagementModelPythonWrapper ( py )
137+ ) ;
165138 AddRiskManagement ( model ) ;
166139 }
167140 }
0 commit comments