Skip to content

Commit

Permalink
Add regresion algos and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Marinovsky committed Jan 5, 2024
1 parent 2b9e313 commit 693cca5
Show file tree
Hide file tree
Showing 8 changed files with 192 additions and 9 deletions.
41 changes: 41 additions & 0 deletions Algorithm.Python/CustomBenchmarkRegressionAlgorithm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# 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.

from AlgorithmImports import *
from CustomBrokerageModelRegressionAlgorithm import CustomBrokerageModel

### <summary>
### Regression algorithm to test we can specify a custom benchmark model, and override some of its methods
### </summary>
class CustomBenchmarkRegressionAlgorithm(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2013,10,7)
self.SetEndDate(2013,10,11)
self.SetBrokerageModel(CustomBrokerageModelWithCustomBenchmark())
self.AddEquity("SPY", Resolution.Daily)
self.updateRequestSubmitted = False

def OnData(self, slice):
if (not self.Portfolio.Invested) and self.Benchmark.Evaluate(self.Time) != 0:
self.SetHoldings("SPY", 1)

class CustomBenchmark:
def Evaluate(self, time):
if time.day % 2 == 0:
return 0
else:
return 1

class CustomBrokerageModelWithCustomBenchmark(CustomBrokerageModel):
def GetBenchmark(self, securities):
return CustomBenchmark()
49 changes: 49 additions & 0 deletions Algorithm.Python/CustomSettlementModelRegressionAlgorithm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# 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.

from AlgorithmImports import *
from CustomBrokerageModelRegressionAlgorithm import CustomBrokerageModel

### <summary>
### Regression algorithm to test we can specify a custom settlement model, and override some of its methods
### </summary>
class CustomSettlementModelRegressionAlgorithm(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2013,10,7)
self.SetEndDate(2013,10,11)
self.SetCash(10000)
self.spy = self.AddEquity("SPY", Resolution.Daily)
self.SetBrokerageModel(CustomBrokerageModelWithCustomSettlementModel())
self.updateRequestSubmitted = False

def OnData(self, slice):
if self.Portfolio.CashBook[Currencies.USD].Amount == 10000:
parameters = ApplyFundsSettlementModelParameters(self.Portfolio, self.spy, self.Time, CashAmount(101, Currencies.USD), None)
self.spy.SettlementModel.ApplyFunds(parameters)

def OnEndOfAlgorithm(self):
if self.Portfolio.CashBook[Currencies.USD].Amount != 10101:
raise Exception(f"It was expected to have 10101 USD in Portfolio, but was {self.Portfolio.CashBook[Currencies.USD].Amount}")

class CustomSettlementModel:
def ApplyFunds(self, parameters):
currency = parameters.CashAmount.Currency;
amount = parameters.CashAmount.Amount
parameters.Portfolio.CashBook[currency].AddAmount(amount)

def Scan(self, parameters):
pass

class CustomBrokerageModelWithCustomSettlementModel(CustomBrokerageModel):
def GetSettlementModel(self, security):
return CustomSettlementModel()
24 changes: 24 additions & 0 deletions Common/Brokerages/DefaultBrokerageModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,18 @@ public virtual ISettlementModel GetSettlementModel(Security security)
return new ImmediateSettlementModel();
}

/// <summary>
/// Gets a new settlement model for the security
/// </summary>
/// <param name="security">The security to get a settlement model for</param>
/// <param name="accountType">The account type</param>
/// <returns>The settlement model for this brokerage</returns>
[Obsolete("Flagged deprecated and will remove December 1st 2018")]
public ISettlementModel GetSettlementModel(Security security, AccountType accountType)
{
return GetSettlementModel(security);
}

/// <summary>
/// Gets a new buying power model for the security, returning the default model with the security's configured leverage.
/// For cash accounts, leverage = 1 is used.
Expand Down Expand Up @@ -358,6 +370,18 @@ public virtual IMarginInterestRateModel GetMarginInterestRateModel(Security secu
return MarginInterestRateModel.Null;
}

/// <summary>
/// Gets a new buying power model for the security
/// </summary>
/// <param name="security">The security to get a buying power model for</param>
/// <param name="accountType">The account type</param>
/// <returns>The buying power model for this brokerage/security</returns>
[Obsolete("Flagged deprecated and will remove December 1st 2018")]
public IBuyingPowerModel GetBuyingPowerModel(Security security, AccountType accountType)
{
return GetBuyingPowerModel(security);
}

/// <summary>
/// Checks if the order quantity is valid, it means, the order size is bigger than the minimum size allowed
/// </summary>
Expand Down
18 changes: 18 additions & 0 deletions Common/Brokerages/IBrokerageModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,31 @@ decimal RequiredFreeBuyingPowerPercent
/// <returns>The margin interest rate model for this brokerage</returns>
IMarginInterestRateModel GetMarginInterestRateModel(Security security);

/// <summary>
/// Gets a new settlement model for the security
/// </summary>
/// <param name="security">The security to get a settlement model for</param>
/// <param name="accountType">The account type</param>
/// <returns>The settlement model for this brokerage</returns>
[Obsolete("Flagged deprecated and will remove December 1st 2018")]
ISettlementModel GetSettlementModel(Security security, AccountType accountType);

/// <summary>
/// Gets a new buying power model for the security
/// </summary>
/// <param name="security">The security to get a buying power model for</param>
/// <returns>The buying power model for this brokerage/security</returns>
IBuyingPowerModel GetBuyingPowerModel(Security security);

/// <summary>
/// Gets a new buying power model for the security
/// </summary>
/// <param name="security">The security to get a buying power model for</param>
/// <param name="accountType">The account type</param>
/// <returns>The buying power model for this brokerage/security</returns>
[Obsolete("Flagged deprecated and will remove December 1st 2018")]
IBuyingPowerModel GetBuyingPowerModel(Security security, AccountType accountType);

/// <summary>
/// Gets the shortable provider
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion Common/Python/BenchmarkPythonWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class BenchmarkPythonWrapper : IBenchmark
/// <summary>
/// Constructor for initialising the <see cref="BenchmarkPythonWrapper"/> class with wrapped <see cref="PyObject"/> object
/// </summary>
/// <param name="model">Models brokerage transactions, fees, and order</param>
/// <param name="model">Python benchmark model</param>
public BenchmarkPythonWrapper(PyObject model)
{
_model = model;
Expand Down
30 changes: 30 additions & 0 deletions Common/Python/BrokerageModelPythonWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,21 @@ public ISettlementModel GetSettlementModel(Security security)
}
}

/// <summary>
/// Gets a new settlement model for the security
/// </summary>
/// <param name="security">The security to get a settlement model for</param>
/// <param name="accountType">The account type</param>
/// <returns>The settlement model for this brokerage</returns>
[Obsolete("Flagged deprecated and will remove December 1st 2018")]
public ISettlementModel GetSettlementModel(Security security, AccountType accountType)
{
using (Py.GIL())
{
return (_model.GetSettlementModel(security, accountType) as PyObject).GetAndDispose<ISettlementModel>();
}
}

/// <summary>
/// Gets a new slippage model that represents this brokerage's fill slippage behavior
/// </summary>
Expand Down Expand Up @@ -324,6 +339,21 @@ public IBuyingPowerModel GetBuyingPowerModel(Security security)
}
}

/// <summary>
/// Gets a new buying power model for the security
/// </summary>
/// <param name="security">The security to get a buying power model for</param>
/// <param name="accountType">The account type</param>
/// <returns>The buying power model for this brokerage/security</returns>
[Obsolete("Flagged deprecated and will remove December 1st 2018")]
public IBuyingPowerModel GetBuyingPowerModel(Security security, AccountType accountType)
{
using (Py.GIL())
{
return (_model.GetBuyingPowerModel(security, accountType) as PyObject).GetAndDispose<IBuyingPowerModel>();
}
}

/// <summary>
/// Gets the shortable provider
/// </summary>
Expand Down
27 changes: 19 additions & 8 deletions Common/Python/SettlementModelPythonWrapper.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
/*
* 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.Securities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QuantConnect.Python
{
Expand All @@ -17,14 +28,14 @@ public class SettlementModelPythonWrapper : ISettlementModel

/// Constructor for initialising the <see cref="SettlementModelPythonWrapper"/> class with wrapped <see cref="PyObject"/> object
/// </summary>
/// <param name="model">Models brokerage transactions, fees, and order</param>
/// <param name="model">Settlement Python Model</param>
public SettlementModelPythonWrapper(PyObject model)
{
_model = model;
}

/// <summary>
/// Applies cash settlement rules using Pyhon method
/// Applies cash settlement rules using the method defined in the Python class
/// </summary>
/// <param name="applyFundsParameters">The funds application parameters</param>
public void ApplyFunds(ApplyFundsSettlementModelParameters applyFundsParameters)
Expand All @@ -36,7 +47,7 @@ public void ApplyFunds(ApplyFundsSettlementModelParameters applyFundsParameters)
}

/// <summary>
/// Scan for pending settlements using Python method
/// Scan for pending settlements using the method defined in the Python class
/// </summary>
/// <param name="settlementParameters">The settlement parameters</param>
public void Scan(ScanSettlementModelParameters settlementParameters)
Expand Down
10 changes: 10 additions & 0 deletions Tests/Algorithm/AlgorithmSetBrokerageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,11 @@ public IBuyingPowerModel GetBuyingPowerModel(Security security)
throw new System.NotImplementedException();
}

public IBuyingPowerModel GetBuyingPowerModel(Security security, AccountType accountType)
{
throw new System.NotImplementedException();
}

public IFeeModel GetFeeModel(Security security)
{
throw new System.NotImplementedException();
Expand All @@ -347,6 +352,11 @@ public ISettlementModel GetSettlementModel(Security security)
throw new System.NotImplementedException();
}

public ISettlementModel GetSettlementModel(Security security, AccountType accountType)
{
throw new System.NotImplementedException();
}

public IShortableProvider GetShortableProvider(Security security)
{
throw new System.NotImplementedException();
Expand Down

0 comments on commit 693cca5

Please sign in to comment.