10, 'min_move' => 0.10, 'horizon' => 30, 'bounce_window' => 10, 'bounce_pct' => 0.03, 'break_pct' => 0.02, 'fib_levels' => [0.236, 0.382, 0.5, 0.618, 0.786], 'control_levels' => 5, 'seed' => 20261024, 'bootstrap' => 5000, ]; function loadCandles(string $file): array { if (!is_file($file)) { throw new RuntimeException("Veri yok: {$file}"); } $raw = json_decode((string)file_get_contents($file), true, 512, JSON_THROW_ON_ERROR); return $raw['candles'] ?? []; } function isPivotHigh(array $candles, int $i, int $k): bool { $h = $candles[$i]['h']; $strict = false; for ($j = max(0, $i - $k); $j <= min(count($candles) - 1, $i + $k); $j++) { if ($j === $i) continue; if ($candles[$j]['h'] > $h) return false; if ($candles[$j]['h'] < $h) $strict = true; } return $strict; } function isPivotLow(array $candles, int $i, int $k): bool { $l = $candles[$i]['l']; $strict = false; for ($j = max(0, $i - $k); $j <= min(count($candles) - 1, $i + $k); $j++) { if ($j === $i) continue; if ($candles[$j]['l'] < $l) return false; if ($candles[$j]['l'] > $l) $strict = true; } return $strict; } function findLegs(array $candles, int $k, float $minMove): array { $n = count($candles); $pivots = []; for ($i = $k; $i < $n - $k; $i++) { if (isPivotHigh($candles, $i, $k)) { $pivots[] = ['i' => $i, 'type' => 'high', 'price' => $candles[$i]['h']]; } elseif (isPivotLow($candles, $i, $k)) { $pivots[] = ['i' => $i, 'type' => 'low', 'price' => $candles[$i]['l']]; } } // Aynı türde ardışık pivotlarda daha uç olanı tut; karşıt türde yeterli hareket varsa ekle. $seq = []; foreach ($pivots as $p) { if (!$seq) { $seq[] = $p; continue; } $last = $seq[count($seq) - 1]; if ($p['type'] === $last['type']) { $moreExtreme = $p['type'] === 'high' ? $p['price'] > $last['price'] : $p['price'] < $last['price']; if ($moreExtreme) $seq[count($seq) - 1] = $p; } else { $amp = abs($p['price'] - $last['price']) / max(1e-9, $last['price']); if ($amp >= $minMove) $seq[] = $p; } } $legs = []; for ($i = 1; $i < count($seq); $i++) { $a = $seq[$i - 1]; $b = $seq[$i]; if ($b['i'] <= $a['i']) continue; $low = min($a['price'], $b['price']); $high = max($a['price'], $b['price']); $legs[] = [ 'id' => count($legs) + 1, 'direction' => $b['price'] > $a['price'] ? 'up' : 'down', 'start_index' => $a['i'], 'end_index' => $b['i'], 'confirm_index' => $b['i'] + $k, 'start_price' => $a['price'], 'end_price' => $b['price'], 'low' => $low, 'high' => $high, 'amplitude' => ($high - $low) / max(1e-9, $low), ]; } return $legs; } function levelPrice(string $direction, float $low, float $high, float $ratio): float { // Yukarı dalgada geri çekilme aşağı; aşağı dalgada yukarı. if ($direction === 'up') return $high - ($high - $low) * $ratio; return $low + ($high - $low) * $ratio; } function firstTouch(array $candles, int $from, int $to, string $direction, float $level): ?int { for ($j = $from; $j <= $to; $j++) { if ($direction === 'up') { if ($candles[$j]['l'] <= $level) return $j; } else { if ($candles[$j]['h'] >= $level) return $j; } } return null; } function bounceResult(array $candles, int $touch, string $direction, float $level, int $window, float $bouncePct, float $breakPct): array { $n = count($candles); $end = min($n - 1, $touch + $window); for ($j = $touch + 1; $j <= $end; $j++) { if ($direction === 'up') { $break = $level * (1 - $breakPct); $target = $level * (1 + $bouncePct); if ($candles[$j]['l'] <= $break) return ['resolved' => true, 'bounce' => false]; if ($candles[$j]['h'] >= $target) return ['resolved' => true, 'bounce' => true]; } else { $break = $level * (1 + $breakPct); $target = $level * (1 - $bouncePct); if ($candles[$j]['h'] >= $break) return ['resolved' => true, 'bounce' => false]; if ($candles[$j]['l'] <= $target) return ['resolved' => true, 'bounce' => true]; } } return ['resolved' => false, 'bounce' => false]; } function buildEvents(array $candles, array $legs, array $config): array { $events = []; foreach ($legs as $leg) { $n = count($candles); $from = $leg['confirm_index'] + 1; $to = min($n - 1, $from + $config['horizon'] - 1); if ($from >= $n - 1 || $to <= $from) continue; $eligible = true; $levels = []; foreach ($config['fib_levels'] as $ratio) { $levels[] = ['kind' => 'fib', 'ratio' => $ratio, 'price' => levelPrice($leg['direction'], $leg['low'], $leg['high'], $ratio)]; } mt_srand($config['seed'] + $leg['id'] * 7919); for ($c = 0; $c < $config['control_levels']; $c++) { $ratio = 0.05 + 0.90 * (mt_rand() / mt_getrandmax()); $levels[] = ['kind' => 'control', 'ratio' => $ratio, 'price' => levelPrice($leg['direction'], $leg['low'], $leg['high'], $ratio)]; } foreach ($levels as $level) { $touch = firstTouch($candles, $from, $to, $leg['direction'], $level['price']); $bounce = ['resolved' => false, 'bounce' => false]; if ($touch !== null) { $bounce = bounceResult($candles, $touch, $leg['direction'], $level['price'], $config['bounce_window'], $config['bounce_pct'], $config['break_pct']); } $events[] = [ 'leg_id' => $leg['id'], 'kind' => $level['kind'], 'ratio' => $level['ratio'], 'price' => $level['price'], 'touch' => $touch !== null, 'resolved' => $bounce['resolved'], 'bounce' => $bounce['bounce'], ]; } } return $events; } function summarize(array $events, array $legs): array { $by = []; foreach ($events as $e) { $key = $e['kind'] . ':' . ($e['kind'] === 'fib' ? number_format($e['ratio'], 3, '.', '') : 'control'); if (!isset($by[$key])) $by[$key] = ['touch' => 0, 'bounce' => 0, 'resolved' => 0, 'events' => 0]; $by[$key]['events']++; if ($e['touch']) $by[$key]['touch']++; if ($e['touch'] && $e['resolved']) { $by[$key]['resolved']++; if ($e['bounce']) $by[$key]['bounce']++; } } return $by; } function bootstrapDifference(array $events, array $legs, int $iterations, int $seed): array { // Leg bazında fib ve control sayıları $groups = []; foreach ($legs as $leg) $groups[$leg['id']] = ['fib' => [0, 0], 'control' => [0, 0]]; foreach ($events as $e) { if (!$e['touch'] || !$e['resolved']) continue; $groups[$e['leg_id']][$e['kind']][1]++; if ($e['bounce']) $groups[$e['leg_id']][$e['kind']][0]++; } $usable = []; foreach ($groups as $g) { if ($g['fib'][1] > 0 && $g['control'][1] > 0) $usable[] = $g; } if (!$usable) return ['diff' => 0.0, 'low' => 0.0, 'high' => 0.0, 'p' => 1.0, 'legs' => 0]; $diffObs = function (array $sample): float { $f = 0; $fn = 0; $c = 0; $cn = 0; foreach ($sample as $g) { $f += $g['fib'][0]; $fn += $g['fib'][1]; $c += $g['control'][0]; $cn += $g['control'][1]; } return ($fn ? $f / $fn : 0) - ($cn ? $c / $cn : 0); }; $observed = $diffObs($usable); mt_srand($seed); $diffs = []; $count = count($usable); for ($b = 0; $b < $iterations; $b++) { $sample = []; for ($i = 0; $i < $count; $i++) $sample[] = $usable[mt_rand(0, $count - 1)]; $diffs[] = $diffObs($sample); } sort($diffs); $low = $diffs[(int)floor(0.025 * $iterations)]; $high = $diffs[(int)floor(0.975 * $iterations)]; $le = 0; foreach ($diffs as $d) if ($d <= 0) $le++; $p = min(1.0, 2 * min($le / $iterations, 1 - $le / $iterations)); return ['diff' => $observed, 'low' => $low, 'high' => $high, 'p' => $p, 'legs' => $count]; } function pointSummary(string $symbol, array $config): array { global $dataDir; $raw = json_decode((string)file_get_contents($dataDir . '/' . strtolower($symbol) . '_1d.json'), true, 512, JSON_THROW_ON_ERROR); $candles = $raw['candles']; $legs = findLegs($candles, $config['pivot_window'], $config['min_move']); $events = buildEvents($candles, $legs, $config); $agg = ['fib' => ['touch' => 0, 'resolved' => 0, 'bounce' => 0, 'events' => 0], 'control' => ['touch' => 0, 'resolved' => 0, 'bounce' => 0, 'events' => 0]]; foreach ($events as $e) { $agg[$e['kind']]['events']++; if ($e['touch']) $agg[$e['kind']]['touch']++; if ($e['touch'] && $e['resolved']) { $agg[$e['kind']]['resolved']++; if ($e['bounce']) $agg[$e['kind']]['bounce']++; } } return [ 'legs' => count($legs), 'fib_bounce_rate' => $agg['fib']['resolved'] ? $agg['fib']['bounce'] / $agg['fib']['resolved'] : null, 'control_bounce_rate' => $agg['control']['resolved'] ? $agg['control']['bounce'] / $agg['control']['resolved'] : null, 'difference_pp' => 100 * (($agg['fib']['resolved'] ? $agg['fib']['bounce'] / $agg['fib']['resolved'] : 0) - ($agg['control']['resolved'] ? $agg['control']['bounce'] / $agg['control']['resolved'] : 0)), ]; } function runAnalysis(string $symbol, array $config): array { global $dataDir; $raw = json_decode((string)file_get_contents($dataDir . '/' . strtolower($symbol) . '_1d.json'), true, 512, JSON_THROW_ON_ERROR); $candles = $raw['candles']; $legs = findLegs($candles, $config['pivot_window'], $config['min_move']); $events = buildEvents($candles, $legs, $config); $summary = summarize($events, $legs); $fib = $summary['fib:0.236'] ?? null; // Aggregate $agg = ['fib' => ['touch' => 0, 'resolved' => 0, 'bounce' => 0, 'events' => 0], 'control' => ['touch' => 0, 'resolved' => 0, 'bounce' => 0, 'events' => 0]]; foreach ($events as $e) { $agg[$e['kind']]['events']++; if ($e['touch']) $agg[$e['kind']]['touch']++; if ($e['touch'] && $e['resolved']) { $agg[$e['kind']]['resolved']++; if ($e['bounce']) $agg[$e['kind']]['bounce']++; } } $boot = bootstrapDifference($events, $legs, $config['bootstrap'], $config['seed'] + 1); $byLevel = []; foreach ($config['fib_levels'] as $ratio) { $key = 'fib:' . number_format($ratio, 3, '.', ''); $s = $summary[$key] ?? ['touch' => 0, 'resolved' => 0, 'bounce' => 0, 'events' => 0]; $byLevel[] = [ 'ratio' => $ratio, 'label' => rtrim(rtrim(number_format($ratio * 100, 1, '.', ''), '0'), '.') . '%', 'events' => $s['events'], 'touches' => $s['touch'], 'resolved' => $s['resolved'], 'bounces' => $s['bounce'], 'touch_rate' => $s['events'] ? $s['touch'] / $s['events'] : null, 'bounce_rate' => $s['resolved'] ? $s['bounce'] / $s['resolved'] : null, ]; } $control = $summary['control:control'] ?? ['touch' => 0, 'resolved' => 0, 'bounce' => 0, 'events' => 0]; return [ 'symbol' => $symbol, 'start' => $raw['start'], 'end' => $raw['end'], 'candles' => $raw['count'], 'legs' => count($legs), 'eligible_legs' => count(array_filter($legs, fn($l) => $l['confirm_index'] + 1 < count($candles))), 'fib' => [ 'events' => $agg['fib']['events'], 'touches' => $agg['fib']['touch'], 'resolved' => $agg['fib']['resolved'], 'bounces' => $agg['fib']['bounce'], 'touch_rate' => $agg['fib']['events'] ? $agg['fib']['touch'] / $agg['fib']['events'] : null, 'bounce_rate' => $agg['fib']['resolved'] ? $agg['fib']['bounce'] / $agg['fib']['resolved'] : null, ], 'control' => [ 'events' => $control['events'], 'touches' => $control['touch'], 'resolved' => $control['resolved'], 'bounces' => $control['bounce'], 'touch_rate' => $control['events'] ? $control['touch'] / $control['events'] : null, 'bounce_rate' => $control['resolved'] ? $control['bounce'] / $control['resolved'] : null, ], 'difference_pp' => $boot['diff'] * 100, 'ci_low_pp' => $boot['low'] * 100, 'ci_high_pp' => $boot['high'] * 100, 'p_bootstrap' => $boot['p'], 'by_level' => $byLevel, 'sample_legs' => array_slice(array_map(function ($l) use ($candles) { return [ 'id' => $l['id'], 'direction' => $l['direction'], 'start_date' => gmdate('Y-m-d', (int)($candles[$l['start_index']]['t'] / 1000)), 'end_date' => gmdate('Y-m-d', (int)($candles[$l['end_index']]['t'] / 1000)), 'confirm_date' => gmdate('Y-m-d', (int)($candles[min(count($candles) - 1, $l['confirm_index'])]['t'] / 1000)), 'start_price' => $l['start_price'], 'end_price' => $l['end_price'], 'low' => $l['low'], 'high' => $l['high'], 'amplitude' => $l['amplitude'], 'start_index' => $l['start_index'], 'end_index' => $l['end_index'], 'confirm_index' => $l['confirm_index'], ]; }, $legs), 0, 200), ]; } $result = [ 'meta' => [ 'title' => 'Fibonacci Retracement Lab', 'method' => 'Fractal pivot + event study + random control levels + bootstrap', 'generated_at' => gmdate('c'), 'params' => $config, 'source' => 'Binance public daily klines (data-api.binance.vision)', 'disclaimer' => 'Educational research. Not investment advice. Past performance does not predict future results.', ], 'symbols' => [], ]; foreach (['BTCUSDT', 'ETHUSDT'] as $symbol) { $result['symbols'][$symbol] = runAnalysis($symbol, $config); } $variants = [ ['label' => 'Baseline', 'overrides' => []], ['label' => '7-day pivot', 'overrides' => ['pivot_window' => 7]], ['label' => '14-day pivot', 'overrides' => ['pivot_window' => 14]], ['label' => '8% min move', 'overrides' => ['min_move' => 0.08]], ['label' => '12% min move', 'overrides' => ['min_move' => 0.12]], ['label' => '20-day window', 'overrides' => ['horizon' => 20]], ['label' => '40-day window', 'overrides' => ['horizon' => 40]], ['label' => '5% bounce target', 'overrides' => ['bounce_pct' => 0.05]], ]; $result['sensitivity'] = []; foreach ($variants as $variant) { $cfg = array_replace($config, $variant['overrides']); $row = ['label' => $variant['label'], 'params' => $variant['overrides'], 'symbols' => []]; foreach (['BTCUSDT', 'ETHUSDT'] as $symbol) { $row['symbols'][$symbol] = pointSummary($symbol, $cfg); } $result['sensitivity'][] = $row; } file_put_contents($dataDir . '/analysis.json', json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)); echo "analysis.json yazıldı\n"; foreach ($result['symbols'] as $symbol => $r) { echo sprintf( "%s: %d legs, fib touch %.1f%%, bounce %.1f%% | control touch %.1f%%, bounce %.1f%% | diff %.1f pp [%.1f, %.1f] p=%.3f\n", $symbol, $r['legs'], 100 * ($r['fib']['touch_rate'] ?? 0), 100 * ($r['fib']['bounce_rate'] ?? 0), 100 * ($r['control']['touch_rate'] ?? 0), 100 * ($r['control']['bounce_rate'] ?? 0), $r['difference_pp'], $r['ci_low_pp'], $r['ci_high_pp'], $r['p_bootstrap'] ); }