Skip to content

Commit

Permalink
Fix market order avg_px for Polymarket trade reports
Browse files Browse the repository at this point in the history
  • Loading branch information
cjdsellers committed Nov 4, 2024
1 parent 13d275a commit 1ec4bc9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions RELEASES.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ None

### Fixes
- Fixed reconcile open orders and account websocket message for dYdX (#2039), thanks @davidsblom
- Fixed market order `avg_px` for Polymarket trade reports

---

Expand Down
20 changes: 19 additions & 1 deletion nautilus_trader/adapters/polymarket/schemas/trade.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
# limitations under the License.
# -------------------------------------------------------------------------------------------------

from decimal import Decimal

import msgspec

from nautilus_trader.adapters.polymarket.common.enums import PolymarketLiquiditySide
Expand Down Expand Up @@ -84,6 +86,17 @@ def venue_order_id(self, maker_address: str) -> VenueOrderId:
else:
return VenueOrderId(self.taker_order_id)

def avg_px(self) -> Decimal:
# We assume there should be at least some filled quantity for a trade report
total_qty = Decimal(0)
avg_px = Decimal(0)
for order in self.maker_orders:
matched_amount = Decimal(order.matched_amount)
avg_px += Decimal(order.price) * matched_amount
total_qty += matched_amount

return avg_px / total_qty

def parse_to_fill_report(
self,
account_id: AccountId,
Expand All @@ -92,6 +105,11 @@ def parse_to_fill_report(
maker_address: str,
ts_init: int,
) -> FillReport:
price = (
float(self.price)
if self.liquidity_side() == LiquiditySide.MAKER
else float(self.avg_px())
)
return FillReport(
account_id=account_id,
instrument_id=instrument.id,
Expand All @@ -100,7 +118,7 @@ def parse_to_fill_report(
trade_id=TradeId(self.id),
order_side=self.order_side(),
last_qty=instrument.make_qty(float(self.size)),
last_px=instrument.make_price(float(self.price)),
last_px=instrument.make_price(price),
liquidity_side=self.liquidity_side(),
commission=Money(0, USDC_POS), # TBC: Hard coded for now
report_id=UUID4(),
Expand Down

0 comments on commit 1ec4bc9

Please sign in to comment.