A command-line tool for computing and exporting market trend strength scores using real financial and macroeconomic data.
This tool calculates a trend strength score ranging from -1 (strong Bear) to +1 (strong Bull) based on:
- Price momentum (fast and slow)
- Market regime classification
- Macroeconomic indicators
- Retrieves real financial data from Financial Modeling Prep (FMP) API
- Fetches macroeconomic indicators from FRED API
- Calculates slow (12-month) and fast (1-month) momentum
- Classifies market regimes (Bull, Bear, Correction, Rebound)
- Computes macroeconomic trend consensus
- Optional PCA analysis of macroeconomic variables
- Exports results to CSV
- Python 3.7+
- Required packages (install with
pip install -r market_trend_requirements.txt):- requests
- pandas
- numpy
- scikit-learn
- statsmodels
- python-dotenv
- cupy-cuda12x (for GPU acceleration)
- Clone the repository
- Install dependencies:
pip install -r market_trend_requirements.txt - Create a
.envfile with your API keys (use.env.templateas a reference):FMP_API_KEY=your_fmp_api_key_here FRED_API_KEY=your_fred_api_key_here
Important: All scripts must be run from the directory where they are located.
Navigate to the directory containing the scripts:
cd /path/to/project/Basic usage:
python market_trend_strength.py --symbol SPY --output trend_scores.csvAll available options:
python market_trend_strength.py --symbol SPY --start 2020-01-01 --end 2023-01-01 --output trend_scores.csv --use-pcaFor automated tasks or production pipelines, use the run_market_trend.py script with the --quiet flag:
python run_market_trend.py --symbol SPY --quiet --output trend_scores.csvThis provides minimal output, showing only the final results and suppresses warnings.
--symbol: Stock or ETF symbol (default: SPY)--start: Start date in YYYY-MM-DD format (default: 1 year before end date)--end: End date in YYYY-MM-DD format (default: today)--output: Output CSV file path (default: trend_strength_scores.csv)--use-pca: Flag to activate PCA functionality for macroeconomic variables--quiet: Suppress all output except final results (only for run_market_trend.py)
The script outputs:
-
To the console:
- Summary of data retrieval
- Table of the most recent trend strength scores
-
To the specified CSV file:
- Date
- Close price
- Market regime
- Slow momentum (12-month return)
- Fast momentum (1-month return)
- Macroeconomic signal
- PCA signal (if enabled)
- Trend strength score
# First, navigate to the directory containing the scripts
cd /path/to/project/
# Then run the analysis
python market_trend_strength.py --symbol AAPL --start 2022-01-01This will:
- Fetch AAPL price data from Jan 1, 2022 to today
- Calculate momentum and classify market regimes
- Retrieve and analyze macroeconomic indicators
- Compute trend strength scores
- Display recent scores and save all results to trend_strength_scores.csv
# Navigate to the script directory
cd /path/to/project/
# Run in quiet mode for production use
python run_market_trend.py --symbol TSLA --start 2023-01-01 --output tesla_trends.csv --quietThis will perform the analysis with minimal output, showing only the final results and saving to tesla_trends.csv.
-
"No such file or directory" error:
- Make sure you're running the script from the correct directory
- Use
cd /path/to/project/to navigate to the directory containing the scripts
-
API Key errors:
- Ensure your
.envfile is in the same directory as the scripts - Verify that your API keys are valid and correctly formatted
- Check that you have sufficient API credits/quota remaining
- Ensure your
-
Missing data for certain dates:
- Some macroeconomic indicators are published monthly or quarterly
- Weekend and holiday dates may not have market data
- Try extending your date range if you're getting limited results
-
PCA errors:
- If PCA analysis fails, the script will automatically fall back to standard analysis
- This is normal and won't affect the basic trend strength calculation
If you encounter persistent issues:
- Check that all dependencies are correctly installed
- Verify your API keys are valid and have sufficient quota
- Try running with a well-known symbol like SPY for testing