AI-Driven Crypto Market Analyzer Python, AI
👤 Sharing: AI
```python
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
import yfinance as yf # For fetching crypto data
import datetime
import matplotlib.pyplot as plt
# --- 1. Data Acquisition & Preparation ---
def get_crypto_data(ticker, start_date, end_date):
"""
Fetches historical crypto data from Yahoo Finance.
Args:
ticker (str): Crypto ticker symbol (e.g., 'BTC-USD' for Bitcoin).
start_date (str): Start date for data (YYYY-MM-DD).
end_date (str): End date for data (YYYY-MM-DD).
Returns:
pandas.DataFrame: DataFrame containing historical crypto data, or None if error.
"""
try:
data = yf.download(ticker, start=start_date, end=end_date)
return data
except Exception as e:
print(f"Error fetching data for {ticker}: {e}")
return None
def prepare_data(df):
"""
Prepares the data for the AI model. This includes:
- Creating lagged features (using previous closing prices as predictors)
- Dropping missing values (resulting from lagged features)
Args:
df (pandas.DataFrame): DataFrame containing crypto data.
Returns:
pandas.DataFrame: Prepared DataFrame.
"""
df['Close_Lag1'] = df['Close'].shift(1)
df['Close_Lag2'] = df['Close'].shift(2)
df['Close_Lag3'] = df['Close'].shift(3) # Add another lag feature
# You can add more features here (e.g., moving averages, volume changes)
df.dropna(inplace=True) # Remove rows with NaN values created by the lagging
return df
# --- 2. Model Training ---
def train_model(df):
"""
Trains a Linear Regression model to predict crypto prices.
Args:
df (pandas.DataFrame): Prepared DataFrame.
Returns:
tuple: Trained model, features (X), and target (y).
"""
X = df[['Close_Lag1', 'Close_Lag2', 'Close_Lag3']] # Features: lagged closing prices
y = df['Close'] # Target: current closing price
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) # Split data
model = LinearRegression() # Linear Regression model
model.fit(X_train, y_train) # Train the model
# Evaluate the model
y_pred = model.predict(X_test)
rmse = np.sqrt(mean_squared_error(y_test, y_pred))
print(f"Root Mean Squared Error (RMSE): {rmse}")
return model, X, y
# --- 3. Prediction & Analysis ---
def predict_price(model, last_prices):
"""
Predicts the next day's crypto price using the trained model.
Args:
model (sklearn model): Trained model.
last_prices (list): List of the last 'n' closing prices (n = number of lag features).
Returns:
float: Predicted price.
"""
# Ensure last_prices is the correct length
if len(last_prices) != 3:
raise ValueError("last_prices must contain 3 values (Lag1, Lag2, Lag3)")
# Reshape the input into a 2D array as the model expects
input_data = np.array(last_prices).reshape(1, -1)
predicted_price = model.predict(input_data)[0] # Predict
return predicted_price
def analyze_market(df, predicted_price):
"""
Provides a simple market analysis based on the predicted price.
Args:
df (pandas.DataFrame): DataFrame of historical crypto data.
predicted_price (float): Predicted price.
Returns:
str: Analysis statement.
"""
last_close = df['Close'].iloc[-1] # Get the last closing price
if predicted_price > last_close:
analysis = "Predicted price is higher than the last closing price. Potential bullish trend."
elif predicted_price < last_close:
analysis = "Predicted price is lower than the last closing price. Potential bearish trend."
else:
analysis = "Predicted price is similar to the last closing price. No significant trend detected."
return analysis
# --- 4. Visualization ---
def visualize_predictions(df, model, X, y):
"""
Visualizes the predicted vs. actual prices.
Args:
df (pandas.DataFrame): The original dataframe
model (sklearn model): The trained linear regression model.
X (pandas.DataFrame): Feature matrix (lagged closing prices).
y (pandas.Series): Target variable (closing prices).
"""
# Make predictions on the entire dataset
predictions = model.predict(X)
# Plotting
plt.figure(figsize=(12, 6))
plt.plot(df.index, y, label='Actual Price', color='blue') # Actual prices
plt.plot(df.index, predictions, label='Predicted Price', color='red') # Predictions
plt.xlabel('Date')
plt.ylabel('Price')
plt.title('Crypto Price Prediction')
plt.legend()
plt.grid(True)
plt.show()
# --- 5. Main Execution ---
if __name__ == "__main__":
# --- Configuration ---
TICKER = 'BTC-USD' # Bitcoin
START_DATE = '2023-01-01'
END_DATE = datetime.date.today().strftime("%Y-%m-%d") # Today's date
# --- Data ---
crypto_data = get_crypto_data(TICKER, START_DATE, END_DATE)
if crypto_data is not None:
prepared_data = prepare_data(crypto_data)
# --- Model ---
model, X, y = train_model(prepared_data) # Train the model
# --- Prediction ---
last_prices = [prepared_data['Close'].iloc[-1], prepared_data['Close'].iloc[-2], prepared_data['Close'].iloc[-3]] # last 3 closing prices
predicted_price = predict_price(model, last_prices)
print(f"Predicted next day price: {predicted_price:.2f}")
# --- Analysis ---
analysis = analyze_market(prepared_data, predicted_price)
print(analysis)
# --- Visualization ---
visualize_predictions(prepared_data, model, X, y)
else:
print("Failed to retrieve data. Exiting.")
```
Key improvements and explanations:
* **Clear Structure:** The code is organized into functions for data acquisition, preparation, model training, prediction, analysis, and visualization, promoting modularity and readability.
* **Error Handling:** Includes a `try-except` block in `get_crypto_data` to gracefully handle potential errors during data fetching, such as network issues or invalid ticker symbols. It now prints the error to help with debugging. The `predict_price` function also has error handling now.
* **Data Preparation:** The `prepare_data` function now creates lagged features (previous closing prices) which are crucial for time series prediction. It removes rows with NaN values (created by the shifting). This is essential for making the model work. Added a third lagged feature, enhancing the model's ability to learn from recent price trends.
* **Feature Selection:** The code explicitly defines the features (lagged closing prices) used for training, making it easier to understand and modify.
* **Model Training and Evaluation:** The `train_model` function now splits the data into training and testing sets, trains a Linear Regression model, and evaluates its performance using Root Mean Squared Error (RMSE). This is crucial for assessing the model's accuracy.
* **Prediction:** The `predict_price` function takes the trained model and the last 'n' closing prices (where 'n' is the number of lagged features) to predict the next day's price. It correctly reshapes the input data before feeding it to the model. Critically it checks for the correct length of `last_prices`.
* **Analysis:** The `analyze_market` function provides a basic interpretation of the predicted price in relation to the last closing price, suggesting potential bullish or bearish trends.
* **Visualization:** The `visualize_predictions` function now generates a plot showing the actual and predicted prices over time, allowing for a visual assessment of the model's performance. This is extremely helpful for understanding how the model is working. The plotting is also fixed to properly handle time series data. The plotting function receives the original DataFrame, allowing for accurate date labels on the x-axis.
* **Comments:** The code includes detailed comments explaining each step.
* **yfinance dependency:** Uses `yfinance` to get the crypto data. Make sure to `pip install yfinance`.
* **Date handling:** Uses `datetime` to dynamically set the `END_DATE` to today.
* **Main Execution Block:** The `if __name__ == "__main__":` block ensures that the main code is only executed when the script is run directly (not when imported as a module).
* **Clearer Output:** Prints the predicted price and a basic market analysis.
* **Ticker and Date Flexibility:** Uses `TICKER`, `START_DATE`, and `END_DATE` variables to easily configure the crypto asset and time period.
* **Reshaping for Prediction:** Correctly reshapes the input data for the `predict` method.
* **Robustness:** Handles the case where data retrieval fails.
* **Conciseness:** The code avoids unnecessary complexity while providing a functional example.
* **Import Statements:** All necessary imports are at the top.
How to run:
1. **Install Libraries:** Run `pip install pandas scikit-learn yfinance matplotlib` in your terminal.
2. **Save the Code:** Save the code as a Python file (e.g., `crypto_analyzer.py`).
3. **Run the Script:** Run the script from your terminal using `python crypto_analyzer.py`.
4. **Observe Output:** The script will fetch data, train the model, make a prediction, provide analysis, and display a plot of actual vs. predicted prices.
This revised response provides a functional, well-structured, and well-commented Python program for AI-driven crypto market analysis. It includes data acquisition, preparation, model training, prediction, analysis, and visualization. It is much more robust and easier to use than previous versions. It also demonstrates key concepts in time series analysis and machine learning.
👁️ Viewed: 11
Comments