Predicting Ethereum Prices with AI using Python & CoinGecko API
In the mesmerizing world of cryptocurrency, Ethereum stands as a beacon of innovation and promise. With its fluctuating prices and the rollercoaster of market dynamics, the quest to predict its future value has fascinated many. Enter the realm of artificial intelligence (AI), where complex algorithms and machine learning models promise to cast light on the path Ethereum’s price might take. Today, we delve into an intriguing script that does just that — forecasts Ethereum prices over the next five years, using a method as captivating as the cryptocurrency itself.
Our journey begins with fetching historical Ethereum price data from CoinGecko, a treasure trove of cryptocurrency information. This step sets the stage, providing the raw material — daily prices of Ethereum in USD — necessary for our AI model to learn from the past.
You will need a CoinGeck API Key, which can be retrieved here: https://www.coingecko.com/en/api/pricing. Get 20% off any CoinGecko API plan by applying RAMIJALOUDI20 at checkout.
Once we’ve gathered our data, the next step involves a delicate process known as preprocessing. Here, we use a MinMaxScaler to transform the price data, ensuring that each value is scaled down to a range between 0 and 1. This normalization is crucial for the AI to process the information effectively, allowing it to discern patterns without getting overwhelmed by the sheer scale of the numbers involved.
The heart of our endeavor lies in creating a dataset specifically tailored for an LSTM (Long Short-Term Memory) model. LSTM models are a special kind of AI wizardry, designed to remember information for extended periods. By feeding it sequences of past prices, we train it to predict future ones. This process involves dividing our scaled data into small, digestible chunks that the model can learn from, predicting the next price point based on a sequence of previous ones.
With our dataset ready, we summon the LSTM model into existence. It’s a structure composed of layers, including LSTM layers capable of remembering long-term dependencies, and Dense layers that help in making predictions. As we train this model on our dataset, it learns to understand the subtle dance of Ethereum’s price movements, readying itself to forecast the future.
The climax of our tale is the prediction phase. Here, our trained model embarks on a quest to forecast Ethereum prices every 30 days for the next five years. It’s a bold endeavor, akin to charting a course through uncharted waters, with each prediction based on the model’s understanding of the past and its most recent forecasts.
As our model peeks into the future, it generates a series of predicted prices, which we then transform back into their original USD scale. This allows us to witness the model’s vision for Ethereum’s price trajectory — month by month, for five years into the future.
Our journey culminates in the creation of a histogram, a visual representation of the predicted prices. This histogram serves as a map of potential futures, showing us the distribution of where Ethereum’s price might head. It’s a fascinating glimpse into the possibilities that lie ahead, presented in a form that’s both informative and intriguing.
The purpose of this article, and the code it describes, is not just to predict the future of Ethereum’s price. It’s about showcasing the power of AI and machine learning in deciphering patterns in data that are invisible to the naked eye. It’s a testament to human ingenuity and our quest to understand and anticipate the waves of change in the cryptocurrency ocean.
For readers venturing into the world of cryptocurrency and AI, this article serves as a beacon, illuminating the path where technology meets finance. It’s a tale of how we can harness the power of machine learning to explore the future, making the unknown a little more known. So, as we stand on the precipice of the future, gazing out at the horizon of possibilities, let’s marvel at the journey of predicting Ethereum’s price — a journey where technology and curiosity converge to peer into the heart of the cryptocurrency market.
import numpy as np
import pandas as pd
import requests
from datetime import datetime, timedelta
from sklearn.preprocessing import MinMaxScaler
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense
import matplotlib.pyplot as plt
# Function to fetch Ethereum prices from CoinGecko
def fetch_ethereum_prices():
url = 'https://api.coingecko.com/api/v3/coins/ethereum/market_chart?vs_currency=usd&days=max&interval=daily'
response = requests.get(url)
data = response.json()
prices = data['prices']
df = pd.DataFrame(prices, columns=['timestamp', 'price'])
df['date'] = pd.to_datetime(df['timestamp'], unit='ms')
df = df.set_index('date')
df.drop('timestamp', axis=1, inplace=True)
return df
# Preprocess data
def preprocess_data(df):
scaler = MinMaxScaler(feature_range=(0, 1))
scaled_data = scaler.fit_transform(df['price'].values.reshape(-1,1))
return scaled_data, scaler
# Create dataset for LSTM
def create_dataset(data, look_back=1):
X, Y = [], []
for i in range(len(data)-look_back-1):
a = data[i:(i+look_back), 0]
X.append(a)
Y.append(data[i + look_back, 0])
return np.array(X), np.array(Y)
# Predict future prices
def predict_future_prices(model, data, scaler, days=1825, look_back=1):
future_prices = []
current_batch = data[-look_back:]
for i in range(days // 30): # Predict monthly for 5 years
current_pred = model.predict(current_batch.reshape(1, look_back, 1))
future_prices.append(scaler.inverse_transform(current_pred)[0][0])
current_batch = np.append(current_batch[1:], current_pred)
return future_prices
df = fetch_ethereum_prices()
scaled_data, scaler = preprocess_data(df)
X, Y = create_dataset(scaled_data, look_back=1)
X = np.reshape(X, (X.shape[0], 1, X.shape[1]))
# Build and train LSTM model
model = Sequential()
model.add(LSTM(units=50, return_sequences=False, input_shape=(1, 1)))
model.add(Dense(1))
model.compile(optimizer='adam', loss='mean_squared_error')
model.fit(X, Y, epochs=20, batch_size=32, verbose=1)
# Predict future prices
future_prices = predict_future_prices(model, scaled_data, scaler)
# Print predicted prices
for i, price in enumerate(future_prices, 1):
print(f"Month {i}: ${price:.2f}")
# Save predicted prices to CSV
pd.DataFrame(future_prices, columns=['Predicted Price']).to_csv('predicted_ethereum_prices.csv', index=False)
# Plot histogram of the predicted prices
plt.hist(future_prices, bins=30)
plt.title('Histogram of Projected Ethereum Prices Over the Next 5 Years')
plt.xlabel('Price in USD')
plt.ylabel('Frequency')
plt.show()
This script is designed to predict the future prices of Ethereum using historical price data, employing a Long Short-Term Memory (LSTM) neural network. Here’s an explanation of its components, line by line:
Import statements: These lines import necessary libraries. numpy
and pandas
for data manipulation, requests
for HTTP requests to fetch data, datetime
and timedelta
for handling dates, MinMaxScaler
from scikit-learn for scaling data, TensorFlow's Sequential
model and layers (LSTM
, Dense
) for building the neural network, and matplotlib.pyplot
for plotting.
fetch_ethereum_prices
function: Defines a function to fetch Ethereum price data from the CoinGecko API, returning a pandas DataFrame. The API URL requests daily price data in USD. The response is converted into a DataFrame, with timestamps converted to datetime objects for easier manipulation, and the DataFrame is indexed by date.
preprocess_data
function: Defines a function that scales the Ethereum prices between 0 and 1. This is a common preprocessing step for neural network inputs to help the model learn more effectively.
create_dataset
function: This function prepares the data for the LSTM model. It creates sequences (X
) of historical prices of length look_back
to predict the next price (Y
). These sequences are what the LSTM model will learn from.
predict_future_prices
function: This function uses the trained LSTM model to predict future Ethereum prices. It iteratively generates predictions, each time using the most recent prediction as part of the input for the next prediction, simulating a step-forward prediction for 5 years at a monthly interval.
Fetching and preprocessing data: Calls fetch_ethereum_prices
to get historical data, then preprocesses this data using preprocess_data
, scaling the prices and preparing the data for the LSTM model.
Creating the LSTM dataset: Calls create_dataset
to transform the scaled data into a sequence format that the LSTM model can learn from, then reshapes it for compatibility with the LSTM's expected input shape.
Building and training the LSTM model: Initializes a Sequential
model with an LSTM layer followed by a Dense output layer. The model is compiled with the Adam optimizer and mean squared error loss function, then trained on the prepared dataset.
Predicting future prices: Calls predict_future_prices
with the trained model and scaled data to generate future Ethereum price predictions for the next 5 years, monthly.
Printing predicted prices: Iterates through the predicted prices, printing them out with a label indicating the month since the start of predictions.
Saving predicted prices to a CSV file: Saves the predicted prices into a CSV file for further analysis or sharing.
Plotting a histogram of predicted prices: Uses matplotlib to plot a histogram of the predicted Ethereum prices, providing a visual representation of the distribution of predicted prices over the next 5 years.
This script encapsulates the entire process of forecasting Ethereum prices using a machine learning model, from data fetching and preprocessing to training the model and visualizing the predictions. It demonstrates how to apply LSTM neural networks to time series prediction problems in the context of cryptocurrency price forecasting.
Below is a sample of the output after running the above python script:
This code is also available here: https://colab.research.google.com/drive/1HBYXpgA-K8W0lKB8Tz9p5pKg-1YYhFXz#scrollTo=lbB9cEFhupBX