Skip to content

fix zero division error in PriceDeviation when aggregate price is 0 #10

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

Merged
merged 10 commits into from
Feb 7, 2022
Merged
46 changes: 46 additions & 0 deletions pyth_observer/calendar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import datetime

EQUITY_START = datetime.time(9, 30, 0)
EQUITY_CLOSE = datetime.time(16, 0, 0)
EQUITY_EARLY_CLOSE = datetime.time(13, 0, 0)
EQUITY_HOLIDAYS = ["2022-01-17", "2022-02-21", "2022-04-15", "2022-05-30",
"2022-06-20", "2022-07-04", "2022-09-05", "2022-11-24", "2022-11-25", "2022-12-26"]
EQUITY_HOLIDAYS_EARLY = ["2022-11-25"]
EQUITY_TRADING_DAYS = [1, 2, 3, 4, 5]

FX_START = datetime.time(17, 0, 0)
FX_CLOSE = datetime.time(16, 0, 0)
FX_TRADING_DAYS = [7, 1, 2, 3, 4, 5]

METAL_START = datetime.time(18, 3, 0)
METAL_CLOSE = datetime.time(16, 58, 0)
METAL_TRADING_DAYS = [7, 1, 2, 3, 4, 5]


class Calendar:
def is_market_open(self, product, dt):
day, date, time = dt.weekday(), dt.date(), dt.time()
if product.attrs["asset_type"] == "Equity":
# market holiday
if date.strftime("%Y-%m-%d") in EQUITY_HOLIDAYS:
# market within early closing hours
if date.strftime("%Y-%m-%d") in EQUITY_HOLIDAYS_EARLY and time >= EQUITY_START and time <= EQUITY_EARLY_CLOSE:
breakpoint()
return True
# market after holiday closing hours
return False
# market within regular opening hours
if day in EQUITY_TRADING_DAYS and time >= EQUITY_START and time <= EQUITY_CLOSE:
return True
# market closed
return False
elif product.attrs["asset_type"] == "FX":
if day in FX_TRADING_DAYS:
if (day == 7 and time < FX_START) or (day == 5 and time > FX_CLOSE):
return False
return True
return False
elif product.attrs["asset_type"] == "Metal":
pass
else: # Crypto
return True
7 changes: 6 additions & 1 deletion pyth_observer/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@ class PriceDeviation(PriceValidationEvent):

def is_valid(self) -> bool:
delta = self.publisher_aggregate.price - self.price.aggregate.price
if self.price.aggregate.price == 0:
# TODO: add another alert that validates whether the aggregate price is close to the truth
return True

self.deviation = abs(delta / self.price.aggregate.price) * 100

if (self.price.is_publishing(self.publisher_key) and
Expand All @@ -196,7 +200,6 @@ def is_valid(self) -> bool:
def get_event_details(self) -> Tuple[str, List[str]]:
agg = self.price.aggregate
published = self.publisher_aggregate

title = f"{self.publisher_name.upper()} price is {self.deviation:.0f}% off on {self.symbol}"
details = [
f"Aggregate: {agg.price:.2f} ± {agg.confidence_interval:.2f} (slot {agg.slot})",
Expand Down Expand Up @@ -232,6 +235,8 @@ def get_event_details(self) -> Tuple[str, List[str]]:
f"Published last slot: {self.publisher_latest.slot}"
)

return title, details
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good eye!



# Price Account events

Expand Down