Refactor FillModels
- Modifying `IFillModel` interface removing old methods and adding new method `Fill Fill(FillModelParameters)`. This is a breaking change. - Adding new `PythonWrapper` property for the `FillModel` base class. This is required due to a limitation in PythonNet: - Given C# class T has `virtual` methods A and B. Where method A calls method B. And given custom python class L inherits class T. And overrides method B. When class L calls base method A (of class T). And when method A internally calls method B. It will call C# implementation, not the python override. This issue is solved going back to the `PythonWrapper`. Adding unit tests. - Adding new `Parameters` property for the `FillModel` base class that will be set by the call to `Fill()`. The `Parameters` property will be used by the modified `XxxxFill()` implementations - Adding new `Fill` result object for the `Fill(FillModelParameters)` method - Adding new check before removing a `SubscriptionDataConfig` due to the FillModels consuming the configuration collection when determining which Price to use. WIll now only remove the `SDC` if the symbol was removed from the selecting `universe`, this will avoid the case where the symbol is never deselected and the subscription ends, which happens at the end of all executions. - Adding unit tests showcasing retro compatibility. - Enabling C# `CustomModelsAlgorithm` as a regression test. Python version returns a different result due to random number generation.
This commit is contained in:
@@ -49,7 +49,7 @@ class CustomModelsAlgorithm(QCAlgorithm):
|
||||
self.security.SetFillModel(CustomFillModel(self))
|
||||
self.security.SetSlippageModel(CustomSlippageModel(self))
|
||||
|
||||
|
||||
|
||||
def OnData(self, data):
|
||||
open_orders = self.Transactions.GetOpenOrders(self.spy)
|
||||
if len(open_orders) != 0: return
|
||||
@@ -58,7 +58,7 @@ class CustomModelsAlgorithm(QCAlgorithm):
|
||||
quantity = self.CalculateOrderQuantity(self.spy, .5)
|
||||
self.Log("MarketOrder: " + str(quantity))
|
||||
self.MarketOrder(self.spy, quantity, True) # async needed for partial fill market orders
|
||||
|
||||
|
||||
elif self.Time.day > 20 and self.security.Holdings.Quantity >= 0:
|
||||
quantity = self.CalculateOrderQuantity(self.spy, -.5)
|
||||
self.Log("MarketOrder: " + str(quantity))
|
||||
@@ -68,15 +68,14 @@ class CustomModelsAlgorithm(QCAlgorithm):
|
||||
class CustomFillModel(ImmediateFillModel):
|
||||
def __init__(self, algorithm):
|
||||
self.algorithm = algorithm
|
||||
self.base = ImmediateFillModel()
|
||||
self.absoluteRemainingByOrderId = {}
|
||||
random.seed(100)
|
||||
|
||||
|
||||
def MarketFill(self, asset, order):
|
||||
#if not _absoluteRemainingByOrderId.TryGetValue(order.Id, absoluteRemaining):
|
||||
absoluteRemaining = order.AbsoluteQuantity
|
||||
self.absoluteRemainingByOrderId[order.Id] = order.AbsoluteQuantity
|
||||
fill = self.base.MarketFill(asset, order)
|
||||
fill = super().MarketFill(asset, order)
|
||||
absoluteFillQuantity = int(min(absoluteRemaining, random.randint(0, 2*int(order.AbsoluteQuantity))))
|
||||
fill.FillQuantity = np.sign(order.Quantity) * absoluteFillQuantity
|
||||
if absoluteRemaining == absoluteFillQuantity:
|
||||
@@ -103,7 +102,7 @@ class CustomFeeModel:
|
||||
class CustomSlippageModel:
|
||||
def __init__(self, algorithm):
|
||||
self.algorithm = algorithm
|
||||
|
||||
|
||||
def GetSlippageApproximation(self, asset, order):
|
||||
# custom slippage math
|
||||
slippage = asset.Price * d.Decimal(0.0001 * np.log10(2*float(order.AbsoluteQuantity)))
|
||||
|
||||
Reference in New Issue
Block a user