Skip to content

Commit 2b9e313

Browse files
committed
Add support for other methods
1 parent 6ef70a3 commit 2b9e313

File tree

7 files changed

+504
-100
lines changed

7 files changed

+504
-100
lines changed

Common/Brokerages/DefaultBrokerageModel.cs

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -310,18 +310,6 @@ public virtual ISettlementModel GetSettlementModel(Security security)
310310
return new ImmediateSettlementModel();
311311
}
312312

313-
/// <summary>
314-
/// Gets a new settlement model for the security
315-
/// </summary>
316-
/// <param name="security">The security to get a settlement model for</param>
317-
/// <param name="accountType">The account type</param>
318-
/// <returns>The settlement model for this brokerage</returns>
319-
[Obsolete("Flagged deprecated and will remove December 1st 2018")]
320-
public ISettlementModel GetSettlementModel(Security security, AccountType accountType)
321-
{
322-
return GetSettlementModel(security);
323-
}
324-
325313
/// <summary>
326314
/// Gets a new buying power model for the security, returning the default model with the security's configured leverage.
327315
/// For cash accounts, leverage = 1 is used.
@@ -370,18 +358,6 @@ public virtual IMarginInterestRateModel GetMarginInterestRateModel(Security secu
370358
return MarginInterestRateModel.Null;
371359
}
372360

373-
/// <summary>
374-
/// Gets a new buying power model for the security
375-
/// </summary>
376-
/// <param name="security">The security to get a buying power model for</param>
377-
/// <param name="accountType">The account type</param>
378-
/// <returns>The buying power model for this brokerage/security</returns>
379-
[Obsolete("Flagged deprecated and will remove December 1st 2018")]
380-
public IBuyingPowerModel GetBuyingPowerModel(Security security, AccountType accountType)
381-
{
382-
return GetBuyingPowerModel(security);
383-
}
384-
385361
/// <summary>
386362
/// Checks if the order quantity is valid, it means, the order size is bigger than the minimum size allowed
387363
/// </summary>

Common/Brokerages/IBrokerageModel.cs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -146,31 +146,13 @@ decimal RequiredFreeBuyingPowerPercent
146146
/// <returns>The margin interest rate model for this brokerage</returns>
147147
IMarginInterestRateModel GetMarginInterestRateModel(Security security);
148148

149-
/// <summary>
150-
/// Gets a new settlement model for the security
151-
/// </summary>
152-
/// <param name="security">The security to get a settlement model for</param>
153-
/// <param name="accountType">The account type</param>
154-
/// <returns>The settlement model for this brokerage</returns>
155-
[Obsolete("Flagged deprecated and will remove December 1st 2018")]
156-
ISettlementModel GetSettlementModel(Security security, AccountType accountType);
157-
158149
/// <summary>
159150
/// Gets a new buying power model for the security
160151
/// </summary>
161152
/// <param name="security">The security to get a buying power model for</param>
162153
/// <returns>The buying power model for this brokerage/security</returns>
163154
IBuyingPowerModel GetBuyingPowerModel(Security security);
164155

165-
/// <summary>
166-
/// Gets a new buying power model for the security
167-
/// </summary>
168-
/// <param name="security">The security to get a buying power model for</param>
169-
/// <param name="accountType">The account type</param>
170-
/// <returns>The buying power model for this brokerage/security</returns>
171-
[Obsolete("Flagged deprecated and will remove December 1st 2018")]
172-
IBuyingPowerModel GetBuyingPowerModel(Security security, AccountType accountType);
173-
174156
/// <summary>
175157
/// Gets the shortable provider
176158
/// </summary>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3+
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using Python.Runtime;
17+
using QuantConnect.Benchmarks;
18+
using System;
19+
20+
namespace QuantConnect.Python
21+
{
22+
/// <summary>
23+
/// Provides an implementation of <see cref="IBenchmark"/> that wraps a <see cref="PyObject"/> object
24+
/// </summary>
25+
public class BenchmarkPythonWrapper : IBenchmark
26+
{
27+
private readonly dynamic _model;
28+
29+
/// <summary>
30+
/// Constructor for initialising the <see cref="BenchmarkPythonWrapper"/> class with wrapped <see cref="PyObject"/> object
31+
/// </summary>
32+
/// <param name="model">Models brokerage transactions, fees, and order</param>
33+
public BenchmarkPythonWrapper(PyObject model)
34+
{
35+
_model = model;
36+
}
37+
38+
/// <summary>
39+
/// Evaluates this benchmark at the specified time using the method defined in the Python class
40+
/// </summary>
41+
/// <param name="time">The time to evaluate the benchmark at</param>
42+
/// <returns>The value of the benchmark at the specified time</returns>
43+
public decimal Evaluate(DateTime time)
44+
{
45+
using (Py.GIL())
46+
{
47+
return (_model.Evaluate(time) as PyObject).GetAndDispose<decimal>();
48+
}
49+
}
50+
}
51+
}

Common/Python/BrokerageModelPythonWrapper.cs

Lines changed: 45 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
using QuantConnect.Benchmarks;
2020
using QuantConnect.Brokerages;
2121
using QuantConnect.Data.Market;
22+
using QuantConnect.Data.Shortable;
2223
using QuantConnect.Interfaces;
2324
using QuantConnect.Orders;
2425
using QuantConnect.Orders.Fees;
@@ -195,7 +196,12 @@ public IBenchmark GetBenchmark(SecurityManager securities)
195196
{
196197
using (Py.GIL())
197198
{
198-
return (_model.GetBenchmark(securities) as PyObject).GetAndDispose<IBenchmark>();
199+
var benchmark = _model.GetBenchmark(securities) as PyObject;
200+
if (benchmark.TryConvert<IBenchmark>(out var csharpBenchmark))
201+
{
202+
return csharpBenchmark;
203+
}
204+
return new BenchmarkPythonWrapper(benchmark);
199205
}
200206
}
201207

@@ -208,7 +214,12 @@ public IFeeModel GetFeeModel(Security security)
208214
{
209215
using (Py.GIL())
210216
{
211-
return (_model.GetFeeModel(security) as PyObject).GetAndDispose<IFeeModel>();
217+
var feeModel = _model.GetFeeModel(security) as PyObject;
218+
if (feeModel.TryConvert<IFeeModel>(out var csharpFeeModel))
219+
{
220+
return csharpFeeModel;
221+
}
222+
return new FeeModelPythonWrapper(feeModel);
212223
}
213224
}
214225

@@ -224,16 +235,9 @@ public IFillModel GetFillModel(Security security)
224235
var fillModel = _model.GetFillModel(security) as PyObject;
225236
if (fillModel.TryConvert<IFillModel>(out var csharpFillModel))
226237
{
227-
return (_model.GetFillModel(security) as PyObject).GetAndDispose<FillModel>();
228-
}
229-
else if (Extensions.TryConvert<IFillModel>(fillModel, out _, allowPythonDerivative: true))
230-
{
231-
return (new FillModelPythonWrapper(fillModel));
232-
}
233-
else
234-
{
235-
throw new Exception($@"{_model.__class__.__name__} Fill Model type is not supported!");
238+
return csharpFillModel;
236239
}
240+
return (new FillModelPythonWrapper(fillModel));
237241
}
238242
}
239243

@@ -259,23 +263,12 @@ public ISettlementModel GetSettlementModel(Security security)
259263
{
260264
using (Py.GIL())
261265
{
262-
return (_model.GetSettlementModel(security) as PyObject).GetAndDispose<ISettlementModel>();
263-
}
264-
}
265-
266-
/// <summary>
267-
/// Gets a new settlement model for the security
268-
/// </summary>
269-
/// <param name="security">The security to get a settlement model for</param>
270-
/// <param name="accountType">The account type</param>
271-
/// <returns>The settlement model for this brokerage</returns>
272-
[Obsolete("Flagged deprecated and will remove December 1st 2018")]
273-
public ISettlementModel GetSettlementModel(Security security, AccountType accountType)
274-
{
275-
using (Py.GIL())
276-
{
277-
return (_model.GetSettlementModel(security, accountType)
278-
as PyObject).GetAndDispose<ISettlementModel>();
266+
var settlementModel = _model.GetSettlementModel(security) as PyObject;
267+
if (settlementModel.TryConvert<ISettlementModel>(out var csharpSettlementModel))
268+
{
269+
return csharpSettlementModel;
270+
}
271+
return new SettlementModelPythonWrapper(settlementModel);
279272
}
280273
}
281274

@@ -288,7 +281,12 @@ public ISlippageModel GetSlippageModel(Security security)
288281
{
289282
using (Py.GIL())
290283
{
291-
return (_model.GetSlippageModel(security) as PyObject).GetAndDispose<ISlippageModel>();
284+
var slippageModel = _model.GetSlippageModel(security) as PyObject;
285+
if (slippageModel.TryConvert<ISlippageModel>(out var csharpSlippageModel))
286+
{
287+
return csharpSlippageModel;
288+
}
289+
return new SlippageModelPythonWrapper(slippageModel);
292290
}
293291
}
294292

@@ -317,23 +315,12 @@ public IBuyingPowerModel GetBuyingPowerModel(Security security)
317315
{
318316
using (Py.GIL())
319317
{
320-
return (_model.GetBuyingPowerModel(security) as PyObject).GetAndDispose<IBuyingPowerModel>();
321-
}
322-
}
323-
324-
/// <summary>
325-
/// Gets a new buying power model for the security
326-
/// </summary>
327-
/// <param name="security">The security to get a buying power model for</param>
328-
/// <param name="accountType">The account type</param>
329-
/// <returns>The buying power model for this brokerage/security</returns>
330-
[Obsolete("Flagged deprecated and will remove December 1st 2018")]
331-
public IBuyingPowerModel GetBuyingPowerModel(Security security, AccountType accountType)
332-
{
333-
using (Py.GIL())
334-
{
335-
return (_model.GetBuyingPowerModel(security, accountType)
336-
as PyObject).GetAndDispose<IBuyingPowerModel>();
318+
var buyingPowerModel = _model.GetBuyingPowerModel(security) as PyObject;
319+
if (buyingPowerModel.TryConvert<IBuyingPowerModel>(out var csharpBuyingPowerModel))
320+
{
321+
return csharpBuyingPowerModel;
322+
}
323+
return new BuyingPowerModelPythonWrapper(buyingPowerModel);
337324
}
338325
}
339326

@@ -345,7 +332,12 @@ public IShortableProvider GetShortableProvider(Security security)
345332
{
346333
using (Py.GIL())
347334
{
348-
return (_model.GetShortableProvider(security) as PyObject).GetAndDispose<IShortableProvider>();
335+
var shortableProvider = _model.GetShortableProvider(security) as PyObject;
336+
if (shortableProvider.TryConvert<IShortableProvider>(out var csharpShortableProvider))
337+
{
338+
return csharpShortableProvider;
339+
}
340+
return new ShortableProviderPythonWrapper(shortableProvider);
349341
}
350342
}
351343

@@ -370,7 +362,12 @@ public IMarginInterestRateModel GetMarginInterestRateModel(Security security)
370362
{
371363
using (Py.GIL())
372364
{
373-
return (_model.GetMarginInterestRateModel(security) as PyObject).GetAndDispose<IMarginInterestRateModel>();
365+
var marginInterestRateModel = _model.GetMarginInterestRateModel(security) as PyObject;
366+
if (marginInterestRateModel.TryConvert<IMarginInterestRateModel>(out var csharpBuyingPowerModel))
367+
{
368+
return csharpBuyingPowerModel;
369+
}
370+
return new MarginInterestRateModelPythonWrapper(marginInterestRateModel);
374371
}
375372
}
376373
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using Python.Runtime;
2+
using QuantConnect.Securities;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace QuantConnect.Python
10+
{
11+
/// <summary>
12+
/// Provides an implementation of <see cref="ISettlementModel"/> that wraps a <see cref="PyObject"/> object
13+
/// </summary>
14+
public class SettlementModelPythonWrapper : ISettlementModel
15+
{
16+
private readonly dynamic _model;
17+
18+
/// Constructor for initialising the <see cref="SettlementModelPythonWrapper"/> class with wrapped <see cref="PyObject"/> object
19+
/// </summary>
20+
/// <param name="model">Models brokerage transactions, fees, and order</param>
21+
public SettlementModelPythonWrapper(PyObject model)
22+
{
23+
_model = model;
24+
}
25+
26+
/// <summary>
27+
/// Applies cash settlement rules using Pyhon method
28+
/// </summary>
29+
/// <param name="applyFundsParameters">The funds application parameters</param>
30+
public void ApplyFunds(ApplyFundsSettlementModelParameters applyFundsParameters)
31+
{
32+
using (Py.GIL())
33+
{
34+
_model.ApplyFunds(applyFundsParameters);
35+
}
36+
}
37+
38+
/// <summary>
39+
/// Scan for pending settlements using Python method
40+
/// </summary>
41+
/// <param name="settlementParameters">The settlement parameters</param>
42+
public void Scan(ScanSettlementModelParameters settlementParameters)
43+
{
44+
using (Py.GIL())
45+
{
46+
_model.Scan(settlementParameters);
47+
}
48+
}
49+
}
50+
}

Tests/Algorithm/AlgorithmSetBrokerageTests.cs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -322,11 +322,6 @@ public IBuyingPowerModel GetBuyingPowerModel(Security security)
322322
throw new System.NotImplementedException();
323323
}
324324

325-
public IBuyingPowerModel GetBuyingPowerModel(Security security, AccountType accountType)
326-
{
327-
throw new System.NotImplementedException();
328-
}
329-
330325
public IFeeModel GetFeeModel(Security security)
331326
{
332327
throw new System.NotImplementedException();
@@ -352,11 +347,6 @@ public ISettlementModel GetSettlementModel(Security security)
352347
throw new System.NotImplementedException();
353348
}
354349

355-
public ISettlementModel GetSettlementModel(Security security, AccountType accountType)
356-
{
357-
throw new System.NotImplementedException();
358-
}
359-
360350
public IShortableProvider GetShortableProvider(Security security)
361351
{
362352
throw new System.NotImplementedException();

0 commit comments

Comments
 (0)