stock_data_fetcher.py is a Python script that performs several key financial analysis and simulation tasks. It fetches historical stock data for a specified ticker from Yahoo Finance, analyzes price trends using a moving average crossover strategy to generate buy/sell signals, simulates trading based on these signals with configurable parameters (initial cash, commission), and reports the overall performance of the strategy.
- Data Fetching: Retrieves historical stock data (Open, High, Low, Close, Volume) from Yahoo Finance.
- Trend Analysis: Implements a configurable moving average (MA) crossover strategy to generate buy and sell signals.
- Trading Simulation: Simulates buy/sell transactions based on the generated signals, accounting for initial cash and commission per trade.
- Performance Reporting: Calculates and displays key performance metrics, including initial and final portfolio value, total return percentage, and a list of all transactions made.
- Command-Line Interface: Highly configurable via command-line arguments, allowing users to specify:
- Stock ticker symbol
- Start and end dates for historical data
- Initial cash for simulation
- Commission per trade
- Short and long Simple Moving Average (SMA) window lengths
The script requires the following Python libraries:
yfinance: To fetch stock data from Yahoo Finance.pandas: For data manipulation and analysis, especially for handling time series data and DataFrames.numpy: For numerical operations, used here for handling NaN values and numeric type checks.
You can install these dependencies using pip:
pip install yfinance pandas numpyExecute the script from your command line using Python. You need to provide the ticker symbol, start date, and end date. Other parameters like initial cash, commission, and SMA windows have default values but can be overridden.
Example:
python stock_data_fetcher.py --ticker AAPL --start_date 2022-01-01 --end_date 2023-01-01 --initial_cash 10000 --short_window 20 --long_window 50 --commission 1.0Key Command-Line Arguments:
--ticker(-t): The stock ticker symbol (e.g.,AAPL,MSFT). (Required)--start_date(-s): The start date for fetching historical data, inYYYY-MM-DDformat. (Required)--end_date(-e): The end date for fetching historical data, inYYYY-MM-DDformat. (Required)--initial_cash(-c): The amount of cash to start the simulation with. (Default:10000.0)--commission(-k): The commission fee to apply per trade (both buy and sell). (Default:0.0)--short_window(-sw): The window length (in days) for the short-term Simple Moving Average. (Default:50)--long_window(-lw): The window length (in days) for the long-term Simple Moving Average. (Default:200)
For a full list of arguments and their descriptions, you can run:
python stock_data_fetcher.py --helpThe trading strategy implemented is the Moving Average Crossover:
- Simple Moving Averages (SMAs): Two SMAs are calculated based on the stock's closing prices: a short-term SMA and a long-term SMA.
- Buy Signal: A "buy" signal is generated when the short-term SMA crosses above the long-term SMA. This event is often interpreted as an indication of upward momentum.
- Sell Signal: A "sell" signal is generated when the short-term SMA crosses below the long-term SMA. This event is often interpreted as an indication of downward momentum.
The simulation buys on a buy signal (if not already holding shares) and sells on a sell signal (if shares are held).
Disclaimer: This script is for educational and simulation purposes only. It is NOT financial advice. Trading stocks involves substantial risk of loss. Past performance is not indicative of future results. Use this script at your own risk.