Skip to content

Commit

Permalink
Fix bug of zero dollar volume
Browse files Browse the repository at this point in the history
  • Loading branch information
mgao6767 committed Dec 20, 2023
1 parent b017092 commit 7062ae9
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "frds"
version = "2.4.0"
version = "2.4.1"
authors = [{ name = "Mingze Gao", email = "adrian.gao@outlook.com" }]
description = "Financial Research Data Services"
readme = "README.md"
Expand Down
6 changes: 4 additions & 2 deletions src/frds/measures/price_impact.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,7 @@ def simple_price_impact(
spread = 2 * trade_direction * (midpoint_later - midpoint) / midpoint * 100
else:
spread = 2 * trade_direction * (np.log(midpoint_later) - np.log(midpoint))

return np.average(spread, weights=volume * price)
dollar_volume = volume * price
if all(dollar_volume == 0):
return np.nanmean(spread)
return np.average(spread, weights=dollar_volume)
11 changes: 8 additions & 3 deletions src/frds/measures/spread.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ def effective_spread(
spread = 2 * trade_direction * (price - midpoint) / midpoint * 100
else:
spread = 2 * trade_direction * (np.log(price) - np.log(midpoint))
return np.average(spread, weights=volume * price)
dollar_volume = volume * price
if all(dollar_volume == 0):
return np.nanmean(spread)
return np.average(spread, weights=dollar_volume)


def realized_spread(
Expand Down Expand Up @@ -108,5 +111,7 @@ def realized_spread(
spread = 2 * trade_direction * (price - midpoint_later) / midpoint * 100
else:
spread = 2 * trade_direction * (np.log(price) - np.log(midpoint_later))

return np.average(spread, weights=volume * price)
dollar_volume = volume * price
if all(dollar_volume == 0):
return np.nanmean(spread)
return np.average(spread, weights=dollar_volume)

0 comments on commit 7062ae9

Please sign in to comment.