Fix candle chart staleness and show live price on today's candle
- Refresh candles on every /candles request instead of only at startup - Patch today's candle close/high/low with the live BTC price intraday - Synthesise today's candle from live price if CoinGecko hasn't published it yet - Display current BTC price next to the chart title Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,9 +1,12 @@
|
||||
from fastapi import APIRouter, Depends, Query
|
||||
from sqlalchemy.orm import Session
|
||||
from datetime import datetime, timezone
|
||||
|
||||
from ..database import get_db
|
||||
from .. import models
|
||||
from ..dependencies import get_current_user
|
||||
from ..services.candles import refresh_latest_candles
|
||||
from ..services.btc import get_btc_price_eur
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@@ -14,6 +17,9 @@ def get_candles(
|
||||
db: Session = Depends(get_db),
|
||||
current_user: models.User = Depends(get_current_user),
|
||||
):
|
||||
# Refresh candles on every request (no-op if DB is already current)
|
||||
refresh_latest_candles(db)
|
||||
|
||||
query = db.query(models.OHLCCandle).order_by(models.OHLCCandle.date.asc())
|
||||
|
||||
if days != "all":
|
||||
@@ -38,6 +44,26 @@ def get_candles(
|
||||
for c in candles_db
|
||||
]
|
||||
|
||||
# Patch today's candle with the live price so it tracks intraday movement
|
||||
today = datetime.now(tz=timezone.utc).strftime("%Y-%m-%d")
|
||||
live_price, _ = get_btc_price_eur()
|
||||
if live_price and candles:
|
||||
last = candles[-1]
|
||||
if last["date"] == today:
|
||||
last["close"] = round(live_price, 2)
|
||||
last["high"] = round(max(last["high"], live_price), 2)
|
||||
last["low"] = round(min(last["low"], live_price), 2)
|
||||
elif last["date"] < today:
|
||||
# No candle for today yet — create a synthetic one from live price
|
||||
prev_close = last["close"]
|
||||
candles.append({
|
||||
"date": today,
|
||||
"open": prev_close,
|
||||
"high": round(max(prev_close, live_price), 2),
|
||||
"low": round(min(prev_close, live_price), 2),
|
||||
"close": round(live_price, 2),
|
||||
})
|
||||
|
||||
purchases_db = db.query(models.Purchase).filter(
|
||||
models.Purchase.user_id == current_user.id
|
||||
).all()
|
||||
|
||||
@@ -39,7 +39,7 @@ const btnStyle = {
|
||||
marginLeft: '0.5rem',
|
||||
};
|
||||
|
||||
export default function BTCCandlestickChart({ candles, purchases, stats, fullscreen, onToggleFullscreen }) {
|
||||
export default function BTCCandlestickChart({ candles, purchases, stats, fullscreen, onToggleFullscreen, livePrice }) {
|
||||
const containerRef = useRef(null);
|
||||
const chartRef = useRef(null);
|
||||
const candleSeriesRef = useRef(null);
|
||||
@@ -167,7 +167,14 @@ export default function BTCCandlestickChart({ candles, purchases, stats, fullscr
|
||||
return (
|
||||
<div style={fullscreen ? fullscreenStyle : cardStyle}>
|
||||
<div style={headerStyle}>
|
||||
<div style={titleStyle}>BTC Candles (EUR)</div>
|
||||
<div style={titleStyle}>
|
||||
BTC Candles (EUR)
|
||||
{livePrice != null && (
|
||||
<span style={{ fontSize: '0.9rem', fontWeight: 400, color: '#ccc', marginLeft: '0.75rem' }}>
|
||||
€{livePrice.toLocaleString()}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
<div>
|
||||
<button style={btnStyle} onClick={handleSave}>Save as PNG</button>
|
||||
<button style={btnStyle} onClick={onToggleFullscreen}>
|
||||
|
||||
@@ -208,6 +208,7 @@ export default function Dashboard() {
|
||||
candles={activeCandles?.candles ?? null}
|
||||
purchases={activeCandles?.purchases ?? purchases}
|
||||
stats={stats}
|
||||
livePrice={stats?.current_price ?? null}
|
||||
fullscreen={fullscreenChart}
|
||||
onToggleFullscreen={handleToggleFullscreen}
|
||||
/>
|
||||
|
||||
Reference in New Issue
Block a user