35761-vm/api/alerts.php
Flatlogic Bot 4bb72bebc1 V1.16
2025-11-15 11:51:00 +00:00

77 lines
2.5 KiB
PHP

<?php
header('Content-Type: application/json');
require_once __DIR__ . '/../db/config.php';
require_once __DIR__ . '/analysis.php';
// Example: /api/alerts.php?symbol=BTCUSDT
$symbol = $_GET['symbol'] ?? 'BTCUSDT';
function check_bearish_alerts($symbol) {
$alerts = [];
// Fetch latest price
$pdo = db();
$stmt = $pdo->prepare("SELECT close_price FROM candlestick_data WHERE symbol = :symbol ORDER BY open_time DESC LIMIT 1");
$stmt->bindParam(':symbol', $symbol, PDO::PARAM_STR);
$stmt->execute();
$current_price = $stmt->fetchColumn();
if (!$current_price) {
return [];
}
// Fetch analysis data
$sma = calculate_sma($symbol, 20);
$rsi = calculate_rsi($symbol, 14);
$macd_data = calculate_macd($symbol);
$patterns = calculate_patterns($symbol);
// Condition 1: Price vs. SMA
$price_below_sma = $current_price < $sma;
// Condition 2: RSI indicates weakening momentum (e.g., dropping from overbought)
// For simplicity, we'll check if RSI is above a certain threshold and not rising.
// A more complex implementation would track RSI changes over time.
$rsi_weakening = $rsi > 50; // Simplified: RSI is in the upper range, potential for reversal
// Condition 3: MACD Bearish Crossover
$macd_bearish_crossover = false;
if ($macd_data && $macd_data['macd'] < $macd_data['signal']) {
// Check if it just crossed
// This requires historical data, for now, we check the current state
$macd_bearish_crossover = true; // Simplified: MACD is below signal line
}
// Check original combo
if ($price_below_sma && $rsi_weakening && $macd_bearish_crossover) {
$alerts[] = [
'type' => 'Strong Bearish Signal',
'indicator' => 'SMA, RSI, MACD Combination',
'message' => 'Price dropped below 20-period SMA, RSI is high, and MACD shows a bearish state.'
];
}
// Check for candlestick patterns
if (!empty($patterns)) {
foreach ($patterns as $pattern_name => $detected) {
if ($detected) {
$alerts[] = [
'type' => 'Strong Bearish Signal',
'indicator' => 'Candlestick Pattern',
'message' => 'Detected bearish pattern: ' . ucwords(str_replace('_', ' ', $pattern_name))
];
}
}
}
return $alerts;
}
$alerts = check_bearish_alerts($symbol);
echo json_encode([
'symbol' => $symbol,
'alerts' => $alerts,
'timestamp' => time()
]);