Get real-time updates from the financial market with your own bot on Telegram
Learn how to build a bot to receive real-time market data using Python and Metatrader 5
One of the most significant advantages of developing investment automation strategies is the possibility of not having to stand in front of the screens computers all the time to identify the best opportunities on the market. Thinking about it, I developed a bot for the Telegram that informs me which assets suffered the most falls or highs in the day, and I will show you step by step so you can develop yours!
In this blog, we will focus on developing a simple application that sends a request to the Bot on Telegram, and it returns the top five and bottom five percentage returns for the day of the stock’s assets. The final result will be like the video below:
To develop your Bot, we will follow the following steps:
- Creating your Bot in Telegram
- Creating the main functions in Python
- Collecting data from the Exchange
- Sending information to the Bot
Five Financial Applications Of Natural Language Processing | Data Driven Investor
The last five years have been revolutionary for the field of natural language processing. We went from glorified ctrl-f…
www.datadriveninvestor.com
Creating your Bot in Telegram
To create your Bot in Telegram, find the user @BotFather, and send the command “/start.” The following screen will appear:
Afterward, we send the “/newbot” command to BotFather. BotFather will ask us to create a name for our robot. Feel free to give it any name you like. In this case, we created a so-called tutobot. After that, we need to create a user for the robot and, necessarily, we must insert _bot at the end.
After creating the user, BotFather will grant a token so that we can send requests. Please copy and paste this token for later use and keep it safe.
Creating the main bot functions in Python
I will not go into detail about each line of code created for the robot. This work has already been done majestically by Mauro de Carvalho in his post:
Desenvolvendo o seu primeiro chatbot no Telegram com Python
Se você está lendo essa postagem, então provavelmente é a primeira vez que você estará desenvolvendo um chatbot. Se…
medium.com
def main():
updater = Updater(token=TELEGRAM_TOKEN)
dp = updater.dispatcher
dp.add_handler(CommandHandler('altas', altas))
dp.add_handler(CommandHandler('baixas', bop))
updater.start_polling()
updater.idle()
Insert the token given by the BotFather in the variables.
This primary function does the following: Waits for the user to send one of the listed commands (/altas, /baixas) and returns the corresponding methods’ results.
Collecting StockData
To collect stock market data, we will use the MetaTrader5 library, as I demonstrated in my previous posts.
Start Building Your Trading Strategies in 5 Minutes With Python and MetaTrader
The fastest way to integrate collecting and analyzing financial market data for your analysis.
medium.com
The Low Function:
def bop(bot, update):
chat_id = update.message.chat_id
mt5.initialize()
tickers = pd.read_csv('tickers_cheios.csv')
tickers['x'] = tickers['x'].astype(str)
symbols = tickers['x']
d = pd.DataFrame()
s = pd.DataFrame()
for i in symbols:
rates = mt5.copy_rates_from_pos(i, mt5.TIMEFRAME_D1, 1, 1)
d[i] = [y['close'] for y in rates]
for i in symbols:
rates = mt5.copy_rates_from_pos(i, mt5.TIMEFRAME_M2, 0, 1)
s[i] = [y['close'] for y in rates]
f = d.append(s)
returns = f.pct_change().dropna().transpose()
returns['tickets'] = returns.index
returns.columns = ['return_pct', 'tickets']
returns['porcentagem'] = returns.return_pct * 100
returns['porcentagem'] = returns['porcentagem'].round(2)
returns = returns.sort_values(by=['return_pct'])
temp_mais_baixas = returns.head(5)
bot.send_message(chat_id=chat_id, text='As ações com maiores baixas de hoje foram:')
for i in range(len(temp_mais_baixas)):
ticket = temp_mais_baixas['tickets'][i]
valor_por = temp_mais_baixas['porcentagem'][i]
text = '\U0001F4C9' + " : " + str(ticket) + " : " + str(valor_por) + " %"
print(text)
bot.send_message(chat_id = chat_id, text = text)
mt5.shutdown()
The High Function
def altas(bot, update):
chat_id = update.message.chat_id
mt5.initialize()
tickers = pd.read_csv('tickers_cheios.csv')
tickers['x'] = tickers['x'].astype(str)
symbols = tickers['x']
d = pd.DataFrame()
s = pd.DataFrame()
for i in symbols:
rates = mt5.copy_rates_from_pos(i, mt5.TIMEFRAME_D1, 1, 1)
d[i] = [y['close'] for y in rates]
for i in symbols:
rates = mt5.copy_rates_from_pos(i, mt5.TIMEFRAME_M2, 0, 1)
s[i] = [y['close'] for y in rates]
f = d.append(s)
returns = f.pct_change().dropna().transpose()
returns['tickets'] = returns.index
returns.columns = ['return_pct', 'tickets']
returns['porcentagem'] = returns.return_pct * 100
returns['porcentagem'] = returns['porcentagem'].round(2)
returns = returns.sort_values(by=['return_pct'])
temp_mais_altas = returns.tail(5)
bot.send_message(chat_id=chat_id, text='As ações com maiores altas de hoje foram:')
for i in range(len(temp_mais_altas)):
ticket = temp_mais_altas['tickets'][i]
valor_por = temp_mais_altas['porcentagem'][i]
text = '\U0001F4C8' + " : " + str(ticket) + " : " + str(valor_por) + " %"
print(text)
bot.send_message(chat_id = chat_id, text = text)
mt5.shutdown()
Sending Data to Bot
Now, all you have to do is send the commands you have configured to have real-time returns from stock quotes! Once the software is running, you will get real-time information about the best and worst results of the day!
Thanks for reading! See you at the next post!
Gain Access to Expert View — Subscribe to DDI Intel
Ten articles before and after
New Kind of Secret Chat: Sensay on Telegram – Best Telegram
PointPay Telegram Community Has Exceeded 22,000 Subscribers – Best Telegram
Interview with WaykiChain(WICC) Telegram Community manager Dave Oke – Best Telegram
Nagricoin + Telegram Passport = easy KYC! – Best Telegram
Dan’s June 1st Telegram AMA: Recap – Best Telegram
Opulous Telegram Referral Competition – Best Telegram
Important Update to our Telegram Community – Best Telegram
Telegram: Hava Uyarı Botu 1.Bölüm – Best Telegram
Sparkster’s transcript from Telegram AMA with Crypto Daku – Best Telegram
Telegram AMA with our CMO. Hi everyone, I’m Steven, the CMO of the… – Best Telegram