Terminal-based stock chart visualization library
Project description
Terminal Stonks 📈
A Python library for rendering beautiful stock candlestick charts directly in your terminal using ASCII art and colors.
Preview
Example of a candlestick chart rendered in the terminal
Features
- 🎨 Beautiful ASCII charts - Render K-line/candlestick charts in your terminal
- 🌈 Color-coded candles - Red for bullish, green for bearish movements
- 📏 Auto-scaling - Charts automatically scale to fit your terminal size
- 📅 Date labels - X-axis shows relevant dates for better context
- 💰 Price labels - Y-axis displays price levels
- 🔄 Real-time compatible - Perfect for live data visualization
Installation
Install from PyPI:
pip install terminal-stonks
Or install from source:
git clone https://github.com/yourusername/terminal-stonks.git
cd terminal-stonks
pip install -e .
Quick Start
import pandas as pd
from terminal_stonks import KChart
# Create sample data
data = pd.DataFrame({
'Open': [100, 102, 101, 103, 105],
'High': [105, 106, 104, 107, 108],
'Low': [99, 101, 100, 102, 104],
'Close': [102, 101, 103, 105, 107]
}, index=pd.date_range('2024-01-01', periods=5, freq='D'))
# Create and render chart
chart = KChart(data)
chart.render(title="My Stock Chart")
API Reference
KChart Class
__init__(data: pd.DataFrame)
Initialize a KChart with stock data.
Parameters:
data(pd.DataFrame): DataFrame with columns 'Open', 'High', 'Low', 'Close' and a DatetimeIndex
Raises:
TypeError: If the index is not a DatetimeIndex
render(title: str = "")
Render the chart to the terminal.
Parameters:
title(str, optional): Chart title to display at the top
Data Format
Your DataFrame should have the following structure:
import pandas as pd
data = pd.DataFrame({
'Open': [float], # Opening prices
'High': [float], # Highest prices
'Low': [float], # Lowest prices
'Close': [float] # Closing prices
}, index=pd.DatetimeIndex) # Must be a DatetimeIndex
Examples
Basic Usage
from terminal_stonks import KChart
import pandas as pd
# Sample OHLC data
data = pd.DataFrame({
'Open': [150.0, 152.0, 151.0, 153.0],
'High': [155.0, 156.0, 154.0, 157.0],
'Low': [149.0, 151.0, 150.0, 152.0],
'Close': [152.0, 151.0, 153.0, 155.0]
}, index=pd.date_range('2024-01-01', periods=4))
chart = KChart(data)
chart.render("AAPL Stock Chart")
With Yahoo Finance Data
import yfinance as yf
from terminal_stonks import KChart
# Fetch real data
ticker = yf.Ticker("AAPL")
data = ticker.history(period="1mo")
# Render chart
chart = KChart(data)
chart.render("Apple Inc. (AAPL) - Last 30 Days")
Example Output:
Chart Components
- Candlestick Body: Represented by
█characters- Red: Bullish (Close > Open)
- Green: Bearish (Close < Open)
- Shadows/Wicks: Represented by
│characters - Price Scale: Left side Y-axis with price levels
- Date Labels: Bottom X-axis with date information
Requirements
- Python 3.8+
- pandas >= 1.3.0
- rich >= 10.0.0
Development
To set up for development:
git clone https://github.com/yourusername/terminal-stonks.git
cd terminal-stonks
pip install -e ".[dev]"
Run tests:
pytest
Format code:
black terminal_stonks/
Contributing
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
Generating Example Screenshots
To update the example image (images/example.png):
# Run the screenshot-friendly script
python scripts/generate_example.py
# Take a screenshot when the chart appears
# Save as images/example.png
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgments
- Built with Rich for beautiful terminal output
- Inspired by the need for simple, terminal-based financial data visualization
Changelog
0.1.0 (Initial Release)
- Basic candlestick chart rendering
- Auto-scaling charts
- Color-coded bullish/bearish candles
- Date and price axis labels
Author
Sun Jiaxuan
Email: sunjiaxuan@hotmail.com
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file terminal_stonks-0.1.0.tar.gz.
File metadata
- Download URL: terminal_stonks-0.1.0.tar.gz
- Upload date:
- Size: 177.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a4073c794ac61740b3c83cad0d8a83f45e1f74f3f4d7653ebee99d136e724ebd
|
|
| MD5 |
9ecf1b5d349351ff359439302a216e19
|
|
| BLAKE2b-256 |
e95fdc1ac94f3a00b6e520b231560cf21595654bb54873ddafca4d2dafd787f1
|
Provenance
The following attestation bundles were made for terminal_stonks-0.1.0.tar.gz:
Publisher:
publish.yml on TianMingNanWei/terminal-stonks
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
terminal_stonks-0.1.0.tar.gz -
Subject digest:
a4073c794ac61740b3c83cad0d8a83f45e1f74f3f4d7653ebee99d136e724ebd - Sigstore transparency entry: 270647221
- Sigstore integration time:
-
Permalink:
TianMingNanWei/terminal-stonks@74c7b08bbf63b3099d0cd0af4a6b39d46023de64 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/TianMingNanWei
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@74c7b08bbf63b3099d0cd0af4a6b39d46023de64 -
Trigger Event:
release
-
Statement type:
File details
Details for the file terminal_stonks-0.1.0-py3-none-any.whl.
File metadata
- Download URL: terminal_stonks-0.1.0-py3-none-any.whl
- Upload date:
- Size: 7.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
43f769ae449f1709f45e2de95c91e06ae134132e57210adb80217ce03c2e3042
|
|
| MD5 |
35ec0d52e840d368ea2619ea4971637c
|
|
| BLAKE2b-256 |
a8f9dea23e3e66a8666fc8f1f34c78acba883bdc6c0462ddfa3c2f2691366ff2
|
Provenance
The following attestation bundles were made for terminal_stonks-0.1.0-py3-none-any.whl:
Publisher:
publish.yml on TianMingNanWei/terminal-stonks
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
terminal_stonks-0.1.0-py3-none-any.whl -
Subject digest:
43f769ae449f1709f45e2de95c91e06ae134132e57210adb80217ce03c2e3042 - Sigstore transparency entry: 270647232
- Sigstore integration time:
-
Permalink:
TianMingNanWei/terminal-stonks@74c7b08bbf63b3099d0cd0af4a6b39d46023de64 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/TianMingNanWei
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@74c7b08bbf63b3099d0cd0af4a6b39d46023de64 -
Trigger Event:
release
-
Statement type: