Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding order.get_filled_price() and enabling pytest coverage #267

Merged
merged 3 commits into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Updating to work with partially filled orders as well as filled ones.
  • Loading branch information
davidlatte committed Aug 3, 2023
commit bcbc5d0050d2ae6253be5edcbbc4dcfc48377297
4 changes: 2 additions & 2 deletions lumibot/entities/order.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ def cash_pending(self, strategy):
else:
return -cash_value

def get_filled_price(self) -> float:
def get_fill_price(self) -> float:
"""
Get the weighted average filled price for this order. Option contracts often encounter partial fills,
so the weighted average is the only valid price that can be used for PnL calculations.
Expand All @@ -483,7 +483,7 @@ def get_filled_price(self) -> float:
has not yet been filled.
"""
# Only calculate on filled orders
if not self.transactions or not self.position_filled or not self.quantity:
if not self.transactions or not self.quantity:
return 0.0

# calculate the weighted average filled price since options often encounter partial fills
Expand Down
13 changes: 4 additions & 9 deletions tests/test_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,18 @@ def test_get_filled_price(self):
buy_order = Order(strategy='abc', asset=asset, side="buy", quantity=100)

# No transactions
assert buy_order.get_filled_price() == 0
assert buy_order.get_fill_price() == 0

buy_order.transactions = [
Order.Transaction(quantity=50, price=20.0),
Order.Transaction(quantity=50, price=30.0)
]

# Order not yet filled
assert buy_order.get_filled_price() == 0

# Order filled
buy_order.position_filled = True
assert buy_order.get_filled_price() == 25.0
assert buy_order.get_fill_price() == 25.0

# Error case where quantity is not set
buy_order._quantity = 0
assert buy_order.get_filled_price() == 0
assert buy_order.get_fill_price() == 0

# Ensure Weighted Average used
sell_order = Order(strategy='abc', asset=asset, side="sell", quantity=100)
Expand All @@ -52,4 +47,4 @@ def test_get_filled_price(self):
Order.Transaction(quantity=20, price=40.0)
]
sell_order.position_filled = True
assert sell_order.get_filled_price() == 32.0
assert sell_order.get_fill_price() == 32.0