π Free Self-Hosted Real-Time Stock Data Platform
A robust HTTP API service that provides real-time market data and historical candle data using the TradingView WebSocket Client library. MarketStream gives you unlimited access to TradingView's data through your own self-hosted platform.
- REST API for real-time market data access
- Real-time quote data with comprehensive market information
- Historical candle data with multiple timeframes
- Dynamic symbol management (add/remove subscriptions during runtime)
- PostgreSQL integration for persistent data storage
- High-performance caching with Ristretto for real-time quotes
- Automatic database migrations with Ent ORM
- Clean interfaces for better testability and dependency injection
- Message router pattern for extensible WebSocket message processing
- Structured error handling with context and retry logic
- Comprehensive input validation for security and data integrity
- Advanced configuration system with YAML, environment variables, and CLI flags
- Development tooling with Makefile automation
- Comprehensive technical analysis supporting 10+ indicators
- Study session management for grouping related indicators
- Real-time calculation with candle data integration
- REST API endpoints for indicator CRUD operations
go get github.com/iiiyu/marketstream# Clone the repository
git clone https://github.com/iiiyu/marketstream
cd marketstream
# Install development tools and dependencies
make setup
# Copy example configuration
cp config.example.yaml config.yamlEdit config.yaml with your settings:
server:
port: "3333"
environment: "development"
database:
host: "localhost"
port: 5432
user: "postgres"
password: "your_password"
db_name: "tradingview"
ssl_mode: "disable"
tradingview:
device_token: "your_device_token_here"
session_id: "your_session_id_here"
session_sign: "your_session_signature_here"
logging:
level: "info"
format: "json"Or use environment variables:
# Database Configuration
DB_HOST=localhost
DB_PORT=5432
DB_USER=postgres
DB_PASSWORD=your_password
DB_NAME=tradingview
DB_SSLMODE=disable
# TradingView Configuration (Required)
TRADINGVIEW_DEVICE_TOKEN=your_device_token
TRADINGVIEW_SESSION_ID=your_session_id
TRADINGVIEW_SESSION_SIGN=your_session_signature
# API Authentication (Required for production)
API_KEY=your_secure_api_key_here
AUTH_ENABLED=true
# Optional Server Configuration
PORT=3333
LOG_LEVEL=info# Development mode with live reload
make dev
# Or run directly
make run
# Or build and run
make build
./bin/tradingview-http-serviceMarketStream uses API key authentication to secure all endpoints except /health.
Set your API key in one of the following ways:
Environment Variables:
API_KEY=your_secure_api_key_here
AUTH_ENABLED=trueConfiguration File (config.yaml):
auth:
enabled: true
api_key: "your_secure_api_key_here"Command Line:
./marketstream --auth.enabled=true --auth.api_key=your_keyInclude your API key in requests using one of these methods:
Option 1: X-API-Key Header
curl -H "X-API-Key: your_api_key_here" http://localhost:3333/symbolsOption 2: Authorization Bearer Token
curl -H "Authorization: Bearer your_api_key_here" http://localhost:3333/symbols200- Success401- Unauthorized (missing or invalid API key)400- Bad Request500- Internal Server Error
GET /health# Add a symbol subscription
POST /symbols
{
"exchange": "NASDAQ",
"symbol": "AAPL",
"type": "quote" # or "candle"
"timeframe": "1D" # required for candle type
}
# Remove a symbol subscription
DELETE /symbols
{
"exchange": "NASDAQ",
"symbol": "AAPL",
"type": "quote"
}
# List active subscriptions
GET /symbols
# Get subscriptions by exchange/symbol
GET /symbols/NASDAQ/AAPL# Get real-time quote data
GET /quotes/NASDAQ/AAPL
# Get historical candlestick data
GET /candles/NASDAQ/AAPL/1D/100 # exchange/symbol/timeframe/limit
# Force reconnection
GET /reconnect# Create a study session
POST /study-sessions
{
"name": "My Analysis",
"symbol": "NASDAQ:AAPL",
"interval": "1D",
"max_bars": 1000
}
# Add indicators to a session
POST /indicators
{
"study_session_id": "session_uuid",
"type": "RSI",
"parameters": {"period": 14}
}
# Get indicator data
GET /indicators/{indicator_id}/data- HTTP API β Manages symbol subscriptions and data requests
- TradingView WebSocket Client β Connects to TradingView real-time data
- Message Processing β Routes and processes incoming market data
- Data Storage β Quote data cached in Ristretto, candle data persisted to PostgreSQL
- API Response β Serves cached quotes and historical candles via REST API
The service uses PostgreSQL with Ent ORM and includes these entities:
- Symbol: Comprehensive symbol metadata with exchange, type, sector information
- Candle: OHLCV data with quality indicators and market microstructure fields
- ActiveSession: Real-time subscription tracking with status and error handling
- StudySession: Technical analysis sessions for indicator calculations
- Indicator: Individual technical indicators with parameters
- IndicatorData: Time-series calculated indicator values
- Command line flags:
--server.port=3333 - Environment variables:
PORT=3333 - Configuration file:
config.yaml - Default values
server:
port: "3333"
host: "0.0.0.0"
environment: "development" # development, staging, production
read_timeout: "30s"
write_timeout: "30s"
database:
host: "localhost"
port: 5432
user: "postgres"
password: "secret"
db_name: "tradingview"
ssl_mode: "disable"
max_open_conns: 25
max_idle_conns: 5
tradingview:
device_token: "required"
session_id: "required"
session_sign: "required"
base_url: "https://www.tradingview.com"
websocket:
url: "wss://prodata.tradingview.com/socket.io/websocket?from=screener%2F"
max_retries: 5
ping_interval: "30s"
read_timeout: "60s"
cache:
max_cost: 1073741824 # 1GB
num_counters: 10000000
default_ttl: "1h"
logging:
level: "info" # debug, info, warn, error
format: "json" # json, textmake help # Show all available commands
make dev # Start development server with live reload
make build # Build the application
make test # Run tests
make test-cover # Run tests with coverage
make lint # Run linter
make fmt # Format code
make clean # Clean build artifacts
make docker-build # Build Docker imageβββ cmd/
β βββ app/ # Main HTTP service application
β βββ example/ # Example usage
β βββ test-config/ # Configuration testing tool
βββ internal/
β βββ config/ # Configuration management
β βββ dto/ # Data Transfer Objects
β βββ handler/ # HTTP API handlers
β βββ service/ # Business logic services
βββ ent/ # Database schema and ORM (Ent)
βββ docs/ # Documentation
βββ deploy/ # Deployment configurations
The service includes a complete Docker Compose setup with PostgreSQL database:
# Navigate to docker directory
cd deploy/docker
# Copy environment template
cp .env.example .env
# Edit .env with your credentials
nano .env
# Start services (includes PostgreSQL + TradingView service)
docker-compose up -d
# View logs
docker-compose logs -f
# Stop services
docker-compose down# Build Docker image
make docker-build
# Run with external database
docker run -p 3333:3333 \
-e DB_HOST=your_db_host \
-e DB_USER=postgres \
-e DB_PASSWORD=your_password \
-e DB_NAME=marketstream \
-e TRADINGVIEW_DEVICE_TOKEN=your_token \
-e TRADINGVIEW_SESSION_ID=your_session_id \
-e TRADINGVIEW_SESSION_SIGN=your_signature \
-e API_KEY=your_secure_api_key \
-e AUTH_ENABLED=true \
marketstream:latestCoolify is a self-hosted alternative to Heroku/Netlify. Deploy with these steps:
git clone https://github.com/your-username/marketstream
cd marketstream- Login to your Coolify dashboard
- Create new project: "MarketStream"
- Choose "Docker Compose" as deployment type
- Set repository URL to your fork
- Set docker-compose file path:
deploy/docker/docker-compose.yml
Add these secrets in Coolify dashboard:
TRADINGVIEW_DEVICE_TOKEN=your_device_token_here
TRADINGVIEW_SESSION_ID=your_session_id_here
TRADINGVIEW_SESSION_SIGN=your_session_signature_here
DB_PASSWORD=your_secure_postgres_password
API_KEY=your_secure_api_key_here- Click "Deploy" in Coolify dashboard
- Monitor deployment logs
- Service will be available at your configured domain
- Health check endpoint:
https://your-domain.com/health
- PostgreSQL data persisted in Docker volume
- Automatic health checks configured
- Auto-restart on failure enabled
- Logs available in Coolify dashboard
# Run all tests
make test
# Run tests with coverage
make test-cover
# Run benchmarks
make benchmark
# Test configuration
go run cmd/test-config/main.gocurl -X POST http://localhost:3333/symbols \
-H "Content-Type: application/json" \
-H "X-API-Key: your_api_key_here" \
-d '{"exchange":"NASDAQ","symbol":"AAPL","type":"quote"}'curl -H "X-API-Key: your_api_key_here" http://localhost:3333/quotes/NASDAQ/AAPLcurl -X POST http://localhost:3333/symbols \
-H "Content-Type: application/json" \
-H "X-API-Key: your_api_key_here" \
-d '{"exchange":"BINANCE","symbol":"BTCUSDT","type":"candle","timeframe":"1D"}'curl -H "X-API-Key: your_api_key_here" http://localhost:3333/candles/BINANCE/BTCUSDT/1D/50- Go 1.24+
- PostgreSQL 12+
- TradingView credentials (device token, session ID, session signature)
- TradingView WebSocket Client - Core WebSocket functionality
- Ent - Database ORM
- Fiber - HTTP web framework
- Ristretto - High-performance cache
- Viper - Configuration management
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Make your changes
- Run tests (
make test) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
- Follow Go best practices and idioms
- Add tests for new functionality
- Update documentation for API changes
- Use the provided Makefile commands
- Ensure all checks pass (
make check)
This project is licensed under the MIT License - see the LICENSE file for details.
- TradingView WebSocket Client - Go library for TradingView WebSocket connections
If you encounter any issues:
- Check the configuration examples
- Run configuration test:
go run cmd/test-config/main.go - Enable debug logging (
LOG_LEVEL=debug) - Check the TradingView WebSocket Client documentation
- Open an issue with detailed information