0) ? ($dayChange / $previousClose) * 100 : 0; // Get historical data $timestamps = $result['timestamp'] ?? []; $quotes = $result['indicators']['quote'][0] ?? []; $closes = $quotes['close'] ?? []; // Find purchase date in historical data $closestIndex = -1; $closestDiff = PHP_INT_MAX; foreach ($timestamps as $i => $ts) { if (isset($closes[$i]) && $closes[$i] !== null && $closes[$i] > 0) { $dateStr = date('M d', $ts); $historicalDates[] = $dateStr; $historicalPrices[] = $closes[$i]; // Find closest timestamp to purchase date $diff = abs($ts - $savedAt); if ($diff < $closestDiff) { $closestDiff = $diff; $closestIndex = count($historicalPrices) - 1; } } } // Mark the purchase point if ($closestIndex >= 0 && $closestIndex < count($historicalPrices)) { $purchasePointIndex = $closestIndex; } } // If price not found via API, try scraping as fallback if ($currentPrice == 0) { $altUrl = "https://finance.yahoo.com/quote/{$symbol}"; $html = fetchWithCurl($altUrl); if ($html && preg_match('/"regularMarketPrice":\{\"raw\":([0-9.]+)/', $html, $matches)) { $currentPrice = floatval($matches[1]); } elseif ($html && preg_match('/]*data-value="([0-9.]+)"[^>]*>/', $html, $matches)) { $currentPrice = floatval($matches[1]); } } // If still no price, use last known from historical or default if ($currentPrice == 0 && !empty($historicalPrices)) { $currentPrice = end($historicalPrices); } // Calculate gain/loss $gainLoss = $currentPrice - $purchasePrice; $gainLossPercent = ($purchasePrice > 0) ? ($gainLoss / $purchasePrice) * 100 : 0; // ============================================================ // STEP 3: Apply SELL/HOLD logic from video // ============================================================ $rating = 'HOLD'; $reasons = []; $score = 5; // Rule 1: Massive loss (>30%) + no recovery signs if ($gainLossPercent < -30 && $holdingDays > 90) { $rating = 'SELL'; $reasons[] = "πŸ”΄ Down " . number_format($gainLossPercent, 1) . "% over {$holdingDays} days – significant capital erosion"; $score -= 3; } // Rule 2: Product deterioration signal (from video - Cyberpunk example) elseif ($gainLossPercent < -15 && $holdingDays > 180) { $reasons[] = "⚠️ Down " . number_format($gainLossPercent, 1) . "% over {$holdingDays} days – investigate product quality (like Cyberpunk pre-crash)"; $score -= 1.5; if ($score <= 3.5) $rating = 'SELL'; } // Rule 3: Taking profits too early (DON'T cut the apple tree!) elseif ($gainLossPercent > 20 && $holdingDays < 365) { $reasons[] = "🍎 Up " . number_format($gainLossPercent, 1) . "% in only {$holdingDays} days – DON'T cut the tree! Let it mature (The Witcher 3)"; $score += 1; if ($score >= 6) $rating = 'BUY'; } // Rule 4: Long-term underperformance (opportunity cost) elseif ($holdingDays > 730 && $gainLossPercent < 10) { $reasons[] = "⚠️ Held {$holdingYears} years with only " . number_format($gainLossPercent, 1) . "% return – consider swapping (Adobeβ†’Uber example)"; $score -= 1; if ($score <= 4) $rating = 'SELL'; } // Rule 5: Healthy gain with long hold - let it ride elseif ($gainLossPercent > 30 && $holdingDays > 365) { $reasons[] = "βœ… Up " . number_format($gainLossPercent, 1) . "% over {$holdingDays} days – tree is bearing excellent fruit!"; $score += 0.5; } // Rule 6: Small loss, short hold - be patient elseif ($gainLossPercent > -10 && $holdingDays < 180) { $reasons[] = "🟑 Down only " . number_format(abs($gainLossPercent), 1) . "% – too early to judge, give the tree time"; $score += 0.5; } // Rule 7: Default - monitor else { $reasons[] = "πŸ“Š Monitoring mode – check product quality and sales growth quarterly"; } // Add video wisdom based on rating if ($rating === 'SELL') { $reasons[] = "πŸ’‘ The apple tree is showing signs of decline – sell on business deterioration, not price targets"; } elseif ($rating === 'BUY') { $reasons[] = "πŸ’‘ This tree is still bearing excellent fruit – don't cap your harvest at 100 apples"; } else { $reasons[] = "🟑 Watch for: product quality, slowing sales, or better opportunities elsewhere"; } // ============================================================ // STEP 4: Output based on mode // ============================================================ // COMPACT MODE - Small widget if ($isCompact) { $pnlText = ($gainLossPercent >= 0 ? '+' : '') . number_format($gainLossPercent, 1) . '%'; $pnlColor = $gainLossPercent >= 0 ? '#10b981' : '#ef4444'; $ratingText = $rating === 'SELL' ? 'SELL' : ($rating === 'BUY' ? 'HOLD' : 'HOLD'); $ratingColor = $rating === 'SELL' ? '#ef4444' : ($rating === 'BUY' ? '#10b981' : '#f59e0b'); header('Content-Type: text/html; charset=utf-8'); ?> πŸ“… β†— <?php echo htmlspecialchars($symbol); ?> – Should You Sell?

πŸ“… Held days

| Bought:
= 0 ? 'β–²' : 'β–Ό'; ?> % today
πŸ’° Bought at: | Total P&L: (= 0 ? '+' : ''; ?>%)
Holding Period
days
Annualized Return
0 ? number_format(($gainLossPercent / $holdingDays) * 365, 1) : '0'; ?>%
Current Price
Purchase Price
πŸ“ Red line = Your purchase price | πŸ”΄ Red dot = Purchase date

πŸ“Š The Verdict – Apple Tree Test

🍎 The Apple Tree Principle

⚠️ This tree is showing signs of decline – like CD Projekt's Cyberpunk (80% crash).
Sell when the BUSINESS deteriorates, not when you hit a price target. βœ… This tree is still bearing excellent fruit – like The Witcher 3 after release.
Don't cut it down just because you hit "100 apples." Let it compound. 🟑 Monitor this tree carefully – watch for product quality or a better opportunity (like swapping Adobe for Uber).
"Never cut down a good tree just because you hit your target."