Skip to content

Commit 2f05142

Browse files
committed
Replace manual TryConvert checks with CreateInstanceOrWrapper helper
1 parent 302b613 commit 2f05142

File tree

13 files changed

+99
-134
lines changed

13 files changed

+99
-134
lines changed

Algorithm/Alphas/CompositeAlphaModel.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,10 @@ public void AddAlpha(IAlphaModel alphaModel)
116116
/// <param name="pyAlphaModel">The alpha model to add</param>
117117
public void AddAlpha(PyObject pyAlphaModel)
118118
{
119-
IAlphaModel alphaModel;
120-
if (!pyAlphaModel.TryConvert(out alphaModel))
121-
{
122-
alphaModel = new AlphaModelPythonWrapper(pyAlphaModel);
123-
}
119+
var alphaModel = PythonUtil.CreateInstanceOrWrapper<IAlphaModel>(
120+
pyAlphaModel,
121+
py => new AlphaModelPythonWrapper(py)
122+
);
124123
_alphaModels.Add(alphaModel);
125124
}
126125
}

Algorithm/QCAlgorithm.Framework.Python.cs

Lines changed: 34 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
using QuantConnect.Algorithm.Framework.Portfolio;
2020
using QuantConnect.Algorithm.Framework.Risk;
2121
using QuantConnect.Algorithm.Framework.Selection;
22+
using QuantConnect.Util;
2223

2324
namespace 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
}

Algorithm/QCAlgorithm.Python.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1372,7 +1372,7 @@ public void SetBenchmark(PyObject benchmark)
13721372
[DocumentationAttribute(Modeling)]
13731373
public void SetBrokerageModel(PyObject model)
13741374
{
1375-
var brokerageModel = PythonUtil.CreateModelOrWrapper<IBrokerageModel>(
1375+
var brokerageModel = PythonUtil.CreateInstanceOrWrapper<IBrokerageModel>(
13761376
model,
13771377
py => new BrokerageModelPythonWrapper(py)
13781378
);
@@ -1390,7 +1390,7 @@ public void SetBrokerageModel(PyObject model)
13901390
[DocumentationAttribute(Logging)]
13911391
public void SetBrokerageMessageHandler(PyObject handler)
13921392
{
1393-
var brokerageMessageHandler = PythonUtil.CreateModelOrWrapper<IBrokerageMessageHandler>(
1393+
var brokerageMessageHandler = PythonUtil.CreateInstanceOrWrapper<IBrokerageMessageHandler>(
13941394
handler,
13951395
py => new BrokerageMessageHandlerPythonWrapper(py)
13961396
);
@@ -1404,7 +1404,7 @@ public void SetBrokerageMessageHandler(PyObject handler)
14041404
[DocumentationAttribute(Modeling)]
14051405
public void SetRiskFreeInterestRateModel(PyObject model)
14061406
{
1407-
var riskFreeInterestRateModel = PythonUtil.CreateModelOrWrapper<IRiskFreeInterestRateModel>(
1407+
var riskFreeInterestRateModel = PythonUtil.CreateInstanceOrWrapper<IRiskFreeInterestRateModel>(
14081408
model,
14091409
py => new RiskFreeInterestRateModelPythonWrapper(py)
14101410
);

Algorithm/Risk/CompositeRiskManagementModel.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public CompositeRiskManagementModel(params IRiskManagementModel[] riskManagement
4949
/// Initializes a new instance of the <see cref="CompositeRiskManagementModel"/> class
5050
/// </summary>
5151
/// <param name="riskManagementModels">The individual risk management models defining this composite model</param>
52-
public CompositeRiskManagementModel(IEnumerable<IRiskManagementModel>riskManagementModels)
52+
public CompositeRiskManagementModel(IEnumerable<IRiskManagementModel> riskManagementModels)
5353
{
5454
foreach (var riskManagementModel in riskManagementModels)
5555
{
@@ -129,11 +129,10 @@ public void AddRiskManagement(IRiskManagementModel riskManagementModel)
129129
/// <param name="pyRiskManagementModel">The risk management model to add</param>
130130
public void AddRiskManagement(PyObject pyRiskManagementModel)
131131
{
132-
IRiskManagementModel riskManagementModel;
133-
if (!pyRiskManagementModel.TryConvert(out riskManagementModel))
134-
{
135-
riskManagementModel = new RiskManagementModelPythonWrapper(pyRiskManagementModel);
136-
}
132+
var riskManagementModel = PythonUtil.CreateInstanceOrWrapper<IRiskManagementModel>(
133+
pyRiskManagementModel,
134+
py => new RiskManagementModelPythonWrapper(py)
135+
);
137136
_riskManagementModels.Add(riskManagementModel);
138137
}
139138
}

Common/Algorithm/Framework/Alphas/Analysis/InsightManager.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using Python.Runtime;
1818
using QuantConnect.Interfaces;
1919
using System.Collections.Generic;
20+
using QuantConnect.Util;
2021

2122
namespace QuantConnect.Algorithm.Framework.Alphas.Analysis
2223
{
@@ -61,15 +62,10 @@ public void SetInsightScoreFunction(IInsightScoreFunction insightScoreFunction)
6162
/// <param name="insightScoreFunction">Model that scores insights</param>
6263
public void SetInsightScoreFunction(PyObject insightScoreFunction)
6364
{
64-
IInsightScoreFunction model;
65-
if (insightScoreFunction.TryConvert(out model))
66-
{
67-
SetInsightScoreFunction(model);
68-
}
69-
else
70-
{
71-
_insightScoreFunction = new InsightScoreFunctionPythonWrapper(insightScoreFunction);
72-
}
65+
_insightScoreFunction = PythonUtil.CreateInstanceOrWrapper<IInsightScoreFunction>(
66+
insightScoreFunction,
67+
py => new InsightScoreFunctionPythonWrapper(py)
68+
);
7369
}
7470

7571
/// <summary>

Common/Algorithm/Framework/Portfolio/SignalExports/SignalExportManager.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,10 @@ public void AddSignalExportProvider(ISignalExportTarget signalExport)
8484
/// <param name="signalExport">Signal export provider</param>
8585
public void AddSignalExportProvider(PyObject signalExport)
8686
{
87-
if (!signalExport.TryConvert<ISignalExportTarget>(out var managedSignalExport))
88-
{
89-
managedSignalExport = new SignalExportTargetPythonWrapper(signalExport);
90-
}
87+
var managedSignalExport = PythonUtil.CreateInstanceOrWrapper<ISignalExportTarget>(
88+
signalExport,
89+
py => new SignalExportTargetPythonWrapper(py)
90+
);
9191
AddSignalExportProvider(managedSignalExport);
9292
}
9393

@@ -172,7 +172,7 @@ public bool SetTargetPortfolio(params PortfolioTarget[] portfolioTargets)
172172
}
173173

174174
if (_signalExports.IsNullOrEmpty())
175-
{
175+
{
176176
return false;
177177
}
178178

Common/Data/SubscriptionManager.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -216,11 +216,10 @@ public void AddConsolidator(Symbol symbol, IDataConsolidator consolidator, TickT
216216
/// <param name="pyConsolidator">The custom python consolidator</param>
217217
public void AddConsolidator(Symbol symbol, PyObject pyConsolidator)
218218
{
219-
if (!pyConsolidator.TryConvert(out IDataConsolidator consolidator))
220-
{
221-
consolidator = new DataConsolidatorPythonWrapper(pyConsolidator);
222-
}
223-
219+
var consolidator = PythonUtil.CreateInstanceOrWrapper<IDataConsolidator>(
220+
pyConsolidator,
221+
py => new DataConsolidatorPythonWrapper(py)
222+
);
224223
AddConsolidator(symbol, consolidator);
225224
}
226225

Common/Python/DividendYieldModelPythonWrapper.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using System;
1717
using Python.Runtime;
1818
using QuantConnect.Data;
19+
using QuantConnect.Util;
1920

2021
namespace QuantConnect.Python
2122
{
@@ -62,11 +63,10 @@ public decimal GetDividendYield(DateTime date, decimal securityPrice)
6263
/// <returns>The converted <see cref="IDividendYieldModel"/> instance</returns>
6364
public static IDividendYieldModel FromPyObject(PyObject model)
6465
{
65-
if (!model.TryConvert(out IDividendYieldModel dividendYieldModel))
66-
{
67-
dividendYieldModel = new DividendYieldModelPythonWrapper(model);
68-
}
69-
66+
var dividendYieldModel = PythonUtil.CreateInstanceOrWrapper<IDividendYieldModel>(
67+
model,
68+
py => new DividendYieldModelPythonWrapper(py)
69+
);
7070
return dividendYieldModel;
7171
}
7272
}

Common/Python/RiskFreeInterestRateModelPythonWrapper.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using System;
1717
using Python.Runtime;
1818
using QuantConnect.Data;
19+
using QuantConnect.Util;
1920

2021
namespace QuantConnect.Python
2122
{
@@ -50,11 +51,10 @@ public decimal GetInterestRate(DateTime date)
5051
/// <returns>The converted <see cref="IRiskFreeInterestRateModel"/> instance</returns>
5152
public static IRiskFreeInterestRateModel FromPyObject(PyObject model)
5253
{
53-
if (!model.TryConvert(out IRiskFreeInterestRateModel riskFreeInterestRateModel))
54-
{
55-
riskFreeInterestRateModel = new RiskFreeInterestRateModelPythonWrapper(model);
56-
}
57-
54+
var riskFreeInterestRateModel = PythonUtil.CreateInstanceOrWrapper<IRiskFreeInterestRateModel>(
55+
model,
56+
py => new RiskFreeInterestRateModelPythonWrapper(py)
57+
);
5858
return riskFreeInterestRateModel;
5959
}
6060
}

Common/Securities/Option/Option.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ public IDerivativeSecurityFilter<OptionUniverse> ContractFilter
462462
/// <param name="pyObject">The option assignment model to use</param>
463463
public void SetOptionAssignmentModel(PyObject pyObject)
464464
{
465-
OptionAssignmentModel = PythonUtil.CreateModelOrWrapper<IOptionAssignmentModel>(
465+
OptionAssignmentModel = PythonUtil.CreateInstanceOrWrapper<IOptionAssignmentModel>(
466466
pyObject,
467467
py => new OptionAssignmentModelPythonWrapper(py)
468468
);
@@ -483,7 +483,7 @@ public void SetOptionAssignmentModel(IOptionAssignmentModel optionAssignmentMode
483483
/// <param name="pyObject">The option exercise model to use</param>
484484
public void SetOptionExerciseModel(PyObject pyObject)
485485
{
486-
OptionExerciseModel = PythonUtil.CreateModelOrWrapper<IOptionExerciseModel>(
486+
OptionExerciseModel = PythonUtil.CreateInstanceOrWrapper<IOptionExerciseModel>(
487487
pyObject,
488488
py => new OptionExerciseModelPythonWrapper(py)
489489
);

0 commit comments

Comments
 (0)