This commit is contained in:
Flatlogic Bot 2025-11-15 11:51:00 +00:00
parent a116dc2b1f
commit 4bb72bebc1
2 changed files with 21 additions and 21 deletions

View File

@ -12,7 +12,7 @@ function check_bearish_alerts($symbol) {
// Fetch latest price
$pdo = db();
$stmt = $pdo->prepare("SELECT close FROM candlestick_data WHERE symbol = :symbol ORDER BY timestamp DESC LIMIT 1");
$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();

View File

@ -16,7 +16,7 @@ if ($period <= 0) {
function calculate_sma($symbol, $period) {
$pdo = db();
$stmt = $pdo->prepare("SELECT close FROM candlestick_data WHERE symbol = :symbol ORDER BY timestamp DESC LIMIT :period");
$stmt = $pdo->prepare("SELECT close_price FROM candlestick_data WHERE symbol = :symbol ORDER BY open_time DESC LIMIT :period");
$stmt->bindParam(':symbol', $symbol, PDO::PARAM_STR);
$stmt->bindParam(':period', $period, PDO::PARAM_INT);
$stmt->execute();
@ -30,7 +30,7 @@ function calculate_sma($symbol, $period) {
function calculate_rsi($symbol, $period = 14) {
$pdo = db();
$limit = $period + 1;
$stmt = $pdo->prepare("SELECT close FROM candlestick_data WHERE symbol = :symbol ORDER BY timestamp DESC LIMIT :limit");
$stmt = $pdo->prepare("SELECT close_price FROM candlestick_data WHERE symbol = :symbol ORDER BY open_time DESC LIMIT :limit");
$stmt->bindParam(':symbol', $symbol, PDO::PARAM_STR);
$stmt->bindParam(':limit', $limit, PDO::PARAM_INT);
$stmt->execute();
@ -80,7 +80,7 @@ function _calculate_ema_series($prices, $period) {
function calculate_macd($symbol, $fast_period = 12, $slow_period = 26, $signal_period = 9) {
$pdo = db();
$limit = $slow_period + $signal_period + 50; // Fetch extra data for stability
$stmt = $pdo->prepare("SELECT close FROM candlestick_data WHERE symbol = :symbol ORDER BY timestamp ASC LIMIT :limit");
$stmt = $pdo->prepare("SELECT close_price FROM candlestick_data WHERE symbol = :symbol ORDER BY open_time ASC LIMIT :limit");
$stmt->bindParam(':symbol', $symbol, PDO::PARAM_STR);
$stmt->bindParam(':limit', $limit, PDO::PARAM_INT);
$stmt->execute();
@ -120,7 +120,7 @@ function calculate_macd($symbol, $fast_period = 12, $slow_period = 26, $signal_p
function calculate_patterns($symbol) {
$pdo = db();
// Fetch last 30 candles for pattern recognition
$stmt = $pdo->prepare("SELECT open, high, low, close, timestamp FROM candlestick_data WHERE symbol = :symbol ORDER BY timestamp DESC LIMIT 30");
$stmt = $pdo->prepare("SELECT open_price, high_price, low_price, close_price, open_time FROM candlestick_data WHERE symbol = :symbol ORDER BY open_time DESC LIMIT 30");
$stmt->bindParam(':symbol', $symbol, PDO::PARAM_STR);
$stmt->execute();
$candles = $stmt->fetchAll(PDO::FETCH_ASSOC);
@ -134,22 +134,22 @@ function calculate_patterns($symbol) {
$prev_candle = $candles[1];
// 1. Shooting Star
$body_size = abs($latest_candle['open'] - $latest_candle['close']);
$upper_wick = $latest_candle['high'] - max($latest_candle['open'], $latest_candle['close']);
$lower_wick = min($latest_candle['open'], $latest_candle['close']) - $latest_candle['low'];
$body_size = abs($latest_candle['open_price'] - $latest_candle['close_price']);
$upper_wick = $latest_candle['high_price'] - max($latest_candle['open_price'], $latest_candle['close_price']);
$lower_wick = min($latest_candle['open_price'], $latest_candle['close_price']) - $latest_candle['low_price'];
if ($upper_wick > $body_size * 2 && $lower_wick < $body_size) {
$patterns['shooting_star'] = true;
}
// 2. Bearish Engulfing
if ($prev_candle['close'] > $prev_candle['open'] && // Previous candle is bullish
$latest_candle['open'] > $prev_candle['close'] && // Current candle opens above previous close
$latest_candle['close'] < $prev_candle['open']) { // Current candle closes below previous open
if ($prev_candle['close_price'] > $prev_candle['open_price'] && // Previous candle is bullish
$latest_candle['open_price'] > $prev_candle['close_price'] && // Current candle opens above previous close
$latest_candle['close_price'] < $prev_candle['open_price']) { // Current candle closes below previous open
$patterns['bearish_engulfing'] = true;
}
// 3. Gravestone Doji
$is_doji = $body_size <= ($latest_candle['high'] - $latest_candle['low']) * 0.1;
$is_doji = $body_size <= ($latest_candle['high_price'] - $latest_candle['low_price']) * 0.1;
if ($is_doji && $lower_wick < $body_size && $upper_wick > $body_size * 3) {
$patterns['gravestone_doji'] = true;
}
@ -159,9 +159,9 @@ function calculate_patterns($symbol) {
$c1 = $candles[2]; // Large bullish candle
$c2 = $candles[1]; // Small body candle (star)
$c3 = $candles[0]; // Bearish candle
if (($c1['close'] > $c1['open']) && (abs($c1['close'] - $c1['open']) > ($c1['high'] - $c1['low']) * 0.7) && // c1 is strong bullish
($c2['open'] > $c1['close']) && (abs($c2['close'] - $c2['open']) < ($c2['high'] - $c2['low']) * 0.3) && // c2 is small body, gapped up
($c3['close'] < $c3['open']) && ($c3['open'] < $c2['open']) && ($c3['close'] < $c1['close'])) { // c3 is bearish, closes below midpoint of c1
if (($c1['close_price'] > $c1['open_price']) && (abs($c1['close_price'] - $c1['open_price']) > ($c1['high_price'] - $c1['low_price']) * 0.7) && // c1 is strong bullish
($c2['open_price'] > $c1['close_price']) && (abs($c2['close_price'] - $c2['open_price']) < ($c2['high_price'] - $c2['low_price']) * 0.3) && // c2 is small body, gapped up
($c3['close_price'] < $c3['open_price']) && ($c3['open_price'] < $c2['open_price']) && ($c3['close_price'] < $c1['close_price'])) { // c3 is bearish, closes below midpoint of c1
$patterns['evening_star'] = true;
}
}
@ -170,13 +170,13 @@ function calculate_patterns($symbol) {
// This is a more complex pattern requiring finding two distinct peaks.
// We'll look for two highs that are close in price, separated by a valley.
if(count($candles) >= 15) {
$recent_highs = array_map(function($c) { return $c['high']; }, array_slice($candles, 0, 15));
$recent_highs = array_map(function($c) { return $c['high_price']; }, array_slice($candles, 0, 15));
$max_high = max($recent_highs);
$peaks = [];
foreach($candles as $i => $c) {
if ($i > 1 && $i < count($candles) -1 && $c['high'] >= $max_high * 0.98) {
if($candles[$i-1]['high'] < $c['high'] && $candles[$i+1]['high'] < $c['high']) {
$peaks[] = ['index' => $i, 'price' => $c['high']];
if ($i > 1 && $i < count($candles) -1 && $c['high_price'] >= $max_high * 0.98) {
if($candles[$i-1]['high_price'] < $c['high_price'] && $candles[$i+1]['high_price'] < $c['high_price']) {
$peaks[] = ['index' => $i, 'price' => $c['high_price']];
}
}
}
@ -193,10 +193,10 @@ function calculate_patterns($symbol) {
if ($price_diff < 0.02 && $time_diff > 5) {
// Find the low point (valley) between the peaks
$valley_slice = array_slice($candles, $peak2['index'] + 1, $time_diff -1);
$valley_low = min(array_map(function($c){ return $c['low']; }, $valley_slice));
$valley_low = min(array_map(function($c){ return $c['low_price']; }, $valley_slice));
// Check if current price has broken below the valley low (neckline)
if($latest_candle['close'] < $valley_low) {
if($latest_candle['close_price'] < $valley_low) {
$patterns['double_top'] = true;
}
}