File size: 7,385 Bytes
1763642
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
document.addEventListener('DOMContentLoaded', function() {
    // Price Input Handling
    const priceInput = document.getElementById('current-price');
    const useLivePriceBtn = document.getElementById('use-live-price');
    const updatePriceBtn = document.getElementById('update-price');
    
    // Position Calculator Elements
    const capitalInput = document.getElementById('capital');
    const riskPercentageInput = document.getElementById('risk-percentage');
    const entryPriceInput = document.getElementById('entry-price');
    const stopLossInput = document.getElementById('stop-loss');
    const calculatePositionBtn = document.getElementById('calculate-position');
    const positionResult = document.getElementById('position-result');
    const positionSizeEl = document.getElementById('position-size');
    const coinAmountEl = document.getElementById('coin-amount');
    
    // Simulate fetching live price (in a real app, this would be from an API)
    function fetchLivePrice() {
        showLoading();
        setTimeout(() => {
            // Simulate a live price (between 0.8 and 1.2)
            const livePrice = (0.8 + Math.random() * 0.4).toFixed(4);
            priceInput.value = livePrice;
            updateAnalysis(livePrice);
            hideLoading();
        }, 1000);
    }
    
    // Update analysis based on price
    function updateAnalysis(price) {
        // Update recommendation
        const recommendationEl = document.getElementById('recommendation');
        const recommendationText = getRandomRecommendation();
        
        recommendationEl.innerHTML = `
            <h2 class="text-xl font-semibold mb-4 text-gray-800">التوصية</h2>
            <div class="flex items-center justify-center py-8">
                <div class="text-center">
                    <div class="text-4xl font-bold mb-2 ${recommendationText.color}">${recommendationText.action}</div>
                    <p class="text-gray-600">${recommendationText.reason}</p>
                </div>
            </div>
        `;
        
        // Update indicators
        updateIndicators(price);
        
        // Update position calculator entry price if empty
        if (!entryPriceInput.value) {
            entryPriceInput.value = price;
        }
    }
    
    // Generate random recommendation for demo
    function getRandomRecommendation() {
        const actions = [
            { action: 'شراء', color: 'text-green-500', reason: 'المؤشرات إيجابية مع زخم صاعد' },
            { action: 'بيع', color: 'text-red-500', reason: 'المؤشرات سلبية مع زخم هابط' },
            { action: 'انتظر', color: 'text-yellow-500', reason: 'السوق في نطاق جانبي يحتاج إلى تأكيد' }
        ];
        return actions[Math.floor(Math.random() * actions.length)];
    }
    
    // Update indicators for demo
    function updateIndicators(price) {
        const indicatorsContainer = document.getElementById('indicators').querySelector('.grid');
        indicatorsContainer.innerHTML = '';
        
        const indicators = [
            { name: 'SMA (7 أيام)', value: (price * (0.95 + Math.random() * 0.1)).toFixed(4), trend: getRandomTrend() },
            { name: 'RSI (14)', value: (30 + Math.random() * 40).toFixed(2), trend: getRandomTrend() },
            { name: 'MACD', value: getRandomMACD(), trend: getRandomTrend() },
            { name: 'حجم التداول (24h)', value: `${(Math.random() * 1000).toFixed(0)}K`, trend: getRandomTrend() },
            { name: 'نسبة الشراء/البيع', value: `${(0.5 + Math.random()).toFixed(2)}`, trend: getRandomTrend() },
            { name: 'نقاط الدعم/المقاومة', value: '3/2', trend: 'neutral' }
        ];
        
        indicators.forEach(indicator => {
            const trendIcon = indicator.trend === 'up' ? 'trending-up' : 
                            indicator.trend === 'down' ? 'trending-down' : 'minus';
            const trendColor = indicator.trend === 'up' ? 'text-green-500' : 
                             indicator.trend === 'down' ? 'text-red-500' : 'text-gray-500';
            
            const card = document.createElement('div');
            card.className = 'indicator-card bg-gray-50 p-4 rounded-lg';
            card.innerHTML = `
                <div class="flex justify-between items-center">
                    <span class="font-medium text-gray-700">${indicator.name}</span>
                    <i data-feather="${trendIcon}" class="w-5 h-5 ${trendColor}"></i>
                </div>
                <div class="text-2xl font-bold mt-2">${indicator.value}</div>
            `;
            indicatorsContainer.appendChild(card);
        });
        
        feather.replace();
    }
    
    // Get random trend for demo
    function getRandomTrend() {
        const trends = ['up', 'down', 'neutral'];
        return trends[Math.floor(Math.random() * trends.length)];
    }
    
    // Get random MACD value for demo
    function getRandomMACD() {
        const value = (Math.random() * 0.02 - 0.01).toFixed(4);
        return parseFloat(value) > 0 ? `+${value}` : value;
    }
    
    // Calculate position size
    function calculatePosition() {
        const capital = parseFloat(capitalInput.value);
        const riskPercentage = parseFloat(riskPercentageInput.value) / 100;
        const entryPrice = parseFloat(entryPriceInput.value);
        const stopLoss = parseFloat(stopLossInput.value);
        
        if (!capital || !entryPrice || !stopLoss) {
            alert('الرجاء إدخال جميع القيم المطلوبة');
            return;
        }
        
        const riskAmount = capital * riskPercentage;
        const riskPerUnit = entryPrice - stopLoss;
        const positionSize = riskAmount / riskPerUnit;
        const coinAmount = positionSize / entryPrice;
        
        positionSizeEl.textContent = positionSize.toFixed(2) + ' USDT';
        coinAmountEl.textContent = coinAmount.toFixed(2) + ' CHESS';
        positionResult.classList.remove('hidden');
    }
    
    // Show loading overlay
    function showLoading() {
        document.body.insertAdjacentHTML('beforeend', `
            <div id="loading-overlay">
                <div class="text-center space-y-4">
                    <div class="loader w-12 h-12 border-4 border-primary-500 border-t-transparent rounded-full"></div>
                    <p class="text-lg font-medium text-gray-700">جارٍ تحميل البيانات من CoinGecko...</p>
                </div>
            </div>
        `);
    }
    
    // Hide loading overlay
    function hideLoading() {
        const overlay = document.getElementById('loading-overlay');
        if (overlay) overlay.remove();
    }
    
    // Event Listeners
    useLivePriceBtn.addEventListener('click', fetchLivePrice);
    updatePriceBtn.addEventListener('click', () => {
        if (priceInput.value) {
            showLoading();
            setTimeout(() => {
                updateAnalysis(priceInput.value);
                hideLoading();
            }, 500);
        } else {
            alert('الرجاء إدخال سعر');
        }
    });
    
    calculatePositionBtn.addEventListener('click', calculatePosition);
    
    // Initialize with a random price
    fetchLivePrice();
});