A Telegram Bot to check Forex Capital Markets (FXCM) accounts

IperGiove
2 min readFeb 8, 2021

Telegram is a powerful app that permits to code chat bots, also in python, so why shouldn’t I code one?
I should code a Telegram bot to know the trading performance of an FXCM account.

I want to do that for two reasons. Once is that I never remember passwords, so the log in using the FXCM app it is so boring. Second is that I want to evaluate different trading strategies in different FXCM accounts.

Let’s code

Libraries

We need to install two libraries: one to communicate with telegram, and one to communicate with FXCM.

pip install python-telegram-bot --upgrade
pip install fxcmpy

I remember you that the last fxcmpy version (1.2.6) need also the socket.io library (4.4.0).

pip install python-socketio=4.4.0

Create a new bot

In telegram we have to select the BotFather chat and text:

/start
/newbot

And now we have to choose the telegram bot name for example

fxcmtest

After that we have the token to access the HTTP API.

The code

Let’s start to import the libraries and the telegram token

from telegram.ext import Updater, CommanfHandler, MessageHandler, Filters
import fxcmpy
TOKEN-tg='token_tg'

Now we code the main function.
There are two different functions to receive a message from the bot.
The help_message that displays the all possible words that we can write to the bot. The message function that displays the informations about an FXCM account.

The /help message call the help_message function that show the all command we can send to the bot.
/performance1 and /performance2 show the trading performance of two different accounts.

The code for the help_message function is super easy.

The code for the message function prints the balance, the gross balance and the floating balance. To do that, we have to choose the correct FXCM token.
Then we check if the username of who sent the message is our telegram username and then call the check_profit function to get the live profit.

So the last function that we have to code is the check_profit function. Where we get all our investment.

Now if we put all together, the entire code is:

References

--

--