From 4c463e88f29695415e29362b5c39bdebe37f41c4 Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Sun, 18 Dec 2022 16:55:09 +0000 Subject: [PATCH] Implement notifications --- app/local_settings.py | 2 ++ core/lib/market.py | 12 +++++++++++- core/lib/notify.py | 23 +++++++++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 core/lib/notify.py diff --git a/app/local_settings.py b/app/local_settings.py index 561abc5..ca1142b 100644 --- a/app/local_settings.py +++ b/app/local_settings.py @@ -32,6 +32,8 @@ REGISTRATION_OPEN = getenv("REGISTRATION_OPEN", "false").lower() in trues # Hook URL, do not include leading or trailing slash HOOK_PATH = "hook" +NOTIFY_TOPIC = getenv("NOTIFY_TOPIC", "great-fisk") + DEBUG = getenv("DEBUG", "false").lower() in trues PROFILER = getenv("PROFILER", "false").lower() in trues diff --git a/core/lib/market.py b/core/lib/market.py index 8c4ec6c..198130d 100644 --- a/core/lib/market.py +++ b/core/lib/market.py @@ -2,6 +2,7 @@ from datetime import datetime from decimal import Decimal as D from core.exchanges import GenericAPIError +from core.lib.notify import sendmsg from core.models import Account, Strategy, Trade from core.util import logs @@ -554,6 +555,7 @@ def execute_strategy(callback, strategy, func): trailing_stop_loss = protection["tsl"] # Create object, note that the amount is rounded to the trade precision + amount_rounded = float(round(trade_size_in_base, trade_precision)) new_trade = Trade.objects.create( user=user, account=account, @@ -563,7 +565,7 @@ def execute_strategy(callback, strategy, func): type=type, time_in_force=strategy.time_in_force, # amount_fiat=amount_fiat, - amount=float(round(trade_size_in_base, trade_precision)), + amount=amount_rounded, # price=price_bound, price=price_bound, stop_loss=float(round(stop_loss, display_precision)), @@ -590,8 +592,16 @@ def execute_strategy(callback, strategy, func): new_trade.save() else: info = new_trade.post() + print("INFO", info) log.debug(f"Posted trade: {info}") + # Send notification with limited number of fields + wanted_fields = ["requestID", "type", "symbol", "units", "reason"] + sendmsg( + ", ".join([str(v) for k, v in info.items() if k in wanted_fields]), + title=f"{direction} {amount_rounded} on {symbol}", + ) + def process_callback(callback): log.info(f"Received callback for {callback.hook} - {callback.signal}") diff --git a/core/lib/notify.py b/core/lib/notify.py new file mode 100644 index 0000000..61608ec --- /dev/null +++ b/core/lib/notify.py @@ -0,0 +1,23 @@ +import requests +from django.conf import settings + +from core.util import logs + +NTFY_URL = "https://ntfy.sh" + +log = logs.get_logger(__name__) + + +def sendmsg(msg, title=None, priority=None, tags=None): + headers = {"Title": "Fisk"} + if title: + headers["Title"] = title + if priority: + headers["Priority"] = priority + if tags: + headers["Tags"] = tags + requests.post( + f"{NTFY_URL}/{settings.NOTIFY_TOPIC}", + data=msg, + headers=headers, + )