2022-03-20 10:09:33 +01:00 | | | import os
|
| | | import sys
|
| | | import asyncio
|
| | | import requests
|
| | |
|
| | | from service.App import *
|
| | | from common.utils import *
|
| | |
|
2022-04-10 19:38:53 +02:00 | | | import logging
|
| | | log = logging.getLogger('notifier')
|
| | |
|
2022-03-20 10:09:33 +01:00 | | |
|
| | | async def notify_telegram():
|
2022-04-10 19:38:53 +02:00 | | | await simulate_trade()
|
| | |
|
2022-03-20 10:09:33 +01:00 | | | symbol = App.config["symbol"]
|
| | |
|
2022-03-26 12:43:02 +01:00 | | | status = App.status
|
| | | signal = App.signal
|
2022-03-20 10:09:33 +01:00 | | | signal_side = signal.get("side")
|
2022-03-26 12:43:02 +01:00 | | | close_price = signal.get('close_price')
|
| | | buy_score = signal.get('buy_score')
|
| | | sell_score = signal.get('sell_score')
|
2022-04-10 19:38:53 +02:00 | | | close_time = signal.get('close_time')
|
| | |
|
| | | model = App.config["signal_model"]
|
| | | buy_notify_threshold = model["buy_notify_threshold"]
|
| | | sell_notify_threshold = model["sell_notify_threshold"]
|
| | | buy_signal_threshold = model["buy_signal_threshold"]
|
| | | sell_signal_threshold = model["sell_signal_threshold"]
|
| | | trade_icon_step = model.get("trade_icon_step", 0)
|
| | | notify_frequency_minutes = model.get("notify_frequency_minutes", 1)
|
2022-03-26 12:43:02 +01:00 | | |
|
| | | # Crypto Currency Symbols: https://github.com/yonilevy/crypto-currency-symbols
|
2022-04-17 19:55:22 +02:00 | | | if symbol == "BTCUSDT":
|
| | | symbol_char = "₿"
|
| | | elif symbol == "ETHUSDT":
|
| | | symbol_char = "Ξ"
|
2022-03-26 12:43:02 +01:00 | | | else:
|
2022-04-17 19:55:22 +02:00 | | | symbol_char = symbol
|
2022-03-26 12:43:02 +01:00 | | |
|
| | | # Notification logic:
|
2022-03-27 18:58:41 +02:00 | | | # 1. Trade signal in the case it is suggested to really buy or sell: BUY or SELL and one corresponding score
|
| | | # 2. Notification signal simply to provide information (separate criteria): both scores
|
2022-04-10 19:38:53 +02:00 | | | # Icons: down: 📉, ⬇ ⬇️🔴 (red), up: 📈, ⬆, ⬆️ ↗️ 🟢 (green)
|
| | | message = ""
|
2022-03-26 12:43:02 +01:00 | | | if signal_side == "BUY":
|
2022-04-10 19:38:53 +02:00 | | | score_steps = (np.abs(buy_score - buy_signal_threshold) // trade_icon_step) if trade_icon_step else 0
|
2022-04-17 19:55:22 +02:00 | | | message = "🟢"*int(score_steps+1) + f" *BUY: {symbol_char} {int(close_price):,} Buy score: {buy_score:+.2f}*"
|
2022-03-26 12:43:02 +01:00 | | | elif signal_side == "SELL":
|
2022-04-10 19:38:53 +02:00 | | | score_steps = (np.abs(sell_score - sell_signal_threshold) // trade_icon_step) if trade_icon_step else 0
|
2022-04-17 19:55:22 +02:00 | | | message = "🔴"*int(score_steps+1) + f" *SELL: {symbol_char} {int(close_price):,} Sell score: {-sell_score:+.2f}*"
|
2022-04-10 19:38:53 +02:00 | | | elif (close_time.minute % notify_frequency_minutes) == 0: # Info message with custom frequency
|
| | | if buy_score > sell_score:
|
2022-04-17 19:55:22 +02:00 | | | message = f"{symbol_char} {int(close_price):,} 📈{buy_score:+.2f}"
|
2022-04-10 19:38:53 +02:00 | | | else:
|
2022-04-17 19:55:22 +02:00 | | | message = f"{symbol_char} {int(close_price):,} 📉{-sell_score:+.2f}"
|
2022-03-26 12:43:02 +01:00 | | | message = message.replace("+", "%2B") # For Telegram to display plus sign
|
2022-03-20 10:09:33 +01:00 | | |
|
2022-04-10 19:38:53 +02:00 | | | if not message:
|
2022-03-20 10:09:33 +01:00 | | | return
|
2022-04-10 19:38:53 +02:00 | | | if buy_score < buy_notify_threshold and sell_score < sell_notify_threshold:
|
| | | return # Do not send notifications with low notification threshold (also no buy/sell notifications)
|
2022-03-20 10:09:33 +01:00 | | |
|
| | | bot_token = App.config["telegram_bot_token"]
|
| | | chat_id = App.config["telegram_chat_id"]
|
| | |
|
| | | try:
|
2022-04-10 19:38:53 +02:00 | | | url = 'https://api.telegram.org/bot' + bot_token + '/sendMessage?chat_id=' + chat_id + '&parse_mode=markdown&text=' + message
|
2022-03-20 10:09:33 +01:00 | | | response = requests.get(url)
|
2022-03-26 12:43:02 +01:00 | | | response_json = response.json()
|
| | | if not response_json.get('ok'):
|
2022-04-10 19:38:53 +02:00 | | | log.error(f"Error sending notification.")
|
2022-03-20 10:09:33 +01:00 | | | except Exception as e:
|
2022-04-10 19:38:53 +02:00 | | | log.error(f"Error sending notification: {e}")
|
| | |
|
| | |
|
| | | async def simulate_trade():
|
| | | symbol = App.config["symbol"]
|
| | |
|
| | | status = App.status
|
| | | signal = App.signal
|
| | | signal_side = signal.get("side")
|
| | | close_price = signal.get('close_price')
|
| | | buy_score = signal.get('buy_score')
|
| | | sell_score = signal.get('sell_score')
|
| | | close_time = signal.get('close_time')
|
| | |
|
| | | t_status = App.transaction.get("status")
|
| | | t_price = App.transaction.get("price")
|
| | | if signal_side == "BUY" and (not t_status or t_status == "BUYING"):
|
| | | profit = t_price - close_price if t_price else 0.0
|
| | | t_dict = dict(timestamp=str(close_time), price=close_price, profit=profit, status="SELLING")
|
| | | elif signal_side == "SELL" and (not t_status or t_status == "SELLING"):
|
| | | profit = close_price - t_price if t_price else 0.0
|
| | | t_dict = dict(timestamp=str(close_time), price=close_price, profit=profit, status="BUYING")
|
| | | else:
|
| | | return
|
| | |
|
| | | # Save this transaction
|
| | | App.transaction = t_dict
|
| | | transaction_file = Path("transactions.txt")
|
| | | with open(transaction_file, 'a+') as f:
|
| | | f.write(",".join([str(v) for v in t_dict.values()]) + "\n")
|
2022-03-20 10:09:33 +01:00 | | |
|
| | |
|
| | | if __name__ == '__main__':
|
| | | pass
|