Initial implementation of custom price triggers
- likely to have some bugs, but this is good enough for a preview release.
This commit is contained in:
@@ -29,6 +29,10 @@ class UpdateTrigger:
|
||||
def squelched(self):
|
||||
return self._squelched
|
||||
|
||||
@squelched.setter
|
||||
def squelched(self, value):
|
||||
self._squelched = value
|
||||
|
||||
def _find_next_trigger(
|
||||
self,
|
||||
comparison_operator: Callable,
|
||||
@@ -93,12 +97,47 @@ class UpdateTrigger:
|
||||
case AlertType.ALL_TIME_HIGH:
|
||||
time_range = now - start_time
|
||||
comparison_operator = operator.gt
|
||||
case AlertType.SPECIFIC_PRICE:
|
||||
# For custom price alerts, check if the price crosses the threshold
|
||||
# We alert when price moves from below to above (or vice versa)
|
||||
target_price = self._alert.price
|
||||
if self._last_trigger is None:
|
||||
# First time - check if current price crossed the threshold
|
||||
if new_datum[1] >= target_price:
|
||||
self._last_trigger = new_datum
|
||||
self._last_alerting = new_datum
|
||||
return True
|
||||
return False
|
||||
else:
|
||||
# Check if we crossed the threshold
|
||||
old_price = self._last_trigger[1]
|
||||
new_price = new_datum[1]
|
||||
|
||||
# Alert if we cross the threshold in either direction
|
||||
crossed_up = old_price < target_price <= new_price
|
||||
crossed_down = old_price >= target_price > new_price
|
||||
|
||||
if crossed_up or crossed_down:
|
||||
self._last_trigger = new_datum
|
||||
self._last_alerting = new_datum
|
||||
was_squelched = self._squelched
|
||||
self._squelched = True
|
||||
return not was_squelched
|
||||
elif self._squelched:
|
||||
# Update last_trigger but don't alert (we're tracking current price)
|
||||
self._last_trigger = new_datum
|
||||
# If we moved away from threshold, allow next crossing to alert
|
||||
if (crossed_up and new_price < target_price) or (crossed_down and new_price >= target_price):
|
||||
self._squelched = False
|
||||
else:
|
||||
# Just update the last trigger for tracking
|
||||
self._last_trigger = new_datum
|
||||
return False
|
||||
case _:
|
||||
# TODO: The logic here is certainly wrong for Custom
|
||||
time_range = datetime.timedelta(days=int(365.25 * 6))
|
||||
comparison_operator = operator.eq
|
||||
|
||||
if new_datum[0] > now - time_range:
|
||||
if new_datum[0] > now - time_range and self._alert.alert_type != AlertType.SPECIFIC_PRICE:
|
||||
if self._last_trigger is None:
|
||||
self._last_trigger = new_datum
|
||||
self._last_alerting = new_datum
|
||||
|
||||
Reference in New Issue
Block a user