<?
php
require_once '[Link]';
// Handle form submissions
if ($_POST) {
if (isset($_POST['action'])) {
switch ($_POST['action']) {
case 'add_trade':
$stmt = $pdo->prepare("INSERT INTO trades (symbol, trade_type,
quantity, entry_price, entry_date, strategy, notes) VALUES (?, ?, ?, ?, ?, ?, ?)");
$stmt->execute([
$_POST['symbol'],
$_POST['trade_type'],
$_POST['quantity'],
$_POST['entry_price'],
$_POST['entry_date'],
$_POST['strategy'],
$_POST['notes']
]);
break;
case 'close_trade':
$exit_price = $_POST['exit_price'];
$trade_id = $_POST['trade_id'];
// Get trade details to calculate P&L
$stmt = $pdo->prepare("SELECT * FROM trades WHERE id = ?");
$stmt->execute([$trade_id]);
$trade = $stmt->fetch();
$pnl = calculatePnL($trade['entry_price'], $exit_price,
$trade['quantity'], $trade['trade_type']);
$stmt = $pdo->prepare("UPDATE trades SET exit_price = ?, exit_date
= NOW(), profit_loss = ?, status = 'CLOSED' WHERE id = ?");
$stmt->execute([$exit_price, $pnl, $trade_id]);
break;
case 'delete_trade':
$stmt = $pdo->prepare("DELETE FROM trades WHERE id = ?");
$stmt->execute([$_POST['trade_id']]);
break;
}
}
}
// Fetch statistics
$stats_query = "SELECT
COUNT(*) as total_trades,
COUNT(CASE WHEN status = 'CLOSED' THEN 1 END) as closed_trades,
COUNT(CASE WHEN status = 'OPEN' THEN 1 END) as open_trades,
COALESCE(SUM(CASE WHEN status = 'CLOSED' THEN profit_loss END), 0) as
total_pnl,
COALESCE(AVG(CASE WHEN status = 'CLOSED' THEN profit_loss END), 0) as avg_pnl,
COUNT(CASE WHEN status = 'CLOSED' AND profit_loss > 0 THEN 1 END) as
winning_trades,
COUNT(CASE WHEN status = 'CLOSED' AND profit_loss < 0 THEN 1 END) as
losing_trades
FROM trades";
$stats = $pdo->query($stats_query)->fetch();
// Calculate win rate
$win_rate = $stats['closed_trades'] > 0 ? ($stats['winning_trades'] /
$stats['closed_trades']) * 100 : 0;
// Fetch all trades
$trades = $pdo->query("SELECT * FROM trades ORDER BY created_at DESC")->fetchAll();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Trading Journal</title>
<link href="[Link]
[Link]" rel="stylesheet">
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
color: #333;
}
.container {
max-width: 1400px;
margin: 0 auto;
padding: 20px;
}
.header {
text-align: center;
margin-bottom: 30px;
}
.header h1 {
color: white;
font-size: 2.5rem;
font-weight: 700;
margin-bottom: 10px;
}
.header p {
color: rgba(255, 255, 255, 0.8);
font-size: 1.1rem;
}
.stats-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 20px;
margin-bottom: 30px;
}
.stat-card {
background: rgba(255, 255, 255, 0.95);
backdrop-filter: blur(10px);
border-radius: 15px;
padding: 25px;
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
border: 1px solid rgba(255, 255, 255, 0.2);
transition: transform 0.3s ease;
}
.stat-card:hover {
transform: translateY(-5px);
}
.stat-card .icon {
font-size: 2rem;
margin-bottom: 15px;
}
.stat-card .value {
font-size: 2rem;
font-weight: 700;
margin-bottom: 5px;
}
.stat-card .label {
color: #666;
font-size: 0.9rem;
text-transform: uppercase;
letter-spacing: 0.5px;
}
.profit { color: #10b981; }
.loss { color: #ef4444; }
.neutral { color: #6b7280; }
.main-content {
display: grid;
grid-template-columns: 1fr 2fr;
gap: 30px;
margin-bottom: 30px;
}
.add-trade-form {
background: rgba(255, 255, 255, 0.95);
backdrop-filter: blur(10px);
border-radius: 15px;
padding: 30px;
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
border: 1px solid rgba(255, 255, 255, 0.2);
height: fit-content;
}
.add-trade-form h2 {
margin-bottom: 25px;
color: #333;
font-size: 1.5rem;
}
.form-group {
margin-bottom: 20px;
}
.form-group label {
display: block;
margin-bottom: 8px;
font-weight: 600;
color: #555;
}
.form-group input,
.form-group select,
.form-group textarea {
width: 100%;
padding: 12px 15px;
border: 2px solid #e1e5e9;
border-radius: 10px;
font-size: 1rem;
transition: border-color 0.3s ease;
background: rgba(255, 255, 255, 0.8);
}
.form-group input:focus,
.form-group select:focus,
.form-group textarea:focus {
outline: none;
border-color: #667eea;
}
.btn {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
border: none;
padding: 12px 25px;
border-radius: 10px;
font-size: 1rem;
font-weight: 600;
cursor: pointer;
transition: transform 0.3s ease;
width: 100%;
}
.btn:hover {
transform: translateY(-2px);
}
.btn-danger {
background: linear-gradient(135deg, #ef4444 0%, #dc2626 100%);
}
.btn-success {
background: linear-gradient(135deg, #10b981 0%, #059669 100%);
}
.trades-section {
background: rgba(255, 255, 255, 0.95);
backdrop-filter: blur(10px);
border-radius: 15px;
padding: 30px;
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
border: 1px solid rgba(255, 255, 255, 0.2);
}
.trades-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 25px;
}
.trades-header h2 {
color: #333;
font-size: 1.5rem;
}
.trade-card {
background: rgba(255, 255, 255, 0.7);
border-radius: 12px;
padding: 20px;
margin-bottom: 15px;
border-left: 4px solid #667eea;
transition: transform 0.3s ease;
}
.trade-card:hover {
transform: translateX(5px);
}
.trade-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 15px;
}
.trade-symbol {
font-size: 1.2rem;
font-weight: 700;
color: #333;
}
.trade-type {
padding: 4px 12px;
border-radius: 20px;
font-size: 0.8rem;
font-weight: 600;
text-transform: uppercase;
}
.[Link] {
background: rgba(16, 185, 129, 0.1);
color: #10b981;
}
.[Link] {
background: rgba(239, 68, 68, 0.1);
color: #ef4444;
}
.trade-details {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
gap: 15px;
margin-bottom: 15px;
}
.trade-detail {
text-align: center;
}
.trade-detail .label {
font-size: 0.8rem;
color: #666;
text-transform: uppercase;
letter-spacing: 0.5px;
margin-bottom: 4px;
}
.trade-detail .value {
font-weight: 700;
color: #333;
}
.trade-actions {
display: flex;
gap: 10px;
margin-top: 15px;
}
.trade-actions .btn {
width: auto;
padding: 8px 15px;
font-size: 0.9rem;
}
.modal {
display: none;
position: fixed;
z-index: 1000;
left: 0;
top: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
backdrop-filter: blur(5px);
}
.modal-content {
background: white;
margin: 15% auto;
padding: 30px;
border-radius: 15px;
width: 90%;
max-width: 500px;
box-shadow: 0 20px 40px rgba(0, 0, 0, 0.3);
}
.close {
color: #aaa;
float: right;
font-size: 28px;
font-weight: bold;
cursor: pointer;
}
.close:hover {
color: #000;
}
.status-badge {
padding: 4px 12px;
border-radius: 20px;
font-size: 0.8rem;
font-weight: 600;
text-transform: uppercase;
}
.status-open {
background: rgba(59, 130, 246, 0.1);
color: #3b82f6;
}
.status-closed {
background: rgba(16, 185, 129, 0.1);
color: #10b981;
}
@media (max-width: 768px) {
.main-content {
grid-template-columns: 1fr;
}
.stats-grid {
grid-template-columns: repeat(2, 1fr);
}
.trade-details {
grid-template-columns: repeat(2, 1fr);
}
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1><i class="fas fa-chart-line"></i> Trading Journal</h1>
<p>Track your trades, analyze performance, and improve your
strategy</p>
</div>
<!-- Statistics Cards -->
<div class="stats-grid">
<div class="stat-card">
<div class="icon"><i class="fas fa-exchange-alt neutral"></i></div>
<div class="value"><?= $stats['total_trades'] ?></div>
<div class="label">Total Trades</div>
</div>
<div class="stat-card">
<div class="icon"><i class="fas fa-dollar-sign <?=
$stats['total_pnl'] >= 0 ? 'profit' : 'loss' ?>"></i></div>
<div class="value <?= $stats['total_pnl'] >= 0 ? 'profit' :
'loss' ?>"><?= formatCurrency($stats['total_pnl']) ?></div>
<div class="label">Total P&L</div>
</div>
<div class="stat-card">
<div class="icon"><i class="fas fa-percentage neutral"></i></div>
<div class="value"><?= number_format($win_rate, 1) ?>%</div>
<div class="label">Win Rate</div>
</div>
<div class="stat-card">
<div class="icon"><i class="fas fa-chart-bar neutral"></i></div>
<div class="value <?= $stats['avg_pnl'] >= 0 ? 'profit' : 'loss' ?
>"><?= formatCurrency($stats['avg_pnl']) ?></div>
<div class="label">Avg P&L</div>
</div>
<div class="stat-card">
<div class="icon"><i class="fas fa-lock-open neutral"></i></div>
<div class="value"><?= $stats['open_trades'] ?></div>
<div class="label">Open Trades</div>
</div>
<div class="stat-card">
<div class="icon"><i class="fas fa-check-circle profit"></i></div>
<div class="value"><?= $stats['winning_trades'] ?></div>
<div class="label">Winning Trades</div>
</div>
</div>
<!-- Main Content -->
<div class="main-content">
<!-- Add Trade Form -->
<div class="add-trade-form">
<h2><i class="fas fa-plus-circle"></i> Add New Trade</h2>
<form method="POST">
<input type="hidden" name="action" value="add_trade">
<div class="form-group">
<label for="symbol">Symbol</label>
<input type="text" id="symbol" name="symbol" required
placeholder="e.g., EURUSD">
</div>
<div class="form-group">
<label for="trade_type">Trade Type</label>
<select id="trade_type" name="trade_type" required>
<option value="BUY">Buy</option>
<option value="SELL">Sell</option>
</select>
</div>
<div class="form-group">
<label for="quantity">Quantity</label>
<input type="number" id="quantity" name="quantity"
step="0.01" required>
</div>
<div class="form-group">
<label for="entry_price">Entry Price</label>
<input type="number" id="entry_price" name="entry_price"
step="0.00001" required>
</div>
<div class="form-group">
<label for="entry_date">Entry Date & Time</label>
<input type="datetime-local" id="entry_date"
name="entry_date" required>
</div>
<div class="form-group">
<label for="strategy">Strategy</label>
<input type="text" id="strategy" name="strategy"
placeholder="e.g., Breakout, Reversal">
</div>
<div class="form-group">
<label for="notes">Notes</label>
<textarea id="notes" name="notes" rows="3"
placeholder="Trade analysis and notes..."></textarea>
</div>
<button type="submit" class="btn">
<i class="fas fa-plus"></i> Add Trade
</button>
</form>
</div>
<!-- Trades List -->
<div class="trades-section">
<div class="trades-header">
<h2><i class="fas fa-list"></i> Trade History</h2>
</div>
<?php foreach ($trades as $trade): ?>
<div class="trade-card">
<div class="trade-header">
<div class="trade-symbol"><?=
htmlspecialchars($trade['symbol']) ?></div>
<div class="trade-badges">
<span class="trade-type <?=
strtolower($trade['trade_type']) ?>">
<?= $trade['trade_type'] ?>
</span>
<span class="status-badge status-<?=
strtolower($trade['status']) ?>">
<?= $trade['status'] ?>
</span>
</div>
</div>
<div class="trade-details">
<div class="trade-detail">
<div class="label">Quantity</div>
<div class="value"><?=
number_format($trade['quantity'], 2) ?></div>
</div>
<div class="trade-detail">
<div class="label">Entry Price</div>
<div class="value"><?=
number_format($trade['entry_price'], 5) ?></div>
</div>
<?php if ($trade['exit_price']): ?>
<div class="trade-detail">
<div class="label">Exit Price</div>
<div class="value"><?=
number_format($trade['exit_price'], 5) ?></div>
</div>
<?php endif; ?>
<div class="trade-detail">
<div class="label">P&L</div>
<div class="value <?= $trade['profit_loss'] >= 0 ?
'profit' : 'loss' ?>">
<?= formatCurrency($trade['profit_loss']) ?>
</div>
</div>
<div class="trade-detail">
<div class="label">Entry Date</div>
<div class="value"><?=
formatDate($trade['entry_date']) ?></div>
</div>
<?php if ($trade['strategy']): ?>
<div class="trade-detail">
<div class="label">Strategy</div>
<div class="value"><?=
htmlspecialchars($trade['strategy']) ?></div>
</div>
<?php endif; ?>
</div>
<?php if ($trade['notes']): ?>
<div style="margin-top: 10px; padding: 10px;
background: rgba(0,0,0,0.05); border-radius: 8px;">
<small><strong>Notes:</strong> <?=
htmlspecialchars($trade['notes']) ?></small>
</div>
<?php endif; ?>
<div class="trade-actions">
<?php if ($trade['status'] === 'OPEN'): ?>
<button class="btn btn-success"
onclick="openCloseModal(<?= $trade['id'] ?>)">
<i class="fas fa-times-circle"></i> Close Trade
</button>
<?php endif; ?>
<button class="btn btn-danger" onclick="deleteTrade(<?=
$trade['id'] ?>)">
<i class="fas fa-trash"></i> Delete
</button>
</div>
</div>
<?php endforeach; ?>
<?php if (empty($trades)): ?>
<div style="text-align: center; padding: 40px; color: #666;">
<i class="fas fa-chart-line" style="font-size: 3rem;
margin-bottom: 20px; opacity: 0.3;"></i>
<p>No trades yet. Add your first trade to get started!</p>
</div>
<?php endif; ?>
</div>
</div>
</div>
<!-- Close Trade Modal -->
<div id="closeModal" class="modal">
<div class="modal-content">
<span class="close">×</span>
<h2>Close Trade</h2>
<form method="POST" id="closeTradeForm">
<input type="hidden" name="action" value="close_trade">
<input type="hidden" name="trade_id" id="closeTradeId">
<div class="form-group">
<label for="exit_price">Exit Price</label>
<input type="number" id="exit_price" name="exit_price"
step="0.00001" required>
</div>
<button type="submit" class="btn btn-success">Close Trade</button>
</form>
</div>
</div>
<script>
// Modal functionality
const modal = [Link]('closeModal');
const span = [Link]('close')[0];
function openCloseModal(tradeId) {
[Link]('closeTradeId').value = tradeId;
[Link] = 'block';
}
[Link] = function() {
[Link] = 'none';
}
[Link] = function(event) {
if ([Link] == modal) {
[Link] = 'none';
}
}
function deleteTrade(tradeId) {
if (confirm('Are you sure you want to delete this trade?')) {
const form = [Link]('form');
[Link] = 'POST';
[Link] = `
<input type="hidden" name="action" value="delete_trade">
<input type="hidden" name="trade_id" value="${tradeId}">
`;
[Link](form);
[Link]();
}
}
// Set current datetime for new trades
[Link]('entry_date').value = new
Date().toISOString().slice(0, 16);
</script>
</body>
</html>