بلاگ
Ethereum: matplotlib chart does not display candlestick data only as a vertical line (no wicks, open high, close low)
Here is a step-by-step guide on how to create an Ethereum candlestick chart using matplotlib in Python:
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import numpy as np
import yfinance as yf
from datetime import datetime, timedelta

Get the current time (displayed as x-axis)start_date = datetime.today().strftime('%Y-%m-%d')
end_date = (datetime.today() - timedelta(days=30)).strftime('%Y-%m-%d')
Define the API endpoint for Ethereum pricesapiEndpoint = "
Set the parameters for the API requestparameters = {
'symbol': 'ETH',
'interval': '1m',
'limit': 100,
Maximum number of data points to retrieve'timeRange': start_date + ',' + end_date,
}
Send an API request and get the responseresponse = yf.get(apiEndpoint, params=params)
Convert the response to a pandas DataFramedf = pd.DataFrame(response).T
Create x-axis dates (index) from 1 to 100 (assuming we need at least 100 data points)dates = np.arange(1, length(df.columns))
Set the date format for matplotlibplt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
Draw a candlestick chartplt.figure(figuresize=(14,10))
for i in range (len(dates)-1):
plt.plot([i۰.۰۱, (i+1)۰.۰۱], [df.loc[i,'close'].mean(), df.loc[i+1,'close'].mean() ], color='g')
if not (i == len(df.columns) - 2):
do not plot the last candleplt.plot([i۰.۰۱, i۰.۰۱ + ۰.۰۱], [df.loc[i,'high'].mean(), df.loc[i+1,'high'].mean()], color='g')
elif (i == len(df.columns) - 2):
do not plot the first candleplt.plot([i۰.۰۱, i۰.۰۱ + ۰.۰۲], [df.loc[i,'low'].mean(), df.loc[i+1,'low'].mean()], color='g')
plt.xlabel('Date')
plt.ylabel('Price (USD)')
plt.title("Ethereum Candle Chart")
plt.show()
To run this script, you must have the required libraries (matplotlib
, pandas
, and yfinance
) installed. You also need a Binance API key.
Make sure your Binance API endpoint is set up correctly according to your API documentation.