Implement notifications

This commit is contained in:
Mark Veidemanis 2022-12-18 16:55:09 +00:00
parent f4772a3c7d
commit 4c463e88f2
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
3 changed files with 36 additions and 1 deletions

View File

@ -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

View File

@ -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}")

23
core/lib/notify.py Normal file
View File

@ -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,
)