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
Next Next commit
Updating unit tests with Bot feedback.
  • Loading branch information
davidlatte committed Aug 3, 2023
commit 4225f286cf940f3fa917c0fd4c6f508d35b6319f
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ todos.txt
*.pem
test_bot.py
.vscode
.coverage*

# Pypi deployment
build
Expand Down
2 changes: 1 addition & 1 deletion lumibot/entities/order.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
if not self.transactions or not self.position_filled or not self.quantity:
return 0.0

# calculate the weighted average filled price since options often encounter partial fills
Expand Down
8 changes: 8 additions & 0 deletions tests/test_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ def test_is_option(self):
def test_get_filled_price(self):
asset = Asset("SPY")
buy_order = Order(strategy='abc', asset=asset, side="buy", quantity=100)

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

buy_order.transactions = [
Order.Transaction(quantity=50, price=20.0),
Order.Transaction(quantity=50, price=30.0)
Expand All @@ -37,6 +41,10 @@ def test_get_filled_price(self):
buy_order.position_filled = True
assert buy_order.get_filled_price() == 25.0

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

# Ensure Weighted Average used
sell_order = Order(strategy='abc', asset=asset, side="sell", quantity=100)
sell_order.transactions = [
Expand Down