سلام! با کمال میل یک صفحه HTML شبیه به تریدینگ ویو برای نمایش چارت زنده بیتکوین به تتر در صرافی بایننس با قابلیت رسم خط روند براساس Series Primitives برایتان طراحی میکنم.
html Copy code
<!DOCTYPE html>
<html lang="fa" dir="rtl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>نمودار بیتکوین به تتر | بایننس</title>
<script src="https://cdn.jsdelivr.net/npm/lightweight-charts/dist/lightweight-charts.standalone.production.js"></script>
<style>
body {
font-family: 'Tahoma', sans-serif;
margin: 0;
padding: 0;
background-color: #151924;
color: #d1d4dc;
}
.container {
display: flex;
flex-direction: column;
height: 100vh;
}
.header {
display: flex;
justify-content: space-between;
align-items: center;
padding: 10px 20px;
background-color: #1e222d;
border-bottom: 1px solid #2a2e39;
}
.symbol-info {
font-size: 18px;
font-weight: bold;
}
.price-info {
display: flex;
gap: 15px;
}
.toolbar {
display: flex;
padding: 10px;
background-color: #1e222d;
border-bottom: 1px solid #2a2e39;
}
.tool-button {
background-color: #2a2e39;
border: none;
color: #d1d4dc;
padding: 5px 10px;
margin-right: 5px;
border-radius: 3px;
cursor: pointer;
}
.tool-button:hover {
background-color: #363c4e;
}
.tool-button.active {
background-color: #4c525e;
}
.chart-container {
flex-grow: 1;
position: relative;
}
#chart {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
}
.positive {
color: #26a69a;
}
.negative {
color: #ef5350;
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<div class="symbol-info">BTCUSDT</div>
<div class="price-info">
<span id="current-price">0.00</span>
<span id="price-change" class="positive">0.00%</span>
</div>
</div>
<div class="toolbar">
<button class="tool-button" id="btn-line">خط روند</button>
<button class="tool-button" id="btn-horizontal">خط افقی</button>
<button class="tool-button" id="btn-clear">پاک کردن</button>
<select id="timeframe" class="tool-button">
<option value="1m">۱ دقیقه</option>
<option value="5m">۵ دقیقه</option>
<option value="15m">۱۵ دقیقه</option>
<option value="1h">۱ ساعت</option>
<option value="4h">۴ ساعت</option>
<option value="1d">روزانه</option>
</select>
</div>
<div class="chart-container">
<div id="chart"></div>
</div>
</div>
<script>
// تنظیمات اولیه چارت
const chartContainer = document.getElementById('chart');
const chart = LightweightCharts.createChart(chartContainer, {
width: chartContainer.clientWidth,
height: chartContainer.clientHeight,
layout: {
backgroundColor: '#151924',
textColor: '#d1d4dc',
},
grid: {
vertLines: {
color: '#1e222d',
},
horzLines: {
color: '#1e222d',
},
},
crosshair: {
mode: LightweightCharts.CrosshairMode.Normal,
},
rightPriceScale: {
borderColor: '#2a2e39',
},
timeScale: {
borderColor: '#2a2e39',
},
});
// ایجاد سری قیمت
const candleSeries = chart.addCandlestickSeries({
upColor: '#26a69a',
downColor: '#ef5350',
borderDownColor: '#ef5350',
borderUpColor: '#26a69a',
wickDownColor: '#ef5350',
wickUpColor: '#26a69a',
});
// متغیرهای مورد نیاز برای رسم خط روند
let isDrawingLine = false;
let lineStart = null;
let currentLine = null;
let activeButton = null;
let drawingMode = null;
let lines = [];
// دکمههای ابزار
const btnLine = document.getElementById('btn-line');
const btnHorizontal = document.getElementById('btn-horizontal');
const btnClear = document.getElementById('btn-clear');
const timeframeSelect = document.getElementById('timeframe');
// فعال کردن حالت رسم خط روند
btnLine.addEventListener('click', () => {
setActiveButton(btnLine);
drawingMode = 'line';
});
// فعال کردن حالت رسم خط افقی
btnHorizontal.addEventListener('click', () => {
setActiveButton(btnHorizontal);
drawingMode = 'horizontal';
});
// پاک کردن همه خطوط
btnClear.addEventListener('click', () => {
lines.forEach(line => chart.removeSeries(line));
lines = [];
});
// تغییر تایم فریم
timeframeSelect.addEventListener('change', () => {
fetchData(timeframeSelect.value);
});
// تنظیم دکمه فعال
function setActiveButton(button) {
if (activeButton) {
activeButton.classList.remove('active');
}
button.classList.add('active');
activeButton = button;
}
// رویداد کلیک روی چارت
chartContainer.addEventListener('mousedown', (e) => {
if (!drawingMode) return;
const coords = chart.timeScale().coordinateToLogical(e.offsetX);
const price = chart.priceScale('right').coordinateToPrice(e.offsetY);
if (!coords) return;
lineStart = { time: coords, price: price };
isDrawingLine = true;
});
// رویداد حرکت موس روی چارت
chartContainer.addEventListener('mousemove', (e) => {
if (!isDrawingLine || !lineStart) return;
const coords = chart.timeScale().coordinateToLogical(e.offsetX);
const price = chart.priceScale('right').coordinateToPrice(e.offsetY);
if (!coords) return;
if (currentLine) {
chart.removeSeries(currentLine);
}
if (drawingMode === 'line') {
currentLine = chart.addLineSeries({
color: '#2196F3',
lineWidth: 2,
});
currentLine.setData([
{ time: lineStart.time, value: lineStart.price },
{ time: coords, value: price }
]);
} else if (drawingMode === 'horizontal') {
currentLine = chart.addLineSeries({
color: '#FF9800',
lineWidth: 2,
lineStyle: 2, // خط چین
});
const visibleRange = chart.timeScale().getVisibleRange();
if (visibleRange) {
currentLine.setData([
{ time: visibleRange.from, value: price },
{ time: visibleRange.to, value: price }
]);
}
}
});
// رویداد رها کردن کلیک موس
chartContainer.addEventListener('mouseup', () => {
if (isDrawingLine && currentLine) {
lines.push(currentLine);
currentLine = null;
}
isDrawingLine = false;
lineStart = null;
});
// تنظیم اندازه چارت با تغییر اندازه پنجره
window.addEventListener('resize', () => {
chart.applyOptions({
width: chartContainer.clientWidth,
height: chartContainer.clientHeight
});
});
// دریافت دادههای قیمت از API بایننس
async function fetchData(interval = '1m') {
try {
const response = await fetch(`https://api.binance.com/api/v3/klines?symbol=BTCUSDT&interval=${interval}&limit=1000`);
const data = await response.json();
const candles = data.map(d => ({
time: d[0] / 1000,
open: parseFloat(d[1]),
high: parseFloat(d[2]),
low: parseFloat(d[3]),
close: parseFloat(d[4])
}));
candleSeries.setData(candles);
// بهروزرسانی قیمت فعلی
const lastCandle = candles[candles.length - 1];
updatePrice(lastCandle.close, lastCandle.close - lastCandle.open);
// تنظیم محدوده زمانی نمایش
chart.timeScale().fitContent();
// شروع دریافت دادههای زنده
startLiveUpdates(interval);
} catch (error) {
console.error('خطا در دریافت دادهها:', error);
}
}
// بهروزرسانی قیمت نمایشی
function updatePrice(price, change) {
const priceElement = document.getElementById('current-price');
const changeElement = document.getElementById('price-change');
priceElement.textContent = price.toFixed(2);
const changePercent = (change / price * 100).toFixed(2);
changeElement.textContent = `${changePercent}%`;
if (change >= 0) {
changeElement.className = 'positive';
} else {
changeElement.className = 'negative';
}
}
// دریافت دادههای زنده
function startLiveUpdates(interval) {
// تبدیل فاصله زمانی به میلیثانیه
const intervalMap = {
'1m': 60000,
'5m': 300000,
'15m': 900000,
'1h': 3600000,
'4h': 14400000,
'1d': 86400000
};
const updateInterval = intervalMap[interval] || 60000;
// هر چند ثانیه یکبار آخرین کندل را دریافت میکنیم
setInterval(async () => {
try {
const response = await fetch(`https://api.binance.com/api/v3/klines?symbol=BTCUSDT&interval=${interval}&limit=1`);
const data = await response.json();
if (data && data.length > 0) {
const lastCandle = {
time: data[0][0] / 1000,
open: parseFloat(data[0][1]),
high: parseFloat(data[0][2]),
low: parseFloat(data[0][3]),
close: parseFloat(data[0][4])
};
candleSeries.update(lastCandle);
updatePrice(lastCandle.close, lastCandle.close - lastCandle.open);
}
} catch (error) {
console.error('خطا در بهروزرسانی دادهها:', error);
}
}, 5000); // هر 5 ثانیه بهروزرسانی میشود
}
// شروع برنامه
fetchData('1m');
</script>
</body>
</html>