DEV Community

Lucas Gragg
Lucas Gragg

Posted on

Building a real-time Flask dashboard for your trading bot

When I first started building my trading bot, I quickly realized that I needed a way to visualize its performance in real-time. I wanted to be able to see the bot's current trades, its profit and loss, and any errors that might be occurring. After some research, I decided to use Flask to build a simple web dashboard that would display this information. In this post, I'll walk through how I built the dashboard and some of the challenges I encountered along the way.

Designing the Dashboard

The first step in building the dashboard was to decide what information I wanted to display. I knew I wanted to show the bot's current trades, including the symbol, entry price, and current profit and loss. I also wanted to display any errors that might be occurring, such as connection issues with the exchange or problems with the bot's trading logic. I decided to use a simple table to display this information, with each row representing a single trade.

Building the Dashboard

To build the dashboard, I used Flask to create a simple web server that would display the trade information. I used the flask library to create the server, and the jinja2 library to render the HTML templates. I also used the ccxt library to connect to the exchange and retrieve the trade information. Here's an example of how I used these libraries to display the trade information:

from flask import Flask, render_template
import ccxt

app = Flask(__name__)

# Connect to the exchange
exchange = ccxt.binance({
    'apiKey': 'YOUR_API_KEY',
    'apiSecret': 'YOUR_API_SECRET',
})

# Define a function to retrieve the trade information
def get_trades():
    trades = exchange.fetch_trades('BTC/USDT')
    return trades

# Define a route for the dashboard
@app.route('/')
def dashboard():
    trades = get_trades()
    return render_template('dashboard.html', trades=trades)

if __name__ == '__main__':
    app.run()
Enter fullscreen mode Exit fullscreen mode

This code creates a simple Flask server that connects to the Binance exchange and retrieves the trade information for the BTC/USDT symbol. It then renders an HTML template called dashboard.html and passes the trade information to the template.

Adding Real-time Updates

To add real-time updates to the dashboard, I used the flask-socketio library to create a WebSocket connection between the client and server. This allowed me to push updates from the server to the client in real-time, without the need for the client to constantly poll the server for updates. Here's an example of how I used this library to add real-time updates:

from flask_socketio import SocketIO, emit

# Create a SocketIO instance
socketio = SocketIO(app)

# Define a function to update the trade information
def update_trades():
    trades = get_trades()
    socketio.emit('update_trades', trades)

# Define a route for the dashboard
@app.route('/')
def dashboard():
    trades = get_trades()
    return render_template('dashboard.html', trades=trades)

# Define a SocketIO event to update the trade information
@socketio.on('connect')
def connect():
    update_trades()

# Run the SocketIO instance
if __name__ == '__main__':
    socketio.run(app)
Enter fullscreen mode Exit fullscreen mode

This code creates a SocketIO instance and defines a function to update the trade information. It then defines a route for the dashboard and a SocketIO event to update the trade information when the client connects.

I actually packaged this into a tool called Crypto Exchange Trading Bot (Python) if you want the full working version, which includes a lot of other features like support for multiple exchanges, different trading strategies, and risk management tools. You can check it out at https://lukegraggster.gumroad.com/l/crypto-exchange-bot?utm_source=devto&utm_medium=blog&utm_campaign=traffic_bot. Overall, building a real-time Flask dashboard for my trading bot was a great way to visualize its performance and make adjustments on the fly.

Top comments (0)