BrianIsaac commited on
Commit
8f5b563
·
1 Parent(s): fe8374d

fix: convert price data to float in performance chart

Browse files

Prices from historical data may be stored as strings. Added explicit
float conversion before numeric operations to prevent TypeError when
comparing or calculating normalized values for both portfolio assets
and benchmark data.

backend/visualizations/plotly_charts.py CHANGED
@@ -312,6 +312,9 @@ def create_performance_chart(
312
  if not dates or not prices:
313
  continue
314
 
 
 
 
315
  # Calculate percentage returns (normalise to 100)
316
  if prices[0] > 0:
317
  normalized = [(p / prices[0]) * 100 for p in prices]
@@ -337,22 +340,26 @@ def create_performance_chart(
337
  benchmark_dates = benchmark_data.get('dates', [])
338
  benchmark_prices = benchmark_data.get('close_prices', [])
339
 
340
- if benchmark_dates and benchmark_prices and benchmark_prices[0] > 0:
341
- normalized_benchmark = [(p / benchmark_prices[0]) * 100 for p in benchmark_prices]
342
-
343
- fig.add_trace(go.Scatter(
344
- x=benchmark_dates,
345
- y=normalized_benchmark,
346
- name="S&P 500 (Benchmark)",
347
- mode='lines',
348
- line=dict(width=3, dash='dash', color='#FFA500'),
349
- hovertemplate=(
350
- "<b>S&P 500</b><br>" +
351
- "%{x|%B %d, %Y}<br>" +
352
- "Value: %{y:.2f}<br>" +
353
- "<extra></extra>"
354
- )
355
- ))
 
 
 
 
356
 
357
  theme = get_chart_theme()
358
 
 
312
  if not dates or not prices:
313
  continue
314
 
315
+ # Convert prices to float in case they're strings
316
+ prices = [float(p) for p in prices]
317
+
318
  # Calculate percentage returns (normalise to 100)
319
  if prices[0] > 0:
320
  normalized = [(p / prices[0]) * 100 for p in prices]
 
340
  benchmark_dates = benchmark_data.get('dates', [])
341
  benchmark_prices = benchmark_data.get('close_prices', [])
342
 
343
+ if benchmark_dates and benchmark_prices:
344
+ # Convert benchmark prices to float in case they're strings
345
+ benchmark_prices = [float(p) for p in benchmark_prices]
346
+
347
+ if benchmark_prices[0] > 0:
348
+ normalized_benchmark = [(p / benchmark_prices[0]) * 100 for p in benchmark_prices]
349
+
350
+ fig.add_trace(go.Scatter(
351
+ x=benchmark_dates,
352
+ y=normalized_benchmark,
353
+ name="S&P 500 (Benchmark)",
354
+ mode='lines',
355
+ line=dict(width=3, dash='dash', color='#FFA500'),
356
+ hovertemplate=(
357
+ "<b>S&P 500</b><br>" +
358
+ "%{x|%B %d, %Y}<br>" +
359
+ "Value: %{y:.2f}<br>" +
360
+ "<extra></extra>"
361
+ )
362
+ ))
363
 
364
  theme = get_chart_theme()
365