Skip to content

Commit 693cca5

Browse files
committed
Add regresion algos and improvements
1 parent 2b9e313 commit 693cca5

8 files changed

+192
-9
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
2+
# Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
14+
from AlgorithmImports import *
15+
from CustomBrokerageModelRegressionAlgorithm import CustomBrokerageModel
16+
17+
### <summary>
18+
### Regression algorithm to test we can specify a custom benchmark model, and override some of its methods
19+
### </summary>
20+
class CustomBenchmarkRegressionAlgorithm(QCAlgorithm):
21+
def Initialize(self):
22+
self.SetStartDate(2013,10,7)
23+
self.SetEndDate(2013,10,11)
24+
self.SetBrokerageModel(CustomBrokerageModelWithCustomBenchmark())
25+
self.AddEquity("SPY", Resolution.Daily)
26+
self.updateRequestSubmitted = False
27+
28+
def OnData(self, slice):
29+
if (not self.Portfolio.Invested) and self.Benchmark.Evaluate(self.Time) != 0:
30+
self.SetHoldings("SPY", 1)
31+
32+
class CustomBenchmark:
33+
def Evaluate(self, time):
34+
if time.day % 2 == 0:
35+
return 0
36+
else:
37+
return 1
38+
39+
class CustomBrokerageModelWithCustomBenchmark(CustomBrokerageModel):
40+
def GetBenchmark(self, securities):
41+
return CustomBenchmark()
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
2+
# Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
14+
from AlgorithmImports import *
15+
from CustomBrokerageModelRegressionAlgorithm import CustomBrokerageModel
16+
17+
### <summary>
18+
### Regression algorithm to test we can specify a custom settlement model, and override some of its methods
19+
### </summary>
20+
class CustomSettlementModelRegressionAlgorithm(QCAlgorithm):
21+
def Initialize(self):
22+
self.SetStartDate(2013,10,7)
23+
self.SetEndDate(2013,10,11)
24+
self.SetCash(10000)
25+
self.spy = self.AddEquity("SPY", Resolution.Daily)
26+
self.SetBrokerageModel(CustomBrokerageModelWithCustomSettlementModel())
27+
self.updateRequestSubmitted = False
28+
29+
def OnData(self, slice):
30+
if self.Portfolio.CashBook[Currencies.USD].Amount == 10000:
31+
parameters = ApplyFundsSettlementModelParameters(self.Portfolio, self.spy, self.Time, CashAmount(101, Currencies.USD), None)
32+
self.spy.SettlementModel.ApplyFunds(parameters)
33+
34+
def OnEndOfAlgorithm(self):
35+
if self.Portfolio.CashBook[Currencies.USD].Amount != 10101:
36+
raise Exception(f"It was expected to have 10101 USD in Portfolio, but was {self.Portfolio.CashBook[Currencies.USD].Amount}")
37+
38+
class CustomSettlementModel:
39+
def ApplyFunds(self, parameters):
40+
currency = parameters.CashAmount.Currency;
41+
amount = parameters.CashAmount.Amount
42+
parameters.Portfolio.CashBook[currency].AddAmount(amount)
43+
44+
def Scan(self, parameters):
45+
pass
46+
47+
class CustomBrokerageModelWithCustomSettlementModel(CustomBrokerageModel):
48+
def GetSettlementModel(self, security):
49+
return CustomSettlementModel()

Common/Brokerages/DefaultBrokerageModel.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,18 @@ 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+
313325
/// <summary>
314326
/// Gets a new buying power model for the security, returning the default model with the security's configured leverage.
315327
/// For cash accounts, leverage = 1 is used.
@@ -358,6 +370,18 @@ public virtual IMarginInterestRateModel GetMarginInterestRateModel(Security secu
358370
return MarginInterestRateModel.Null;
359371
}
360372

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+
361385
/// <summary>
362386
/// Checks if the order quantity is valid, it means, the order size is bigger than the minimum size allowed
363387
/// </summary>

Common/Brokerages/IBrokerageModel.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,31 @@ 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+
149158
/// <summary>
150159
/// Gets a new buying power model for the security
151160
/// </summary>
152161
/// <param name="security">The security to get a buying power model for</param>
153162
/// <returns>The buying power model for this brokerage/security</returns>
154163
IBuyingPowerModel GetBuyingPowerModel(Security security);
155164

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+
156174
/// <summary>
157175
/// Gets the shortable provider
158176
/// </summary>

Common/Python/BenchmarkPythonWrapper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class BenchmarkPythonWrapper : IBenchmark
2929
/// <summary>
3030
/// Constructor for initialising the <see cref="BenchmarkPythonWrapper"/> class with wrapped <see cref="PyObject"/> object
3131
/// </summary>
32-
/// <param name="model">Models brokerage transactions, fees, and order</param>
32+
/// <param name="model">Python benchmark model</param>
3333
public BenchmarkPythonWrapper(PyObject model)
3434
{
3535
_model = model;

Common/Python/BrokerageModelPythonWrapper.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,21 @@ public ISettlementModel GetSettlementModel(Security security)
272272
}
273273
}
274274

275+
/// <summary>
276+
/// Gets a new settlement model for the security
277+
/// </summary>
278+
/// <param name="security">The security to get a settlement model for</param>
279+
/// <param name="accountType">The account type</param>
280+
/// <returns>The settlement model for this brokerage</returns>
281+
[Obsolete("Flagged deprecated and will remove December 1st 2018")]
282+
public ISettlementModel GetSettlementModel(Security security, AccountType accountType)
283+
{
284+
using (Py.GIL())
285+
{
286+
return (_model.GetSettlementModel(security, accountType) as PyObject).GetAndDispose<ISettlementModel>();
287+
}
288+
}
289+
275290
/// <summary>
276291
/// Gets a new slippage model that represents this brokerage's fill slippage behavior
277292
/// </summary>
@@ -324,6 +339,21 @@ public IBuyingPowerModel GetBuyingPowerModel(Security security)
324339
}
325340
}
326341

342+
/// <summary>
343+
/// Gets a new buying power model for the security
344+
/// </summary>
345+
/// <param name="security">The security to get a buying power model for</param>
346+
/// <param name="accountType">The account type</param>
347+
/// <returns>The buying power model for this brokerage/security</returns>
348+
[Obsolete("Flagged deprecated and will remove December 1st 2018")]
349+
public IBuyingPowerModel GetBuyingPowerModel(Security security, AccountType accountType)
350+
{
351+
using (Py.GIL())
352+
{
353+
return (_model.GetBuyingPowerModel(security, accountType) as PyObject).GetAndDispose<IBuyingPowerModel>();
354+
}
355+
}
356+
327357
/// <summary>
328358
/// Gets the shortable provider
329359
/// </summary>

Common/Python/SettlementModelPythonWrapper.cs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
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+
117
using Python.Runtime;
218
using QuantConnect.Securities;
3-
using System;
4-
using System.Collections.Generic;
5-
using System.Linq;
6-
using System.Text;
7-
using System.Threading.Tasks;
819

920
namespace QuantConnect.Python
1021
{
@@ -17,14 +28,14 @@ public class SettlementModelPythonWrapper : ISettlementModel
1728

1829
/// Constructor for initialising the <see cref="SettlementModelPythonWrapper"/> class with wrapped <see cref="PyObject"/> object
1930
/// </summary>
20-
/// <param name="model">Models brokerage transactions, fees, and order</param>
31+
/// <param name="model">Settlement Python Model</param>
2132
public SettlementModelPythonWrapper(PyObject model)
2233
{
2334
_model = model;
2435
}
2536

2637
/// <summary>
27-
/// Applies cash settlement rules using Pyhon method
38+
/// Applies cash settlement rules using the method defined in the Python class
2839
/// </summary>
2940
/// <param name="applyFundsParameters">The funds application parameters</param>
3041
public void ApplyFunds(ApplyFundsSettlementModelParameters applyFundsParameters)
@@ -36,7 +47,7 @@ public void ApplyFunds(ApplyFundsSettlementModelParameters applyFundsParameters)
3647
}
3748

3849
/// <summary>
39-
/// Scan for pending settlements using Python method
50+
/// Scan for pending settlements using the method defined in the Python class
4051
/// </summary>
4152
/// <param name="settlementParameters">The settlement parameters</param>
4253
public void Scan(ScanSettlementModelParameters settlementParameters)

Tests/Algorithm/AlgorithmSetBrokerageTests.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,11 @@ 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+
325330
public IFeeModel GetFeeModel(Security security)
326331
{
327332
throw new System.NotImplementedException();
@@ -347,6 +352,11 @@ public ISettlementModel GetSettlementModel(Security security)
347352
throw new System.NotImplementedException();
348353
}
349354

355+
public ISettlementModel GetSettlementModel(Security security, AccountType accountType)
356+
{
357+
throw new System.NotImplementedException();
358+
}
359+
350360
public IShortableProvider GetShortableProvider(Security security)
351361
{
352362
throw new System.NotImplementedException();

0 commit comments

Comments
 (0)