maher-tradingview-api
Version:
Tradingview instant stocks API, indicator alerts, trading bot, and more !
276 lines (244 loc) • 10.7 kB
JavaScript
/* eslint-disable linebreak-style */
/* eslint-disable no-restricted-properties */
/* eslint-disable linebreak-style */
/* eslint-disable no-unused-vars */
/* eslint-disable prefer-const */
/* eslint-disable linebreak-style */
/* eslint-disable space-before-function-paren */
/* eslint-disable no-const-assign */
/* eslint-disable arrow-parens */
/* eslint-disable no-use-before-define */
/* eslint-disable comma-dangle */
/* eslint-disable no-plusplus */
/* eslint-disable no-trailing-spaces */
/* eslint-disable object-shorthand */
/* eslint-disable func-names */
/* eslint-disable linebreak-style */
/* eslint-disable no-undef */
/* eslint-disable linebreak-style */
/**
* chart-initializer-script.js
* سكريبت لإضافته إلى ملف market-data-clean.ejs لبدء تشغيل الرسوم البيانية
*/
// وظيفة لإعداد الرسوم البيانية للمؤشرات الفنية
function setupTechnicalCharts() {
if (window.candleData && Array.isArray(window.candleData) && window.candleData.length > 0) {
// رسم مؤشر المتوسطات المتحركة
if (document.getElementById('maChart')) {
window.TechnicalIndicators.renderMAChart('maChart', window.candleData, [20, 50, 200]);
}
// رسم مؤشر المتوسط المتحرك الأسي
if (document.getElementById('emaChart') && window.TechnicalIndicators) {
const emaData = window.candleData;
// إنشاء إعدادات EMA مشابهة لـ MA
const ctx = document.getElementById('emaChart').getContext('2d');
// تدمير أي رسم بياني موجود مسبقًا على نفس العنصر
const existingChart = Chart.getChart(ctx.canvas);
if (existingChart) {
existingChart.destroy();
}
const prices = emaData.map(candle => parseFloat(candle.close));
const ema12 = window.TechnicalIndicators.calculateEMA(prices, 12);
const ema26 = window.TechnicalIndicators.calculateEMA(prices, 26);
const labels = emaData.map(candle => {
if (typeof candle.time === 'string') {
return candle.time.split(',')[0];
}
return candle.time;
});
new Chart(ctx, {
type: 'line',
data: {
labels: labels,
datasets: [
{
label: 'سعر الإغلاق',
data: prices,
borderColor: '#0088cc',
backgroundColor: 'rgba(0, 136, 204, 0.1)',
borderWidth: 2,
pointRadius: 0,
fill: false
},
{
label: 'EMA (12)',
data: Array(12 - 1).fill(null).concat(ema12),
borderColor: '#e74c3c',
backgroundColor: 'transparent',
borderWidth: 2,
pointRadius: 0,
fill: false
},
{
label: 'EMA (26)',
data: Array(26 - 1).fill(null).concat(ema26),
borderColor: '#2ecc71',
backgroundColor: 'transparent',
borderWidth: 2,
pointRadius: 0,
fill: false
}
]
},
options: {
responsive: true,
maintainAspectRatio: false,
scales: {
x: { display: false },
y: { grid: { color: 'rgba(0, 0, 0, 0.05)' } }
},
plugins: {
legend: { display: true, position: 'top' },
tooltip: { mode: 'index', intersect: false }
}
}
});
}
// رسم مؤشر بولينجر باندز
if (document.getElementById('bbChart')) {
window.TechnicalIndicators.renderBollingerBandsChart('bbChart', window.candleData, 20, 2);
}
// رسم مؤشر RSI
if (document.getElementById('rsiChart')) {
const rsiValues = window.TechnicalIndicators.calculateRSI(window.candleData, 14);
const lastRSI = rsiValues[rsiValues.length - 1].toFixed(2);
// تحديث قيمة RSI على الواجهة
const rsiValueElement = document.getElementById('rsiValue');
if (rsiValueElement) {
const rsiColor = lastRSI > 70 ? '#f23645' : (lastRSI < 30 ? '#22ab94' : '#666');
rsiValueElement.textContent = lastRSI;
rsiValueElement.style.color = rsiColor;
}
window.TechnicalIndicators.renderRSIChart('rsiChart', window.candleData, 14);
}
// رسم مؤشر MACD
if (document.getElementById('macdChart')) {
const macdData = window.TechnicalIndicators.calculateMACD(window.candleData);
const lastMacdValue = macdData.macdLine[macdData.macdLine.length - 1].toFixed(2);
const lastSignalValue = macdData.signalLine[macdData.signalLine.length - 1].toFixed(2);
// تحديث قيم MACD على الواجهة
const macdValueElement = document.getElementById('macdValue');
if (macdValueElement) {
const macdColor = lastMacdValue > lastSignalValue ? '#22ab94' : '#f23645';
macdValueElement.textContent = `${lastMacdValue} (إشارة: ${lastSignalValue})`;
macdValueElement.style.color = macdColor;
}
window.TechnicalIndicators.renderMACDChart('macdChart', window.candleData, 12, 26, 9);
}
// رسم مؤشر الحجم
if (document.getElementById('volumeChart')) {
// هنا يمكن إضافة رسم بياني للحجم إذا كان متاحًا
// تنفيذ بسيط للحجم استنادًا إلى البيانات المتاحة
const ctx = document.getElementById('volumeChart').getContext('2d');
const existingChart = Chart.getChart(ctx.canvas);
if (existingChart) {
existingChart.destroy();
}
const volumes = window.candleData.map(candle => parseFloat(candle.volume || 0));
const labels = window.candleData.map(candle => {
if (typeof candle.time === 'string') {
return candle.time.split(',')[0];
}
return candle.time;
});
new Chart(ctx, {
type: 'bar',
data: {
labels: labels,
datasets: [{
label: 'الحجم',
data: volumes,
backgroundColor: function(context) {
const index = context.dataIndex;
if (index > 0 &&
window.candleData[index] &&
window.candleData[index-1] &&
window.candleData[index].close > window.candleData[index-1].close) {
return 'rgba(75, 192, 192, 0.6)';
}
return 'rgba(255, 99, 132, 0.6)';
}
}]
},
options: {
responsive: true,
maintainAspectRatio: false,
plugins: {
legend: { display: true, position: 'top' }
},
scales: {
x: { display: false },
y: {
grid: { color: 'rgba(0, 0, 0, 0.05)' },
ticks: { callback: function(value) { return value >= 1000000 ? value/1000000 + 'M' : value >= 1000 ? value/1000 + 'K' : value; } }
}
}
}
});
}
// رسم مؤشر ستوكاستيك
if (document.getElementById('stochasticChart') && window.AdditionalIndicators) {
window.AdditionalIndicators.renderStochasticChart('stochasticChart', window.candleData, 14, 3);
}
// رسم مؤشر OBV
if (document.getElementById('obvChart') && window.AdditionalIndicators) {
window.AdditionalIndicators.renderOBVChart('obvChart', window.candleData);
}
// رسم مؤشر ADX
if (document.getElementById('adxChart') && window.AdditionalIndicators) {
window.AdditionalIndicators.renderADXChart('adxChart', window.candleData, 14);
}
}
}
// وظيفة لإعادة محاولة رسم الرسوم البيانية بعد فترة
function retryChartsAfterDelay() {
setTimeout(function() {
setupTechnicalCharts();
console.log('تمت إعادة محاولة رسم الرسوم البيانية بعد فترة...');
}, 2000);
}
// إضافة مستمع لحدث DOMContentLoaded
document.addEventListener('DOMContentLoaded', function() {
console.log('إضافة مستمع لحدث تحميل مكتبات الرسوم البيانية');
// إعداد تبديل علامات التبويب للمؤشرات الفنية
const tabs = document.querySelectorAll('.indicator-tab');
const contents = document.querySelectorAll('.indicator-content');
tabs.forEach(tab => {
tab.addEventListener('click', function() {
// إزالة الفئة "active" من جميع علامات التبويب
tabs.forEach(t => t.classList.remove('active'));
// إخفاء جميع محتويات علامات التبويب
contents.forEach(c => c.style.display = 'none');
// إضافة الفئة "active" للعلامة التي تم النقر عليها
this.classList.add('active');
// عرض المحتوى المرتبط بعلامة التبويب التي تم النقر عليها
const targetId = this.getAttribute('data-target');
document.getElementById(targetId).style.display = 'block';
// تحديث أنماط CSS لعلامة التبويب النشطة
tabs.forEach(t => {
if (t.classList.contains('active')) {
t.style.color = '#0088cc';
t.style.borderBottom = '2px solid #0088cc';
} else {
t.style.color = '#666';
t.style.borderBottom = 'none';
}
});
// تحقق من الرسوم البيانية وإعادة المحاولة إذا لزم الأمر
retryChartsAfterDelay();
});
});
// انتظر مكتبة Chart.js ثم قم برسم المخططات
let checkCounter = 0;
const checkInterval = setInterval(function() {
checkCounter++;
if (typeof Chart !== 'undefined' && window.candleData) {
clearInterval(checkInterval);
console.log('تم تحميل مكتبة Chart.js بنجاح! بدء رسم المخططات...');
setupTechnicalCharts();
} else if (checkCounter >= 20) { // 10 ثواني كحد أقصى
clearInterval(checkInterval);
console.error('تعذر تحميل مكتبة Chart.js في الوقت المحدد');
}
}, 500);
});