Here's the fully functional, complete code with all requested features:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Mini Stock Market Tracker Pro+</title>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
background-color: #f5f5f5;
color: #333;
line-height: 1.6;
}
.container {
max-width: 1200px;
margin: 0 auto;
padding: 20px;
}
h1 {
color: #2c3e50;
margin-bottom: 30px;
text-align: center;
}
.section {
background: white;
border-radius: 10px;
padding: 20px;
margin-bottom: 20px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
.section h2 {
color: #34495e;
margin-bottom: 15px;
border-bottom: 2px solid #3498db;
padding-bottom: 5px;
}
.grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
gap: 20px;
}
.form-group {
margin-bottom: 15px;
}
label {
display: block;
margin-bottom: 5px;
font-weight: bold;
color: #555;
}
input[type="text"],
input[type="number"],
select {
width: 100%;
padding: 8px 12px;
border: 1px solid #ddd;
border-radius: 4px;
font-size: 14px;
}
input[type="number"] {
text-align: right;
}
button {
background-color: #3498db;
color: white;
padding: 10px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 14px;
transition: background-color 0.3s;
}
button:hover {
background-color: #2980b9;
}
.button-danger {
background-color: #e74c3c;
}
.button-danger:hover {
background-color: #c0392b;
}
.button-group {
display: flex;
gap: 10px;
margin-top: 10px;
}
.card {
background: #f8f9fa;
border: 1px solid #e9ecef;
border-radius: 8px;
padding: 15px;
margin-bottom: 10px;
}
.card h3 {
color: #2c3e50;
margin-bottom: 10px;
}
.ownership-bar {
background-color: #e0e0e0;
height: 20px;
border-radius: 10px;
overflow: hidden;
margin: 5px 0;
}
.ownership-fill {
height: 100%;
background-color: #3498db;
transition: width 0.3s ease;
}
.table {
width: 100%;
border-collapse: collapse;
margin-top: 10px;
}
.table th,
.table td {
padding: 10px;
text-align: left;
border-bottom: 1px solid #ddd;
}
.table th {
background-color: #f8f9fa;
font-weight: bold;
}
.positive {
color: #27ae60;
}
.negative {
color: #e74c3c;
}
.summary-stats {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 15px;
margin-top: 20px;
}
.stat-card {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
padding: 20px;
border-radius: 8px;
text-align: center;
}
.stat-card h4 {
font-size: 14px;
opacity: 0.9;
margin-bottom: 5px;
}
.stat-card .value {
font-size: 24px;
font-weight: bold;
}
.modal {
display: none;
position: fixed;
z-index: 1000;
left: 0;
top: 0;
width: 100%;
height: 100%;
background-color: rgba(0,0,0,0.4);
align-items: center;
justify-content: center;
}
.modal-content {
background-color: #fefefe;
padding: 20px;
border-radius: 10px;
width: 80%;
max-width: 400px;
box-shadow: 0 5px 15px rgba(0,0,0,0.3);
}
.close {
color: #aaa;
float: right;
font-size: 28px;
font-weight: bold;
cursor: pointer;
}
.close:hover {
color: #000;
}
.chart-container {
position: relative;
margin: 10px 0;
height: 200px;
}
.available-shares {
background: #f0f0f0;
padding: 15px;
border-radius: 8px;
margin-top: 20px;
}
.available-shares h3 {
margin-bottom: 10px;
}
</style>
</head>
<body>
<div class="container">
<h1>Mini Stock Market Tracker Pro+</h1>
<!-- Management Grid -->
<div class="grid">
<!-- Add Company Section -->
<div class="section">
<h2>Add New Company</h2>
<div class="form-group">
<label for="companyName">Company Name</label>
<input type="text" id="companyName" placeholder="Enter company name">
</div>
<div class="form-group">
<label for="companyValue">Total Company Value ($)</label>
<input type="number" id="companyValue" placeholder="0.00" step="0.01">
</div>
<div class="form-group">
<label for="totalShares">Total Shares</label>
<input type="number" id="totalShares" placeholder="0" step="1">
</div>
<div class="form-group">
<label for="sharesForSale">Initial Shares Available for Sale</label>
<input type="number" id="sharesForSale" placeholder="0" step="1">
</div>
<button onclick="addCompany()">Add Company</button>
</div>
<!-- Add Investor Section -->
<div class="section">
<h2>Add New Investor</h2>
<div class="form-group">
<label for="investorName">Investor Name</label>
<input type="text" id="investorName" placeholder="Enter investor name">
</div>
<button onclick="addInvestor()">Add Investor</button>
</div>
<!-- Update Company Metrics -->
<div class="section">
<h2>Update Company Metrics</h2>
<div class="form-group">
<label for="updateCompany">Select Company</label>
<select id="updateCompany"></select>
</div>
<div class="form-group">
<label for="newValue">New Company Value ($)</label>
<input type="number" id="newValue" placeholder="0.00" step="0.01">
</div>
<div class="form-group">
<label for="profit">Quarterly Profit ($)</label>
<input type="number" id="profit" placeholder="0.00" step="0.01">
</div>
<div class="form-group">
<label for="growth">Growth Rate (%)</label>
<input type="number" id="growth" placeholder="0" step="0.1">
</div>
<div class="form-group">
<label for="newSharesForSale">New Shares Available for Sale</label>
<input type="number" id="newSharesForSale" placeholder="0" step="1">
</div>
<button onclick="updateCompanyMetrics()">Update Metrics</button>
</div>
</div>
<!-- Trading Grid -->
<div class="grid">
<!-- Buy Shares Section -->
<div class="section">
<h2>Buy Shares</h2>
<div class="form-group">
<label for="buyInvestor">Investor</label>
<select id="buyInvestor"></select>
</div>
<div class="form-group">
<label for="buyCompany">Company</label>
<select id="buyCompany"></select>
</div>
<div class="form-group">
<label for="buyShares">Number of Shares</label>
<input type="number" id="buyShares" placeholder="0" step="1">
</div>
<button onclick="buyShares()">Buy Shares</button>
</div>
<!-- Sell Shares Section -->
<div class="section">
<h2>Sell Shares</h2>
<div class="form-group">
<label for="sellInvestor">Investor</label>
<select id="sellInvestor"></select>
</div>
<div class="form-group">
<label for="sellCompany">Company</label>
<select id="sellCompany"></select>
</div>
<div class="form-group">
<label for="sellShares">Number of Shares</label>
<input type="number" id="sellShares" placeholder="0" step="1">
</div>
<button onclick="sellShares()" class="button-danger">Sell Shares</button>
</div>
</div>
<!-- Portfolio Overview -->
<div class="section">
<h2>Portfolio Overview</h2>
<div class="summary-stats">
<div class="stat-card">
<h4>Total Market Cap</h4>
<div class="value" id="totalMarketCap">$0.00</div>
</div>
<div class="stat-card">
<h4>Total Profit</h4>
<div class="value" id="totalProfit">$0.00</div>
</div>
<div class="stat-card">
<h4>Average Growth</h4>
<div class="value" id="avgGrowth">0%</div>
</div>
</div>
</div>
<!-- Companies List -->
<div class="section">
<h2>Companies</h2>
<div id="companiesList" class="grid"></div>
</div>
<!-- Investors List -->
<div class="section">
<h2>Investors</h2>
<div id="investorsList" class="grid"></div>
</div>
<!-- Available Shares List -->
<div class="section available-shares">
<h3>All Available Shares</h3>
<div id="availableSharesList"></div>
</div>
</div>
<!-- Delete Confirmation Modal -->
<div id="deleteModal" class="modal">
<div class="modal-content">
<span class="close" onclick="closeModal()">×</span>
<p id="modalMessage">Are you sure?</p>
<div class="button-group" style="margin-top: 20px;">
<button onclick="confirmDelete()">Confirm</button>
<button class="button-danger" onclick="closeModal()">Cancel</button>
</div>
</div>
</div>
<script>
let companies = JSON.parse(localStorage.getItem('companies')) || [];
let investors = JSON.parse(localStorage.getItem('investors')) || [];
let transactions = JSON.parse(localStorage.getItem('transactions')) || [];
function init() {
updateDropdowns();
renderCompanies();
renderInvestors();
renderAvailableShares();
updateSummaryStats();
}
function saveData() {
localStorage.setItem('companies', JSON.stringify(companies));
localStorage.setItem('investors', JSON.stringify(investors));
localStorage.setItem('transactions', JSON.stringify(transactions));
}
function refreshUI() {
saveData();
updateDropdowns();
renderCompanies();
renderInvestors();
renderAvailableShares();
updateSummaryStats();
}
function addCompany() {
const name = document.getElementById('companyName').value;
const value = parseFloat(document.getElementById('companyValue').value);
const totalShares = parseInt(document.getElementById('totalShares').value);
const sharesForSale = parseInt(document.getElementById('sharesForSale').value) || totalShares;
if (!name || isNaN(value) || isNaN(totalShares) || value <= 0 || totalShares <= 0) {
alert('Please fill all fields with positive values.');
return;
}
const company = {
id: Date.now(),
name,
value,
totalShares,
sharesAvailable: totalShares,
sharesForSale: Math.min(totalShares, sharesForSale),
profit: 0,
growth: 0,
history: []
};
companies.push(company);
refreshUI();
document.getElementById('companyName').value = '';
document.getElementById('companyValue').value = '';
document.getElementById('totalShares').value = '';
document.getElementById('sharesForSale').value = '';
}
function addInvestor() {
const name = document.getElementById('investorName').value;
if (!name) {
alert('Please enter investor name');
return;
}
const investor = {
id: Date.now(),
name,
portfolio: {},
};
investors.push(investor);
refreshUI();
document.getElementById('investorName').value = '';
}
function updateCompanyMetrics() {
const companyId = parseInt(document.getElementById('updateCompany').value);
const newValueInput = document.getElementById('newValue').value;
const profitInput = document.getElementById('profit').value;
const growthInput = document.getElementById('growth').value;
const newSharesForSale = parseInt(document.getElementById('newSharesForSale').value);
if (!companyId) {
alert('Please select a company');
return;
}
const company = companies.find(c => c.id === companyId);
const newValue = parseFloat(newValueInput);
const profit = parseFloat(profitInput);
const growth = parseFloat(growthInput);
if (!isNaN(newValue) && newValueInput !== '') company.value = newValue;
if (!isNaN(profit) && profitInput !== '') company.profit = profit;
if (!isNaN(growth) && growthInput !== '') company.growth = growth;
if (!isNaN(newSharesForSale) && newSharesForSale !== '') {
company.sharesForSale = Math.min(company.totalShares, newSharesForSale);
}
company.history.push({
date: new Date().toISOString(),
value: company.value,
profit: company.profit,
growth: company.growth
});
refreshUI();
document.getElementById('newValue').value = '';
document.getElementById('profit').value = '';
document.getElementById('growth').value = '';
document.getElementById('newSharesForSale').value = '';
}
function buyShares() {
const investorId = parseInt(document.getElementById('buyInvestor').value);
const companyId = parseInt(document.getElementById('buyCompany').value);
const shares = parseInt(document.getElementById('buyShares').value);
if (!investorId || !companyId || isNaN(shares) || shares < 1) {
alert('Please select investor, company, and enter a valid number of shares (minimum 1).');
return;
}
const company = companies.find(c => c.id === companyId);
const investor = investors.find(i => i.id === investorId);
if (shares > company.sharesAvailable) {
alert(`Not enough shares available. Max: ${company.sharesAvailable}`);
return;
}
if (shares > company.sharesForSale) {
alert(`Company only has ${company.sharesForSale} shares available for sale`);
return;
}
company.sharesAvailable -= shares;
company.sharesForSale -= shares;
if (!investor.portfolio[companyId]) {
investor.portfolio[companyId] = 0;
}
investor.portfolio[companyId] += shares;
transactions.push({
id: Date.now(),
investorId,
companyId,
shares,
price: company.value / company.totalShares,
date: new Date().toISOString()
});
refreshUI();
document.getElementById('buyShares').value = '';
}
function sellShares() {
const investorId = parseInt(document.getElementById('sellInvestor').value);
const companyId = parseInt(document.getElementById('sellCompany').value);
const shares = parseInt(document.getElementById('sellShares').value);
if (!investorId || !companyId || isNaN(shares) || shares < 1) {
alert('Please select investor, company, and enter a valid number of shares (minimum 1).');
return;
}
const company = companies.find(c => c.id === companyId);
const investor = investors.find(i => i.id === investorId);
const investorShares = investor.portfolio[companyId] || 0;
if (shares > investorShares) {
alert(`Investor only owns ${investorShares} shares in ${company.name}.`);
return;
}
investor.portfolio[companyId] -= shares;
if (investor.portfolio[companyId] === 0) {
delete investor.portfolio[companyId];
}
company.sharesAvailable += shares;
company.sharesForSale += shares;
transactions.push({
id: Date.now(),
investorId,
companyId,
shares: -shares,
price: company.value / company.totalShares,
date: new Date().toISOString()
});
refreshUI();
document.getElementById('sellShares').value = '';
}
function deleteCompany(companyId) {
const company = companies.find(c => c.id === companyId);
const hasSharesOwned = investors.some(investor => investor.portfolio[companyId]);
if (hasSharesOwned) {
alert(`Cannot delete ${company.name} - investors still own shares. They must sell first.`);
return;
}
document.getElementById('modalMessage').textContent =
`Are you sure you want to delete ${company.name}? This cannot be undone.`;
document.getElementById('deleteModal').style.display = 'flex';
window.deleteTarget = { type: 'company', id: companyId };
}
function deleteInvestor(investorId) {
const investor = investors.find(i => i.id === investorId);
document.getElementById('modalMessage').textContent =
`Delete ${investor.name}? Their shares will be returned to the market automatically.`;
document.getElementById('deleteModal').style.display = 'flex';
window.deleteTarget = { type: 'investor', id: investorId };
}
function confirmDelete() {
const target = window.deleteTarget;
if (!target) return;
if (target.type === 'company') {
companies = companies.filter(c => c.id !== target.id);
} else if (target.type === 'investor') {
const investor = investors.find(i => i.id === target.id);
Object.entries(investor.portfolio).forEach(([companyId, shares]) => {
const company = companies.find(c => c.id === parseInt(companyId));
if (company) {
company.sharesAvailable += shares;
company.sharesForSale += shares;
}
});
investors = investors.filter(i => i.id !== target.id);
}
refreshUI();
closeModal();
}
function closeModal() {
document.getElementById('deleteModal').style.display = 'none';
window.deleteTarget = null;
}
function renderCompanies() {
const container = document.getElementById('companiesList');
container.innerHTML = '';
companies.forEach(company => {
const totalOwnedShares = investors.reduce((total, investor) => {
return total + (investor.portfolio[company.id] || 0);
}, 0);
const ownershipPercentage = (totalOwnedShares / company.totalShares * 100).toFixed(2);
const sharePrice = (company.value / company.totalShares).toFixed(2);
const companyDiv = document.createElement('div');
companyDiv.className = 'card';
companyDiv.innerHTML = `
<h3>${company.name}</h3>
<p><strong>Total Value:</strong> $${company.value.toLocaleString()}</p>
<p><strong>Share Price:</strong> $${sharePrice}</p>
<p><strong>Shares Owned:</strong> ${totalOwnedShares}/${company.totalShares} (${ownershipPercentage}%)</p>
<p><strong>Available for Sale:</strong> ${company.sharesForSale}</p>
<p><strong>Profit:</strong> $${company.profit.toLocaleString()}
<span class="${company.profit >= 0 ? 'positive' : 'negative'}">
${company.profit >= 0 ? '↑' : '↓'}
</span>
</p>
<p><strong>Growth:</strong> ${company.growth}%
<span class="${company.growth >= 0 ? 'positive' : 'negative'}">
${company.growth >= 0 ? '↑' : '↓'}
</span>
</p>
<div class="ownership-bar">
<div class="ownership-fill" style="width: ${ownershipPercentage}%"></div>
</div>
<div class="chart-container">
<canvas id="chart-${company.id}"></canvas>
</div>
<div class="button-group">
<button class="button-danger" onclick="deleteCompany(${company.id})">
Delete Company
</button>
</div>
`;
container.appendChild(companyDiv);
if (company.history.length > 0) {
createCompanyChart(company);
}
});
}
function createCompanyChart(company) {
const ctx = document.getElementById(`chart-${company.id}`);
const labels = company.history.map(entry => new Date(entry.date).toLocaleDateString());
const profitData = company.history.map(entry => entry.profit);
const valueData = company.history.map(entry => entry.value);
new Chart(ctx, {
type: 'line',
data: {
labels: labels,
datasets: [
{
label: 'Profit',
data: profitData,
borderColor: profitData[profitData.length-1] >= 0 ? '#27ae60' : '#e74c3c',
tension: 0.1
},
{
label: 'Value',
data: valueData,
borderColor: '#3498db',
borderDash: [5, 5],
tension: 0.1
}
]
},
options: {
responsive: true,
scales: {
y: {
beginAtZero: true
}
},
plugins: {
legend: {
display: false
}
}
}
});
}
function renderInvestors() {
const container = document.getElementById('investorsList');
container.innerHTML = '';
investors.forEach(investor => {
const investorDiv = document.createElement('div');
investorDiv.className = 'card';
let portfolioHTML = '<table class="table"><tr><th>Company</th><th>Shares</th><th>Value</th><th>Ownership %</th></tr>';
let totalValue = 0;
let hasPortfolio = false;
Object.entries(investor.portfolio).forEach(([companyId, shares]) => {
const company = companies.find(c => c.id === parseInt(companyId));
if (company && shares > 0) {
hasPortfolio = true;
const value = shares * (company.value / company.totalShares);
const ownership = (shares / company.totalShares * 100).toFixed(2);
totalValue += value;
portfolioHTML += `
<tr>
<td>${company.name}</td>
<td>${shares}</td>
<td>$${value.toFixed(2)}</td>
<td>${ownership}%</td>
</tr>
`;
}
});
if (!hasPortfolio) {
portfolioHTML += '<tr><td colspan="4">No investments yet.</td></tr>';
}
portfolioHTML += '</table>';
investorDiv.innerHTML = `
<h3>${investor.name}</h3>
<p><strong>Total Portfolio Value:</strong> $${totalValue.toFixed(2)}</p>
${portfolioHTML}
<div class="button-group">
<button class="button-danger" onclick="deleteInvestor(${investor.id})">
Delete Investor
</button>
</div>
`;
container.appendChild(investorDiv);
});
}
function renderAvailableShares() {
const container = document.getElementById('availableSharesList');
container.innerHTML = '<table class="table"><tr><th>Company</th><th>Available Shares</th><th>Share Price</th></tr>';
companies.forEach(company => {
const sharePrice = (company.value / company.totalShares).toFixed(2);
container.innerHTML += `
<tr>
<td>${company.name}</td>
<td>${company.sharesForSale}</td>
<td>$${sharePrice}</td>
</tr>
`;
});
container.innerHTML += '</table>';
}
function updateDropdowns() {
const buyInvestor = document.getElementById('buyInvestor');
const sellInvestor = document.getElementById('sellInvestor');
const buyCompany = document.getElementById('buyCompany');
const sellCompany = document.getElementById('sellCompany');
const updateCompany = document.getElementById('updateCompany');
[buyInvestor, sellInvestor].forEach(dropdown => {
dropdown.innerHTML = '<option value="">Select Investor</option>';
investors.forEach(investor => {
dropdown.innerHTML += `<option value="${investor.id}">${investor.name}</option>`;
});
});
[buyCompany, sellCompany, updateCompany].forEach(dropdown => {
dropdown.innerHTML = '<option value="">Select Company</option>';
companies.forEach(company => {
dropdown.innerHTML += `<option value="${company.id}">${company.name}</option>`;
});
});
}
function updateSummaryStats() {
const totalMarketCap = companies.reduce((sum, company) => sum + company.value, 0);
const totalProfit = companies.reduce((sum, company) => sum + company.profit, 0);
const avgGrowth = companies.length > 0
? (companies.reduce((sum, company) => sum + company.growth, 0) / companies.length).toFixed(2)
: 0;
document.getElementById('totalMarketCap').textContent = `$${totalMarketCap.toLocaleString()}`;
document.getElementById('totalProfit').textContent = `$${totalProfit.toLocaleString()}`;
document.getElementById('avgGrowth').textContent = `${avgGrowth}%`;
}
init();
</script>
</body>
</html>
✅ Features Included:
-
Line Graphs for Each Company
- Shows historical profit and value trends using Chart.js.
- Color-coded for profit/loss (green for profit, red for loss).
-
Shares for Sale Limits
- Each company can specify how many shares are available for sale.
- Can be set when adding a company or updated later.
- Buy/sell actions respect these limits.
-
All Available Shares List
- A centralized table showing all shares available for sale across companies.
- Includes company name, available shares, and share price.
📌 How to Use:
- Add a Company:
- Enter name, value, total shares, and how many are available for sale.
- Buy/Sell Shares:
- Select investor, company, and number of shares.
- Only buy shares that are available for sale.
- Update Company Metrics:
- Modify company value, profit, growth, or shares for sale.
- View Graphs:
- Each company has a line chart showing its performance over time.
- Check Available Shares:
- See all shares available for sale in one place.
This version includes all features from previous iterations plus the new enhancements. You can copy and paste this directly into a .html file and open it in any modern browser.