Spaces:
Running
Running
File size: 17,341 Bytes
b72deed e610a2f e6131ae 0cafa73 b72deed e6131ae facb33b e610a2f b72deed 20fef51 e610a2f b72deed 20fef51 b72deed facb33b b72deed e6131ae 41719ef e610a2f 0cafa73 41719ef b72deed e610a2f 41719ef e610a2f b72deed facb33b b72deed 41719ef b72deed e610a2f b72deed facb33b e6131ae b72deed facb33b b72deed facb33b b72deed facb33b b72deed 006e994 e610a2f 20fef51 b72deed 20fef51 e610a2f 0cafa73 b72deed 0cafa73 b72deed 0cafa73 b72deed | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 | import React from 'react'
import { TrendingUp, TrendingDown, Minus, AlertTriangle, Building2, Newspaper, Loader2, RefreshCw, ShieldCheck } from 'lucide-react'
import { Prediction, QuoteData } from '../api/stockApi'
import { getPredictionReliabilitySummary } from '../utils/predictionReliability'
interface Props {
prediction: Prediction
stockName: string
quote?: QuoteData | null
currency?: string
onRecalculate?: () => void
recalculating?: boolean
}
const signalConfig = {
BUY: {
label: 'BUY',
labelZh: '買入',
bg: 'bg-green-500/10',
border: 'border-green-500',
text: 'text-green-400',
badgeBg: 'bg-green-600',
icon: TrendingUp,
},
SELL: {
label: 'SELL',
labelZh: '賣出',
bg: 'bg-red-500/10',
border: 'border-red-500',
text: 'text-red-400',
badgeBg: 'bg-red-600',
icon: TrendingDown,
},
HOLD: {
label: 'HOLD',
labelZh: '持觀望',
bg: 'bg-yellow-500/10',
border: 'border-yellow-500',
text: 'text-yellow-400',
badgeBg: 'bg-yellow-600',
icon: Minus,
},
}
function formatShares(value?: number): string {
const v = Number(value ?? 0)
const abs = Math.abs(v)
const sign = v > 0 ? '+' : v < 0 ? '-' : ''
if (abs >= 1_000_000) return `${sign}${(abs / 1_000_000).toFixed(1)}M`
if (abs >= 1_000) return `${sign}${(abs / 1_000).toFixed(0)}K`
return `${sign}${abs.toFixed(0)}`
}
export const PredictionPanel: React.FC<Props> = ({
prediction,
stockName,
quote,
currency = 'NT$',
onRecalculate,
recalculating = false,
}) => {
const cfg = signalConfig[prediction.signal]
const Icon = cfg.icon
const probPct = Math.round(prediction.signal_probability * 100)
const rawProbPct = prediction.raw_signal_probability != null
? Math.round(prediction.raw_signal_probability * 100)
: probPct
const newsAdjustmentPct = Math.round((prediction.news_adjustment ?? 0) * 100)
const changePositive = prediction.predicted_change_pct >= 0
const cur = currency
const displayPrice = quote?.price ?? prediction.current_price
const todayChangePct = quote?.change_pct ?? null
const todayChangeAbs = quote?.change ?? null
const todayUp = todayChangePct != null && todayChangePct >= 0
const isQuick = prediction.optimized === false
const oldwang = prediction.oldwang_context
const oldwangRiskBlocked = prediction.signal === 'BUY' && (oldwang?.bear_score ?? 0) >= 2
const displayCfg = oldwangRiskBlocked ? signalConfig.HOLD : cfg
const DisplayIcon = oldwangRiskBlocked ? AlertTriangle : Icon
const oldwangLevels = oldwang?.key_levels
const oldwangKeyLevels = [
['爆量低點', oldwangLevels?.volume_spike_low],
['缺口支撐', oldwangLevels?.gap_support],
['20MA', oldwangLevels?.ma20],
].filter((entry): entry is [string, number] => typeof entry[1] === 'number')
const reliability = getPredictionReliabilitySummary({
confidence: prediction.confidence,
trainingAccuracy: prediction.training_accuracy,
isQuick,
})
return (
<div className={`rounded-xl border-2 ${displayCfg.border} ${displayCfg.bg} p-3 sm:p-5 space-y-3 sm:space-y-4`}>
{/* Quick-estimate banner */}
{isQuick && (
<div className="flex items-center gap-2 px-3 py-2 rounded-lg bg-slate-700/60 border border-slate-500 text-slate-300 text-xs">
<Loader2 size={14} className="animate-spin text-yellow-400 shrink-0" />
<span>
<span className="text-yellow-400 font-semibold">快速估算</span>
{' '}— 僅供參考(技術指標規則),ML 模型正在背景計算中,約 2 分鐘後自動更新。
</span>
</div>
)}
{oldwangRiskBlocked && (
<div className="flex items-center gap-2 px-3 py-2 rounded-lg bg-red-950/50 border border-red-700 text-red-200 text-xs">
<AlertTriangle size={14} className="text-red-300 shrink-0" />
<span>
<span className="font-semibold">老王高風險觀望</span>
{' '}— ML 原始訊號為 BUY,但已出現 {oldwang?.bear_score.toFixed(0)} 個老王風險條件,不列入買入建議。
</span>
</div>
)}
{/* Header */}
<div className="flex flex-col sm:flex-row items-start sm:items-center justify-between gap-2">
<div>
<h2 className="text-base sm:text-lg font-bold text-white">
{stockName} — {isQuick ? '快速估算' : 'ML 預測信號'}
</h2>
<p className="text-xs text-slate-400 mt-0.5">預測期間:未來 5 個交易日</p>
</div>
<div className="flex items-center gap-2 flex-wrap justify-end">
{onRecalculate && (
<button
type="button"
onClick={onRecalculate}
disabled={recalculating}
className="flex items-center gap-1.5 px-3 py-1.5 rounded-lg bg-slate-800 hover:bg-slate-700 disabled:opacity-60 disabled:cursor-not-allowed border border-slate-600 text-slate-200 text-xs font-medium transition-colors"
title="清除快取並重新計算 ML 預測"
>
{recalculating ? <Loader2 size={13} className="animate-spin" /> : <RefreshCw size={13} />}
{recalculating ? '重算中' : '重算'}
</button>
)}
<div className={`flex items-center gap-2 px-3 sm:px-4 py-1.5 sm:py-2 rounded-lg ${displayCfg.badgeBg} shadow-lg`}>
<DisplayIcon size={18} className="text-white" />
<span className="text-white font-black text-lg sm:text-xl tracking-wider">
{oldwangRiskBlocked ? 'HOLD' : cfg.label}
</span>
<span className="text-white/80 text-xs sm:text-sm">
({oldwangRiskBlocked ? '老王風險觀望' : cfg.labelZh})
</span>
</div>
</div>
</div>
{/* Price grid */}
<div className="grid grid-cols-2 sm:grid-cols-4 gap-3">
<PriceCard
label="目前價格"
labelEn="Current Price"
value={`${cur} ${displayPrice.toFixed(2)}`}
valueClass={todayChangePct == null ? 'text-white' : todayUp ? 'text-green-400' : 'text-red-400'}
sub={todayChangePct != null ? (
<span className={todayUp ? 'text-green-400' : 'text-red-400'}>
{todayUp ? '▲' : '▼'}{Math.abs(todayChangePct).toFixed(2)}%
{todayChangeAbs != null && ` (${todayUp ? '+' : ''}${todayChangeAbs.toFixed(2)})`}
</span>
) : undefined}
/>
<PriceCard
label="預測價格 (5日)"
labelEn="Predicted Price"
value={`${cur} ${prediction.predicted_price.toFixed(2)}`}
sub={
<span className={changePositive ? 'text-green-400' : 'text-red-400'}>
{changePositive ? '+' : ''}{prediction.predicted_change_pct.toFixed(2)}%
</span>
}
valueClass={changePositive ? 'text-green-400' : 'text-red-400'}
/>
<PriceCard
label="建議賣出目標"
labelEn="Sell Target"
value={`${cur} ${prediction.sell_target.toFixed(2)}`}
valueClass="text-green-300"
/>
<PriceCard
label="停損價位"
labelEn="Stop Loss"
value={`${cur} ${prediction.stop_loss.toFixed(2)}`}
valueClass="text-red-300"
/>
</div>
{/* Signal probability bar */}
<div className="space-y-1.5">
<div className="flex justify-between items-center text-xs">
<span className="text-slate-400">
{isQuick ? '快速估算分數 (Quick Score)' : '買入機率 (Buy Probability)'}
</span>
<span className={`font-bold ${displayCfg.text}`}>
{probPct}%{rawProbPct !== probPct && <span className="text-slate-500 font-medium"> · ML {rawProbPct}%</span>}
</span>
</div>
<div className="h-2.5 bg-slate-700 rounded-full overflow-hidden">
<div
className={`h-full rounded-full transition-all duration-700 ${
probPct >= 60 ? 'bg-green-500' : probPct <= 35 ? 'bg-red-500' : 'bg-yellow-500'
}`}
style={{ width: `${probPct}%` }}
/>
</div>
<div className="flex justify-between text-xs text-slate-500">
<span>強力賣出 (Strong Sell)</span>
<span>強力買入 (Strong Buy)</span>
</div>
</div>
<div className="grid grid-cols-1 lg:grid-cols-2 gap-3">
<div className="bg-slate-900/60 rounded-lg p-3 border border-slate-700 space-y-2">
<div className="flex items-center gap-2">
<Building2 size={14} className="text-cyan-400" />
<span className="text-xs font-semibold text-slate-200">法人籌碼</span>
{prediction.institutional_carry_forward && (
<span className="text-[10px] text-yellow-300 bg-yellow-500/10 border border-yellow-700 rounded px-1.5 py-0.5">沿用前日</span>
)}
</div>
<div className="grid grid-cols-4 gap-2 text-center">
{[
['外資', prediction.institutional_flow?.foreign_net],
['投信', prediction.institutional_flow?.trust_net],
['自營', prediction.institutional_flow?.dealer_net],
['合計', prediction.institutional_flow?.institutional_net],
].map(([label, value]) => {
const num = Number(value ?? 0)
return (
<div key={label as string} className="min-w-0">
<div className="text-[10px] text-slate-500">{label as string}</div>
<div className={`text-xs font-bold truncate ${num >= 0 ? 'text-green-400' : 'text-red-400'}`}>
{formatShares(num)}
</div>
</div>
)
})}
</div>
<p className="text-[10px] text-slate-500">
資料日: {prediction.institutional_as_of ?? '尚無'} · {prediction.feature_version ?? 'feature-v1'}
</p>
</div>
<div className="bg-slate-900/60 rounded-lg p-3 border border-slate-700 space-y-2">
<div className="flex items-center gap-2">
<Newspaper size={14} className="text-amber-400" />
<span className="text-xs font-semibold text-slate-200">新聞影響</span>
<span className={`text-[10px] rounded px-1.5 py-0.5 border ${
newsAdjustmentPct > 0
? 'text-green-300 bg-green-500/10 border-green-700'
: newsAdjustmentPct < 0
? 'text-red-300 bg-red-500/10 border-red-700'
: 'text-slate-400 bg-slate-800 border-slate-700'
}`}>
{newsAdjustmentPct > 0 ? '+' : ''}{newsAdjustmentPct}%
</span>
</div>
<div className="grid grid-cols-3 gap-2 text-center">
<div>
<div className="text-[10px] text-slate-500">情緒</div>
<div className="text-xs font-bold text-slate-200">{((prediction.news_signal?.sentiment_score ?? 0) * 100).toFixed(0)}</div>
</div>
<div>
<div className="text-[10px] text-slate-500">事件</div>
<div className="text-xs font-bold text-slate-200">{((prediction.news_signal?.event_score ?? 0) * 100).toFixed(0)}</div>
</div>
<div>
<div className="text-[10px] text-slate-500">信度</div>
<div className="text-xs font-bold text-slate-200">{((prediction.news_signal?.confidence ?? 0) * 100).toFixed(0)}%</div>
</div>
</div>
<p className="text-[10px] text-slate-500 truncate">
{prediction.news_signal?.reason ? '新聞未納入調整' : prediction.news_as_of ? `更新: ${prediction.news_as_of.slice(0, 10)}` : '未啟用或無新聞訊號'}
</p>
</div>
</div>
{oldwang && (
<div className="bg-slate-900/60 rounded-lg p-3 border border-slate-700 space-y-3">
<div className="flex items-center justify-between gap-2 flex-wrap">
<div className="flex items-center gap-2">
<ShieldCheck size={14} className="text-emerald-400" />
<span className="text-xs font-semibold text-slate-200">老王策略</span>
</div>
<div className="flex items-center gap-1.5 text-[10px]">
<span className="rounded px-1.5 py-0.5 bg-green-500/10 text-green-300 border border-green-700">
多方 {oldwang.bull_score.toFixed(0)}
</span>
<span className="rounded px-1.5 py-0.5 bg-red-500/10 text-red-300 border border-red-700">
風險 {oldwang.bear_score.toFixed(0)}
</span>
</div>
</div>
<div className="grid grid-cols-1 sm:grid-cols-2 gap-3">
<div className="space-y-1">
<div className="text-[10px] text-green-300 font-semibold">多方理由</div>
{(oldwang.bull_reasons?.length ?? 0) > 0 ? (
oldwang.bull_reasons?.slice(0, 3).map((reason) => (
<div key={reason} className="text-xs text-slate-300 leading-relaxed">{reason}</div>
))
) : (
<div className="text-xs text-slate-500">暫無明顯多方條件</div>
)}
</div>
<div className="space-y-1">
<div className="text-[10px] text-red-300 font-semibold">風險提醒</div>
{(oldwang.risk_reasons?.length ?? 0) > 0 ? (
oldwang.risk_reasons?.slice(0, 3).map((reason) => (
<div key={reason} className="text-xs text-slate-300 leading-relaxed">{reason}</div>
))
) : (
<div className="text-xs text-slate-500">暫無主要風險條件</div>
)}
</div>
</div>
{oldwangKeyLevels.length > 0 && (
<div className="grid grid-cols-3 gap-2 pt-1">
{oldwangKeyLevels.map(([label, value]) => (
<div key={label} className="rounded bg-slate-800/80 px-2 py-1.5 min-w-0">
<div className="text-[10px] text-slate-500 truncate">{label}</div>
<div className="text-xs font-bold text-slate-200 truncate">{cur} {value.toFixed(2)}</div>
</div>
))}
</div>
)}
</div>
)}
{/* Reliability + confidence + accuracy */}
<div className="grid grid-cols-1 sm:grid-cols-3 gap-2 text-sm">
<div className="bg-slate-800 rounded-lg px-3 py-2">
<div className="text-[11px] text-slate-400">整體可靠度</div>
<div className={`font-bold ${reliability.levelColor}`}>{reliability.levelText}</div>
</div>
<div className="bg-slate-800 rounded-lg px-3 py-2">
<div className="text-[11px] text-slate-400">預測信心度</div>
<div className={`font-bold ${reliability.confidenceColor}`}>{reliability.confidenceText}</div>
</div>
<div className="bg-slate-800 rounded-lg px-3 py-2">
<div className="text-[11px] text-slate-400">模型近期驗證準確率</div>
<div className={`font-semibold ${reliability.accuracyColor}`}>{reliability.accuracyText}</div>
</div>
</div>
{reliability.warning && (
<div className="flex items-start gap-2 bg-red-500/10 rounded-lg p-3 border border-red-700/70">
<AlertTriangle size={14} className="text-red-300 mt-0.5 flex-shrink-0" />
<p className="text-xs text-red-200 leading-relaxed">{reliability.warning}</p>
</div>
)}
{/* Disclaimer */}
<div className="flex items-start gap-2 bg-slate-900/70 rounded-lg p-3 border border-slate-700">
<AlertTriangle size={14} className="text-yellow-400 mt-0.5 flex-shrink-0" />
<p className="text-xs text-slate-400 leading-relaxed">
<span className="text-yellow-400 font-semibold">免責聲明:</span>
本系統所有預測訊號僅供學術研究與教育用途,不構成任何投資建議。
機器學習模型無法保證未來績效,投資人應自行判斷並承擔風險。
{' '}
<span className="text-slate-500 italic">
(ML signals are for educational purposes only and do NOT constitute financial advice.)
</span>
</p>
</div>
</div>
)
}
// ---------------------------------------------------------------------------
// Helper sub-component
// ---------------------------------------------------------------------------
interface PriceCardProps {
label: string
labelEn: string
value: string
valueClass?: string
sub?: React.ReactNode
}
const PriceCard: React.FC<PriceCardProps> = ({
label,
labelEn,
value,
valueClass = 'text-white',
sub,
}) => (
<div className="bg-slate-900/60 rounded-lg p-3 space-y-1 border border-slate-700">
<p className="text-xs text-slate-400 leading-tight">{label}</p>
<p className="text-xs text-slate-500">{labelEn}</p>
<p className={`text-base font-bold ${valueClass} leading-tight`}>{value}</p>
{sub && <div className="text-xs font-semibold">{sub}</div>}
</div>
)
|