Skip to main content

Terminal-based stock chart visualization library

Project description

Terminal Stonks 📈

PyPI version Python Versions License: MIT

A Python library for rendering beautiful stock candlestick charts directly in your terminal using ASCII art and colors.

Preview

Terminal Stonks Example 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: Terminal Chart Example

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

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

terminal_stonks-0.1.0.tar.gz (177.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

terminal_stonks-0.1.0-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

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

Hashes for terminal_stonks-0.1.0.tar.gz
Algorithm Hash digest
SHA256 a4073c794ac61740b3c83cad0d8a83f45e1f74f3f4d7653ebee99d136e724ebd
MD5 9ecf1b5d349351ff359439302a216e19
BLAKE2b-256 e95fdc1ac94f3a00b6e520b231560cf21595654bb54873ddafca4d2dafd787f1

See more details on using hashes here.

Provenance

The following attestation bundles were made for terminal_stonks-0.1.0.tar.gz:

Publisher: publish.yml on TianMingNanWei/terminal-stonks

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file terminal_stonks-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for terminal_stonks-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 43f769ae449f1709f45e2de95c91e06ae134132e57210adb80217ce03c2e3042
MD5 35ec0d52e840d368ea2619ea4971637c
BLAKE2b-256 a8f9dea23e3e66a8666fc8f1f34c78acba883bdc6c0462ddfa3c2f2691366ff2

See more details on using hashes here.

Provenance

The following attestation bundles were made for terminal_stonks-0.1.0-py3-none-any.whl:

Publisher: publish.yml on TianMingNanWei/terminal-stonks

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page