Market Data Service is a high-performance financial data API that provides comprehensive Symbol prices of different markets through both RESTful endpoints and real-time WebSocket connections.
|
|
2 ay önce | |
|---|---|---|
| MT5 | 2 ay önce | |
| config | 3 ay önce | |
| docker | 2 ay önce | |
| docs | 2 ay önce | |
| models | 4 ay önce | |
| nginx | 2 ay önce | |
| scripts | 3 ay önce | |
| src | 2 ay önce | |
| tests | 3 ay önce | |
| .env.example | 2 ay önce | |
| .gitignore | 2 ay önce | |
| DOCKER.md | 2 ay önce | |
| Dockerfile | 3 ay önce | |
| README.md | 2 ay önce | |
| docker-compose.prod.yml | 2 ay önce | |
| docker-compose.yml | 3 ay önce | |
| package-lock.json | 3 ay önce | |
| package.json | 3 ay önce | |
| schema.sql | 3 ay önce |
A high-performance financial data API that provides comprehensive market data for various financial instruments including cryptocurrencies, stocks, forex, and commodities through both RESTful endpoints and real-time WebSocket connections.
The service includes an MT5 Expert Advisor (EA) that automatically sends historical candle data and live tick prices to the API.
MT5/Experts/MarketDataSender.mq5 to your MT5 Experts directoryConfigure EA settings:
ApiBaseUrl: Your API endpoint (e.g., http://your-server:3000)ApiKey: Optional authentication keyHistoricalCandleCount: Number of historical candles to send (default: 1000)HistoricalTimeframe: Timeframe for historical data (default: H1)Attach the EA to any MT5 chart
The EA will:
EXCHANGE_SYMBOL)✅ MT5 Integration
✅ Data Integrity
✅ Reliability
Comprehensive test coverage
Multi-Asset Support: Handles cryptocurrencies, stocks, forex, commodities, and indices
Real-time Data: Live price feeds with bid/ask spreads
Historical Data: OHLCV candle data with flexible timeframes
RESTful API: Well-structured endpoints for all operations
Data Validation: Comprehensive input validation using Joi
Error Handling: Robust error handling with detailed responses
Security: Helmet.js for security headers, CORS support
Logging: Winston-based logging with multiple transports
Database: PostgreSQL with Sequelize ORM
Scalable Architecture: Modular design with controllers, routes, and middleware
market-data-service/
├── src/
│ ├── config/
│ │ └── database.js # Database configuration
│ ├── controllers/
│ │ ├── symbolController.js # Symbol CRUD operations
│ │ ├── candleController.js # Candle data operations
│ │ └── livePriceController.js # Live price operations
│ ├── middleware/
│ │ ├── errorHandler.js # Global error handling
│ │ └── validation.js # Request validation
│ ├── models/
│ │ ├── Symbol.js # Symbol model
│ │ ├── Candle.js # Multi-timeframe candle model
│ │ ├── LivePrice.js # Live price model
│ │ └── index.js # Model associations
│ ├── routes/
│ │ ├── symbols.js # Symbol routes
│ │ ├── candles.js # Candle routes
│ │ └── livePrices.js # Live price routes
│ ├── utils/
│ │ └── logger.js # Logging utility
│ ├── app.js # Express app configuration
│ └── server.js # Server startup
├── tests/ # Test files
├── schema.sql # Database schema
├── .env # Environment variables
├── .gitignore # Git ignore rules
├── package.json # Dependencies and scripts
└── README.md # This file
Database Constraints
ALTER TABLE candles_1h
ADD CONSTRAINT unique_symbol_open_time
UNIQUE (symbol_id, open_time);
Precision Requirements
// All numeric fields require 15 decimal precision
Joi.number().precision(15)
# Clone repository
git clone <your-repo-url>
cd market-data-service
# Start Docker services
docker-compose up -d
# Access API at http://market-data.local
📖 For detailed local development setup, see docs/LOCAL_DEV_SETUP.md
# Set up environment variables
cp .env.example .env
# Edit .env with your values
# Start production services
docker-compose -f docker-compose.prod.yml up -d
📖 For complete production deployment guide, see docs/DEPLOYMENT.md
# Update system packages
sudo apt update && sudo apt upgrade -y
# Install required tools
sudo apt install -y curl wget git htop nano ufw
# Using NodeSource repository
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt-get install -y nodejs
# Verify installation
node --version # Should show v18.x.x
npm --version # Should show latest version
# Install PostgreSQL
sudo apt install -y postgresql postgresql-contrib
# Start and enable PostgreSQL
sudo systemctl enable postgresql
sudo systemctl start postgresql
# Verify installation
psql --version # Should show 15.x or higher
# Create application directory
sudo mkdir -p /root/market-data-service
sudo chown $USER:$USER /root/market-data-service
cd /root/market-data-service
# Clone repository (replace with your actual repo URL)
git clone https://git.mqldevelopment.com/muhammad.uzair/market-data-service.git .
# OR copy your project files to this directory
# Install dependencies
npm install --production
# Set up environment variables
cat > .env << EOF
DB_TYPE=postgres
DB_HOST=localhost
DB_PORT=5432
DB_NAME=financial_data
DB_USER=postgres
DB_PASSWORD=your_secure_password_here
PORT=3001
NODE_ENV=production
JWT_SECRET=your_secure_jwt_secret_key_here
CORS_ORIGIN=*
LOG_LEVEL=info
EOF
# Switch to postgres user
sudo -u postgres psql
# In PostgreSQL shell, run:
CREATE DATABASE financial_data;
ALTER USER postgres PASSWORD 'your_secure_password_here';
GRANT ALL PRIVILEGES ON DATABASE financial_data TO postgres;
\q
# Exit and run migrations
npx sequelize-cli db:migrate
# Verify tables were created
sudo -u postgres psql -d financial_data -c "\dt"
⚠️ Outdated Instructions: This section contains legacy deployment steps. For current production deployment with containerized SSL certificates, see docs/DEPLOYMENT.md
# Navigate to project directory
cd /root/market-data-service
# Copy environment template
cp .env.example .env
# Edit .env file with production values
nano .env
# Build and start production containers
docker-compose -f docker-compose.prod.yml up -d --build
📖 For detailed Docker deployment instructions, see DOCKER.md
📖 For production deployment with SSL, see docs/DEPLOYMENT.md
Quick Overview: For complete setup instructions, troubleshooting, and MT5 EA configuration, see docs/LOCAL_DEV_SETUP.md.
# Clone from GitHub
git clone https://git.mqldevelopment.com/muhammad.uzair/market-data-service.git
cd market-data-service
# Run automated setup script (fully automated on macOS - will prompt for password)
# On Linux, you may need to run with: sudo bash scripts/setup-local-dev.sh
bash scripts/setup-local-dev.sh
Note: On macOS, the script automatically prompts for your administrator password. On Linux, you may need to run with sudo.
Alternative (Manual):
# Add market-data.local to hosts file manually
echo "127.0.0.1 market-data.local" | sudo tee -a /etc/hosts
# Start all services (database, API, Nginx)
docker-compose up -d
# Verify services are running
docker-compose ps
# Test health endpoint
curl http://market-data.local/health
# Test API endpoints
curl http://market-data.local/api/symbols
curl http://market-data.local/api/health
✅ Setup Complete! Your API is now accessible at http://market-data.local
MT5 Expert Advisors require valid domain URLs and do not allow localhost or 127.0.0.1. The market-data.local domain resolves to your local Docker setup, enabling MT5 EA connectivity.
Add URL to MT5 Allowed List:
http://market-data.localConfigure EA:
MarketDataSender.mq5 to any chartApiBaseUrl = "http://market-data.local"Verify Connection:
✅ Symbols initializedView Logs:
# All services
docker-compose logs -f
# Specific service
docker-compose logs -f api
docker-compose logs -f nginx
Code Changes:
src/ is automatically reloaded (no container restart needed)docker-compose logs -f apiStop Services:
docker-compose down
Restart Services:
docker-compose restart
📖 For comprehensive setup instructions, troubleshooting, and advanced configuration, see:
If you prefer to run without Docker (not recommended for MT5 EA):
Click to expand non-Docker setup instructions
Install Dependencies:
npm install
Set Up Database: ```bash createdb financial_data
cat > .env << EOF DB_TYPE=postgres DB_HOST=localhost DB_PORT=5432 DB_NAME=financial_data DB_USER=your_local_username DB_PASSWORD=your_local_password PORT=3001 NODE_ENV=development JWT_SECRET=your_development_jwt_secret_key CORS_ORIGIN=http://localhost:3000 LOG_LEVEL=debug EOF
3. **Run Migrations:**
```bash
npx sequelize-cli db:migrate
Start Server:
npm run dev
Test:
curl http://localhost:3001/health
Note: This setup won't work with MT5 EA (requires domain URL). Use Docker setup for MT5 integration.
# Navigate to project directory
cd /root/market-data-service
# Backup current deployment (optional but recommended)
cp .env .env.backup
# Pull latest changes
git pull origin master
# Rebuild and restart containers (migrations run automatically)
docker-compose -f docker-compose.prod.yml up -d --build
# Verify everything is working
docker-compose -f docker-compose.prod.yml ps
curl https://your-domain.com/health
# Navigate to project directory
cd /root/market-data-service
# Create backup
sudo cp -r /root/market-data-service /root/market-data-service-backup-$(date +%Y%m%d_%H%M%S)
# Stop containers
docker-compose -f docker-compose.prod.yml down
# Update code
git fetch origin
git reset --hard origin/master
# Update environment if needed
nano .env
# Rebuild and start containers (migrations run automatically)
docker-compose -f docker-compose.prod.yml up -d --build
# Verify deployment
docker-compose -f docker-compose.prod.yml ps
curl https://your-domain.com/health
Error: ERROR: role "postgres" already exists
Solution:
# Just set password for existing user
sudo -u postgres psql
ALTER USER postgres PASSWORD 'your_password';
GRANT ALL PRIVILEGES ON DATABASE financial_data TO postgres;
\q
Error: cannot load certificate "/etc/letsencrypt/live/.../fullchain.pem"
Solution:
# Generate certificate first
sudo certbot --nginx -d your-domain.com
# Check if certificates exist
ls -la /etc/letsencrypt/live/your-domain.com/
# Test nginx configuration
sudo nginx -t
Problem: Container won't start or keeps restarting
Solution:
# Check container logs
docker-compose -f docker-compose.prod.yml logs api
# Check container status
docker-compose -f docker-compose.prod.yml ps
# Restart specific service
docker-compose -f docker-compose.prod.yml restart api
# Check service status
docker-compose -f docker-compose.prod.yml ps
sudo systemctl status nginx
# View logs
docker-compose -f docker-compose.prod.yml logs -f api
docker-compose -f docker-compose.prod.yml logs -f db
sudo tail -f /var/log/nginx/access.log
sudo tail -f /var/log/nginx/error.log
Note: SSL certificates are now automatically managed via Docker containers. See docs/DEPLOYMENT.md for details.
# Check container status
docker-compose -f docker-compose.prod.yml ps
# View logs
docker-compose -f docker-compose.prod.yml logs -f api
docker-compose -f docker-compose.prod.yml logs -f db
# Restart services
docker-compose -f docker-compose.prod.yml restart
# Stop all services
docker-compose -f docker-compose.prod.yml down
# Start services
docker-compose -f docker-compose.prod.yml up -d
# Rebuild and restart
docker-compose -f docker-compose.prod.yml up -d --build
sudo systemctl status nginx # Check status
sudo systemctl restart nginx # Restart nginx
sudo nginx -t # Test configuration
sudo tail -f /var/log/nginx/error.log # View error logs
# Connect to database
sudo -u postgres psql -d financial_data
# View tables
\dt
# View table structure
\d table_name
# Backup database
pg_dump financial_data > backup.sql
# Restore database
psql financial_data < backup.sql
See docs/CONFIGURATION.md for complete environment variable documentation.
Quick Reference:
DB_PASSWORD - Database password (required)JWT_SECRET - JWT secret key (required)DOMAIN_NAME - Domain for SSL certificates (production)SSL_EMAIL - Email for Let's Encrypt (production)CORS_ORIGIN - Allowed CORS originsSee docs/API_CONTRACT.md for complete API documentation.
Quick Reference:
GET /health - Health checkGET /api/symbols - Get all symbolsGET /api/candles - Get candle dataPOST /api/candles/bulk - Bulk create candlesGET /api/live-prices - Get live pricesPOST /api/live-prices/bulk - Bulk update live pricesnpm start - Start production servernpm run dev - Start development server with auto-reloadnpm test - Run Jest test suitenpm run test:watch - Run tests in watch modenpm run migrate - Run database migrationsnpm run migrate:undo - Revert last migrationnpm run lint - Run ESLintnpm run lint:fix - Fix ESLint issuesWe use Sequelize CLI for database migrations:
# Create new migration
npx sequelize-cli migration:generate --name your-migration-name
# Run pending migrations
npx sequelize-cli db:migrate
# Revert last migration
npx sequelize-cli db:migrate:undo
The project is fully containerized using Docker with automatic SSL certificate management.