This project provides a modular, fully-configurable Python toolkit for analyzing stock OHLCV (Open, High, Low, Close, Volume) data, generating trading signals, visualizing them, and backtesting strategies. All parameters (ticker, timeframes, indicators, signal logic, and backtest settings) are controlled via a YAML config file or CLI overrides.
- Data Fetching: Downloads OHLCV data for any ticker and interval using yfinance, with optional resampling.
- Technical Indicators: Computes SMA (Simple Moving Average), RSI (Relative Strength Index), and ATR (Average True Range) with configurable periods.
- Signal Generation: Detects trend changes and generates BUY/SELL signals based on indicator crossovers and candle logic, with configurable lookback.
- Visualization: Plots price, indicators, and signals using plotext (terminal plotting), with dynamic column names.
- Backtesting: Simulates trades based on signals and reports win/loss statistics, with configurable ATR-based stop loss and take profit.
- Multiple Stop/Target Strategies: Choose from ATR, hybrid, or swing-high/low stop/target logic, or run all for comparison.
All settings are controlled via config.yaml at the project root. Example:
tickers:
- "BPCL.NS"
period: "180d" # Data fetch period (e.g., 180d, 1y)
interval: "1h" # Data fetch interval (e.g., 1h, 4h, 1d)
resample_to: "4h" # Resample to this interval (optional)
indicators:
sma_fast: 20 # Fast SMA period
sma_slow: 50 # Slow SMA period
rsi_period: 14 # RSI period
atr_period: 14 # ATR period
signals:
min_bars_since_crossover: 2 # Min bars since SMA crossover for trend confirmation
backtest:
stop_loss_atr: 1.5 # Stop loss ATR multiplier
take_profit_atr: 2.0 # Take profit ATR multiplier
initial_capital: 100000 # Starting capital
trailing_atr_mode: fixed # fixed or dynamic (ATR at entry or at each bar)
ratcheting_stop: true # true = trailing/ratcheting stop, false = static stop
capital_fraction_per_trade: 0.5
stop_target_strategy: atr # Options: atr, hybrid, swing, all
plot: false # Whether to plot the graph
debug: false # Set to true to print extra data info
debug_trade: trueatr: ATR-based stop/target (with/without trailing)hybrid: No trailing until target/stop is reached, then target becomes stop and trailing beginsswing: Use most recent swing high/low as stopall: Run all strategies and print summary for each
You can override any config value via CLI arguments (see below).
-
Install dependencies:
python -m venv .venv source .venv/bin/activate pip install pandas yfinance numpy plotext pyyaml -
Run the analysis:
.venv/bin/python main.py --mode backtest
-
Override config via CLI:
.venv/bin/python main.py --ticker TCS.NS --period 60d --interval 1h --resample_to 4h --sma_fast 10 --sma_slow 30 --rsi_period 7 --atr_period 7 --lookback 2 --stop_loss_atr 1.0 --take_profit_atr 1.5 --stop-target-strategy hybrid
To run all strategies:
.venv/bin/python main.py --stop-target-strategy all
src/
data_fetcher.py # Data fetching and resampling
indicators.py # Indicator calculations (SMA, RSI, ATR)
signals.py # Trend and signal logic
visualize.py # Terminal plotting (plotext)
backtest.py # Backtesting logic
main.py # Pipeline entry point, config loading, CLI
config.yaml # Configuration file
README.md # This file
- src/data_fetcher.py: Fetches and resamples OHLCV data. All parameters are passed in from config/main.
- src/indicators.py: Computes SMA, RSI, and ATR. All periods are passed in from config/main.
- src/signals.py: Detects trend and generates signals. All logic parameters (e.g., lookback) are passed in from config/main.
- src/visualize.py: Plots price, SMAs, and signals. SMA column names are passed in from config/main.
- src/backtest.py: Simulates trades using ATR-based, hybrid, or swing-high/low stop/target logic. All multipliers and strategy are passed in from config/main.
- main.py: Loads config, parses CLI, orchestrates the pipeline.
- The code is modular—each file can be extended or modified independently.
- Visualization uses plotext for terminal-based plotting; for more advanced plots, consider matplotlib.
- Backtesting is basic and for educational purposes; for production, use a dedicated backtesting library.
Feel free to extend the config, add new indicators, or plug in new signal logic as needed!