{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# StockTeller Analyzer\n", "\n", "This notebook demonstrates stock data analysis and prediction." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "source": [ "# Install required packages\n", "!pip install pandas numpy matplotlib scikit-learn yfinance" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "source": [ "import pandas as pd\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "from datetime import datetime, timedelta\n", "from sklearn.linear_model import LinearRegression\n", "import yfinance as yf\n", "\n", "# Set plot style\n", "plt.style.use('ggplot')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Define Functions for Stock Analysis" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "source": [ "# Function to load stock data\n", "def load_stock_data(symbol, period='1mo'):\n", " \"\"\"\n", " Load stock data from Yahoo Finance\n", " \"\"\"\n", " try:\n", " # Try to get data from Yahoo Finance\n", " data = yf.download(symbol, period=period)\n", " # Create a simpler dataframe\n", " stock_data = pd.DataFrame({\n", " 'date': data.index,\n", " 'price': data['Close'],\n", " 'volume': data['Volume'],\n", " })\n", " stock_data.reset_index(inplace=True, drop=True)\n", " \n", " # Calculate daily change\n", " stock_data['change'] = stock_data['price'].diff()\n", " stock_data['changePercent'] = stock_data['price'].pct_change() * 100\n", " \n", " # Fill NaN values in first row\n", " stock_data.loc[0, 'change'] = 0\n", " stock_data.loc[0, 'changePercent'] = 0\n", " \n", " # Add symbol column\n", " stock_data['symbol'] = symbol\n", " \n", " return stock_data\n", " except Exception as e:\n", " print(f\"Error fetching data from Yahoo Finance: {e}\")\n", " # Generate mock data if there's an error\n", " return generate_mock_stock_data(symbol)\n", "\n", "# Function to generate mock stock data\n", "def generate_mock_stock_data(symbol, days=30):\n", " \"\"\"\n", " Generate mock stock data for demonstration\n", " \"\"\"\n", " today = datetime.now()\n", " dates = [today - timedelta(days=i) for i in range(days)]\n", " dates.reverse() # Oldest first\n", " \n", " # Start with a random price\n", " base_price = np.random.uniform(50, 550)\n", " prices = [base_price]\n", " \n", " # Generate price series with some randomness\n", " for i in range(1, days):\n", " change = np.random.normal(0, 0.02) * prices[-1] # 2% standard deviation\n", " new_price = max(prices[-1] + change, 1) # Ensure price doesn't go below 1\n", " prices.append(new_price)\n", " \n", " # Generate volumes\n", " volumes = np.random.randint(500000, 10000000, size=days)\n", " \n", " # Calculate changes\n", " changes = [0] # First day has no change\n", " for i in range(1, days):\n", " changes.append(prices[i] - prices[i-1])\n", " \n", " # Calculate percent changes\n", " pct_changes = [0] # First day has no change\n", " for i in range(1, days):\n", " pct_changes.append((changes[i] / prices[i-1]) * 100)\n", " \n", " # Create DataFrame\n", " data = pd.DataFrame({\n", " 'date': dates,\n", " 'price': prices,\n", " 'volume': volumes,\n", " 'change': changes,\n", " 'changePercent': pct_changes,\n", " 'symbol': symbol\n", " })\n", " \n", " return data\n", "\n", "# Function to predict stock prices\n", "def predict_stock_prices(data, days_to_predict=7):\n", " \"\"\"\n", " Predict future stock prices based on historical data\n", " \"\"\"\n", " # Make a copy of the data to avoid modifying the original\n", " df = data.copy()\n", " \n", " # Get the last date and price from our data\n", " last_date = df['date'].iloc[-1] # Already a datetime object\n", " last_price = df['price'].iloc[-1]\n", " \n", " # Prepare data for prediction\n", " X = np.array(range(len(df))).reshape(-1, 1)\n", " y = df['price'].values\n", " \n", " # Fit linear regression model\n", " model = LinearRegression()\n", " model.fit(X, y)\n", " \n", " # Prepare prediction dataframe\n", " predictions = []\n", " \n", " # Generate prediction dates (skip weekends)\n", " for i in range(1, days_to_predict + 1):\n", " next_date = last_date + timedelta(days=i)\n", " \n", " # Skip weekends\n", " while next_date.weekday() >= 5: # 5 = Saturday, 6 = Sunday\n", " next_date = next_date + timedelta(days=1)\n", " \n", " # Add some randomness to the prediction\n", " future_X = np.array([[len(df) + i - 1]]) # Adjust for 0-based indexing\n", " predicted_price = model.predict(future_X)[0]\n", " \n", " # Add some random variation (±2%)\n", " random_factor = np.random.uniform(0.98, 1.02)\n", " predicted_price = predicted_price * random_factor\n", " \n", " # Calculate change\n", " if i == 1:\n", " change = predicted_price - last_price\n", " else:\n", " change = predicted_price - predictions[-1]['price']\n", " \n", " # Calculate percent change\n", " if i == 1:\n", " pct_change = (change / last_price) * 100\n", " else:\n", " pct_change = (change / predictions[-1]['price']) * 100\n", " \n", " # Add to predictions list\n", " predictions.append({\n", " 'date': next_date,\n", " 'price': predicted_price,\n", " 'volume': np.random.randint(500000, 10000000),\n", " 'change': change,\n", " 'changePercent': pct_change,\n", " 'symbol': df['symbol'].iloc[0],\n", " 'isPredicted': True\n", " })\n", " \n", " # Convert to DataFrame\n", " prediction_df = pd.DataFrame(predictions)\n", " \n", " return prediction_df\n", "\n", "# Function to visualize stock data\n", "def visualize_stock_data(stock_data, predicted_data=None, ticker='AAPL'):\n", " \"\"\"\n", " Create visualization for stock data and predictions\n", " \"\"\"\n", " plt.figure(figsize=(14, 10))\n", " \n", " # Plot price\n", " plt.subplot(2, 1, 1)\n", " plt.plot(stock_data['date'], stock_data['price'], label='Historical Price', color='blue')\n", " \n", " # Plot moving averages\n", " ma7 = stock_data['price'].rolling(window=7).mean()\n", " ma30 = stock_data['price'].rolling(window=30).mean() if len(stock_data) >= 30 else None\n", " \n", " plt.plot(stock_data['date'], ma7, label='7-Day MA', color='orange')\n", " if ma30 is not None:\n", " plt.plot(stock_data['date'], ma30, label='30-Day MA', color='green')\n", " \n", " # Plot predictions if available\n", " if predicted_data is not None and not predicted_data.empty:\n", " plt.plot(predicted_data['date'], predicted_data['price'], 'r--', label='Predicted Price')\n", " \n", " # Shade the prediction area\n", " plt.axvspan(predicted_data['date'].min(), predicted_data['date'].max(), \n", " alpha=0.1, color='red')\n", " \n", " plt.title(f'{ticker} Stock Price Analysis')\n", " plt.xlabel('Date')\n", " plt.ylabel('Price ($)')\n", " plt.legend()\n", " plt.grid(True, alpha=0.3)\n", " \n", " # Plot volume\n", " plt.subplot(2, 1, 2)\n", " plt.bar(stock_data['date'], stock_data['volume'], color='lightblue', alpha=0.7)\n", " plt.title(f'{ticker} Trading Volume')\n", " plt.xlabel('Date')\n", " plt.ylabel('Volume')\n", " plt.grid(True, alpha=0.3)\n", " \n", " plt.tight_layout()\n", " plt.show()\n", " \n", " # Print prediction summary if available\n", " if predicted_data is not None and not predicted_data.empty:\n", " print(\"\\nPredicted prices for the next days:\")\n", " for i, row in predicted_data.iterrows():\n", " print(f\"{row['date'].strftime('%Y-%m-%d')}: ${row['price']:.2f} (Change: {row['change']:.2f}, {row['changePercent']:.2f}%)\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Load and Analyze Stock Data" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "source": [ "# Set the ticker to analyze\n", "ticker = 'AAPL'\n", "\n", "# Load stock data\n", "print(f\"Loading data for {ticker}...\")\n", "stock_data = load_stock_data(ticker, period='3mo')\n", "\n", "# Display the first few rows\n", "stock_data.head()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "source": [ "# Generate predictions\n", "print(\"Generating predictions...\")\n", "predicted_data = predict_stock_prices(stock_data, days_to_predict=7)\n", "\n", "# Display predicted data\n", "predicted_data" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "source": [ "# Visualize the data and predictions\n", "visualize_stock_data(stock_data, predicted_data, ticker)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Calculate Key Statistics" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "source": [ "# Calculate some key statistics\n", "latest_price = stock_data['price'].iloc[-1]\n", "max_price = stock_data['price'].max()\n", "min_price = stock_data['price'].min()\n", "avg_volume = stock_data['volume'].mean()\n", "price_change = stock_data['price'].iloc[-1] - stock_data['price'].iloc[0]\n", "price_change_pct = (price_change / stock_data['price'].iloc[0]) * 100\n", "\n", "print(f\"Latest Price: ${latest_price:.2f}\")\n", "print(f\"High: ${max_price:.2f}\")\n", "print(f\"Low: ${min_price:.2f}\")\n", "print(f\"Average Volume: {avg_volume:,.0f}\")\n", "print(f\"Price Change: ${price_change:.2f} ({price_change_pct:.2f}%)\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Export Data to CSV" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "source": [ "# Export to CSV\n", "stock_data.to_csv(f\"{ticker}_historical.csv\", index=False)\n", "predicted_data.to_csv(f\"{ticker}_predictions.csv\", index=False)\n", "print(f\"Data exported to {ticker}_historical.csv and {ticker}_predictions.csv\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Try Different Stocks" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "source": [ "# Function to analyze a new stock\n", "def analyze_stock(symbol, period='3mo', days_to_predict=7):\n", " # Load data\n", " data = load_stock_data(symbol, period=period)\n", " \n", " # Generate predictions\n", " predictions = predict_stock_prices(data, days_to_predict=days_to_predict)\n", " \n", " # Visualize\n", " visualize_stock_data(data, predictions, symbol)\n", " \n", " # Calculate key stats\n", " latest_price = data['price'].iloc[-1]\n", " max_price = data['price'].max()\n", " min_price = data['price'].min()\n", " avg_volume = data['volume'].mean()\n", " price_change = data['price'].iloc[-1] - data['price'].iloc[0]\n", " price_change_pct = (price_change / data['price'].iloc[0]) * 100\n", " \n", " print(f\"Latest Price: ${latest_price:.2f}\")\n", " print(f\"High: ${max_price:.2f}\")\n", " print(f\"Low: ${min_price:.2f}\")\n", " print(f\"Average Volume: {avg_volume:,.0f}\")\n", " print(f\"Price Change: ${price_change:.2f} ({price_change_pct:.2f}%)\")\n", " \n", " return data, predictions" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "source": [ "# Analyze a different stock\n", "# Uncomment and change the symbol to analyze\n", "# analyze_stock('MSFT')" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.10" } }, "nbformat": 4, "nbformat_minor": 4 }