Streamlining Trading Notifier: From Slim Update to Full Rewrite

in LeoFinance6 hours ago

For long time I run a telegram bot pushing notification on fulfilled position, I decided to improve it today. The goal was initially simple: slim down notifications and make them easier to manage. However, the project quickly grew into a complete rewrite, transforming not just the interface but also the underlying logic. I ended up integrating database persistence, adding interactive menus and conversation processing.

The first step is retrieving fulfilled positions from both Hive Engine and the Hive internal market. Processing the information and create a message the chat. Hive-Engine information stems from trades history of the past 24h and internal market data is the account's history limit to

'limit_order_create','fill_order_operation

of the past 2 days.

Next, I added an interactive menu to each notification, allowing me to create follow-up positions directly from Telegram. Instead of just receiving a static message, I could respond to each notification to take the next trading action. Implementing this menu required understanding how Telegram bots handle inline buttons and callbacks, which at first seemed daunting.
However, once set up, it made managing follow-up trades much faster and more intuitive. This feature transformed the bot from a simple notifier into a lightweight trading assistant.

To persist data, I integrated database functionality using the postgres_persistence package from ptbcontrib.

https://github.com/python-telegram-bot/ptbcontrib

This allowed the bot to remember previous interactions and maintain context across sessions. Interestingly, the package did not create the database automatically, so I had to set it up manually. While this added an extra step, it also gave me full control over the schema and data structure. In the end, having persistent storage significantly increased the bot's reliability and made it suitable for long-term use. Currently it's working with PostgreSQL but I might switch later to MongoDB, due to the data structure. Document orientated seams more fitting.

Finally, replying to messages initially seemed complex, but regular expressions simplified the task. By defining patterns for expected input, I could easily extract values like token symbols, amounts, and prices from user replies.

import re
pattern = re.compile(
r'(?P<trader>[A-Za-z0-9._-]+)\s+'               # trader (buyer or seller)
r'(?P<action>bought|sold)\s+'                   # action
r'(?P<amount>[\d.]+)\s+'                        # amount
r'(?P<token>[A-Za-z0-9._-]+)\s+'                # token
r'(?:from|to)\s+'                               # direction keyword
r'(?P<counterparty>[A-Za-z0-9._-]+)\s*@\s*'     # counterparty
r'(?P<price>[\d.]+)\s*=\s*'                     # price
r'(?P<hive_volume>[\d.]+)\s*SWAP\.HIVE',        # HIVE volume
re.IGNORECASE)
match = pattern.search(text)
if match:
     data = match.groupdict()
# Determine buyer/seller from action + direction
if data["action"].lower() == "bought":
    data["buyer"] = data["trader"]
    data["seller"] = data["counterparty"]
else:
    data["buyer"] = data["counterparty"]
    data["seller"] = data["trader"]

This approach reduced the complexity of message parsing and allowed quick, accurate responses to each notification. The regex system turned what seemed like a complicated text handling problem into a manageable solution. Overall, combining regular expressions with the interactive menu made the Telegram bot highly responsive and user-friendly.

Posted Using INLEO