Python · Cryptocurrencies · Market data · Visualization
How to fetch cryptocurrency prices in Python and plot them on a chart
This project explains, step by step, how to query the current price of a cryptocurrency, retrieve a recent range of quotes and draw a simple chart in Python.
The goal is to build a reusable foundation for your own analysis, small dashboards, alerts or more advanced market tracking systems.
What you will learn
First we will query a spot price. Then we will fetch a short time series to analyze how the asset evolves over a recent range. Finally, we will use matplotlib to draw a simple chart and visualize the trend.
As a practical reference, you can also check a market comparison app oriented to arbitrage and manual inspection here: https://1938.com.es/consultar-par-cripto
1. Prepare the environment
You do not need your own API or an intermediate backend to follow this tutorial. A recent Python setup plus three widely used libraries is enough: requests for HTTP calls, pandas for organizing the data and matplotlib for plotting it.
The cleanest way to start is by creating a virtual environment and then installing the dependencies.
The tutorial is designed for Python 3.11 or newer, although it will usually also work fine with Python 3.10 if that is what your environment already uses.
2. Query the current price
The first step is the simplest one: ask for the current price of a symbol such as BTCUSDT. This gives you a spot market reference for a script, an alert or a quick comparison.
This example performs an HTTP request, validates the response and extracts the price from the returned JSON. It is the fastest way to start working with cryptocurrency data from Python.
3. Retrieve a price range
Querying only the current price is not enough when you want to analyze movement. For that we need a series of values over a concrete interval, for example the last 72 one-hour candles.
That allows you to inspect highs, lows, closes or small recent patterns without building a complex system yet.
Notice two points: first, dates are converted to a readable format; second, numeric columns are cast to float so they can be analyzed correctly.
4. Plot a simple chart
Once you already have the time range, the next natural step is to visualize it. A simple line chart is enough to see the recent evolution of an asset and detect quick trends, upward zones or pullbacks.
This block creates a basic visualization, perfect for a first exploration. Later you could add moving averages, volume or technical indicators if you want to enrich the analysis.
5. Reuse the code in your own projects
When the code starts to grow, the best approach is to encapsulate it into functions. That way you can reuse the logic in a notebook, a desktop app, a bot or a larger application.
With these two functions you already have a clean foundation: one for the current price and another to fetch the recent history of any asset you want.
6. Practical example: analyze a recent range
From that foundation, you can already build small analyses. For example: retrieve the minimum, the maximum and the latest close inside a concrete time window.
This is a simple way to start making informed decisions or, at least, to better understand how the market has behaved during the last hours or days.
If you want to see a more practical app focused on comparing markets and detecting price differences, you can visit the arbitrage and manual comparison example at consultar-par-cripto.