Live demo: stock-ml.gvonb.dev
This Python script predicts when a stock's 150-day moving average will reach a given target price using linear regression. It fetches historical stock data via yfinance, performs calculations, and presents the results in a clean, easy-to-read table format.
- Fetches real-time stock data from Yahoo Finance.
- Calculates 50-day and 150-day moving averages.
- Uses linear regression to predict the crossover date.
- Beautiful terminal output with ANSI colors and a formatted table.
If you're new to Python and Git, follow these steps to install and run the project.
Ensure you have Python 3.8+ installed. Check with:
python3 --versionIf Python is not installed, download it from python.org.
If you have Git installed, run:
git clone https://github.com/GVonB/stock-moving-average-ml.git
cd stock-moving-average-mlIf you donβt have Git, download the ZIP file from GitHub and extract it manually.
python3 -m venv venv
source venv/bin/activate # macOS/Linux
venv\Scripts\activate # Windowspip install -r requirements.txtRun the script using the command:
python3 prediction.py <STOCK_TICKER> <TARGET_PRICE>python3 prediction.py NVDA 150This command predicts when NVIDIA (NVDA) will have a 150-day moving average reaching $150.00.
Fetching stock data for NVDA...
π Stock Analysis for NVDA
βββββββββββββββββββββββββββββββ€ββββββββββββββ
β Stock Ticker β NVDA β
βββββββββββββββββββββββββββββββΌββββββββββββββ€
β Current Price β $116.66 β
βββββββββββββββββββββββββββββββΌββββββββββββββ€
β Target Moving Average β $150.00 β
βββββββββββββββββββββββββββββββΌββββββββββββββ€
β Projected Days Until Target β 197.52 days β
βββββββββββββββββββββββββββββββ§ββββββββββββββ
π Based on current trends, NVDA is expected to hit $150.0 in approximately 197.52 days!Make sure you're entering a valid ticker symbol (e.g., AAPL, TSLA, GOOGL).
Run:
pip install --upgrade -r requirements.txtCurrently, the script is set to 150-day MA, but you can modify predict_ma_hit() to use custom time frames.
The necessary format is shown in the 50-day MA, which is currently calculated but unused.
Feel free to submit pull requests or open issues for improvements.
This project is open-source under the MIT License.