📈
Ween's Lab
UdemyYouTubeTikTok
  • Welcome
  • 📻FPGA Tutorials
    • FPGA Boards: Getting Started
      • Getting Started with PYNQ on Kria KV260 Vision AI Starter Kit
      • Getting Started with PYNQ on Red Pitaya STEMlab 125-14
      • Getting Started with PYNQ on ZYBO
    • FPGA Ethernet Tutorial
      • FPGA Tutorial Ethernet 1: Simple TCP Server
    • PYNQ FPGA Tutorial 101
      • Part 0: Introduction
      • Part 1: GPIO
      • Part 2: Custom IP
      • Part 3: Memory
      • Part 4: ANN Processor
    • PYNQ FPGA Tutorial 102
      • Part 0: Introduction
      • Part 1: Memory Mapped
      • Part 2: Direct Memory Access
      • Part 3: AXI-Lite Multiplier
      • Part 4: AXI-Stream Multiplier with DMA
      • Part 5: AXI-Lite GCD
      • Part 6: AXI-Stream GCD with DMA
      • Part 7: Access to DDR from PL
    • ZYNQ FPGA Tutorial
      • Part 1: Gate-Level Combinational Circuit
      • Part 2: RT-Level Combinational Circuit
      • Part 3: Regular Sequential Circuit
      • Part 4: FSM Sequential Circuit
      • Part 5: ZYNQ Architecture
      • Part 6: ARM CPU and FPGA Module
      • Part 7: FPGA Memory
      • Part 8: Hardware Accelerator for Neural Networks
    • ZYNQ FPGA Linux Kernel Module
      • Cross Compiling Kernel, Kernel Module, and User Program for PYNQ
      • Configure PL to PS Interrupt in Kernel Module
      • Configure AXI DMA in Kernel Module
  • 📟Proyek Arduino
    • Kumpulan Proyek
      • Rangkaian LED
      • LED Berkedip Nyala Api
      • LED Chaser
      • LED Binary Counter
      • OLED 128x4 Bitcoin Ticker
      • Rangkaian Button
      • Button Multifungsi
      • Button Interrupt
      • Button Debouncing
    • Pelatihan Mikrokontroler Arduino ESP32
      • Bab 1 Pengenalan Mikrokontroler
      • Bab 2 Pengenalan Arduino
      • Bab 3 Pengenalan Bahasa C
      • Bab 4 Digital Output
      • Bab 5 Digital Input
      • Bab 6 Serial Communication
      • Bab 7 Analog-to-Digital Conversion
      • Bab 8 Interrupt
      • Bab 9 Timer
      • Bab 10 Pulse-Width Modulation
      • Bab 11 SPI Communication
      • Bab 12 I2C Communication
  • 💰Finance
    • Coding for Finance
      • Build a Bitcoin Price Alert with Google Cloud and Telegram
      • Build a Bitcoin Ticker with ESP32 and Arduino
      • Stock Price Forecasting with LSTM
    • Trading dan Investasi
      • Istilah Ekonomi, Keuangan, Bisnis, Trading, dan Investasi
      • Jalan Menuju Financial Abundance
      • Memahami Korelasi Emas, Oil, Dollar, BTC, Bonds, dan Saham
      • Mindset Trading dan Investasi
      • Rangkuman Buku: Rahasia Analisis Fundamental Saham
      • Rangkuman Buku: The Psychology of Money
      • Rangkuman Kuliah: Introduction to Adaptive Markets
      • Rumus Menjadi Orang Kaya
  • 📝Life
    • Life Quotes
Powered by GitBook
On this page
  • Setup Free Tier Google Cloud
  • Install Miniconda
  • Setup Telegram Bot
  • Send Telegram Message from Python
  • Setup CronJob
  • Get Data from REST API
  • Bitcoin Price Alert
  • References
  1. Finance
  2. Coding for Finance

Build a Bitcoin Price Alert with Google Cloud and Telegram

PreviousCoding for FinanceNextBuild a Bitcoin Ticker with ESP32 and Arduino

Last updated 11 months ago

In this article, I am going to share how to build a Telegram bot for Bitcoin price alerts. The code is written in Python. I use the free tier Google Cloud to run the Python code 24/7 for free. The Python code will read the Bitcoin price from the Binance API, and then it will send a message alert to our Telegram account.

Setup Free Tier Google Cloud

First, we have to create and setup a free tier Google Cloud. You can follow the following . With this setup, we can use Google Cloud for free even after the trial period is over.

Install Miniconda

First, we have to download and install Miniconda. To download the Miniconda installer, we can use wget commad. If this command is not installed yet, you can install it with the following command:.

sudo apt-get install wget

Download Miniconda installer with the following command:

wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh

To install Miniconda, execute the following command:

bash Miniconda3-latest-Linux-x86_64.sh

I installed Miniconda in the following location:

/home/weenslab/miniconda3

After the installation process is finished, we have to restart the Google VM.

Setup Telegram Bot

Talk with the BotFather on Telegram () and then create a new bot. You will get a token to access the HTTP API of the bot.

Install Telegram library for Python from the Google VM with the following command:

pip install telegram-send

Configure telegram-send with your bot using the token with the following command. Then, you will be asked for the token. After that, you will be asked to add your bot on Telegram and send it the given password. After finising, you are ready to use telegram-send.

telegram-send --configure

To test the connection with your Telegram account, you can execute this command from the Google VM:

telegram-send "Hello, World!"

Send Telegram Message from Python

Now, our Google VM is connected to our Telegram account. Next, we can try to send message from a Python program. This is the Python code:

telegram_test.py
import telegram_send
import asyncio
import datetime
from pytz import timezone

async def main():
        now_utc = datetime.datetime.now(timezone('UTC'))
        now_wib = now_utc.astimezone(timezone('Asia/Jakarta'))
        ct = now_wib.strftime("%d %b %Y, %H:%M")
        await telegram_send.send(messages=["Current time: " + ct])

asyncio.run(main())

Run this Python code with the following command:

python telegram_test.py

Setup CronJob

CronJob creates jobs on a repeating schedule. We can use CronJob to execute our Python code at regular intervals.

To access CronJob setting, use the following command:

crontab -e

Add this setting in order to execute the code every 1 hour.

0 * * * * /home/weenslab/miniconda3/bin/python telegram_test.py # every 1 hour

Get Data from REST API

To get data from REST API, we have to install requests library for Python:

pip install requests

Use the following code to test the library with Binance API:

binance_api_test.py
import requests

api_url = "https://api.binance.us/api/v3/ticker/price?symbol=BTCUSDT"
response = requests.get(api_url)
print(response.json().get('symbol') + ': ' + response.json().get('price'))

Bitcoin Price Alert

This code combines all the functionality to get price data from Binance and then send it to the Telegram account.

price_alert.py
import datetime
from pytz import timezone
import requests
import telegram_send
import asyncio

async def main():
        now_utc = datetime.datetime.now(timezone('UTC'))
        now_wib = now_utc.astimezone(timezone('Asia/Jakarta'))
        ct = now_wib.strftime("%d %b %Y, %H:%M")

        coin_list = ['BTCUSDT','ETHUSDT']
        api_url = "https://api.binance.us/api/v3/ticker/price?symbol="
        responses = []
        for c in coin_list:
                #print(api_url + c)
                responses.append(requests.get(api_url + c))

        msg = "My Price Alert\n" + ct + "\n"
        for r in responses:
                msg = msg + r.json().get("symbol") + ": " + str(float(r.json().get("price"))) + "\n"
        #print(msg)

        await telegram_send.send(messages=[msg])

asyncio.run(main())

After that, we can setup the CronJob to automate the Python code execution, for example:

* * * * * /home/weenslab/miniconda3/bin/python price_alert.py # every 1 minute
0 * * * * /home/weenslab/miniconda3/bin/python price_alert.py # every 1 hour
0 */3 * * * /home/weenslab/miniconda3/bin/python price_alert.py # every 3 hour

This figure shows the message in Telegram.

References

Details of the CronJob time format can be studied at .

Google Cloud Platform (Free Tier) WordPress Setup,

Python in the Cloud Part I - How to run Python in the Cloud / Conda environment setup on GCP,

crontab guru,

💰
tutorial
https://telegram.me/BotFather
crontab.guru
https://www.youtube.com/watch?v=v46bRHR1Pq0
https://www.youtube.com/watch?v=lIJlhKrP_SI
https://crontab.guru/
coding_for_finance/telegram_bitcoin_price_alert/price_alert.py at main · weenslab/coding_for_financeGitHub
Logo