Skip to content

FinceptTerminal is a modern finance application offering advanced market analytics, investment research, and economic data tools, designed for interactive exploration and data-driven decision-making in a user-friendly environment.

equity tab broker and algo tab update

tilakpatel2279bfb3ca92d5
diff --git a/fincept-qt/CMakeLists.txt b/fincept-qt/CMakeLists.txt
index b3fcd250e..5d8d8058b 100644
--- a/fincept-qt/CMakeLists.txt
+++ b/fincept-qt/CMakeLists.txt
@@ -844,6 +844,7 @@ set(STORAGE_SOURCES
src/storage/sqlite/migrations/v032_pending_orders.cpp
src/storage/sqlite/migrations/v033_historify.cpp
src/storage/sqlite/migrations/v034_strategy_portfolio.cpp
+ src/storage/sqlite/migrations/v035_news_analysis.cpp
# Historical OHLCV data store (Historify, Phase 3 §13)
src/storage/HistoricalDataStore.cpp
@@ -1020,6 +1021,7 @@ set(TRADING_SOURCES
src/trading/instruments/ZerodhaInstrumentParser.cpp
src/trading/instruments/FyersInstrumentParser.cpp
src/trading/instruments/GrowwInstrumentParser.cpp
+ src/trading/instruments/DhanInstrumentParser.cpp
src/trading/instruments/InstrumentService.cpp
src/trading/instruments/SymbolResolver.cpp
)
@@ -1036,6 +1038,7 @@ set(ALGO_ENGINE_SOURCES
src/algo_engine/AlgoEngineProducer.cpp
src/algo_engine/AlgoScanner.cpp
src/algo_engine/CandleDataFetcher.cpp
+ src/algo_engine/BacktestEngine.cpp
)
# Services
@@ -2235,6 +2238,7 @@ set_source_files_properties(
src/storage/sqlite/migrations/v032_pending_orders.cpp
src/storage/sqlite/migrations/v033_historify.cpp
src/storage/sqlite/migrations/v034_strategy_portfolio.cpp
+ src/storage/sqlite/migrations/v035_news_analysis.cpp
# Polymarket screen files — each defines static fmt_* helpers in same namespace
src/screens/polymarket/PolymarketScreen.cpp
src/screens/polymarket/PolymarketCommandBar.cpp
diff --git a/fincept-qt/scripts/agents/finagent_core/agent_factory.py b/fincept-qt/scripts/agents/finagent_core/agent_factory.py
index dcda39d36..adde42474 100644
--- a/fincept-qt/scripts/agents/finagent_core/agent_factory.py
+++ b/fincept-qt/scripts/agents/finagent_core/agent_factory.py
@@ -127,8 +127,20 @@ class AgentFactory:
# ── Native provider instances ─────────────────────────────────────────
if provider == "openai":
from agno.models.openai import OpenAIChat
- return OpenAIChat(id=model_id, api_key=api_key,
+ # Agno maps "system"→"developer" by default, valid only for OpenAI
+ # o-series models. gpt-* models reject "developer", so use the
+ # standard role map unless this is an o-series id.
+ oai_kwargs = dict(id=model_id, api_key=api_key,
temperature=temperature, max_tokens=max_tokens)
+ if not (model_id or "").lower().startswith(("o1", "o3", "o4")):
+ oai_kwargs["role_map"] = {
+ "system": "system",
+ "user": "user",
+ "assistant": "assistant",
+ "tool": "tool",
+ "model": "assistant",
+ }
+ return OpenAIChat(**{k: v for k, v in oai_kwargs.items() if v})
if provider == "anthropic":
from agno.models.anthropic import Claude
diff --git a/fincept-qt/scripts/agents/finagent_core/main.py b/fincept-qt/scripts/agents/finagent_core/main.py
index 208dc1e86..195ab013c 100644
--- a/fincept-qt/scripts/agents/finagent_core/main.py
+++ b/fincept-qt/scripts/agents/finagent_core/main.py
@@ -236,7 +236,15 @@ def main(args=None):
print(json.dumps(result), flush=True)
return ""
else:
- result = dispatch_action(action, api_keys, params, config)
+ # Keep stdout pristine for the single JSON envelope. Some agent/LLM
+ # SDKs log to stdout directly (rich-style consoles that bypass the
+ # stderr logging config above); those brace-containing lines would
+ # otherwise corrupt the JSON the C++ side parses. Route everything
+ # the action prints to stderr; main()'s final print() (after this
+ # returns, stdout restored) emits the clean envelope.
+ import contextlib
+ with contextlib.redirect_stdout(sys.stderr):
+ result = dispatch_action(action, api_keys, params, config)
return json.dumps(result)
except json.JSONDecodeError as e:
diff --git a/fincept-qt/scripts/agents/finagent_core/registries/models_registry.py b/fincept-qt/scripts/agents/finagent_core/registries/models_registry.py
index 5b46eaa68..64b6b774f 100644
--- a/fincept-qt/scripts/agents/finagent_core/registries/models_registry.py
+++ b/fincept-qt/scripts/agents/finagent_core/registries/models_registry.py
@@ -458,6 +458,25 @@ class ModelsRegistry:
if v is not None:
model_kwargs[k] = v
+ # Agno's OpenAI-protocol models default their role_map to
+ # {"system": "developer", ...}, which is ONLY valid for OpenAI's
+ # o-series reasoning models. Standard gpt-* models and every
+ # OpenAI-compatible endpoint (MiniMax, DeepSeek, OpenRouter, Groq,
+ # Together, etc.) reject it with "invalid role: developer". Force the
+ # standard role map for every OpenAI-protocol model except genuine
+ # o-series ids.
+ if "openai" in class_path.lower() and "role_map" not in model_kwargs:
+ _mid = (final_model_id or "").lower()
+ _is_o_series = _mid.startswith(("o1", "o3", "o4"))
+ if not _is_o_series:
+ model_kwargs["role_map"] = {
+ "system": "system",
+ "user": "user",
+ "assistant": "assistant",
+ "tool": "tool",
+ "model": "assistant",
+ }
+
model_instance = model_class(**model_kwargs)
logger.debug(f"Created model: {provider}/{final_model_id}")
diff --git a/fincept-qt/scripts/algo_trading/__init__.py b/fincept-qt/scripts/algo_trading/__init__.py
deleted file mode 100644
index 652861391..000000000
--- a/fincept-qt/scripts/algo_trading/__init__.py
+++ /dev/null
@@ -1 +0,0 @@
-# Algo Trading Module
diff --git a/fincept-qt/scripts/algo_trading/backtest_engine.py b/fincept-qt/scripts/algo_trading/backtest_engine.py
deleted file mode 100644
index bf44c9ef3..000000000
--- a/fincept-qt/scripts/algo_trading/backtest_engine.py
+++ /dev/null
@@ -1,752 +0,0 @@
-"""
-Backtest Engine — Walk-forward strategy backtester
-
-Fetches historical data via yfinance or from local candle_cache (Fyers),
-walks through candles bar-by-bar, evaluates entry/exit conditions at each bar,
-and computes performance metrics.
-
-Usage:
- python backtest_engine.py \
- --symbol RELIANCE \
- --entry-conditions '[{"indicator":"RSI","params":{"period":14},"field":"value","operator":"crosses_below","value":30}]' \
- --exit-conditions '[{"indicator":"RSI","params":{"period":14},"field":"value","operator":"crosses_above","value":70}]' \
- --timeframe 1d \
- --period 1y \
- --stop-loss 3 \
- --take-profit 5 \
- --initial-capital 100000 \
- --provider fyers \
- --db /path/to/db
-
-Output:
- JSON with trades, equity curve, and performance metrics
-"""
-
-import json
-import sys
-import argparse
-import math
-import os
-import sqlite3
-import traceback
-
-# Debug log collector
-DEBUG_LOG = []
-
-def debug(msg):
- """Add a debug message to the log."""
- DEBUG_LOG.append(f"[py:backtest] {msg}")
-
-debug("Script starting...")
-debug(f"Python version: {sys.version}")
-debug(f"Working directory: {os.getcwd()}")
-debug(f"Script location: {os.path.abspath(__file__)}")
-
-try:
- import pandas as pd
- debug(f"pandas version: {pd.__version__}")
-except ImportError as e:
- debug(f"ERROR: pandas not installed: {e}")
- print(json.dumps({
- 'success': False,
- 'error': f'pandas not installed: {e}',
- 'debug': DEBUG_LOG
- }))
- sys.exit(1)
-
-sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
-debug(f"Added to sys.path: {os.path.dirname(os.path.abspath(__file__))}")
-
-try:
- from condition_evaluator import evaluate_condition_group
- debug("condition_evaluator imported successfully")
-except ImportError as e:
- debug(f"ERROR: Failed to import condition_evaluator: {e}")
- print(json.dumps({
- 'success': False,
- 'error': f'Failed to import condition_evaluator: {e}',
- 'debug': DEBUG_LOG
- }))
- sys.exit(1)
-
-# Minimum candles needed before we start evaluating (for indicator warmup)
-WARMUP_BARS = 50
-
-
-def fetch_from_yfinance(symbol: str, period: str, interval: str):
- """Fetch OHLCV data via yfinance."""
- debug(f"fetch_from_yfinance: symbol={symbol}, period={period}, interval={interval}")
- try:
- import yfinance as yf
- debug(f"yfinance version: {yf.__version__}")
- ticker = yf.Ticker(symbol)
- debug(f"Created ticker object for {symbol}")
- df = ticker.history(period=period, interval=interval)
- debug(f"history() returned DataFrame with shape: {df.shape if df is not None else 'None'}")
- if df.empty:
- debug("DataFrame is empty")
- return None, "No data returned from yfinance"
- # Normalize column names
- df.columns = [c.lower() for c in df.columns]
- debug(f"Columns after lowercasing: {list(df.columns)}")
- # Ensure we have needed columns
- for col in ['open', 'high', 'low', 'close', 'volume']:
- if col not in df.columns:
- debug(f"Missing column: {col}")
- return None, f"Missing column: {col}"
- df = df[['open', 'high', 'low', 'close', 'volume']].reset_index(drop=True)
- debug(f"Final DataFrame shape: {df.shape}, first row: {df.iloc[0].to_dict() if len(df) > 0 else 'empty'}")
- return df, None
- except ImportError as e:
- debug(f"yfinance ImportError: {e}")
- return None, "yfinance not installed. Install with: pip install yfinance"
- except Exception as e:
- debug(f"yfinance Exception: {e}\n{traceback.format_exc()}")
- return None, f"yfinance error: {e}"
-
-
-def fetch_from_candle_cache(db_path: str, symbol: str, timeframe: str, limit: int = 500):
- """Fetch OHLCV data from local candle_cache (populated by Fyers/broker)."""
- debug(f"fetch_from_candle_cache: db_path={db_path}, symbol={symbol}, timeframe={timeframe}, limit={limit}")
- try:
- if not os.path.exists(db_path):
- debug(f"Database file does not exist: {db_path}")
- return None, f"Database not found: {db_path}"
-
- debug(f"Database file exists, size: {os.path.getsize(db_path)} bytes")
- conn = sqlite3.connect(db_path)
-
- # First check if table exists
- cursor = conn.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='candle_cache'")
- tables = cursor.fetchall()
- debug(f"candle_cache table exists: {len(tables) > 0}")
-
- if len(tables) == 0:
- conn.close()
- return None, "candle_cache table does not exist in database"
-
- # Check what data exists for this symbol
- cursor = conn.execute(
- "SELECT COUNT(*), MIN(open_time), MAX(open_time) FROM candle_cache WHERE symbol = ? AND timeframe = ?",
- (symbol, timeframe)
- )
- row = cursor.fetchone()
- debug(f"Data for {symbol}/{timeframe}: count={row[0]}, min_time={row[1]}, max_time={row[2]}")
-
- # Also try without timeframe filter to see what's available
- cursor = conn.execute(
- "SELECT DISTINCT symbol, timeframe, COUNT(*) as cnt FROM candle_cache GROUP BY symbol, timeframe LIMIT 20"
- )
- available = cursor.fetchall()
- debug(f"Available data in candle_cache: {available[:10]}") # Show first 10
-
- query = """
- SELECT open_time, o, h, l, c, volume
- FROM candle_cache
- WHERE symbol = ? AND timeframe = ? AND is_closed = 1
- ORDER BY open_time ASC
- LIMIT ?
- """
- df = pd.read_sql_query(query, conn, params=(symbol, timeframe, limit))
- conn.close()
-
- debug(f"Query returned {len(df)} rows")
-
- if df.empty:
- return None, f"No data in candle_cache for {symbol} ({timeframe}). Run a scan first to fetch data."
-
- df.rename(columns={'o': 'open', 'h': 'high', 'l': 'low', 'c': 'close'}, inplace=True)
- df = df[['open', 'high', 'low', 'close', 'volume']].reset_index(drop=True)
- debug(f"Final DataFrame shape: {df.shape}, first row: {df.iloc[0].to_dict() if len(df) > 0 else 'empty'}")
- return df, None
- except Exception as e:
- debug(f"Database error: {e}\n{traceback.format_exc()}")
- return None, f"Database error: {e}"
-
-
-def fetch_historical_data(symbol: str, period: str, interval: str, provider: str = 'yfinance', db_path: str = None):
- """Fetch OHLCV data from the specified provider."""
- debug(f"fetch_historical_data: symbol={symbol}, period={period}, interval={interval}, provider={provider}, db_path={db_path}")
-
- if provider == 'fyers' or provider == 'candle_cache':
- if not db_path:
- # Try default path
- db_path = os.path.join(os.environ.get('APPDATA', ''), 'fincept-terminal', 'fincept_terminal.db')
- debug(f"Using default db_path: {db_path}")
-
- # Map period to limit
- period_to_limit = {
- '1mo': 30, '3mo': 90, '6mo': 180,
- '1y': 365, '2y': 730, '5y': 1825,
- }
- limit = period_to_limit.get(period, 365)
- debug(f"Period {period} -> limit {limit}")
-
- df, error = fetch_from_candle_cache(db_path, symbol, interval, limit)
- if error:
- debug(f"fetch_from_candle_cache error: {error}")
- return None, error
- debug(f"fetch_from_candle_cache success: {len(df)} rows")
- return df, None
- else:
- # Default to yfinance
- debug("Using yfinance provider")
- return fetch_from_yfinance(symbol, period, interval)
-
-
-def run_backtest(
- df,
- entry_conditions: list,
- exit_conditions: list,
- stop_loss_pct: float = 0,
- take_profit_pct: float = 0,
- initial_capital: float = 100000,
- timeframe: str = '1d',
-):
- """
- Walk forward through candle data and generate trades.
- Uses proper position sizing based on available capital.
-
- Returns dict with trades, equity curve, and metrics.
- """
- trades = []
- equity_curve = []
- in_position = False
- entry_price = 0.0
- entry_bar = 0
- position_shares = 0
- capital = initial_capital
- peak_capital = initial_capital
- max_drawdown = 0.0
-
- for i in range(WARMUP_BARS, len(df)):
- # Use candles up to and including bar i
- window = df.iloc[:i + 1].copy()
- current_price = window['close'].iloc[-1]
- current_bar = i
-
- if not in_position:
- # Check entry conditions
- result = evaluate_condition_group(entry_conditions, window)
- if result['result']:
- in_position = True
- entry_price = current_price
- entry_bar = current_bar
- # Position sizing: invest all available capital
- position_shares = math.floor(capital / entry_price) if entry_price > 0 else 0
- if position_shares <= 0:
- in_position = False # Can't afford even 1 share
- continue
- capital -= position_shares * entry_price
- else:
- # Check risk management first
- pnl_pct = ((current_price - entry_price) / entry_price) * 100
-
- # Stop loss
- if stop_loss_pct > 0 and pnl_pct <= -stop_loss_pct:
- exit_price = current_price
- pnl = (exit_price - entry_price) * position_shares
- capital += position_shares * exit_price
- trades.append({
- 'entry_bar': entry_bar,
- 'exit_bar': current_bar,
- 'entry_price': round(entry_price, 2),
- 'exit_price': round(exit_price, 2),
- 'shares': position_shares,
- 'pnl': round(pnl, 2),
- 'pnl_pct': round(pnl_pct, 2),
- 'reason': 'stop_loss',
- 'bars_held': current_bar - entry_bar,
- })
- in_position = False
- position_shares = 0
- continue
-
- # Take profit
- if take_profit_pct > 0 and pnl_pct >= take_profit_pct:
- exit_price = current_price
- pnl = (exit_price - entry_price) * position_shares
- capital += position_shares * exit_price
- trades.append({
- 'entry_bar': entry_bar,
- 'exit_bar': current_bar,
- 'entry_price': round(entry_price, 2),
- 'exit_price': round(exit_price, 2),
- 'shares': position_shares,
- 'pnl': round(pnl, 2),
- 'pnl_pct': round(pnl_pct, 2),
- 'reason': 'take_profit',
- 'bars_held': current_bar - entry_bar,
- })
- in_position = False
- position_shares = 0
- continue
-
- # Check exit conditions
- result = evaluate_condition_group(exit_conditions, window)
- if result['result']:
- exit_price = current_price
- pnl = (exit_price - entry_price) * position_shares
- capital += position_shares * exit_price
- trades.append({
- 'entry_bar': entry_bar,
- 'exit_bar': current_bar,
- 'entry_price': round(entry_price, 2),
- 'exit_price': round(exit_price, 2),
- 'shares': position_shares,
- 'pnl': round(pnl, 2),
- 'pnl_pct': round(pnl_pct, 2),
- 'reason': 'exit_signal',
- 'bars_held': current_bar - entry_bar,
- })
- in_position = False
- position_shares = 0
-
- # Track equity curve
- unrealized = (current_price - entry_price) * position_shares if in_position else 0
- current_equity = capital + (position_shares * current_price if in_position else 0)
- equity_curve.append(round(current_equity, 2))
-
- if current_equity > peak_capital:
- peak_capital = current_equity
- dd = ((peak_capital - current_equity) / peak_capital) * 100 if peak_capital > 0 else 0
- if dd > max_drawdown:
- max_drawdown = dd
-
- # Close any open position at last bar
- if in_position and len(df) > 0:
- exit_price = df['close'].iloc[-1]
- pnl = (exit_price - entry_price) * position_shares
- pnl_pct = ((exit_price - entry_price) / entry_price) * 100
- capital += position_shares * exit_price
- trades.append({
- 'entry_bar': entry_bar,
- 'exit_bar': len(df) - 1,
- 'entry_price': round(entry_price, 2),
- 'exit_price': round(exit_price, 2),
- 'shares': position_shares,
- 'pnl': round(pnl, 2),
- 'pnl_pct': round(pnl_pct, 2),
- 'reason': 'end_of_data',
- 'bars_held': len(df) - 1 - entry_bar,
- })
-
- # Compute metrics
- total_trades = len(trades)
- if total_trades == 0:
- return {
- 'trades': [],
- 'equity_curve': equity_curve,
- 'metrics': {
- 'total_trades': 0,
- 'win_rate': 0,
- 'total_return': 0,
- 'total_return_pct': 0,
- 'max_drawdown': round(max_drawdown, 2),
- 'avg_pnl': 0,
- 'avg_bars_held': 0,
- 'profit_factor': 0,
- 'sharpe': 0,
- },
- }
-
- wins = [t for t in trades if t['pnl'] > 0]
- losses = [t for t in trades if t['pnl'] <= 0]
- win_rate = (len(wins) / total_trades) * 100
-
- total_return = capital - initial_capital
- total_return_pct = (total_return / initial_capital) * 100
-
- avg_pnl = sum(t['pnl'] for t in trades) / total_trades
- avg_bars_held = sum(t['bars_held'] for t in trades) / total_trades
-
- gross_profit = sum(t['pnl'] for t in wins) if wins else 0
- gross_loss = abs(sum(t['pnl'] for t in losses)) if losses else 0
- profit_factor = (gross_profit / gross_loss) if gross_loss > 0 else float('inf') if gross_profit > 0 else 0
-
- # Annualization factor based on timeframe (bars per year)
- BARS_PER_YEAR = {
- '1m': 252 * 390, '3m': 252 * 130, '5m': 252 * 78, '10m': 252 * 39,
- '15m': 252 * 26, '30m': 252 * 13, '1h': int(252 * 6.5), '4h': int(252 * 1.625),
- '1d': 252, '1D': 252, 'D': 252, '1w': 52, '1W': 52, '1M': 12,
- }
- ann_factor = BARS_PER_YEAR.get(timeframe, 252)
-
- # Sharpe ratio (annualized using timeframe-aware factor)
- if len(equity_curve) > 1:
- returns = []
- for i in range(1, len(equity_curve)):
- if equity_curve[i - 1] != 0:
- returns.append((equity_curve[i] - equity_curve[i - 1]) / equity_curve[i - 1])
- if returns:
- mean_ret = sum(returns) / len(returns)
- std_ret = (sum((r - mean_ret) ** 2 for r in returns) / len(returns)) ** 0.5
- sharpe = (mean_ret / std_ret) * (ann_factor ** 0.5) if std_ret > 0 else 0
- else:
- sharpe = 0
- else:
- sharpe = 0
-
- # Cap profit_factor for JSON serialization
- if math.isinf(profit_factor):
- profit_factor = 999.99
-
- # Downsample equity curve if too large (keep full shape visible)
- if len(equity_curve) > 500:
- step = len(equity_curve) // 500
- equity_curve_out = equity_curve[::step]
- # Ensure the last point is always included
- if equity_curve_out[-1] != equity_curve[-1]:
- equity_curve_out.append(equity_curve[-1])
- else:
- equity_curve_out = equity_curve
-
- return {
- 'trades': trades,
- 'equity_curve': equity_curve_out,
- 'metrics': {
- 'total_trades': total_trades,
- 'winning_trades': len(wins),
- 'losing_trades': len(losses),
- 'win_rate': round(win_rate, 1),
- 'total_return': round(total_return, 2),
- 'total_return_pct': round(total_return_pct, 2),
- 'max_drawdown': round(max_drawdown, 2),
- 'avg_pnl': round(avg_pnl, 2),
- 'avg_bars_held': round(avg_bars_held, 1),
- 'profit_factor': round(profit_factor, 2),
- 'sharpe': round(sharpe, 2),
- },
- }
-
-
-def get_db_path(arg_db: str) -> str:
- """Resolve DB path from arg or fallback to AppData location."""
- if arg_db and os.path.exists(arg_db):
- return arg_db
- appdata = os.environ.get('APPDATA', '')
- if appdata:
- candidate = os.path.join(appdata, 'Fincept', 'FinceptTerminal', 'fincept.db')
- if os.path.exists(candidate):
- return candidate
- return arg_db or ''
-
-
-def open_db(db_path: str):
- conn = sqlite3.connect(db_path, timeout=10)
- conn.row_factory = sqlite3.Row
- conn.execute("PRAGMA journal_mode=WAL")
- conn.execute("PRAGMA busy_timeout=5000")
- return conn
-
-
-def cmd_save_strategy(params: dict, db_path: str):
- """Insert or replace a strategy in algo_strategies table."""
- try:
- conn = open_db(db_path)
- conn.execute("""
- CREATE TABLE IF NOT EXISTS algo_strategies (
- id TEXT PRIMARY KEY,
- name TEXT NOT NULL,
- description TEXT DEFAULT '',
- timeframe TEXT DEFAULT '1d',
- entry_conditions TEXT DEFAULT '[]',
- exit_conditions TEXT DEFAULT '[]',
- entry_logic TEXT DEFAULT 'AND',
- exit_logic TEXT DEFAULT 'AND',
- stop_loss REAL DEFAULT 0,
- take_profit REAL DEFAULT 0,
- trailing_stop REAL DEFAULT 0,
- trailing_stop_type TEXT DEFAULT 'percent',
- is_active INTEGER DEFAULT 1,
- created_at TEXT DEFAULT CURRENT_TIMESTAMP,
- updated_at TEXT DEFAULT CURRENT_TIMESTAMP
- )
- """)
- conn.execute("""
- INSERT INTO algo_strategies
- (id, name, description, timeframe,
- entry_conditions, exit_conditions, entry_logic, exit_logic,
- stop_loss, take_profit, trailing_stop, updated_at)
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP)
- ON CONFLICT(id) DO UPDATE SET
- name = excluded.name,
- description = excluded.description,
- timeframe = excluded.timeframe,
- entry_conditions = excluded.entry_conditions,
- exit_conditions = excluded.exit_conditions,
- entry_logic = excluded.entry_logic,
- exit_logic = excluded.exit_logic,
- stop_loss = excluded.stop_loss,
- take_profit = excluded.take_profit,
- trailing_stop = excluded.trailing_stop,
- updated_at = CURRENT_TIMESTAMP
- """, (
- params['id'], params.get('name', ''), params.get('description', ''),
- params.get('timeframe', '1d'),
- json.dumps(params.get('entry_conditions', [])),
- json.dumps(params.get('exit_conditions', [])),
- params.get('entry_logic', 'AND'), params.get('exit_logic', 'AND'),
- params.get('stop_loss', 0), params.get('take_profit', 0),
- params.get('trailing_stop', 0),
- ))
- conn.commit()
- conn.close()
- print(json.dumps({'success': True, 'id': params['id']}))
- except Exception as e:
- print(json.dumps({'success': False, 'error': str(e)}))
-
-
-def cmd_list_strategies(db_path: str):
- """Return all strategies from DB."""
- try:
- conn = open_db(db_path)
- rows = conn.execute("""
- SELECT id, name, description, timeframe,
- entry_conditions, exit_conditions, entry_logic, exit_logic,
- stop_loss, take_profit, trailing_stop,
- is_active, created_at, updated_at
- FROM algo_strategies
- WHERE is_active = 1
- ORDER BY created_at DESC
- """).fetchall()
- conn.close()
- strategies = []
- for r in rows:
- s = dict(r)
- # Parse JSON arrays stored as text
- try:
- s['entry_conditions'] = json.loads(s['entry_conditions'] or '[]')
- except Exception:
- s['entry_conditions'] = []
- try:
- s['exit_conditions'] = json.loads(s['exit_conditions'] or '[]')
- except Exception:
- s['exit_conditions'] = []
- strategies.append(s)
- print(json.dumps({'success': True, 'strategies': strategies}))
- except sqlite3.OperationalError as e:
- if 'no such table' in str(e).lower():
- print(json.dumps({'success': True, 'strategies': []}))
- else:
- print(json.dumps({'success': False, 'error': str(e), 'strategies': []}))
- except Exception as e:
- print(json.dumps({'success': False, 'error': str(e), 'strategies': []}))
-
-
-def cmd_list_registry():
- """Return all strategies from the strategies/_registry.py index."""
- try:
- import importlib.util
- registry_path = os.path.join(os.path.dirname(__file__), '..', 'strategies', '_registry.py')
- registry_path = os.path.normpath(registry_path)
- spec = importlib.util.spec_from_file_location('_registry', registry_path)
- mod = importlib.util.module_from_spec(spec)
- spec.loader.exec_module(mod)
- reg = mod.STRATEGY_REGISTRY # dict: id -> {name, category, path}
- strategies = []
- for fct_id, meta in reg.items():
- strategies.append({
- 'id': fct_id,
- 'name': meta.get('name', fct_id),
- 'description': meta.get('category', ''),
- 'timeframe': '1d',
- 'entry_conditions': [],
- 'exit_conditions': [],
- 'entry_logic': 'AND',
- 'exit_logic': 'AND',
- 'stop_loss': 0,
- 'take_profit': 0,
- 'trailing_stop': 0,
- 'is_active': 1,
- 'created_at': '',
- 'updated_at': '',
- 'script_path': meta.get('path', ''),
- })
- print(json.dumps({'success': True, 'strategies': strategies}))
- except Exception as e:
- print(json.dumps({'success': False, 'error': str(e), 'strategies': []}))
-
-
-def cmd_delete_strategy(strategy_id: str, db_path: str):
- """Soft-delete a strategy (set is_active=0)."""
- try:
- conn = open_db(db_path)
- conn.execute(
- "UPDATE algo_strategies SET is_active = 0, updated_at = CURRENT_TIMESTAMP WHERE id = ?",
- (strategy_id,)
- )
- conn.commit()
- conn.close()
- print(json.dumps({'success': True, 'id': strategy_id}))
- except Exception as e:
- print(json.dumps({'success': False, 'error': str(e)}))
-
-
-def cmd_run_backtest(params: dict, db_path: str):
- """Run walk-forward backtest for a strategy loaded from DB."""
- debug("cmd_run_backtest started")
- strategy_id = params.get('strategy_id', '')
- symbol = params.get('symbol', '')
- start_date = params.get('start_date', '2024-01-01')
- end_date = params.get('end_date', '2025-01-01')
- initial_capital = float(params.get('initial_capital', 100000))
-
- # Load strategy conditions from DB
- entry_conditions = []
- exit_conditions = []
- stop_loss_pct = 0.0
- take_profit_pct = 0.0
- timeframe = '1d'
-
- if db_path and os.path.exists(db_path):
- try:
- conn = open_db(db_path)
- row = conn.execute(
- "SELECT entry_conditions, exit_conditions, entry_logic, exit_logic, "
- "stop_loss, take_profit, timeframe FROM algo_strategies WHERE id = ?",
- (strategy_id,)
- ).fetchone()
- conn.close()
- if row:
- entry_conditions = json.loads(row['entry_conditions'] or '[]')
- exit_conditions = json.loads(row['exit_conditions'] or '[]')
- stop_loss_pct = row['stop_loss'] or 0.0
- take_profit_pct = row['take_profit'] or 0.0
- timeframe = row['timeframe'] or '1d'
- debug(f"Strategy loaded from DB: {len(entry_conditions)} entry, {len(exit_conditions)} exit conditions")
- else:
- debug(f"Strategy {strategy_id} not found in DB, proceeding with empty conditions")
- except Exception as e:
- debug(f"Failed to load strategy from DB: {e}")
-
- # Determine period string from date range for yfinance
- try:
- from datetime import datetime as dt
- d1 = dt.strptime(start_date, '%Y-%m-%d')
- d2 = dt.strptime(end_date, '%Y-%m-%d')
- days = (d2 - d1).days
- if days <= 90:
- period = '3mo'
- elif days <= 180:
- period = '6mo'
- elif days <= 370:
- period = '1y'
- elif days <= 740:
- period = '2y'
- else:
- period = '5y'
- except Exception:
- period = '1y'
-
- debug(f"Fetching data: symbol={symbol}, period={period}, timeframe={timeframe}")
- try:
- df, error = fetch_historical_data(symbol, period, timeframe, provider='yfinance', db_path=db_path)
- except Exception as e:
- debug(f"Exception fetching data: {e}\n{traceback.format_exc()}")
- print(json.dumps({'success': False, 'error': f'Exception fetching data: {e}', 'debug': DEBUG_LOG}))
- return
-
- if error or df is None or (hasattr(df, 'empty') and df.empty):
- print(json.dumps({
- 'success': False,
- 'error': error or f'No historical data for {symbol}',
- 'hint': 'Use .NS suffix for NSE stocks (e.g. RELIANCE.NS) for yfinance.',
- 'debug': DEBUG_LOG,
- }))
- return
-
- if len(df) < WARMUP_BARS + 10:
- print(json.dumps({
- 'success': False,
- 'error': f'Insufficient data ({len(df)} bars, need >= {WARMUP_BARS + 10})',
- 'debug': DEBUG_LOG,
- }))
- return
-
- try:
- result = run_backtest(
- df=df,
- entry_conditions=entry_conditions,
- exit_conditions=exit_conditions,
- stop_loss_pct=stop_loss_pct,
- take_profit_pct=take_profit_pct,
- initial_capital=initial_capital,
- timeframe=timeframe,
- )
- except Exception as e:
- debug(f"Exception in run_backtest: {e}\n{traceback.format_exc()}")
- print(json.dumps({'success': False, 'error': f'Backtest error: {e}', 'debug': DEBUG_LOG}))
- return
-
- metrics = result.get('metrics', {})
- print(json.dumps({
- 'success': True,
- 'symbol': symbol,
- 'timeframe': timeframe,
- 'total_bars': len(df),
- 'total_return': metrics.get('total_return_pct', 0),
- 'total_return_abs': metrics.get('total_return', 0),
- 'sharpe_ratio': metrics.get('sharpe', 0),
- 'max_drawdown': metrics.get('max_drawdown', 0),
- 'total_trades': metrics.get('total_trades', 0),
- 'win_rate': metrics.get('win_rate', 0),
- 'profit_factor': metrics.get('profit_factor', 0),
- 'debug': DEBUG_LOG,
- **result,
- }))
-
-
-def main():
- debug("main() started")
- debug(f"sys.argv: {sys.argv}")
-
- parser = argparse.ArgumentParser(description='Backtest Engine')
- parser.add_argument('command', choices=['save_strategy', 'list_strategies', 'list_registry', 'delete_strategy', 'run_backtest'],
- help='Subcommand to execute')
- parser.add_argument('payload', nargs='?', default=None,
- help='JSON payload or ID depending on command')
- parser.add_argument('--db', default=None, help='SQLite database path')
-
- try:
- args = parser.parse_args()
- debug(f"command={args.command}, payload_len={len(args.payload or '')}, db={args.db}")
- except Exception as e:
- debug(f"Argument parsing error: {e}")
- print(json.dumps({'success': False, 'error': f'Argument parsing error: {e}', 'debug': DEBUG_LOG}))
- sys.exit(1)
-
- db_path = get_db_path(args.db)
- debug(f"Resolved db_path={db_path}")
-
- if args.command == 'save_strategy':
- try:
- params = json.loads(args.payload or '{}')
- except json.JSONDecodeError as e:
- print(json.dumps({'success': False, 'error': f'Invalid JSON: {e}', 'debug': DEBUG_LOG}))
- sys.exit(1)
- cmd_save_strategy(params, db_path)
-
- elif args.command == 'list_strategies':
- cmd_list_strategies(db_path)
-
- elif args.command == 'list_registry':
- cmd_list_registry()
-
- elif args.command == 'delete_strategy':
- if not args.payload:
- print(json.dumps({'success': False, 'error': 'strategy_id required', 'debug': DEBUG_LOG}))
- sys.exit(1)
- cmd_delete_strategy(args.payload, db_path)
-
- elif args.command == 'run_backtest':
- try:
- params = json.loads(args.payload or '{}')
- except json.JSONDecodeError as e:
- print(json.dumps({'success': False, 'error': f'Invalid JSON: {e}', 'debug': DEBUG_LOG}))
- sys.exit(1)
- cmd_run_backtest(params, db_path)
-
-
-if __name__ == '__main__':
- main()
diff --git a/fincept-qt/scripts/algo_trading/fetch_candles.py b/fincept-qt/scripts/algo_trading/fetch_candles.py
deleted file mode 100644
index 3db00163a..000000000
--- a/fincept-qt/scripts/algo_trading/fetch_candles.py
+++ /dev/null
@@ -1,91 +0,0 @@
-"""
-Minimal candle data fetcher — data source only, no algo logic.
-Returns OHLCV JSON for multiple symbols via yfinance.
-
-Usage: python fetch_candles.py '{"symbols":["RELIANCE","TCS"],"timeframe":"1d","lookback_days":365}'
-"""
-import json
-import sys
-
-try:
- import yfinance as yf
-except ImportError:
- print(json.dumps({"success": False, "error": "yfinance not installed"}))
- sys.exit(1)
-
-
-TIMEFRAME_MAP = {
- "1m": "1m", "3m": "5m", "5m": "5m", "15m": "15m",
- "30m": "30m", "1h": "1h", "4h": "1h", "1d": "1d",
-}
-
-PERIOD_MAP = {
- "1m": "7d", "3m": "60d", "5m": "60d", "15m": "60d",
- "30m": "60d", "1h": "730d", "4h": "730d", "1d": "max",
-}
-
-
-def symbol_to_yf(symbol: str) -> str:
- s = symbol.strip().upper()
- if "." in s or "/" in s or "-" in s:
- return s
- return s + ".NS"
-
-
-def fetch_symbol(symbol: str, timeframe: str, lookback_days: int):
- yf_symbol = symbol_to_yf(symbol)
- interval = TIMEFRAME_MAP.get(timeframe, "1d")
- period = PERIOD_MAP.get(timeframe, "max")
-
- if lookback_days and lookback_days < 365:
- period = f"{lookback_days}d"
-
- try:
- df = yf.download(yf_symbol, period=period, interval=interval, progress=False)
- if df.empty:
- return []
-
- if hasattr(df.columns, 'droplevel') and df.columns.nlevels > 1:
- df.columns = df.columns.droplevel(1)
-
- candles = []
- for idx, row in df.iterrows():
- candles.append({
- "t": int(idx.timestamp() * 1000),
- "o": round(float(row["Open"]), 4),
- "h": round(float(row["High"]), 4),
- "l": round(float(row["Low"]), 4),
- "c": round(float(row["Close"]), 4),
- "v": round(float(row.get("Volume", 0)), 0),
- })
- return candles
- except Exception as e:
- return []
-
-
-def main():
- if len(sys.argv) < 2:
- print(json.dumps({"success": False, "error": "No params provided"}))
- sys.exit(1)
-
- try:
- params = json.loads(sys.argv[1])
- except json.JSONDecodeError as e:
- print(json.dumps({"success": False, "error": f"Invalid JSON: {e}"}))
- sys.exit(1)
-
- symbols = params.get("symbols", [])
- timeframe = params.get("timeframe", "1d")
- lookback_days = params.get("lookback_days", 365)
-
- data = {}
- for symbol in symbols:
- candles = fetch_symbol(symbol, timeframe, lookback_days)
- if candles:
- data[symbol] = candles
-
- print(json.dumps({"success": True, "data": data}))
-
-
-if __name__ == "__main__":
- main()
diff --git a/fincept-qt/scripts/algo_trading/python_backtest_engine.py b/fincept-qt/scripts/algo_trading/python_backtest_engine.py
deleted file mode 100644
index 6bd78b034..000000000
--- a/fincept-qt/scripts/algo_trading/python_backtest_engine.py
+++ /dev/null
@@ -1,765 +0,0 @@
-"""
-Python Strategy Backtest Engine
-
-Loads and executes QCAlgorithm-compatible Python strategies from the Fincept
-strategy library. Supports multi-symbol backtesting with proper portfolio tracking.
-
-Usage:
- python python_backtest_engine.py \
- --strategy-id FCT-82A09B26 \
- --symbols "AAPL,MSFT,GOOGL" \
- --start-date 2023-01-01 \
- --end-date 2024-01-01 \
- --initial-cash 100000 \
- --data-provider yfinance \
- --parameters '{"fast_period": 10, "slow_period": 20}'
-
-Output:
- JSON with trades, equity curve, and performance metrics
-"""
-
-import json
-import sys
-import argparse
-import os
-import traceback
-import importlib.util
-from datetime import datetime, timedelta
-from typing import Dict, List, Any, Optional
-
-# Debug log collector
-DEBUG_LOG = []
-
-def debug(msg):
- """Add a debug message to the log."""
- DEBUG_LOG.append(f"[py:python_backtest] {msg}")
-
-debug("Script starting...")
-debug(f"Python version: {sys.version}")
-debug(f"Working directory: {os.getcwd()}")
-
-# Add strategies folder to path
-SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
-STRATEGIES_DIR = os.path.join(os.path.dirname(SCRIPT_DIR), "strategies")
-sys.path.insert(0, STRATEGIES_DIR)
-debug(f"Strategies dir: {STRATEGIES_DIR}")
-
-try:
- import pandas as pd
- import numpy as np
- debug(f"pandas version: {pd.__version__}, numpy version: {np.__version__}")
-except ImportError as e:
- debug(f"ERROR: Required library not installed: {e}")
- print(json.dumps({
- 'success': False,
- 'error': f'Required library not installed: {e}',
- 'debug': DEBUG_LOG
- }))
- sys.exit(1)
-
-
-def load_registry() -> Dict[str, Any]:
- """Load the strategy registry."""
- registry_path = os.path.join(STRATEGIES_DIR, "_registry.py")
- debug(f"Loading registry from: {registry_path}")
-
- if not os.path.exists(registry_path):
- raise FileNotFoundError(f"Registry not found: {registry_path}")
-
- # Execute registry file to get STRATEGY_REGISTRY dict
- registry_globals = {}
- with open(registry_path, 'r', encoding='utf-8') as f:
- exec(f.read(), registry_globals)
-
- return registry_globals.get('STRATEGY_REGISTRY', {})
-
-
-def _is_strategy_class(obj, name: str) -> bool:
- """Check if an object is a valid QCAlgorithm strategy class."""
- if not isinstance(obj, type):
- return False
- if name == 'QCAlgorithm':
- return False
- # Must be a QCAlgorithm subclass (not just any class with initialize)
- try:
- from fincept_engine.algorithm import QCAlgorithm as _QCA
- if issubclass(obj, _QCA) and obj is not _QCA:
- return True
- except ImportError:
- pass
- # Fallback: check for both initialize and on_data (strategy signature)
- if hasattr(obj, 'initialize') and hasattr(obj, 'on_data'):
- return True
- return False
-
-
-def load_strategy_class(strategy_id: str, custom_code: Optional[str] = None):
- """Load a strategy class by ID or from custom code."""
- if custom_code:
- debug("Loading from custom code")
- # Execute custom code to get the strategy class
- strategy_globals = {}
- exec(custom_code, strategy_globals)
- # Find the QCAlgorithm subclass
- for name, obj in strategy_globals.items():
- if _is_strategy_class(obj, name):
- return obj
- raise ValueError("No valid QCAlgorithm subclass found in custom code")
-
- registry = load_registry()
- if strategy_id not in registry:
- raise ValueError(f"Strategy not found: {strategy_id}")
-
- strategy_info = registry[strategy_id]
- strategy_path = os.path.join(STRATEGIES_DIR, strategy_info['path'])
- debug(f"Loading strategy from: {strategy_path}")
-
- if not os.path.exists(strategy_path):
- raise FileNotFoundError(f"Strategy file not found: {strategy_path}")
-
- # Load the module
- spec = importlib.util.spec_from_file_location(strategy_info['name'], strategy_path)
- module = importlib.util.module_from_spec(spec)
-
- # Execute the module
- spec.loader.exec_module(module)
-
- # Find the QCAlgorithm subclass — prefer the class matching the file name
- expected_class = strategy_info['name']
- candidates = []
- for name in dir(module):
- obj = getattr(module, name)
- if _is_strategy_class(obj, name):
- if name == expected_class:
- debug(f"Found exact match class: {name}")
- return obj
- candidates.append((name, obj))
-
- if candidates:
- debug(f"Found {len(candidates)} candidate classes: {[c[0] for c in candidates]}, using first")
- return candidates[0][1]
-
- raise ValueError(f"No valid QCAlgorithm subclass found in {strategy_info['path']}")
-
-
-def fetch_historical_data(symbols: List[str], start_date: str, end_date: str,
- provider: str = 'yfinance') -> Dict[str, pd.DataFrame]:
- """Fetch historical data for multiple symbols."""
- debug(f"Fetching data: symbols={symbols}, start={start_date}, end={end_date}, provider={provider}")
- data = {}
-
- if provider == 'yfinance':
- try:
- import yfinance as yf
- debug(f"yfinance version: {yf.__version__}")
-
- for symbol in symbols:
- debug(f"Fetching {symbol}...")
- ticker = yf.Ticker(symbol)
- df = ticker.history(start=start_date, end=end_date)
-
- if df.empty:
- debug(f"No data for {symbol}")
- continue
-
- # Normalize column names
- df.columns = [c.lower() for c in df.columns]
- df = df[['open', 'high', 'low', 'close', 'volume']].copy()
- df.index = pd.to_datetime(df.index)
- data[symbol.upper()] = df
- debug(f"Fetched {len(df)} bars for {symbol}")
- except ImportError:
- raise ImportError("yfinance not installed. Install with: pip install yfinance")
- else:
- raise ValueError(f"Unsupported data provider: {provider}")
-
- return data
-
-
-class EmptyDataDict:
- """Empty placeholder for option_chains / future_chains / quote_bars etc.
- Supports dict-like access plus QuantConnect-style .contains_key() / .ContainsKey()."""
- def __contains__(self, item):
- return False
- def __getitem__(self, key):
- return None
- def __len__(self):
- return 0
- def __bool__(self):
- return False
- def __iter__(self):
- return iter([])
- def keys(self):
- return []
- def values(self):
- return []
- def items(self):
- return []
- def get(self, key, default=None):
- return default
- def contains_key(self, key) -> bool:
- return False
- def ContainsKey(self, key) -> bool:
- return False
-
-_EMPTY_DATA = EmptyDataDict()
-
-
-class BacktestSlice:
- """Represents a single time slice of market data."""
-
- def __init__(self, timestamp: datetime, bars: Dict[str, Any]):
- self._timestamp = timestamp
- self._bars = bars
- # Many strategies access slice.bars or slice.Bars
- self.bars = self
- self.Bars = self
- # Derivative data stubs (strategies check these)
- self.option_chains = _EMPTY_DATA
- self.OptionChains = _EMPTY_DATA
- self.future_chains = _EMPTY_DATA
- self.FutureChains = _EMPTY_DATA
- self.quote_bars = _EMPTY_DATA
- self.QuoteBars = _EMPTY_DATA
- self.ticks = _EMPTY_DATA
- self.Ticks = _EMPTY_DATA
- self.splits = _EMPTY_DATA
- self.Splits = _EMPTY_DATA
- self.dividends = _EMPTY_DATA
- self.Dividends = _EMPTY_DATA
- self.delistings = _EMPTY_DATA
- self.Delistings = _EMPTY_DATA
- self.symbol_changed_events = _EMPTY_DATA
- self.SymbolChangedEvents = _EMPTY_DATA
- self.has_data = len(bars) > 0
-
- @property
- def time(self) -> datetime:
- return self._timestamp
-
- @property
- def Time(self) -> datetime:
- return self._timestamp
-
- def __contains__(self, symbol):
- return str(symbol).upper() in self._bars
-
- def __getitem__(self, symbol):
- ticker = str(symbol).upper()
- if ticker not in self._bars:
- return None
- return self._bars[ticker]
-
- def __len__(self):
- return len(self._bars)
-
- def __bool__(self):
- return len(self._bars) > 0
-
- def keys(self):
- return self._bars.keys()
-
- def values(self):
- return self._bars.values()
-
- def items(self):
- return self._bars.items()
-
- def get(self, symbol, default=None):
- ticker = str(symbol).upper()
- return self._bars.get(ticker, default)
-
- def contains_key(self, symbol) -> bool:
- return str(symbol).upper() in self._bars
-
- def ContainsKey(self, symbol) -> bool:
- return self.contains_key(symbol)
-
- @property
- def count(self) -> int:
- return len(self._bars)
-
- Count = property(lambda self: self.count)
-
-
-class BacktestBar:
- """Represents a single OHLCV bar."""
-
- def __init__(self, symbol: str, row: pd.Series, timestamp: datetime):
- self.symbol = symbol
- self.time = timestamp
- self.open = float(row['open'])
- self.high = float(row['high'])
- self.low = float(row['low'])
- self.close = float(row['close'])
- self.volume = float(row['volume'])
- self.price = self.close
-
-
-def run_backtest(
- strategy_class,
- historical_data: Dict[str, pd.DataFrame],
- initial_cash: float,
- parameters: Dict[str, str],
- start_date: str,
- end_date: str
-) -> Dict[str, Any]:
- """Execute the backtest."""
- debug(f"Starting backtest with {len(historical_data)} symbols")
-
- # Create algorithm instance
- algo = strategy_class()
-
- # Set parameters
- algo.set_parameters(parameters)
-
- # Override dates and cash
- algo.set_start_date(datetime.strptime(start_date, '%Y-%m-%d'))
- algo.set_end_date(datetime.strptime(end_date, '%Y-%m-%d'))
- algo.set_cash(initial_cash)
-
- # Combine all data into a single timeline
- all_dates = set()
- for symbol, df in historical_data.items():
- all_dates.update(df.index.tolist())
- all_dates = sorted(all_dates)
- debug(f"Total trading days: {len(all_dates)}")
-
- # Monkey-patch register_indicator to capture symbol→indicator mappings
- _indicator_symbol_map: Dict[str, str] = {} # indicator_name → symbol
- _original_register = algo.register_indicator
-
- def _patched_register(symbol, indicator, resolution_or_consolidator=None, selector=None):
- _original_register(symbol, indicator, resolution_or_consolidator, selector)
- ind_name = getattr(indicator, 'name', str(id(indicator)))
- sym_str = str(symbol).upper()
- _indicator_symbol_map[ind_name] = sym_str
-
- algo.register_indicator = _patched_register
-
- # Initialize algorithm
- debug("Calling initialize()...")
- try:
- algo.initialize()
- except Exception as e:
- debug(f"initialize() error: {e}\n{traceback.format_exc()}")
- return {
- 'success': False,
- 'error': f'Strategy initialization failed: {e}',
- 'trades': [],
- 'equity_curve': [],
- 'metrics': {}
- }
-
- # Re-apply initial cash AFTER initialize() since strategies often override set_cash
- # This ensures the backtest uses the user-specified capital
- algo.set_cash(initial_cash)
- debug(f"Cash after re-apply: {algo.portfolio.cash}")
-
- # Normalize _indicators to dict if a strategy stored it as a list
- if isinstance(algo._indicators, list):
- ind_dict = {}
- for ind in algo._indicators:
- name = getattr(ind, 'name', str(id(ind)))
- ind_dict[name] = ind
- algo._indicators = ind_dict
-
- # Build indicator→symbol map from registered indicators
- data_symbols = [s.upper() for s in historical_data.keys()]
- default_symbol = data_symbols[0] if data_symbols else None
-
- for ind_key, indicator in algo._indicators.items():
- if ind_key in _indicator_symbol_map:
- continue # Already mapped via register_indicator
- # Try to extract symbol from indicator key (e.g. "MACD_SPY", "SMA_SPY_20")
- for sym in data_symbols:
- if sym in ind_key.upper():
- _indicator_symbol_map[ind_key] = sym
- break
- if ind_key not in _indicator_symbol_map and default_symbol:
- _indicator_symbol_map[ind_key] = default_symbol
-
- debug(f"Indicator→symbol map ({len(_indicator_symbol_map)} indicators): {list(_indicator_symbol_map.keys())[:10]}")
-
- # Track trades and equity
- trades = []
- equity_curve = []
- trade_id = 0
-
- # Add securities that exist in data
- for symbol in historical_data.keys():
- algo.add_equity(symbol)
-
- # Initialize security prices
- for symbol, df in historical_data.items():
- if len(df) > 0:
- sec = algo.securities[symbol]
- sec.price = float(df['close'].iloc[0])
- sec.close = sec.price
- sec.has_data = True
-
- # Track entry info per symbol for trade recording
- entry_info: Dict[str, Dict] = {}
-
- # Walk through each bar
- for i, timestamp in enumerate(all_dates):
- # Update algorithm time
- algo._time = pd.Timestamp(timestamp).to_pydatetime()
-
- # Build slice for this timestamp
- bars = {}
- for symbol, df in historical_data.items():
- if timestamp in df.index:
- row = df.loc[timestamp]
- bar = BacktestBar(symbol, row, timestamp)
- bars[symbol] = bar
- # Update security price
- if symbol in algo.securities:
- sec = algo.securities[symbol]
- sec.price = bar.close
- sec.close = bar.close
- sec.open = bar.open
- sec.high = bar.high
- sec.low = bar.low
- sec.volume = bar.volume
- sec.has_data = True
-
- slice_data = BacktestSlice(timestamp, bars)
-
- # Update portfolio holdings' market prices so equity tracks correctly
- price_updates = {sym: bar.close for sym, bar in bars.items()}
- if price_updates:
- algo.portfolio.update_market_prices(price_updates)
-
- # Update registered indicators with new bar data
- for ind_key, indicator in algo._indicators.items():
- if not hasattr(indicator, 'update'):
- continue
- sym = _indicator_symbol_map.get(ind_key)
- if sym and sym in bars:
- try:
- bar = bars[sym]
- # Pass full bar to indicators that need OHLC (e.g. ATR)
- if hasattr(indicator, 'update_bar') and hasattr(bar, 'high'):
- indicator.update(bar)
- else:
- indicator.update(timestamp, bar.close)
- except Exception:
- pass
-
- # Record positions before on_data
- positions_before = {sym: algo.portfolio[sym].quantity for sym in historical_data.keys()}
-
- # Call on_data
- try:
- algo.on_data(slice_data)
- except Exception as e:
- debug(f"on_data() error at {timestamp}: {e}")
- # Continue execution despite errors
-
- # Check for position changes (trades)
- for symbol in historical_data.keys():
- pos_before = positions_before.get(symbol, 0)
- pos_after = algo.portfolio[symbol].quantity
-
- if pos_before != pos_after:
- qty_change = pos_after - pos_before
- price = algo.securities[symbol].price if symbol in algo.securities else 0
-
- if qty_change > 0: # Entry or add
- if symbol not in entry_info:
- entry_info[symbol] = {
- 'entry_time': timestamp,
- 'entry_price': price,
- 'entry_bar': i,
- 'quantity': qty_change
- }
- else:
- # Adding to position - average price
- old_qty = entry_info[symbol]['quantity']
- old_price = entry_info[symbol]['entry_price']
- new_qty = old_qty + qty_change
- entry_info[symbol]['entry_price'] = (old_price * old_qty + price * qty_change) / new_qty
- entry_info[symbol]['quantity'] = new_qty
- else: # Exit or reduce
- if symbol in entry_info:
- entry = entry_info[symbol]
- exit_qty = min(abs(qty_change), entry['quantity'])
- pnl = (price - entry['entry_price']) * exit_qty
-
- trade_id += 1
- trades.append({
- 'id': str(trade_id),
- 'symbol': symbol,
- 'side': 'BUY',
- 'quantity': exit_qty,
- 'entry_price': round(entry['entry_price'], 2),
- 'exit_price': round(price, 2),
- 'pnl': round(pnl, 2),
- 'entry_time': entry['entry_time'].isoformat() if hasattr(entry['entry_time'], 'isoformat') else str(entry['entry_time']),
- 'exit_time': timestamp.isoformat() if hasattr(timestamp, 'isoformat') else str(timestamp),
- 'bars_held': i - entry['entry_bar']
- })
-
- # Update or remove entry info
- remaining = entry['quantity'] - exit_qty
- if remaining <= 0:
- del entry_info[symbol]
- else:
- entry_info[symbol]['quantity'] = remaining
-
- # Update portfolio market prices again after on_data (fills may have changed holdings)
- if price_updates:
- algo.portfolio.update_market_prices(price_updates)
-
- # Record equity
- equity = algo.portfolio.total_portfolio_value
- equity_curve.append({
- 'time': timestamp.isoformat() if hasattr(timestamp, 'isoformat') else str(timestamp),
- 'value': round(equity, 2)
- })
-
- # Call on_end_of_algorithm
- try:
- algo.on_end_of_algorithm()
- except Exception as e:
- debug(f"on_end_of_algorithm() error: {e}")
-
- # Close any remaining positions
- for symbol, entry in list(entry_info.items()):
- if symbol in algo.securities:
- price = algo.securities[symbol].price
- pnl = (price - entry['entry_price']) * entry['quantity']
-
- trade_id += 1
- trades.append({
- 'id': str(trade_id),
- 'symbol': symbol,
- 'side': 'BUY',
- 'quantity': entry['quantity'],
- 'entry_price': round(entry['entry_price'], 2),
- 'exit_price': round(price, 2),
- 'pnl': round(pnl, 2),
- 'entry_time': entry['entry_time'].isoformat() if hasattr(entry['entry_time'], 'isoformat') else str(entry['entry_time']),
- 'exit_time': all_dates[-1].isoformat() if hasattr(all_dates[-1], 'isoformat') else str(all_dates[-1]),
- 'bars_held': len(all_dates) - entry['entry_bar']
- })
-
- # Calculate metrics
- metrics = calculate_metrics(trades, equity_curve, initial_cash)
-
- return {
- 'success': True,
- 'trades': trades,
- 'equity_curve': equity_curve[-100:], # Last 100 points for chart
- 'metrics': metrics,
- 'debug': DEBUG_LOG
- }
-
-
-def calculate_metrics(trades: List[Dict], equity_curve: List[Dict], initial_cash: float) -> Dict[str, Any]:
- """Calculate backtest performance metrics."""
- if not equity_curve:
- return {
- 'total_trades': 0,
- 'winning_trades': 0,
- 'losing_trades': 0,
- 'win_rate': 0,
- 'total_return': 0,
- 'total_return_pct': 0,
- 'max_drawdown': 0,
- 'max_drawdown_pct': 0,
- 'sharpe_ratio': 0,
- 'profit_factor': 0,
- 'avg_pnl': 0,
- 'avg_bars_held': 0
- }
-
- final_equity = equity_curve[-1]['value']
- total_return = final_equity - initial_cash
- total_return_pct = (total_return / initial_cash) * 100
-
- # Max drawdown
- peak = initial_cash
- max_dd = 0
- max_dd_pct = 0
- for point in equity_curve:
- equity = point['value']
- if equity > peak:
- peak = equity
- dd = peak - equity
- dd_pct = (dd / peak) * 100 if peak > 0 else 0
- if dd_pct > max_dd_pct:
- max_dd = dd
- max_dd_pct = dd_pct
-
- # Trade statistics
- total_trades = len(trades)
- if total_trades == 0:
- return {
- 'total_trades': 0,
- 'winning_trades': 0,
- 'losing_trades': 0,
- 'win_rate': 0,
- 'total_return': round(total_return, 2),
- 'total_return_pct': round(total_return_pct, 2),
- 'max_drawdown': round(max_dd, 2),
- 'max_drawdown_pct': round(max_dd_pct, 2),
- 'sharpe_ratio': 0,
- 'profit_factor': 0,
- 'avg_pnl': 0,
- 'avg_bars_held': 0
- }
-
- wins = [t for t in trades if t['pnl'] > 0]
- losses = [t for t in trades if t['pnl'] <= 0]
- win_rate = (len(wins) / total_trades) * 100
-
- gross_profit = sum(t['pnl'] for t in wins) if wins else 0
- gross_loss = abs(sum(t['pnl'] for t in losses)) if losses else 0
- profit_factor = (gross_profit / gross_loss) if gross_loss > 0 else (999.99 if gross_profit > 0 else 0)
-
- avg_pnl = sum(t['pnl'] for t in trades) / total_trades
- avg_bars_held = sum(t['bars_held'] for t in trades) / total_trades
-
- # Sharpe ratio (simplified daily calculation)
- if len(equity_curve) > 1:
- returns = []
- for i in range(1, len(equity_curve)):
- prev = equity_curve[i-1]['value']
- curr = equity_curve[i]['value']
- if prev > 0:
- returns.append((curr - prev) / prev)
-
- if returns:
- mean_ret = np.mean(returns)
- std_ret = np.std(returns)
- sharpe = (mean_ret / std_ret) * np.sqrt(252) if std_ret > 0 else 0
- else:
- sharpe = 0
- else:
- sharpe = 0
-
- return {
- 'total_trades': total_trades,
- 'winning_trades': len(wins),
- 'losing_trades': len(losses),
- 'win_rate': round(win_rate, 1),
- 'total_return': round(total_return, 2),
- 'total_return_pct': round(total_return_pct, 2),
- 'max_drawdown': round(max_dd, 2),
- 'max_drawdown_pct': round(max_dd_pct, 2),
- 'sharpe_ratio': round(sharpe, 2),
- 'profit_factor': round(min(profit_factor, 999.99), 2),
- 'avg_pnl': round(avg_pnl, 2),
- 'avg_bars_held': round(avg_bars_held, 1)
- }
-
-
-def main():
- debug("main() started")
- debug(f"sys.argv: {sys.argv}")
-
- parser = argparse.ArgumentParser(description='Python Strategy Backtest Engine')
- parser.add_argument('--strategy-id', required=True, help='Strategy ID (e.g., FCT-82A09B26)')
- parser.add_argument('--custom-code', default=None, help='Optional custom strategy code (base64 or file path)')
- parser.add_argument('--symbols', required=True, help='Comma-separated symbols')
- parser.add_argument('--start-date', required=True, help='Start date (YYYY-MM-DD)')
- parser.add_argument('--end-date', required=True, help='End date (YYYY-MM-DD)')
- parser.add_argument('--initial-cash', type=float, default=100000, help='Starting capital')
- parser.add_argument('--data-provider', default='yfinance', help='Data provider: yfinance')
- parser.add_argument('--parameters', default='{}', help='JSON parameters for strategy')
-
- try:
- args = parser.parse_args()
- debug(f"Parsed args: strategy_id={args.strategy_id}, symbols={args.symbols}")
- debug(f"start_date={args.start_date}, end_date={args.end_date}, initial_cash={args.initial_cash}")
- except Exception as e:
- debug(f"Argument parsing error: {e}")
- print(json.dumps({'success': False, 'error': f'Argument parsing error: {e}', 'debug': DEBUG_LOG}))
- sys.exit(1)
-
- # Parse symbols
- symbols = [s.strip().upper() for s in args.symbols.split(',') if s.strip()]
- if not symbols:
- print(json.dumps({'success': False, 'error': 'No symbols provided', 'debug': DEBUG_LOG}))
- sys.exit(1)
- debug(f"Symbols: {symbols}")
-
- # Parse parameters
- try:
- parameters = json.loads(args.parameters)
- except json.JSONDecodeError as e:
- debug(f"Parameters JSON error: {e}")
- parameters = {}
- debug(f"Parameters: {parameters}")
-
- # Load strategy class
- try:
- custom_code = None
- if args.custom_code:
- if os.path.isfile(args.custom_code):
- with open(args.custom_code, 'r', encoding='utf-8') as f:
- custom_code = f.read()
- else:
- # Assume base64 encoded
- import base64
- custom_code = base64.b64decode(args.custom_code).decode('utf-8')
-
- strategy_class = load_strategy_class(args.strategy_id, custom_code)
- debug(f"Loaded strategy class: {strategy_class.__name__}")
- except Exception as e:
- debug(f"Strategy loading error: {e}\n{traceback.format_exc()}")
- print(json.dumps({
- 'success': False,
- 'error': f'Failed to load strategy: {e}',
- 'debug': DEBUG_LOG
- }))
- sys.exit(1)
-
- # Fetch historical data
- try:
- historical_data = fetch_historical_data(
- symbols, args.start_date, args.end_date, args.data_provider
- )
- if not historical_data:
- print(json.dumps({
- 'success': False,
- 'error': 'No historical data available for the specified symbols',
- 'debug': DEBUG_LOG
- }))
- sys.exit(1)
- except Exception as e:
- debug(f"Data fetching error: {e}\n{traceback.format_exc()}")
- print(json.dumps({
- 'success': False,
- 'error': f'Failed to fetch historical data: {e}',
- 'debug': DEBUG_LOG
- }))
- sys.exit(1)
-
- # Run backtest
- try:
- result = run_backtest(
- strategy_class=strategy_class,
- historical_data=historical_data,
- initial_cash=args.initial_cash,
- parameters=parameters,
- start_date=args.start_date,
- end_date=args.end_date
- )
- except Exception as e:
- debug(f"Backtest error: {e}\n{traceback.format_exc()}")
- print(json.dumps({
- 'success': False,
- 'error': f'Backtest execution failed: {e}',
- 'debug': DEBUG_LOG
- }))
- sys.exit(1)
-
- debug(f"Backtest completed: {result.get('metrics', {}).get('total_trades', 'N/A')} trades")
- print(json.dumps(result))
-
-
-if __name__ == '__main__':
- main()
diff --git a/fincept-qt/scripts/optimize_portfolio_weights.py b/fincept-qt/scripts/optimize_portfolio_weights.py
index 081d63006..237967cca 100644
--- a/fincept-qt/scripts/optimize_portfolio_weights.py
+++ b/fincept-qt/scripts/optimize_portfolio_weights.py
@@ -312,6 +312,8 @@ def main():
"target return": "target_return",
"black_litterman": "black_litterman",
"b-l model": "black_litterman",
+ "b_l_model": "black_litterman", # "B-L Model" → lower → replace(-,_)
+ "b-l_model": "black_litterman",
}
raw_method = params.get("method", "max_sharpe").lower().replace("-", "_")
strategy = method_map.get(raw_method, raw_method)
@@ -368,6 +370,50 @@ def main():
except Exception:
pass
+ # ── Risk decomposition for the primary weights ────────────────────────────
+ # Marginal risk contribution MRC = (Σ·w)/σ_p ; risk contribution RC = w·MRC.
+ # Σ RC = σ_p, so RC%/asset = RC/σ_p. Reported as % of total portfolio risk.
+ risk_contributions = {}
+ marginal_risk = {}
+ asset_volatility = {}
+ try:
+ w_p = np.asarray(weights_arr, dtype=float)
+ sigma_p = float(np.sqrt(max(np.dot(w_p, np.dot(cov, w_p)), 0.0)))
+ cov_w = np.dot(cov, w_p)
+ mrc = cov_w / sigma_p if sigma_p > 1e-12 else np.zeros(n)
+ rc = w_p * mrc
+ diag = np.sqrt(np.clip(np.diag(cov), 0.0, None))
+ for i in range(n):
+ sym = available[i]
+ asset_volatility[sym] = float(diag[i]) # annualised vol
+ marginal_risk[sym] = float(mrc[i])
+ risk_contributions[sym] = float(rc[i] / sigma_p * 100.0) if sigma_p > 1e-12 else 0.0
+ except Exception:
+ pass
+
+ # ── Black-Litterman market-implied equilibrium returns ────────────────────
+ # π = δ · Σ · w_market, using the caller's current weights as the market
+ # proxy (falls back to equal weight). Lets the B-L tab show what returns the
+ # market is implicitly pricing in before any investor views are applied.
+ implied_returns = {}
+ try:
+ in_weights = params.get("weights", [])
+ w_mkt = np.zeros(n)
+ if isinstance(in_weights, list) and len(in_weights) == len(symbols):
+ wmap = {symbols[i]: float(in_weights[i]) for i in range(len(symbols))}
+ for i in range(n):
+ w_mkt[i] = wmap.get(available[i], 0.0)
+ if w_mkt.sum() <= 1e-9:
+ w_mkt = np.ones(n) / n
+ else:
+ w_mkt = w_mkt / w_mkt.sum()
+ delta = 2.5 # risk-aversion coefficient
+ pi = delta * np.dot(cov, w_mkt)
+ for i in range(n):
+ implied_returns[available[i]] = float(pi[i])
+ except Exception:
+ pass
+
output = {
"weights": weights_dict,
"expected_annual_return": primary["return"],
@@ -377,6 +423,10 @@ def main():
"symbols": available,
"frontier": frontier,
"comparison": comparison,
+ "risk_contributions": risk_contributions,
+ "marginal_risk": marginal_risk,
+ "asset_volatility": asset_volatility,
+ "implied_returns": implied_returns,
}
print(json.dumps(convert_numpy(output)))
diff --git a/fincept-qt/scripts/yfinance_data.py b/fincept-qt/scripts/yfinance_data.py
index 2a20549db..26b47cb6f 100644
--- a/fincept-qt/scripts/yfinance_data.py
+++ b/fincept-qt/scripts/yfinance_data.py
@@ -6,6 +6,7 @@ Returns JSON output for Qt/C++ integration
import sys
import json
+import re
import yfinance as yf
import pandas as pd
from datetime import datetime
@@ -315,6 +316,66 @@ def get_batch_quotes(symbols):
results.append(quote)
return results
+# Month tokens used by futures shortNames, e.g. "Gold Jun 26" / "Crude Oil Jul 26".
+# We strip the contract-month suffix so the panel shows "Gold", not "Gold Jun 26".
+_FUTURES_MONTH_RE = re.compile(
+ r'\s+(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s+\d{2,4}$')
+
+def _clean_display_name(symbol, info):
+ """Pick the most human-readable name yfinance offers for `symbol`.
+
+ Preference: longName (fuller, e.g. "Invesco QQQ Trust") over shortName
+ (often truncated to ~30 chars). Falls back to shortName, then "".
+ Futures (`=F`) have no longName and a month-stamped shortName
+ ("Gold Jun 26") — strip the contract month so panels show "Gold".
+ """
+ name = (info.get('longName') or info.get('shortName') or '').strip()
+ if not name:
+ return ''
+ if symbol.endswith('=F'):
+ name = _FUTURES_MONTH_RE.sub('', name).strip()
+ return name
+
+def get_quote_names(symbols):
+ """Resolve display names + quote currency for symbols via yfinance .info.
+
+ Returns {"names": {symbol: name}, "currencies": {symbol: code}}. Both are
+ static per symbol, so the C++ caller caches the result and only asks for
+ symbols it hasn't resolved yet — the per-symbol .info cost is paid once,
+ never on the quote-refresh path. Symbols yfinance can't name/price are
+ simply omitted (caller keeps the raw ticker / no prefix as fallback).
+
+ Currency is omitted for forex pairs (quoteType CURRENCY — the price is a
+ rate, not a monetary amount) and for indices/yields (quoteType INDEX —
+ e.g. a currency symbol on the S&P level or the 10Y yield is misleading),
+ so callers only ever prefix a currency where it's meaningful.
+ """
+ import io, contextlib, logging
+ yf_logger = logging.getLogger("yfinance")
+ prev_level = yf_logger.level
+ yf_logger.setLevel(logging.CRITICAL)
+ names = {}
+ currencies = {}
+ try:
+ for symbol in symbols:
+ try:
+ _buf = io.StringIO()
+ with contextlib.redirect_stdout(_buf), contextlib.redirect_stderr(_buf):
+ info = yf.Ticker(symbol).info
+ info = info or {}
+ name = _clean_display_name(symbol, info)
+ if name:
+ names[symbol] = name
+ quote_type = (info.get("quoteType") or "").upper()
+ currency = info.get("currency")
+ if currency and quote_type not in ("CURRENCY", "INDEX"):
+ currencies[symbol] = currency
+ except Exception:
+ continue
+ finally:
+ yf_logger.setLevel(prev_level)
+ return {"names": names, "currencies": currencies}
+
def get_batch_sparklines(symbols, period="5d", interval="1h"):
"""Fetch close price series for multiple symbols — used for blotter sparklines.
Returns: {"AAPL": [170.1, 171.3, ...], "MSFT": [...], ...}
@@ -1142,6 +1203,13 @@ def main(args=None):
symbols = args[1:]
result = get_batch_sparklines(symbols)
+ elif command == "quote_names":
+ if len(args) < 2:
+ result = {"error": "Usage: python yfinance_data.py quote_names <sym1> <sym2> ..."}
+ else:
+ symbols = args[1:]
+ result = get_quote_names(symbols)
+
elif command == "batch_all":
# Unified hub-refresh endpoint: payload is a single JSON blob on argv[1]
# (or @tempfile via PythonRunner's arg-spill) describing quotes + sparklines
diff --git a/fincept-qt/src/algo_engine/BacktestEngine.cpp b/fincept-qt/src/algo_engine/BacktestEngine.cpp
new file mode 100644
index 000000000..425ff9d7d
--- /dev/null
+++ b/fincept-qt/src/algo_engine/BacktestEngine.cpp
@@ -0,0 +1,276 @@
+// src/algo_engine/BacktestEngine.cpp
+#include "algo_engine/BacktestEngine.h"
+
+#include "algo_engine/ConditionEvaluator.h"
+
+#include <algorithm>
+#include <cmath>
+
+namespace fincept::algo {
+
+namespace {
+
+// Bars required before any evaluation (indicator warm-up). Matches the old
+// Python engine's WARMUP_BARS so results are comparable in shape.
+constexpr int kWarmupBars = 50;
+
+// Trailing window passed to the indicator/condition evaluator each bar. Bounds
+// per-bar cost to O(window) instead of O(i) — 500 bars is ample warm-up for any
+// indicator in the catalog (largest typical period ~200).
+constexpr int kEvalWindow = 500;
+
+double round_to(double v, int decimals) {
+ double f = std::pow(10.0, decimals);
+ return std::round(v * f) / f;
+}
+
+// Bars per year for timeframe-aware Sharpe annualisation (mirrors the old engine).
+double bars_per_year(const QString& tf) {
+ if (tf == "1m") return 252.0 * 390.0;
+ if (tf == "3m") return 252.0 * 130.0;
+ if (tf == "5m") return 252.0 * 78.0;
+ if (tf == "15m") return 252.0 * 26.0;
+ if (tf == "30m") return 252.0 * 13.0;
+ if (tf == "1h") return 252.0 * 6.5;
+ if (tf == "4h") return 252.0 * 1.625;
+ if (tf == "1d") return 252.0;
+ return 252.0;
+}
+
+} // namespace
+
+QJsonObject BacktestEngine::run(const QVector<OhlcvCandle>& candles,
+ const QJsonArray& entry_conditions, const QString& entry_logic,
+ const QJsonArray& exit_conditions, const QString& exit_logic,
+ double stop_loss_pct, double take_profit_pct, double trailing_stop_pct,
+ double initial_capital, const QString& timeframe) {
+ const int n = candles.size();
+ if (n < kWarmupBars + 10) {
+ QJsonObject err;
+ err["success"] = false;
+ err["error"] = QString("Insufficient data: %1 candles (need at least %2)")
+ .arg(n)
+ .arg(kWarmupBars + 10);
+ return err;
+ }
+
+ double cash = initial_capital;
+ bool in_pos = false;
+ double entry_price = 0.0;
+ int entry_bar = 0;
+ long long shares = 0;
+ double highest = 0.0; // high-watermark for trailing stop (long)
+
+ // Signals latched on the close of bar i, executed at the open of bar i+1.
+ bool entry_signal = false;
+ bool exit_signal = false;
+
+ QJsonArray trades;
+ QVector<double> equity_curve;
+ equity_curve.reserve(n - kWarmupBars);
+ double peak_equity = initial_capital;
+ double max_dd = 0.0;
+
+ auto close_trade = [&](double exit_price, const char* reason, int exit_bar) {
+ const double pnl = (exit_price - entry_price) * static_cast<double>(shares);
+ const double pnl_pct = entry_price > 0 ? (exit_price - entry_price) / entry_price * 100.0 : 0.0;
+ cash += static_cast<double>(shares) * exit_price;
+ QJsonObject t;
+ t["entry_bar"] = entry_bar;
+ t["exit_bar"] = exit_bar;
+ t["entry_price"] = round_to(entry_price, 2);
+ t["exit_price"] = round_to(exit_price, 2);
+ t["shares"] = static_cast<double>(shares);
+ t["pnl"] = round_to(pnl, 2);
+ t["pnl_pct"] = round_to(pnl_pct, 2);
+ t["reason"] = QString::fromLatin1(reason);
+ t["bars_held"] = exit_bar - entry_bar;
+ trades.append(t);
+ in_pos = false;
+ shares = 0;
+ };
+
+ for (int i = kWarmupBars; i < n; ++i) {
+ const OhlcvCandle& bar = candles[i];
+
+ // ── 1. Execute pending signal fills at THIS bar's open ──────────────
+ if (!in_pos && entry_signal) {
+ const double px = bar.open;
+ const long long qty = px > 0 ? static_cast<long long>(std::floor(cash / px)) : 0;
+ if (qty > 0) {
+ in_pos = true;
+ entry_price = px;
+ entry_bar = i;
+ shares = qty;
+ cash -= static_cast<double>(shares) * px;
+ highest = px;
+ }
+ entry_signal = false;
+ } else if (in_pos && exit_signal) {
+ close_trade(bar.open, "exit_signal", i);
+ exit_signal = false;
+ }
+
+ // ── 2. Intrabar stop-loss / take-profit on THIS bar ─────────────────
+ if (in_pos) {
+ highest = std::max(highest, bar.high);
+
+ bool have_stop = false;
+ double stop_price = 0.0;
+ if (stop_loss_pct > 0) {
+ stop_price = entry_price * (1.0 - stop_loss_pct / 100.0);
+ have_stop = true;
+ }
+ if (trailing_stop_pct > 0) {
+ const double trail = highest * (1.0 - trailing_stop_pct / 100.0);
+ stop_price = have_stop ? std::max(stop_price, trail) : trail;
+ have_stop = true;
+ }
+ const double tp_price = take_profit_pct > 0 ? entry_price * (1.0 + take_profit_pct / 100.0) : 0.0;
+
+ // Stop checked first (conservative when both touched in one bar).
+ if (have_stop && bar.low <= stop_price) {
+ close_trade(stop_price, "stop_loss", i);
+ } else if (take_profit_pct > 0 && bar.high >= tp_price) {
+ close_trade(tp_price, "take_profit", i);
+ }
+ }
+
+ // ── 3. Evaluate conditions on close of bar i → latch for next bar ───
+ const int start = std::max(0, i - kEvalWindow + 1);
+ const QVector<OhlcvCandle> window = candles.mid(start, i - start + 1);
+ if (!in_pos && !entry_signal && !entry_conditions.isEmpty()) {
+ if (ConditionEvaluator::evaluate_group(entry_conditions, entry_logic, window).triggered)
+ entry_signal = true;
+ } else if (in_pos && !exit_signal && !exit_conditions.isEmpty()) {
+ if (ConditionEvaluator::evaluate_group(exit_conditions, exit_logic, window).triggered)
+ exit_signal = true;
+ }
+
+ // ── 4. Mark-to-market equity on close ───────────────────────────────
+ const double equity = cash + (in_pos ? static_cast<double>(shares) * bar.close : 0.0);
+ equity_curve.append(equity);
+ if (equity > peak_equity)
+ peak_equity = equity;
+ const double dd = peak_equity > 0 ? (peak_equity - equity) / peak_equity * 100.0 : 0.0;
+ if (dd > max_dd)
+ max_dd = dd;
+ }
+
+ // Close any open position at the last bar's close.
+ if (in_pos)
+ close_trade(candles[n - 1].close, "end_of_data", n - 1);
+
+ // ── Metrics ─────────────────────────────────────────────────────────────
+ const int total_trades = trades.size();
+ const double final_value = cash;
+ const double total_return = final_value - initial_capital;
+ const double total_return_pct = initial_capital > 0 ? total_return / initial_capital * 100.0 : 0.0;
+
+ QJsonObject out;
+ out["success"] = true;
+
+ if (total_trades == 0) {
+ out["total_trades"] = 0;
+ out["winning_trades"] = 0;
+ out["losing_trades"] = 0;
+ out["win_rate"] = 0.0;
+ out["total_return"] = round_to(total_return_pct, 2);
+ out["total_return_abs"] = round_to(total_return, 2);
+ out["final_value"] = round_to(final_value, 2);
+ out["max_drawdown"] = round_to(max_dd, 2);
+ out["avg_pnl"] = 0.0;
+ out["avg_bars_held"] = 0.0;
+ out["profit_factor"] = 0.0;
+ out["sharpe_ratio"] = 0.0;
+ out["sharpe"] = 0.0;
+ out["equity_curve"] = QJsonArray();
+ out["trades"] = trades;
+ return out;
+ }
+
+ int wins = 0;
+ double gross_profit = 0.0, gross_loss = 0.0, sum_pnl = 0.0;
+ long long sum_bars_held = 0;
+ for (const auto& tv : trades) {
+ const QJsonObject t = tv.toObject();
+ const double pnl = t.value("pnl").toDouble();
+ sum_pnl += pnl;
+ sum_bars_held += t.value("bars_held").toInt();
+ if (pnl > 0) {
+ ++wins;
+ gross_profit += pnl;
+ } else {
+ gross_loss += std::abs(pnl);
+ }
+ }
+ const int losses = total_trades - wins;
+ const double win_rate = static_cast<double>(wins) / total_trades * 100.0;
+ const double avg_pnl = sum_pnl / total_trades;
+ const double avg_bars_held = static_cast<double>(sum_bars_held) / total_trades;
+
+ double profit_factor;
+ if (gross_loss > 0)
+ profit_factor = gross_profit / gross_loss;
+ else
+ profit_factor = gross_profit > 0 ? 999.99 : 0.0;
+ if (profit_factor > 999.99)
+ profit_factor = 999.99;
+
+ // Sharpe from equity-curve point-to-point returns, annualised by timeframe.
+ double sharpe = 0.0;
+ if (equity_curve.size() > 1) {
+ QVector<double> rets;
+ rets.reserve(equity_curve.size() - 1);
+ for (int i = 1; i < equity_curve.size(); ++i) {
+ if (equity_curve[i - 1] != 0.0)
+ rets.append((equity_curve[i] - equity_curve[i - 1]) / equity_curve[i - 1]);
+ }
+ if (!rets.isEmpty()) {
+ double mean = 0.0;
+ for (double r : rets)
+ mean += r;
+ mean /= rets.size();
+ double var = 0.0;
+ for (double r : rets)
+ var += (r - mean) * (r - mean);
+ var /= rets.size();
+ const double sd = std::sqrt(var);
+ if (sd > 0.0)
+ sharpe = (mean / sd) * std::sqrt(bars_per_year(timeframe));
+ }
+ }
+
+ // Downsample equity curve to <= 500 points (keep last point).
+ QJsonArray equity_out;
+ if (equity_curve.size() > 500) {
+ const int step = equity_curve.size() / 500;
+ for (int i = 0; i < equity_curve.size(); i += step)
+ equity_out.append(round_to(equity_curve[i], 2));
+ if (equity_out.isEmpty() ||
+ equity_out.last().toDouble() != round_to(equity_curve.last(), 2))
+ equity_out.append(round_to(equity_curve.last(), 2));
+ } else {
+ for (double e : equity_curve)
+ equity_out.append(round_to(e, 2));
+ }
+
+ out["total_trades"] = total_trades;
+ out["winning_trades"] = wins;
+ out["losing_trades"] = losses;
+ out["win_rate"] = round_to(win_rate, 1);
+ out["total_return"] = round_to(total_return_pct, 2); // panel reads as percent
+ out["total_return_abs"] = round_to(total_return, 2);
+ out["final_value"] = round_to(final_value, 2);
+ out["max_drawdown"] = round_to(max_dd, 2);
+ out["avg_pnl"] = round_to(avg_pnl, 2);
+ out["avg_bars_held"] = round_to(avg_bars_held, 1);
+ out["profit_factor"] = round_to(profit_factor, 2);
+ out["sharpe_ratio"] = round_to(sharpe, 3); // panel reads sharpe_ratio
+ out["sharpe"] = round_to(sharpe, 3); // alias
+ out["equity_curve"] = equity_out;
+ out["trades"] = trades;
+ return out;
+}
+
+} // namespace fincept::algo
diff --git a/fincept-qt/src/algo_engine/BacktestEngine.h b/fincept-qt/src/algo_engine/BacktestEngine.h
new file mode 100644
index 000000000..8d33886c6
--- /dev/null
+++ b/fincept-qt/src/algo_engine/BacktestEngine.h
@@ -0,0 +1,36 @@
+// src/algo_engine/BacktestEngine.h
+#pragma once
+#include "algo_engine/AlgoEngineTypes.h"
+
+#include <QJsonArray>
+#include <QJsonObject>
+#include <QString>
+#include <QVector>
+
+namespace fincept::algo {
+
+/// Event-driven, single-symbol, long-only backtester.
+///
+/// Pure computation: given candles + strategy parameters it returns a FLAT
+/// metrics JSON object matching exactly what
+/// `StrategyBuilderPanel::display_backtest_result` reads
+/// (total_return, sharpe_ratio, max_drawdown, total_trades, win_rate,
+/// profit_factor, final_value), plus equity_curve and the raw trade list.
+///
+/// Fill model (no look-ahead bias):
+/// - Entry/exit SIGNALS are evaluated on the close of bar i.
+/// - A signal fills at the OPEN of bar i+1.
+/// - Stop-loss / take-profit (incl. trailing) are checked intrabar against
+/// each bar's high/low, filling at the stop/target price.
+///
+/// On insufficient data the returned object has {"success": false, "error": …}.
+class BacktestEngine {
+public:
+ static QJsonObject run(const QVector<OhlcvCandle>& candles,
+ const QJsonArray& entry_conditions, const QString& entry_logic,
+ const QJsonArray& exit_conditions, const QString& exit_logic,
+ double stop_loss_pct, double take_profit_pct, double trailing_stop_pct,
+ double initial_capital, const QString& timeframe);
+};
+
+} // namespace fincept::algo
diff --git a/fincept-qt/src/algo_engine/CandleDataFetcher.cpp b/fincept-qt/src/algo_engine/CandleDataFetcher.cpp
index 77394d169..1cc875e69 100644
--- a/fincept-qt/src/algo_engine/CandleDataFetcher.cpp
+++ b/fincept-qt/src/algo_engine/CandleDataFetcher.cpp
@@ -1,31 +1,230 @@
// src/algo_engine/CandleDataFetcher.cpp
#include "algo_engine/CandleDataFetcher.h"
#include "core/logging/Logger.h"
-#include "python/PythonRunner.h"
#include "trading/AccountManager.h"
#include "trading/BrokerRegistry.h"
+#include "trading/instruments/InstrumentService.h"
#include <QDateTime>
#include <QJsonArray>
+#include <QSet>
#include <QJsonDocument>
#include <QJsonObject>
+#include <QNetworkAccessManager>
+#include <QNetworkReply>
+#include <QNetworkRequest>
#include <QPointer>
+#include <QUrl>
#include <QtConcurrent>
+#include <atomic>
+#include <memory>
+
namespace fincept::algo {
+namespace {
+
+// ── Yahoo Finance interval mapping ──────────────────────────────────────────
+// Yahoo's chart API supports 1m, 2m, 5m, 15m, 30m, 60m/1h, 1d, ... but NOT 3m or
+// 4h. For those we fetch the nearest finer native interval and aggregate N→1.
+struct YahooInterval {
+ QString interval; // native Yahoo interval string
+ int aggregate; // how many native bars compose one requested bar (>=1)
+};
+
+YahooInterval yahoo_interval(const QString& tf) {
+ if (tf == "1m") return {QStringLiteral("1m"), 1};
+ if (tf == "3m") return {QStringLiteral("1m"), 3};
+ if (tf == "5m") return {QStringLiteral("5m"), 1};
+ if (tf == "15m") return {QStringLiteral("15m"), 1};
+ if (tf == "30m") return {QStringLiteral("30m"), 1};
+ if (tf == "1h") return {QStringLiteral("60m"), 1};
+ if (tf == "4h") return {QStringLiteral("60m"), 4};
+ if (tf == "1d") return {QStringLiteral("1d"), 1};
+ return {QStringLiteral("1d"), 1};
+}
+
+// Yahoo caps how far back intraday intervals reach. Clamp the request window so
+// we don't get an empty response for asking too much history.
+int yahoo_max_lookback_days(const QString& native_interval) {
+ if (native_interval == "1m") return 7;
+ if (native_interval == "5m" || native_interval == "15m" || native_interval == "30m") return 60;
+ if (native_interval == "60m") return 730;
+ return 100000; // daily / no practical cap
+}
+
+// Mirror the old Python symbol_to_yf: bare symbols are assumed NSE (.NS).
+QString symbol_to_yahoo(const QString& symbol) {
+ const QString s = symbol.trimmed().toUpper();
+ if (s.contains('.') || s.contains('/') || s.contains('-'))
+ return s;
+ return s + QStringLiteral(".NS");
+}
+
+// Aggregate `factor` consecutive bars into one. Drops an incomplete trailing
+// group so we never emit a misleading partial bar.
+QVector<OhlcvCandle> aggregate_candles(const QVector<OhlcvCandle>& in, int factor) {
+ if (factor <= 1)
+ return in;
+ QVector<OhlcvCandle> out;
+ out.reserve(in.size() / factor + 1);
+ for (int i = 0; i + factor <= in.size(); i += factor) {
+ OhlcvCandle c = in[i];
+ c.high = in[i].high;
+ c.low = in[i].low;
+ c.volume = 0;
+ for (int j = 0; j < factor; ++j) {
+ c.high = std::max(c.high, in[i + j].high);
+ c.low = std::min(c.low, in[i + j].low);
+ c.volume += in[i + j].volume;
+ }
+ c.close = in[i + factor - 1].close;
+ c.close_time = in[i + factor - 1].close_time;
+ c.is_closed = true;
+ out.append(c);
+ }
+ return out;
+}
+
+// Parse a Yahoo v8 chart response into OHLCV candles (epoch-ms, close_time set).
+QVector<OhlcvCandle> parse_yahoo_chart(const QJsonObject& root, int64_t tf_ms, QString* err) {
+ QVector<OhlcvCandle> candles;
+ const auto chart = root.value("chart").toObject();
+ if (!chart.value("error").isNull()) {
+ if (err)
+ *err = chart.value("error").toObject().value("description").toString(QStringLiteral("Yahoo error"));
+ return candles;
+ }
+ const auto results = chart.value("result").toArray();
+ if (results.isEmpty()) {
+ if (err)
+ *err = QStringLiteral("Empty result");
+ return candles;
+ }
+ const auto result = results.first().toObject();
+ const auto timestamps = result.value("timestamp").toArray();
+ const auto quote = result.value("indicators").toObject().value("quote").toArray();
+ if (timestamps.isEmpty() || quote.isEmpty()) {
+ if (err)
+ *err = QStringLiteral("No candles");
+ return candles;
+ }
+ const auto q = quote.first().toObject();
+ const auto opens = q.value("open").toArray();
+ const auto highs = q.value("high").toArray();
+ const auto lows = q.value("low").toArray();
+ const auto closes = q.value("close").toArray();
+ const auto volumes = q.value("volume").toArray();
+
+ candles.reserve(timestamps.size());
+ for (int i = 0; i < timestamps.size(); ++i) {
+ // Yahoo emits null for gaps/halts — skip those rows (matches yfinance dropna).
+ if (i >= opens.size() || opens[i].isNull() || highs[i].isNull() || lows[i].isNull() ||
+ closes[i].isNull())
+ continue;
+ OhlcvCandle c;
+ c.open_time = static_cast<int64_t>(timestamps[i].toDouble()) * 1000; // sec → ms
+ c.close_time = c.open_time + tf_ms;
+ c.open = opens[i].toDouble();
+ c.high = highs[i].toDouble();
+ c.low = lows[i].toDouble();
+ c.close = closes[i].toDouble();
+ c.volume = (i < volumes.size() && !volumes[i].isNull()) ? volumes[i].toDouble() : 0.0;
+ c.is_closed = true;
+ candles.append(c);
+ }
+ return candles;
+}
+
+// ── Broker symbol resolution ─────────────────────────────────────────────────
+// Most broker get_history() implementations need more than a bare symbol:
+// - token brokers want EXCHANGE:SYMBOL:TOKEN (numeric instrument_token);
+// - tradejini wants the scrip id at parts[1];
+// - the rest resolve EXCHANGE:SYMBOL internally.
+// We resolve via InstrumentService (warming its cache on a worker thread if
+// needed). Unresolvable token brokers return "" so the caller skips to Yahoo.
+
+const QSet<QString>& brokers_needing_token() {
+ static const QSet<QString> s = {
+ QStringLiteral("motilal"), QStringLiteral("aliceblue"), QStringLiteral("fivepaisa"),
+ QStringLiteral("iifl"), QStringLiteral("ibkr"), QStringLiteral("saxobank"),
+ };
+ return s;
+}
+
+// Worker-thread-safe synchronous instrument load. Uses the worker-safe loader
+// (private named connection); never the main-thread load_from_db().
+bool ensure_instruments_loaded_blocking(const QString& broker_id) {
+ auto& svc = fincept::trading::InstrumentService::instance();
+ if (svc.is_loaded(broker_id))
+ return true;
+ svc.load_from_db_worker(broker_id);
+ return svc.is_loaded(broker_id);
+}
+
+// Build the symbol string <broker_id>::get_history expects. Empty result means a
+// token was required but could not be resolved (caller should fall back to Yahoo).
+QString resolve_broker_symbol(const QString& broker_id, const QString& bare_symbol,
+ const QString& exchange) {
+ using fincept::trading::InstrumentService;
+ if (bare_symbol.contains(':')) // already fully qualified by caller
+ return bare_symbol;
+ const QString sym = bare_symbol.trimmed().toUpper();
+ const QString exch = exchange.isEmpty() ? QStringLiteral("NSE") : exchange.toUpper();
+
+ if (broker_id == QStringLiteral("tradejini")) { // get_history reads parts[1]
+ if (ensure_instruments_loaded_blocking(broker_id)) {
+ auto inst = InstrumentService::instance().find(sym, exch, broker_id);
+ if (inst) {
+ const QString symid =
+ inst->brsymbol.isEmpty() ? QString::number(inst->instrument_token) : inst->brsymbol;
+ return exch + ":" + symid;
+ }
+ }
+ return exch + ":" + sym; // best-effort
+ }
+ if (brokers_needing_token().contains(broker_id)) { // EXCHANGE:SYMBOL:TOKEN
+ if (!ensure_instruments_loaded_blocking(broker_id))
+ return {};
+ auto tok = InstrumentService::instance().instrument_token(sym, exch, broker_id);
+ if (!tok.has_value() || tok.value() <= 0)
+ return {};
+ return exch + ":" + sym + ":" + QString::number(tok.value());
+ }
+ // Brokers whose get_history resolves the symbol internally via InstrumentService
+ // (numeric token lookup) — warm the cache so that lookup succeeds, but pass the
+ // BARE symbol: they default the exchange to NSE and a colon-form is unnecessary.
+ static const QSet<QString> kInternalResolvers = {
+ QStringLiteral("zerodha"), QStringLiteral("angelone"),
+ QStringLiteral("dhan"), QStringLiteral("upstox"),
+ };
+ if (kInternalResolvers.contains(broker_id))
+ ensure_instruments_loaded_blocking(broker_id);
+ // Everyone else accepts the bare symbol as-is (Fyers/Groww format it themselves;
+ // Alpaca/Tradier/MetaApi want a plain ticker). Preserve the original string.
+ return bare_symbol;
+}
+
+} // namespace
+
CandleDataFetcher& CandleDataFetcher::instance() {
static CandleDataFetcher s;
return s;
}
QVector<OhlcvCandle> CandleDataFetcher::broker_candles_to_ohlcv(
- const QVector<trading::BrokerCandle>& src) {
+ const QVector<trading::BrokerCandle>& src, const QString& timeframe) {
+ const int64_t tf_ms = static_cast<int64_t>(timeframe_seconds(timeframe_from_string(timeframe))) * 1000;
QVector<OhlcvCandle> out;
out.reserve(src.size());
for (const auto& c : src) {
OhlcvCandle o;
- o.open_time = c.timestamp;
+ // BrokerCandle.timestamp is epoch seconds; OhlcvCandle uses epoch-ms.
+ int64_t ts = c.timestamp;
+ if (ts > 0 && ts < 100000000000LL) // < ~year 5138 in seconds → treat as seconds
+ ts *= 1000;
+ o.open_time = ts;
+ o.close_time = ts + tf_ms;
o.open = c.open;
o.high = c.high;
o.low = c.low;
@@ -62,9 +261,9 @@ void CandleDataFetcher::fetch(const QString& symbol, const QString& timeframe,
return;
}
LOG_WARN("CandleDataFetcher",
- QString("Broker fetch failed for %1, falling back to yfinance: %2")
+ QString("Broker fetch failed for %1, falling back to Yahoo: %2")
.arg(symbol, err));
- fetch_from_yfinance({symbol}, timeframe, lookback_days,
+ fetch_from_yahoo({symbol}, timeframe, lookback_days,
[callback, symbol](const QHash<QString, QVector<OhlcvCandle>>& data, const QStringList& errors) {
if (data.contains(symbol)) {
callback(true, data[symbol], {});
@@ -76,8 +275,8 @@ void CandleDataFetcher::fetch(const QString& symbol, const QString& timeframe,
return;
}
- // YFinance or Auto without broker
- fetch_from_yfinance({symbol}, timeframe, lookback_days,
+ // YFinance, or Auto without a connected broker → native Yahoo.
+ fetch_from_yahoo({symbol}, timeframe, lookback_days,
[callback, symbol](const QHash<QString, QVector<OhlcvCandle>>& data, const QStringList& errors) {
if (data.contains(symbol)) {
callback(true, data[symbol], {});
@@ -96,30 +295,44 @@ void CandleDataFetcher::fetch_multi(const QStringList& symbols, const QString& t
QPointer<CandleDataFetcher> self = this;
auto results = std::make_shared<QHash<QString, QVector<OhlcvCandle>>>();
auto errors = std::make_shared<QStringList>();
+ auto failed = std::make_shared<QStringList>();
auto remaining = std::make_shared<std::atomic<int>>(symbols.size());
for (const auto& sym : symbols) {
fetch_from_broker(sym, timeframe, lookback_days, broker_id, account_id,
- [self, sym, results, errors, remaining, callback, symbols, timeframe, lookback_days]
+ [self, sym, results, errors, failed, remaining, callback, timeframe, lookback_days]
(bool ok, const QVector<OhlcvCandle>& candles, const QString& err) {
+ // Broker callbacks are marshalled to self's (main) thread, so the
+ // shared containers are mutated serially — no extra locking needed.
if (ok && !candles.isEmpty()) {
results->insert(sym, candles);
} else {
+ failed->append(sym);
errors->append(QString("%1: %2").arg(sym, err));
}
- if (remaining->fetch_sub(1) == 1) {
- if (!results->isEmpty()) {
- callback(*results, *errors);
- } else if (self) {
- self->fetch_from_yfinance(symbols, timeframe, lookback_days, callback);
- }
+ if (remaining->fetch_sub(1) != 1)
+ return;
+ // Per-symbol Yahoo fallback: retry only the symbols the broker
+ // couldn't serve, then merge Yahoo data over the broker results.
+ if (failed->isEmpty() || !self) {
+ callback(*results, *errors);
+ return;
}
+ self->fetch_from_yahoo(*failed, timeframe, lookback_days,
+ [results, errors, callback](const QHash<QString, QVector<OhlcvCandle>>& ydata,
+ const QStringList& yerr) {
+ for (auto it = ydata.begin(); it != ydata.end(); ++it)
+ results->insert(it.key(), it.value());
+ QStringList merged = *errors;
+ merged += yerr;
+ callback(*results, merged);
+ });
});
}
return;
}
- fetch_from_yfinance(symbols, timeframe, lookback_days, callback);
+ fetch_from_yahoo(symbols, timeframe, lookback_days, callback);
}
void CandleDataFetcher::fetch_from_broker(const QString& symbol, const QString& timeframe,
@@ -143,82 +356,118 @@ void CandleDataFetcher::fetch_from_broker(const QString& symbol, const QString&
return;
}
- QDateTime to = QDateTime::currentDateTime();
- QDateTime from = to.addDays(-lookback_days);
- QString resolution = timeframe_to_broker_resolution(timeframe);
+ const QDateTime to = QDateTime::currentDateTime();
+ const QDateTime from = to.addDays(-lookback_days);
+ const QString resolution = timeframe_to_broker_resolution(timeframe);
+ const QString from_s = from.toString("yyyy-MM-dd");
+ const QString to_s = to.toString("yyyy-MM-dd");
- auto result = broker->get_history(creds, symbol, resolution,
- from.toString("yyyy-MM-dd"),
- to.toString("yyyy-MM-dd"));
+ // Build the broker-specific symbol string (EXCHANGE:SYMBOL[:TOKEN]); empty
+ // means a token broker we couldn't resolve — fall straight through to Yahoo.
+ const QString resolved = resolve_broker_symbol(broker_id, symbol, QStringLiteral("NSE"));
+
+ auto has_data = [](const trading::ApiResponse<QVector<trading::BrokerCandle>>& r) {
+ return r.success && r.data.value_or(QVector<trading::BrokerCandle>{}).size() > 0;
+ };
+
+ trading::ApiResponse<QVector<trading::BrokerCandle>> result;
+ QString last_err;
+ if (!resolved.isEmpty()) {
+ result = broker->get_history(creds, resolved, resolution, from_s, to_s);
+ if (!result.success)
+ last_err = result.error;
+ } else {
+ last_err = QStringLiteral("symbol resolution failed (no instrument token)");
+ }
+ // Fallback: retry with the bare symbol if the resolved form yielded nothing.
+ if (!has_data(result) && resolved != symbol) {
+ auto r2 = broker->get_history(creds, symbol, resolution, from_s, to_s);
+ if (has_data(r2))
+ result = r2;
+ else if (last_err.isEmpty())
+ last_err = r2.error;
+ }
if (!self) return;
- if (result.success) {
- auto candles = broker_candles_to_ohlcv(result.data.value_or({}));
+ if (has_data(result)) {
+ auto candles = broker_candles_to_ohlcv(result.data.value_or(QVector<trading::BrokerCandle>{}), timeframe);
QMetaObject::invokeMethod(self, [callback, candles]() {
callback(true, candles, {});
}, Qt::QueuedConnection);
} else {
- QMetaObject::invokeMethod(self, [callback, result]() {
- callback(false, {}, result.error);
+ const QString err = last_err.isEmpty() ? QStringLiteral("No data") : last_err;
+ QMetaObject::invokeMethod(self, [callback, err]() {
+ callback(false, {}, err);
}, Qt::QueuedConnection);
}
});
}
-void CandleDataFetcher::fetch_from_yfinance(const QStringList& symbols, const QString& timeframe,
- int lookback_days, MultiCandleCallback callback) {
- QJsonObject params;
- params["symbols"] = QJsonArray::fromStringList(symbols);
- params["timeframe"] = timeframe;
- params["lookback_days"] = lookback_days;
- auto json = QJsonDocument(params).toJson(QJsonDocument::Compact);
+void CandleDataFetcher::fetch_from_yahoo(const QStringList& symbols, const QString& timeframe,
+ int lookback_days, MultiCandleCallback callback) {
+ if (symbols.isEmpty()) {
+ callback({}, {});
+ return;
+ }
+ if (!yahoo_nam_)
+ yahoo_nam_ = new QNetworkAccessManager(this);
- QPointer<CandleDataFetcher> self = this;
- python::PythonRunner::instance().run(
- QStringLiteral("algo_trading/fetch_candles.py"), {json},
- [self, callback](python::PythonResult result) {
- if (!self) return;
-
- if (!result.success) {
- QMetaObject::invokeMethod(self, [callback, result]() {
- callback({}, {result.error});
- }, Qt::QueuedConnection);
- return;
- }
+ const YahooInterval yi = yahoo_interval(timeframe);
+ const int max_days = yahoo_max_lookback_days(yi.interval);
+ int days = lookback_days > 0 ? lookback_days : max_days;
+ if (days > max_days)
+ days = max_days;
- auto doc = QJsonDocument::fromJson(
- python::extract_json(result.output).toUtf8());
- auto root = doc.object();
+ const qint64 period2 = QDateTime::currentSecsSinceEpoch();
+ const qint64 period1 = period2 - static_cast<qint64>(days) * 86400;
+ const int64_t tf_ms =
+ static_cast<int64_t>(timeframe_seconds(timeframe_from_string(timeframe))) * 1000;
- QHash<QString, QVector<OhlcvCandle>> data;
- QStringList errors;
+ auto results = std::make_shared<QHash<QString, QVector<OhlcvCandle>>>();
+ auto errors = std::make_shared<QStringList>();
+ auto remaining = std::make_shared<std::atomic<int>>(symbols.size());
- if (!root.value("success").toBool()) {
- errors.append(root.value("error").toString());
- } else {
- auto symbols_data = root.value("data").toObject();
- for (auto it = symbols_data.begin(); it != symbols_data.end(); ++it) {
- QVector<OhlcvCandle> candles;
- for (const auto& cv : it.value().toArray()) {
- auto co = cv.toObject();
- OhlcvCandle c;
- c.open_time = static_cast<int64_t>(co.value("t").toDouble());
- c.open = co.value("o").toDouble();
- c.high = co.value("h").toDouble();
- c.low = co.value("l").toDouble();
- c.close = co.value("c").toDouble();
- c.volume = co.value("v").toDouble();
- c.is_closed = true;
- candles.append(c);
+ for (const QString& sym : symbols) {
+ const QString yf = symbol_to_yahoo(sym);
+ const QString url =
+ QString("https://query1.finance.yahoo.com/v8/finance/chart/%1"
+ "?period1=%2&period2=%3&interval=%4&includePrePost=false")
+ .arg(yf)
+ .arg(period1)
+ .arg(period2)
+ .arg(yi.interval);
+
+ QNetworkRequest req{QUrl(url)};
+ // Yahoo blocks non-browser User-Agents — present a realistic one.
+ req.setHeader(QNetworkRequest::UserAgentHeader,
+ "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 "
+ "(KHTML, like Gecko) Chrome/120.0 Safari/537.36");
+ req.setRawHeader("Accept", "application/json");
+
+ QPointer<CandleDataFetcher> self = this;
+ QNetworkReply* reply = yahoo_nam_->get(req);
+ connect(reply, &QNetworkReply::finished, this,
+ [self, reply, sym, yf, tf_ms, yi, results, errors, remaining, callback]() {
+ reply->deleteLater();
+ const QByteArray body = reply->readAll();
+
+ if (reply->error() != QNetworkReply::NoError) {
+ errors->append(QString("%1: %2").arg(sym, reply->errorString()));
+ } else {
+ QString perr;
+ auto candles =
+ parse_yahoo_chart(QJsonDocument::fromJson(body).object(), tf_ms, &perr);
+ candles = aggregate_candles(candles, yi.aggregate);
+ if (!candles.isEmpty())
+ results->insert(sym, candles);
+ else
+ errors->append(QString("%1: %2").arg(sym, perr.isEmpty() ? "No data" : perr));
}
- data.insert(it.key(), candles);
- }
- }
- QMetaObject::invokeMethod(self, [callback, data, errors]() {
- callback(data, errors);
- }, Qt::QueuedConnection);
- });
+ if (remaining->fetch_sub(1) == 1)
+ callback(*results, *errors);
+ });
+ }
}
} // namespace fincept::algo
diff --git a/fincept-qt/src/algo_engine/CandleDataFetcher.h b/fincept-qt/src/algo_engine/CandleDataFetcher.h
index 225d9d78c..7ac1e4aff 100644
--- a/fincept-qt/src/algo_engine/CandleDataFetcher.h
+++ b/fincept-qt/src/algo_engine/CandleDataFetcher.h
@@ -8,6 +8,8 @@
#include <QVector>
#include <functional>
+class QNetworkAccessManager;
+
namespace fincept::algo {
enum class DataSource { Broker, YFinance, Auto };
@@ -51,11 +53,15 @@ private:
const QString& broker_id, const QString& account_id,
CandleCallback callback);
- void fetch_from_yfinance(const QStringList& symbols, const QString& timeframe,
- int lookback_days, MultiCandleCallback callback);
+ // Native Yahoo Finance fetch (replaces the old Python yfinance fallback).
+ void fetch_from_yahoo(const QStringList& symbols, const QString& timeframe,
+ int lookback_days, MultiCandleCallback callback);
- static QVector<OhlcvCandle> broker_candles_to_ohlcv(const QVector<fincept::trading::BrokerCandle>& src);
+ static QVector<OhlcvCandle> broker_candles_to_ohlcv(const QVector<fincept::trading::BrokerCandle>& src,
+ const QString& timeframe);
static QString timeframe_to_broker_resolution(const QString& tf);
+
+ QNetworkAccessManager* yahoo_nam_ = nullptr; // lazy-created, browser UA for Yahoo
};
} // namespace fincept::algo
diff --git a/fincept-qt/src/app/WindowFrame_Auth.cpp b/fincept-qt/src/app/WindowFrame_Auth.cpp
index c30fc733e..ff534ad63 100644
--- a/fincept-qt/src/app/WindowFrame_Auth.cpp
+++ b/fincept-qt/src/app/WindowFrame_Auth.cpp
@@ -159,6 +159,7 @@ void WindowFrame::on_auth_state_changed() {
fincept::trading::InstrumentService::instance().load_from_db_async("angelone");
fincept::trading::InstrumentService::instance().load_from_db_async("groww");
fincept::trading::InstrumentService::instance().load_from_db_async("fyers");
+ fincept::trading::InstrumentService::instance().load_from_db_async("dhan");
} else {
// Free/no plan → show pricing gate
set_shell_visible(false);
diff --git a/fincept-qt/src/app/main.cpp b/fincept-qt/src/app/main.cpp
index 6d3a051d1..ac4fe8705 100644
--- a/fincept-qt/src/app/main.cpp
+++ b/fincept-qt/src/app/main.cpp
@@ -637,6 +637,7 @@ int main(int argc, char* argv[]) {
fincept::register_migration_v032();
fincept::register_migration_v033();
fincept::register_migration_v034();
+ fincept::register_migration_v035();
// Open main database
QString db_path = fincept::AppPaths::data() + "/fincept.db";
diff --git a/fincept-qt/src/python/PythonRunner.cpp b/fincept-qt/src/python/PythonRunner.cpp
index 61235c39a..bfee973c5 100644
--- a/fincept-qt/src/python/PythonRunner.cpp
+++ b/fincept-qt/src/python/PythonRunner.cpp
@@ -488,9 +488,16 @@ void PythonRunner::run_code(const QString& code, Callback cb) {
return;
}
- // Write code to a temp file
+ // Write code to a temp file. The filename must be unique even when several
+ // run_code() calls fire within the same millisecond (e.g. on portfolio load
+ // fetch_correlation + benchmark history + risk-free rate all kick off at
+ // once). A bare millisecond timestamp collided, so one cell's completion
+ // would delete the temp file out from under another still-pending cell
+ // ("can't open file … No such file or directory"). A UUID guarantees
+ // uniqueness regardless of timing.
QString temp_dir = QStandardPaths::writableLocation(QStandardPaths::TempLocation);
- QString temp_path = temp_dir + "/fincept_cell_" + QString::number(QDateTime::currentMSecsSinceEpoch()) + ".py";
+ QString temp_path = temp_dir + "/fincept_cell_" + QString::number(QDateTime::currentMSecsSinceEpoch()) + "_" +
+ QUuid::createUuid().toString(QUuid::Id128) + ".py";
QFile file(temp_path);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
@@ -763,11 +770,57 @@ void PythonRunner::start_next() {
// ── Extract JSON ─────────────────────────────────────────────────────────────
QString extract_json(const QString& output) {
- // Find the first '{' or '[' and return everything from that point.
- // Scripts print multi-line indented JSON, so we cannot take a single line.
+ // Return the LAST complete top-level JSON value in the output.
+ //
+ // Script/agent stdout is frequently polluted with leading log lines — some
+ // of which contain braces (e.g. a Python dict repr like
+ // "ERROR ... {'error': {...}, 'status': 401}" leaked onto stdout by a
+ // third-party logger). The old "first '{' to end" logic grabbed that dict
+ // repr and QJsonDocument::fromJson() then returned null → the caller saw a
+ // spurious "exit=0" failure. Our scripts always print their real JSON
+ // envelope LAST, so we scan forward tracking brace/bracket depth (respecting
+ // double-quoted strings) and keep the final value whose depth returns to 0.
+ int best_start = -1, best_end = -1; // [start, end) of the last balanced value
+ int cur_start = -1, depth = 0;
+ bool in_str = false, esc = false;
+ for (int i = 0; i < output.size(); ++i) {
+ const QChar c = output[i];
+ if (in_str) {
+ if (esc)
+ esc = false;
+ else if (c == '\\')
+ esc = true;
+ else if (c == '"')
+ in_str = false;
+ continue;
+ }
+ if (c == '"') {
+ in_str = true;
+ continue;
+ }
+ if (depth == 0) {
+ if (c == '{' || c == '[') {
+ cur_start = i;
+ depth = 1;
+ }
+ } else {
+ if (c == '{' || c == '[') {
+ ++depth;
+ } else if (c == '}' || c == ']') {
+ if (--depth == 0) {
+ best_start = cur_start;
+ best_end = i + 1;
+ }
+ }
+ }
+ }
+
+ if (best_start >= 0)
+ return output.mid(best_start, best_end - best_start).trimmed();
+
+ // Fallback: original heuristic (first bracket to end) for partial output.
int brace = output.indexOf('{');
int bracket = output.indexOf('[');
-
int start = -1;
if (brace >= 0 && bracket >= 0)
start = qMin(brace, bracket);
@@ -775,10 +828,8 @@ QString extract_json(const QString& output) {
start = brace;
else if (bracket >= 0)
start = bracket;
-
if (start < 0)
return {};
-
return output.mid(start).trimmed();
}
diff --git a/fincept-qt/src/screens/algo_trading/AlgoTradingScreen.cpp b/fincept-qt/src/screens/algo_trading/AlgoTradingScreen.cpp
index 8333dd3bc..d3f1bee97 100644
--- a/fincept-qt/src/screens/algo_trading/AlgoTradingScreen.cpp
+++ b/fincept-qt/src/screens/algo_trading/AlgoTradingScreen.cpp
@@ -95,11 +95,6 @@ QWidget* AlgoTradingScreen::build_top_bar() {
.arg(ui::colors::TEXT_PRIMARY()));
hl->addWidget(title);
- auto* subtitle = new QLabel("strategy builder · backtesting · live deployment", bar);
- subtitle->setStyleSheet(
- QString("color:%1; font-size:10px; background:transparent;").arg(ui::colors::TEXT_TERTIARY()));
- hl->addWidget(subtitle);
-
auto* div = new QWidget(bar);
div->setFixedSize(1, 20);
div->setStyleSheet(QString("background:%1;").arg(ui::colors::BORDER_DIM()));
diff --git a/fincept-qt/src/screens/algo_trading/StrategyBuilderPanel.cpp b/fincept-qt/src/screens/algo_trading/StrategyBuilderPanel.cpp
index cf6d9cdcc..d77291e6e 100644
--- a/fincept-qt/src/screens/algo_trading/StrategyBuilderPanel.cpp
+++ b/fincept-qt/src/screens/algo_trading/StrategyBuilderPanel.cpp
@@ -262,14 +262,28 @@ void StrategyBuilderPanel::on_backtest() {
return;
}
- // Save first, then backtest
- on_save();
+ // Build the strategy from current UI state, persist it, then backtest it
+ // directly (no DB round-trip — unsaved edits are tested as-shown).
+ services::algo::AlgoStrategy strat;
+ strat.id = QUuid::createUuid().toString(QUuid::WithoutBraces);
+ strat.name = name_edit_->text().trimmed();
+ strat.description = desc_edit_->text().trimmed();
+ strat.timeframe = timeframe_combo_->currentText();
+ strat.entry_conditions = entry_section_->conditions();
+ strat.exit_conditions = exit_section_->conditions();
+ strat.entry_logic = entry_section_->combined_logic();
+ strat.exit_logic = exit_section_->combined_logic();
+ strat.stop_loss = risk_panel_->stop_loss();
+ strat.take_profit = risk_panel_->take_profit();
+ strat.trailing_stop = risk_panel_->trailing_stop();
+
+ services::algo::AlgoTradingService::instance().save_strategy(strat);
+
QString symbol = symbol_combo_->currentText().trimmed();
if (symbol.isEmpty()) symbol = QStringLiteral("RELIANCE");
services::algo::AlgoTradingService::instance().run_backtest(
- QString(), symbol, bt_start_date_->text(), bt_end_date_->text(),
- bt_capital_->value());
+ strat, symbol, bt_start_date_->text(), bt_end_date_->text(), bt_capital_->value());
status_label_->setText(tr("Running backtest..."));
}
diff --git a/fincept-qt/src/screens/derivatives/DerivativesScreen.cpp b/fincept-qt/src/screens/derivatives/DerivativesScreen.cpp
index 6f4736cc3..7e50abda0 100644
--- a/fincept-qt/src/screens/derivatives/DerivativesScreen.cpp
+++ b/fincept-qt/src/screens/derivatives/DerivativesScreen.cpp
@@ -161,7 +161,7 @@ QWidget* DerivativesScreen::create_header_bar() {
auto* hl = new QHBoxLayout(bar);
hl->setContentsMargins(16, 0, 16, 0);
- auto* icon = new QLabel(QStringLiteral("\xF0\x9F\x93\x90")); // calculator emoji
+ auto* icon = new QLabel(QString::fromUtf8("\xF0\x9F\x93\x90")); // calculator emoji
icon->setStyleSheet(QString("color:%1;font-size:18px;background:transparent;").arg(colors::AMBER()));
auto* title_col = new QVBoxLayout;
@@ -336,7 +336,7 @@ QWidget* DerivativesScreen::create_equity_options_panel() {
bh->setFixedHeight(34);
auto* bhl = new QHBoxLayout(bh);
bhl->setContentsMargins(12, 0, 12, 0);
- auto* bhi = new QLabel(QStringLiteral("\xE2\x86\x97")); // arrow
+ auto* bhi = new QLabel(QString::fromUtf8("\xE2\x86\x97")); // arrow
bhi->setObjectName("derivPanelIcon");
auto* bht = new QLabel("BLACK-SCHOLES PRICING");
bht->setObjectName("derivPanelTitle");
@@ -386,7 +386,7 @@ QWidget* DerivativesScreen::create_equity_options_panel() {
ivh->setFixedHeight(34);
auto* ivhl = new QHBoxLayout(ivh);
ivhl->setContentsMargins(12, 0, 12, 0);
- auto* ivhi = new QLabel(QStringLiteral("\xE2\x9A\xA1")); // lightning
+ auto* ivhi = new QLabel(QString::fromUtf8("\xE2\x9A\xA1")); // lightning
ivhi->setObjectName("derivPanelIcon");
auto* ivht = new QLabel("IMPLIED VOLATILITY");
ivht->setObjectName("derivPanelTitle");
@@ -443,7 +443,7 @@ QWidget* DerivativesScreen::create_fx_options_panel() {
hdr->setFixedHeight(34);
auto* hl = new QHBoxLayout(hdr);
hl->setContentsMargins(12, 0, 12, 0);
- auto* icon = new QLabel(QStringLiteral("\xF0\x9F\x92\xB1")); // currency emoji
+ auto* icon = new QLabel(QString::fromUtf8("\xF0\x9F\x92\xB1")); // currency emoji
icon->setObjectName("derivPanelIcon");
auto* title = new QLabel("FX VANILLA OPTION PRICING");
title->setObjectName("derivPanelTitle");
@@ -498,7 +498,7 @@ QWidget* DerivativesScreen::create_swaps_panel() {
hdr->setFixedHeight(34);
auto* hl = new QHBoxLayout(hdr);
hl->setContentsMargins(12, 0, 12, 0);
- auto* icon = new QLabel(QStringLiteral("\xF0\x9F\x94\x84")); // arrows emoji
+ auto* icon = new QLabel(QString::fromUtf8("\xF0\x9F\x94\x84")); // arrows emoji
icon->setObjectName("derivPanelIcon");
auto* title = new QLabel("INTEREST RATE SWAP PRICING");
title->setObjectName("derivPanelTitle");
@@ -550,7 +550,7 @@ QWidget* DerivativesScreen::create_credit_panel() {
hdr->setFixedHeight(34);
auto* hl = new QHBoxLayout(hdr);
hl->setContentsMargins(12, 0, 12, 0);
- auto* icon = new QLabel(QStringLiteral("\xF0\x9F\x8E\xAF")); // target emoji
+ auto* icon = new QLabel(QString::fromUtf8("\xF0\x9F\x8E\xAF")); // target emoji
icon->setObjectName("derivPanelIcon");
auto* title = new QLabel("CREDIT DEFAULT SWAP PRICING");
title->setObjectName("derivPanelTitle");
diff --git a/fincept-qt/src/screens/equity_research/EquityOverviewTab.cpp b/fincept-qt/src/screens/equity_research/EquityOverviewTab.cpp
index 698415262..2d3e98102 100644
--- a/fincept-qt/src/screens/equity_research/EquityOverviewTab.cpp
+++ b/fincept-qt/src/screens/equity_research/EquityOverviewTab.cpp
@@ -75,7 +75,7 @@ QLabel* EquityOverviewTab::add_row_(QFrame* panel, const char* key, const char*
.arg(FONT_KEY));
i18n_labels_.insert(k, key);
- auto* v = new QLabel(QStringLiteral("\xe2\x80\x94"));
+ auto* v = new QLabel(QString::fromUtf8("\xe2\x80\x94"));
v->setStyleSheet(QString("color:%1;font-size:%2px;font-weight:600;background:transparent;border:0;")
.arg(val_color)
.arg(FONT_VAL));
@@ -504,7 +504,7 @@ QWidget* EquityOverviewTab::build_analyst_panel() {
target_low_val_ = add_row_(p, QT_TR_NOOP("LOW"), ui::colors::NEGATIVE);
analyst_count_val_ = add_row_(p, QT_TR_NOOP("ANALYSTS"), CYAN);
- rec_key_label_ = new QLabel(QStringLiteral("\xe2\x80\x94"));
+ rec_key_label_ = new QLabel(QString::fromUtf8("\xe2\x80\x94"));
rec_key_label_->setAlignment(Qt::AlignCenter);
rec_key_label_->setStyleSheet(QString("background:%1;color:%2;border-radius:2px;padding:3px 8px;"
"font-size:12px;font-weight:700;")
@@ -564,7 +564,7 @@ QWidget* EquityOverviewTab::build_bottom_row() {
QWidget* EquityOverviewTab::build_company_desc_panel() {
auto* p = make_panel_(QT_TR_NOOP("COMPANY OVERVIEW"), CYAN);
- company_desc_ = new QLabel(QStringLiteral("\xe2\x80\x94"));
+ company_desc_ = new QLabel(QString::fromUtf8("\xe2\x80\x94"));
company_desc_->setWordWrap(true);
company_desc_->setAlignment(Qt::AlignTop | Qt::AlignLeft);
company_desc_->setStyleSheet(QString("color:%1;font-size:%2px;line-height:1.5;"
@@ -691,7 +691,7 @@ void EquityOverviewTab::render_info(const services::equity::StockInfo& info) {
rec_text = tr("STRONG SELL");
rec_color = ui::colors::NEGATIVE;
}
- rec_key_label_->setText(rec_text.isEmpty() ? QStringLiteral("\xe2\x80\x94") : rec_text);
+ rec_key_label_->setText(rec_text.isEmpty() ? QString::fromUtf8("\xe2\x80\x94") : rec_text);
rec_key_label_->setStyleSheet(QString("background:%1;color:%2;border-radius:2px;padding:3px 8px;"
"font-size:12px;font-weight:700;")
.arg(ui::colors::BG_RAISED(), rec_color));
@@ -826,7 +826,7 @@ QString EquityOverviewTab::currency_symbol(const QString& currency_code) {
QString EquityOverviewTab::fmt_price(double v) const {
if (v == 0.0)
- return QStringLiteral("\xe2\x80\x94");
+ return QString::fromUtf8("\xe2\x80\x94");
const QString sym = current_currency_.isEmpty() ? "$" : currency_symbol(current_currency_);
return QString("%1%2").arg(sym).arg(v, 0, 'f', 2);
}
diff --git a/fincept-qt/src/screens/markets/MarketPanel.cpp b/fincept-qt/src/screens/markets/MarketPanel.cpp
index 7b4cd96c2..0c8bbe1b4 100644
--- a/fincept-qt/src/screens/markets/MarketPanel.cpp
+++ b/fincept-qt/src/screens/markets/MarketPanel.cpp
@@ -143,19 +143,31 @@ void MarketPanel::setup_table_columns() {
const QStringList& cols = config_.column_order;
table_->setColumnCount(cols.size());
- // Set header labels with alignment matching cell alignment
+ // Set header labels with alignment matching cell alignment.
+ // The locked first column ("SYMBOL") now renders the human-readable name,
+ // so it's labelled NAME; the raw ticker is available via the TICKER column.
for (int i = 0; i < cols.size(); ++i) {
const QString& c = cols[i];
- auto* hdr = new QTableWidgetItem(c);
- bool is_text = (c == "SYMBOL" || c == "NAME");
+ const QString label = (c == "SYMBOL") ? QStringLiteral("NAME") : c;
+ auto* hdr = new QTableWidgetItem(label);
+ bool is_text = (c == "SYMBOL" || c == "NAME" || c == "TICKER");
hdr->setTextAlignment(is_text ? (Qt::AlignLeft | Qt::AlignVCenter)
: (Qt::AlignRight | Qt::AlignVCenter));
table_->setHorizontalHeaderItem(i, hdr);
}
- // All columns stretch to fill available width evenly.
- for (int i = 0; i < cols.size(); ++i)
- table_->horizontalHeader()->setSectionResizeMode(i, QHeaderView::Stretch);
+ // Column sizing: the NAME column (human-readable, variable-length) gets the
+ // leftover width via Stretch, while the compact numeric/ticker columns size
+ // to their content. Sharing equal stretch across every column starved the
+ // name column and truncated long names ("State Street SPDR S&P 500 ETF…").
+ auto* hh = table_->horizontalHeader();
+ for (int i = 0; i < cols.size(); ++i) {
+ const QString& c = cols[i];
+ if (c == "SYMBOL" || c == "NAME")
+ hh->setSectionResizeMode(i, QHeaderView::Stretch);
+ else
+ hh->setSectionResizeMode(i, QHeaderView::ResizeToContents);
+ }
}
// ---------------------------------------------------------------------------
@@ -172,7 +184,9 @@ void MarketPanel::open_cols_dropdown() {
.arg(ui::colors::BG_RAISED(), ui::colors::BORDER_MED(),
ui::colors::TEXT_PRIMARY(), ui::colors::BG_HOVER()));
- const QStringList optional = {"CHG", "CHG%", "HIGH", "LOW", "VOL", "BID", "ASK", "OPEN", "NAME"};
+ // NAME is now the locked first column (renders the friendly name), so the
+ // optional set offers TICKER instead for users who also want the raw symbol.
+ const QStringList optional = {"CHG", "CHG%", "HIGH", "LOW", "VOL", "BID", "ASK", "OPEN", "TICKER"};
for (const QString& col : optional) {
auto* act = menu->addAction(col);
@@ -256,6 +270,25 @@ void MarketPanel::hub_resubscribe() {
return;
}
+ // Resolve human-readable display names so the table shows "S&P 500" / "Gold"
+ // instead of "^GSPC" / "GC=F". Cached after first resolution, so this is a
+ // no-op fast path on subsequent refreshes. Re-render when names arrive.
+ QPointer<MarketPanel> self = this;
+ services::MarketDataService::instance().resolve_names(
+ config_.symbols, [self](const QHash<QString, QString>& m) {
+ if (!self || m.isEmpty())
+ return;
+ bool changed = false;
+ for (auto it = m.constBegin(); it != m.constEnd(); ++it) {
+ if (self->names_.value(it.key()) != it.value()) {
+ self->names_.insert(it.key(), it.value());
+ changed = true;
+ }
+ }
+ if (changed && self->has_data_)
+ self->populate(self->cached_quotes_);
+ });
+
// Fresh subscribe if never active or symbol set was reconfigured.
// Cheaper to always re-wire since MarketsScreen rarely calls refresh()
// outside of user action / auto-refresh tick, and the hub dedupes topics.
@@ -364,16 +397,38 @@ void MarketPanel::populate(const QVector<services::QuoteData>& quotes) {
return item;
};
+ // Human-readable display name: prefer the resolved yfinance name
+ // (e.g. "S&P 500", "Gold"), fall back to any name on the quote, then
+ // to the raw ticker. The ticker is still kept on the cell (UserRole +
+ // tooltip) so copy / backtest / context-menu actions keep working.
+ QString disp = names_.value(q.symbol);
+ if (disp.isEmpty())
+ disp = (!q.name.isEmpty() && q.name != q.symbol) ? q.name : q.symbol;
+
+ // Currency symbol shown in front of price levels (e.g. "$", "₹").
+ // Empty for forex pairs / index levels / yields, where it isn't
+ // meaningful. Resolved & cached alongside the display name.
+ const QString cur = services::MarketDataService::instance().currency_prefix(q.symbol);
+
const QStringList& cols = config_.column_order;
for (int ci = 0; ci < cols.size(); ++ci) {
const QString& col = cols[ci];
- if (col == "SYMBOL") table_->setItem(row, ci, mk(q.symbol, ui::colors::TEXT_PRIMARY(), Qt::AlignLeft | Qt::AlignVCenter));
- else if (col == "NAME") table_->setItem(row, ci, mk(q.name, ui::colors::TEXT_DIM(), Qt::AlignLeft | Qt::AlignVCenter));
- else if (col == "LAST") table_->setItem(row, ci, mk(QString::number(q.price, 'f', prec), ui::colors::AMBER()));
+ if (col == "SYMBOL") {
+ auto* item = mk(disp, ui::colors::TEXT_PRIMARY(), Qt::AlignLeft | Qt::AlignVCenter);
+ item->setData(Qt::UserRole, q.symbol); // raw ticker, for copy / backtest
+ // Full name + ticker on hover, so a name too long for the column
+ // (e.g. "State Street SPDR S&P 500 ETF Trust") is still readable.
+ item->setToolTip(disp == q.symbol ? q.symbol
+ : QString("%1 (%2)").arg(disp, q.symbol));
+ table_->setItem(row, ci, item);
+ }
+ else if (col == "NAME") table_->setItem(row, ci, mk(disp, ui::colors::TEXT_DIM(), Qt::AlignLeft | Qt::AlignVCenter));
+ else if (col == "TICKER") table_->setItem(row, ci, mk(q.symbol, ui::colors::TEXT_DIM(), Qt::AlignLeft | Qt::AlignVCenter));
+ else if (col == "LAST") table_->setItem(row, ci, mk(cur + QString::number(q.price, 'f', prec), ui::colors::AMBER()));
else if (col == "CHG") table_->setItem(row, ci, mk(QString("%1 %2").arg(arr).arg(std::abs(q.change), 0, 'f', 2), cc));
else if (col == "CHG%") table_->setItem(row, ci, mk(QString("%1%2%").arg(arr).arg(std::abs(q.change_pct), 0, 'f', 2), cc));
- else if (col == "HIGH") table_->setItem(row, ci, mk(QString::number(q.high, 'f', 2), ui::colors::TEXT_SECONDARY()));
- else if (col == "LOW") table_->setItem(row, ci, mk(QString::number(q.low, 'f', 2), ui::colors::TEXT_SECONDARY()));
+ else if (col == "HIGH") table_->setItem(row, ci, mk(cur + QString::number(q.high, 'f', 2), ui::colors::TEXT_SECONDARY()));
+ else if (col == "LOW") table_->setItem(row, ci, mk(cur + QString::number(q.low, 'f', 2), ui::colors::TEXT_SECONDARY()));
else if (col == "VOL") table_->setItem(row, ci, mk(fincept::ui::formatting::format_compact_volume(static_cast<qint64>(q.volume)), ui::colors::TEXT_DIM()));
else if (col == "BID") table_->setItem(row, ci, mk("--", ui::colors::TEXT_DIM()));
else if (col == "ASK") table_->setItem(row, ci, mk("--", ui::colors::TEXT_DIM()));
@@ -487,20 +542,28 @@ void MarketPanel::show_row_context_menu(const QPoint& pos) {
auto* it = table_->itemAt(pos);
if (!it) return;
+ // Column 0 now shows the friendly name; the raw ticker lives in UserRole.
+ auto ticker_at = [this](int row) -> QString {
+ auto* sym_item = table_->item(row, 0);
+ if (!sym_item) return QString();
+ const QString t = sym_item->data(Qt::UserRole).toString();
+ return t.isEmpty() ? sym_item->text() : t;
+ };
+
QMenu menu(this);
QAction* copy_act = menu.addAction("Copy Symbol");
- connect(copy_act, &QAction::triggered, this, [this, it]() {
- auto* sym_item = table_->item(it->row(), 0);
- if (sym_item)
- QApplication::clipboard()->setText(sym_item->text());
+ connect(copy_act, &QAction::triggered, this, [it, ticker_at]() {
+ const QString t = ticker_at(it->row());
+ if (!t.isEmpty())
+ QApplication::clipboard()->setText(t);
});
QAction* backtest_act = menu.addAction("Backtest This Symbol");
- connect(backtest_act, &QAction::triggered, this, [this, it]() {
- auto* sym_item = table_->item(it->row(), 0);
- if (!sym_item) return;
+ connect(backtest_act, &QAction::triggered, this, [it, ticker_at]() {
+ const QString t = ticker_at(it->row());
+ if (t.isEmpty()) return;
QJsonObject config;
QJsonArray symbols;
- symbols.append(sym_item->text());
+ symbols.append(t);
config["symbols"] = symbols;
fincept::services::backtest::BacktestingService::instance().set_pending_portfolio_config(config);
fincept::EventBus::instance().publish("nav.switch_screen", {{"screen_id", QString("backtesting")}});
diff --git a/fincept-qt/src/screens/markets/MarketPanel.h b/fincept-qt/src/screens/markets/MarketPanel.h
index 46fafa9f4..2f2505f09 100644
--- a/fincept-qt/src/screens/markets/MarketPanel.h
+++ b/fincept-qt/src/screens/markets/MarketPanel.h
@@ -71,6 +71,7 @@ class MarketPanel : public QWidget {
bool fetch_failed_ = false;
QHash<QString, services::QuoteData> row_cache_;
+ QHash<QString, QString> names_; // symbol → human-readable display name (yfinance, cached)
QSet<QString> pending_initial_; // symbols awaiting first delivery for refresh_finished
bool refresh_inflight_ = false;
bool hub_active_ = false;
diff --git a/fincept-qt/src/screens/markets/MarketPanelConfig.h b/fincept-qt/src/screens/markets/MarketPanelConfig.h
index 43e580407..a388e5cf4 100644
--- a/fincept-qt/src/screens/markets/MarketPanelConfig.h
+++ b/fincept-qt/src/screens/markets/MarketPanelConfig.h
@@ -6,8 +6,11 @@
namespace fincept::screens {
/// Returns the full list of available column identifiers for MarketPanel.
+/// SYMBOL is the locked first column and renders the human-readable name;
+/// TICKER is the optional column that shows the raw ticker. NAME is retained
+/// for backward-compatibility with previously-saved panel configs.
inline QStringList all_market_columns() {
- return {"SYMBOL", "LAST", "CHG", "CHG%", "HIGH", "LOW", "VOL", "BID", "ASK", "OPEN", "NAME"};
+ return {"SYMBOL", "LAST", "CHG", "CHG%", "HIGH", "LOW", "VOL", "BID", "ASK", "OPEN", "TICKER", "NAME"};
}
/// Returns the default column set used for new panels.
diff --git a/fincept-qt/src/screens/news/NewsDetailPanel.cpp b/fincept-qt/src/screens/news/NewsDetailPanel.cpp
index 38fea23b8..120b0c0d5 100644
--- a/fincept-qt/src/screens/news/NewsDetailPanel.cpp
+++ b/fincept-qt/src/screens/news/NewsDetailPanel.cpp
@@ -22,9 +22,17 @@
namespace fincept::screens {
+namespace {
+// Fixed width of the right-hand detail panel. The scroll content is capped to
+// this so word-wrapped labels wrap against it instead of reporting their full
+// unwrapped width — otherwise revealing the AI-analysis section balloons the
+// panel's size hint and pushes it past the screen edge.
+constexpr int kPanelWidth = 420;
+} // namespace
+
NewsDetailPanel::NewsDetailPanel(QWidget* parent) : QWidget(parent) {
setObjectName("newsDetailOverlay");
- setFixedWidth(420);
+ setFixedWidth(kPanelWidth);
hide(); // start hidden — shown on article click
auto* root = new QVBoxLayout(this);
@@ -90,6 +98,10 @@ QWidget* NewsDetailPanel::build_content_view() {
auto* content = new QWidget(scroll);
content->setObjectName("newsDetailContent");
+ // Cap the content to the panel width so word-wrapped labels wrap against it
+ // rather than reporting their full single-line width — without this, showing
+ // the AI-analysis section grows the panel beyond the screen edge.
+ content->setMaximumWidth(kPanelWidth);
auto* layout = new QVBoxLayout(content);
layout->setContentsMargins(12, 10, 12, 10);
layout->setSpacing(8);
@@ -339,9 +351,9 @@ QWidget* NewsDetailPanel::build_content_view() {
ai_keywords_->hide();
analysis_layout->addWidget(ai_keywords_);
- auto* kp_title = new QLabel("KEY POINTS", analysis_section_);
- kp_title->setObjectName("newsDetailSubTitle");
- analysis_layout->addWidget(kp_title);
+ key_points_title_ = new QLabel("KEY POINTS", analysis_section_);
+ key_points_title_->setObjectName("newsDetailSubTitle");
+ analysis_layout->addWidget(key_points_title_);
auto* kp_container = new QWidget(analysis_section_);
key_points_layout_ = new QVBoxLayout(kp_container);
@@ -349,9 +361,9 @@ QWidget* NewsDetailPanel::build_content_view() {
key_points_layout_->setSpacing(2);
analysis_layout->addWidget(kp_container);
- auto* risk_title = new QLabel("RISK SIGNALS", analysis_section_);
- risk_title->setObjectName("newsDetailSubTitle");
- analysis_layout->addWidget(risk_title);
+ risk_title_ = new QLabel("RISK SIGNALS", analysis_section_);
+ risk_title_->setObjectName("newsDetailSubTitle");
+ analysis_layout->addWidget(risk_title_);
auto* risk_container = new QWidget(analysis_section_);
risk_layout_ = new QVBoxLayout(risk_container);
@@ -359,9 +371,9 @@ QWidget* NewsDetailPanel::build_content_view() {
risk_layout_->setSpacing(2);
analysis_layout->addWidget(risk_container);
- auto* topics_title = new QLabel("TOPICS", analysis_section_);
- topics_title->setObjectName("newsDetailSubTitle");
- analysis_layout->addWidget(topics_title);
+ topics_title_ = new QLabel("TOPICS", analysis_section_);
+ topics_title_->setObjectName("newsDetailSubTitle");
+ analysis_layout->addWidget(topics_title_);
auto* topics_container = new QWidget(analysis_section_);
topics_layout_ = new QVBoxLayout(topics_container);
@@ -607,7 +619,9 @@ void NewsDetailPanel::show_analysis(const services::NewsAnalysis& analysis) {
QString pred_color = pred.contains("positive") ? ui::colors::POSITIVE
: (pred.contains("negative") ? ui::colors::NEGATIVE
: ui::colors::WARNING);
- ai_prediction_->setText(QString("Outlook: %1").arg(pred.isEmpty() ? "neutral" : pred));
+ // API sends snake_case (e.g. "moderate_positive"); show it as readable words.
+ QString pred_text = pred.isEmpty() ? "neutral" : QString(pred).replace('_', ' ');
+ ai_prediction_->setText(QString("Outlook: %1").arg(pred_text));
ai_prediction_->setStyleSheet(QString("color: %1;").arg(pred_color));
ai_confidence_->setText(
@@ -626,6 +640,11 @@ void NewsDetailPanel::show_analysis(const services::NewsAnalysis& analysis) {
lbl->setWordWrap(true);
key_points_layout_->addWidget(lbl);
}
+ // Hide the heading + container entirely when there's nothing to show, so
+ // the panel doesn't render a bare "KEY POINTS" label over empty space.
+ const bool has_key_points = !analysis.key_points.isEmpty();
+ key_points_title_->setVisible(has_key_points);
+ key_points_layout_->parentWidget()->setVisible(has_key_points);
// Risk signals — each shows level (color-coded) plus the detail text
// inline (was tooltip-only before), wrapped so it never overflows.
@@ -668,6 +687,11 @@ void NewsDetailPanel::show_analysis(const services::NewsAnalysis& analysis) {
add_risk("Geopolitical", analysis.geopolitical);
add_risk("Operational", analysis.operational);
add_risk("Market", analysis.market);
+ // add_risk skips "none"/empty levels, so an all-clear article adds no rows —
+ // hide the heading + container in that case.
+ const bool has_risks = risk_layout_->count() > 0;
+ risk_title_->setVisible(has_risks);
+ risk_layout_->parentWidget()->setVisible(has_risks);
// Topics — wrapping grid of badges (a QHBoxLayout would overflow the
// panel's fixed width with no way to wrap).
@@ -694,6 +718,9 @@ void NewsDetailPanel::show_analysis(const services::NewsAnalysis& analysis) {
topics_grid->setColumnStretch(c, 1);
topics_layout_->addWidget(topics_row);
}
+ const bool has_topics = !analysis.topics.isEmpty();
+ topics_title_->setVisible(has_topics);
+ topics_layout_->parentWidget()->setVisible(has_topics);
// Entities — organizations (with ticker/sector), people, locations.
while (ai_entities_layout_->count() > 0) {
diff --git a/fincept-qt/src/screens/news/NewsDetailPanel.h b/fincept-qt/src/screens/news/NewsDetailPanel.h
index ccd40aad3..00d67c9e0 100644
--- a/fincept-qt/src/screens/news/NewsDetailPanel.h
+++ b/fincept-qt/src/screens/news/NewsDetailPanel.h
@@ -67,8 +67,11 @@ class NewsDetailPanel : public QWidget {
QLabel* ai_confidence_ = nullptr;
QLabel* ai_keywords_ = nullptr;
QLabel* ai_credits_ = nullptr;
+ QLabel* key_points_title_ = nullptr;
QVBoxLayout* key_points_layout_ = nullptr;
+ QLabel* risk_title_ = nullptr;
QVBoxLayout* risk_layout_ = nullptr;
+ QLabel* topics_title_ = nullptr;
QVBoxLayout* topics_layout_ = nullptr;
QLabel* ai_entities_title_ = nullptr;
QVBoxLayout* ai_entities_layout_ = nullptr;
diff --git a/fincept-qt/src/screens/news/NewsScreen.cpp b/fincept-qt/src/screens/news/NewsScreen.cpp
index c25586ad9..9adc176db 100644
--- a/fincept-qt/src/screens/news/NewsScreen.cpp
+++ b/fincept-qt/src/screens/news/NewsScreen.cpp
@@ -491,6 +491,11 @@ void NewsScreen::on_article_clicked(const services::NewsArticle& article) {
detail_panel_->show_article(article);
feed_panel_->set_selected(article.id);
+ // Re-show a previously-run AI analysis for this article, if one was saved.
+ // Only a fresh ANALYZE click re-fetches/overwrites it.
+ if (auto cached = services::NewsService::instance().cached_analysis(article.link))
+ detail_panel_->show_analysis(*cached);
+
// Find related articles from the same cluster
for (const auto& cluster : clusters_) {
if (cluster.lead_article.id == article.id ||
diff --git a/fincept-qt/src/screens/portfolio/PortfolioCommandBar.cpp b/fincept-qt/src/screens/portfolio/PortfolioCommandBar.cpp
index cb187843a..cc2287a24 100644
--- a/fincept-qt/src/screens/portfolio/PortfolioCommandBar.cpp
+++ b/fincept-qt/src/screens/portfolio/PortfolioCommandBar.cpp
@@ -456,14 +456,6 @@ void PortfolioCommandBar::set_selected_portfolio(const portfolio::Portfolio& p)
update_selector_label();
}
-void PortfolioCommandBar::set_summary(const portfolio::PortfolioSummary& /*s*/) {
- // No-op. The bar no longer displays KPIs \u2014 those moved to the
- // StatsRibbon below the bar (single source of truth for portfolio stats).
- // Method retained as a public no-op so existing wiring in PortfolioScreen
- // doesn't have to change; can be removed in a follow-up once callers drop
- // the call.
-}
-
void PortfolioCommandBar::set_refreshing(bool refreshing) {
refresh_btn_->setEnabled(!refreshing);
refresh_btn_->setText(refreshing ? "\u23F3" : "\u21BB");
diff --git a/fincept-qt/src/screens/portfolio/PortfolioCommandBar.h b/fincept-qt/src/screens/portfolio/PortfolioCommandBar.h
index bfe1dfbf6..90454a2ef 100644
--- a/fincept-qt/src/screens/portfolio/PortfolioCommandBar.h
+++ b/fincept-qt/src/screens/portfolio/PortfolioCommandBar.h
@@ -21,7 +21,6 @@ class PortfolioCommandBar : public QWidget {
void set_portfolios(const QVector<portfolio::Portfolio>& portfolios);
void set_selected_portfolio(const portfolio::Portfolio& p);
- void set_summary(const portfolio::PortfolioSummary& summary);
void set_refreshing(bool refreshing);
void set_detail_view(std::optional<portfolio::DetailView> view);
void set_has_selection(bool has_selection);
diff --git a/fincept-qt/src/screens/portfolio/PortfolioDetailWrapper.cpp b/fincept-qt/src/screens/portfolio/PortfolioDetailWrapper.cpp
index 76e41a3c1..7297f891f 100644
--- a/fincept-qt/src/screens/portfolio/PortfolioDetailWrapper.cpp
+++ b/fincept-qt/src/screens/portfolio/PortfolioDetailWrapper.cpp
@@ -101,6 +101,12 @@ void PortfolioDetailWrapper::update_snapshots(const QVector<portfolio::Portfolio
if (auto* v = qobject_cast<PerformanceRiskView*>(*it))
v->set_snapshots(snapshots);
}
+ // Planning derives its real return/volatility assumptions from snapshots.
+ auto pit = views_.find(static_cast<int>(portfolio::DetailView::Planning));
+ if (pit != views_.end()) {
+ if (auto* p = qobject_cast<PlanningView*>(*pit))
+ p->set_snapshots(snapshots);
+ }
}
void PortfolioDetailWrapper::update_data(const portfolio::PortfolioSummary& summary, const QString& currency) {
@@ -114,9 +120,11 @@ void PortfolioDetailWrapper::update_data(const portfolio::PortfolioSummary& summ
// Update the active view with new data
if (auto* sectors_v = qobject_cast<AnalyticsSectorsView*>(current)) {
sectors_v->set_data(summary, currency);
+ sectors_v->set_correlation(current_correlation_);
} else if (auto* perf_v = qobject_cast<PerformanceRiskView*>(current)) {
perf_v->set_data(summary, currency);
perf_v->set_snapshots(current_snapshots_);
+ perf_v->set_metrics(current_metrics_);
} else if (auto* risk_v = qobject_cast<RiskManagementView*>(current)) {
risk_v->set_data(summary, currency);
risk_v->set_metrics(current_metrics_);
@@ -130,6 +138,8 @@ void PortfolioDetailWrapper::update_data(const portfolio::PortfolioSummary& summ
idx_v->set_data(summary, currency);
} else if (auto* plan_v = qobject_cast<PlanningView*>(current)) {
plan_v->set_data(summary, currency);
+ plan_v->set_snapshots(current_snapshots_);
+ plan_v->set_metrics(current_metrics_);
} else if (auto* econ_v = qobject_cast<EconomicsView*>(current)) {
econ_v->set_data(summary, currency);
}
@@ -142,6 +152,19 @@ void PortfolioDetailWrapper::update_metrics(const portfolio::ComputedMetrics& me
return;
if (auto* v = qobject_cast<RiskManagementView*>(current))
v->set_metrics(metrics);
+ else if (auto* p = qobject_cast<PerformanceRiskView*>(current))
+ p->set_metrics(metrics);
+ else if (auto* plan = qobject_cast<PlanningView*>(current))
+ plan->set_metrics(metrics);
+}
+
+void PortfolioDetailWrapper::update_correlation(const QHash<QString, double>& matrix) {
+ current_correlation_ = matrix;
+ auto* current = view_stack_->currentWidget();
+ if (!current)
+ return;
+ if (auto* v = qobject_cast<AnalyticsSectorsView*>(current))
+ v->set_correlation(matrix);
}
QWidget* PortfolioDetailWrapper::get_or_create_view(portfolio::DetailView view) {
@@ -156,6 +179,8 @@ QWidget* PortfolioDetailWrapper::get_or_create_view(portfolio::DetailView view)
auto* asv = new AnalyticsSectorsView;
connect(asv, &AnalyticsSectorsView::sector_selected,
this, &PortfolioDetailWrapper::sector_selected);
+ if (!current_correlation_.isEmpty())
+ asv->set_correlation(current_correlation_);
widget = asv;
break;
}
@@ -163,6 +188,7 @@ QWidget* PortfolioDetailWrapper::get_or_create_view(portfolio::DetailView view)
auto* prv = new PerformanceRiskView;
if (!current_snapshots_.isEmpty())
prv->set_snapshots(current_snapshots_);
+ prv->set_metrics(current_metrics_);
widget = prv;
break;
}
@@ -184,9 +210,17 @@ QWidget* PortfolioDetailWrapper::get_or_create_view(portfolio::DetailView view)
case portfolio::DetailView::Indices:
widget = new CustomIndexView;
break;
- case portfolio::DetailView::Planning:
- widget = new PlanningView;
+ case portfolio::DetailView::Planning: {
+ auto* pv = new PlanningView;
+ if (!current_snapshots_.isEmpty())
+ pv->set_snapshots(current_snapshots_);
+ pv->set_metrics(current_metrics_);
+ // "Optimize for this return" → navigate to the Optimization sub-tab.
+ connect(pv, &PlanningView::optimize_for_return, this,
+ [this](double target) { emit optimize_requested(target); });
+ widget = pv;
break;
+ }
case portfolio::DetailView::Economics:
widget = new EconomicsView;
break;
diff --git a/fincept-qt/src/screens/portfolio/PortfolioDetailWrapper.h b/fincept-qt/src/screens/portfolio/PortfolioDetailWrapper.h
index c229f8186..075d694d8 100644
--- a/fincept-qt/src/screens/portfolio/PortfolioDetailWrapper.h
+++ b/fincept-qt/src/screens/portfolio/PortfolioDetailWrapper.h
@@ -22,13 +22,15 @@ class PortfolioDetailWrapper : public QWidget {
void update_data(const portfolio::PortfolioSummary& summary, const QString& currency);
void update_snapshots(const QVector<portfolio::PortfolioSnapshot>& snapshots);
void update_metrics(const portfolio::ComputedMetrics& metrics);
+ void update_correlation(const QHash<QString, double>& matrix);
protected:
void changeEvent(QEvent* event) override;
signals:
void back_requested();
- void sector_selected(QString sector); // forwarded from AnalyticsSectorsView
+ void sector_selected(QString sector); // forwarded from AnalyticsSectorsView
+ void optimize_requested(double target_return); // PlanningView → open Optimization tab
private:
void build_ui();
@@ -50,6 +52,7 @@ class PortfolioDetailWrapper : public QWidget {
portfolio::ComputedMetrics current_metrics_;
QString current_currency_;
QVector<portfolio::PortfolioSnapshot> current_snapshots_;
+ QHash<QString, double> current_correlation_; // keyed "SYM_A|SYM_B"
};
} // namespace fincept::screens
diff --git a/fincept-qt/src/screens/portfolio/PortfolioFFNView.cpp b/fincept-qt/src/screens/portfolio/PortfolioFFNView.cpp
index adec5da35..df965e413 100644
--- a/fincept-qt/src/screens/portfolio/PortfolioFFNView.cpp
+++ b/fincept-qt/src/screens/portfolio/PortfolioFFNView.cpp
@@ -6,6 +6,7 @@
#include "ui/theme/Theme.h"
#include <QAreaSeries>
+#include <QTabBar>
#include <QChart>
#include <QChartView>
#include <QDateTimeAxis>
@@ -120,6 +121,9 @@ void PortfolioFFNView::build_ui() {
// ── Tabs ──────────────────────────────────────────────────────────────────
tabs_ = new QTabWidget;
+ tabs_->tabBar()->setElideMode(Qt::ElideNone);
+ tabs_->tabBar()->setExpanding(false);
+ tabs_->tabBar()->setUsesScrollButtons(false);
tabs_->setDocumentMode(true);
tabs_->setStyleSheet(QString("QTabWidget::pane { border:0; background:%1; }"
"QTabBar::tab { background:%2; color:%3; padding:6px 14px; border:0;"
diff --git a/fincept-qt/src/screens/portfolio/PortfolioInsightsPanel.cpp b/fincept-qt/src/screens/portfolio/PortfolioInsightsPanel.cpp
index 4bd875e85..34593c227 100644
--- a/fincept-qt/src/screens/portfolio/PortfolioInsightsPanel.cpp
+++ b/fincept-qt/src/screens/portfolio/PortfolioInsightsPanel.cpp
@@ -4,6 +4,7 @@
#include "services/llm/LlmService.h"
#include "core/logging/Logger.h"
#include "services/agents/AgentService.h"
+#include "storage/repositories/SettingsRepository.h"
#include "ui/markdown/MarkdownRenderer.h"
#include "ui/theme/Theme.h"
@@ -32,6 +33,9 @@ QString fmt_now() {
return QDateTime::currentDateTime().toString("HH:mm:ss");
}
+// SettingsRepository category under which AI/agent results are persisted.
+constexpr const char* kInsightsCategory = "portfolio_insights";
+
// Markdown rendering is delegated to ui::MarkdownRenderer — the same path used
// by AgentChatPanel and AI Chat so portfolio responses look consistent.
@@ -62,7 +66,10 @@ PortfolioInsightsPanel::PortfolioInsightsPanel(QWidget* parent) : QWidget(parent
header_status_->clear();
if (r.success && !r.response.isEmpty()) {
ai_cache_.insert(type, r.response);
- ai_meta_->setText(tr("Last run %1 • %2ms").arg(fmt_now()).arg(r.execution_time_ms));
+ const QString meta = tr("Last run %1 • %2ms").arg(fmt_now()).arg(r.execution_time_ms);
+ ai_meta_cache_.insert(type, meta);
+ ai_meta_->setText(meta);
+ persist_ai_result(type, r.response, meta);
render_result(ai_content_, r.response);
QString upper = type.toUpper();
if (upper == "OPPORTUNITIES")
@@ -118,7 +125,10 @@ PortfolioInsightsPanel::PortfolioInsightsPanel(QWidget* parent) : QWidget(parent
if (r.success && !final_text.isEmpty()) {
agent_cache_.insert(agent_id, final_text);
- agent_meta_->setText(tr("Last run %1 • %2ms").arg(fmt_now()).arg(r.execution_time_ms));
+ const QString meta = tr("Last run %1 • %2ms").arg(fmt_now()).arg(r.execution_time_ms);
+ agent_meta_cache_.insert(agent_id, meta);
+ agent_meta_->setText(meta);
+ persist_agent_result(agent_id, final_text, meta);
render_result(agent_content_, final_text);
agent_run_->setText(tr("RE-RUN AGENT"));
} else if (r.success) {
@@ -367,10 +377,19 @@ QWidget* PortfolioInsightsPanel::build_agent_page() {
return;
agent_desc_->setText(agent_cb_->currentData(Qt::UserRole + 1).toString());
QString id = agent_cb_->currentData().toString();
- if (agent_cache_.contains(id))
+ if (agent_cache_.contains(id)) {
render_result(agent_content_, agent_cache_.value(id));
- else
+ if (agent_meta_)
+ agent_meta_->setText(agent_meta_cache_.value(id));
+ if (agent_run_)
+ agent_run_->setText(tr("RE-RUN AGENT"));
+ } else {
render_empty(agent_content_, tr("Press RUN to analyze your portfolio with this agent."));
+ if (agent_meta_)
+ agent_meta_->clear();
+ if (agent_run_)
+ agent_run_->setText(tr("RUN AGENT"));
+ }
});
cl->addWidget(agent_cb_);
@@ -407,16 +426,99 @@ QWidget* PortfolioInsightsPanel::build_agent_page() {
}
void PortfolioInsightsPanel::set_summary(const portfolio::PortfolioSummary& summary) {
+ summary_ = summary;
+ // Reload only when the portfolio actually changes (or on first show, when
+ // last_portfolio_id_ is empty — e.g. a freshly-recreated panel after the
+ // user navigated away and back). Same-portfolio re-entry keeps the live
+ // in-memory caches untouched.
if (summary.portfolio.id != last_portfolio_id_) {
- ai_cache_.clear();
- agent_cache_.clear();
- if (ai_content_)
- render_empty(ai_content_, tr("Select an analysis type and press RUN."));
- if (agent_content_)
- render_empty(agent_content_, tr("Load a saved agent above and press RUN."));
last_portfolio_id_ = summary.portfolio.id;
+ load_persisted_results(summary.portfolio.id); // clears + repopulates caches from disk
+ // Re-render whatever was previously run for this portfolio so switching
+ // away and back (or restarting the app) shows the saved results.
+ set_ai_type(ai_type_);
+ restore_agent_view();
+ }
+}
+
+void PortfolioInsightsPanel::persist_ai_result(const QString& type, const QString& response, const QString& meta) {
+ if (summary_.portfolio.id.isEmpty())
+ return;
+ QJsonObject o;
+ o["r"] = response;
+ o["m"] = meta;
+ SettingsRepository::instance().set(
+ QString("pi.ai.%1.%2").arg(summary_.portfolio.id, type),
+ QString::fromUtf8(QJsonDocument(o).toJson(QJsonDocument::Compact)), kInsightsCategory);
+}
+
+void PortfolioInsightsPanel::persist_agent_result(const QString& agent_id, const QString& response,
+ const QString& meta) {
+ if (summary_.portfolio.id.isEmpty() || agent_id.isEmpty())
+ return;
+ QJsonObject o;
+ o["r"] = response;
+ o["m"] = meta;
+ SettingsRepository::instance().set(
+ QString("pi.agent.%1.%2").arg(summary_.portfolio.id, agent_id),
+ QString::fromUtf8(QJsonDocument(o).toJson(QJsonDocument::Compact)), kInsightsCategory);
+}
+
+void PortfolioInsightsPanel::load_persisted_results(const QString& portfolio_id) {
+ ai_cache_.clear();
+ agent_cache_.clear();
+ ai_meta_cache_.clear();
+ agent_meta_cache_.clear();
+ if (portfolio_id.isEmpty())
+ return;
+
+ auto r = SettingsRepository::instance().get_by_category(kInsightsCategory);
+ if (r.is_err())
+ return;
+
+ const QString ai_prefix = QString("pi.ai.%1.").arg(portfolio_id);
+ const QString agent_prefix = QString("pi.agent.%1.").arg(portfolio_id);
+ for (const auto& s : r.value()) {
+ QString resp, meta;
+ QJsonParseError err;
+ const auto doc = QJsonDocument::fromJson(s.value.toUtf8(), &err);
+ if (err.error == QJsonParseError::NoError && doc.isObject()) {
+ resp = doc.object().value("r").toString();
+ meta = doc.object().value("m").toString();
+ } else {
+ resp = s.value; // legacy/plain value
+ }
+ if (resp.isEmpty())
+ continue;
+ if (s.key.startsWith(ai_prefix)) {
+ const QString type = s.key.mid(ai_prefix.size());
+ ai_cache_.insert(type, resp);
+ ai_meta_cache_.insert(type, meta);
+ } else if (s.key.startsWith(agent_prefix)) {
+ const QString aid = s.key.mid(agent_prefix.size());
+ agent_cache_.insert(aid, resp);
+ agent_meta_cache_.insert(aid, meta);
+ }
+ }
+}
+
+void PortfolioInsightsPanel::restore_agent_view() {
+ if (!agent_cb_ || !agent_content_)
+ return;
+ const QString aid = agent_cb_->currentData().toString();
+ if (!aid.isEmpty() && agent_cache_.contains(aid)) {
+ render_result(agent_content_, agent_cache_.value(aid));
+ if (agent_meta_)
+ agent_meta_->setText(agent_meta_cache_.value(aid));
+ if (agent_run_)
+ agent_run_->setText(tr("RE-RUN AGENT"));
+ } else {
+ render_empty(agent_content_, tr("Press RUN to analyze your portfolio with this agent."));
+ if (agent_meta_)
+ agent_meta_->clear();
+ if (agent_run_)
+ agent_run_->setText(tr("RUN AGENT"));
}
- summary_ = summary;
}
void PortfolioInsightsPanel::open_tab(Tab tab) {
@@ -463,10 +565,15 @@ void PortfolioInsightsPanel::set_ai_type(const QString& type) {
ai_run_->setText(ai_cache_.contains(type) ? tr("RE-RUN %1 ANALYSIS").arg(upper)
: tr("RUN %1 ANALYSIS").arg(upper));
- if (ai_cache_.contains(type))
+ if (ai_cache_.contains(type)) {
render_result(ai_content_, ai_cache_.value(type));
- else
+ if (ai_meta_)
+ ai_meta_->setText(ai_meta_cache_.value(type));
+ } else {
render_empty(ai_content_, tr("Press RUN to start this analysis."));
+ if (ai_meta_)
+ ai_meta_->clear();
+ }
}
void PortfolioInsightsPanel::reload_agents() {
diff --git a/fincept-qt/src/screens/portfolio/PortfolioInsightsPanel.h b/fincept-qt/src/screens/portfolio/PortfolioInsightsPanel.h
index 134e5c946..bdf979310 100644
--- a/fincept-qt/src/screens/portfolio/PortfolioInsightsPanel.h
+++ b/fincept-qt/src/screens/portfolio/PortfolioInsightsPanel.h
@@ -55,6 +55,14 @@ class PortfolioInsightsPanel : public QWidget {
void retranslateUi();
void retranslate_ai_run_label(); // re-applies "RUN/RE-RUN <type> ANALYSIS" from cache state
+ // Durable result persistence (survives tab switches, panel recreation, and
+ // app restarts). Keyed by portfolio id so each portfolio keeps its own
+ // AI-analysis and agent results.
+ void persist_ai_result(const QString& type, const QString& response, const QString& meta);
+ void persist_agent_result(const QString& agent_id, const QString& response, const QString& meta);
+ void load_persisted_results(const QString& portfolio_id); // repopulates the in-memory caches
+ void restore_agent_view(); // renders the selected agent's cached result
+
// Layout
QLabel* header_title_ = nullptr;
QPushButton* header_close_btn_ = nullptr;
@@ -94,6 +102,8 @@ class PortfolioInsightsPanel : public QWidget {
QString agent_streaming_text_; // accumulated tokens for live rendering
QHash<QString, QString> ai_cache_; // type -> markdown
QHash<QString, QString> agent_cache_; // agent_id -> markdown
+ QHash<QString, QString> ai_meta_cache_; // type -> "Last run …" label
+ QHash<QString, QString> agent_meta_cache_; // agent_id -> "Last run …" label
};
} // namespace fincept::screens
diff --git a/fincept-qt/src/screens/portfolio/PortfolioScreen.cpp b/fincept-qt/src/screens/portfolio/PortfolioScreen.cpp
index 20caffd43..189134c68 100644
--- a/fincept-qt/src/screens/portfolio/PortfolioScreen.cpp
+++ b/fincept-qt/src/screens/portfolio/PortfolioScreen.cpp
@@ -70,6 +70,8 @@ PortfolioScreen::PortfolioScreen(QWidget* parent) : QWidget(parent) {
connect(&svc, &services::PortfolioService::correlation_computed, this, [this](QHash<QString, double> matrix) {
if (sector_panel_)
sector_panel_->set_correlation(matrix);
+ if (detail_wrapper_)
+ detail_wrapper_->update_correlation(matrix);
});
connect(&svc, &services::PortfolioService::spy_history_loaded, this,
[this](QStringList /*dates*/, QVector<double> /*closes*/) {
diff --git a/fincept-qt/src/screens/portfolio/PortfolioScreen_Handlers.cpp b/fincept-qt/src/screens/portfolio/PortfolioScreen_Handlers.cpp
index 4c6502926..2e89e5aaf 100644
--- a/fincept-qt/src/screens/portfolio/PortfolioScreen_Handlers.cpp
+++ b/fincept-qt/src/screens/portfolio/PortfolioScreen_Handlers.cpp
@@ -103,7 +103,6 @@ void PortfolioScreen::on_summary_loaded(portfolio::PortfolioSummary summary) {
current_summary_ = summary;
summary_loaded_ = true;
- command_bar_->set_summary(summary);
command_bar_->set_refreshing(false);
stats_ribbon_->set_summary(summary);
status_bar_->set_summary(summary);
diff --git a/fincept-qt/src/screens/portfolio/PortfolioScreen_Layout.cpp b/fincept-qt/src/screens/portfolio/PortfolioScreen_Layout.cpp
index 031d71448..cdc004401 100644
--- a/fincept-qt/src/screens/portfolio/PortfolioScreen_Layout.cpp
+++ b/fincept-qt/src/screens/portfolio/PortfolioScreen_Layout.cpp
@@ -156,6 +156,15 @@ void PortfolioScreen::build_ui() {
blotter_->set_sector_filter(matching);
});
+ // PlanningView "Optimize for this return" → jump to the Optimization sub-tab.
+ connect(detail_wrapper_, &PortfolioDetailWrapper::optimize_requested, this, [this](double /*target*/) {
+ active_detail_ = portfolio::DetailView::Optimization;
+ detail_wrapper_->show_view(portfolio::DetailView::Optimization, current_summary_,
+ current_summary_.portfolio.currency);
+ command_bar_->set_detail_view(active_detail_);
+ update_content_state();
+ });
+
// FFN view
ffn_view_ = new PortfolioFFNView(this);
connect(ffn_view_, &PortfolioFFNView::back_requested, this, [this]() {
@@ -668,6 +677,10 @@ QWidget* PortfolioScreen::build_main_view() {
});
connect(positions_filter_edit_, &QLineEdit::textChanged, blotter_, &PortfolioBlotter::set_filter);
+ // Guarantee the positions table is tall enough to read several rows even on
+ // shorter windows (it still expands to fill the 60% stretch when space
+ // allows). The table itself shows a vertical scrollbar as-needed.
+ blotter_->setMinimumHeight(300);
blotter_layout->addWidget(blotter_, 1);
root_layout->addWidget(blotter_section, 60); // 60% of vertical space
@@ -676,12 +689,13 @@ QWidget* PortfolioScreen::build_main_view() {
// Sits at the bottom of the main view, full-width. Defaults open at 140px;
// user can collapse via the chevron in its header.
txn_panel_ = new PortfolioTxnPanel;
- txn_panel_->setFixedHeight(140);
+ txn_panel_->setFixedHeight(240);
root_layout->addWidget(txn_panel_);
connect(txn_panel_, &PortfolioTxnPanel::collapse_toggled, this, [this](bool collapsed) {
// Header is 30px, table content is the rest. When collapsed, shrink
- // to header height; when expanded, restore to 140px.
- txn_panel_->setFixedHeight(collapsed ? 30 : 140);
+ // to header height; when expanded, restore to the full height (the
+ // table shows a vertical scrollbar as-needed for longer histories).
+ txn_panel_->setFixedHeight(collapsed ? 30 : 240);
});
// Order panel is a floating overlay (parented to PortfolioScreen, not in layout)
diff --git a/fincept-qt/src/screens/portfolio/PortfolioTypes.h b/fincept-qt/src/screens/portfolio/PortfolioTypes.h
index 05704d5c8..6ac736390 100644
--- a/fincept-qt/src/screens/portfolio/PortfolioTypes.h
+++ b/fincept-qt/src/screens/portfolio/PortfolioTypes.h
@@ -95,7 +95,9 @@ struct PortfolioSummary {
struct ComputedMetrics {
std::optional<double> sharpe;
+ std::optional<double> sortino; // annualized, downside-deviation based
std::optional<double> beta;
+ std::optional<double> alpha; // annualized Jensen's alpha vs benchmark, %
std::optional<double> volatility; // annualized %
std::optional<double> max_drawdown; // %
std::optional<double> var_95; // 1-day VaR in currency
diff --git a/fincept-qt/src/screens/portfolio/views/AnalyticsSectorsView.cpp b/fincept-qt/src/screens/portfolio/views/AnalyticsSectorsView.cpp
index 5d475b935..ce016f5d8 100644
--- a/fincept-qt/src/screens/portfolio/views/AnalyticsSectorsView.cpp
+++ b/fincept-qt/src/screens/portfolio/views/AnalyticsSectorsView.cpp
@@ -4,6 +4,7 @@
#include "ui/theme/Theme.h"
#include <QChart>
+#include <QTabBar>
#include <QChartView>
#include <QEvent>
#include <QFrame>
@@ -125,6 +126,9 @@ void AnalyticsSectorsView::build_ui() {
root->setSpacing(0);
tabs_ = new QTabWidget;
+ tabs_->tabBar()->setElideMode(Qt::ElideNone);
+ tabs_->tabBar()->setExpanding(false);
+ tabs_->tabBar()->setUsesScrollButtons(false);
tabs_->setDocumentMode(true);
tabs_->setStyleSheet(
QString("QTabWidget::pane { border:0; background:%1; }"
@@ -333,8 +337,8 @@ QWidget* AnalyticsSectorsView::build_correlation_tab() {
lay->addWidget(corr_title_);
corr_note_ = new QLabel(
- tr("Top-10 holdings by weight. Values use a day-change sign proxy until "
- "OHLC history is wired in — treat the magnitudes as directional, not precise."));
+ tr("Top-10 holdings by weight. Pearson correlation of daily returns over "
+ "the trailing 30 trading days (from real price history)."));
corr_note_->setWordWrap(true);
corr_note_->setStyleSheet(QString("color:%1; font-size:%2px;")
.arg(ui::colors::TEXT_TERTIARY())
@@ -364,6 +368,11 @@ void AnalyticsSectorsView::set_data(const portfolio::PortfolioSummary& summary,
update_correlation();
}
+void AnalyticsSectorsView::set_correlation(const QHash<QString, double>& matrix) {
+ correlation_matrix_ = matrix;
+ update_correlation();
+}
+
void AnalyticsSectorsView::changeEvent(QEvent* event) {
if (event->type() == QEvent::LanguageChange)
retranslateUi();
@@ -383,8 +392,8 @@ void AnalyticsSectorsView::retranslateUi() {
if (table_title_) table_title_->setText(tr("SECTOR BREAKDOWN"));
if (corr_title_) corr_title_->setText(tr("HOLDINGS CORRELATION MATRIX"));
if (corr_note_)
- corr_note_->setText(tr("Top-10 holdings by weight. Values use a day-change sign proxy until "
- "OHLC history is wired in — treat the magnitudes as directional, not precise."));
+ corr_note_->setText(tr("Top-10 holdings by weight. Pearson correlation of daily returns over "
+ "the trailing 30 trading days (from real price history)."));
if (sector_table_)
sector_table_->setHorizontalHeaderLabels(
@@ -770,6 +779,19 @@ void AnalyticsSectorsView::update_correlation() {
return;
}
+ // Real correlation matrix is fetched asynchronously (PortfolioService::
+ // fetch_correlation → correlation_computed). Until it arrives, show a
+ // loading state rather than fabricating values from day-change signs.
+ if (correlation_matrix_.isEmpty()) {
+ auto* msg = new QLabel(tr("Computing correlations from price history…"), corr_panel_);
+ msg->setAlignment(Qt::AlignCenter);
+ msg->setStyleSheet(QString("color:%1; font-size:%2px; padding:40px; background:transparent;")
+ .arg(ui::colors::TEXT_TERTIARY())
+ .arg(ui::fonts::font_px()));
+ corr_grid_->addWidget(msg, 0, 0);
+ return;
+ }
+
auto sorted = summary_.holdings;
std::sort(sorted.begin(), sorted.end(),
[](const auto& a, const auto& b) { return a.weight > b.weight; });
@@ -796,24 +818,39 @@ void AnalyticsSectorsView::update_correlation() {
corr_grid_->addWidget(make_header(sorted[r].symbol.left(6), false), r + 1, 0);
for (int c = 0; c < n; ++c) {
- double corr;
+ // Real Pearson correlation looked up from the service matrix,
+ // keyed "SYMBOL_A|SYMBOL_B". Try both orderings; if a pair is
+ // missing (e.g. no price history for a symbol) show a blank cell
+ // rather than inventing a value.
+ const QString& sa = sorted[r].symbol;
+ const QString& sb = sorted[c].symbol;
+ bool have = false;
+ double corr = 0.0;
if (r == c) {
corr = 1.0;
+ have = true;
} else {
- double a = sorted[r].day_change_percent;
- double b = sorted[c].day_change_percent;
- bool same_sign = (a >= 0) == (b >= 0);
- double mag = std::min(std::abs(a) + std::abs(b), 10.0) / 10.0;
- corr = same_sign ? (mag * 0.8 + 0.1) : -(mag * 0.6 + 0.05);
+ const QString key = sa + "|" + sb;
+ const QString rkey = sb + "|" + sa;
+ if (correlation_matrix_.contains(key)) {
+ corr = correlation_matrix_.value(key);
+ have = true;
+ } else if (correlation_matrix_.contains(rkey)) {
+ corr = correlation_matrix_.value(rkey);
+ have = true;
+ }
}
- auto* cell = new QLabel(QString::number(corr, 'f', 2));
+ auto* cell = new QLabel(have ? QString::number(corr, 'f', 2) : QStringLiteral("—"));
cell->setAlignment(Qt::AlignCenter);
cell->setFixedSize(54, 26);
QColor bg;
QString fg;
- if (r == c) {
+ if (!have) {
+ bg = QColor(ui::colors::BORDER_DIM());
+ fg = ui::colors::TEXT_TERTIARY();
+ } else if (r == c) {
bg = QColor(ui::colors::AMBER());
fg = ui::colors::BG_BASE();
} else if (corr > 0) {
diff --git a/fincept-qt/src/screens/portfolio/views/AnalyticsSectorsView.h b/fincept-qt/src/screens/portfolio/views/AnalyticsSectorsView.h
index 1cc6d1470..c7a8f3000 100644
--- a/fincept-qt/src/screens/portfolio/views/AnalyticsSectorsView.h
+++ b/fincept-qt/src/screens/portfolio/views/AnalyticsSectorsView.h
@@ -3,6 +3,7 @@
#include "screens/portfolio/PortfolioTypes.h"
#include <QColor>
+#include <QHash>
#include <QWidget>
class QChartView;
@@ -20,14 +21,17 @@ namespace fincept::screens {
/// - OVERVIEW: KPI strip, donut + sector table, 4 performer cards, HHI +
/// top-3 concentration badges. Everything the user needs to
/// reason about sector exposure on one page.
-/// - CORRELATION: symbol×symbol matrix using a day-change proxy until
-/// OHLC history is wired (note displayed inline).
+/// - CORRELATION: symbol×symbol Pearson correlation matrix computed from
+/// real trailing-30-day daily returns (PortfolioService).
class AnalyticsSectorsView : public QWidget {
Q_OBJECT
public:
explicit AnalyticsSectorsView(QWidget* parent = nullptr);
void set_data(const portfolio::PortfolioSummary& summary, const QString& currency);
+ /// Real Pearson correlation matrix keyed "SYMBOL_A|SYMBOL_B", supplied by
+ /// PortfolioService::correlation_computed. Triggers a redraw of the matrix.
+ void set_correlation(const QHash<QString, double>& matrix);
protected:
void changeEvent(QEvent* event) override;
@@ -108,6 +112,7 @@ class AnalyticsSectorsView : public QWidget {
// Data
portfolio::PortfolioSummary summary_;
QString currency_;
+ QHash<QString, double> correlation_matrix_; // keyed "SYM_A|SYM_B"
bool has_data_ = false;
};
diff --git a/fincept-qt/src/screens/portfolio/views/CustomIndexView.cpp b/fincept-qt/src/screens/portfolio/views/CustomIndexView.cpp
index de2895717..f59b1b101 100644
--- a/fincept-qt/src/screens/portfolio/views/CustomIndexView.cpp
+++ b/fincept-qt/src/screens/portfolio/views/CustomIndexView.cpp
@@ -6,6 +6,7 @@
#define QT_CHARTS_USE_NAMESPACE
#include <QChart>
+#include <QTabBar>
#include <QChartView>
#include <QDateTimeAxis>
#include <QEvent>
@@ -75,6 +76,9 @@ void CustomIndexView::build_ui() {
layout->setContentsMargins(0, 0, 0, 0);
tabs_ = new QTabWidget;
+ tabs_->tabBar()->setElideMode(Qt::ElideNone);
+ tabs_->tabBar()->setExpanding(false);
+ tabs_->tabBar()->setUsesScrollButtons(false);
tabs_->setDocumentMode(true);
tabs_->setStyleSheet(QString("QTabWidget::pane { border:0; background:%1; }"
"QTabBar::tab { background:%2; color:%3; padding:6px 14px; border:0;"
diff --git a/fincept-qt/src/screens/portfolio/views/EconomicsView.cpp b/fincept-qt/src/screens/portfolio/views/EconomicsView.cpp
index 99de85e82..d6417c720 100644
--- a/fincept-qt/src/screens/portfolio/views/EconomicsView.cpp
+++ b/fincept-qt/src/screens/portfolio/views/EconomicsView.cpp
@@ -1,11 +1,23 @@
// src/screens/portfolio/views/EconomicsView.cpp
#include "screens/portfolio/views/EconomicsView.h"
+#include "python/PythonRunner.h"
+#include "storage/secure/SecureStorage.h"
#include "ui/theme/Theme.h"
+#include <QColor>
+#include <QDate>
#include <QEvent>
#include <QHBoxLayout>
#include <QHeaderView>
+#include <QInputDialog>
+#include <QJsonArray>
+#include <QJsonDocument>
+#include <QJsonObject>
+#include <QLineEdit>
+#include <QPointer>
+#include <QPushButton>
+#include <QTableWidgetItem>
#include <QVBoxLayout>
#include <algorithm>
@@ -60,7 +72,7 @@ void EconomicsView::build_ui() {
.arg(ui::colors::BG_BASE(), ui::colors::TEXT_PRIMARY(), ui::colors::BORDER_DIM(),
ui::colors::BG_SURFACE(), ui::colors::TEXT_SECONDARY(), ui::colors::AMBER()));
ind_layout->addWidget(indicators_table_, 1);
- layout->addWidget(ind_section, 6);
+ layout->addWidget(ind_section, 5);
// Separator
auto* sep = new QWidget(this);
@@ -68,6 +80,71 @@ void EconomicsView::build_ui() {
sep->setStyleSheet(QString("background:%1;").arg(ui::colors::BORDER_DIM()));
layout->addWidget(sep);
+ // ── Middle: live macro conditions (FRED) ──────────────────────────────────
+ auto* macro_section = new QWidget(this);
+ auto* macro_layout = new QVBoxLayout(macro_section);
+ macro_layout->setContentsMargins(12, 8, 12, 8);
+ macro_layout->setSpacing(4);
+
+ macro_title_ = new QLabel(tr("CURRENT MACRO CONDITIONS (LIVE · FRED)"));
+ macro_title_->setStyleSheet(
+ QString("color:%1; font-size:11px; font-weight:700; letter-spacing:1px;").arg(ui::colors::AMBER()));
+ macro_layout->addWidget(macro_title_);
+
+ macro_note_ = new QLabel(tr("Loading live macro data…"));
+ macro_note_->setWordWrap(true);
+ macro_note_->setStyleSheet(QString("color:%1; font-size:9px;").arg(ui::colors::TEXT_TERTIARY()));
+ macro_layout->addWidget(macro_note_);
+
+ // Shown only when no FRED key is configured — lets the user paste one inline.
+ macro_set_key_btn_ = new QPushButton(tr("➜ SET FREE FRED API KEY"));
+ macro_set_key_btn_->setCursor(Qt::PointingHandCursor);
+ macro_set_key_btn_->setFixedHeight(24);
+ macro_set_key_btn_->setStyleSheet(
+ QString("QPushButton { background:transparent; color:%1; border:1px solid %1; font-size:9px;"
+ " font-weight:700; letter-spacing:1px; padding:0 10px; } QPushButton:hover { background:%1; color:#000; }")
+ .arg(ui::colors::AMBER()));
+ macro_set_key_btn_->setVisible(false);
+ connect(macro_set_key_btn_, &QPushButton::clicked, this, [this]() {
+ bool ok = false;
+ const QString key = QInputDialog::getText(
+ this, tr("FRED API Key"),
+ tr("Paste your free FRED API key (get one at https://fredaccount.stlouisfed.org/apikeys).\n"
+ "It is stored encrypted and shared with all FRED-backed features."),
+ QLineEdit::Normal, QString(), &ok);
+ if (!ok || key.trimmed().isEmpty())
+ return;
+ fincept::SecureStorage::instance().store("FRED_API_KEY", key.trimmed());
+ macro_loaded_ = false; // force a refetch with the new key
+ fetch_macro();
+ });
+ {
+ auto* btn_row = new QHBoxLayout;
+ btn_row->setContentsMargins(0, 0, 0, 0);
+ btn_row->addWidget(macro_set_key_btn_);
+ btn_row->addStretch();
+ macro_layout->addLayout(btn_row);
+ }
+
+ macro_table_ = new QTableWidget;
+ macro_table_->setColumnCount(3);
+ macro_table_->setHorizontalHeaderLabels({tr("INDICATOR"), tr("CURRENT"), tr("AS OF")});
+ macro_table_->setSelectionMode(QAbstractItemView::NoSelection);
+ macro_table_->setEditTriggers(QAbstractItemView::NoEditTriggers);
+ macro_table_->setShowGrid(false);
+ macro_table_->verticalHeader()->setVisible(false);
+ macro_table_->horizontalHeader()->setStretchLastSection(true);
+ macro_table_->setColumnWidth(0, 220);
+ macro_table_->setColumnWidth(1, 140);
+ macro_table_->setStyleSheet(indicators_table_->styleSheet());
+ macro_layout->addWidget(macro_table_, 1);
+ layout->addWidget(macro_section, 3);
+
+ auto* sep2 = new QWidget(this);
+ sep2->setFixedHeight(1);
+ sep2->setStyleSheet(QString("background:%1;").arg(ui::colors::BORDER_DIM()));
+ layout->addWidget(sep2);
+
// ── Bottom: Portfolio Factor Sensitivity ──────────────────────────────────
auto* sens_section = new QWidget(this);
auto* sens_layout = new QVBoxLayout(sens_section);
@@ -102,6 +179,107 @@ void EconomicsView::set_data(const portfolio::PortfolioSummary& summary, const Q
has_data_ = true;
update_indicators();
update_sensitivity();
+ // Macro data is portfolio-independent — fetch once per view lifetime.
+ if (!macro_loaded_ && !macro_loading_)
+ fetch_macro();
+}
+
+// ── Live macro conditions (FRED) ──────────────────────────────────────────────
+
+namespace {
+// FRED series → friendly label + unit suffix, in display order.
+struct MacroSeries {
+ const char* id;
+ const char* label;
+ const char* suffix;
+};
+const MacroSeries kMacroSeries[] = {
+ {"DGS10", "10Y Treasury Yield", "%"},
+ {"T10YIE", "Inflation (10Y breakeven)", "%"},
+ {"A191RL1Q225SBEA", "Real GDP Growth (annualised)", "%"},
+ {"DTWEXBGS", "USD Index (broad)", ""},
+ {"DCOILWTICO", "WTI Crude Oil", " $/bbl"},
+};
+} // namespace
+
+void EconomicsView::fetch_macro() {
+ macro_loading_ = true;
+ if (macro_note_)
+ macro_note_->setText(tr("Loading live macro data from FRED…"));
+
+ // Fetch the last ~120 days so we get a recent observation cheaply.
+ QStringList args;
+ args << "multiple";
+ for (const auto& m : kMacroSeries)
+ args << m.id;
+ args << QDate::currentDate().addDays(-120).toString("yyyy-MM-dd");
+
+ QPointer<EconomicsView> self = this;
+ fincept::python::PythonRunner::instance().run(
+ "fred_data.py", args, [self](const fincept::python::PythonResult& r) {
+ if (!self)
+ return;
+ self->macro_loading_ = false;
+ self->macro_loaded_ = true;
+ self->macro_values_.clear();
+ self->macro_dates_.clear();
+
+ const auto doc = QJsonDocument::fromJson(r.output.trimmed().toUtf8());
+ if (!r.success || !doc.isArray()) {
+ self->macro_status_ =
+ tr("Could not load live macro data. Add a free FRED API key in Settings → API Credentials.");
+ self->update_macro_table();
+ return;
+ }
+ for (const auto v : doc.array()) {
+ const auto o = v.toObject();
+ if (o.contains("error"))
+ continue;
+ const QString id = o.value("series_id").toString();
+ const auto obs = o.value("observations").toArray();
+ if (obs.isEmpty())
+ continue;
+ const auto last = obs.last().toObject();
+ self->macro_values_[id] = last.value("value").toDouble();
+ self->macro_dates_[id] = last.value("date").toString();
+ }
+ self->macro_status_.clear();
+ self->update_macro_table();
+ });
+}
+
+void EconomicsView::update_macro_table() {
+ if (!macro_table_)
+ return;
+ if (macro_note_) {
+ macro_note_->setText(macro_status_.isEmpty()
+ ? tr("Live levels from the U.S. Federal Reserve (FRED). Your factor exposures below "
+ "show how the portfolio reacts to moves in each.")
+ : macro_status_);
+ macro_note_->setStyleSheet(QString("color:%1; font-size:9px;")
+ .arg(macro_status_.isEmpty() ? ui::colors::TEXT_TERTIARY() : ui::colors::WARNING()));
+ }
+ // Offer the inline key-entry button whenever live data failed to load.
+ if (macro_set_key_btn_)
+ macro_set_key_btn_->setVisible(!macro_status_.isEmpty());
+ const int n = static_cast<int>(sizeof(kMacroSeries) / sizeof(kMacroSeries[0]));
+ macro_table_->setRowCount(n);
+ for (int r = 0; r < n; ++r) {
+ const auto& m = kMacroSeries[r];
+ macro_table_->setRowHeight(r, 26);
+ auto set = [&](int col, const QString& text, const char* color, int align) {
+ auto* it = new QTableWidgetItem(text);
+ it->setTextAlignment(static_cast<Qt::Alignment>(align) | Qt::AlignVCenter);
+ if (color)
+ it->setForeground(QColor(color));
+ macro_table_->setItem(r, col, it);
+ };
+ const bool have = macro_values_.contains(m.id);
+ const QString val = have ? (QString::number(macro_values_.value(m.id), 'f', 2) + m.suffix) : QStringLiteral("—");
+ set(0, tr(m.label), ui::colors::TEXT_PRIMARY, Qt::AlignLeft);
+ set(1, val, have ? ui::colors::CYAN : ui::colors::TEXT_TERTIARY, Qt::AlignRight);
+ set(2, macro_dates_.value(m.id, QStringLiteral("—")), ui::colors::TEXT_TERTIARY, Qt::AlignRight);
+ }
}
void EconomicsView::changeEvent(QEvent* event) {
diff --git a/fincept-qt/src/screens/portfolio/views/EconomicsView.h b/fincept-qt/src/screens/portfolio/views/EconomicsView.h
index c3d1e5f8c..98313bf21 100644
--- a/fincept-qt/src/screens/portfolio/views/EconomicsView.h
+++ b/fincept-qt/src/screens/portfolio/views/EconomicsView.h
@@ -2,7 +2,9 @@
#pragma once
#include "screens/portfolio/PortfolioTypes.h"
+#include <QHash>
#include <QLabel>
+#include <QString>
#include <QTableWidget>
#include <QWidget>
@@ -24,6 +26,8 @@ class EconomicsView : public QWidget {
void retranslateUi();
void update_indicators();
void update_sensitivity();
+ void fetch_macro(); // pull live FRED macro levels (async)
+ void update_macro_table(); // render macro_values_ into macro_table_
// Section titles/notes
QLabel* ind_title_ = nullptr;
@@ -37,6 +41,17 @@ class EconomicsView : public QWidget {
// Sensitivity matrix
QTableWidget* sensitivity_table_ = nullptr;
+ // Live macro conditions (FRED)
+ QLabel* macro_title_ = nullptr;
+ QLabel* macro_note_ = nullptr;
+ QPushButton* macro_set_key_btn_ = nullptr; // shown when no FRED key configured
+ QTableWidget* macro_table_ = nullptr;
+ QHash<QString, double> macro_values_; // series_id -> latest value
+ QHash<QString, QString> macro_dates_; // series_id -> as-of date
+ QString macro_status_; // "" ok, else error/hint
+ bool macro_loading_ = false;
+ bool macro_loaded_ = false;
+
portfolio::PortfolioSummary summary_;
QString currency_;
bool has_data_ = false;
diff --git a/fincept-qt/src/screens/portfolio/views/PerformanceRiskView.cpp b/fincept-qt/src/screens/portfolio/views/PerformanceRiskView.cpp
index 670efa48c..61aad0d9f 100644
--- a/fincept-qt/src/screens/portfolio/views/PerformanceRiskView.cpp
+++ b/fincept-qt/src/screens/portfolio/views/PerformanceRiskView.cpp
@@ -1,6 +1,7 @@
// src/screens/portfolio/views/PerformanceRiskView.cpp
#include "screens/portfolio/views/PerformanceRiskView.h"
+#include "storage/secure/SecureStorage.h"
#include "ui/theme/Theme.h"
#include <QAreaSeries>
@@ -88,6 +89,15 @@ void PerformanceRiskView::build_ui() {
.arg(ui::colors::TEXT_SECONDARY(), ui::colors::BG_SURFACE()));
layout->addWidget(metrics_header_);
+ // Shown only when no FRED API key is configured — Sharpe/Sortino then use a
+ // flat 4% risk-free rate instead of the live 10-year Treasury yield.
+ rf_hint_ = new QLabel;
+ rf_hint_->setWordWrap(true);
+ rf_hint_->setStyleSheet(QString("color:%1; font-size:9px; padding:2px 12px; background:transparent;")
+ .arg(ui::colors::WARNING()));
+ rf_hint_->setVisible(false);
+ layout->addWidget(rf_hint_);
+
auto* cards_layout = new QGridLayout;
cards_layout->setContentsMargins(12, 8, 12, 8);
cards_layout->setSpacing(8);
@@ -95,11 +105,11 @@ void PerformanceRiskView::build_ui() {
sharpe_card_ = add_metric_card(cards_layout, tr("SHARPE RATIO"), tr("Risk-adjusted return (annualised)"), ui::colors::CYAN);
sortino_card_ = add_metric_card(cards_layout, tr("SORTINO RATIO"), tr("Downside risk-adjusted return"), ui::colors::CYAN);
beta_card_ = add_metric_card(cards_layout, tr("BETA"), tr("Sensitivity vs SPY (snapshot regression)"), ui::colors::WARNING);
- alpha_card_ = add_metric_card(cards_layout, tr("ALPHA"), tr("Excess return vs 8% annual benchmark"), ui::colors::POSITIVE);
+ alpha_card_ = add_metric_card(cards_layout, tr("ALPHA"), tr("Jensen's alpha vs benchmark (CAPM)"), ui::colors::POSITIVE);
vol_card_ = add_metric_card(cards_layout, tr("VOLATILITY"), tr("Annualised from daily returns"), ui::colors::AMBER);
drawdown_card_ =
add_metric_card(cards_layout, tr("MAX DRAWDOWN"), tr("Peak-to-trough from snapshots"), ui::colors::NEGATIVE);
- var_card_ = add_metric_card(cards_layout, tr("VALUE AT RISK (95%)"), tr("1-day parametric VaR"), ui::colors::NEGATIVE);
+ var_card_ = add_metric_card(cards_layout, tr("VALUE AT RISK (95%)"), tr("1-day historical VaR"), ui::colors::NEGATIVE);
cvar_card_ = add_metric_card(cards_layout, tr("CONDITIONAL VaR"), tr("Expected shortfall (95%)"), ui::colors::NEGATIVE);
auto* cards_widget = new QWidget(this);
@@ -156,6 +166,25 @@ void PerformanceRiskView::set_snapshots(const QVector<portfolio::PortfolioSnapsh
update_metrics();
}
+void PerformanceRiskView::set_metrics(const portfolio::ComputedMetrics& metrics) {
+ metrics_ = metrics;
+ update_metrics();
+}
+
+void PerformanceRiskView::update_rf_hint() {
+ if (!rf_hint_)
+ return;
+ // Sharpe/Sortino use the live 10-year Treasury yield from FRED when a key is
+ // configured; otherwise they fall back to a flat 4%. Surface that so the
+ // numbers aren't silently approximate.
+ const auto key = fincept::SecureStorage::instance().retrieve("FRED_API_KEY");
+ const bool have_key = key.is_ok() && !key.value().trimmed().isEmpty();
+ rf_hint_->setVisible(!have_key);
+ if (!have_key)
+ rf_hint_->setText(tr("⚠ Using a default 4% risk-free rate for Sharpe/Sortino. "
+ "Add a free FRED API key in Settings → API Credentials for the live Treasury yield."));
+}
+
void PerformanceRiskView::changeEvent(QEvent* event) {
if (event->type() == QEvent::LanguageChange)
retranslateUi();
@@ -175,13 +204,13 @@ void PerformanceRiskView::retranslateUi() {
if (beta_card_.title) beta_card_.title->setText(tr("BETA"));
if (beta_card_.desc) beta_card_.desc->setText(tr("Sensitivity vs SPY (snapshot regression)"));
if (alpha_card_.title) alpha_card_.title->setText(tr("ALPHA"));
- if (alpha_card_.desc) alpha_card_.desc->setText(tr("Excess return vs 8% annual benchmark"));
+ if (alpha_card_.desc) alpha_card_.desc->setText(tr("Jensen's alpha vs benchmark (CAPM)"));
if (vol_card_.title) vol_card_.title->setText(tr("VOLATILITY"));
if (vol_card_.desc) vol_card_.desc->setText(tr("Annualised from daily returns"));
if (drawdown_card_.title) drawdown_card_.title->setText(tr("MAX DRAWDOWN"));
if (drawdown_card_.desc) drawdown_card_.desc->setText(tr("Peak-to-trough from snapshots"));
if (var_card_.title) var_card_.title->setText(tr("VALUE AT RISK (95%)"));
- if (var_card_.desc) var_card_.desc->setText(tr("1-day parametric VaR"));
+ if (var_card_.desc) var_card_.desc->setText(tr("1-day historical VaR"));
if (cvar_card_.title) cvar_card_.title->setText(tr("CONDITIONAL VaR"));
if (cvar_card_.desc) cvar_card_.desc->setText(tr("Expected shortfall (95%)"));
@@ -257,20 +286,21 @@ void PerformanceRiskView::update_chart() {
min_val = std::min(min_val, last_val);
max_val = std::max(max_val, last_val);
} else {
- // Fallback: interpolate cost → current
- int pts = 30;
+ // Insufficient snapshot history (backfill not yet complete): draw an
+ // honest straight segment connecting the two real values we do know —
+ // cost basis at acquisition and current market value today. No
+ // fabricated intermediate movement.
QDateTime now = QDateTime::currentDateTime();
- for (int i = 0; i < pts; ++i) {
- double t = static_cast<double>(i) / (pts - 1);
- double val = first_val + (last_val - first_val) * t;
- val *= (1.0 + 0.005 * std::sin(i * 0.7));
- qint64 ms = now.addDays(-(pts - 1 - i)).toMSecsSinceEpoch();
- line->append(ms, val);
- upper->append(ms, val);
- lower->append(ms, first_val);
- min_val = std::min(min_val, val);
- max_val = std::max(max_val, val);
- }
+ qint64 start_ms = now.addDays(-30).toMSecsSinceEpoch();
+ qint64 now_ms = now.toMSecsSinceEpoch();
+ line->append(start_ms, first_val);
+ upper->append(start_ms, first_val);
+ lower->append(start_ms, first_val);
+ line->append(now_ms, last_val);
+ upper->append(now_ms, last_val);
+ lower->append(now_ms, first_val);
+ min_val = std::min({min_val, first_val, last_val});
+ max_val = std::max({max_val, first_val, last_val});
}
bool up = last_val >= first_val;
@@ -313,153 +343,93 @@ void PerformanceRiskView::update_chart() {
}
void PerformanceRiskView::update_metrics() {
+ update_rf_hint();
auto fmt = [](double v, int dp = 2) { return QString::number(v, 'f', dp); };
- // ── Build daily return series from snapshots ───────────────────────────
- // daily_ret[i] = (val[i] - val[i-1]) / val[i-1]
- auto snaps = snapshots_;
- std::sort(snaps.begin(), snaps.end(),
- [](const auto& a, const auto& b) { return a.snapshot_date < b.snapshot_date; });
-
- QVector<double> daily_ret;
- for (int i = 1; i < snaps.size(); ++i) {
- double prev = snaps[i - 1].total_value;
- double curr = snaps[i].total_value;
- if (prev > 1.0)
- daily_ret.append((curr - prev) / prev * 100.0);
- }
-
- // Fall back to using holdings day-change if no snapshot history
- if (daily_ret.size() < 3) {
- for (const auto& h : summary_.holdings)
- if (std::abs(h.day_change_percent) > 0.001)
- daily_ret.append(h.day_change_percent);
- }
-
- int n = daily_ret.size();
- double daily_mean = 0, daily_vol = 0;
-
- if (n >= 2) {
- daily_mean = std::accumulate(daily_ret.begin(), daily_ret.end(), 0.0) / n;
- double var = 0;
- for (double r : daily_ret)
- var += (r - daily_mean) * (r - daily_mean);
- daily_vol = std::sqrt(var / n);
- }
-
- double ann_vol = daily_vol * std::sqrt(252.0);
- double risk_free_daily = 4.0 / 252.0; // 4% annual
-
- // ── Sharpe ────────────────────────────────────────────────────────────────
- if (daily_vol > 0.001) {
- double sharpe = (daily_mean - risk_free_daily) / daily_vol * std::sqrt(252.0);
- sharpe_card_.value->setText(fmt(sharpe));
- const char* c = sharpe >= 1.0 ? ui::colors::POSITIVE : sharpe >= 0 ? ui::colors::WARNING : ui::colors::NEGATIVE;
- sharpe_card_.value->setStyleSheet(QString("color:%1; font-size:18px; font-weight:700; border:none;").arg(c));
+ // All headline metrics are sourced from the service's ComputedMetrics
+ // (PortfolioService::compute_metrics), which derives them from real daily
+ // snapshots and a real OLS regression against the currency benchmark. The
+ // view no longer recomputes anything from assumed market returns. When a
+ // metric is unset (insufficient history / no benchmark alignment), the card
+ // shows "N/A" rather than a fabricated value.
+ const QString na = tr("N/A");
+ const QString dim_style =
+ QString("color:%1; font-size:18px; font-weight:700; border:none;").arg(ui::colors::TEXT_TERTIARY());
+
+ auto set_na = [&](MetricCard& card) {
+ if (!card.value)
+ return;
+ card.value->setText(na);
+ card.value->setStyleSheet(dim_style);
+ };
+ auto set_colored = [&](MetricCard& card, const QString& text, const char* color) {
+ if (!card.value)
+ return;
+ card.value->setText(text);
+ card.value->setStyleSheet(
+ QString("color:%1; font-size:18px; font-weight:700; border:none;").arg(color));
+ };
+
+ // ── Sharpe ──────────────────────────────────────────────────────────────
+ if (metrics_.sharpe) {
+ const double v = *metrics_.sharpe;
+ const char* c = v >= 1.0 ? ui::colors::POSITIVE : v >= 0 ? ui::colors::WARNING : ui::colors::NEGATIVE;
+ set_colored(sharpe_card_, fmt(v), c);
+ } else {
+ set_na(sharpe_card_);
}
- // ── Sortino (downside deviation only) ────────────────────────────────────
- double neg_sq = 0;
- int neg_n = 0;
- for (double r : daily_ret) {
- if (r < 0) {
- neg_sq += r * r;
- ++neg_n;
- }
- }
- if (neg_n > 0) {
- double dd = std::sqrt(neg_sq / neg_n);
- if (dd > 0.001) {
- double sortino = (daily_mean - risk_free_daily) / dd * std::sqrt(252.0);
- sortino_card_.value->setText(fmt(sortino));
- }
+ // ── Sortino ─────────────────────────────────────────────────────────────
+ if (metrics_.sortino) {
+ const double v = *metrics_.sortino;
+ const char* c = v >= 1.0 ? ui::colors::POSITIVE : v >= 0 ? ui::colors::WARNING : ui::colors::NEGATIVE;
+ set_colored(sortino_card_, fmt(v), c);
+ } else {
+ set_na(sortino_card_);
}
- // ── Beta (OLS regression of portfolio daily returns vs assumed market 0.08%/day)
- // We don't have real SPY daily data, so we compute beta from the actual
- // snapshot return variance relative to a 8% annual market assumption:
- // beta ≈ cov(portfolio, market) / var(market)
- // With market daily return assumed ~N(0.032%, 1%), we proxy:
- // beta = daily_mean / 0.032 (mean-based slope)
- double market_daily = 8.0 / 252.0 / 100.0 * 100.0; // 0.0317%
- if (n >= 5 && daily_vol > 0.001) {
- double beta = (market_daily > 0.001) ? (daily_mean / market_daily) : 1.0;
- // Constrain to reasonable range
- beta = std::max(-3.0, std::min(5.0, beta));
- beta_card_.value->setText(fmt(beta));
- const char* c = std::abs(beta - 1.0) < 0.2 ? ui::colors::POSITIVE
- : std::abs(beta - 1.0) < 0.5 ? ui::colors::WARNING
- : ui::colors::NEGATIVE;
- beta_card_.value->setStyleSheet(QString("color:%1; font-size:18px; font-weight:700; border:none;").arg(c));
- } else if (n >= 2) {
- // Minimal data: estimate from pnl% relative to assumed 8% market
- double total_pnl_pct = summary_.total_unrealized_pnl_percent;
- double beta = total_pnl_pct / 8.0;
- beta = std::max(-3.0, std::min(5.0, beta));
- beta_card_.value->setText(fmt(beta));
+ // ── Beta (real OLS vs benchmark) ──────────────────────────────────────────
+ if (metrics_.beta) {
+ const double v = *metrics_.beta;
+ const char* c = std::abs(v - 1.0) < 0.2 ? ui::colors::POSITIVE
+ : std::abs(v - 1.0) < 0.5 ? ui::colors::WARNING
+ : ui::colors::NEGATIVE;
+ set_colored(beta_card_, fmt(v), c);
+ } else {
+ set_na(beta_card_);
}
- // ── Alpha (annualised excess over 8% benchmark) ───────────────────────────
- // Alpha = annualised portfolio return - 8%
- if (snaps.size() >= 2) {
- double first = snaps.first().total_value;
- double last = snaps.last().total_value;
- int days =
- static_cast<int>(snaps.first().snapshot_date.left(10) < snaps.last().snapshot_date.left(10)
- ? QDate::fromString(snaps.first().snapshot_date.left(10), Qt::ISODate)
- .daysTo(QDate::fromString(snaps.last().snapshot_date.left(10), Qt::ISODate))
- : 1);
- if (first > 1.0 && days > 0) {
- double total_ret = (last - first) / first * 100.0;
- double ann_ret = total_ret * 365.0 / days;
- double alpha = ann_ret - 8.0;
- alpha_card_.value->setText(QString("%1%2%").arg(alpha >= 0 ? "+" : "").arg(fmt(alpha, 1)));
- const char* c = alpha >= 0 ? ui::colors::POSITIVE : ui::colors::NEGATIVE;
- alpha_card_.value->setStyleSheet(QString("color:%1; font-size:18px; font-weight:700; border:none;").arg(c));
- }
+ // ── Alpha (real Jensen's alpha vs benchmark) ──────────────────────────────
+ if (metrics_.alpha) {
+ const double v = *metrics_.alpha;
+ const char* c = v >= 0 ? ui::colors::POSITIVE : ui::colors::NEGATIVE;
+ set_colored(alpha_card_, QString("%1%2%").arg(v >= 0 ? "+" : "").arg(fmt(v, 1)), c);
} else {
- double alpha = summary_.total_unrealized_pnl_percent - 8.0;
- alpha_card_.value->setText(QString("%1%2%").arg(alpha >= 0 ? "+" : "").arg(fmt(alpha, 1)));
+ set_na(alpha_card_);
}
- // ── Volatility ────────────────────────────────────────────────────────────
- if (n >= 2) {
- vol_card_.value->setText(QString("%1%").arg(fmt(ann_vol, 1)));
- }
+ // ── Volatility (annualised %) ─────────────────────────────────────────────
+ if (metrics_.volatility)
+ set_colored(vol_card_, QString("%1%").arg(fmt(*metrics_.volatility, 1)), ui::colors::TEXT_PRIMARY);
+ else
+ set_na(vol_card_);
- // ── Max Drawdown from snapshots ───────────────────────────────────────────
- if (snaps.size() >= 2) {
- double peak = 0, max_dd = 0;
- for (const auto& s : snaps) {
- peak = std::max(peak, s.total_value);
- if (peak > 0) {
- double dd = (peak - s.total_value) / peak * 100.0;
- max_dd = std::max(max_dd, dd);
- }
- }
- // Also check against current value
- peak = std::max(peak, summary_.total_market_value);
- double cur_dd = peak > 0 ? (peak - summary_.total_market_value) / peak * 100.0 : 0;
- max_dd = std::max(max_dd, cur_dd);
- drawdown_card_.value->setText(QString("-%1%").arg(fmt(max_dd, 1)));
- } else {
- // Fallback: if current < cost basis, that is the drawdown
- double dd = summary_.total_unrealized_pnl < 0 ? std::abs(summary_.total_unrealized_pnl_percent) : 0;
- drawdown_card_.value->setText(QString("-%1%").arg(fmt(dd, 1)));
- }
+ // ── Max Drawdown (% , negative) ───────────────────────────────────────────
+ if (metrics_.max_drawdown)
+ set_colored(drawdown_card_, QString("%1%").arg(fmt(*metrics_.max_drawdown, 1)), ui::colors::NEGATIVE);
+ else
+ set_na(drawdown_card_);
- // ── VaR 95% (parametric, 1-day) ───────────────────────────────────────────
- double mv = summary_.total_market_value;
- if (daily_vol > 0.001 && mv > 0) {
- // Parametric: VaR = MV * (mean - 1.645 * sigma) / 100
- double var95 = mv * std::abs(daily_mean - 1.645 * daily_vol) / 100.0;
- var_card_.value->setText(QString("%1 %2").arg(currency_, fmt(var95)));
-
- // CVaR ≈ E[loss | loss > VaR], for normal: CVaR = MV * (phi(1.645)/0.05) * sigma
- // phi(1.645) = 0.1031, so CVaR ≈ 1.546 * VaR
- double cvar95 = var95 * 1.546;
- cvar_card_.value->setText(QString("%1 %2").arg(currency_, fmt(cvar95)));
- }
+ // ── VaR / CVaR 95% (historical, in currency) ──────────────────────────────
+ if (metrics_.var_95)
+ set_colored(var_card_, QString("%1 %2").arg(currency_, fmt(*metrics_.var_95)), ui::colors::TEXT_PRIMARY);
+ else
+ set_na(var_card_);
+
+ if (metrics_.cvar_95)
+ set_colored(cvar_card_, QString("%1 %2").arg(currency_, fmt(*metrics_.cvar_95)), ui::colors::TEXT_PRIMARY);
+ else
+ set_na(cvar_card_);
}
} // namespace fincept::screens
diff --git a/fincept-qt/src/screens/portfolio/views/PerformanceRiskView.h b/fincept-qt/src/screens/portfolio/views/PerformanceRiskView.h
index 18f543736..2123eca26 100644
--- a/fincept-qt/src/screens/portfolio/views/PerformanceRiskView.h
+++ b/fincept-qt/src/screens/portfolio/views/PerformanceRiskView.h
@@ -17,6 +17,7 @@ class PerformanceRiskView : public QWidget {
void set_data(const portfolio::PortfolioSummary& summary, const QString& currency);
void set_snapshots(const QVector<portfolio::PortfolioSnapshot>& snapshots);
+ void set_metrics(const portfolio::ComputedMetrics& metrics);
protected:
void changeEvent(QEvent* event) override;
@@ -35,6 +36,9 @@ class PerformanceRiskView : public QWidget {
// Header labels
QLabel* chart_title_ = nullptr;
QLabel* metrics_header_ = nullptr;
+ QLabel* rf_hint_ = nullptr; // shown only when no FRED key is configured
+
+ void update_rf_hint();
// Chart
QChartView* chart_view_ = nullptr;
@@ -59,6 +63,7 @@ class PerformanceRiskView : public QWidget {
// Data
portfolio::PortfolioSummary summary_;
QVector<portfolio::PortfolioSnapshot> snapshots_;
+ portfolio::ComputedMetrics metrics_;
QString currency_;
bool has_data_ = false;
};
diff --git a/fincept-qt/src/screens/portfolio/views/PlanningView.cpp b/fincept-qt/src/screens/portfolio/views/PlanningView.cpp
index ecdd41b77..59ab8f335 100644
--- a/fincept-qt/src/screens/portfolio/views/PlanningView.cpp
+++ b/fincept-qt/src/screens/portfolio/views/PlanningView.cpp
@@ -1,15 +1,29 @@
// src/screens/portfolio/views/PlanningView.cpp
#include "screens/portfolio/views/PlanningView.h"
+#include "storage/repositories/SettingsRepository.h"
#include "ui/theme/Theme.h"
+#include <QAbstractItemView>
+#include <QColor>
#include <QEvent>
#include <QFormLayout>
#include <QGridLayout>
#include <QHBoxLayout>
+#include <QHeaderView>
+#include <QJsonArray>
+#include <QJsonDocument>
+#include <QJsonObject>
+#include <QLineEdit>
+#include <QTabBar>
+#include <QTableWidget>
+#include <QTableWidgetItem>
#include <QVBoxLayout>
+#include <algorithm>
#include <cmath>
+#include <numeric>
+#include <random>
namespace fincept::screens {
@@ -23,10 +37,16 @@ void PlanningView::build_ui() {
tabs_ = new QTabWidget;
tabs_->setDocumentMode(true);
+ // Tabs must show their full label ("RETIREMENT", not "RETIRE…"). Size each
+ // tab to its text and never elide; scroll buttons appear only if the bar
+ // genuinely can't fit (it can, for these three short labels).
+ tabs_->tabBar()->setElideMode(Qt::ElideNone);
+ tabs_->tabBar()->setExpanding(false);
+ tabs_->tabBar()->setUsesScrollButtons(false);
tabs_->setStyleSheet(QString("QTabWidget::pane { border:0; background:%1; }"
- "QTabBar::tab { background:%2; color:%3; padding:6px 14px; border:0;"
+ "QTabBar::tab { background:%2; color:%3; padding:6px 16px; border:0;"
" border-bottom:2px solid transparent; font-size:9px; font-weight:700;"
- " letter-spacing:0.5px; }"
+ " letter-spacing:0.5px; min-width:72px; }"
"QTabBar::tab:selected { color:%4; border-bottom:2px solid %4; }"
"QTabBar::tab:hover { color:%5; }")
.arg(ui::colors::BG_BASE(), ui::colors::BG_SURFACE(), ui::colors::TEXT_SECONDARY(),
@@ -59,6 +79,11 @@ QWidget* PlanningView::build_retirement_tab() {
QString("color:%1; font-size:11px; font-weight:700; letter-spacing:1px;").arg(ui::colors::AMBER()));
input_layout->addWidget(input_title_);
+ retire_assump_note_ = new QLabel;
+ retire_assump_note_->setWordWrap(true);
+ retire_assump_note_->setStyleSheet(QString("color:%1; font-size:9px; padding:2px 0 4px 0;").arg(ui::colors::CYAN()));
+ input_layout->addWidget(retire_assump_note_);
+
auto* form = new QFormLayout;
form->setSpacing(8);
form->setLabelAlignment(Qt::AlignRight);
@@ -199,11 +224,55 @@ QWidget* PlanningView::build_retirement_tab() {
res_layout->addLayout(grid);
+ // Monte Carlo outcome (probability + percentile band).
+ retire_mc_label_ = new QLabel;
+ retire_mc_label_->setWordWrap(true);
+ retire_mc_label_->setStyleSheet(QString("color:%1; font-size:11px; padding:8px 12px 0 12px; background:transparent;")
+ .arg(ui::colors::TEXT_PRIMARY()));
+ res_layout->addWidget(retire_mc_label_);
+
+ // Drawdown/VaR stress line (real metrics).
+ retire_stress_label_ = new QLabel;
+ retire_stress_label_->setWordWrap(true);
+ retire_stress_label_->setStyleSheet(QString("color:%1; font-size:11px; padding:2px 12px 0 12px; background:transparent;")
+ .arg(ui::colors::NEGATIVE()));
+ res_layout->addWidget(retire_stress_label_);
+
status_label_ = new QLabel;
status_label_->setWordWrap(true);
status_label_->setStyleSheet(QString("color:%1; font-size:12px; padding:12px;").arg(ui::colors::TEXT_SECONDARY()));
res_layout->addWidget(status_label_);
+ // Cross-link: optimize the portfolio for the return this plan needs.
+ retire_optimize_btn_ = new QPushButton(tr("⚙ OPTIMIZE PORTFOLIO FOR THIS GOAL"));
+ retire_optimize_btn_->setCursor(Qt::PointingHandCursor);
+ retire_optimize_btn_->setFixedHeight(28);
+ retire_optimize_btn_->setStyleSheet(
+ QString("QPushButton { background:transparent; color:%1; border:1px solid %1; font-size:10px;"
+ " font-weight:700; letter-spacing:1px; } QPushButton:hover { background:%2; color:#000; }")
+ .arg(ui::colors::CYAN(), ui::colors::CYAN()));
+ connect(retire_optimize_btn_, &QPushButton::clicked, this, [this]() {
+ // Annualised return the plan needs to exactly hit the target; hand it to
+ // the Optimization tab so the user can build a portfolio that targets it.
+ const double years = retire_age_->value() - current_age_->value();
+ const double target = (withdrawal_rate_->value() > 1e-6)
+ ? (annual_expense_->value() / (withdrawal_rate_->value() / 100.0))
+ : annual_expense_->value() * 25.0;
+ const double pv = summary_.total_market_value;
+ const double contrib = monthly_contrib_->value() * 12.0;
+ // Solve required real return r s.t. FV(pv, contrib, years, r) = target (bisection).
+ double lo = -0.10, hi = 0.40, req = 0.08;
+ for (int it = 0; it < 60; ++it) {
+ req = 0.5 * (lo + hi);
+ double bal = pv;
+ for (int y = 0; y < static_cast<int>(years); ++y)
+ bal = bal * (1.0 + req) + contrib;
+ (bal < target ? lo : hi) = req;
+ }
+ emit optimize_for_return(std::max(0.0, req));
+ });
+ res_layout->addWidget(retire_optimize_btn_);
+
res_layout->addStretch();
layout->addWidget(results, 1);
@@ -212,53 +281,741 @@ QWidget* PlanningView::build_retirement_tab() {
QWidget* PlanningView::build_goals_tab() {
auto* w = new QWidget(this);
- auto* layout = new QVBoxLayout(w);
+ auto* layout = new QHBoxLayout(w);
layout->setContentsMargins(16, 12, 16, 12);
- layout->setAlignment(Qt::AlignCenter);
+ layout->setSpacing(24);
+
+ auto style_spin = [](QDoubleSpinBox* sb) {
+ sb->setFixedHeight(26);
+ sb->setStyleSheet(
+ QString("QDoubleSpinBox { background:%1; color:%2; border:1px solid %3;"
+ " padding:0 8px; font-size:11px; }"
+ "QDoubleSpinBox:focus { border-color:%4; }")
+ .arg(ui::colors::BG_BASE(), ui::colors::TEXT_PRIMARY(), ui::colors::BORDER_DIM(), ui::colors::AMBER()));
+ };
+ const QString label_style = QString("color:%1; font-size:10px;").arg(ui::colors::TEXT_SECONDARY());
+
+ // ── Left: inputs ──────────────────────────────────────────────────────────
+ auto* input_panel = new QWidget(this);
+ input_panel->setFixedWidth(320);
+ input_panel->setStyleSheet(
+ QString("background:%1; border:1px solid %2;").arg(ui::colors::BG_RAISED(), ui::colors::BORDER_DIM()));
+ auto* input_layout = new QVBoxLayout(input_panel);
+ input_layout->setContentsMargins(16, 12, 16, 12);
+ input_layout->setSpacing(8);
goals_title_ = new QLabel(tr("GOAL-BASED PLANNING"));
- goals_title_->setAlignment(Qt::AlignCenter);
goals_title_->setStyleSheet(
+ QString("color:%1; font-size:11px; font-weight:700; letter-spacing:1px;").arg(ui::colors::AMBER()));
+ input_layout->addWidget(goals_title_);
+
+ auto* form = new QFormLayout;
+ form->setSpacing(8);
+ form->setLabelAlignment(Qt::AlignRight);
+
+ goal_name_ = new QLineEdit(tr("House Down Payment"));
+ goal_name_->setFixedHeight(26);
+ goal_name_->setStyleSheet(
+ QString("QLineEdit { background:%1; color:%2; border:1px solid %3; padding:0 8px; font-size:11px; }"
+ "QLineEdit:focus { border-color:%4; }")
+ .arg(ui::colors::BG_BASE(), ui::colors::TEXT_PRIMARY(), ui::colors::BORDER_DIM(), ui::colors::AMBER()));
+ l_goal_name_ = new QLabel(tr("Goal:"));
+ l_goal_name_->setStyleSheet(label_style);
+ form->addRow(l_goal_name_, goal_name_);
+
+ goal_target_ = new QDoubleSpinBox;
+ goal_target_->setRange(0, 1000000000);
+ goal_target_->setValue(100000);
+ goal_target_->setPrefix("$ ");
+ goal_target_->setDecimals(0);
+ style_spin(goal_target_);
+ l_goal_target_ = new QLabel(tr("Target Amount:"));
+ l_goal_target_->setStyleSheet(label_style);
+ form->addRow(l_goal_target_, goal_target_);
+
+ goal_years_ = new QDoubleSpinBox;
+ goal_years_->setRange(1, 60);
+ goal_years_->setValue(5);
+ goal_years_->setDecimals(0);
+ style_spin(goal_years_);
+ l_goal_years_ = new QLabel(tr("Years to Goal:"));
+ l_goal_years_->setStyleSheet(label_style);
+ form->addRow(l_goal_years_, goal_years_);
+
+ goal_return_ = new QDoubleSpinBox;
+ goal_return_->setRange(0, 30);
+ goal_return_->setValue(6);
+ goal_return_->setSuffix(" %");
+ goal_return_->setDecimals(1);
+ style_spin(goal_return_);
+ l_goal_return_ = new QLabel(tr("Exp. Return:"));
+ l_goal_return_->setStyleSheet(label_style);
+ form->addRow(l_goal_return_, goal_return_);
+
+ goal_monthly_ = new QDoubleSpinBox;
+ goal_monthly_->setRange(0, 1000000);
+ goal_monthly_->setValue(1000);
+ goal_monthly_->setPrefix("$ ");
+ goal_monthly_->setDecimals(0);
+ style_spin(goal_monthly_);
+ l_goal_monthly_ = new QLabel(tr("Monthly Saving:"));
+ l_goal_monthly_->setStyleSheet(label_style);
+ form->addRow(l_goal_monthly_, goal_monthly_);
+
+ goal_alloc_pct_ = new QDoubleSpinBox;
+ goal_alloc_pct_->setRange(0, 100);
+ goal_alloc_pct_->setValue(0);
+ goal_alloc_pct_->setSuffix(" %");
+ goal_alloc_pct_->setDecimals(0);
+ style_spin(goal_alloc_pct_);
+ l_goal_alloc_ = new QLabel(tr("% Portfolio Allocated:"));
+ l_goal_alloc_->setStyleSheet(label_style);
+ form->addRow(l_goal_alloc_, goal_alloc_pct_);
+
+ input_layout->addLayout(form);
+
+ goals_calc_btn_ = new QPushButton(tr("ADD / UPDATE GOAL"));
+ goals_calc_btn_->setFixedHeight(28);
+ goals_calc_btn_->setCursor(Qt::PointingHandCursor);
+ goals_calc_btn_->setStyleSheet(
+ QString("QPushButton { background:%1; color:#000; border:none; font-size:10px; font-weight:700; }"
+ "QPushButton:hover { background:%2; }")
+ .arg(ui::colors::AMBER(), ui::colors::WARNING()));
+ connect(goals_calc_btn_, &QPushButton::clicked, this, [this]() {
+ recalculate_goals(); // refresh the projection cards for the typed goal
+ add_or_update_goal(); // and persist it into the saved list
+ });
+ input_layout->addWidget(goals_calc_btn_);
+ input_layout->addStretch();
+
+ layout->addWidget(input_panel);
+
+ // ── Right: results ──────────────────────────────────────────────────────────
+ auto* results = new QWidget(this);
+ auto* res_layout = new QVBoxLayout(results);
+ res_layout->setContentsMargins(0, 0, 0, 0);
+ res_layout->setSpacing(12);
+
+ goals_res_title_ = new QLabel(tr("GOAL PROJECTION"));
+ goals_res_title_->setStyleSheet(
QString("color:%1; font-size:12px; font-weight:700; letter-spacing:1px;").arg(ui::colors::AMBER()));
- layout->addWidget(goals_title_);
+ res_layout->addWidget(goals_res_title_);
- goals_desc_ = new QLabel(tr("Define financial goals (house, education, emergency fund)\n"
- "and track your progress toward each target."));
- goals_desc_->setAlignment(Qt::AlignCenter);
- goals_desc_->setWordWrap(true);
- goals_desc_->setStyleSheet(QString("color:%1; font-size:11px;").arg(ui::colors::TEXT_TERTIARY()));
- layout->addWidget(goals_desc_);
+ auto* grid = new QGridLayout;
+ grid->setSpacing(8);
+ auto add_card = [&](int r, int c, const QString& label, QLabel*& val_label, QLabel*& title_label,
+ const char* color) {
+ auto* card = new QWidget(this);
+ card->setStyleSheet(
+ QString("background:%1; border:1px solid %2;").arg(ui::colors::BG_RAISED(), ui::colors::BORDER_DIM()));
+ auto* cl = new QVBoxLayout(card);
+ cl->setContentsMargins(12, 8, 12, 8);
+ cl->setSpacing(2);
+ title_label = new QLabel(label);
+ title_label->setStyleSheet(
+ QString("color:%1; font-size:8px; font-weight:700; letter-spacing:0.5px;").arg(ui::colors::TEXT_TERTIARY()));
+ cl->addWidget(title_label);
+ val_label = new QLabel("--");
+ val_label->setStyleSheet(QString("color:%1; font-size:18px; font-weight:700;").arg(color));
+ cl->addWidget(val_label);
+ grid->addWidget(card, r, c);
+ };
+ add_card(0, 0, tr("TARGET AMOUNT"), goal_target_value_label_, goal_target_card_label_, ui::colors::WARNING);
+ add_card(0, 1, tr("PROJECTED VALUE"), goal_projected_label_, goal_projected_card_label_, ui::colors::POSITIVE);
+ add_card(1, 0, tr("REQUIRED MONTHLY"), goal_required_label_, goal_required_card_label_, ui::colors::CYAN);
+ add_card(1, 1, tr("PROGRESS"), goal_progress_label_, goal_progress_card_label_, ui::colors::TEXT_PRIMARY);
+ res_layout->addLayout(grid);
+
+ goal_mc_label_ = new QLabel;
+ goal_mc_label_->setWordWrap(true);
+ goal_mc_label_->setTextFormat(Qt::RichText);
+ goal_mc_label_->setStyleSheet(QString("color:%1; font-size:11px; padding:0 12px;").arg(ui::colors::TEXT_PRIMARY()));
+ res_layout->addWidget(goal_mc_label_);
+
+ goal_status_label_ = new QLabel;
+ goal_status_label_->setWordWrap(true);
+ goal_status_label_->setStyleSheet(QString("color:%1; font-size:12px; padding:12px;").arg(ui::colors::TEXT_SECONDARY()));
+ res_layout->addWidget(goal_status_label_);
+
+ // ── Saved goals list (per-portfolio, with funding %) ─────────────────────
+ auto* saved_title = new QLabel(tr("SAVED GOALS"));
+ saved_title->setStyleSheet(
+ QString("color:%1; font-size:10px; font-weight:700; letter-spacing:1px; padding:0 0 0 12px;")
+ .arg(ui::colors::TEXT_TERTIARY()));
+ res_layout->addWidget(saved_title);
+
+ goals_list_ = new QTableWidget;
+ goals_list_->setColumnCount(4);
+ goals_list_->setHorizontalHeaderLabels({tr("GOAL"), tr("TARGET"), tr("FUNDED NOW"), tr("ON TRACK")});
+ goals_list_->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
+ goals_list_->verticalHeader()->setVisible(false);
+ goals_list_->setEditTriggers(QAbstractItemView::NoEditTriggers);
+ goals_list_->setSelectionBehavior(QAbstractItemView::SelectRows);
+ goals_list_->setSelectionMode(QAbstractItemView::SingleSelection);
+ goals_list_->setStyleSheet(
+ QString("QTableWidget { background:%1; color:%2; border:1px solid %3; gridline-color:%3; font-size:11px; }"
+ "QHeaderView::section { background:%4; color:%5; border:0; padding:5px; font-size:9px;"
+ " font-weight:700; letter-spacing:0.5px; }")
+ .arg(ui::colors::BG_BASE(), ui::colors::TEXT_PRIMARY(), ui::colors::BORDER_DIM(), ui::colors::BG_SURFACE(),
+ ui::colors::TEXT_SECONDARY()));
+ // Clicking a saved goal loads it back into the editor.
+ connect(goals_list_, &QTableWidget::cellClicked, this, [this](int row, int) {
+ if (row < 0 || row >= goals_.size())
+ return;
+ const Goal& g = goals_[row];
+ if (goal_name_) goal_name_->setText(g.name);
+ if (goal_target_) goal_target_->setValue(g.target);
+ if (goal_years_) goal_years_->setValue(g.years);
+ if (goal_monthly_) goal_monthly_->setValue(g.monthly);
+ if (goal_alloc_pct_) goal_alloc_pct_->setValue(g.alloc_pct);
+ recalculate_goals();
+ });
+ res_layout->addWidget(goals_list_, 1);
+ goal_remove_btn_ = new QPushButton(tr("REMOVE SELECTED GOAL"));
+ goal_remove_btn_->setFixedHeight(24);
+ goal_remove_btn_->setCursor(Qt::PointingHandCursor);
+ goal_remove_btn_->setStyleSheet(
+ QString("QPushButton { background:transparent; color:%1; border:1px solid %2; font-size:9px;"
+ " font-weight:700; } QPushButton:hover { color:%3; border-color:%3; }")
+ .arg(ui::colors::TEXT_TERTIARY(), ui::colors::BORDER_DIM(), ui::colors::NEGATIVE()));
+ connect(goal_remove_btn_, &QPushButton::clicked, this, &PlanningView::remove_selected_goal);
+ res_layout->addWidget(goal_remove_btn_);
+
+ layout->addWidget(results, 1);
return w;
}
QWidget* PlanningView::build_savings_tab() {
auto* w = new QWidget(this);
- auto* layout = new QVBoxLayout(w);
+ auto* layout = new QHBoxLayout(w);
layout->setContentsMargins(16, 12, 16, 12);
- layout->setAlignment(Qt::AlignCenter);
+ layout->setSpacing(24);
+
+ auto style_spin = [](QDoubleSpinBox* sb) {
+ sb->setFixedHeight(26);
+ sb->setStyleSheet(
+ QString("QDoubleSpinBox { background:%1; color:%2; border:1px solid %3;"
+ " padding:0 8px; font-size:11px; }"
+ "QDoubleSpinBox:focus { border-color:%4; }")
+ .arg(ui::colors::BG_BASE(), ui::colors::TEXT_PRIMARY(), ui::colors::BORDER_DIM(), ui::colors::AMBER()));
+ };
+ const QString label_style = QString("color:%1; font-size:10px;").arg(ui::colors::TEXT_SECONDARY());
+
+ // ── Left: inputs ──────────────────────────────────────────────────────────
+ auto* input_panel = new QWidget(this);
+ input_panel->setFixedWidth(320);
+ input_panel->setStyleSheet(
+ QString("background:%1; border:1px solid %2;").arg(ui::colors::BG_RAISED(), ui::colors::BORDER_DIM()));
+ auto* input_layout = new QVBoxLayout(input_panel);
+ input_layout->setContentsMargins(16, 12, 16, 12);
+ input_layout->setSpacing(8);
savings_title_ = new QLabel(tr("SAVINGS RATE ANALYSIS"));
- savings_title_->setAlignment(Qt::AlignCenter);
savings_title_->setStyleSheet(
- QString("color:%1; font-size:12px; font-weight:700; letter-spacing:1px;").arg(ui::colors::AMBER()));
- layout->addWidget(savings_title_);
+ QString("color:%1; font-size:11px; font-weight:700; letter-spacing:1px;").arg(ui::colors::AMBER()));
+ input_layout->addWidget(savings_title_);
- savings_desc_ = new QLabel(tr("Analyze how different savings rates and contribution schedules\n"
- "affect your long-term wealth accumulation."));
- savings_desc_->setAlignment(Qt::AlignCenter);
+ savings_desc_ = new QLabel(tr("Projects wealth accumulation across a range of savings rates, "
+ "starting from your current portfolio value."));
savings_desc_->setWordWrap(true);
- savings_desc_->setStyleSheet(QString("color:%1; font-size:11px;").arg(ui::colors::TEXT_TERTIARY()));
- layout->addWidget(savings_desc_);
+ savings_desc_->setStyleSheet(QString("color:%1; font-size:10px;").arg(ui::colors::TEXT_TERTIARY()));
+ input_layout->addWidget(savings_desc_);
+
+ sav_assump_note_ = new QLabel;
+ sav_assump_note_->setWordWrap(true);
+ sav_assump_note_->setStyleSheet(QString("color:%1; font-size:9px; padding:2px 0;").arg(ui::colors::CYAN()));
+ input_layout->addWidget(sav_assump_note_);
+ auto* form = new QFormLayout;
+ form->setSpacing(8);
+ form->setLabelAlignment(Qt::AlignRight);
+
+ sav_income_ = new QDoubleSpinBox;
+ sav_income_->setRange(0, 100000000);
+ sav_income_->setValue(120000);
+ sav_income_->setPrefix("$ ");
+ sav_income_->setDecimals(0);
+ style_spin(sav_income_);
+ l_sav_income_ = new QLabel(tr("Annual Income:"));
+ l_sav_income_->setStyleSheet(label_style);
+ form->addRow(l_sav_income_, sav_income_);
+
+ sav_rate_ = new QDoubleSpinBox;
+ sav_rate_->setRange(0, 90);
+ sav_rate_->setValue(20);
+ sav_rate_->setSuffix(" %");
+ sav_rate_->setDecimals(0);
+ style_spin(sav_rate_);
+ l_sav_rate_ = new QLabel(tr("Savings Rate:"));
+ l_sav_rate_->setStyleSheet(label_style);
+ form->addRow(l_sav_rate_, sav_rate_);
+
+ sav_return_ = new QDoubleSpinBox;
+ sav_return_->setRange(0, 30);
+ sav_return_->setValue(7);
+ sav_return_->setSuffix(" %");
+ sav_return_->setDecimals(1);
+ style_spin(sav_return_);
+ l_sav_return_ = new QLabel(tr("Exp. Return:"));
+ l_sav_return_->setStyleSheet(label_style);
+ form->addRow(l_sav_return_, sav_return_);
+
+ sav_years_ = new QDoubleSpinBox;
+ sav_years_->setRange(1, 60);
+ sav_years_->setValue(20);
+ sav_years_->setDecimals(0);
+ style_spin(sav_years_);
+ l_sav_years_ = new QLabel(tr("Years:"));
+ l_sav_years_->setStyleSheet(label_style);
+ form->addRow(l_sav_years_, sav_years_);
+
+ input_layout->addLayout(form);
+
+ savings_calc_btn_ = new QPushButton(tr("CALCULATE"));
+ savings_calc_btn_->setFixedHeight(28);
+ savings_calc_btn_->setCursor(Qt::PointingHandCursor);
+ savings_calc_btn_->setStyleSheet(
+ QString("QPushButton { background:%1; color:#000; border:none; font-size:10px; font-weight:700; }"
+ "QPushButton:hover { background:%2; }")
+ .arg(ui::colors::AMBER(), ui::colors::WARNING()));
+ connect(savings_calc_btn_, &QPushButton::clicked, this, &PlanningView::recalculate_savings);
+ input_layout->addWidget(savings_calc_btn_);
+ input_layout->addStretch();
+
+ layout->addWidget(input_panel);
+
+ // ── Right: comparison table ─────────────────────────────────────────────────
+ auto* results = new QWidget(this);
+ auto* res_layout = new QVBoxLayout(results);
+ res_layout->setContentsMargins(0, 0, 0, 0);
+ res_layout->setSpacing(12);
+
+ savings_res_title_ = new QLabel(tr("WEALTH BY SAVINGS RATE"));
+ savings_res_title_->setStyleSheet(
+ QString("color:%1; font-size:12px; font-weight:700; letter-spacing:1px;").arg(ui::colors::AMBER()));
+ res_layout->addWidget(savings_res_title_);
+
+ savings_table_ = new QTableWidget;
+ savings_table_->setColumnCount(4);
+ savings_table_->setHorizontalHeaderLabels(
+ {tr("SAVINGS RATE"), tr("ANNUAL SAVED"), tr("PROJECTED WEALTH"), tr("VS CURRENT RATE")});
+ savings_table_->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
+ savings_table_->verticalHeader()->setVisible(false);
+ savings_table_->setEditTriggers(QAbstractItemView::NoEditTriggers);
+ savings_table_->setSelectionMode(QAbstractItemView::NoSelection);
+ savings_table_->setStyleSheet(
+ QString("QTableWidget { background:%1; color:%2; border:1px solid %3; gridline-color:%3; font-size:11px; }"
+ "QHeaderView::section { background:%4; color:%5; border:0; padding:6px; font-size:9px;"
+ " font-weight:700; letter-spacing:0.5px; }")
+ .arg(ui::colors::BG_BASE(), ui::colors::TEXT_PRIMARY(), ui::colors::BORDER_DIM(), ui::colors::BG_SURFACE(),
+ ui::colors::TEXT_SECONDARY()));
+ res_layout->addWidget(savings_table_, 1);
+
+ savings_status_label_ = new QLabel;
+ savings_status_label_->setWordWrap(true);
+ savings_status_label_->setStyleSheet(
+ QString("color:%1; font-size:12px; padding:8px;").arg(ui::colors::TEXT_SECONDARY()));
+ res_layout->addWidget(savings_status_label_);
+
+ layout->addWidget(results, 1);
return w;
}
+void PlanningView::recalculate_goals() {
+ if (!goal_target_)
+ return;
+ const double target = goal_target_->value();
+ const int years = static_cast<int>(goal_years_->value());
+ const double ret = goal_return_->value() / 100.0;
+ const double monthly = goal_monthly_->value();
+ const double alloc_pct = goal_alloc_pct_->value() / 100.0;
+
+ // Starting capital = the slice of the current portfolio earmarked for this goal.
+ const double start_capital = summary_.total_market_value * alloc_pct;
+
+ const double monthly_rate = ret / 12.0;
+ const int months = years * 12;
+ const double growth = std::pow(1.0 + monthly_rate, months);
+ const double fv_start = start_capital * growth;
+ const double annuity_factor = (monthly_rate > 1e-9) ? ((growth - 1.0) / monthly_rate) : months;
+ const double fv_contrib = monthly * annuity_factor;
+ const double projected = fv_start + fv_contrib;
+
+ // Required monthly contribution to exactly hit the target from start_capital.
+ const double remaining = std::max(0.0, target - fv_start);
+ const double required_monthly = (annuity_factor > 1e-9) ? (remaining / annuity_factor) : 0.0;
+
+ const double progress = (target > 1e-6) ? (projected / target * 100.0) : 0.0;
+
+ goal_target_value_label_->setText(QString("%1 %2").arg(currency_, QString::number(target, 'f', 0)));
+ goal_projected_label_->setText(QString("%1 %2").arg(currency_, QString::number(projected, 'f', 0)));
+ goal_required_label_->setText(QString("%1 %2").arg(currency_, QString::number(required_monthly, 'f', 0)));
+ goal_progress_label_->setText(QString("%1%").arg(QString::number(std::min(progress, 999.0), 'f', 0)));
+
+ const bool on_track = projected >= target;
+ const char* col = on_track ? ui::colors::POSITIVE : ui::colors::WARNING;
+ goal_progress_label_->setStyleSheet(QString("color:%1; font-size:18px; font-weight:700;").arg(col));
+
+ const QString name = goal_name_->text().trimmed().isEmpty() ? tr("this goal") : goal_name_->text().trimmed();
+ if (on_track) {
+ goal_status_label_->setText(tr("✓ On track for %1. Saving %2 %3/month reaches %2 %4 in %5 years — "
+ "%6% of the %2 %7 target.")
+ .arg(name)
+ .arg(currency_)
+ .arg(QString::number(monthly, 'f', 0))
+ .arg(QString::number(projected, 'f', 0))
+ .arg(years)
+ .arg(QString::number(progress, 'f', 0))
+ .arg(QString::number(target, 'f', 0)));
+ goal_status_label_->setStyleSheet(QString("color:%1; font-size:12px; padding:12px;").arg(ui::colors::POSITIVE()));
+ } else {
+ goal_status_label_->setText(tr("⚠ Short of %1. At %2 %3/month you reach %2 %4 (%5%). "
+ "Save %2 %6/month to hit the %2 %7 target in %8 years.")
+ .arg(name)
+ .arg(currency_)
+ .arg(QString::number(monthly, 'f', 0))
+ .arg(QString::number(projected, 'f', 0))
+ .arg(QString::number(progress, 'f', 0))
+ .arg(QString::number(required_monthly, 'f', 0))
+ .arg(QString::number(target, 'f', 0))
+ .arg(years));
+ goal_status_label_->setStyleSheet(QString("color:%1; font-size:12px; padding:12px;").arg(ui::colors::WARNING()));
+ }
+
+ // Monte Carlo probability of hitting this goal, calibrated to portfolio vol.
+ if (goal_mc_label_) {
+ const double vol = have_history_ ? hist_vol_ : 0.15;
+ const McResult mc = monte_carlo(start_capital, monthly, years, ret, vol, target);
+ if (mc.valid) {
+ const char* pc = mc.success_prob >= 0.75 ? ui::colors::POSITIVE
+ : mc.success_prob >= 0.5 ? ui::colors::WARNING
+ : ui::colors::NEGATIVE;
+ goal_mc_label_->setText(tr("Monte Carlo: <b><span style='color:%1'>%2%% chance</span></b> of reaching "
+ "%3 %4 — likely %3 %5 – %3 %6.")
+ .arg(pc)
+ .arg(QString::number(mc.success_prob * 100, 'f', 0))
+ .arg(currency_)
+ .arg(QString::number(target, 'f', 0))
+ .arg(QString::number(mc.p5, 'f', 0))
+ .arg(QString::number(mc.p95, 'f', 0)));
+ }
+ }
+
+ // Keep the saved-goals funding column in sync with the current return input.
+ refresh_goals_list();
+}
+
+void PlanningView::recalculate_savings() {
+ if (!savings_table_)
+ return;
+ const double income = sav_income_->value();
+ const double base_rate = sav_rate_->value();
+ const double ret = sav_return_->value() / 100.0;
+ const int years = static_cast<int>(sav_years_->value());
+ const double start_capital = summary_.total_market_value;
+
+ // Compare the user's rate against a band of nearby rates.
+ QVector<double> rates;
+ for (double delta : {-10.0, -5.0, 0.0, 5.0, 10.0}) {
+ const double r = base_rate + delta;
+ if (r >= 0.0 && r <= 100.0)
+ rates.append(r);
+ }
+ std::sort(rates.begin(), rates.end());
+ rates.erase(std::unique(rates.begin(), rates.end()), rates.end());
+
+ auto project = [&](double rate_pct) {
+ const double annual_saved = income * rate_pct / 100.0;
+ const double growth = std::pow(1.0 + ret, years);
+ const double fv_start = start_capital * growth;
+ const double fv_saved = (ret > 1e-9) ? annual_saved * ((growth - 1.0) / ret) : annual_saved * years;
+ return std::make_pair(annual_saved, fv_start + fv_saved);
+ };
+
+ const double base_wealth = project(base_rate).second;
+
+ savings_table_->setRowCount(rates.size());
+ for (int i = 0; i < rates.size(); ++i) {
+ const double rate = rates[i];
+ const auto [annual_saved, wealth] = project(rate);
+ const bool is_base = std::abs(rate - base_rate) < 0.01;
+ const double diff = wealth - base_wealth;
+
+ auto make_item = [&](const QString& text, const char* color = nullptr) {
+ auto* it = new QTableWidgetItem(text);
+ it->setTextAlignment(Qt::AlignCenter);
+ if (color)
+ it->setForeground(QColor(QString(color)));
+ if (is_base)
+ it->setBackground(QColor(ui::colors::BG_SURFACE()));
+ return it;
+ };
+
+ savings_table_->setItem(i, 0, make_item(QString("%1%%2").arg(QString::number(rate, 'f', 0),
+ is_base ? tr(" (current)") : QString())));
+ savings_table_->setItem(i, 1, make_item(QString("%1 %2").arg(currency_, QString::number(annual_saved, 'f', 0))));
+ savings_table_->setItem(i, 2, make_item(QString("%1 %2").arg(currency_, QString::number(wealth, 'f', 0))));
+ const char* dcol = diff > 0 ? ui::colors::POSITIVE : diff < 0 ? ui::colors::NEGATIVE : ui::colors::TEXT_TERTIARY();
+ savings_table_->setItem(
+ i, 3,
+ make_item(is_base ? QStringLiteral("—")
+ : QString("%1%2 %3").arg(diff >= 0 ? "+" : "-", currency_,
+ QString::number(std::abs(diff), 'f', 0)),
+ dcol));
+ }
+
+ const auto [base_annual, base_proj] = project(base_rate);
+ savings_status_label_->setText(tr("At a %1% savings rate you set aside %2 %3/year and project %2 %4 "
+ "after %5 years (%6% annual return, starting from %2 %7).")
+ .arg(QString::number(base_rate, 'f', 0))
+ .arg(currency_)
+ .arg(QString::number(base_annual, 'f', 0))
+ .arg(QString::number(base_proj, 'f', 0))
+ .arg(years)
+ .arg(QString::number(sav_return_->value(), 'f', 1))
+ .arg(QString::number(start_capital, 'f', 0)));
+}
+
void PlanningView::set_data(const portfolio::PortfolioSummary& summary, const QString& currency) {
summary_ = summary;
currency_ = currency;
has_data_ = true;
+
+ // Money inputs must reflect the portfolio's own currency, not a hardcoded
+ // "$". Result labels in this view already use currency_ (e.g. "INR 60000"),
+ // so prefix the spin boxes with the same currency code to stay consistent.
+ const QString prefix = (currency_.isEmpty() ? QStringLiteral("USD") : currency_) + " ";
+ for (QDoubleSpinBox* sb : {annual_expense_, monthly_contrib_, goal_target_, goal_monthly_, sav_income_}) {
+ if (sb)
+ sb->setPrefix(prefix);
+ }
+
+ load_goals();
recalculate();
+ recalculate_goals();
+ recalculate_savings();
+}
+
+void PlanningView::set_snapshots(const QVector<portfolio::PortfolioSnapshot>& snapshots) {
+ snapshots_ = snapshots;
+ recompute_assumptions();
+ if (has_data_) {
+ recalculate();
+ recalculate_goals();
+ recalculate_savings();
+ }
+}
+
+void PlanningView::set_metrics(const portfolio::ComputedMetrics& metrics) {
+ metrics_ = metrics;
+ if (has_data_) {
+ recalculate();
+ recalculate_goals();
+ }
+}
+
+void PlanningView::recompute_assumptions() {
+ have_history_ = false;
+ hist_cagr_ = 0.0;
+ hist_vol_ = 0.0;
+
+ auto snaps = snapshots_;
+ std::sort(snaps.begin(), snaps.end(),
+ [](const auto& a, const auto& b) { return a.snapshot_date < b.snapshot_date; });
+ QVector<double> rets;
+ for (int i = 1; i < snaps.size(); ++i) {
+ const double p = snaps[i - 1].total_value;
+ const double c = snaps[i].total_value;
+ if (p > 1.0)
+ rets.append((c - p) / p);
+ }
+ if (rets.size() >= 20) {
+ const int n = rets.size();
+ const double mean = std::accumulate(rets.begin(), rets.end(), 0.0) / n;
+ double var = 0.0;
+ for (double r : rets)
+ var += (r - mean) * (r - mean);
+ const double daily_vol = std::sqrt(var / (n - 1));
+ hist_cagr_ = std::pow(1.0 + mean, 252.0) - 1.0; // annualise daily mean
+ hist_vol_ = daily_vol * std::sqrt(252.0);
+ // Clamp to sane planning bounds (a 30-day sample can be extreme).
+ hist_cagr_ = std::max(-0.5, std::min(0.35, hist_cagr_));
+ hist_vol_ = std::max(0.01, std::min(1.0, hist_vol_));
+ have_history_ = true;
+ }
+
+ const QString note =
+ have_history_
+ ? tr("◉ Calibrated to your portfolio: %1% expected return, %2% volatility "
+ "(from %3 days of history). Edit any field to override.")
+ .arg(QString::number(hist_cagr_ * 100, 'f', 1))
+ .arg(QString::number(hist_vol_ * 100, 'f', 1))
+ .arg(rets.size())
+ : tr("○ Not enough portfolio history yet to calibrate — using your typed assumptions. "
+ "Estimates improve as daily snapshots accumulate.");
+ if (retire_assump_note_)
+ retire_assump_note_->setText(note);
+ if (sav_assump_note_)
+ sav_assump_note_->setText(note);
+
+ // Seed the return inputs from the real CAGR (user can still override).
+ if (have_history_) {
+ const double pct = hist_cagr_ * 100.0;
+ if (expected_return_)
+ expected_return_->setValue(pct);
+ if (goal_return_)
+ goal_return_->setValue(pct);
+ if (sav_return_)
+ sav_return_->setValue(pct);
+ }
+}
+
+PlanningView::McResult PlanningView::monte_carlo(double start, double monthly, int years, double mean_annual,
+ double vol_annual, double target) const {
+ McResult r;
+ if (years <= 0)
+ return r;
+ constexpr int kSims = 2000;
+ std::mt19937 gen(12345u); // fixed seed → stable, reproducible projections
+ std::normal_distribution<double> nd(mean_annual, std::max(vol_annual, 1e-6));
+ const double annual_contrib = monthly * 12.0;
+ QVector<double> ends;
+ ends.reserve(kSims);
+ int success = 0;
+ for (int s = 0; s < kSims; ++s) {
+ double bal = start;
+ for (int y = 0; y < years; ++y) {
+ bal = bal * (1.0 + nd(gen)) + annual_contrib;
+ if (bal < 0.0)
+ bal = 0.0;
+ }
+ ends.append(bal);
+ if (bal >= target)
+ ++success;
+ }
+ std::sort(ends.begin(), ends.end());
+ auto pct = [&](double p) { return ends[std::clamp(static_cast<int>(p * kSims), 0, kSims - 1)]; };
+ r.p5 = pct(0.05);
+ r.p50 = pct(0.50);
+ r.p95 = pct(0.95);
+ r.success_prob = static_cast<double>(success) / kSims;
+ r.valid = true;
+ return r;
+}
+
+// ── Goals persistence (per-portfolio) ─────────────────────────────────────────
+
+void PlanningView::load_goals() {
+ goals_.clear();
+ const QString pid = summary_.portfolio.id;
+ if (!pid.isEmpty()) {
+ auto res = SettingsRepository::instance().get(QString("pi.goals.%1").arg(pid));
+ if (res.is_ok() && !res.value().isEmpty()) {
+ const auto doc = QJsonDocument::fromJson(res.value().toUtf8());
+ for (const auto v : doc.array()) {
+ const auto o = v.toObject();
+ Goal g;
+ g.name = o["name"].toString();
+ g.target = o["target"].toDouble();
+ g.years = o["years"].toInt();
+ g.monthly = o["monthly"].toDouble();
+ g.alloc_pct = o["alloc"].toDouble();
+ goals_.append(g);
+ }
+ }
+ }
+ refresh_goals_list();
+}
+
+void PlanningView::save_goals() {
+ const QString pid = summary_.portfolio.id;
+ if (pid.isEmpty())
+ return;
+ QJsonArray arr;
+ for (const auto& g : goals_) {
+ QJsonObject o;
+ o["name"] = g.name;
+ o["target"] = g.target;
+ o["years"] = g.years;
+ o["monthly"] = g.monthly;
+ o["alloc"] = g.alloc_pct;
+ arr.append(o);
+ }
+ SettingsRepository::instance().set(QString("pi.goals.%1").arg(pid),
+ QString::fromUtf8(QJsonDocument(arr).toJson(QJsonDocument::Compact)),
+ "portfolio_insights");
+}
+
+void PlanningView::refresh_goals_list() {
+ if (!goals_list_)
+ return;
+ const double mean = (expected_return_ ? expected_return_->value() : 8.0) / 100.0;
+ goals_list_->setRowCount(goals_.size());
+ for (int i = 0; i < goals_.size(); ++i) {
+ const Goal& g = goals_[i];
+ const double start = summary_.total_market_value * g.alloc_pct / 100.0;
+ const double growth = std::pow(1.0 + mean / 12.0, g.years * 12);
+ const double annuity = (mean > 1e-9) ? ((growth - 1.0) / (mean / 12.0)) : g.years * 12;
+ const double projected = start * growth + g.monthly * annuity;
+ const double funded_now = (g.target > 1e-6) ? (start / g.target * 100.0) : 0.0;
+ const double proj_pct = (g.target > 1e-6) ? (projected / g.target * 100.0) : 0.0;
+ const bool on_track = projected >= g.target;
+
+ auto item = [&](const QString& t, const char* color = nullptr, int align = Qt::AlignLeft) {
+ auto* it = new QTableWidgetItem(t);
+ it->setTextAlignment(static_cast<Qt::Alignment>(align) | Qt::AlignVCenter);
+ if (color)
+ it->setForeground(QColor(QString(color)));
+ return it;
+ };
+ goals_list_->setItem(i, 0, item(g.name.isEmpty() ? tr("(unnamed)") : g.name, ui::colors::TEXT_PRIMARY));
+ goals_list_->setItem(i, 1, item(QString("%1 %2").arg(currency_, QString::number(g.target, 'f', 0)),
+ ui::colors::TEXT_SECONDARY, Qt::AlignRight));
+ goals_list_->setItem(i, 2, item(QString("%1%").arg(QString::number(funded_now, 'f', 0)),
+ ui::colors::CYAN, Qt::AlignRight));
+ goals_list_->setItem(i, 3,
+ item(QString("%1%").arg(QString::number(std::min(proj_pct, 999.0), 'f', 0)),
+ on_track ? ui::colors::POSITIVE : ui::colors::WARNING, Qt::AlignRight));
+ }
+}
+
+void PlanningView::add_or_update_goal() {
+ if (!goal_name_)
+ return;
+ Goal g;
+ g.name = goal_name_->text().trimmed();
+ g.target = goal_target_ ? goal_target_->value() : 0;
+ g.years = goal_years_ ? static_cast<int>(goal_years_->value()) : 0;
+ g.monthly = goal_monthly_ ? goal_monthly_->value() : 0;
+ g.alloc_pct = goal_alloc_pct_ ? goal_alloc_pct_->value() : 0;
+ if (g.name.isEmpty() || g.target <= 0)
+ return;
+ // Update existing goal with the same name, else append.
+ bool updated = false;
+ for (auto& existing : goals_) {
+ if (existing.name.compare(g.name, Qt::CaseInsensitive) == 0) {
+ existing = g;
+ updated = true;
+ break;
+ }
+ }
+ if (!updated)
+ goals_.append(g);
+ save_goals();
+ refresh_goals_list();
+ recalculate_goals();
+}
+
+void PlanningView::remove_selected_goal() {
+ if (!goals_list_)
+ return;
+ const int row = goals_list_->currentRow();
+ if (row < 0 || row >= goals_.size())
+ return;
+ goals_.removeAt(row);
+ save_goals();
+ refresh_goals_list();
}
void PlanningView::changeEvent(QEvent* event) {
@@ -288,15 +1045,38 @@ void PlanningView::retranslateUi() {
if (projected_card_label_) projected_card_label_->setText(tr("PROJECTED VALUE"));
if (gap_card_label_) gap_card_label_->setText(tr("SURPLUS / GAP"));
if (goals_title_) goals_title_->setText(tr("GOAL-BASED PLANNING"));
- if (goals_desc_) goals_desc_->setText(tr("Define financial goals (house, education, emergency fund)\n"
- "and track your progress toward each target."));
+ if (l_goal_name_) l_goal_name_->setText(tr("Goal:"));
+ if (l_goal_target_) l_goal_target_->setText(tr("Target Amount:"));
+ if (l_goal_years_) l_goal_years_->setText(tr("Years to Goal:"));
+ if (l_goal_return_) l_goal_return_->setText(tr("Exp. Return:"));
+ if (l_goal_monthly_) l_goal_monthly_->setText(tr("Monthly Saving:"));
+ if (l_goal_alloc_) l_goal_alloc_->setText(tr("% Portfolio Allocated:"));
+ if (goals_calc_btn_) goals_calc_btn_->setText(tr("CALCULATE"));
+ if (goals_res_title_) goals_res_title_->setText(tr("GOAL PROJECTION"));
+ if (goal_target_card_label_) goal_target_card_label_->setText(tr("TARGET AMOUNT"));
+ if (goal_projected_card_label_) goal_projected_card_label_->setText(tr("PROJECTED VALUE"));
+ if (goal_required_card_label_) goal_required_card_label_->setText(tr("REQUIRED MONTHLY"));
+ if (goal_progress_card_label_) goal_progress_card_label_->setText(tr("PROGRESS"));
+
if (savings_title_) savings_title_->setText(tr("SAVINGS RATE ANALYSIS"));
- if (savings_desc_) savings_desc_->setText(tr("Analyze how different savings rates and contribution schedules\n"
- "affect your long-term wealth accumulation."));
+ if (savings_desc_) savings_desc_->setText(tr("Projects wealth accumulation across a range of savings rates, "
+ "starting from your current portfolio value."));
+ if (l_sav_income_) l_sav_income_->setText(tr("Annual Income:"));
+ if (l_sav_rate_) l_sav_rate_->setText(tr("Savings Rate:"));
+ if (l_sav_return_) l_sav_return_->setText(tr("Exp. Return:"));
+ if (l_sav_years_) l_sav_years_->setText(tr("Years:"));
+ if (savings_calc_btn_) savings_calc_btn_->setText(tr("CALCULATE"));
+ if (savings_res_title_) savings_res_title_->setText(tr("WEALTH BY SAVINGS RATE"));
+ if (savings_table_)
+ savings_table_->setHorizontalHeaderLabels(
+ {tr("SAVINGS RATE"), tr("ANNUAL SAVED"), tr("PROJECTED WEALTH"), tr("VS CURRENT RATE")});
// re-run so status_label_ tr() strings pick up new locale
- if (has_data_)
+ if (has_data_) {
recalculate();
+ recalculate_goals();
+ recalculate_savings();
+ }
}
void PlanningView::recalculate() {
@@ -351,6 +1131,43 @@ void PlanningView::recalculate() {
.arg(QString::number(needed_monthly, 'f', 0)));
status_label_->setStyleSheet(QString("color:%1; font-size:12px; padding:12px;").arg(ui::colors::WARNING()));
}
+
+ // \u2500\u2500 Monte Carlo (probabilistic, calibrated to portfolio vol) \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
+ if (retire_mc_label_) {
+ const double vol = have_history_ ? hist_vol_ : 0.15; // default 15% if uncalibrated
+ const McResult mc = monte_carlo(current_value, monthly, static_cast<int>(years), real_ret, vol, target);
+ if (mc.valid) {
+ const char* pc = mc.success_prob >= 0.75 ? ui::colors::POSITIVE
+ : mc.success_prob >= 0.5 ? ui::colors::WARNING
+ : ui::colors::NEGATIVE;
+ retire_mc_label_->setText(
+ tr("Monte Carlo (2000 runs, %1% vol): <b><span style='color:%2'>%3%% chance</span></b> "
+ "of reaching your target. Likely range %4 %5 \u2013 %6 %7 (median %4 %8).")
+ .arg(QString::number(vol * 100, 'f', 0))
+ .arg(pc)
+ .arg(QString::number(mc.success_prob * 100, 'f', 0))
+ .arg(currency_)
+ .arg(QString::number(mc.p5, 'f', 0))
+ .arg(currency_)
+ .arg(QString::number(mc.p95, 'f', 0))
+ .arg(QString::number(mc.p50, 'f', 0)));
+ retire_mc_label_->setTextFormat(Qt::RichText);
+ }
+ }
+
+ // \u2500\u2500 Drawdown stress (real metric) \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
+ if (retire_stress_label_) {
+ const double dd_pct = metrics_.max_drawdown ? std::abs(*metrics_.max_drawdown) : 30.0;
+ const double shocked = projected * (1.0 - dd_pct / 100.0);
+ const double shocked_funded = (target > 1e-6) ? (shocked / target * 100.0) : 0.0;
+ retire_stress_label_->setText(
+ tr("Stress: a %1%% drawdown (your historical max) near retirement would cut the projection to "
+ "%2 %3 \u2014 %4%% of target.")
+ .arg(QString::number(dd_pct, 'f', 0))
+ .arg(currency_)
+ .arg(QString::number(shocked, 'f', 0))
+ .arg(QString::number(shocked_funded, 'f', 0)));
+ }
}
} // namespace fincept::screens
diff --git a/fincept-qt/src/screens/portfolio/views/PlanningView.h b/fincept-qt/src/screens/portfolio/views/PlanningView.h
index 4638e1383..05c80f370 100644
--- a/fincept-qt/src/screens/portfolio/views/PlanningView.h
+++ b/fincept-qt/src/screens/portfolio/views/PlanningView.h
@@ -4,8 +4,10 @@
#include <QDoubleSpinBox>
#include <QLabel>
+#include <QLineEdit>
#include <QPushButton>
#include <QTabWidget>
+#include <QTableWidget>
#include <QWidget>
namespace fincept::screens {
@@ -18,6 +20,16 @@ class PlanningView : public QWidget {
explicit PlanningView(QWidget* parent = nullptr);
void set_data(const portfolio::PortfolioSummary& summary, const QString& currency);
+ /// Daily NAV snapshots — used to derive the portfolio's real historical CAGR
+ /// and volatility, which seed the planning assumptions.
+ void set_snapshots(const QVector<portfolio::PortfolioSnapshot>& snapshots);
+ /// Computed risk metrics — used for the drawdown/VaR stress line.
+ void set_metrics(const portfolio::ComputedMetrics& metrics);
+
+ signals:
+ /// Emitted by the "Optimize for this return" action — asks the host to open
+ /// the Optimization sub-tab targeting the given annualised return (decimal).
+ void optimize_for_return(double target_annual_return);
protected:
void changeEvent(QEvent* event) override;
@@ -29,6 +41,29 @@ class PlanningView : public QWidget {
QWidget* build_goals_tab();
QWidget* build_savings_tab();
void recalculate();
+ void recalculate_goals();
+ void recalculate_savings();
+
+ // Derive historical CAGR + annualised volatility from snapshots_.
+ void recompute_assumptions();
+
+ // Monte Carlo wealth-path simulation calibrated to the portfolio's own
+ // mean/vol. Simulates `years` of annual returns ~Normal(mean, vol) on a
+ // balance that also receives `monthly`*12 each year.
+ struct McResult {
+ double p5 = 0, p50 = 0, p95 = 0; // ending-balance percentiles
+ double success_prob = 0; // fraction of paths reaching `target`
+ bool valid = false;
+ };
+ McResult monte_carlo(double start, double monthly, int years, double mean_annual, double vol_annual,
+ double target) const;
+
+ // Goals persistence (per-portfolio, SettingsRepository).
+ void load_goals();
+ void save_goals();
+ void refresh_goals_list();
+ void add_or_update_goal();
+ void remove_selected_goal();
QTabWidget* tabs_ = nullptr;
int retirement_tab_index_ = -1;
@@ -60,13 +95,48 @@ class PlanningView : public QWidget {
QLabel* projected_card_label_ = nullptr;
QLabel* gap_card_label_ = nullptr;
- // Goals tab labels
+ // Goals tab
QLabel* goals_title_ = nullptr;
QLabel* goals_desc_ = nullptr;
+ QLineEdit* goal_name_ = nullptr;
+ QDoubleSpinBox* goal_target_ = nullptr;
+ QDoubleSpinBox* goal_years_ = nullptr;
+ QDoubleSpinBox* goal_return_ = nullptr;
+ QDoubleSpinBox* goal_monthly_ = nullptr;
+ QDoubleSpinBox* goal_alloc_pct_ = nullptr; // % of current portfolio earmarked for this goal
+ QLabel* l_goal_name_ = nullptr;
+ QLabel* l_goal_target_ = nullptr;
+ QLabel* l_goal_years_ = nullptr;
+ QLabel* l_goal_return_ = nullptr;
+ QLabel* l_goal_monthly_ = nullptr;
+ QLabel* l_goal_alloc_ = nullptr;
+ QPushButton* goals_calc_btn_ = nullptr;
+ QLabel* goals_res_title_ = nullptr;
+ QLabel* goal_required_card_label_ = nullptr;
+ QLabel* goal_projected_card_label_ = nullptr;
+ QLabel* goal_target_card_label_ = nullptr;
+ QLabel* goal_progress_card_label_ = nullptr;
+ QLabel* goal_required_label_ = nullptr;
+ QLabel* goal_projected_label_ = nullptr;
+ QLabel* goal_target_value_label_ = nullptr;
+ QLabel* goal_progress_label_ = nullptr;
+ QLabel* goal_status_label_ = nullptr;
- // Savings tab labels
+ // Savings tab
QLabel* savings_title_ = nullptr;
QLabel* savings_desc_ = nullptr;
+ QDoubleSpinBox* sav_income_ = nullptr;
+ QDoubleSpinBox* sav_rate_ = nullptr;
+ QDoubleSpinBox* sav_return_ = nullptr;
+ QDoubleSpinBox* sav_years_ = nullptr;
+ QLabel* l_sav_income_ = nullptr;
+ QLabel* l_sav_rate_ = nullptr;
+ QLabel* l_sav_return_ = nullptr;
+ QLabel* l_sav_years_ = nullptr;
+ QPushButton* savings_calc_btn_ = nullptr;
+ QLabel* savings_res_title_ = nullptr;
+ QTableWidget* savings_table_ = nullptr;
+ QLabel* savings_status_label_ = nullptr;
// Results
QLabel* years_label_ = nullptr;
@@ -75,8 +145,38 @@ class PlanningView : public QWidget {
QLabel* gap_label_ = nullptr;
QLabel* status_label_ = nullptr;
+ // Retirement: Monte Carlo + stress + assumptions note
+ QLabel* retire_assump_note_ = nullptr;
+ QLabel* retire_mc_label_ = nullptr; // "Success: 72% | P5 … P95 …"
+ QLabel* retire_stress_label_ = nullptr; // drawdown/VaR stress line
+ QPushButton* retire_optimize_btn_ = nullptr;
+
+ // Goals: saved-goals list + funding
+ QTableWidget* goals_list_ = nullptr;
+ QPushButton* goal_add_btn_ = nullptr;
+ QPushButton* goal_remove_btn_ = nullptr;
+ QLabel* goal_mc_label_ = nullptr;
+
+ // Savings: assumptions note
+ QLabel* sav_assump_note_ = nullptr;
+
+ // A persisted goal.
+ struct Goal {
+ QString name;
+ double target = 0;
+ int years = 0;
+ double monthly = 0;
+ double alloc_pct = 0; // % of portfolio earmarked
+ };
+ QVector<Goal> goals_;
+
portfolio::PortfolioSummary summary_;
QString currency_;
+ QVector<portfolio::PortfolioSnapshot> snapshots_;
+ portfolio::ComputedMetrics metrics_;
+ double hist_cagr_ = 0.0; // derived annualised return (decimal), 0 if unknown
+ double hist_vol_ = 0.0; // derived annualised volatility (decimal), 0 if unknown
+ bool have_history_ = false;
bool has_data_ = false;
};
diff --git a/fincept-qt/src/screens/portfolio/views/PortfolioOptimizationView.cpp b/fincept-qt/src/screens/portfolio/views/PortfolioOptimizationView.cpp
index 3aa6256c3..da87403bb 100644
--- a/fincept-qt/src/screens/portfolio/views/PortfolioOptimizationView.cpp
+++ b/fincept-qt/src/screens/portfolio/views/PortfolioOptimizationView.cpp
@@ -13,6 +13,7 @@ using fincept::services::PortfolioAnalyticsService;
#define QT_CHARTS_USE_NAMESPACE
#include <QChart>
+#include <QTabBar>
#include <QDate>
#include <QEvent>
#include <QHBoxLayout>
@@ -77,6 +78,9 @@ void PortfolioOptimizationView::build_ui() {
layout->setContentsMargins(0, 0, 0, 0);
tabs_ = new QTabWidget;
+ tabs_->tabBar()->setElideMode(Qt::ElideNone);
+ tabs_->tabBar()->setExpanding(false);
+ tabs_->tabBar()->setUsesScrollButtons(false);
tabs_->setDocumentMode(true);
tabs_->setStyleSheet(QString("QTabWidget::pane { border:0; background:%1; }"
"QTabBar::tab { background:%2; color:%3; padding:6px 14px; border:0;"
@@ -131,7 +135,7 @@ QWidget* PortfolioOptimizationView::build_optimize_tab() {
// via the combo's currentText only after look-up against the storage key.
method_cb_ = add_combo(tr("METHOD:"), method_field_label_,
{"Max Sharpe", "Min Volatility", "Risk Parity", "Max Return", "Equal Weight", "HRP",
- "Target Return"});
+ "Target Return", "B-L Model"});
returns_cb_ = add_combo(tr("RETURNS:"), returns_field_label_,
{"Mean Historical", "EMA", "CAPM", "James-Stein"});
risk_model_cb_ = add_combo(tr("RISK MODEL:"), risk_model_field_label_,
@@ -493,9 +497,19 @@ QWidget* PortfolioOptimizationView::build_risk_tab() {
QString("color:%1; font-size:12px; font-weight:700; letter-spacing:1px;").arg(ui::colors::AMBER()));
layout->addWidget(risk_title_);
- risk_body_ = make_placeholder(tr("Risk decomposition shows how each holding contributes to overall portfolio risk.\n"
- "Run optimization to compute marginal risk contributions."));
- layout->addWidget(risk_body_);
+ risk_stack_ = new QStackedWidget;
+ risk_body_ = make_placeholder(tr("Marginal risk contribution decomposes total portfolio volatility across holdings.\n"
+ "Run optimization on the OPTIMIZE tab to compute it from the covariance matrix."));
+ risk_stack_->addWidget(risk_body_);
+
+ risk_table_ = new QTableWidget;
+ risk_table_->setColumnCount(5);
+ risk_table_->setHorizontalHeaderLabels(
+ {tr("SYMBOL"), tr("WEIGHT"), tr("ASSET VOL"), tr("MARGINAL RISK"), tr("RISK CONTRIB")});
+ style_table(risk_table_);
+ risk_stack_->addWidget(risk_table_);
+
+ layout->addWidget(risk_stack_, 1);
return w;
}
@@ -504,14 +518,24 @@ QWidget* PortfolioOptimizationView::build_stress_tab() {
auto* layout = new QVBoxLayout(w);
layout->setContentsMargins(16, 12, 16, 12);
- stress_title_ = new QLabel(tr("OPTIMIZATION STRESS SCENARIOS"));
+ stress_title_ = new QLabel(tr("STRESS SCENARIOS"));
stress_title_->setStyleSheet(
QString("color:%1; font-size:12px; font-weight:700; letter-spacing:1px;").arg(ui::colors::AMBER()));
layout->addWidget(stress_title_);
- stress_body_ = make_placeholder(tr("Test how different optimization methods perform under stress conditions.\n"
- "Compares optimal weights across historical crisis scenarios."));
- layout->addWidget(stress_body_);
+ stress_stack_ = new QStackedWidget;
+ stress_body_ = make_placeholder(tr("Applies historical crisis shocks to your current asset-class exposure.\n"
+ "Select a portfolio to see estimated impact per scenario."));
+ stress_stack_->addWidget(stress_body_);
+
+ stress_table_ = new QTableWidget;
+ stress_table_->setColumnCount(4);
+ stress_table_->setHorizontalHeaderLabels(
+ {tr("SCENARIO"), tr("DESCRIPTION"), tr("IMPACT"), tr("EST. LOSS")});
+ style_table(stress_table_);
+ stress_stack_->addWidget(stress_table_);
+
+ layout->addWidget(stress_stack_, 1);
return w;
}
@@ -525,10 +549,25 @@ QWidget* PortfolioOptimizationView::build_black_litterman_tab() {
QString("color:%1; font-size:12px; font-weight:700; letter-spacing:1px;").arg(ui::colors::AMBER()));
layout->addWidget(bl_title_);
- bl_body_ = make_placeholder(tr("The Black-Litterman model combines market equilibrium returns with investor views\n"
- "to produce more stable and intuitive portfolio allocations.\n\n"
- "Select 'B-L Model' from the METHOD dropdown on the OPTIMIZE tab to run it."));
+ bl_body_ = new QLabel(tr("Market-implied equilibrium returns (π = δ·Σ·w_market) shown below. "
+ "Select 'B-L Model' as the METHOD on the OPTIMIZE tab to also compute B-L weights."));
+ bl_body_->setWordWrap(true);
+ bl_body_->setStyleSheet(QString("color:%1; font-size:10px;").arg(ui::colors::TEXT_TERTIARY()));
layout->addWidget(bl_body_);
+
+ bl_stack_ = new QStackedWidget;
+ auto* bl_placeholder = make_placeholder(tr("Run optimization on the OPTIMIZE tab to compute "
+ "market-implied returns and Black-Litterman weights."));
+ bl_stack_->addWidget(bl_placeholder);
+
+ bl_table_ = new QTableWidget;
+ bl_table_->setColumnCount(4);
+ bl_table_->setHorizontalHeaderLabels(
+ {tr("SYMBOL"), tr("IMPLIED RETURN"), tr("CURRENT WT"), tr("OPTIMIZED WT")});
+ style_table(bl_table_);
+ bl_stack_->addWidget(bl_table_);
+
+ layout->addWidget(bl_stack_, 1);
return w;
}
@@ -539,6 +578,7 @@ void PortfolioOptimizationView::set_data(const portfolio::PortfolioSummary& summ
currency_ = currency;
has_data_ = true;
update_allocation();
+ update_stress(); // stress test runs off current holdings — no optimization needed
}
void PortfolioOptimizationView::changeEvent(QEvent* event) {
@@ -589,25 +629,35 @@ void PortfolioOptimizationView::retranslateUi() {
"against historical data to evaluate out-of-sample performance."));
if (risk_title_) risk_title_->setText(tr("RISK DECOMPOSITION"));
if (risk_body_)
- risk_body_->setText(tr("Risk decomposition shows how each holding contributes to overall portfolio risk.\n"
- "Run optimization to compute marginal risk contributions."));
- if (stress_title_) stress_title_->setText(tr("OPTIMIZATION STRESS SCENARIOS"));
+ risk_body_->setText(tr("Marginal risk contribution decomposes total portfolio volatility across holdings.\n"
+ "Run optimization on the OPTIMIZE tab to compute it from the covariance matrix."));
+ if (risk_table_)
+ risk_table_->setHorizontalHeaderLabels(
+ {tr("SYMBOL"), tr("WEIGHT"), tr("ASSET VOL"), tr("MARGINAL RISK"), tr("RISK CONTRIB")});
+ if (stress_title_) stress_title_->setText(tr("STRESS SCENARIOS"));
if (stress_body_)
- stress_body_->setText(tr("Test how different optimization methods perform under stress conditions.\n"
- "Compares optimal weights across historical crisis scenarios."));
+ stress_body_->setText(tr("Applies historical crisis shocks to your current asset-class exposure.\n"
+ "Select a portfolio to see estimated impact per scenario."));
+ if (stress_table_)
+ stress_table_->setHorizontalHeaderLabels(
+ {tr("SCENARIO"), tr("DESCRIPTION"), tr("IMPACT"), tr("EST. LOSS")});
if (bl_title_) bl_title_->setText(tr("BLACK-LITTERMAN MODEL"));
if (bl_body_)
- bl_body_->setText(tr("The Black-Litterman model combines market equilibrium returns with investor views\n"
- "to produce more stable and intuitive portfolio allocations.\n\n"
- "Select 'B-L Model' from the METHOD dropdown on the OPTIMIZE tab to run it."));
+ bl_body_->setText(tr("Market-implied equilibrium returns (π = δ·Σ·w_market) shown below. "
+ "Select 'B-L Model' as the METHOD on the OPTIMIZE tab to also compute B-L weights."));
+ if (bl_table_)
+ bl_table_->setHorizontalHeaderLabels(
+ {tr("SYMBOL"), tr("IMPLIED RETURN"), tr("CURRENT WT"), tr("OPTIMIZED WT")});
// Combo items are English keys sent to Python (Max Sharpe, Risk Parity, etc.)
// and intentionally not translated — they double as cache keys + the analytics
// service contract. Combos remain untranslated. update_allocation() and
// update_strategies/compare rebuild table content from data, no tr() literals
// in row cells, so a re-render isn't needed for table contents.
- if (has_data_)
+ if (has_data_) {
update_allocation();
+ update_stress();
+ }
}
// ── Optimization ──────────────────────────────────────────────────────────────
@@ -692,6 +742,8 @@ void PortfolioOptimizationView::run_optimization() {
self->update_frontier(root["frontier"].toArray());
self->update_strategies(root["comparison"].toObject());
self->update_compare(root["comparison"].toObject());
+ self->update_risk(root);
+ self->update_black_litterman(root);
if (self->backtest_optimal_btn_)
self->backtest_optimal_btn_->setEnabled(true);
},
@@ -774,10 +826,12 @@ void PortfolioOptimizationView::run_optimization() {
set(4, action, ac);
}
- // ── Frontier, Strategies, Compare ─────────────────────────
+ // ── Frontier, Strategies, Compare, Risk, B-L ──────────────
self->update_frontier(root["frontier"].toArray());
self->update_strategies(root["comparison"].toObject());
self->update_compare(root["comparison"].toObject());
+ self->update_risk(root);
+ self->update_black_litterman(root);
if (self->backtest_optimal_btn_)
self->backtest_optimal_btn_->setEnabled(true);
@@ -1022,4 +1076,156 @@ void PortfolioOptimizationView::update_compare(const QJsonObject& comparison) {
compare_stack_->setCurrentIndex(1);
}
+// ── Risk decomposition ──────────────────────────────────────────────────────────
+// Renders the per-asset marginal risk contribution computed by the optimizer
+// (risk_contributions / marginal_risk / asset_volatility from the Python output).
+void PortfolioOptimizationView::update_risk(const QJsonObject& root) {
+ const auto rc = root["risk_contributions"].toObject();
+ const auto av = root["asset_volatility"].toObject();
+ const auto mr = root["marginal_risk"].toObject();
+ const auto weights = root["weights"].toObject();
+ if (rc.isEmpty() || !risk_table_)
+ return;
+
+ struct Row {
+ QString sym;
+ double w, vol, mrc, rc;
+ };
+ QVector<Row> rows;
+ for (auto it = rc.begin(); it != rc.end(); ++it) {
+ const QString sym = it.key();
+ rows.append({sym, weights.value(sym).toDouble() * 100.0, av.value(sym).toDouble() * 100.0,
+ mr.value(sym).toDouble(), it.value().toDouble()});
+ }
+ std::sort(rows.begin(), rows.end(), [](const Row& a, const Row& b) { return a.rc > b.rc; });
+
+ risk_table_->setRowCount(rows.size());
+ for (int r = 0; r < rows.size(); ++r) {
+ const auto& row = rows[r];
+ risk_table_->setRowHeight(r, 28);
+ auto set = [&](int col, const QString& text, const char* color = nullptr) {
+ auto* item = new QTableWidgetItem(text);
+ item->setTextAlignment(col == 0 ? (Qt::AlignLeft | Qt::AlignVCenter) : (Qt::AlignRight | Qt::AlignVCenter));
+ if (color)
+ item->setForeground(QColor(color));
+ risk_table_->setItem(r, col, item);
+ };
+ set(0, row.sym, ui::colors::CYAN);
+ set(1, QString("%1%").arg(row.w, 0, 'f', 1));
+ set(2, QString("%1%").arg(row.vol, 0, 'f', 1));
+ set(3, QString::number(row.mrc, 'f', 3));
+ // Flag assets whose share of risk exceeds their share of capital (risk concentration).
+ const char* rc_col = row.rc > row.w + 0.5 ? ui::colors::NEGATIVE : ui::colors::AMBER;
+ set(4, QString("%1%").arg(row.rc, 0, 'f', 1), rc_col);
+ }
+ risk_stack_->setCurrentIndex(1);
+}
+
+// ── Stress scenarios ────────────────────────────────────────────────────────────
+// Applies historical crisis shocks to the portfolio's current asset-class mix.
+// Runs off current holdings — no optimization required.
+void PortfolioOptimizationView::update_stress() {
+ if (summary_.holdings.isEmpty() || !stress_table_)
+ return;
+
+ struct StressScenario {
+ const char* name;
+ const char* desc;
+ double equity_shock; // %
+ double bond_shock; // %
+ double commodity_shock; // %
+ };
+ static const StressScenario kScenarios[] = {
+ {"2008 Financial Crisis", "Lehman collapse, credit freeze", -38.5, 5.2, -33.0},
+ {"2020 COVID Crash", "Pandemic selloff (Feb–Mar 2020)", -33.9, 1.2, -20.0},
+ {"Dot-com Bust", "Tech bubble burst (2000–2002)", -49.1, 8.5, -10.0},
+ {"Interest Rate Shock +3%", "Aggressive Fed tightening", -15.0, -12.0, -5.0},
+ {"Black Monday 1987", "Single-day market crash", -22.6, 2.0, -8.0},
+ {"Inflation Surge", "1970s-style stagflation", -10.0, -15.0, 25.0},
+ {"Geopolitical Crisis", "Major conflict escalation", -12.0, 3.0, 15.0},
+ {"Currency Crisis", "Emerging-market contagion", -18.0, 1.0, 5.0},
+ };
+
+ // Classify current holdings into asset classes by symbol heuristics.
+ double equity_wt = 0, bond_wt = 0, commodity_wt = 0, crypto_wt = 0;
+ for (const auto& h : summary_.holdings) {
+ const QString s = h.symbol.toUpper();
+ const double wt = h.weight / 100.0;
+ const bool is_crypto = s.endsWith("-USD") || s.endsWith("-USDT") || s.endsWith("USDT");
+ const bool is_bond = s == "TLT" || s == "AGG" || s == "BND" || s == "IEF" || s == "SHY" || s == "LQD" ||
+ s == "HYG" || s.contains("TREAS") || s.contains("BOND");
+ const bool is_comm = s == "GLD" || s == "IAU" || s == "SLV" || s == "USO" || s == "DBC" ||
+ s.contains("GOLD") || s.contains("OIL");
+ if (is_crypto)
+ crypto_wt += wt;
+ else if (is_bond)
+ bond_wt += wt;
+ else if (is_comm)
+ commodity_wt += wt;
+ else
+ equity_wt += wt;
+ }
+
+ const double total_mv = summary_.total_market_value;
+ const int n = static_cast<int>(sizeof(kScenarios) / sizeof(kScenarios[0]));
+ stress_table_->setRowCount(n);
+ for (int i = 0; i < n; ++i) {
+ const auto& sc = kScenarios[i];
+ // Crypto treated as 1.5× equity beta to the shock.
+ const double impact_pct = sc.equity_shock * equity_wt + sc.bond_shock * bond_wt +
+ sc.commodity_shock * commodity_wt + sc.equity_shock * 1.5 * crypto_wt;
+ const double loss = total_mv * impact_pct / 100.0;
+
+ stress_table_->setRowHeight(i, 28);
+ auto set = [&](int col, const QString& text, const char* color = nullptr) {
+ auto* item = new QTableWidgetItem(text);
+ item->setTextAlignment(col <= 1 ? (Qt::AlignLeft | Qt::AlignVCenter) : (Qt::AlignRight | Qt::AlignVCenter));
+ if (color)
+ item->setForeground(QColor(color));
+ stress_table_->setItem(i, col, item);
+ };
+ set(0, tr(sc.name), ui::colors::TEXT_PRIMARY);
+ set(1, tr(sc.desc), ui::colors::TEXT_TERTIARY);
+ const char* ic = impact_pct >= 0 ? ui::colors::POSITIVE : ui::colors::NEGATIVE;
+ set(2, QString("%1%2%").arg(impact_pct >= 0 ? "+" : "").arg(impact_pct, 0, 'f', 1), ic);
+ set(3, QString("%1%2 %3").arg(loss >= 0 ? "+" : "-", currency_, QString::number(std::abs(loss), 'f', 0)), ic);
+ }
+ stress_stack_->setCurrentIndex(1);
+}
+
+// ── Black-Litterman ─────────────────────────────────────────────────────────────
+// Shows market-implied equilibrium returns plus the optimized weights. When the
+// B-L method was the one run, the final column is the Black-Litterman allocation.
+void PortfolioOptimizationView::update_black_litterman(const QJsonObject& root) {
+ const auto implied = root["implied_returns"].toObject();
+ const auto opt_weights = root["weights"].toObject();
+ if (implied.isEmpty() || !bl_table_)
+ return;
+
+ const bool is_bl = root["strategy"].toString() == "black_litterman";
+ bl_table_->setHorizontalHeaderLabels(
+ {tr("SYMBOL"), tr("IMPLIED RETURN"), tr("CURRENT WT"), is_bl ? tr("B-L WT") : tr("OPTIMIZED WT")});
+
+ bl_table_->setRowCount(static_cast<int>(summary_.holdings.size()));
+ for (int r = 0; r < static_cast<int>(summary_.holdings.size()); ++r) {
+ const auto& h = summary_.holdings[r];
+ bl_table_->setRowHeight(r, 28);
+ auto set = [&](int col, const QString& text, const char* color = nullptr) {
+ auto* item = new QTableWidgetItem(text);
+ item->setTextAlignment(col == 0 ? (Qt::AlignLeft | Qt::AlignVCenter) : (Qt::AlignRight | Qt::AlignVCenter));
+ if (color)
+ item->setForeground(QColor(color));
+ bl_table_->setItem(r, col, item);
+ };
+ const double imp = implied.value(h.symbol).toDouble() * 100.0;
+ const double ow = opt_weights.value(h.symbol).toDouble(h.weight / 100.0) * 100.0;
+ set(0, h.symbol, ui::colors::CYAN);
+ set(1, QString("%1%2%").arg(imp >= 0 ? "+" : "").arg(imp, 0, 'f', 1),
+ imp >= 0 ? ui::colors::POSITIVE : ui::colors::NEGATIVE);
+ set(2, QString("%1%").arg(h.weight, 0, 'f', 1));
+ set(3, QString("%1%").arg(ow, 0, 'f', 1), ui::colors::AMBER);
+ }
+ bl_stack_->setCurrentIndex(1);
+}
+
} // namespace fincept::screens
diff --git a/fincept-qt/src/screens/portfolio/views/PortfolioOptimizationView.h b/fincept-qt/src/screens/portfolio/views/PortfolioOptimizationView.h
index 949c21487..5b3bc82f1 100644
--- a/fincept-qt/src/screens/portfolio/views/PortfolioOptimizationView.h
+++ b/fincept-qt/src/screens/portfolio/views/PortfolioOptimizationView.h
@@ -51,6 +51,9 @@ class PortfolioOptimizationView : public QWidget {
void update_frontier(const QJsonArray& frontier_pts);
void update_strategies(const QJsonObject& comparison);
void update_compare(const QJsonObject& comparison);
+ void update_risk(const QJsonObject& root);
+ void update_stress();
+ void update_black_litterman(const QJsonObject& root);
QTabWidget* tabs_ = nullptr;
int optimize_tab_index_ = -1;
@@ -89,11 +92,17 @@ class PortfolioOptimizationView : public QWidget {
QTableWidget* backtest_metrics_table_ = nullptr;
QLabel* backtest_status_label_ = nullptr;
QLabel* risk_title_ = nullptr;
- QLabel* risk_body_ = nullptr;
+ QLabel* risk_body_ = nullptr; // placeholder (page 0 of risk_stack_)
+ QStackedWidget* risk_stack_ = nullptr;
+ QTableWidget* risk_table_ = nullptr;
QLabel* stress_title_ = nullptr;
- QLabel* stress_body_ = nullptr;
+ QLabel* stress_body_ = nullptr; // placeholder (page 0 of stress_stack_)
+ QStackedWidget* stress_stack_ = nullptr;
+ QTableWidget* stress_table_ = nullptr;
QLabel* bl_title_ = nullptr;
- QLabel* bl_body_ = nullptr;
+ QLabel* bl_body_ = nullptr; // explanatory note, kept above the B-L table
+ QStackedWidget* bl_stack_ = nullptr;
+ QTableWidget* bl_table_ = nullptr;
// ── FRONTIER tab ──────────────────────────────────────────────────────────
QStackedWidget* frontier_stack_ = nullptr; // 0=placeholder, 1=chart
diff --git a/fincept-qt/src/screens/portfolio/views/QuantStatsView.cpp b/fincept-qt/src/screens/portfolio/views/QuantStatsView.cpp
index 472774d69..34de73f06 100644
--- a/fincept-qt/src/screens/portfolio/views/QuantStatsView.cpp
+++ b/fincept-qt/src/screens/portfolio/views/QuantStatsView.cpp
@@ -6,6 +6,7 @@
#include "ui/theme/Theme.h"
#include <QBarSeries>
+#include <QTabBar>
#include <QBarSet>
#include <QChart>
#include <QChartView>
@@ -131,6 +132,9 @@ void QuantStatsView::build_ui() {
// ── Tab widget ────────────────────────────────────────────────────────────
tabs_ = new QTabWidget;
+ tabs_->tabBar()->setElideMode(Qt::ElideNone);
+ tabs_->tabBar()->setExpanding(false);
+ tabs_->tabBar()->setUsesScrollButtons(false);
tabs_->setDocumentMode(true);
tabs_->setStyleSheet(QString("QTabWidget::pane { border:0; background:%1; }"
"QTabBar::tab { background:%2; color:%3; padding:6px 14px; border:0;"
diff --git a/fincept-qt/src/screens/portfolio/views/ReportsView.cpp b/fincept-qt/src/screens/portfolio/views/ReportsView.cpp
index a58573d5d..83868f36c 100644
--- a/fincept-qt/src/screens/portfolio/views/ReportsView.cpp
+++ b/fincept-qt/src/screens/portfolio/views/ReportsView.cpp
@@ -5,6 +5,7 @@
#include "ui/theme/Theme.h"
#include <QEvent>
+#include <QTabBar>
#include <QGridLayout>
#include <QHBoxLayout>
#include <QHeaderView>
@@ -24,6 +25,9 @@ void ReportsView::build_ui() {
layout->setContentsMargins(0, 0, 0, 0);
tabs_ = new QTabWidget;
+ tabs_->tabBar()->setElideMode(Qt::ElideNone);
+ tabs_->tabBar()->setExpanding(false);
+ tabs_->tabBar()->setUsesScrollButtons(false);
tabs_->setDocumentMode(true);
tabs_->setStyleSheet(QString("QTabWidget::pane { border:0; background:%1; }"
"QTabBar::tab { background:%2; color:%3; padding:6px 14px; border:0;"
diff --git a/fincept-qt/src/screens/portfolio/views/RiskManagementView.cpp b/fincept-qt/src/screens/portfolio/views/RiskManagementView.cpp
index a951337c5..5f008febb 100644
--- a/fincept-qt/src/screens/portfolio/views/RiskManagementView.cpp
+++ b/fincept-qt/src/screens/portfolio/views/RiskManagementView.cpp
@@ -4,6 +4,7 @@
#include "ui/theme/Theme.h"
#include <QEvent>
+#include <QTabBar>
#include <QGridLayout>
#include <QHBoxLayout>
#include <QHeaderView>
@@ -52,6 +53,9 @@ void RiskManagementView::build_ui() {
layout->setContentsMargins(0, 0, 0, 0);
tabs_ = new QTabWidget;
+ tabs_->tabBar()->setElideMode(Qt::ElideNone);
+ tabs_->tabBar()->setExpanding(false);
+ tabs_->tabBar()->setUsesScrollButtons(false);
tabs_->setDocumentMode(true);
tabs_->setStyleSheet(QString("QTabWidget::pane { border:0; background:%1; }"
"QTabBar::tab { background:%2; color:%3; padding:8px 18px; border:0;"
diff --git a/fincept-qt/src/services/algo_trading/AlgoTradingService.cpp b/fincept-qt/src/services/algo_trading/AlgoTradingService.cpp
index c9318f6f3..03cf2039f 100644
--- a/fincept-qt/src/services/algo_trading/AlgoTradingService.cpp
+++ b/fincept-qt/src/services/algo_trading/AlgoTradingService.cpp
@@ -1,30 +1,21 @@
// src/services/algo_trading/AlgoTradingService.cpp
#include "services/algo_trading/AlgoTradingService.h"
-#include "core/config/AppPaths.h"
+#include "algo_engine/BacktestEngine.h"
+#include "algo_engine/CandleDataFetcher.h"
#include "core/logging/Logger.h"
#include "python/PythonRunner.h"
-#include "storage/cache/CacheManager.h"
#include "storage/sqlite/Database.h"
-#include "trading/UnifiedTrading.h"
+#include "trading/AccountManager.h"
+#include <QDate>
#include <QFile>
+#include <QJsonArray>
#include <QJsonDocument>
-#include <QPointer>
#include <QUuid>
namespace fincept::services::algo {
-static constexpr int kStrategiesTtlSec = 30;
-static constexpr int kDeploymentsTtlSec = 30;
-static constexpr const char* kStrategiesCacheKey = "algo:strategies:registry";
-static constexpr const char* kDeploymentsCacheKey = "algo:deployments";
-
-// ── DB path helper ────────────────────────────────────────────────────────────
-static QString algo_db_path() {
- return fincept::AppPaths::data() + "/fincept.db";
-}
-
AlgoTradingService& AlgoTradingService::instance() {
static AlgoTradingService inst;
return inst;
@@ -32,54 +23,48 @@ AlgoTradingService& AlgoTradingService::instance() {
AlgoTradingService::AlgoTradingService(QObject* parent) : QObject(parent) {}
-void AlgoTradingService::run_python(const QString& script, const QStringList& args, const QString& context,
- std::function<void(bool, const QString&)> cb) {
- QPointer<AlgoTradingService> self = this;
- python::PythonRunner::instance().run(script, args, [self, context, cb](python::PythonResult result) {
- if (!self)
- return;
- cb(result.success, result.success ? result.output : result.error);
- });
-}
-
// ── Strategy CRUD ─────────────────────────────────────────────────────────────
+// Native SQLite UPSERT into algo_strategies (schema owned by migration v023).
void AlgoTradingService::save_strategy(const AlgoStrategy& strategy) {
- QJsonObject obj;
-
- // Upsert by name: if a strategy with the same name exists, reuse its ID
+ // Upsert by name: if an active strategy with the same name exists, reuse its ID
QString resolved_id = strategy.id;
if (resolved_id.isEmpty() && !strategy.name.isEmpty()) {
auto q = fincept::Database::instance().execute(
- "SELECT id FROM algo_strategies WHERE name = ? AND is_active = 1 LIMIT 1",
- {strategy.name});
+ "SELECT id FROM algo_strategies WHERE name = ? AND is_active = 1 LIMIT 1", {strategy.name});
if (q.is_ok() && q.value().next())
resolved_id = q.value().value(0).toString();
}
if (resolved_id.isEmpty())
resolved_id = QUuid::createUuid().toString(QUuid::WithoutBraces);
- obj["id"] = resolved_id;
- obj["name"] = strategy.name;
- obj["description"] = strategy.description;
- obj["timeframe"] = strategy.timeframe;
- obj["entry_conditions"] = strategy.entry_conditions;
- obj["exit_conditions"] = strategy.exit_conditions;
- obj["entry_logic"] = strategy.entry_logic;
- obj["exit_logic"] = strategy.exit_logic;
- obj["stop_loss"] = strategy.stop_loss;
- obj["take_profit"] = strategy.take_profit;
- obj["trailing_stop"] = strategy.trailing_stop;
-
- auto json = QJsonDocument(obj).toJson(QJsonDocument::Compact);
- run_python("algo_trading/backtest_engine.py", {"save_strategy", json, "--db", algo_db_path()}, "save_strategy",
- [this, obj](bool ok, const QString& out) {
- if (!ok) {
- emit error_occurred("save_strategy", out);
- return;
- }
- fincept::CacheManager::instance().remove(kStrategiesCacheKey);
- emit strategy_saved(obj["id"].toString());
- });
+ const QString entry_json =
+ QString::fromUtf8(QJsonDocument(strategy.entry_conditions).toJson(QJsonDocument::Compact));
+ const QString exit_json =
+ QString::fromUtf8(QJsonDocument(strategy.exit_conditions).toJson(QJsonDocument::Compact));
+
+ // is_active=1 in the UPDATE branch so re-saving a soft-deleted strategy revives it.
+ auto r = fincept::Database::instance().execute(
+ "INSERT INTO algo_strategies "
+ "(id, name, description, timeframe, entry_conditions, exit_conditions, "
+ " entry_logic, exit_logic, stop_loss, take_profit, trailing_stop, updated_at) "
+ "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP) "
+ "ON CONFLICT(id) DO UPDATE SET "
+ " name=excluded.name, description=excluded.description, timeframe=excluded.timeframe, "
+ " entry_conditions=excluded.entry_conditions, exit_conditions=excluded.exit_conditions, "
+ " entry_logic=excluded.entry_logic, exit_logic=excluded.exit_logic, "
+ " stop_loss=excluded.stop_loss, take_profit=excluded.take_profit, "
+ " trailing_stop=excluded.trailing_stop, is_active=1, updated_at=CURRENT_TIMESTAMP",
+ {resolved_id, strategy.name, strategy.description, strategy.timeframe, entry_json, exit_json,
+ strategy.entry_logic, strategy.exit_logic, strategy.stop_loss, strategy.take_profit,
+ strategy.trailing_stop});
+
+ if (r.is_err()) {
+ LOG_ERROR("AlgoTrading", QString("save_strategy failed: %1").arg(QString::fromStdString(r.error())));
+ emit error_occurred("save_strategy", QString::fromStdString(r.error()));
+ return;
+ }
+ LOG_INFO("AlgoTrading", QString("Saved strategy %1 (%2)").arg(resolved_id, strategy.name));
+ emit strategy_saved(resolved_id);
}
static QVector<AlgoStrategy> parse_strategies(const QJsonArray& arr) {
@@ -163,15 +148,16 @@ void AlgoTradingService::list_strategies() {
}
void AlgoTradingService::delete_strategy(const QString& id) {
- run_python("algo_trading/backtest_engine.py", {"delete_strategy", id, "--db", algo_db_path()}, "delete_strategy",
- [this, id](bool ok, const QString& out) {
- if (!ok) {
- emit error_occurred("delete_strategy", out);
- return;
- }
- fincept::CacheManager::instance().remove(kStrategiesCacheKey);
- emit strategy_deleted(id);
- });
+ // Soft delete — keeps history; list_strategies filters on is_active = 1.
+ auto r = fincept::Database::instance().execute(
+ "UPDATE algo_strategies SET is_active = 0, updated_at = CURRENT_TIMESTAMP WHERE id = ?", {id});
+ if (r.is_err()) {
+ LOG_ERROR("AlgoTrading", QString("delete_strategy failed: %1").arg(QString::fromStdString(r.error())));
+ emit error_occurred("delete_strategy", QString::fromStdString(r.error()));
+ return;
+ }
+ LOG_INFO("AlgoTrading", QString("Deleted strategy %1").arg(id));
+ emit strategy_deleted(id);
}
// ── Deployment lifecycle — now handled by AlgoEngine (src/algo_engine/) ──────
@@ -179,26 +165,63 @@ void AlgoTradingService::delete_strategy(const QString& id) {
// list_deployments, check_bridge_needed have been removed.
// Use fincept::algo::AlgoEngine::instance() for all deployment operations.
-// ── Backtesting ───────────────────────────────────────────────────────────────
-void AlgoTradingService::run_backtest(const QString& strategy_id, const QString& symbol, const QString& start_date,
- const QString& end_date, double capital) {
- QJsonObject params;
- params["strategy_id"] = strategy_id;
- params["symbol"] = symbol;
- params["start_date"] = start_date;
- params["end_date"] = end_date;
- params["initial_capital"] = capital;
- auto json = QJsonDocument(params).toJson(QJsonDocument::Compact);
-
- run_python("algo_trading/backtest_engine.py", {"run_backtest", json, "--db", algo_db_path()}, "backtest",
- [this](bool ok, const QString& out) {
- if (!ok) {
- emit error_occurred("backtest", out);
- return;
- }
- auto doc = QJsonDocument::fromJson(python::extract_json(out).toUtf8());
- emit backtest_result(doc.object());
- });
+// ── Backtesting (native C++ engine) ────────────────────────────────────────────
+void AlgoTradingService::run_backtest(const AlgoStrategy& strategy, const QString& symbol,
+ const QString& start_date, const QString& end_date, double capital) {
+ // Derive the historical window from the date range (fallback: 1 year).
+ int lookback_days = 365;
+ const QDate d1 = QDate::fromString(start_date, "yyyy-MM-dd");
+ const QDate d2 = QDate::fromString(end_date, "yyyy-MM-dd");
+ if (d1.isValid() && d2.isValid() && d1 < d2)
+ lookback_days = static_cast<int>(d1.daysTo(d2));
+ if (lookback_days < 1)
+ lookback_days = 365;
+
+ // Data source: a connected broker if one exists, otherwise native Yahoo.
+ QString broker_id, account_id;
+ auto& accts = trading::AccountManager::instance();
+ for (const auto& acc : accts.active_accounts()) {
+ if (accts.connection_state(acc.account_id) == trading::ConnectionState::Connected) {
+ broker_id = acc.broker_id;
+ account_id = acc.account_id;
+ break;
+ }
+ }
+ const fincept::algo::DataSource source =
+ broker_id.isEmpty() ? fincept::algo::DataSource::YFinance : fincept::algo::DataSource::Auto;
+
+ // Capture strategy parameters for the async callback.
+ const QJsonArray entry = strategy.entry_conditions;
+ const QJsonArray exit = strategy.exit_conditions;
+ const QString entry_logic = strategy.entry_logic.isEmpty() ? QStringLiteral("AND") : strategy.entry_logic;
+ const QString exit_logic = strategy.exit_logic.isEmpty() ? QStringLiteral("AND") : strategy.exit_logic;
+ const double sl = strategy.stop_loss;
+ const double tp = strategy.take_profit;
+ const double trail = strategy.trailing_stop;
+ const QString timeframe = strategy.timeframe.isEmpty() ? QStringLiteral("1d") : strategy.timeframe;
+
+ LOG_INFO("AlgoTrading",
+ QString("Backtest %1 [%2] %3 — source=%4")
+ .arg(symbol, timeframe, strategy.name,
+ broker_id.isEmpty() ? "yahoo" : broker_id));
+
+ // Singleton — `this` outlives any async work, so capture directly.
+ fincept::algo::CandleDataFetcher::instance().fetch(
+ symbol, timeframe, lookback_days, source, broker_id, account_id,
+ [this, entry, exit, entry_logic, exit_logic, sl, tp, trail, capital, timeframe, symbol](
+ bool ok, const QVector<fincept::algo::OhlcvCandle>& candles, const QString& err) {
+ if (!ok || candles.isEmpty()) {
+ emit error_occurred("backtest", err.isEmpty() ? QStringLiteral("No data") : err);
+ return;
+ }
+ const QJsonObject result = fincept::algo::BacktestEngine::run(
+ candles, entry, entry_logic, exit, exit_logic, sl, tp, trail, capital, timeframe);
+ if (!result.value("success").toBool(false)) {
+ emit error_occurred("backtest", result.value("error").toString(QStringLiteral("Backtest failed")));
+ return;
+ }
+ emit backtest_result(result);
+ });
}
// Scanner is now in AlgoScanner (src/algo_engine/AlgoScanner.h/.cpp).
diff --git a/fincept-qt/src/services/algo_trading/AlgoTradingService.h b/fincept-qt/src/services/algo_trading/AlgoTradingService.h
index cb413915b..103f504f0 100644
--- a/fincept-qt/src/services/algo_trading/AlgoTradingService.h
+++ b/fincept-qt/src/services/algo_trading/AlgoTradingService.h
@@ -6,7 +6,8 @@
namespace fincept::services::algo {
-/// Singleton service for Algo Trading — strategy CRUD, deployment, scanning, backtesting.
+/// Singleton service for Algo Trading — strategy CRUD and native C++ backtesting.
+/// Deployment lifecycle lives in AlgoEngine; scanning in AlgoScanner.
class AlgoTradingService : public QObject {
Q_OBJECT
public:
@@ -19,9 +20,12 @@ class AlgoTradingService : public QObject {
// Deployment lifecycle is now in AlgoEngine (src/algo_engine/AlgoEngine.h).
- // ── Backtesting ─────────────────────────────────────────────────────────
- void run_backtest(const QString& strategy_id, const QString& symbol, const QString& start_date,
- const QString& end_date, double capital);
+ // ── Backtesting (native C++ engine) ──────────────────────────────────────
+ // Backtests the given strategy's current conditions directly (no DB round-trip),
+ // so unsaved builder edits are testable. Candles come from the connected broker
+ // when one is connected, otherwise native Yahoo Finance.
+ void run_backtest(const fincept::services::algo::AlgoStrategy& strategy, const QString& symbol,
+ const QString& start_date, const QString& end_date, double capital);
// Scanner is now in AlgoScanner (src/algo_engine/AlgoScanner.h).
@@ -29,17 +33,12 @@ class AlgoTradingService : public QObject {
void strategy_saved(QString id);
void strategies_loaded(QVector<fincept::services::algo::AlgoStrategy> strategies);
void strategy_deleted(QString id);
- void deployment_started(QString deployment_id);
- void deployment_stopped(QString deployment_id);
void backtest_result(QJsonObject data);
void error_occurred(QString context, QString message);
private:
explicit AlgoTradingService(QObject* parent = nullptr);
Q_DISABLE_COPY(AlgoTradingService)
-
- void run_python(const QString& script, const QStringList& args, const QString& context,
- std::function<void(bool, const QString&)> cb);
};
} // namespace fincept::services::algo
diff --git a/fincept-qt/src/services/markets/MarketDataService.cpp b/fincept-qt/src/services/markets/MarketDataService.cpp
index dd48c1f56..09ded9b0d 100644
--- a/fincept-qt/src/services/markets/MarketDataService.cpp
+++ b/fincept-qt/src/services/markets/MarketDataService.cpp
@@ -4,6 +4,7 @@
#include "python/PythonRunner.h"
#include "python/PythonWorker.h"
#include "storage/cache/CacheManager.h"
+#include "storage/repositories/SettingsRepository.h"
# include "datahub/DataHub.h"
# include "datahub/DataHubMetaTypes.h"
@@ -791,4 +792,124 @@ void MarketDataService::fetch_sparklines(const QStringList& symbols, SparklineCa
});
}
+// ── Display-name resolution ─────────────────────────────────────────────────
+
+void MarketDataService::load_name_cache() {
+ if (name_cache_loaded_)
+ return;
+ name_cache_loaded_ = true;
+
+ auto load = [](const char* key, QHash<QString, QString>& cache) {
+ auto res = SettingsRepository::instance().get(key);
+ if (!res.is_ok() || res.value().isEmpty())
+ return;
+ auto doc = QJsonDocument::fromJson(res.value().toUtf8());
+ if (!doc.isObject())
+ return;
+ const QJsonObject obj = doc.object();
+ for (auto it = obj.begin(); it != obj.end(); ++it)
+ cache.insert(it.key(), it.value().toString());
+ };
+ load("market_symbol_names", name_cache_);
+ load("market_symbol_currencies", currency_cache_);
+}
+
+void MarketDataService::persist_name_cache() {
+ auto dump = [](const QHash<QString, QString>& cache) {
+ QJsonObject obj;
+ for (auto it = cache.constBegin(); it != cache.constEnd(); ++it)
+ obj.insert(it.key(), it.value());
+ return QString::fromUtf8(QJsonDocument(obj).toJson(QJsonDocument::Compact));
+ };
+ auto& settings = SettingsRepository::instance();
+ settings.set("market_symbol_names", dump(name_cache_), "market_data");
+ settings.set("market_symbol_currencies", dump(currency_cache_), "market_data");
+}
+
+// ISO 4217 code → display symbol. Falls back to "<CODE> " so an unmapped but
+// valid currency still reads sensibly (e.g. "SEK 142.50").
+QString MarketDataService::currency_prefix(const QString& symbol) {
+ load_name_cache();
+ const QString code = currency_cache_.value(symbol);
+ if (code.isEmpty())
+ return {};
+
+ static const QHash<QString, QString> kSymbols = {
+ {"USD", "$"}, {"EUR", "€"}, {"GBP", "£"}, {"JPY", "¥"},
+ {"CNY", "CN¥"}, {"INR", "₹"}, {"HKD", "HK$"}, {"AUD", "A$"},
+ {"CAD", "C$"}, {"NZD", "NZ$"}, {"SGD", "S$"}, {"KRW", "₩"},
+ {"BRL", "R$"}, {"ZAR", "R"}, {"CHF", "CHF "}, {"RUB", "₽"},
+ {"TWD", "NT$"}, {"THB", "฿"}, {"IDR", "Rp"}, {"MYR", "RM"},
+ {"AED", "AED "},
+ };
+ auto it = kSymbols.find(code.toUpper());
+ return it != kSymbols.end() ? it.value() : (code + QLatin1Char(' '));
+}
+
+void MarketDataService::resolve_names(const QStringList& symbols, NamesCallback cb) {
+ load_name_cache();
+
+ // Build the cached subset for the requested symbols only.
+ auto subset_for = [this](const QStringList& syms) {
+ QHash<QString, QString> m;
+ for (const auto& s : syms)
+ if (name_cache_.contains(s))
+ m.insert(s, name_cache_.value(s));
+ return m;
+ };
+
+ // Deliver what we already know straight away (may be empty on cold start).
+ if (cb)
+ cb(subset_for(symbols));
+
+ // Collect the symbols we still need a name for.
+ QStringList missing;
+ for (const auto& s : symbols)
+ if (!s.isEmpty() && !name_cache_.contains(s))
+ missing.append(s);
+ if (missing.isEmpty())
+ return;
+
+ QStringList args;
+ args << "quote_names" << missing;
+
+ QPointer<MarketDataService> self = this;
+ python::PythonRunner::instance().run(
+ "yfinance_data.py", args,
+ [self, symbols, cb, subset_for](python::PythonResult result) {
+ if (!self)
+ return;
+ if (result.success && !result.output.trimmed().isEmpty()) {
+ auto doc = QJsonDocument::fromJson(result.output.trimmed().toUtf8());
+ if (doc.isObject()) {
+ const QJsonObject root = doc.object();
+ const QJsonObject names = root.value("names").toObject();
+ const QJsonObject currs = root.value("currencies").toObject();
+ bool changed = false;
+ for (auto it = names.begin(); it != names.end(); ++it) {
+ const QString name = it.value().toString().trimmed();
+ if (!name.isEmpty()) {
+ self->name_cache_.insert(it.key(), name);
+ changed = true;
+ }
+ }
+ for (auto it = currs.begin(); it != currs.end(); ++it) {
+ const QString code = it.value().toString().trimmed();
+ if (!code.isEmpty()) {
+ self->currency_cache_.insert(it.key(), code);
+ changed = true;
+ }
+ }
+ if (changed)
+ self->persist_name_cache();
+ }
+ } else if (!result.success) {
+ LOG_WARN("MarketData", "quote_names failed: " + result.error.left(200));
+ }
+ // Re-deliver with whatever resolved (cached subset now richer).
+ if (cb)
+ cb(subset_for(symbols));
+ });
+}
+
} // namespace fincept::services
diff --git a/fincept-qt/src/services/markets/MarketDataService.h b/fincept-qt/src/services/markets/MarketDataService.h
index 84524f7f9..b05d839a7 100644
--- a/fincept-qt/src/services/markets/MarketDataService.h
+++ b/fincept-qt/src/services/markets/MarketDataService.h
@@ -121,6 +121,25 @@ class MarketDataService : public QObject
using SparklineCallback = std::function<void(bool, QHash<QString, QVector<double>>)>;
void fetch_sparklines(const QStringList& symbols, SparklineCallback cb);
+ /// Resolve human-readable display names (e.g. "^GSPC" → "S&P 500",
+ /// "GC=F" → "Gold") for cryptic tickers so market tables can show names
+ /// instead of raw symbols. Names come from yfinance (longName/shortName).
+ ///
+ /// Names are static, so they are cached to disk: the callback fires once
+ /// immediately with whatever is already cached for `symbols`, and — if any
+ /// were missing — again after a single `quote_names` Python call resolves
+ /// and persists them. The network hit is paid once per symbol, never on
+ /// the quote-refresh path.
+ using NamesCallback = std::function<void(const QHash<QString, QString>&)>;
+ void resolve_names(const QStringList& symbols, NamesCallback cb);
+
+ /// Currency-symbol prefix for a symbol's price (e.g. "AAPL" → "$",
+ /// "RELIANCE.NS" → "₹"), or an empty string when unknown or not meaningful
+ /// (forex pairs, index levels/yields). Resolved alongside display names by
+ /// `resolve_names` and read from the same disk-backed cache, so this is a
+ /// cheap synchronous lookup.
+ QString currency_prefix(const QString& symbol);
+
static QVector<MarketCategory> default_global_markets();
static QVector<RegionalMarket> default_regional_markets();
@@ -143,6 +162,15 @@ class MarketDataService : public QObject
const QVector<HistoryPoint>& points);
void publish_sparkline_to_hub(const QString& symbol, const QVector<double>& points);
+ // ── Display-name cache (symbol → human-readable name) ──
+ // Persisted to SettingsRepository so resolution survives restarts and the
+ // per-symbol yfinance .info cost is paid at most once.
+ void load_name_cache();
+ void persist_name_cache();
+ QHash<QString, QString> name_cache_;
+ QHash<QString, QString> currency_cache_; // symbol → ISO currency code (e.g. "USD")
+ bool name_cache_loaded_ = false;
+
// ── Batching ──
struct PendingRequest {
QStringList symbols;
diff --git a/fincept-qt/src/services/news/NewsService.cpp b/fincept-qt/src/services/news/NewsService.cpp
index 08b81b136..cfe6a0669 100644
--- a/fincept-qt/src/services/news/NewsService.cpp
+++ b/fincept-qt/src/services/news/NewsService.cpp
@@ -13,6 +13,7 @@
#include "core/logging/Logger.h"
#include "network/http/HttpClient.h"
#include "storage/cache/CacheManager.h"
+#include "storage/repositories/NewsArticleRepository.h"
# include "datahub/DataHub.h"
# include "datahub/DataHubMetaTypes.h"
@@ -370,13 +371,81 @@ void NewsService::fetch_all_news_progressive(bool force, ArticlesCallback final_
// ── AI Analysis via Fincept API ─────────────────────────────────────────────
+// Parse the `data` object of a /news/analyze response into a NewsAnalysis.
+// Shared by the live request path and the persisted-cache load path so both
+// stay in lockstep.
+static NewsAnalysis parse_analysis_data(const QJsonObject& data) {
+ auto a = data["analysis"].toObject();
+ auto sent = a["sentiment"].toObject();
+ auto mi = a["market_impact"].toObject();
+ auto rs = a["risk_signals"].toObject();
+
+ NewsAnalysis analysis;
+ analysis.sentiment = {sent["score"].toDouble(), sent["intensity"].toDouble(), sent["confidence"].toDouble()};
+ analysis.market_impact = {mi["urgency"].toString(), mi["prediction"].toString()};
+ analysis.summary = a["summary"].toString();
+ analysis.credits_used = data["credits_used"].toInt();
+ analysis.credits_remaining = data["credits_remaining"].toInt();
+
+ for (const auto& v : a["keywords"].toArray())
+ analysis.keywords << v.toString();
+ for (const auto& v : a["topics"].toArray())
+ analysis.topics << v.toString();
+ for (const auto& v : a["key_points"].toArray())
+ analysis.key_points << v.toString();
+
+ auto reg = rs["regulatory"].toObject();
+ auto geo = rs["geopolitical"].toObject();
+ auto ops = rs["operational"].toObject();
+ auto mkt = rs["market"].toObject();
+ analysis.regulatory = {reg["level"].toString(), reg["details"].toString()};
+ analysis.geopolitical = {geo["level"].toString(), geo["details"].toString()};
+ analysis.operational = {ops["level"].toString(), ops["details"].toString()};
+ analysis.market = {mkt["level"].toString(), mkt["details"].toString()};
+
+ // Entities — organizations carry ticker/sector/sentiment; locations
+ // carry a country code; people are name-only.
+ auto ent = a["entities"].toObject();
+ for (const auto& v : ent["organizations"].toArray()) {
+ auto o = v.toObject();
+ analysis.organizations.push_back(
+ {o["name"].toString(), o["ticker"].toString(), o["sector"].toString(), o["sentiment"].toDouble()});
+ }
+ for (const auto& v : ent["people"].toArray()) {
+ auto p = v.toObject();
+ analysis.people.push_back({p["name"].toString(), {}, {}, 0});
+ }
+ for (const auto& v : ent["locations"].toArray()) {
+ auto l = v.toObject();
+ analysis.locations.push_back({l["name"].toString(), l["country_code"].toString(), {}, 0});
+ }
+
+ // Content fetch metadata — surfaces publisher-block notices.
+ auto content = data["content"].toObject();
+ analysis.content.headline = content["headline"].toString();
+ analysis.content.word_count = content["word_count"].toInt();
+ analysis.content.fetch_note = content["fetch_note"].toString();
+
+ return analysis;
+}
+
+std::optional<NewsAnalysis> NewsService::cached_analysis(const QString& url) {
+ auto r = fincept::NewsArticleRepository::instance().load_analysis(url);
+ if (r.is_err() || r.value().isEmpty())
+ return std::nullopt;
+ const QJsonDocument doc = QJsonDocument::fromJson(r.value().toUtf8());
+ if (!doc.isObject())
+ return std::nullopt;
+ return parse_analysis_data(doc.object());
+}
+
void NewsService::analyze_article(const QString& url, AnalysisCallback cb) {
QJsonObject body;
body["url"] = url;
// context = `this` ensures the callback drops if NewsService ever stops
// being a singleton — today it always outlives the request.
- HttpClient::instance().post("/news/analyze", body, [this, cb](Result<QJsonDocument> result) {
+ HttpClient::instance().post("/news/analyze", body, [url, cb](Result<QJsonDocument> result) {
if (result.is_err()) {
LOG_ERROR("NewsService", "Analysis failed: " + QString::fromStdString(result.error()));
cb(false, {});
@@ -391,56 +460,15 @@ void NewsService::analyze_article(const QString& url, AnalysisCallback cb) {
}
auto data = obj["data"].toObject();
- auto a = data["analysis"].toObject();
- auto sent = a["sentiment"].toObject();
- auto mi = a["market_impact"].toObject();
- auto rs = a["risk_signals"].toObject();
-
- NewsAnalysis analysis;
- analysis.sentiment = {sent["score"].toDouble(), sent["intensity"].toDouble(), sent["confidence"].toDouble()};
- analysis.market_impact = {mi["urgency"].toString(), mi["prediction"].toString()};
- analysis.summary = a["summary"].toString();
- analysis.credits_used = data["credits_used"].toInt();
- analysis.credits_remaining = data["credits_remaining"].toInt();
-
- for (const auto& v : a["keywords"].toArray())
- analysis.keywords << v.toString();
- for (const auto& v : a["topics"].toArray())
- analysis.topics << v.toString();
- for (const auto& v : a["key_points"].toArray())
- analysis.key_points << v.toString();
-
- auto reg = rs["regulatory"].toObject();
- auto geo = rs["geopolitical"].toObject();
- auto ops = rs["operational"].toObject();
- auto mkt = rs["market"].toObject();
- analysis.regulatory = {reg["level"].toString(), reg["details"].toString()};
- analysis.geopolitical = {geo["level"].toString(), geo["details"].toString()};
- analysis.operational = {ops["level"].toString(), ops["details"].toString()};
- analysis.market = {mkt["level"].toString(), mkt["details"].toString()};
-
- // Entities — organizations carry ticker/sector/sentiment; locations
- // carry a country code; people are name-only.
- auto ent = a["entities"].toObject();
- for (const auto& v : ent["organizations"].toArray()) {
- auto o = v.toObject();
- analysis.organizations.push_back(
- {o["name"].toString(), o["ticker"].toString(), o["sector"].toString(), o["sentiment"].toDouble()});
- }
- for (const auto& v : ent["people"].toArray()) {
- auto p = v.toObject();
- analysis.people.push_back({p["name"].toString(), {}, {}, 0});
- }
- for (const auto& v : ent["locations"].toArray()) {
- auto l = v.toObject();
- analysis.locations.push_back({l["name"].toString(), l["country_code"].toString(), {}, 0});
- }
-
- // Content fetch metadata — surfaces publisher-block notices.
- auto content = data["content"].toObject();
- analysis.content.headline = content["headline"].toString();
- analysis.content.word_count = content["word_count"].toInt();
- analysis.content.fetch_note = content["fetch_note"].toString();
+ NewsAnalysis analysis = parse_analysis_data(data);
+
+ // Persist the raw `data` object keyed by URL so reopening the article
+ // re-shows this result without re-spending credits. Overwrites any
+ // prior cache (re-running ANALYZE refreshes it).
+ const QString json = QString::fromUtf8(QJsonDocument(data).toJson(QJsonDocument::Compact));
+ auto wr = fincept::NewsArticleRepository::instance().save_analysis(url, json);
+ if (wr.is_err())
+ LOG_WARN("NewsService", "Failed to persist analysis: " + QString::fromStdString(wr.error()));
cb(true, analysis);
}, this);
diff --git a/fincept-qt/src/services/news/NewsService.h b/fincept-qt/src/services/news/NewsService.h
index ca275d5ab..2aa9938a0 100644
--- a/fincept-qt/src/services/news/NewsService.h
+++ b/fincept-qt/src/services/news/NewsService.h
@@ -16,6 +16,7 @@ class QWebSocket;
#include <atomic>
#include <cstdint>
#include <functional>
+#include <optional>
namespace fincept::services {
@@ -164,6 +165,11 @@ class NewsService : public QObject
void fetch_all_news(bool force, ArticlesCallback cb);
void analyze_article(const QString& url, AnalysisCallback cb);
+ /// Load a previously-persisted analysis for an article URL, if one exists.
+ /// Lets the detail panel re-show a prior ANALYZE result on reopen without
+ /// hitting the network. Returns nullopt when nothing is cached.
+ std::optional<NewsAnalysis> cached_analysis(const QString& url);
+
/// Summarize top N headlines via AI. Cached for 10 min per headline signature.
void summarize_headlines(const QVector<NewsArticle>& articles, int count, SummaryCallback cb);
diff --git a/fincept-qt/src/services/portfolio/PortfolioAnalyticsService.cpp b/fincept-qt/src/services/portfolio/PortfolioAnalyticsService.cpp
index 165cf293c..0f7e4a706 100644
--- a/fincept-qt/src/services/portfolio/PortfolioAnalyticsService.cpp
+++ b/fincept-qt/src/services/portfolio/PortfolioAnalyticsService.cpp
@@ -64,9 +64,14 @@ void PortfolioAnalyticsService::run_ffn(const QStringList& symbols,
void PortfolioAnalyticsService::run_script(const QString& script,
const QString& args_json,
AnalyticsCallback cb) {
+ // PythonRunner::run() resolves scripts_dir/<name> verbatim — the name must
+ // include the ".py" extension (as e.g. the yfinance_data.py daemon path
+ // does). These analytics callers pass the bare stem, so normalise here;
+ // otherwise the existence check fails with "Script not found".
+ const QString script_file = script.endsWith(QLatin1String(".py")) ? script : script + ".py";
fincept::python::PythonRunner::instance().run(
- script, {args_json},
- [cb = std::move(cb), script](const fincept::python::PythonResult& result) {
+ script_file, {args_json},
+ [cb = std::move(cb), script = script_file](const fincept::python::PythonResult& result) {
AnalyticsResult out;
if (!result.success || result.output.trimmed().isEmpty()) {
out.error = result.error.isEmpty()
diff --git a/fincept-qt/src/services/portfolio/PortfolioService_Metrics.cpp b/fincept-qt/src/services/portfolio/PortfolioService_Metrics.cpp
index acf4ed614..141f2f734 100644
--- a/fincept-qt/src/services/portfolio/PortfolioService_Metrics.cpp
+++ b/fincept-qt/src/services/portfolio/PortfolioService_Metrics.cpp
@@ -32,6 +32,40 @@
namespace fincept::services {
+namespace {
+
+// ── Risk-model tuning constants ───────────────────────────────────────────────
+// The composite risk score is a 0–100 number built from normalised sub-scores.
+// Each raw input is divided by a "cap" (the level at which that input is
+// considered maximally risky and saturates its sub-score at 1.0), then weighted.
+// Caps are expressed in the same units as the inputs (annualised % for vol,
+// weight % for concentration, % for drawdown, unitless for beta).
+constexpr double kVolCapPct = 40.0; // 40% annualised vol → max vol risk
+constexpr double kConcentrationCapPct = 80.0; // top-3 weight 80% → max concentration risk
+constexpr double kDrawdownCapPct = 50.0; // 50% max drawdown → max drawdown risk
+constexpr double kBetaCap = 2.0; // |beta| 2.0 → max market-sensitivity risk
+
+// Sub-score weights when ALL four signals are available (time-series path).
+// They sum to 100.
+constexpr double kWVolFull = 30.0;
+constexpr double kWConcFull = 25.0;
+constexpr double kWDrawdownFull = 25.0;
+constexpr double kWBetaFull = 20.0;
+
+// Sub-score weights for the DEGRADED path (<3 snapshots): only volatility and
+// concentration are estimable, so the drawdown/beta weight is redistributed
+// evenly across the two available signals (50/50, summing to 100).
+constexpr double kWVolDegraded = 50.0;
+constexpr double kWConcDegraded = 50.0;
+
+// Historical VaR/CVaR confidence level. kVar95Tail is the tail probability
+// (5% → 95% confidence); kVar95Z is the corresponding one-sided normal z-score,
+// used only by the parametric fallback when the historical sample is too small.
+constexpr double kVar95Tail = 0.05;
+constexpr double kVar95Z = 1.645;
+
+} // namespace
+
void PortfolioService::fetch_correlation(const QStringList& symbols) {
if (symbols.size() < 2) {
emit correlation_computed({});
@@ -56,10 +90,16 @@ for sym in symbols:
try:
hist = yf.download(sym, period="30d", interval="1d", progress=False)
if hist is not None and not hist.empty:
- closes = hist["Close"].dropna().tolist()
- if hasattr(closes[0], 'item'):
+ close_obj = hist["Close"]
+ # Newer yfinance returns MultiIndex columns even for a single ticker,
+ # so hist["Close"] can be a DataFrame — collapse it to a Series.
+ if hasattr(close_obj, "columns"):
+ close_obj = close_obj.iloc[:, 0]
+ closes = close_obj.dropna().tolist()
+ if closes and hasattr(closes[0], 'item'):
closes = [v.item() for v in closes]
- data[sym] = closes
+ if closes:
+ data[sym] = closes
except Exception:
pass
@@ -219,27 +259,35 @@ void PortfolioService::fetch_risk_free_rate() {
}
}
- // Fetch from FRED via inline Python (requests library)
+ // Fetch from FRED via inline Python. The API key is read from the
+ // FRED_API_KEY environment variable, which PythonRunner::build_python_env()
+ // injects from SecureStorage (user configures it under Settings → API
+ // Credentials → "FRED (Federal Reserve)"). No key is ever hardcoded here.
+ // If no key is configured we skip the network call and fall back to the
+ // default rate so Sharpe/Sortino still compute (with a logged note).
const QString code = QString(R"python(
-import json, urllib.request
-api_key = "9da0d86e0cf58f4a4023e5e686226d69"
-url = (
- "https://api.stlouisfed.org/fred/series/observations"
- "?series_id=DGS10&api_key=" + api_key +
- "&file_type=json&sort_order=desc&limit=5"
-)
-try:
- with urllib.request.urlopen(url, timeout=10) as r:
- data = json.loads(r.read().decode())
- rate = None
- for obs in data.get("observations", []):
- v = obs.get("value", ".")
- if v != ".":
- rate = float(v) / 100.0 # convert percent to decimal
- break
- print(json.dumps({"rate": rate if rate is not None else 0.04}))
-except Exception as e:
- print(json.dumps({"rate": 0.04, "error": str(e)}))
+import os, json, urllib.request
+api_key = os.environ.get("FRED_API_KEY", "").strip()
+if not api_key:
+ print(json.dumps({"rate": 0.04, "error": "FRED_API_KEY not configured; using default risk-free rate"}))
+else:
+ url = (
+ "https://api.stlouisfed.org/fred/series/observations"
+ "?series_id=DGS10&api_key=" + api_key +
+ "&file_type=json&sort_order=desc&limit=5"
+ )
+ try:
+ with urllib.request.urlopen(url, timeout=10) as r:
+ data = json.loads(r.read().decode())
+ rate = None
+ for obs in data.get("observations", []):
+ v = obs.get("value", ".")
+ if v != ".":
+ rate = float(v) / 100.0 # convert percent to decimal
+ break
+ print(json.dumps({"rate": rate if rate is not None else 0.04}))
+ except Exception as e:
+ print(json.dumps({"rate": 0.04, "error": str(e)}))
)python");
QPointer<PortfolioService> self = this;
@@ -250,8 +298,12 @@ except Exception as e:
if (result.success && !result.output.trimmed().isEmpty()) {
QJsonParseError err;
const auto doc = QJsonDocument::fromJson(result.output.trimmed().toUtf8(), &err);
- if (err.error == QJsonParseError::NoError && doc.object().contains("rate"))
+ if (err.error == QJsonParseError::NoError && doc.object().contains("rate")) {
rate = doc.object()["rate"].toDouble(0.04);
+ const QString note = doc.object().value("error").toString();
+ if (!note.isEmpty())
+ LOG_WARN("PortfolioSvc", "Risk-free rate: " + note + " (using " + QString::number(rate * 100, 'f', 2) + "%)");
+ }
}
// Persist to 24h cache
auto& settings = SettingsRepository::instance();
@@ -282,7 +334,9 @@ void PortfolioService::compute_metrics(const portfolio::PortfolioSummary& summar
for (qsizetype i = 0; i < std::min(qsizetype{3}, weights.size()); ++i)
conc += weights[i];
metrics.concentration_top3 = conc;
- metrics.risk_score = std::min(conc / 80.0, 1.0) * 50.0; // concentration-only baseline
+ // Concentration-only baseline (overwritten below once vol/drawdown/beta are
+ // known). Capped at half-scale since it is a single-signal estimate.
+ metrics.risk_score = std::min(conc / kConcentrationCapPct, 1.0) * kWConcDegraded;
// ── Load snapshots synchronously for time-series metrics ─────────────────
// (this runs on the calling thread — compute_metrics is always called from
@@ -321,9 +375,9 @@ void PortfolioService::compute_metrics(const portfolio::PortfolioSummary& summar
if (daily_vol > 1e-6)
metrics.sharpe = ((mean / 100.0 - rf_daily) / (daily_vol / 100.0)) * std::sqrt(252.0);
if (summary.total_market_value > 0)
- metrics.var_95 = summary.total_market_value * std::abs(mean / 100.0 - 1.645 * daily_vol / 100.0);
- const double vol_score = std::min(ann_vol / 40.0, 1.0) * 50.0;
- const double conc_score = std::min(conc / 80.0, 1.0) * 50.0;
+ metrics.var_95 = summary.total_market_value * std::abs(mean / 100.0 - kVar95Z * daily_vol / 100.0);
+ const double vol_score = std::min(ann_vol / kVolCapPct, 1.0) * kWVolDegraded;
+ const double conc_score = std::min(conc / kConcentrationCapPct, 1.0) * kWConcDegraded;
metrics.risk_score = vol_score + conc_score;
}
emit metrics_computed(metrics);
@@ -367,6 +421,22 @@ void PortfolioService::compute_metrics(const portfolio::PortfolioSummary& summar
if (daily_vol > 1e-6)
metrics.sharpe = ((mean - rf_daily) / daily_vol) * std::sqrt(252.0);
+ // ── Sortino ratio (annualised) ────────────────────────────────────────────
+ // Like Sharpe but penalises only downside deviation below the minimum
+ // acceptable return (MAR = daily risk-free rate). Returns at or above MAR
+ // contribute zero to the downside variance.
+ {
+ double downside_sq = 0.0;
+ for (const double r : port_returns) {
+ const double shortfall = r - rf_daily;
+ if (shortfall < 0.0)
+ downside_sq += shortfall * shortfall;
+ }
+ const double downside_dev = std::sqrt(downside_sq / n); // population over all periods
+ if (downside_dev > 1e-6)
+ metrics.sortino = ((mean - rf_daily) / downside_dev) * std::sqrt(252.0);
+ }
+
// ── Max drawdown ──────────────────────────────────────────────────────────
double peak = snaps.first().total_value;
double max_dd = 0.0;
@@ -430,8 +500,20 @@ void PortfolioService::compute_metrics(const portfolio::PortfolioSummary& summar
cov += (port_aligned[i] - port_mean) * (spy_aligned[i] - spy_mean);
var_spy += (spy_aligned[i] - spy_mean) * (spy_aligned[i] - spy_mean);
}
- if (var_spy > 1e-10)
- metrics.beta = cov / var_spy;
+ if (var_spy > 1e-10) {
+ const double beta_val = cov / var_spy;
+ metrics.beta = beta_val;
+
+ // ── Jensen's alpha (annualised) ──────────────────────────────
+ // alpha = R_p - [R_f + beta * (R_m - R_f)], all annualised.
+ // port_mean / spy_mean are daily % returns over the aligned
+ // window; annualise by *252 and convert % → decimal.
+ const double port_ann = port_mean / 100.0 * 252.0;
+ const double mkt_ann = spy_mean / 100.0 * 252.0;
+ const double alpha_dec =
+ port_ann - (rf_rate_ + beta_val * (mkt_ann - rf_rate_));
+ metrics.alpha = alpha_dec * 100.0; // store as %
+ }
}
}
@@ -440,7 +522,7 @@ void PortfolioService::compute_metrics(const portfolio::PortfolioSummary& summar
if (summary.total_market_value > 0 && !port_returns.isEmpty()) {
QVector<double> sorted_rets = port_returns;
std::sort(sorted_rets.begin(), sorted_rets.end());
- const int tail_count = std::max(1, static_cast<int>(std::floor(sorted_rets.size() * 0.05)));
+ const int tail_count = std::max(1, static_cast<int>(std::floor(sorted_rets.size() * kVar95Tail)));
// VaR: loss at 95th percentile (positive value = amount at risk)
const double var_pct = -sorted_rets[tail_count - 1]; // worst 5th pct return (%)
metrics.var_95 = summary.total_market_value * std::max(var_pct, 0.0) / 100.0;
@@ -453,12 +535,14 @@ void PortfolioService::compute_metrics(const portfolio::PortfolioSummary& summar
}
// ── Composite risk score (0-100) ─────────────────────────────────────────
+ // Full four-signal model: volatility + concentration + drawdown + beta,
+ // weighted 30/25/25/20. See the tuning constants at the top of this file.
{
- const double vol_score = std::min(ann_vol / 40.0, 1.0) * 30.0;
- const double conc_score = std::min(conc / 80.0, 1.0) * 25.0;
- const double dd_score = std::min(std::abs(max_dd) / 50.0, 1.0) * 25.0;
+ const double vol_score = std::min(ann_vol / kVolCapPct, 1.0) * kWVolFull;
+ const double conc_score = std::min(conc / kConcentrationCapPct, 1.0) * kWConcFull;
+ const double dd_score = std::min(std::abs(max_dd) / kDrawdownCapPct, 1.0) * kWDrawdownFull;
const double beta_val = metrics.beta.value_or(1.0);
- const double beta_score = std::min(std::abs(beta_val) / 2.0, 1.0) * 20.0;
+ const double beta_score = std::min(std::abs(beta_val) / kBetaCap, 1.0) * kWBetaFull;
metrics.risk_score = vol_score + conc_score + dd_score + beta_score;
}
diff --git a/fincept-qt/src/storage/repositories/NewsArticleRepository.cpp b/fincept-qt/src/storage/repositories/NewsArticleRepository.cpp
index 5bfb138a2..aa8f4ff00 100644
--- a/fincept-qt/src/storage/repositories/NewsArticleRepository.cpp
+++ b/fincept-qt/src/storage/repositories/NewsArticleRepository.cpp
@@ -295,6 +295,24 @@ Result<QVector<fincept::services::NewsArticle>> NewsArticleRepository::load_save
{}, map_row);
}
+// ── save_analysis / load_analysis ─────────────────────────────────────────────
+
+Result<void> NewsArticleRepository::save_analysis(const QString& url, const QString& analysis_json) const {
+ return exec_write("INSERT OR REPLACE INTO news_analysis (url, analysis_json, created_at) "
+ "VALUES (?, ?, strftime('%s','now'))",
+ {url, analysis_json});
+}
+
+Result<QString> NewsArticleRepository::load_analysis(const QString& url) const {
+ auto r = db().execute("SELECT analysis_json FROM news_analysis WHERE url = ?", {url});
+ if (r.is_err())
+ return Result<QString>::err(r.error());
+ auto& q = r.value();
+ if (!q.next())
+ return Result<QString>::ok({}); // none stored — not an error
+ return Result<QString>::ok(q.value(0).toString());
+}
+
// ── search_fts ────────────────────────────────────────────────────────────────
Result<QVector<fincept::services::NewsArticle>> NewsArticleRepository::search_fts(const QString& query,
diff --git a/fincept-qt/src/storage/repositories/NewsArticleRepository.h b/fincept-qt/src/storage/repositories/NewsArticleRepository.h
index fc29082c2..8be48c6fe 100644
--- a/fincept-qt/src/storage/repositories/NewsArticleRepository.h
+++ b/fincept-qt/src/storage/repositories/NewsArticleRepository.h
@@ -47,6 +47,14 @@ class NewsArticleRepository : public BaseRepository<fincept::services::NewsArtic
/// Ensure the saved column exists (idempotent ALTER TABLE).
void ensure_saved_column();
+ /// Persist the AI analysis JSON for an article URL (INSERT OR REPLACE).
+ /// Overwrites any prior result so a re-run from the UI updates the cache.
+ Result<void> save_analysis(const QString& url, const QString& analysis_json) const;
+
+ /// Load the cached analysis JSON for a URL. Returns an empty string (ok)
+ /// when none is stored.
+ Result<QString> load_analysis(const QString& url) const;
+
/// Full-text search over headline + summary using the FTS5 index.
/// Returns matching articles ordered by FTS rank (best match first).
/// Falls back to LIKE scan if FTS table is unavailable.
diff --git a/fincept-qt/src/storage/sqlite/migrations/MigrationRunner.h b/fincept-qt/src/storage/sqlite/migrations/MigrationRunner.h
index 7160624c5..f0cc769d6 100644
--- a/fincept-qt/src/storage/sqlite/migrations/MigrationRunner.h
+++ b/fincept-qt/src/storage/sqlite/migrations/MigrationRunner.h
@@ -81,5 +81,6 @@ void register_migration_v031();
void register_migration_v032();
void register_migration_v033();
void register_migration_v034();
+void register_migration_v035();
} // namespace fincept
diff --git a/fincept-qt/src/storage/sqlite/migrations/v035_news_analysis.cpp b/fincept-qt/src/storage/sqlite/migrations/v035_news_analysis.cpp
new file mode 100644
index 000000000..7ae6ecd34
--- /dev/null
+++ b/fincept-qt/src/storage/sqlite/migrations/v035_news_analysis.cpp
@@ -0,0 +1,41 @@
+// v035_news_analysis — Persisted AI analysis results for news articles.
+//
+// Caches the /news/analyze response (the raw `data` JSON object) keyed by the
+// article URL so a previously-run analysis re-appears whenever the article is
+// reopened, without re-spending credits. Overwritten only when the user clicks
+// ANALYZE again (INSERT OR REPLACE).
+
+#include "storage/sqlite/migrations/MigrationRunner.h"
+
+#include <QSqlError>
+#include <QSqlQuery>
+
+namespace fincept {
+namespace {
+
+static Result<void> sql(QSqlDatabase& db, const char* stmt) {
+ QSqlQuery q(db);
+ if (!q.exec(stmt))
+ return Result<void>::err(q.lastError().text().toStdString());
+ return Result<void>::ok();
+}
+
+Result<void> apply_v035(QSqlDatabase& db) {
+ return sql(db, "CREATE TABLE IF NOT EXISTS news_analysis ("
+ " url TEXT PRIMARY KEY,"
+ " analysis_json TEXT NOT NULL,"
+ " created_at INTEGER NOT NULL DEFAULT (strftime('%s','now'))"
+ ")");
+}
+
+} // anonymous namespace
+
+void register_migration_v035() {
+ static bool done = false;
+ if (done)
+ return;
+ done = true;
+ MigrationRunner::register_migration({35, "news_analysis", apply_v035});
+}
+
+} // namespace fincept
diff --git a/fincept-qt/src/trading/brokers/aliceblue/AliceBlueBroker.cpp b/fincept-qt/src/trading/brokers/aliceblue/AliceBlueBroker.cpp
index 26516d4e7..87510dd2c 100644
--- a/fincept-qt/src/trading/brokers/aliceblue/AliceBlueBroker.cpp
+++ b/fincept-qt/src/trading/brokers/aliceblue/AliceBlueBroker.cpp
@@ -8,6 +8,7 @@
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
+#include <QTimeZone>
namespace fincept::trading {
@@ -528,9 +529,8 @@ ApiResponse<QVector<BrokerCandle>> AliceBlueBroker::get_history(const BrokerCred
dt.setTime(QTime(23, 59, 59));
else
dt.setTime(QTime(9, 15, 0));
- // IST = UTC+5:30 → subtract 5h30m to get UTC, then ms
- qint64 epoch_ms = dt.toMSecsSinceEpoch() - (5 * 3600 + 30 * 60) * 1000LL;
- return QString::number(epoch_ms);
+ dt.setTimeZone(QTimeZone(19800)); // interpret wall-clock as IST (+5:30)
+ return QString::number(dt.toMSecsSinceEpoch());
};
QString res = ab_resolution(resolution);
@@ -564,7 +564,9 @@ ApiResponse<QVector<BrokerCandle>> AliceBlueBroker::get_history(const BrokerCred
QJsonObject c = item.toObject();
BrokerCandle candle;
// Response fields: time (YYYY-MM-DD HH:MM:SS), open, high, low, close, volume
- candle.timestamp = QDateTime::fromString(c["time"].toString(), "yyyy-MM-dd HH:mm:ss").toMSecsSinceEpoch();
+ QDateTime dt = QDateTime::fromString(c["time"].toString(), "yyyy-MM-dd HH:mm:ss");
+ dt.setTimeZone(QTimeZone(19800)); // interpret wall-clock as IST (+5:30)
+ candle.timestamp = dt.toMSecsSinceEpoch();
candle.open = c["open"].toDouble();
candle.high = c["high"].toDouble();
candle.low = c["low"].toDouble();
diff --git a/fincept-qt/src/trading/brokers/angelone/AngelOneBroker.cpp b/fincept-qt/src/trading/brokers/angelone/AngelOneBroker.cpp
index 658eb3ccc..3431ca8bc 100644
--- a/fincept-qt/src/trading/brokers/angelone/AngelOneBroker.cpp
+++ b/fincept-qt/src/trading/brokers/angelone/AngelOneBroker.cpp
@@ -14,6 +14,7 @@
#include "trading/instruments/InstrumentService.h"
#include <QCryptographicHash>
+#include <QDate>
#include <QDateTime>
#include <QJsonArray>
#include <QJsonDocument>
@@ -21,6 +22,8 @@
#include <QNetworkInterface>
#include <QRegularExpression>
+#include <algorithm>
+
namespace fincept::trading {
static constexpr const char* TAG_AO = "AngelOneBroker";
@@ -933,42 +936,141 @@ ApiResponse<QVector<BrokerCandle>> AngelOneBroker::get_history(const BrokerCrede
if (token.isEmpty())
return {false, std::nullopt, "Symbol token not found for: " + symbol, ts};
- QJsonObject payload{
- {"exchange", exchange}, {"symboltoken", token}, {"interval", map_resolution(resolution)},
- {"fromdate", from_date}, // "YYYY-MM-DD HH:MM"
- {"todate", to_date},
+ // SmartAPI getCandleData requires fromdate/todate as "yyyy-MM-dd HH:mm".
+ // Pass through if already in that form; otherwise append session open/close
+ // to a bare "yyyy-MM-dd" date so Angel does not reject the request.
+ auto normalize_dt = [](const QString& s, const QString& def) -> QString {
+ if (QDateTime::fromString(s, "yyyy-MM-dd HH:mm").isValid())
+ return s;
+ QDate d = QDate::fromString(s, "yyyy-MM-dd");
+ if (d.isValid())
+ return d.toString("yyyy-MM-dd") + " " + def;
+ return s;
};
+ const QString from_norm = normalize_dt(from_date, "09:15");
+ const QString to_norm = normalize_dt(to_date, "15:30");
+
+ const QString interval = map_resolution(resolution);
+
+ // SmartAPI getCandleData caps the number of days per request, by interval.
+ // Larger ranges must be split into consecutive sub-windows and merged.
+ auto cap_days_for_interval = [](const QString& iv) -> int {
+ if (iv == "ONE_MINUTE")
+ return 30;
+ if (iv == "THREE_MINUTE")
+ return 60;
+ if (iv == "FIVE_MINUTE")
+ return 100;
+ if (iv == "TEN_MINUTE")
+ return 100;
+ if (iv == "FIFTEEN_MINUTE")
+ return 200;
+ if (iv == "THIRTY_MINUTE")
+ return 200;
+ if (iv == "ONE_HOUR")
+ return 400;
+ if (iv == "ONE_DAY")
+ return 2000;
+ return 2000;
+ };
+ const int cap_days = cap_days_for_interval(interval);
- auto resp = BrokerHttp::instance().post_json(BASE_AO + "/rest/secure/angelbroking/historical/v1/getCandleData",
- payload, auth_headers(creds));
+ // POST + parse a single date window. `ok` reports request success so the
+ // caller can distinguish a first-window failure (hard error) from a later
+ // failure after data has already been collected (partial result).
+ auto fetch_window = [&](const QString& win_from, const QString& win_to, QVector<BrokerCandle>& out,
+ bool& ok) -> QString {
+ QJsonObject payload{
+ {"exchange", exchange}, {"symboltoken", token}, {"interval", interval},
+ {"fromdate", win_from}, // "YYYY-MM-DD HH:MM"
+ {"todate", win_to},
+ };
- if (!resp.success || !resp.json.value("status").toBool())
- return {false, std::nullopt, checked_error(resp, "get_history failed"), ts};
+ auto resp = BrokerHttp::instance().post_json(BASE_AO + "/rest/secure/angelbroking/historical/v1/getCandleData",
+ payload, auth_headers(creds));
+
+ if (!resp.success || !resp.json.value("status").toBool()) {
+ ok = false;
+ return checked_error(resp, "get_history failed");
+ }
+ ok = true;
+
+ // data is array of [timestamp, open, high, low, close, volume]
+ auto arr = resp.json.value("data").toArray();
+ for (const auto& v : arr) {
+ auto row = v.toArray();
+ if (row.size() < 6)
+ continue;
+ BrokerCandle c;
+ // AngelOne returns ISO 8601: "2025-03-03T00:00:00+05:30"
+ QString dt_str = row[0].toString();
+ QDateTime dt = QDateTime::fromString(dt_str, Qt::ISODate);
+ if (!dt.isValid())
+ dt = QDateTime::fromString(dt_str, "yyyy-MM-dd HH:mm");
+ if (!dt.isValid())
+ dt = QDateTime::fromString(dt_str, "yyyy-MM-ddTHH:mm:ss");
+ c.timestamp = dt.toMSecsSinceEpoch();
+ // AngelOne returns OHLCV as numbers (not strings)
+ c.open = row[1].toDouble();
+ c.high = row[2].toDouble();
+ c.low = row[3].toDouble();
+ c.close = row[4].toDouble();
+ c.volume = row[5].toDouble();
+ out.append(c);
+ }
+ return {};
+ };
+
+ // Parse the normalized bounds back to QDateTime to measure the span. If
+ // either fails to parse, fall back to the original single-request behavior.
+ const QDateTime from_dt = QDateTime::fromString(from_norm, "yyyy-MM-dd HH:mm");
+ const QDateTime to_dt = QDateTime::fromString(to_norm, "yyyy-MM-dd HH:mm");
+ const bool bounds_ok = from_dt.isValid() && to_dt.isValid() && from_dt <= to_dt;
+ const qint64 span_days = bounds_ok ? from_dt.date().daysTo(to_dt.date()) : 0;
- // data is array of [timestamp, open, high, low, close, volume]
QVector<BrokerCandle> candles;
- auto arr = resp.json.value("data").toArray();
- for (const auto& v : arr) {
- auto row = v.toArray();
- if (row.size() < 6)
- continue;
- BrokerCandle c;
- // AngelOne returns ISO 8601: "2025-03-03T00:00:00+05:30"
- QString dt_str = row[0].toString();
- QDateTime dt = QDateTime::fromString(dt_str, Qt::ISODate);
- if (!dt.isValid())
- dt = QDateTime::fromString(dt_str, "yyyy-MM-dd HH:mm");
- if (!dt.isValid())
- dt = QDateTime::fromString(dt_str, "yyyy-MM-ddTHH:mm:ss");
- c.timestamp = dt.toMSecsSinceEpoch();
- // AngelOne returns OHLCV as numbers (not strings)
- c.open = row[1].toDouble();
- c.high = row[2].toDouble();
- c.low = row[3].toDouble();
- c.close = row[4].toDouble();
- c.volume = row[5].toDouble();
- candles.append(c);
+
+ if (!bounds_ok || span_days <= cap_days) {
+ // Single request — identical to the original behavior.
+ bool ok = false;
+ QString err = fetch_window(from_norm, to_norm, candles, ok);
+ if (!ok)
+ return {false, std::nullopt, err, ts};
+ return {true, candles, "", ts};
}
+
+ // Chunked: walk consecutive <=cap-day windows from `from_dt` to `to_dt`.
+ constexpr int MAX_ITERATIONS = 60;
+ QDateTime win_start = from_dt;
+ bool any_data = false;
+ for (int iter = 0; iter < MAX_ITERATIONS && win_start <= to_dt; ++iter) {
+ QDateTime win_end = win_start.addDays(cap_days);
+ if (win_end > to_dt)
+ win_end = to_dt;
+
+ bool ok = false;
+ QString err =
+ fetch_window(win_start.toString("yyyy-MM-dd HH:mm"), win_end.toString("yyyy-MM-dd HH:mm"), candles, ok);
+ if (!ok) {
+ if (!any_data)
+ return {false, std::nullopt, err, ts}; // first-window failure → hard error
+ break; // later failure → return what we have
+ }
+ any_data = true;
+
+ if (win_end >= to_dt)
+ break;
+ // Advance one day past this window's end to avoid re-requesting the boundary day.
+ win_start = win_end.addDays(1);
+ }
+
+ // Sort ascending by timestamp and drop duplicate timestamps from window overlap.
+ std::sort(candles.begin(), candles.end(),
+ [](const BrokerCandle& a, const BrokerCandle& b) { return a.timestamp < b.timestamp; });
+ candles.erase(std::unique(candles.begin(), candles.end(),
+ [](const BrokerCandle& a, const BrokerCandle& b) { return a.timestamp == b.timestamp; }),
+ candles.end());
+
return {true, candles, "", ts};
}
diff --git a/fincept-qt/src/trading/brokers/dhan/DhanBroker.cpp b/fincept-qt/src/trading/brokers/dhan/DhanBroker.cpp
index 7481338f2..c91affae6 100644
--- a/fincept-qt/src/trading/brokers/dhan/DhanBroker.cpp
+++ b/fincept-qt/src/trading/brokers/dhan/DhanBroker.cpp
@@ -20,6 +20,9 @@
#include <QDateTime>
#include <QJsonArray>
+#include <QSet>
+
+#include <algorithm>
namespace fincept::trading {
@@ -460,15 +463,17 @@ ApiResponse<QVector<BrokerCandle>> DhanBroker::get_history(const BrokerCredentia
const QString seg = dhan_exchange(exch);
// Determine daily vs intraday + interval string
- bool is_daily = (resolution == "D" || resolution == "1D" || resolution == "W" || resolution == "M");
+ const QString res_lc = resolution.toLower();
+ const bool is_daily = (res_lc == "d" || res_lc == "1d" || res_lc == "w" || res_lc == "m");
static const QMap<QString, QString> intraday_map = {
{"1", "1"}, {"1m", "1"}, {"5", "5"}, {"5m", "5"}, {"15", "15"}, {"15m", "15"},
{"25", "25"}, {"25m", "25"}, {"60", "60"}, {"60m", "60"}, {"1h", "60"},
};
const QString interval_str = intraday_map.value(resolution, "");
if (!is_daily && interval_str.isEmpty()) {
- // Unsupported resolution — fall back to daily
- is_daily = true;
+ // Unsupported intraday interval (Dhan supports only 1/5/15/25/60) — error out
+ // rather than silently returning daily candles for a different timeframe.
+ return {false, std::nullopt, "Unsupported resolution for Dhan history: " + resolution, ts};
}
QDate from = QDate::fromString(from_date, "yyyy-MM-dd");
@@ -496,18 +501,25 @@ ApiResponse<QVector<BrokerCandle>> DhanBroker::get_history(const BrokerCredentia
// Map exchange segment → Dhan `instrument` enum. EQUITY is correct only
// for cash; F&O / currency / commodity each take a distinct value.
// Spec values: EQUITY, INDEX, FUTIDX, FUTSTK, OPTIDX, OPTSTK,
- // FUTCUR, OPTCUR, FUTCOM, OPTCOM.
+ // FUTCUR, OPTCUR, FUTCOM, OPTFUT.
QString instrument = "EQUITY";
if (seg == "NSE_FNO" || seg == "BSE_FNO") {
- // Heuristic: option contract names end in CE/PE; futures end in FUT.
+ // Index derivatives use OPTIDX/FUTIDX; stock derivatives use OPTSTK/FUTSTK.
+ // Detect index underlyings by their well-known root symbols.
+ static const QSet<QString> kIdxRoots = {"NIFTY", "BANKNIFTY", "FINNIFTY", "MIDCPNIFTY",
+ "NIFTYNXT50", "SENSEX", "BANKEX", "SENSEX50"};
+ const bool isIdx =
+ std::any_of(kIdxRoots.begin(), kIdxRoots.end(),
+ [&](const QString& r) { return name.startsWith(r); });
+ // Heuristic: option contract names end in CE/PE; futures contain FUT.
if (name.endsWith("CE") || name.endsWith("PE"))
- instrument = "OPTSTK";
+ instrument = isIdx ? "OPTIDX" : "OPTSTK";
else if (name.contains("FUT"))
- instrument = "FUTSTK";
+ instrument = isIdx ? "FUTIDX" : "FUTSTK";
} else if (seg == "IDX_I") {
instrument = "INDEX";
} else if (seg == "MCX_COMM") {
- instrument = name.contains("FUT") ? "FUTCOM" : "OPTCOM";
+ instrument = name.contains("FUT") ? "FUTCOM" : "OPTFUT";
} else if (seg == "NSE_CURRENCY" || seg == "BSE_CURRENCY") {
instrument = name.contains("FUT") ? "FUTCUR" : "OPTCUR";
}
diff --git a/fincept-qt/src/trading/brokers/fyers/FyersBroker.cpp b/fincept-qt/src/trading/brokers/fyers/FyersBroker.cpp
index 859bd5ac7..c1b5a3d0a 100644
--- a/fincept-qt/src/trading/brokers/fyers/FyersBroker.cpp
+++ b/fincept-qt/src/trading/brokers/fyers/FyersBroker.cpp
@@ -8,9 +8,12 @@
#include <QCryptographicHash>
#include <QDateTime>
#include <QJsonArray>
+#include <QSet>
#include <QUrl>
#include <QUrlQuery>
+#include <algorithm>
+
namespace fincept::trading {
static int64_t now_ts() {
@@ -425,7 +428,7 @@ ApiResponse<QVector<BrokerCandle>> FyersBroker::get_history(const BrokerCredenti
// Map chart timeframes to Fyers resolution codes
static const QHash<QString, QString> tf_map = {
{"1m", "1"}, {"5m", "5"}, {"15m", "15"}, {"30m", "30"},
- {"1h", "60"}, {"1d", "D"}, {"1w", "W"},
+ {"1h", "60"}, {"1d", "D"}, {"1w", "1W"},
};
const QString fyers_res = tf_map.value(resolution, resolution);
@@ -433,32 +436,102 @@ ApiResponse<QVector<BrokerCandle>> FyersBroker::get_history(const BrokerCredenti
const QString from = from_date.left(10);
const QString to = to_date.left(10);
- QString url = QString("%1/data/history?symbol=%2&resolution=%3&date_format=1&range_from=%4&range_to=%5&cont_flag=1")
- .arg(base_url(), to_fyers_sym(symbol), fyers_res, from, to);
-
- auto resp = BrokerHttp::instance().get(url, auth_headers(creds));
int64_t ts = now_ts();
- if (!resp.success)
- return {false, std::nullopt, fyers_check_auth(resp, resp.error), ts};
- if (resp.json.value("s").toString() != "ok")
- return {false, std::nullopt, fyers_check_auth(resp, resp.json.value("message").toString("Failed")), ts};
+ // Fyers /data/history caps the date window per request. Daily/weekly/monthly
+ // resolutions allow up to 366 days; all intraday minute resolutions allow up
+ // to 100 days. Determine the cap from the resolved Fyers resolution code.
+ static const QSet<QString> daily_res = {"D", "1W", "1M"};
+ const int cap_days = daily_res.contains(fyers_res) ? 366 : 100;
+
+ const QString fyers_sym = to_fyers_sym(symbol);
+
+ // Fetch + parse a single window. Returns true on a successful response and
+ // appends parsed candles to `out`; on failure sets `err` and returns false.
+ auto fetch_window = [&](const QString& range_from, const QString& range_to,
+ QVector<BrokerCandle>& out, QString& err) -> bool {
+ const QString url =
+ QString("%1/data/history?symbol=%2&resolution=%3&date_format=1&range_from=%4&range_to=%5&cont_flag=1")
+ .arg(base_url(), fyers_sym, fyers_res, range_from, range_to);
+
+ auto resp = BrokerHttp::instance().get(url, auth_headers(creds));
+ if (!resp.success) {
+ err = fyers_check_auth(resp, resp.error);
+ return false;
+ }
+ if (resp.json.value("s").toString() != "ok") {
+ err = fyers_check_auth(resp, resp.json.value("message").toString("Failed"));
+ return false;
+ }
+
+ const auto arr = resp.json.value("candles").toArray();
+ for (const auto& v : arr) {
+ auto c = v.toArray();
+ if (c.size() < 6)
+ continue;
+ BrokerCandle candle;
+ // Fyers returns epoch in seconds; internal convention is milliseconds.
+ candle.timestamp = c[0].toVariant().toLongLong() * 1000;
+ candle.open = c[1].toDouble();
+ candle.high = c[2].toDouble();
+ candle.low = c[3].toDouble();
+ candle.close = c[4].toDouble();
+ candle.volume = c[5].toDouble();
+ out.append(candle);
+ }
+ return true;
+ };
+
+ const QDate from_d = QDate::fromString(from, "yyyy-MM-dd");
+ const QDate to_d = QDate::fromString(to, "yyyy-MM-dd");
+
+ // If parsing fails or the range fits in one window, preserve the original
+ // single-request behavior.
+ if (!from_d.isValid() || !to_d.isValid() || from_d.daysTo(to_d) <= cap_days) {
+ QVector<BrokerCandle> candles;
+ QString err;
+ if (!fetch_window(from, to, candles, err))
+ return {false, std::nullopt, err, ts};
+ return {true, candles, "", ts};
+ }
+
+ // Range exceeds the cap: chunk into consecutive <=cap-day sub-windows.
QVector<BrokerCandle> candles;
- auto arr = resp.json.value("candles").toArray();
- for (const auto& v : arr) {
- auto c = v.toArray();
- if (c.size() < 6)
- continue;
- BrokerCandle candle;
- // Fyers returns epoch in seconds; internal convention is milliseconds.
- candle.timestamp = c[0].toVariant().toLongLong() * 1000;
- candle.open = c[1].toDouble();
- candle.high = c[2].toDouble();
- candle.low = c[3].toDouble();
- candle.close = c[4].toDouble();
- candle.volume = c[5].toDouble();
- candles.append(candle);
+ QDate win_from = from_d;
+ constexpr int kMaxIterations = 60;
+ int iterations = 0;
+ bool first_window = true;
+ while (win_from <= to_d && iterations < kMaxIterations) {
+ ++iterations;
+ QDate win_to = win_from.addDays(cap_days);
+ if (win_to > to_d)
+ win_to = to_d;
+
+ QString err;
+ if (!fetch_window(win_from.toString("yyyy-MM-dd"), win_to.toString("yyyy-MM-dd"), candles, err)) {
+ // First-window failure surfaces the error as before. A later failure
+ // after we already collected data returns the partial result.
+ if (first_window)
+ return {false, std::nullopt, err, ts};
+ LOG_WARN("FyersBroker",
+ QString("get_history: window fetch failed after partial data (%1); returning %2 candles")
+ .arg(err).arg(candles.size()));
+ break;
+ }
+ first_window = false;
+ win_from = win_to.addDays(1);
}
+
+ // Sort ascending by timestamp and dedupe consecutive duplicate timestamps
+ // (overlapping window boundaries / repeated bars).
+ std::sort(candles.begin(), candles.end(),
+ [](const BrokerCandle& a, const BrokerCandle& b) { return a.timestamp < b.timestamp; });
+ candles.erase(std::unique(candles.begin(), candles.end(),
+ [](const BrokerCandle& a, const BrokerCandle& b) {
+ return a.timestamp == b.timestamp;
+ }),
+ candles.end());
+
return {true, candles, "", ts};
}
diff --git a/fincept-qt/src/trading/brokers/groww/GrowwBroker.cpp b/fincept-qt/src/trading/brokers/groww/GrowwBroker.cpp
index c9a9cde91..9fa3a2d11 100644
--- a/fincept-qt/src/trading/brokers/groww/GrowwBroker.cpp
+++ b/fincept-qt/src/trading/brokers/groww/GrowwBroker.cpp
@@ -11,6 +11,8 @@
#include <QUrlQuery>
#include <QUuid>
+#include <algorithm>
+
namespace fincept::trading {
static int64_t now_ts() {
@@ -111,12 +113,15 @@ int GrowwBroker::resolution_to_minutes(const QString& resolution) {
return history_interval_minutes(resolution);
}
-// Groww /v1/historical/candles accepts 1, 5, 10, 15, 30, 60 minute bars plus 1440 (1D)
-// and 10080 (1W). Windows are capped per interval: ≤5m → 30d, 10–30m → 90d, ≥1h → 180d.
+// Groww /v1/historical/candles accepts 1, 2, 3, 5, 10, 15, 30, 60 minute bars plus
+// 1440 (1D), 10080 (1W) and 43200 (1M). Windows are capped per interval:
+// ≤5m → 30d, 10–30m → 90d, ≥1h → 180d.
int GrowwBroker::history_interval_minutes(const QString& resolution) {
const QString r = resolution.toLower();
if (r == "1" || r == "1m" || r == "1min")
return 1;
+ if (r == "2" || r == "2m" || r == "2min")
+ return 2;
if (r == "3" || r == "3m" || r == "3min")
return 3;
if (r == "5" || r == "5m" || r == "5min")
@@ -140,23 +145,24 @@ int GrowwBroker::history_interval_minutes(const QString& resolution) {
return 1;
}
-// Groww /v1/historical/candles takes a string token, NOT a number.
-// Token enum per docs: 1min, 3min, 5min, 10min, 15min, 30min, 1hour, 4hours,
-// 1day, 1week, 1month.
+// Groww /v1/historical/candles takes a string candle_interval token, NOT a number.
+// Token enum per docs (full-word SINGULAR): 1minute, 2minute, 3minute, 5minute,
+// 10minute, 15minute, 30minute, 1hour, 4hour, 1day, 1week, 1month.
QString GrowwBroker::history_interval_token(const QString& resolution) {
switch (history_interval_minutes(resolution)) {
- case 1: return "1min";
- case 3: return "3min";
- case 5: return "5min";
- case 10: return "10min";
- case 15: return "15min";
- case 30: return "30min";
+ case 1: return "1minute";
+ case 2: return "2minute";
+ case 3: return "3minute";
+ case 5: return "5minute";
+ case 10: return "10minute";
+ case 15: return "15minute";
+ case 30: return "30minute";
case 60: return "1hour";
- case 240: return "4hours";
+ case 240: return "4hour";
case 1440: return "1day";
case 10080:return "1week";
case 43200:return "1month";
- default: return "1min";
+ default: return "1minute";
}
}
@@ -811,47 +817,121 @@ ApiResponse<QVector<BrokerCandle>> GrowwBroker::get_history(const BrokerCredenti
// groww_symbol is "EXCHANGE-TRADINGSYMBOL" (e.g. "NSE-WIPRO"); for options the
// suffix is the option-contract code (e.g. "NSE-NIFTY-30Sep25-24650-CE"). The
// hyphen prefix is required — passing the bare trading symbol returns 4xx.
- QString start_time = from_date + " 09:15:00";
- QString end_time = to_date + " 15:30:00";
const QString groww_symbol = ex + "-" + trading_symbol;
- QUrl qurl(QStringLiteral("https://api.groww.in/v1/historical/candles"));
- QUrlQuery qq;
- qq.addQueryItem(QStringLiteral("exchange"), ex);
- qq.addQueryItem(QStringLiteral("segment"), segment);
- qq.addQueryItem(QStringLiteral("groww_symbol"), groww_symbol);
- qq.addQueryItem(QStringLiteral("start_time"), start_time);
- qq.addQueryItem(QStringLiteral("end_time"), end_time);
- qq.addQueryItem(QStringLiteral("candle_interval"), interval_token);
- qurl.setQuery(qq);
- const QString url = qurl.toString();
+ // Groww caps the queryable window per request by interval. Resolve the cap (in
+ // days) from the interval-minutes bucket so large ranges can be chunked into
+ // consecutive sub-windows and stitched back together.
+ // ≤5m → 30d, 10/15/30m → 90d, ≥1h (incl. day/week/month) → 180d.
+ const int interval_minutes = history_interval_minutes(resolution);
+ int window_cap_days;
+ if (interval_minutes <= 5)
+ window_cap_days = 30;
+ else if (interval_minutes <= 30)
+ window_cap_days = 90;
+ else
+ window_cap_days = 180;
+
+ // One-window fetch + parse. Returns the BrokerHttpResponse so the caller can
+ // distinguish a hard failure (network / token) from a successful-but-empty
+ // window, and appends parsed candles into `out`. Only start_time/end_time vary
+ // between windows; the 09:15:00 / 15:30:00 session suffixes are preserved.
+ auto fetch_window = [&](const QString& win_from, const QString& win_to,
+ QVector<BrokerCandle>& out) -> BrokerHttpResponse {
+ const QString start_time = win_from + " 09:15:00";
+ const QString end_time = win_to + " 15:30:00";
+
+ QUrl qurl(QStringLiteral("https://api.groww.in/v1/historical/candles"));
+ QUrlQuery qq;
+ qq.addQueryItem(QStringLiteral("exchange"), ex);
+ qq.addQueryItem(QStringLiteral("segment"), segment);
+ qq.addQueryItem(QStringLiteral("groww_symbol"), groww_symbol);
+ qq.addQueryItem(QStringLiteral("start_time"), start_time);
+ qq.addQueryItem(QStringLiteral("end_time"), end_time);
+ qq.addQueryItem(QStringLiteral("candle_interval"), interval_token);
+ qurl.setQuery(qq);
+
+ auto resp = BrokerHttp::instance().get(qurl.toString(), hdrs);
+ if (!resp.success || is_token_expired(resp))
+ return resp;
+
+ const QJsonArray candles = resp.json["payload"].toObject()["candles"].toArray();
+ out.reserve(out.size() + candles.size());
+ for (const auto& item : candles) {
+ QJsonArray c = item.toArray();
+ if (c.size() < 6)
+ continue;
+ BrokerCandle candle;
+ // [timestamp_ms, open, high, low, close, volume]
+ candle.timestamp = static_cast<int64_t>(c[0].toDouble());
+ candle.open = c[1].toDouble();
+ candle.high = c[2].toDouble();
+ candle.low = c[3].toDouble();
+ candle.close = c[4].toDouble();
+ candle.volume = c[5].toDouble();
+ out.append(candle);
+ }
+ return resp;
+ };
- auto resp = BrokerHttp::instance().get(url, hdrs);
+ const QDate from = QDate::fromString(from_date, QStringLiteral("yyyy-MM-dd"));
+ const QDate to = QDate::fromString(to_date, QStringLiteral("yyyy-MM-dd"));
- if (!resp.success)
- return {false, std::nullopt, checked_error(resp, "Network error"), ts};
- if (is_token_expired(resp))
- return {false, std::nullopt, "[TOKEN_EXPIRED]", ts};
-
- QJsonArray candles = resp.json["payload"].toObject()["candles"].toArray();
QVector<BrokerCandle> result;
- result.reserve(candles.size());
-
- for (const auto& item : candles) {
- QJsonArray c = item.toArray();
- if (c.size() < 6)
- continue;
- BrokerCandle candle;
- // [timestamp_ms, open, high, low, close, volume]
- candle.timestamp = static_cast<int64_t>(c[0].toDouble());
- candle.open = c[1].toDouble();
- candle.high = c[2].toDouble();
- candle.low = c[3].toDouble();
- candle.close = c[4].toDouble();
- candle.volume = c[5].toDouble();
- result.append(candle);
+
+ // Single-request path: when the dates don't parse (defensive) or the span fits
+ // within the cap, issue exactly one GET — behaviour is unchanged from before.
+ if (!from.isValid() || !to.isValid() || from.daysTo(to) <= window_cap_days) {
+ auto resp = fetch_window(from_date, to_date, result);
+ if (!resp.success)
+ return {false, std::nullopt, checked_error(resp, "Network error"), ts};
+ if (is_token_expired(resp))
+ return {false, std::nullopt, "[TOKEN_EXPIRED]", ts};
+ return {true, result, "", ts};
+ }
+
+ // Chunked path: walk consecutive ≤cap-day sub-windows. The first window's
+ // failure is fatal (mirrors the single-request behaviour); a later failure once
+ // we already have data breaks the loop and returns the partial series.
+ constexpr int kMaxIterations = 60;
+ QDate win_start = from;
+ int iterations = 0;
+ bool first = true;
+ while (win_start <= to && iterations < kMaxIterations) {
+ QDate win_end = win_start.addDays(window_cap_days);
+ if (win_end > to)
+ win_end = to;
+
+ auto resp = fetch_window(win_start.toString(QStringLiteral("yyyy-MM-dd")),
+ win_end.toString(QStringLiteral("yyyy-MM-dd")), result);
+ const bool failed = !resp.success || is_token_expired(resp);
+ if (failed) {
+ if (first) {
+ if (is_token_expired(resp))
+ return {false, std::nullopt, "[TOKEN_EXPIRED]", ts};
+ return {false, std::nullopt, checked_error(resp, "Network error"), ts};
+ }
+ break; // partial data already collected — return what we have
+ }
+
+ first = false;
+ // Advance past this window. Sub-windows are half-open against the day-cap so
+ // adjacent windows don't re-request the same boundary day (dedupe still guards
+ // overlap from the inclusive session suffixes).
+ win_start = win_end.addDays(1);
+ ++iterations;
}
+ // Sort ascending by timestamp and drop duplicate bars (windows can overlap at
+ // their boundaries / on retried days).
+ std::sort(result.begin(), result.end(),
+ [](const BrokerCandle& a, const BrokerCandle& b) { return a.timestamp < b.timestamp; });
+ result.erase(std::unique(result.begin(), result.end(),
+ [](const BrokerCandle& a, const BrokerCandle& b) {
+ return a.timestamp == b.timestamp;
+ }),
+ result.end());
+
return {true, result, "", ts};
}
diff --git a/fincept-qt/src/trading/brokers/ibkr/IBKRBroker.cpp b/fincept-qt/src/trading/brokers/ibkr/IBKRBroker.cpp
index 72fe25673..b899ca99f 100644
--- a/fincept-qt/src/trading/brokers/ibkr/IBKRBroker.cpp
+++ b/fincept-qt/src/trading/brokers/ibkr/IBKRBroker.cpp
@@ -5,6 +5,10 @@
#include <QDateTime>
#include <QJsonArray>
#include <QJsonDocument>
+#include <QTimeZone>
+
+#include <algorithm>
+#include <limits>
namespace fincept::trading {
@@ -88,7 +92,18 @@ IBKRBroker::HistoryParams IBKRBroker::ibkr_history_params(const QString& resolut
period = "5y";
}
- return {bar, period};
+ // Derive startTime from to_date. IBKR anchors the END of the returned window
+ // at startTime (UTC, "YYYYMMDD-HH:mm:ss") and extends backward over `period`.
+ // Use end-of-day UTC so the requested to_date is fully included.
+ // If to_date is empty/invalid, leave start_time empty to preserve the
+ // default "most recent" behavior.
+ QString start_time;
+ if (to.isValid()) {
+ QDateTime end_of_day(to, QTime(23, 59, 59), QTimeZone::UTC);
+ start_time = end_of_day.toString("yyyyMMdd-HH:mm:ss");
+ }
+
+ return {bar, period, start_time};
}
bool IBKRBroker::is_token_expired(const BrokerHttpResponse& resp) {
@@ -555,39 +570,116 @@ ApiResponse<QVector<BrokerCandle>> IBKRBroker::get_history(const BrokerCredentia
auto params = ibkr_history_params(resolution, from_date, to_date);
- QString url = gw + "/v1/api/iserver/marketdata/history?conid=" + conid + "&period=" + params.period +
- "&bar=" + params.bar + "&outsideRth=false";
-
auto& http = BrokerHttp::instance();
- auto resp = http.get(url, auth_headers(creds));
-
- if (!resp.success)
- return {false, std::nullopt, checked_error(resp, "get_history failed"), ts};
-
- QJsonDocument doc = QJsonDocument::fromJson(resp.raw_body.toUtf8());
- if (!doc.isObject())
- return {false, std::nullopt, "get_history: invalid response", ts};
-
- QJsonObject obj = doc.object();
- if (!obj.value("error").toString().isEmpty())
- return {false, std::nullopt, obj.value("error").toString(), ts};
-
- // Response: {data: [{t, o, h, l, c, v}, ...]}
- QJsonArray arr = obj.value("data").toArray();
- QVector<BrokerCandle> candles;
- candles.reserve(arr.size());
-
- for (const QJsonValue& v : arr) {
- QJsonObject o = v.toObject();
- BrokerCandle c;
- // t is epoch milliseconds
- c.timestamp = static_cast<int64_t>(o.value("t").toDouble());
- c.open = o.value("o").toDouble();
- c.high = o.value("h").toDouble();
- c.low = o.value("l").toDouble();
- c.close = o.value("c").toDouble();
- c.volume = static_cast<int64_t>(o.value("v").toDouble());
- candles.append(c);
+ auto headers = auth_headers(creds);
+
+ // Single fetch+parse for one window ending at `start_time` (UTC "YYYYMMDD-HH:mm:ss").
+ // An empty `start_time` preserves the gateway's default "most recent" window.
+ // `ok` distinguishes a transport/gateway failure from a successful empty page.
+ struct PageResult {
+ bool ok = false;
+ QString error;
+ QVector<BrokerCandle> candles;
+ };
+ auto fetch_page = [&](const QString& start_time) -> PageResult {
+ QString url = gw + "/v1/api/iserver/marketdata/history?conid=" + conid + "&period=" + params.period +
+ "&bar=" + params.bar + "&outsideRth=false";
+
+ // Anchor the END of the returned window at this page's start_time when provided.
+ if (!start_time.isEmpty())
+ url += "&startTime=" + start_time;
+
+ auto resp = http.get(url, headers);
+ if (!resp.success)
+ return {false, checked_error(resp, "get_history failed"), {}};
+
+ QJsonDocument doc = QJsonDocument::fromJson(resp.raw_body.toUtf8());
+ if (!doc.isObject())
+ return {false, "get_history: invalid response", {}};
+
+ QJsonObject obj = doc.object();
+ if (!obj.value("error").toString().isEmpty())
+ return {false, obj.value("error").toString(), {}};
+
+ // Response: {data: [{t, o, h, l, c, v}, ...]}
+ QJsonArray arr = obj.value("data").toArray();
+ QVector<BrokerCandle> page;
+ page.reserve(arr.size());
+ for (const QJsonValue& v : arr) {
+ QJsonObject o = v.toObject();
+ BrokerCandle c;
+ // t is epoch milliseconds
+ c.timestamp = static_cast<int64_t>(o.value("t").toDouble());
+ c.open = o.value("o").toDouble();
+ c.high = o.value("h").toDouble();
+ c.low = o.value("l").toDouble();
+ c.close = o.value("c").toDouble();
+ c.volume = static_cast<int64_t>(o.value("v").toDouble());
+ page.append(c);
+ }
+ return {true, "", page};
+ };
+
+ // ---- First request: ends at startTime=to_date, covers `period` (unchanged single-call). ----
+ PageResult first = fetch_page(params.start_time);
+ if (!first.ok)
+ return {false, std::nullopt, first.error, ts};
+
+ QVector<BrokerCandle> candles = first.candles;
+
+ // ---- Backward paging for long fine-grained ranges ----
+ // IBKR returns at most ~1000 bars per request. If the first page is full AND its
+ // oldest bar is still newer than from_date, walk backward by re-anchoring startTime
+ // just before the oldest bar until we reach from_date / run out of data / hit the cap.
+ // Only engage when both dates are valid (otherwise keep single-request behavior).
+ constexpr int kFullPage = 1000; // page size at/above which more data may exist
+ constexpr int kMaxIterations = 30; // safety cap on extra requests
+
+ QDate from_d = QDate::fromString(from_date, "yyyy-MM-dd");
+ QDate to_d = QDate::fromString(to_date, "yyyy-MM-dd");
+ if (from_d.isValid() && to_d.isValid()) {
+ // from_date lower bound in epoch milliseconds (start-of-day UTC) to match bar timestamps.
+ const int64_t from_ms =
+ QDateTime(from_d, QTime(0, 0, 0), QTimeZone::UTC).toMSecsSinceEpoch();
+
+ auto oldest_ms = [](const QVector<BrokerCandle>& page) -> int64_t {
+ int64_t m = std::numeric_limits<int64_t>::max();
+ for (const auto& c : page)
+ m = std::min(m, c.timestamp);
+ return m;
+ };
+
+ QVector<BrokerCandle> page = first.candles;
+ int iterations = 0;
+ while (!page.isEmpty() && page.size() >= kFullPage && iterations < kMaxIterations) {
+ int64_t oldest = oldest_ms(page);
+ if (oldest <= from_ms)
+ break; // reached the requested start of the range
+
+ // Re-anchor: end the next window one second before the oldest bar (UTC).
+ QDateTime anchor = QDateTime::fromMSecsSinceEpoch(oldest, QTimeZone::UTC).addSecs(-1);
+ PageResult next = fetch_page(anchor.toString("yyyyMMdd-HH:mm:ss"));
+ if (!next.ok)
+ break; // later-page failure: stop and keep what we collected
+ if (next.candles.isEmpty())
+ break; // no more data
+
+ candles += next.candles;
+ page = next.candles;
+ ++iterations;
+ }
+
+ // Merge: sort ascending, dedupe identical timestamps, drop bars older than from_date.
+ std::sort(candles.begin(), candles.end(),
+ [](const BrokerCandle& a, const BrokerCandle& b) { return a.timestamp < b.timestamp; });
+ candles.erase(std::unique(candles.begin(), candles.end(),
+ [](const BrokerCandle& a, const BrokerCandle& b) {
+ return a.timestamp == b.timestamp;
+ }),
+ candles.end());
+ candles.erase(std::remove_if(candles.begin(), candles.end(),
+ [from_ms](const BrokerCandle& c) { return c.timestamp < from_ms; }),
+ candles.end());
}
return {true, candles, "", ts};
diff --git a/fincept-qt/src/trading/brokers/ibkr/IBKRBroker.h b/fincept-qt/src/trading/brokers/ibkr/IBKRBroker.h
index 2aaa50a8b..86324a9db 100644
--- a/fincept-qt/src/trading/brokers/ibkr/IBKRBroker.h
+++ b/fincept-qt/src/trading/brokers/ibkr/IBKRBroker.h
@@ -82,6 +82,7 @@ class IBKRBroker : public IBroker {
struct HistoryParams {
QString bar;
QString period;
+ QString start_time; // IBKR startTime "YYYYMMDD-HH:mm:ss" (UTC); empty = most-recent window
};
static HistoryParams ibkr_history_params(const QString& resolution, const QString& from_date,
const QString& to_date);
diff --git a/fincept-qt/src/trading/brokers/metaapi/MetaApiBroker.cpp b/fincept-qt/src/trading/brokers/metaapi/MetaApiBroker.cpp
index 7831e0a0b..2cb01d46a 100644
--- a/fincept-qt/src/trading/brokers/metaapi/MetaApiBroker.cpp
+++ b/fincept-qt/src/trading/brokers/metaapi/MetaApiBroker.cpp
@@ -3,11 +3,14 @@
#include "trading/brokers/BrokerHttp.h"
+#include <QDate>
#include <QDateTime>
#include <QJsonArray>
#include <QJsonDocument>
#include <QThread>
+#include <QTimeZone>
+#include <algorithm>
#include <cmath>
namespace fincept::trading {
@@ -653,52 +656,179 @@ ApiResponse<QVector<BrokerCandle>> MetaApiBroker::get_history(const BrokerCreden
const QString region = region_from_creds(creds);
const QString tf = map_timeframe(resolution);
- QString start_time;
- if (!from_date.isEmpty()) {
- const auto dt = QDateTime::fromString(from_date, Qt::ISODate);
- if (dt.isValid())
- start_time = dt.toUTC().toString(Qt::ISODate);
- }
- if (start_time.isEmpty())
- start_time = QDateTime::currentDateTimeUtc().addDays(-30).toUTC().toString(Qt::ISODate);
-
- QString url = market_data_url(region) +
- QStringLiteral(
- "/users/current/accounts/%1/historical-market-data/symbols/%2/timeframes/%3/candles"
- "?startTime=%4&limit=500")
- .arg(account_id, symbol, tf, start_time);
-
- if (!to_date.isEmpty()) {
- const auto dt = QDateTime::fromString(to_date, Qt::ISODate);
- if (dt.isValid())
- url += QStringLiteral("&endTime=%1").arg(dt.toUTC().toString(Qt::ISODate));
- }
+ // Parse a flexible date string (ISO / yyyy-MM-dd / yyyy-MM-dd HH:mm) into a
+ // UTC QDateTime. Returns an invalid QDateTime if none of the forms match.
+ const auto parse_dt = [](const QString& s) -> QDateTime {
+ if (s.isEmpty())
+ return {};
+ QDateTime dt = QDateTime::fromString(s, Qt::ISODate);
+ if (!dt.isValid()) {
+ const QDate d = QDate::fromString(s, "yyyy-MM-dd");
+ if (d.isValid()) dt = QDateTime(d, QTime(0, 0), QTimeZone::utc());
+ }
+ if (!dt.isValid()) {
+ dt = QDateTime::fromString(s, "yyyy-MM-dd HH:mm");
+ if (dt.isValid()) dt.setTimeZone(QTimeZone::utc());
+ }
+ return dt.isValid() ? dt.toUTC() : QDateTime();
+ };
- auto resp = BrokerHttp::instance().get(url, auth_headers(creds));
- if (!resp.success)
- return {false, std::nullopt, checked_error(resp, "Failed to fetch candles.")};
+ // MetaAPI loads candles BACKWARD from startTime (the latest time of the
+ // window). There is no endTime parameter, so map to_date -> startTime.
+ QDateTime start_dt = parse_dt(to_date);
+ if (!start_dt.isValid())
+ start_dt = QDateTime::currentDateTimeUtc();
- const auto doc = QJsonDocument::fromJson(resp.raw_body.toUtf8());
- const auto arr = doc.array();
+ // Lower bound for the requested window. When invalid we keep today's
+ // single-request behaviour (no paging, no lower-bound trimming).
+ const QDateTime from_dt = parse_dt(from_date);
+ const bool from_valid = from_dt.isValid();
+
+ constexpr int kLimit = 1000;
+ // One backward fetch ending at `startTime` (the latest time of the page).
+ // Appends parsed candles to `out`, sets `page_count` to the number of
+ // candles returned, and records the millisecond-precision time of the
+ // OLDEST candle in the page. Returns false only on HTTP failure.
QVector<BrokerCandle> candles;
- candles.reserve(arr.size());
+ const auto fetch_page = [&](const QDateTime& startTime,
+ int& page_count,
+ QDateTime& oldest_dt) -> bool {
+ const QString start_str = startTime.toUTC().toString(Qt::ISODateWithMs);
+ const QString url =
+ market_data_url(region) +
+ QStringLiteral(
+ "/users/current/accounts/%1/historical-market-data/symbols/%2/timeframes/%3/candles"
+ "?startTime=%4&limit=%5")
+ .arg(account_id, symbol, tf, start_str)
+ .arg(kLimit);
- for (const auto& item : arr) {
- const auto c = item.toObject();
- BrokerCandle candle;
+ auto resp = BrokerHttp::instance().get(url, auth_headers(creds));
+ if (!resp.success) {
+ page_count = 0;
+ oldest_dt = QDateTime();
+ return false;
+ }
+
+ const auto doc = QJsonDocument::fromJson(resp.raw_body.toUtf8());
+ const auto arr = doc.array();
+ page_count = arr.size();
+ oldest_dt = QDateTime();
+
+ for (const auto& item : arr) {
+ const auto c = item.toObject();
+ BrokerCandle candle;
+
+ const QString time_str = c.value("time").toString();
+ const auto dt = QDateTime::fromString(time_str, Qt::ISODate);
+ candle.timestamp = dt.isValid() ? dt.toSecsSinceEpoch() : 0;
+
+ candle.open = c.value("open").toDouble();
+ candle.high = c.value("high").toDouble();
+ candle.low = c.value("low").toDouble();
+ candle.close = c.value("close").toDouble();
+ candle.volume = c.value("tickVolume").toDouble(c.value("volume").toDouble());
+
+ // Track oldest candle (ms precision) to seed the next page's
+ // startTime. The API may return the page in any order, so compare.
+ if (dt.isValid() && (!oldest_dt.isValid() || dt < oldest_dt))
+ oldest_dt = dt.toUTC();
- const QString time_str = c.value("time").toString();
- const auto dt = QDateTime::fromString(time_str, Qt::ISODate);
- candle.timestamp = dt.isValid() ? dt.toSecsSinceEpoch() : 0;
+ candles.append(candle);
+ }
+ return true;
+ };
+
+ // --- Page 1 (preserves single-request behaviour for short ranges) -------
+ int page_count = 0;
+ QDateTime oldest;
+ {
+ const QString start_str = start_dt.toUTC().toString(Qt::ISODateWithMs);
+ const QString url =
+ market_data_url(region) +
+ QStringLiteral(
+ "/users/current/accounts/%1/historical-market-data/symbols/%2/timeframes/%3/candles"
+ "?startTime=%4&limit=%5")
+ .arg(account_id, symbol, tf, start_str)
+ .arg(kLimit);
+
+ auto resp = BrokerHttp::instance().get(url, auth_headers(creds));
+ if (!resp.success)
+ return {false, std::nullopt, checked_error(resp, "Failed to fetch candles.")};
+
+ const auto doc = QJsonDocument::fromJson(resp.raw_body.toUtf8());
+ const auto arr = doc.array();
+ page_count = arr.size();
- candle.open = c.value("open").toDouble();
- candle.high = c.value("high").toDouble();
- candle.low = c.value("low").toDouble();
- candle.close = c.value("close").toDouble();
- candle.volume = c.value("tickVolume").toDouble(c.value("volume").toDouble());
+ for (const auto& item : arr) {
+ const auto c = item.toObject();
+ BrokerCandle candle;
+
+ const QString time_str = c.value("time").toString();
+ const auto dt = QDateTime::fromString(time_str, Qt::ISODate);
+ candle.timestamp = dt.isValid() ? dt.toSecsSinceEpoch() : 0;
+
+ candle.open = c.value("open").toDouble();
+ candle.high = c.value("high").toDouble();
+ candle.low = c.value("low").toDouble();
+ candle.close = c.value("close").toDouble();
+ candle.volume = c.value("tickVolume").toDouble(c.value("volume").toDouble());
+
+ if (dt.isValid() && (!oldest.isValid() || dt < oldest))
+ oldest = dt.toUTC();
+
+ candles.append(candle);
+ }
+ }
+
+ // --- Backward paging ----------------------------------------------------
+ // Only page when from_date is valid (otherwise: single request as before),
+ // the page was full (== limit), and the oldest candle is still newer than
+ // the requested lower bound.
+ if (from_valid) {
+ constexpr int kMaxIterations = 30;
+ int iterations = 0;
+ while (page_count == kLimit && oldest.isValid() && oldest > from_dt &&
+ iterations < kMaxIterations) {
+ ++iterations;
+
+ // Next page ends 1 ms before the oldest candle we already have.
+ const QDateTime next_start = oldest.addMSecs(-1);
+ const QDateTime prev_oldest = oldest;
+
+ int next_count = 0;
+ QDateTime page_oldest;
+ if (!fetch_page(next_start, next_count, page_oldest))
+ break; // later-page failure: stop, return what we collected
+
+ page_count = next_count;
+ if (next_count == 0 || !page_oldest.isValid())
+ break; // empty / unparseable page: stop
+ if (!(page_oldest < prev_oldest))
+ break; // no backward progress: avoid infinite loop
+ oldest = page_oldest;
+ }
+ }
- candles.append(candle);
+ // --- Merge: sort ascending by time, dedupe identical timestamps ---------
+ std::sort(candles.begin(), candles.end(),
+ [](const BrokerCandle& a, const BrokerCandle& b) {
+ return a.timestamp < b.timestamp;
+ });
+ candles.erase(std::unique(candles.begin(), candles.end(),
+ [](const BrokerCandle& a, const BrokerCandle& b) {
+ return a.timestamp == b.timestamp;
+ }),
+ candles.end());
+
+ // Drop candles older than the requested lower bound (only when valid).
+ if (from_valid) {
+ const int64_t from_ts = from_dt.toSecsSinceEpoch();
+ candles.erase(std::remove_if(candles.begin(), candles.end(),
+ [from_ts](const BrokerCandle& c) {
+ return c.timestamp < from_ts;
+ }),
+ candles.end());
}
return {true, candles, ""};
diff --git a/fincept-qt/src/trading/brokers/saxo/SaxoBankBroker.cpp b/fincept-qt/src/trading/brokers/saxo/SaxoBankBroker.cpp
index b0583e712..9bcb89125 100644
--- a/fincept-qt/src/trading/brokers/saxo/SaxoBankBroker.cpp
+++ b/fincept-qt/src/trading/brokers/saxo/SaxoBankBroker.cpp
@@ -8,6 +8,9 @@
#include <QJsonDocument>
#include <QUrlQuery>
+#include <algorithm>
+#include <limits>
+
namespace fincept::trading {
// Live and SIM token endpoints
@@ -30,6 +33,14 @@ QString SaxoBankBroker::extract_uic(const QString& symbol) {
return {};
}
+QString SaxoBankBroker::extract_asset_type(const QString& symbol) {
+ // Optional 4th part: "FXCM:EURUSD:21:FxSpot" — AssetType is 4th part; default "Stock".
+ QStringList parts = symbol.split(":");
+ if (parts.size() >= 4 && !parts[3].trimmed().isEmpty())
+ return parts[3].trimmed();
+ return QStringLiteral("Stock");
+}
+
const BrokerEnumMap<QString>& SaxoBankBroker::saxo_enum_map() {
static const auto m = [] {
BrokerEnumMap<QString> x;
@@ -181,7 +192,9 @@ OrderPlaceResponse SaxoBankBroker::place_order(const BrokerCredentials& creds, c
QJsonObject order_obj;
order_obj["AccountKey"] = account_key;
order_obj["Uic"] = uic_str.toLongLong();
- order_obj["AssetType"] = "Stock";
+ // AssetType from the optional 4th symbol segment (EXCH:SYM:UIC:ASSETTYPE);
+ // defaults to "Stock" so existing 3-part equity symbols are unchanged.
+ order_obj["AssetType"] = extract_asset_type(order.symbol);
order_obj["BuySell"] = (order.side == OrderSide::Buy) ? "Buy" : "Sell";
order_obj["Amount"] = order.quantity;
order_obj["OrderType"] = saxo_enum_map().order_type_or(order.order_type, "Market");
@@ -223,7 +236,9 @@ ApiResponse<QJsonObject> SaxoBankBroker::modify_order(const BrokerCredentials& c
QJsonObject body;
body["AccountKey"] = creds.user_id;
body["OrderId"] = order_id;
- body["AssetType"] = "Stock";
+ // modify_order has no symbol; let the caller pass "assetType" in mods for
+ // FX/CFD/futures, defaulting to "Stock" to preserve existing equity behavior.
+ body["AssetType"] = mods.value("assetType").toString("Stock");
body["ManualOrder"] = false;
if (mods.contains("quantity"))
@@ -486,9 +501,12 @@ ApiResponse<QVector<BrokerQuote>> SaxoBankBroker::get_quotes(const BrokerCredent
if (uics.isEmpty())
return {false, std::nullopt, "get_quotes: Uic required (symbol format EXCHANGE:SYMBOL:UIC)", ts};
- QString url = QString("%1/trade/v1/infoprices/list?Uics=%2&AssetType=Stock"
+ // infoprices/list takes a single AssetType per call — derive it from the first
+ // symbol's optional 4th segment (EXCH:SYM:UIC:ASSETTYPE); defaults to "Stock".
+ const QString asset_type = extract_asset_type(symbols.first());
+ QString url = QString("%1/trade/v1/infoprices/list?Uics=%2&AssetType=%3"
"&FieldGroups=DisplayAndFormat,PriceInfo,PriceInfoDetails,Quote")
- .arg(BASE_LIVE, uics.join(","));
+ .arg(BASE_LIVE, uics.join(","), asset_type);
auto& http = BrokerHttp::instance();
auto resp = http.get(url, auth_headers(creds));
@@ -541,7 +559,10 @@ ApiResponse<QVector<BrokerCandle>> SaxoBankBroker::get_history(const BrokerCrede
QString uic = extract_uic(symbol);
if (uic.isEmpty())
- return {false, std::nullopt, "get_history: Uic required (symbol format EXCHANGE:SYMBOL:UIC)", ts};
+ return {false, std::nullopt, "get_history: Uic required (symbol format EXCHANGE:SYMBOL:UIC[:ASSETTYPE])", ts};
+
+ // Optional 4th symbol segment carries AssetType (e.g. FxSpot/CfdOnFutures); defaults to "Stock".
+ QString asset_type = extract_asset_type(symbol);
int horizon = saxo_horizon(resolution);
@@ -555,62 +576,148 @@ ApiResponse<QVector<BrokerCandle>> SaxoBankBroker::get_history(const BrokerCrede
to = QDate::currentDate();
int days = from.daysTo(to);
- int count = 500; // default
+ int desired_count = 500; // default
if (horizon >= 1440) {
- count = days + 1;
+ desired_count = days + 1;
} else if (horizon >= 60) {
- count = qMin(days * 8, 500); // ~8 bars/day for 1h
+ desired_count = qMin(days * 8, 500); // ~8 bars/day for 1h
} else {
- count = qMin(days * (390 / qMax(horizon, 1)), 1200);
+ desired_count = days * (390 / qMax(horizon, 1));
}
- count = qMax(1, qMin(count, 1200)); // Saxo max is typically 1200
+ desired_count = qMax(1, desired_count);
- QString time_str = QDateTime(to, QTime(23, 59, 59), QTimeZone::UTC).toString(Qt::ISODate);
+ // Saxo returns at most SAXO_MAX_COUNT samples per request (Mode=UpTo ends at Time, going
+ // backward). The first-page count preserves the historical single-request behavior.
+ static constexpr int SAXO_MAX_COUNT = 1200;
+ static constexpr int SAXO_MAX_PAGES = 30; // safety cap on backward paging iterations
- QString url = QString("%1/chart/v1/charts?Uic=%2&AssetType=Stock&Horizon=%3&Count=%4"
- "&Mode=UpTo&Time=%5")
- .arg(BASE_LIVE, uic, QString::number(horizon), QString::number(count),
- QString(QUrl::toPercentEncoding(time_str)));
+ // Fx*/Cfd* instruments report Ask/Bid OHLC (use mid); Stock/Etf/Bond/Future report plain
+ // OHLC + Volume. Decide off the AssetType, falling back to the open==0.0 heuristic for "Stock".
+ const bool ask_bid_asset = asset_type.startsWith("Fx", Qt::CaseInsensitive) ||
+ asset_type.startsWith("Cfd", Qt::CaseInsensitive);
auto& http = BrokerHttp::instance();
- auto resp = http.get(url, auth_headers(creds));
- if (!resp.success)
- return {false, std::nullopt, checked_error(resp, "get_history failed"), ts};
+ // Single-request fetch+parse parameterized by Time (Mode=UpTo) and Count. Reuses the existing
+ // URL build and the asset-type-aware OHLC parser. ok=false signals a transport/parse failure.
+ struct PageResult {
+ bool ok = false;
+ QString error;
+ QVector<BrokerCandle> candles;
+ };
+ auto fetch_page = [&](const QString& time_str, int page_count) -> PageResult {
+ PageResult page;
+
+ QString url = QString("%1/chart/v1/charts?Uic=%2&AssetType=%3&Horizon=%4&Count=%5"
+ "&Mode=UpTo&Time=%6")
+ .arg(BASE_LIVE, uic, QString(QUrl::toPercentEncoding(asset_type)),
+ QString::number(horizon), QString::number(page_count),
+ QString(QUrl::toPercentEncoding(time_str)));
+
+ auto resp = http.get(url, auth_headers(creds));
+ if (!resp.success) {
+ page.error = checked_error(resp, "get_history failed");
+ return page;
+ }
- QJsonDocument doc = QJsonDocument::fromJson(resp.raw_body.toUtf8());
- if (!doc.isObject())
- return {false, std::nullopt, "get_history: invalid response", ts};
+ QJsonDocument doc = QJsonDocument::fromJson(resp.raw_body.toUtf8());
+ if (!doc.isObject()) {
+ page.error = "get_history: invalid response";
+ return page;
+ }
- QJsonArray arr = doc.object().value("Data").toArray();
- QVector<BrokerCandle> candles;
- candles.reserve(arr.size());
+ QJsonArray arr = doc.object().value("Data").toArray();
+ page.candles.reserve(arr.size());
+ for (const QJsonValue& v : arr) {
+ QJsonObject o = v.toObject();
+ // Time is ISO8601 string: "2024-01-15T00:00:00.000000Z"
+ QDateTime dt = QDateTime::fromString(o.value("Time").toString(), Qt::ISODate);
+
+ BrokerCandle c;
+ c.timestamp = dt.isValid() ? dt.toMSecsSinceEpoch() : 0LL;
+ // Stocks/Etf/Bond/Future use Open/High/Low/Close/Volume directly
+ c.open = o.value("Open").toDouble();
+ c.high = o.value("High").toDouble();
+ c.low = o.value("Low").toDouble();
+ c.close = o.value("Close").toDouble();
+ c.volume = static_cast<int64_t>(o.value("Volume").toDouble());
+
+ // Fx*/Cfd* (ask/bid split) — use mid. Fallback heuristic: plain Open absent (==0.0).
+ if (ask_bid_asset || c.open == 0.0) {
+ c.open = (o.value("OpenAsk").toDouble() + o.value("OpenBid").toDouble()) / 2.0;
+ c.high = (o.value("HighAsk").toDouble() + o.value("HighBid").toDouble()) / 2.0;
+ c.low = (o.value("LowAsk").toDouble() + o.value("LowBid").toDouble()) / 2.0;
+ c.close = (o.value("CloseAsk").toDouble() + o.value("CloseBid").toDouble()) / 2.0;
+ }
+ page.candles.append(c);
+ }
- for (const QJsonValue& v : arr) {
- QJsonObject o = v.toObject();
- // Time is ISO8601 string: "2024-01-15T00:00:00.000000Z"
- QDateTime dt = QDateTime::fromString(o.value("Time").toString(), Qt::ISODate);
-
- BrokerCandle c;
- c.timestamp = dt.isValid() ? dt.toMSecsSinceEpoch() : 0LL;
- // Stocks use Open/High/Low/Close/Volume directly
- c.open = o.value("Open").toDouble();
- c.high = o.value("High").toDouble();
- c.low = o.value("Low").toDouble();
- c.close = o.value("Close").toDouble();
- c.volume = static_cast<int64_t>(o.value("Volume").toDouble());
-
- // Fallback for FX (ask/bid split) — use mid
- if (c.open == 0.0) {
- c.open = (o.value("OpenAsk").toDouble() + o.value("OpenBid").toDouble()) / 2.0;
- c.high = (o.value("HighAsk").toDouble() + o.value("HighBid").toDouble()) / 2.0;
- c.low = (o.value("LowAsk").toDouble() + o.value("LowBid").toDouble()) / 2.0;
- c.close = (o.value("CloseAsk").toDouble() + o.value("CloseBid").toDouble()) / 2.0;
+ page.ok = true;
+ return page;
+ };
+
+ const QString to_time_str = QDateTime(to, QTime(23, 59, 59), QTimeZone::UTC).toString(Qt::ISODate);
+
+ // Single-request path (count fits in one page): identical to the historical behavior.
+ if (desired_count <= SAXO_MAX_COUNT) {
+ PageResult page = fetch_page(to_time_str, desired_count);
+ if (!page.ok)
+ return {false, std::nullopt, page.error, ts};
+ return {true, page.candles, "", ts};
+ }
+
+ // Backward paging: repeatedly request SAXO_MAX_COUNT samples ending at Time, walking Time back
+ // to (oldest returned sample - 1s) until we reach from_date, get a short/empty page, or hit
+ // the safety cap. First-page failure returns an error; later-page failure stops and returns
+ // what was collected so far.
+ const int64_t from_ms = QDateTime(from, QTime(0, 0, 0), QTimeZone::UTC).toMSecsSinceEpoch();
+ QVector<BrokerCandle> candles;
+ QString time_str = to_time_str;
+
+ for (int iter = 0; iter < SAXO_MAX_PAGES; ++iter) {
+ PageResult page = fetch_page(time_str, SAXO_MAX_COUNT);
+ if (!page.ok) {
+ if (iter == 0)
+ return {false, std::nullopt, page.error, ts};
+ break; // later-page failure: stop and return collected
}
- candles.append(c);
+ if (page.candles.isEmpty())
+ break;
+
+ candles += page.candles;
+
+ // Determine the oldest sample time in this page (Saxo returns ascending, but don't assume).
+ int64_t oldest_ms = page.candles.first().timestamp;
+ for (const BrokerCandle& c : page.candles)
+ if (c.timestamp > 0 && c.timestamp < oldest_ms)
+ oldest_ms = c.timestamp;
+
+ // Reached or passed the start of the requested range, or got a short page (no more data).
+ if (oldest_ms <= from_ms || page.candles.size() < SAXO_MAX_COUNT)
+ break;
+
+ // Next page ends just before the oldest sample we already have.
+ QDateTime next = QDateTime::fromMSecsSinceEpoch(oldest_ms - 1000, QTimeZone::UTC);
+ time_str = next.toString(Qt::ISODate);
+ }
+
+ // Merge: sort ascending by time, dedupe by timestamp, drop samples older than from_date.
+ std::sort(candles.begin(), candles.end(),
+ [](const BrokerCandle& a, const BrokerCandle& b) { return a.timestamp < b.timestamp; });
+
+ QVector<BrokerCandle> merged;
+ merged.reserve(candles.size());
+ int64_t last_ts = std::numeric_limits<int64_t>::min();
+ for (const BrokerCandle& c : candles) {
+ if (c.timestamp < from_ms)
+ continue;
+ if (c.timestamp == last_ts)
+ continue; // dedupe overlapping page boundaries
+ merged.append(c);
+ last_ts = c.timestamp;
}
- return {true, candles, "", ts};
+ return {true, merged, "", ts};
}
} // namespace fincept::trading
diff --git a/fincept-qt/src/trading/brokers/saxo/SaxoBankBroker.h b/fincept-qt/src/trading/brokers/saxo/SaxoBankBroker.h
index 05ea0014f..93478aa0a 100644
--- a/fincept-qt/src/trading/brokers/saxo/SaxoBankBroker.h
+++ b/fincept-qt/src/trading/brokers/saxo/SaxoBankBroker.h
@@ -87,6 +87,8 @@ class SaxoBankBroker : public IBroker {
static int saxo_horizon(const QString& resolution);
// Extract Uic from symbol "NYSE:AAPL:211" → "211"
static QString extract_uic(const QString& symbol);
+ // Extract optional AssetType from symbol "EXCHANGE:SYMBOL:UIC:ASSETTYPE" → 4th part, else "Stock"
+ static QString extract_asset_type(const QString& symbol);
};
} // namespace fincept::trading
diff --git a/fincept-qt/src/trading/brokers/tradejini/TradejiniBroker.cpp b/fincept-qt/src/trading/brokers/tradejini/TradejiniBroker.cpp
index 55d6f542b..c23ae4bd8 100644
--- a/fincept-qt/src/trading/brokers/tradejini/TradejiniBroker.cpp
+++ b/fincept-qt/src/trading/brokers/tradejini/TradejiniBroker.cpp
@@ -7,6 +7,8 @@
#include <QJsonDocument>
#include <QUrlQuery>
+#include <algorithm>
+
namespace fincept::trading {
static constexpr const char* BASE = "https://api.tradejini.com/v2";
@@ -478,63 +480,149 @@ ApiResponse<QVector<BrokerCandle>> TradejiniBroker::get_history(const BrokerCred
// Tradejini intervals are integer minutes; daily is unsupported via this endpoint.
static const QMap<QString, QString> interval_map = {
- {"1", "1"}, {"5", "5"}, {"15", "15"}, {"30", "30"}, {"60", "60"},
+ {"1", "1"}, {"5", "5"}, {"15", "15"}, {"30", "30"},
};
- QString interval = interval_map.value(resolution, "5");
-
- QUrlQuery q;
- q.addQueryItem("id", sym_id);
- q.addQueryItem("interval", interval);
- q.addQueryItem("from", QString::number(from_epoch));
- q.addQueryItem("to", QString::number(to_epoch));
- QString url = QString("%1/api/mkt-data/chart/interval-data?%2").arg(BASE, q.toString(QUrl::FullyEncoded));
+ if (!interval_map.contains(resolution))
+ return {false, std::nullopt,
+ QString("Tradejini chart supports only minute intervals (1,5,30); resolution '%1' (e.g. daily) is not supported by this endpoint").arg(resolution), ts};
+ QString interval = interval_map.value(resolution);
auto& http = BrokerHttp::instance();
- auto resp = http.get(url, {{"Authorization", bearer(creds)}});
- if (!resp.success && resp.status_code == 0)
- return {false, std::nullopt, checked_error(resp, "get_history failed"), ts};
+ // Tradejini's chart endpoint caps each request to ~30 days. Fetch one
+ // <=30-day window at a time, keeping the IST 09:15/15:30 session clamp.
+ // Returns parsed candles on success; on failure, `ok` is false and `err`
+ // carries the upstream error.
+ struct WindowResult {
+ bool ok = false;
+ QVector<BrokerCandle> candles;
+ QString err;
+ };
+ auto fetch_window = [&](int64_t win_from, int64_t win_to) -> WindowResult {
+ WindowResult wr;
- QJsonDocument doc = QJsonDocument::fromJson(resp.raw_body.toUtf8());
- if (!doc.isObject())
- return {false, std::nullopt, "get_history: invalid response", ts};
+ QUrlQuery q;
+ q.addQueryItem("id", sym_id);
+ q.addQueryItem("interval", interval);
+ q.addQueryItem("from", QString::number(win_from));
+ q.addQueryItem("to", QString::number(win_to));
+ QString url = QString("%1/api/mkt-data/chart/interval-data?%2").arg(BASE, q.toString(QUrl::FullyEncoded));
- QJsonObject obj = doc.object();
- if (obj.value("s").toString() != "ok")
- return {false, std::nullopt, checked_error(resp, "get_history failed"), ts};
+ auto resp = http.get(url, {{"Authorization", bearer(creds)}});
- QVector<BrokerCandle> candles;
- // Payload "d" is typically an array of candle rows; support both object-rows
- // and column arrays [t, o, h, l, c, v].
- for (const QJsonValue& v : obj.value("d").toArray()) {
- BrokerCandle c;
- if (v.isObject()) {
- QJsonObject o = v.toObject();
- QJsonValue tv = o.contains("timestamp") ? o.value("timestamp") : o.value("time");
- int64_t t = static_cast<int64_t>(tv.toVariant().toLongLong());
- c.timestamp = (t > 1000000000000LL) ? t : t * 1000LL;
- c.open = o.value("open").toVariant().toDouble();
- c.high = o.value("high").toVariant().toDouble();
- c.low = o.value("low").toVariant().toDouble();
- c.close = o.value("close").toVariant().toDouble();
- c.volume = o.value("volume").toVariant().toDouble();
- } else if (v.isArray()) {
- QJsonArray row = v.toArray();
- if (row.size() < 6)
+ if (!resp.success && resp.status_code == 0) {
+ wr.err = checked_error(resp, "get_history failed");
+ return wr;
+ }
+
+ QJsonDocument doc = QJsonDocument::fromJson(resp.raw_body.toUtf8());
+ if (!doc.isObject()) {
+ wr.err = "get_history: invalid response";
+ return wr;
+ }
+
+ QJsonObject obj = doc.object();
+ if (obj.value("s").toString() != "ok") {
+ wr.err = checked_error(resp, "get_history failed");
+ return wr;
+ }
+
+ // Payload "d" is typically an array of candle rows; support both object-rows
+ // and column arrays [t, o, h, l, c, v].
+ for (const QJsonValue& v : obj.value("d").toArray()) {
+ BrokerCandle c;
+ if (v.isObject()) {
+ QJsonObject o = v.toObject();
+ QJsonValue tv = o.contains("timestamp") ? o.value("timestamp") : o.value("time");
+ int64_t t = static_cast<int64_t>(tv.toVariant().toLongLong());
+ c.timestamp = (t > 1000000000000LL) ? t : t * 1000LL;
+ c.open = o.value("open").toVariant().toDouble();
+ c.high = o.value("high").toVariant().toDouble();
+ c.low = o.value("low").toVariant().toDouble();
+ c.close = o.value("close").toVariant().toDouble();
+ c.volume = o.value("volume").toVariant().toDouble();
+ } else if (v.isArray()) {
+ QJsonArray row = v.toArray();
+ if (row.size() < 6)
+ continue;
+ int64_t t = row[0].toVariant().toLongLong();
+ c.timestamp = (t > 1000000000000LL) ? t : t * 1000LL;
+ c.open = row[1].toVariant().toDouble();
+ c.high = row[2].toVariant().toDouble();
+ c.low = row[3].toVariant().toDouble();
+ c.close = row[4].toVariant().toDouble();
+ c.volume = row[5].toVariant().toDouble();
+ } else {
continue;
- int64_t t = row[0].toVariant().toLongLong();
- c.timestamp = (t > 1000000000000LL) ? t : t * 1000LL;
- c.open = row[1].toVariant().toDouble();
- c.high = row[2].toVariant().toDouble();
- c.low = row[3].toVariant().toDouble();
- c.close = row[4].toVariant().toDouble();
- c.volume = row[5].toVariant().toDouble();
- } else {
- continue;
+ }
+ wr.candles.append(c);
+ }
+
+ wr.ok = true;
+ return wr;
+ };
+
+ // Window cap: <= 30 days per request for all supported intervals.
+ static constexpr int64_t kMaxWindowSecs = 30LL * 24 * 60 * 60;
+ static constexpr int kMaxIterations = 60;
+
+ QVector<BrokerCandle> candles;
+
+ if (to_epoch - from_epoch <= kMaxWindowSecs) {
+ // Range fits in a single request: behaves exactly as before.
+ WindowResult wr = fetch_window(from_epoch, to_epoch);
+ if (!wr.ok)
+ return {false, std::nullopt, wr.err, ts};
+ candles = wr.candles;
+ } else {
+ // Chunk into consecutive <=30-day sub-windows, re-clamping each window's
+ // day boundaries to the IST 09:15/15:30 session.
+ bool any_data = false;
+ int iterations = 0;
+ int64_t cursor = from_epoch;
+
+ while (cursor < to_epoch && iterations < kMaxIterations) {
+ ++iterations;
+
+ QDate win_from_date = QDateTime::fromSecsSinceEpoch(cursor).date();
+ int64_t win_from = QDateTime(win_from_date, QTime(9, 15, 0)).toSecsSinceEpoch();
+
+ int64_t tentative_to = cursor + kMaxWindowSecs;
+ int64_t win_end = qMin(tentative_to, to_epoch);
+ QDate win_to_date = QDateTime::fromSecsSinceEpoch(win_end).date();
+ int64_t win_to = QDateTime(win_to_date, QTime(15, 30, 0)).toSecsSinceEpoch();
+ if (win_to > to_epoch)
+ win_to = to_epoch;
+
+ WindowResult wr = fetch_window(win_from, win_to);
+ if (!wr.ok) {
+ // First-window failure surfaces the error; a later failure after we
+ // already have data returns the partial result.
+ if (!any_data)
+ return {false, std::nullopt, wr.err, ts};
+ break;
+ }
+
+ if (!wr.candles.isEmpty()) {
+ candles += wr.candles;
+ any_data = true;
+ }
+
+ // Advance one day past this window's end to avoid re-requesting the
+ // boundary day.
+ cursor = win_end + 24 * 60 * 60;
}
- candles.append(c);
}
+ // Sort ascending by timestamp and dedupe (windows may overlap at boundaries).
+ std::sort(candles.begin(), candles.end(),
+ [](const BrokerCandle& a, const BrokerCandle& b) { return a.timestamp < b.timestamp; });
+ candles.erase(std::unique(candles.begin(), candles.end(),
+ [](const BrokerCandle& a, const BrokerCandle& b) {
+ return a.timestamp == b.timestamp;
+ }),
+ candles.end());
+
return {true, candles, "", ts};
}
diff --git a/fincept-qt/src/trading/brokers/tradier/TradierBroker.cpp b/fincept-qt/src/trading/brokers/tradier/TradierBroker.cpp
index e28893450..368887a5b 100644
--- a/fincept-qt/src/trading/brokers/tradier/TradierBroker.cpp
+++ b/fincept-qt/src/trading/brokers/tradier/TradierBroker.cpp
@@ -8,6 +8,8 @@
#include <QJsonDocument>
#include <QUrlQuery>
+#include <algorithm>
+
namespace fincept::trading {
static int64_t now_ts() {
@@ -485,6 +487,15 @@ ApiResponse<QVector<BrokerCandle>> TradierBroker::get_history(const BrokerCreden
bool is_intraday = (resolution == "1" || resolution == "5" || resolution == "15" || resolution == "1m" ||
resolution == "5m" || resolution == "15m");
+ // Reject unsupported intraday resolutions — Tradier only offers 1min/5min/15min bars
+ if (resolution == "30" || resolution == "60" || resolution == "240" || resolution == "30m" ||
+ resolution == "60m" || resolution == "240m" || resolution == "1h" || resolution == "4h") {
+ return {false, std::nullopt,
+ "Tradier does not support " + resolution +
+ "-minute bars; supported intraday intervals are 1min/5min/15min only",
+ ts};
+ }
+
if (is_intraday) {
// timesales endpoint
QString intrv;
@@ -495,39 +506,123 @@ ApiResponse<QVector<BrokerCandle>> TradierBroker::get_history(const BrokerCreden
else
intrv = "15min";
- // timesales expects datetime format "YYYY-MM-DD HH:MM"
- QString start = from_date + " 09:30";
- QString end = to_date + " 16:00";
-
- QString url = base(creds) + "/v1/markets/timesales?symbol=" + ticker + "&interval=" + intrv +
- "&start=" + QUrl::toPercentEncoding(start) + "&end=" + QUrl::toPercentEncoding(end);
-
- auto resp = http.get(url, auth_headers(creds));
- if (!resp.success)
- return {false, std::nullopt, checked_error(resp, "get_history failed"), ts};
-
- QJsonDocument doc = QJsonDocument::fromJson(resp.raw_body.toUtf8());
- if (!doc.isObject())
- return {false, std::nullopt, "get_history: invalid response", ts};
-
- QJsonValue series_val = doc.object().value("series");
- if (series_val.isNull() || series_val.isString())
- return {true, candles, "", ts};
-
- QJsonArray arr = normalize_array(series_val.toObject().value("data"));
- candles.reserve(arr.size());
-
- for (const QJsonValue& v : arr) {
- QJsonObject o = v.toObject();
- BrokerCandle c;
- // timesales has both "time" (ISO string) and "timestamp" (epoch seconds)
- c.timestamp = static_cast<int64_t>(o.value("timestamp").toDouble()) * 1000LL;
- c.open = o.value("open").toDouble();
- c.high = o.value("high").toDouble();
- c.low = o.value("low").toDouble();
- c.close = o.value("close").toDouble();
- c.volume = static_cast<int64_t>(o.value("volume").toDouble());
- candles.append(c);
+ // Per-request day cap: Tradier's timesales window is bounded, so long
+ // ranges are split into consecutive sub-windows.
+ // 1min -> <= 20 days per request
+ // 5min/15min -> <= 40 days per request
+ const int cap_days = (intrv == "1min") ? 20 : 40;
+
+ // One-window fetch + parse. Returns success flag, error message, and the
+ // parsed candles for [winStart, winEnd] (dates as yyyy-MM-dd). The
+ // datetime format and ms timestamp conversion match the original code.
+ struct WindowResult {
+ bool success = false;
+ QString error;
+ QVector<BrokerCandle> candles;
+ };
+ auto fetch_window = [&](const QString& winStart, const QString& winEnd) -> WindowResult {
+ WindowResult wr;
+
+ // timesales expects datetime format "YYYY-MM-DD HH:MM"
+ QString start = winStart + " 09:30";
+ QString end = winEnd + " 16:00";
+
+ QString url = base(creds) + "/v1/markets/timesales?symbol=" + ticker + "&interval=" + intrv +
+ "&start=" + QUrl::toPercentEncoding(start) + "&end=" + QUrl::toPercentEncoding(end);
+
+ auto resp = http.get(url, auth_headers(creds));
+ if (!resp.success) {
+ wr.error = checked_error(resp, "get_history failed");
+ return wr;
+ }
+
+ QJsonDocument doc = QJsonDocument::fromJson(resp.raw_body.toUtf8());
+ if (!doc.isObject()) {
+ wr.error = "get_history: invalid response";
+ return wr;
+ }
+
+ QJsonValue series_val = doc.object().value("series");
+ if (series_val.isNull() || series_val.isString()) {
+ // No data for this window — treat as a successful empty result.
+ wr.success = true;
+ return wr;
+ }
+
+ QJsonArray arr = normalize_array(series_val.toObject().value("data"));
+ wr.candles.reserve(arr.size());
+
+ for (const QJsonValue& v : arr) {
+ QJsonObject o = v.toObject();
+ BrokerCandle c;
+ // timesales has both "time" (ISO string) and "timestamp" (epoch seconds)
+ c.timestamp = static_cast<int64_t>(o.value("timestamp").toDouble()) * 1000LL;
+ c.open = o.value("open").toDouble();
+ c.high = o.value("high").toDouble();
+ c.low = o.value("low").toDouble();
+ c.close = o.value("close").toDouble();
+ c.volume = static_cast<int64_t>(o.value("volume").toDouble());
+ wr.candles.append(c);
+ }
+
+ wr.success = true;
+ return wr;
+ };
+
+ QDate startDate = QDate::fromString(from_date, "yyyy-MM-dd");
+ QDate endDate = QDate::fromString(to_date, "yyyy-MM-dd");
+
+ // If either date is unparseable, fall back to a single request over the
+ // raw range (preserves original behavior for unexpected inputs).
+ bool fits_one_window =
+ !startDate.isValid() || !endDate.isValid() || startDate.daysTo(endDate) < cap_days;
+
+ if (fits_one_window) {
+ // Single request — identical to the original single-window path.
+ WindowResult wr = fetch_window(from_date, to_date);
+ if (!wr.success)
+ return {false, std::nullopt, wr.error, ts};
+ candles = std::move(wr.candles);
+ } else {
+ // Loop consecutive <= cap_days sub-windows. Each window spans cap_days
+ // inclusive of both endpoints, i.e. [w, w + (cap_days - 1)].
+ const int kMaxIterations = 60; // safety bound
+ QDate winStart = startDate;
+ int iterations = 0;
+ bool got_data = false;
+
+ while (winStart <= endDate && iterations < kMaxIterations) {
+ QDate winEnd = winStart.addDays(cap_days - 1);
+ if (winEnd > endDate)
+ winEnd = endDate;
+
+ WindowResult wr = fetch_window(winStart.toString("yyyy-MM-dd"), winEnd.toString("yyyy-MM-dd"));
+ if (!wr.success) {
+ // First-window failure → propagate error as the original code did.
+ // Later failure (after collecting data) → break and return partial.
+ if (!got_data)
+ return {false, std::nullopt, wr.error, ts};
+ break;
+ }
+
+ if (!wr.candles.isEmpty()) {
+ got_data = true;
+ candles.append(wr.candles);
+ }
+
+ winStart = winEnd.addDays(1);
+ ++iterations;
+ }
+
+ // Sort ascending by timestamp and dedupe (windows are adjacent but a
+ // shared boundary bar could appear in two requests).
+ std::sort(candles.begin(), candles.end(),
+ [](const BrokerCandle& a, const BrokerCandle& b) { return a.timestamp < b.timestamp; });
+ candles.erase(std::unique(candles.begin(), candles.end(),
+ [](const BrokerCandle& a, const BrokerCandle& b) {
+ return a.timestamp == b.timestamp;
+ }),
+ candles.end());
}
} else {
@@ -537,8 +632,10 @@ ApiResponse<QVector<BrokerCandle>> TradierBroker::get_history(const BrokerCreden
intrv = "weekly";
else if (resolution == "M" || resolution == "1M")
intrv = "monthly";
- else
+ else if (resolution == "D" || resolution == "1D" || resolution == "1d" || resolution.isEmpty())
intrv = "daily";
+ else
+ return {false, std::nullopt, "get_history: unsupported resolution '" + resolution + "'", ts};
QString url = base(creds) + "/v1/markets/history?symbol=" + ticker + "&interval=" + intrv +
"&start=" + from_date + "&end=" + to_date;
diff --git a/fincept-qt/src/trading/brokers/upstox/UpstoxBroker.cpp b/fincept-qt/src/trading/brokers/upstox/UpstoxBroker.cpp
index 7ee9054dc..d171a856c 100644
--- a/fincept-qt/src/trading/brokers/upstox/UpstoxBroker.cpp
+++ b/fincept-qt/src/trading/brokers/upstox/UpstoxBroker.cpp
@@ -465,7 +465,13 @@ ApiResponse<QVector<BrokerQuote>> UpstoxBroker::get_quotes(const BrokerCredentia
ApiResponse<QVector<BrokerCandle>> UpstoxBroker::get_history(const BrokerCredentials& creds, const QString& symbol,
const QString& resolution, const QString& from_date,
const QString& to_date) {
- const QString ikey = instrument_key(symbol, "NSE", creds.broker_id);
+ // Support "EXCHANGE:SYMBOL" format (mirrors get_quotes); default exchange NSE
+ QString exch = "NSE", s = symbol;
+ if (symbol.contains(':')) {
+ exch = symbol.section(':', 0, 0);
+ s = symbol.section(':', 1);
+ }
+ const QString ikey = instrument_key(s, exch, creds.broker_id);
const auto iv = upstox_interval(resolution);
// Chunk the date range if needed (Upstox has per-interval max window limits)
@@ -525,6 +531,7 @@ ApiResponse<QVector<BrokerCandle>> UpstoxBroker::get_history(const BrokerCredent
candle.low = c[3].toDouble();
candle.close = c[4].toDouble();
candle.volume = c[5].toDouble();
+ if (c.size() > 6) candle.oi = c[6].toDouble();
all_candles.append(candle);
}
diff --git a/fincept-qt/src/trading/brokers/zerodha/ZerodhaBroker.cpp b/fincept-qt/src/trading/brokers/zerodha/ZerodhaBroker.cpp
index b6e4d015e..02009e3fd 100644
--- a/fincept-qt/src/trading/brokers/zerodha/ZerodhaBroker.cpp
+++ b/fincept-qt/src/trading/brokers/zerodha/ZerodhaBroker.cpp
@@ -7,12 +7,14 @@
#include "trading/instruments/InstrumentService.h"
#include <QCryptographicHash>
+#include <QDate>
#include <QDateTime>
#include <QJsonArray>
#include <QJsonDocument>
#include <algorithm>
#include <cmath>
+#include <utility>
namespace fincept::trading {
@@ -88,7 +90,7 @@ QString ZerodhaBroker::zerodha_interval(const QString& resolution) {
{"2h", "60minute"}, {"2H", "60minute"},
{"3d", "day"}, {"3D", "day"},
};
- return map.value(resolution, "day");
+ return map.value(resolution, QString());
}
bool ZerodhaBroker::is_token_expired(const BrokerHttpResponse& resp) {
@@ -409,34 +411,113 @@ ApiResponse<QVector<BrokerCandle>> ZerodhaBroker::get_history(const BrokerCreden
}
QString interval = zerodha_interval(resolution);
- QString url = QString("%1/instruments/historical/%2/%3?from=%4&to=%5&continuous=0&oi=1")
- .arg(base_url(), token, interval, from_date, to_date);
- auto resp = BrokerHttp::instance().get(url, auth_headers(creds));
- int64_t ts = now_ts();
- if (!resp.success)
- return {false, std::nullopt, checked_error(resp, resp.error), ts};
+ if (interval.isEmpty()) {
+ return {false, std::nullopt,
+ "Zerodha get_history: unsupported resolution '" + resolution + "'", now_ts()};
+ }
+ // Kite Connect caps the date range PER REQUEST by interval. Resolve the max
+ // number of days allowed in a single request for the (already-resolved) interval.
+ auto cap_days_for_interval = [](const QString& iv) -> int {
+ static const QMap<QString, int> caps = {
+ {"minute", 60}, {"2minute", 60}, {"3minute", 100}, {"4minute", 100}, {"5minute", 100},
+ {"10minute", 100}, {"15minute", 200}, {"30minute", 200}, {"60minute", 400}, {"hour", 400},
+ {"2hour", 400}, {"3hour", 400}, {"4hour", 400}, {"day", 2000}, {"week", 2000},
+ };
+ return caps.value(iv, 2000);
+ };
+ const int cap_days = cap_days_for_interval(interval);
+
+ // Fetch + parse ONE date window. Returns {http_resp, parsed_candles}.
+ // Reuses the existing URL build + response parse (percent-encoding/URL unchanged per request).
+ auto fetch_window = [&](const QString& win_from, const QString& win_to) {
+ QString url = QString("%1/instruments/historical/%2/%3?from=%4&to=%5&continuous=0&oi=1")
+ .arg(base_url(), token, interval, win_from, win_to);
+ auto resp = BrokerHttp::instance().get(url, auth_headers(creds));
+
+ QVector<BrokerCandle> win_candles;
+ if (resp.success) {
+ auto arr = resp.json.value("data").toObject().value("candles").toArray();
+ for (const auto& v : arr) {
+ auto c = v.toArray();
+ if (c.size() < 6)
+ continue;
+ qint64 epoch = QDateTime::fromString(c[0].toString(), Qt::ISODateWithMs).toSecsSinceEpoch();
+ if (epoch == 0)
+ epoch = QDateTime::fromString(c[0].toString(), Qt::ISODate).toSecsSinceEpoch();
+ BrokerCandle bc;
+ bc.timestamp = epoch;
+ bc.open = c[1].toDouble();
+ bc.high = c[2].toDouble();
+ bc.low = c[3].toDouble();
+ bc.close = c[4].toDouble();
+ bc.volume = c[5].toDouble();
+ // c[6] is Open Interest when oi=1 is requested (F&O only); 0 elsewhere.
+ if (c.size() >= 7)
+ bc.oi = c[6].toDouble();
+ win_candles.append(bc);
+ }
+ }
+ return std::make_pair(resp, win_candles);
+ };
+
+ // The caller passes "yyyy-MM-dd". Parse to compute the span; on failure, fall back
+ // to the single-request path (preserves existing behavior for unexpected formats).
+ const QDate d_from = QDate::fromString(from_date, "yyyy-MM-dd");
+ const QDate d_to = QDate::fromString(to_date, "yyyy-MM-dd");
+ const bool dates_valid = d_from.isValid() && d_to.isValid() && d_from <= d_to;
+ const qint64 span_days = dates_valid ? d_from.daysTo(d_to) : 0;
+
+ // Single-window path: range fits in one request (or dates couldn't be parsed).
+ // Behaviorally identical to the original implementation.
+ if (!dates_valid || span_days <= cap_days) {
+ auto [resp, candles] = fetch_window(from_date, to_date);
+ int64_t ts = now_ts();
+ if (!resp.success)
+ return {false, std::nullopt, checked_error(resp, resp.error), ts};
+ return {true, candles, "", ts};
+ }
+
+ // Multi-window path: slice [from,to] into consecutive <=cap_days sub-windows
+ // (oldest -> newest) and accumulate candles.
QVector<BrokerCandle> candles;
- auto arr = resp.json.value("data").toObject().value("candles").toArray();
- for (const auto& v : arr) {
- auto c = v.toArray();
- if (c.size() < 6)
- continue;
- qint64 epoch = QDateTime::fromString(c[0].toString(), Qt::ISODateWithMs).toSecsSinceEpoch();
- if (epoch == 0)
- epoch = QDateTime::fromString(c[0].toString(), Qt::ISODate).toSecsSinceEpoch();
- BrokerCandle bc;
- bc.timestamp = epoch;
- bc.open = c[1].toDouble();
- bc.high = c[2].toDouble();
- bc.low = c[3].toDouble();
- bc.close = c[4].toDouble();
- bc.volume = c[5].toDouble();
- // c[6] is Open Interest when oi=1 is requested (F&O only); 0 elsewhere.
- if (c.size() >= 7)
- bc.oi = c[6].toDouble();
- candles.append(bc);
+ constexpr int kMaxIterations = 60;
+ int iterations = 0;
+ QDate cursor = d_from;
+ int64_t ts = now_ts();
+ while (cursor <= d_to) {
+ if (++iterations > kMaxIterations)
+ break; // SAFETY: cap the loop; return what we have so far.
+
+ QDate win_end = cursor.addDays(cap_days);
+ if (win_end > d_to)
+ win_end = d_to;
+
+ auto [resp, win_candles] = fetch_window(cursor.toString("yyyy-MM-dd"), win_end.toString("yyyy-MM-dd"));
+ ts = now_ts();
+ if (!resp.success) {
+ // First window fails -> propagate the error as today. Otherwise, partial success:
+ // stop and return whatever we have already collected.
+ if (candles.isEmpty())
+ return {false, std::nullopt, checked_error(resp, resp.error), ts};
+ break;
+ }
+ candles += win_candles;
+
+ // Advance past the current window. Step to the day after win_end to avoid an
+ // infinite loop and overlap; consecutive duplicate timestamps are deduped below.
+ if (win_end == d_to)
+ break;
+ cursor = win_end.addDays(1);
}
+
+ // Sort ascending by timestamp, then dedupe consecutive duplicate timestamps.
+ std::sort(candles.begin(), candles.end(),
+ [](const BrokerCandle& a, const BrokerCandle& b) { return a.timestamp < b.timestamp; });
+ candles.erase(std::unique(candles.begin(), candles.end(),
+ [](const BrokerCandle& a, const BrokerCandle& b) { return a.timestamp == b.timestamp; }),
+ candles.end());
+
return {true, candles, "", ts};
}
diff --git a/fincept-qt/src/trading/instruments/DhanInstrumentParser.cpp b/fincept-qt/src/trading/instruments/DhanInstrumentParser.cpp
new file mode 100644
index 000000000..5de7f1367
--- /dev/null
+++ b/fincept-qt/src/trading/instruments/DhanInstrumentParser.cpp
@@ -0,0 +1,203 @@
+#include "trading/instruments/DhanInstrumentParser.h"
+
+#include "core/logging/Logger.h"
+
+#include <QDate>
+#include <QMap>
+#include <QStringList>
+#include <QTextStream>
+
+namespace fincept::trading {
+namespace {
+
+// Dhan index trading symbols carry spaces ("NIFTY 50", "NIFTY BANK"); map the
+// well-known tradeable ones to the fincept normalised form. Anonymous namespace
+// isolates this from the other parsers' index_map() under unity builds.
+const QMap<QString, QString>& dhan_index_map() {
+ static const QMap<QString, QString> m = {
+ {"NIFTY 50", "NIFTY"},
+ {"NIFTY BANK", "BANKNIFTY"},
+ {"NIFTY FIN SERVICE", "FINNIFTY"},
+ {"NIFTY FINANCIAL SERVICES", "FINNIFTY"},
+ {"NIFTY MID SELECT", "MIDCPNIFTY"},
+ {"NIFTY MIDCAP SELECT", "MIDCPNIFTY"},
+ {"NIFTY NEXT 50", "NIFTYNXT50"},
+ {"INDIA VIX", "INDIAVIX"},
+ {"SENSEX", "SENSEX"},
+ {"BANKEX", "BANKEX"},
+ {"SENSEX 50", "SENSEX50"},
+ };
+ return m;
+}
+
+InstrumentType map_dhan_type(const QString& instrument_name, const QString& option_type) {
+ if (instrument_name == "EQUITY")
+ return InstrumentType::EQ;
+ if (instrument_name == "INDEX")
+ return InstrumentType::INDEX;
+ if (instrument_name.startsWith("OPT")) {
+ if (option_type == "CE")
+ return InstrumentType::CE;
+ if (option_type == "PE")
+ return InstrumentType::PE;
+ return InstrumentType::UNKNOWN;
+ }
+ if (instrument_name.startsWith("FUT"))
+ return InstrumentType::FUT;
+ return InstrumentType::UNKNOWN;
+}
+
+} // namespace
+
+QString DhanInstrumentParser::map_index_name(const QString& trading_symbol) {
+ const QString up = trading_symbol.toUpper();
+ auto it = dhan_index_map().constFind(up);
+ if (it != dhan_index_map().constEnd())
+ return it.value();
+ // Unmapped index: strip spaces (e.g. "NIFTY MIDCAP 150" → "NIFTYMIDCAP150").
+ return QString(up).remove(' ');
+}
+
+// "2024-08-28 14:30:00" (or "2024-08-28") → "28AUG24"
+QString DhanInstrumentParser::expiry_nodashes(const QString& expiry_raw) {
+ if (expiry_raw.isEmpty())
+ return {};
+ QDate d = QDate::fromString(expiry_raw.left(10), "yyyy-MM-dd");
+ if (!d.isValid() || d.year() <= 1)
+ return {}; // Dhan uses "0001-01-01" as a null-expiry sentinel
+ return d.toString("ddMMMyy").toUpper();
+}
+
+QString DhanInstrumentParser::normalise_exchange(const QString& exch, const QString& segment, const QString& itype) {
+ Q_UNUSED(itype)
+ const QString ex = exch.toUpper();
+ const QString seg = segment.toUpper();
+ if (seg == "I")
+ return ex + "_INDEX";
+ if (seg == "D")
+ return ex == "NSE" ? QStringLiteral("NFO") : ex == "BSE" ? QStringLiteral("BFO") : ex;
+ if (seg == "C")
+ return ex == "NSE" ? QStringLiteral("CDS") : ex == "BSE" ? QStringLiteral("BCD") : ex;
+ if (seg == "M")
+ return ex == "MCX" ? QStringLiteral("MCX") : ex;
+ // E (equity/cash) and anything else → exchange unchanged.
+ return ex;
+}
+
+QString DhanInstrumentParser::normalise_symbol(const QString& trading_symbol, const QString& underlying,
+ const QString& itype, const QString& expiry_nd, double strike) {
+ if (itype == "INDEX")
+ return map_index_name(trading_symbol);
+ if (itype == "FUT") {
+ if (!underlying.isEmpty() && !expiry_nd.isEmpty())
+ return map_index_name(underlying) + expiry_nd + "FUT";
+ return trading_symbol.toUpper();
+ }
+ if (itype == "CE" || itype == "PE") {
+ if (!underlying.isEmpty() && !expiry_nd.isEmpty()) {
+ QString strike_str;
+ if (strike == static_cast<qint64>(strike))
+ strike_str = QString::number(static_cast<qint64>(strike));
+ else
+ strike_str = QString::number(strike, 'f', 2);
+ return map_index_name(underlying) + expiry_nd + strike_str + itype;
+ }
+ return trading_symbol.toUpper();
+ }
+ // EQ and everything else: trading_symbol as-is (already exchange-canonical, bare).
+ return trading_symbol.toUpper();
+}
+
+Instrument DhanInstrumentParser::parse_row(const QStringList& cols) {
+ if (cols.size() < 16)
+ return {};
+
+ const QString exch = cols[0].trimmed(); // NSE/BSE/MCX
+ const QString segment = cols[1].trimmed(); // E/D/C/M/I
+ const QString security_id = cols[2].trimmed(); // numeric
+ const QString instrument_name = cols[3].trimmed(); // EQUITY/INDEX/OPTSTK...
+ const QString trading_symbol = cols[5].trimmed();
+ const double lot = cols[6].toDouble();
+ const QString expiry_raw = cols[8].trimmed();
+ const double strike = cols[9].toDouble();
+ const QString option_type = cols[10].trimmed().toUpper();
+ const double tick = cols[11].toDouble();
+ const QString underlying = cols[15].trimmed().toUpper();
+
+ Instrument inst;
+ inst.broker_id = QStringLiteral("dhan");
+
+ bool token_ok = false;
+ const qint64 sid = security_id.toLongLong(&token_ok);
+ inst.instrument_token = token_ok ? sid : 0;
+ inst.exchange_token = inst.instrument_token;
+
+ const InstrumentType type = map_dhan_type(instrument_name, option_type);
+ QString itype_str;
+ switch (type) {
+ case InstrumentType::EQ: itype_str = "EQ"; break;
+ case InstrumentType::FUT: itype_str = "FUT"; break;
+ case InstrumentType::CE: itype_str = "CE"; break;
+ case InstrumentType::PE: itype_str = "PE"; break;
+ case InstrumentType::INDEX: itype_str = "INDEX"; break;
+ default: itype_str = "UNKNOWN"; break;
+ }
+
+ inst.brsymbol = trading_symbol;
+ inst.name = underlying.isEmpty() ? trading_symbol.toUpper() : underlying;
+ inst.brexchange = exch.toUpper();
+ inst.tick_size = tick > 0 ? tick : 0.05;
+ inst.lot_size = lot > 0 ? static_cast<int>(lot) : 1;
+ inst.strike = (type == InstrumentType::CE || type == InstrumentType::PE) ? strike : 0.0;
+
+ const QString expiry_nd = expiry_nodashes(expiry_raw);
+ if (!expiry_nd.isEmpty())
+ inst.expiry = expiry_nd.left(2) + "-" + expiry_nd.mid(2, 3) + "-" + expiry_nd.right(2); // "28-AUG-24"
+
+ inst.exchange = normalise_exchange(exch, segment, itype_str);
+ inst.instrument_type = type;
+ inst.symbol = normalise_symbol(trading_symbol, underlying, itype_str, expiry_nd, strike);
+
+ return inst;
+}
+
+QVector<Instrument> DhanInstrumentParser::parse(const QByteArray& csv_data) {
+ QVector<Instrument> results;
+ if (csv_data.isEmpty()) {
+ LOG_WARN("DhanParser", "Empty CSV data received");
+ return results;
+ }
+
+ QTextStream stream(csv_data);
+ QString header = stream.readLine();
+ Q_UNUSED(header)
+
+ int skipped = 0;
+ results.reserve(230000); // Dhan ships ~220k rows (full equity + F&O + commodity master)
+ while (!stream.atEnd()) {
+ const QString line = stream.readLine();
+ if (line.isEmpty())
+ continue;
+
+ // Dhan's CSV has exactly 16 fields and no quoted/comma values — simple split is safe.
+ const QStringList cols = line.split(',');
+ if (cols.size() < 16) {
+ ++skipped;
+ continue;
+ }
+
+ Instrument inst = parse_row(cols);
+ // Require a usable securityId + symbol + exchange.
+ if (inst.instrument_token <= 0 || inst.symbol.isEmpty() || inst.exchange.isEmpty()) {
+ ++skipped;
+ continue;
+ }
+ results.append(inst);
+ }
+
+ LOG_INFO("DhanParser",
+ QString("Parsed %1 instruments (skipped %2 malformed rows)").arg(results.size()).arg(skipped));
+ return results;
+}
+
+} // namespace fincept::trading
diff --git a/fincept-qt/src/trading/instruments/DhanInstrumentParser.h b/fincept-qt/src/trading/instruments/DhanInstrumentParser.h
new file mode 100644
index 000000000..001e94771
--- /dev/null
+++ b/fincept-qt/src/trading/instruments/DhanInstrumentParser.h
@@ -0,0 +1,48 @@
+#pragma once
+#include "trading/instruments/InstrumentTypes.h"
+
+#include <QByteArray>
+#include <QVector>
+
+namespace fincept::trading {
+
+/// Parses Dhan's scrip-master CSV (from
+/// https://images.dhan.co/api-data/api-scrip-master.csv) and normalises each row
+/// into an Instrument struct whose `instrument_token` carries Dhan's numeric
+/// securityId — which DhanBroker::lookup_security_id() reads back for orders,
+/// quotes, and historical data.
+///
+/// Dhan CSV columns (16 fields, in order):
+/// 0: SEM_EXM_EXCH_ID (NSE/BSE/MCX)
+/// 1: SEM_SEGMENT (E=equity, D=derivative, C=currency, M=commodity, I=index)
+/// 2: SEM_SMST_SECURITY_ID (numeric — used as instrument_token)
+/// 3: SEM_INSTRUMENT_NAME (EQUITY/INDEX/OPTSTK/OPTIDX/FUTSTK/FUTIDX/OPTFUT/FUTCOM/OPTCUR/FUTCUR)
+/// 4: SEM_EXPIRY_CODE 5: SEM_TRADING_SYMBOL 6: SEM_LOT_UNITS
+/// 7: SEM_CUSTOM_SYMBOL 8: SEM_EXPIRY_DATE ("YYYY-MM-DD HH:MM:SS" or blank)
+/// 9: SEM_STRIKE_PRICE (-0.01 sentinel for non-options) 10: SEM_OPTION_TYPE (CE/PE/XX)
+/// 11: SEM_TICK_SIZE 12: SEM_EXPIRY_FLAG 13: SEM_EXCH_INSTRUMENT_TYPE
+/// 14: SEM_SERIES 15: SM_SYMBOL_NAME (underlying)
+///
+/// Normalisation mirrors the other parsers so cross-broker lookups behave
+/// identically:
+/// EQ : symbol = trading_symbol (e.g. "RELIANCE")
+/// INDEX : symbol = map_index_name(trading_symbol); exchange = "<EX>_INDEX"
+/// FUT : symbol = underlying + DDMMMYY + "FUT"
+/// CE/PE : symbol = underlying + DDMMMYY + strike + CE/PE
+/// exchange: E→NSE/BSE, D→NFO/BFO, C→CDS/BCD, M→MCX, I→<EX>_INDEX
+class DhanInstrumentParser {
+ public:
+ /// Parse raw CSV bytes downloaded from the Dhan scrip-master URL.
+ /// Returns empty vector on parse failure (logged internally).
+ static QVector<Instrument> parse(const QByteArray& csv_data);
+
+ private:
+ static Instrument parse_row(const QStringList& cols);
+ static QString normalise_exchange(const QString& exch, const QString& segment, const QString& itype);
+ static QString normalise_symbol(const QString& trading_symbol, const QString& underlying, const QString& itype,
+ const QString& expiry_nd, double strike);
+ static QString expiry_nodashes(const QString& expiry_raw); // "2024-08-28 14:30:00" → "28AUG24"
+ static QString map_index_name(const QString& trading_symbol);
+};
+
+} // namespace fincept::trading
diff --git a/fincept-qt/src/trading/instruments/InstrumentRepository.cpp b/fincept-qt/src/trading/instruments/InstrumentRepository.cpp
index 7ec280ac1..d4d4e2dee 100644
--- a/fincept-qt/src/trading/instruments/InstrumentRepository.cpp
+++ b/fincept-qt/src/trading/instruments/InstrumentRepository.cpp
@@ -120,6 +120,8 @@ std::optional<Instrument> InstrumentRepository::find_by_brsymbol(const QString&
QVector<Instrument> InstrumentRepository::search(const QString& query, const QString& exchange,
const QString& broker_id, int limit) const {
QString pattern = "%" + query.toUpper() + "%";
+ // Exchange is stored upper-case; normalise the filter so "nse" matches. Empty = any.
+ const QString exch = exchange.toUpper();
QString sql = "SELECT instrument_token, exchange_token, symbol, brsymbol, name, exchange, brexchange, "
"expiry, strike, lot_size, instrument_type, tick_size, broker_id "
"FROM instruments "
@@ -129,7 +131,7 @@ QVector<Instrument> InstrumentRepository::search(const QString& query, const QSt
"ORDER BY CASE instrument_type "
" WHEN 'EQ' THEN 0 WHEN 'INDEX' THEN 1 WHEN 'FUT' THEN 2 ELSE 3 END, symbol "
"LIMIT ?";
- auto r = db().execute(sql, {broker_id, exchange, exchange, pattern, pattern, pattern, limit});
+ auto r = db().execute(sql, {broker_id, exch, exch, pattern, pattern, pattern, limit});
if (r.is_err())
return {};
QVector<Instrument> results;
@@ -168,4 +170,16 @@ int InstrumentRepository::count(const QString& broker_id) const {
return q.next() ? q.value(0).toInt() : 0;
}
+QDateTime InstrumentRepository::last_updated(const QString& broker_id) const {
+ auto r = db().execute("SELECT MAX(updated_at) FROM instruments WHERE broker_id = ?", {broker_id});
+ if (r.is_err())
+ return {};
+ auto& q = r.value();
+ if (!q.next() || q.value(0).isNull())
+ return {};
+ QDateTime dt = QDateTime::fromString(q.value(0).toString(), "yyyy-MM-dd HH:mm:ss");
+ dt.setTimeSpec(Qt::UTC); // updated_at is written as datetime('now') (UTC)
+ return dt;
+}
+
} // namespace fincept::trading
diff --git a/fincept-qt/src/trading/instruments/InstrumentRepository.h b/fincept-qt/src/trading/instruments/InstrumentRepository.h
index 8db1daac0..e8a1e5090 100644
--- a/fincept-qt/src/trading/instruments/InstrumentRepository.h
+++ b/fincept-qt/src/trading/instruments/InstrumentRepository.h
@@ -2,6 +2,8 @@
#include "storage/repositories/BaseRepository.h"
#include "trading/instruments/InstrumentTypes.h"
+#include <QDateTime>
+
#include <optional>
namespace fincept::trading {
@@ -43,6 +45,10 @@ class InstrumentRepository : public fincept::BaseRepository<Instrument> {
/// Count rows for a broker (quick freshness check).
int count(const QString& broker_id) const;
+ /// Most recent updated_at across this broker's instruments, or an invalid
+ /// QDateTime if no rows exist. Used for age-based refresh freshness checks.
+ QDateTime last_updated(const QString& broker_id) const;
+
/// Map a positioned QSqlQuery row to an Instrument. Public for use by async loaders.
static Instrument map_row_static(QSqlQuery& q);
diff --git a/fincept-qt/src/trading/instruments/InstrumentService.cpp b/fincept-qt/src/trading/instruments/InstrumentService.cpp
index fbacf186d..e9e895fc8 100644
--- a/fincept-qt/src/trading/instruments/InstrumentService.cpp
+++ b/fincept-qt/src/trading/instruments/InstrumentService.cpp
@@ -3,6 +3,7 @@
#include "core/logging/Logger.h"
#include "storage/sqlite/Database.h"
#include "trading/brokers/BrokerHttp.h"
+#include "trading/instruments/DhanInstrumentParser.h"
#include "trading/instruments/FyersInstrumentParser.h"
#include "trading/instruments/GrowwInstrumentParser.h"
#include "trading/instruments/InstrumentRepository.h"
@@ -212,6 +213,9 @@ void ensure_builtin_sources_registered() {
r.register_source({QStringLiteral("fyers"),
[](const BrokerCredentials&) { return InstrumentService::download_fyers_json(); },
[](const QByteArray& p) { return FyersInstrumentParser::parse(p); }});
+ r.register_source({QStringLiteral("dhan"),
+ [](const BrokerCredentials&) { return InstrumentService::download_dhan_csv(); },
+ [](const QByteArray& p) { return DhanInstrumentParser::parse(p); }});
return true;
}();
Q_UNUSED(registered);
@@ -228,7 +232,23 @@ InstrumentService& InstrumentService::instance() {
// ── Public API ────────────────────────────────────────────────────────────────
void InstrumentService::refresh(const QString& broker_id, const BrokerCredentials& creds, int max_age_hours) {
- Q_UNUSED(max_age_hours)
+ // Age-based freshness gate. max_age_hours <= 0 disables it (always download).
+ // Timestamp source: MAX(updated_at) on the instruments table (set on every
+ // full refresh via DELETE+INSERT).
+ if (max_age_hours > 0) {
+ const QDateTime last = InstrumentRepository::instance().last_updated(broker_id);
+ if (last.isValid()) {
+ const qint64 age_hours = last.secsTo(QDateTime::currentDateTimeUtc()) / 3600;
+ if (age_hours >= 0 && age_hours < max_age_hours) {
+ LOG_INFO("InstrumentService",
+ QString("%1 instruments are fresh (%2h old < %3h) — skipping download")
+ .arg(broker_id)
+ .arg(age_hours)
+ .arg(max_age_hours));
+ return;
+ }
+ }
+ }
// For AngelOne we need NSE equities to be present — check specifically for NSE count.
// A partial cache (e.g. only NFO/MCX) means a previous download was incomplete.
{
@@ -370,6 +390,43 @@ void InstrumentService::load_from_db_async(const QString& broker_id,
});
}
+void InstrumentService::load_from_db_worker(const QString& broker_id) {
+ // Synchronous DB load safe to call from a QtConcurrent worker thread. Opens a
+ // private named connection (never touches the shared main-thread connection)
+ // then build_cache (mutex-guarded). No-op if already loaded.
+ if (is_loaded(broker_id))
+ return;
+ const QString db_path = fincept::Database::instance().path();
+ const QString conn_name =
+ "inst_sync_" + broker_id + "_" + QUuid::createUuid().toString(QUuid::WithoutBraces);
+ QVector<Instrument> all;
+ {
+ QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", conn_name);
+ db.setDatabaseName(db_path);
+ if (db.open()) {
+ QSqlQuery q(db);
+ q.prepare("SELECT instrument_token, exchange_token, symbol, brsymbol, name, "
+ "exchange, brexchange, expiry, strike, lot_size, instrument_type, "
+ "tick_size, broker_id FROM instruments WHERE broker_id = ?");
+ q.addBindValue(broker_id);
+ if (q.exec()) {
+ while (q.next())
+ all.append(InstrumentRepository::map_row_static(q));
+ } else {
+ LOG_ERROR("InstrumentService", QString("load_from_db_worker: query failed for %1: %2")
+ .arg(broker_id, q.lastError().text()));
+ }
+ db.close();
+ } else {
+ LOG_ERROR("InstrumentService", QString("load_from_db_worker: failed to open DB for %1: %2")
+ .arg(broker_id, db.lastError().text()));
+ }
+ }
+ QSqlDatabase::removeDatabase(conn_name);
+ if (!all.isEmpty())
+ build_cache(broker_id, all);
+}
+
// ── Lookups ───────────────────────────────────────────────────────────────────
std::optional<qint64> InstrumentService::instrument_token(const QString& symbol, const QString& exchange,
@@ -378,7 +435,9 @@ std::optional<qint64> InstrumentService::instrument_token(const QString& symbol,
auto it = caches_.find(broker_id);
if (it == caches_.end())
return std::nullopt;
- auto jt = it->by_symbol.find({symbol, exchange});
+ // Cache keys are stored upper-case; normalise so callers passing e.g.
+ // "reliance"/"nse" still match.
+ auto jt = it->by_symbol.find({symbol.toUpper(), exchange.toUpper()});
if (jt == it->by_symbol.end())
return std::nullopt;
return jt->instrument_token;
@@ -390,7 +449,9 @@ std::optional<QString> InstrumentService::to_brsymbol(const QString& symbol, con
auto it = caches_.find(broker_id);
if (it == caches_.end())
return std::nullopt;
- auto jt = it->by_symbol.find({symbol, exchange});
+ // Cache keys are stored upper-case; normalise so callers passing e.g.
+ // "reliance"/"nse" still match.
+ auto jt = it->by_symbol.find({symbol.toUpper(), exchange.toUpper()});
if (jt == it->by_symbol.end())
return std::nullopt;
return jt->brsymbol;
@@ -402,7 +463,7 @@ std::optional<QString> InstrumentService::from_brsymbol(const QString& brsymbol,
auto it = caches_.find(broker_id);
if (it == caches_.end())
return std::nullopt;
- auto jt = it->by_brsymbol.find({brsymbol, brexchange});
+ auto jt = it->by_brsymbol.find({brsymbol.toUpper(), brexchange.toUpper()});
if (jt == it->by_brsymbol.end())
return std::nullopt;
return jt->symbol;
@@ -414,7 +475,9 @@ std::optional<Instrument> InstrumentService::find(const QString& symbol, const Q
auto it = caches_.find(broker_id);
if (it == caches_.end())
return std::nullopt;
- auto jt = it->by_symbol.find({symbol, exchange});
+ // Cache keys are stored upper-case; normalise so callers passing e.g.
+ // "reliance"/"nse" still match.
+ auto jt = it->by_symbol.find({symbol.toUpper(), exchange.toUpper()});
if (jt == it->by_symbol.end())
return std::nullopt;
return *jt;
@@ -764,6 +827,53 @@ QByteArray InstrumentService::download_groww_csv() {
return data;
}
+QByteArray InstrumentService::download_dhan_csv() {
+ // Public, unauthenticated scrip master (~27MB, 16-column CSV, no auth needed).
+ // Own QNAM (not BrokerHttp) so the long download doesn't starve concurrent
+ // quote/order calls — same rationale as download_groww_csv.
+ static const QString kUrl = "https://images.dhan.co/api-data/api-scrip-master.csv";
+
+ auto* nam = new QNetworkAccessManager;
+ QNetworkRequest req{QUrl(kUrl)};
+ req.setRawHeader("Accept", "text/csv");
+ req.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy);
+
+ QNetworkReply* reply = nam->get(req);
+
+ QEventLoop loop;
+ QTimer timer;
+ timer.setSingleShot(true);
+ QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
+ QObject::connect(&timer, &QTimer::timeout, &loop, &QEventLoop::quit);
+ timer.start(60000); // 60s — ~27MB file on slow connection
+ loop.exec();
+
+ auto drain = [&]() {
+ reply->deleteLater();
+ nam->deleteLater();
+ QCoreApplication::sendPostedEvents(nullptr, QEvent::DeferredDelete);
+ };
+
+ if (!timer.isActive()) {
+ reply->abort();
+ LOG_ERROR("InstrumentService", "Dhan scrip master download timed out");
+ drain();
+ return {};
+ }
+ timer.stop();
+
+ if (reply->error() != QNetworkReply::NoError) {
+ LOG_ERROR("InstrumentService", "Failed to download Dhan scrip master: " + reply->errorString());
+ drain();
+ return {};
+ }
+
+ QByteArray data = reply->readAll();
+ drain();
+ LOG_INFO("InstrumentService", QString("Downloaded Dhan scrip master: %1 bytes").arg(data.size()));
+ return data;
+}
+
QByteArray InstrumentService::download_fyers_json() {
static const QStringList urls = {
"https://public.fyers.in/sym_details/NSE_CM_sym_master.json",
diff --git a/fincept-qt/src/trading/instruments/InstrumentService.h b/fincept-qt/src/trading/instruments/InstrumentService.h
index cb87058aa..962424863 100644
--- a/fincept-qt/src/trading/instruments/InstrumentService.h
+++ b/fincept-qt/src/trading/instruments/InstrumentService.h
@@ -26,8 +26,8 @@ namespace fincept::trading {
/// auto sym = InstrumentService::instance().to_brsymbol("NIFTY28MAR24FUT", "NFO", "zerodha");
///
/// Thread safety: refresh() runs on a QtConcurrent worker thread and posts results
-/// back to the UI thread via QMetaObject::invokeMethod. All cache reads are
-/// guarded by a QReadWriteLock (reads concurrent, writes exclusive on refresh).
+/// back to the UI thread via QMetaObject::invokeMethod. All cache reads and writes
+/// are guarded by a single QMutex (mutex_); reads and writes are mutually exclusive.
class InstrumentService : public QObject {
Q_OBJECT
public:
@@ -53,6 +53,11 @@ class InstrumentService : public QObject {
void load_from_db_async(const QString& broker_id,
std::function<void(int)> callback = nullptr);
+ /// Synchronous DB load safe to call from a QtConcurrent worker thread.
+ /// Opens a private named QSqlDatabase connection (does NOT touch the shared
+ /// main-thread connection). Blocks until the cache is built. No-op if loaded.
+ void load_from_db_worker(const QString& broker_id);
+
// ── Lookups (synchronous, in-memory) ─────────────────────────────────────
/// symbol + exchange → numeric instrument_token (for get_history)
@@ -142,6 +147,7 @@ class InstrumentService : public QObject {
static QByteArray download_angel_master_json();
static QByteArray download_groww_csv();
static QByteArray download_fyers_json();
+ static QByteArray download_dhan_csv();
};
} // namespace fincept::trading
diff --git a/fincept-qt/src/ui/navigation/FKeyBar.cpp b/fincept-qt/src/ui/navigation/FKeyBar.cpp
index 2ab514cac..74b8e92e7 100644
--- a/fincept-qt/src/ui/navigation/FKeyBar.cpp
+++ b/fincept-qt/src/ui/navigation/FKeyBar.cpp
@@ -23,7 +23,7 @@ TabBar::TabBar(QWidget* parent) : QWidget(parent) {
{"dashboard", "DASHBOARD"}, {"markets", "MARKETS"}, {"crypto_trading", "CRYPTO"}, {"portfolio", "PORTFOLIO"},
{"news", "NEWS"}, {"ai_chat", "AI CHAT"}, {"backtesting", "BACKTEST"}, {"algo_trading", "ALGO"},
{"node_editor", "NODES"}, {"code_editor", "CODE"}, {"ai_quant_lab", "QUANT LAB"}, {"quantlib", "QUANTLIB"},
- {"forum", "FORUM"}, {"settings", "SETTINGS"}, {"profile", "PROFILE"},
+ {"settings", "SETTINGS"}, {"profile", "PROFILE"},
};
for (const auto& def : tab_defs_)
add_tab(def);
diff --git a/fincept-qt/src/ui/navigation/ToolBar.cpp b/fincept-qt/src/ui/navigation/ToolBar.cpp
index aea789933..7fcb69ad2 100644
--- a/fincept-qt/src/ui/navigation/ToolBar.cpp
+++ b/fincept-qt/src/ui/navigation/ToolBar.cpp
@@ -166,8 +166,11 @@ void ToolBar::retranslateUi() {
if (live_label_) live_label_->setText(tr(" LIVE"));
if (plan_btn_) plan_btn_->setToolTip(tr("View Plans & Pricing"));
if (chat_mode_btn_) {
- // Keep the ⬡ glyph as a visual icon; only the label after it translates.
- chat_mode_btn_->setText(QStringLiteral("\xe2\xac\xa1 ") + tr("CHAT"));
+ // Keep the ⬡ glyph (U+2B21) as a visual icon; only the label after it
+ // translates. Must use fromUtf8 to decode the UTF-8 bytes — wrapping
+ // them in QStringLiteral widens each byte into its own UTF-16 unit and
+ // renders as mojibake ("⬡").
+ chat_mode_btn_->setText(QString::fromUtf8("\xe2\xac\xa1 ") + tr("CHAT"));
chat_mode_btn_->setToolTip(tr("Switch to Chat Mode (F9)"));
}
if (logout_btn_) logout_btn_->setText(tr("LOGOUT"));