Fix CustomPartialFillModelAlgorithmFails.cs/py (#7321)
Python Virtual Environments / build (push) Has been cancelled
Benchmarks / build (push) Has been cancelled
Build & Test Lean / build (push) Has been cancelled
Regression Tests / build (push) Has been cancelled
Research Regression Tests / build (push) Has been cancelled

* Fix bug

The algorithm `CustomPartialFillModelAlgorithm.cs` was not working as
expected with short orders because in its Fill model, the variable
`absoluteRemaining` instead of decrease was being increased since
the `FillQuantity` in each call to this method was negative. Therefore
when this amount was substracted to `absoluteRemaining`, instead of
substract, it was added. Hence the method created more partial orders
than expected, specifically: 580 short orders for SPY with Quantity -10.

* Fix `CustomPartialFillModelAlgorithm.py`

* Address required changes

* Nit change

* Address required changes
This commit is contained in:
Ricardo Andrés Marino Rojas
2023-06-20 08:46:11 -05:00
committed by GitHub
parent 5ddc8c3766
commit a309322ae5
3 changed files with 70 additions and 17 deletions
@@ -21,8 +21,8 @@ class CustomPartialFillModelAlgorithm(QCAlgorithm):
'''Basic template algorithm that implements a fill model with partial fills'''
def Initialize(self):
self.SetStartDate(2019,1,1)
self.SetEndDate(2019,3,1)
self.SetStartDate(2019, 1, 1)
self.SetEndDate(2019, 3, 1)
equity = self.AddEquity("SPY", Resolution.Hour)
self.spy = equity.Symbol
@@ -37,7 +37,7 @@ class CustomPartialFillModelAlgorithm(QCAlgorithm):
if len(open_orders) != 0: return
if self.Time.day > 10 and self.holdings.Quantity <= 0:
self.MarketOrder(self.spy, 100, True)
self.MarketOrder(self.spy, 105, True)
elif self.Time.day > 20 and self.holdings.Quantity >= 0:
self.MarketOrder(self.spy, -100, True)
@@ -56,16 +56,17 @@ class CustomPartialFillModel(FillModel):
# Create the object
fill = super().MarketFill(asset, order)
# Set this fill amount
# Set the fill amount
fill.FillQuantity = np.sign(order.Quantity) * 10
if absoluteRemaining == fill.FillQuantity:
if (min(abs(fill.FillQuantity), absoluteRemaining) == absoluteRemaining):
fill.FillQuantity = np.sign(order.Quantity) * absoluteRemaining
fill.Status = OrderStatus.Filled
self.absoluteRemainingByOrderId.pop(order.Id, None)
else:
fill.Status = OrderStatus.PartiallyFilled
self.absoluteRemainingByOrderId[order.Id] = absoluteRemaining - fill.FillQuantity
self.absoluteRemainingByOrderId[order.Id] = absoluteRemaining - abs(fill.FillQuantity)
price = fill.FillPrice
self.algorithm.Debug(f"{self.algorithm.Time} - Partial Fill - Remaining {absoluteRemaining} Price - {price}")
# self.algorithm.Debug(f"{self.algorithm.Time} - Partial Fill - Remaining {self.absoluteRemainingByOrderId[order.Id]} Price - {price}")
return fill