borsa / nextjs-app /src /lib /technical /trend-analysis.ts
veteroner's picture
feat: live position monitoring with charts + trading system production ready
656ac31
import type { Candle, TrendAnalysis } from './types'
export function analyzeTrend(candles: Candle[], sma20: (number | null)[], sma50: (number | null)[], sma200: (number | null)[]): TrendAnalysis {
const last = candles[candles.length - 1]
const s20 = sma20[sma20.length - 1]
const s50 = sma50[sma50.length - 1]
const s200 = sma200[sma200.length - 1]
function trend(current: number, ma: number | null): 'Yükseliş' | 'Düşüş' | 'Yatay' {
if (ma === null) return 'Yatay'
const diff = (current - ma) / ma
if (diff > 0.02) return 'Yükseliş'
if (diff < -0.02) return 'Düşüş'
return 'Yatay'
}
const st = trend(last.close, s20)
const mt = trend(last.close, s50)
const lt = trend(last.close, s200)
let strength = 50
if (s20 && last.close > s20) strength += 10; else if (s20) strength -= 10
if (s50 && last.close > s50) strength += 15; else if (s50) strength -= 15
if (s200 && last.close > s200) strength += 20; else if (s200) strength -= 20
if (s20 && s50 && s20 > s50) strength += 5; else if (s20 && s50) strength -= 5
strength = Math.max(0, Math.min(100, strength))
const descriptions: string[] = []
if (lt === 'Yükseliş') descriptions.push('Uzun vadeli yükseliş trendinde (SMA200 üstünde)')
else if (lt === 'Düşüş') descriptions.push('Uzun vadeli düşüş trendinde (SMA200 altında)')
if (s20 && s50 && s20 > s50) descriptions.push('Golden Cross: SMA20 > SMA50 (yükseliş)')
else if (s20 && s50 && s20 < s50) descriptions.push('Death Cross: SMA20 < SMA50 (düşüş)')
if (st === 'Yükseliş' && mt === 'Yükseliş') descriptions.push('Kısa ve orta vadede güçlü yükseliş')
if (st === 'Düşüş' && mt === 'Düşüş') descriptions.push('Kısa ve orta vadede güçlü düşüş')
if (st !== mt) descriptions.push('Kısa ve orta vade uyumsuz - dikkatli olun')
return {
shortTerm: st,
mediumTerm: mt,
longTerm: lt,
strength,
description: descriptions.join('. ') || 'Trend nötr.',
}
}