# Copy Trading System - Premium Dark-Themed Module

A comprehensive copy trading system module for forex/crypto broker websites, featuring a premium dark-themed UI inspired by Bybit, Binance, and Bitget platforms.

## Features

### User Features
- **Copy Trader Marketplace**: Browse and discover top-performing traders with advanced filtering and sorting
- **Trader Profile Pages**: Detailed trader profiles with ROI charts, analytics, trading history, and performance metrics
- **Copy Trading Setup**: Configure investment amount, leverage, risk level, stop loss, take profit, and copy mode
- **My Copies Dashboard**: Manage active copy trades, view profit/loss, and stop copying traders
- **Interactive Charts**: Real-time ROI and PnL charts using Chart.js

### Admin Features
- **Admin Dashboard**: Overview of platform statistics, recent traders, and copy trades
- **Trader Management**: Add, edit, approve, suspend, and delete traders
- **Trading Signals**: Manage trading signals from traders
- **Copy Trade Management**: Monitor and manage all copy trading activities
- **Platform Settings**: Configure design, security, and trading settings
- **Analytics**: Comprehensive analytics with charts and statistics

### Security Features
- **Email Verification**: Secure email verification for registration using PHPMailer SMTP
- **Login OTP**: Two-factor authentication with OTP for login security
- **CSRF Protection**: Cross-site request forgery protection
- **XSS Protection**: Cross-site scripting prevention
- **Rate Limiting**: Protection against brute force attacks
- **Prepared Statements**: SQL injection prevention
- **Secure File Upload**: File upload validation

## Installation

### 1. Database Setup

Run the SQL schema file to create the necessary tables:

```bash
mysql -u username -p database_name < database/copy_trading_schema.sql
```

This will create the following tables:
- `copy_traders` - Trader profiles
- `copied_trades` - Active copy trades
- `trader_followers` - Trader followers
- `trader_statistics` - Trader performance statistics
- `login_verification` - Login OTP records
- `email_verification` - Email verification tokens
- `notifications` - User notifications
- `platform_settings` - Platform configuration
- `trading_signals` - Trading signals
- `trader_analytics` - Daily trader analytics
- `rate_limits` - Rate limiting records
- `security_logs` - Security event logs

### 2. Configuration

Update your `config.php` file with the following SMTP settings:

```php
define('SMTP_HOST', 'mail.yourdomain.com');
define('SMTP_PORT', 587);
define('SMTP_USER', 'noreply@yourdomain.com');
define('SMTP_PASS', 'your-email-password');
define('SMTP_FROM', 'noreply@yourdomain.com');
define('SMTP_FROM_NAME', 'Your Platform Name');
```

### 3. Install PHPMailer

Install PHPMailer using Composer:

```bash
composer require phpmailer/phpmailer
```

Or download manually from: https://github.com/PHPMailer/PHPMailer

### 4. File Structure

The system uses the following directory structure:

```
broker/
├── admin/
│   └── copy-trading/
│       ├── dashboard.php
│       ├── traders.php
│       ├── add_trader.php
│       ├── edit_trader.php
│       ├── signals.php
│       ├── copies.php
│       ├── settings.php
│       └── analytics.php
├── copy-trading/
│   ├── marketplace.php
│   ├── trader-profile.php
│   ├── my-copies.php
│   ├── verify_email.php
│   ├── verify_otp.php
│   ├── includes/
│   │   ├── security.php
│   │   ├── email_verification.php
│   │   └── login_verification.php
│   └── ajax/
│       ├── copy_trader.php
│       ├── follow_trader.php
│       └── stop_copy.php
├── assets/
│   ├── css/
│   │   └── copy-trading/
│   │       └── style.css
│   └── js/
│       └── copy-trading/
│           ├── marketplace.js
│           ├── trader-profile.js
│           └── my-copies.js
├── mail/
│   └── templates/
│       ├── registration_verification.html
│       ├── otp_verification.html
│       ├── password_reset.html
│       ├── email_change.html
│       └── notification.html
└── database/
    └── copy_trading_schema.sql
```

## Usage

### For Users

1. **Access Marketplace**: Navigate to `/copy-trading/marketplace.php`
2. **Browse Traders**: Filter and sort traders by ROI, followers, risk level
3. **View Profiles**: Click on a trader to view detailed analytics
4. **Start Copying**: Click "Copy Now" and configure your investment settings
5. **Manage Copies**: View and manage your active copies at `/copy-trading/my-copies.php`

### For Admins

1. **Access Dashboard**: Navigate to `/admin/copy-trading/dashboard.php`
2. **Manage Traders**: Add, edit, approve, or suspend traders
3. **Monitor Signals**: View and manage trading signals
4. **Track Copies**: Monitor all copy trading activities
5. **Configure Settings**: Adjust platform settings
6. **View Analytics**: Analyze platform performance

## Integration with Existing System

This module extends the existing broker website by:

1. **Using Existing Authentication**: Leverages the existing `register` table for user accounts
2. **Extending Database**: Adds new tables without modifying existing ones
3. **Session Management**: Uses existing session system
4. **Configuration**: Integrates with existing `config.php`

## Security Implementation

### CSRF Protection
```php
require_once 'copy-trading/includes/security.php';
$token = generateCSRFToken();
// Include token in forms
<input type="hidden" name="csrf_token" value="<?php echo $token; ?>">
```

### Input Sanitization
```php
$input = sanitizeInput($_POST['field']);
```

### Rate Limiting
```php
if (!checkRateLimit($identifier, 5, 300)) {
    // Too many attempts
}
```

### Email Verification
```php
require_once 'copy-trading/includes/email_verification.php';
sendVerificationEmail($user_id, $email, 'Registration');
```

### Login OTP
```php
require_once 'copy-trading/includes/login_verification.php';
sendLoginOTP($user_id, $email);
```

## Customization

### Theme Colors

Modify CSS variables in `assets/css/copy-trading/style.css`:

```css
:root {
    --primary-color: #f97316;
    --secondary-color: #eab308;
    --bg-dark: #0f101e;
    --bg-darker: #0a0b14;
}
```

### Platform Settings

Access `/admin/copy-trading/settings.php` to configure:
- Primary and secondary colors
- Dark theme and glassmorphism
- OTP expiry and attempts
- Email and login verification requirements
- Minimum investment and maximum leverage

## Dependencies

- PHP 7.4 or higher
- MySQL 5.7 or higher
- PHPMailer 6.0 or higher
- Chart.js 4.0 or higher
- Font Awesome 6.0 or higher

## Browser Support

- Chrome (latest)
- Firefox (latest)
- Safari (latest)
- Edge (latest)

## Mobile Responsiveness

The system is fully responsive and optimized for mobile devices with:
- Mobile-first design approach
- Touch-friendly interface
- Responsive charts
- Optimized performance

## Support

For issues or questions, please refer to the existing broker website documentation or contact the development team.

## License

This module is part of the broker website system and follows the same licensing terms.

---

**Note**: This module extends the existing broker website and does not replace any existing functionality. All new tables and features are additive to the current system.
