Skip to main content

BackDB Queries

The backdb database provides access to backtest results stored in a relational database. This database contains comprehensive information about each backtest, including metadata, configuration parameters, and performance metrics.

Database Schema

The main table available for querying is backtest_results, which contains information about each backtest run.

backtest_results Schema
Field NameDescriptionData TypeOptional
idUnique identifier for the backtestVARCHAR(40)No
user_idIdentifier of the user who created the backtestVARCHAR(40)No
created_atTimestamp when backtest was createdDATETIMENo
started_atTimestamp when backtest execution startedDATETIMEYes
finished_atTimestamp when backtest execution completedDATETIMEYes
delete_modeDeletion type if backtest was deletedVARCHAR(32)Yes
deleted_atTimestamp when backtest was deletedDATETIMEYes
sharedWhether the backtest is shared with other usersBOOLEANYes
stateCurrent status of the backtestVARCHAR(128)Yes
nameUser-defined name of the backtestVARCHAR(250)Yes
strategy_nameName of the strategy usedVARCHAR(128)Yes
strategy_definitionComplete configuration of the strategy definitionJSONNo
failure_reasonDescription of failure if backtest failedTEXTYes
start_periodStart date of the backtest periodDATETIMEYes
end_periodEnd date of the backtest periodDATETIMEYes
strat_trade_cntTotal number of trades executedNUMERICYes
strat_win_cntNumber of winning tradesNUMERICYes
strat_loss_cntNumber of losing tradesNUMERICYes
strat_adjustment_cntNumber of trade adjustmentsNUMERICYes
strat_pt_hit_cntNumber of profit target hitsNUMERICYes
strat_sl_hit_cntNumber of stop loss hitsNUMERICYes
strat_max_dit_reached_cntNumber of trades that reached maximum days in tradeNUMERICYes
strat_settlement_cntNumber of trades settled at expirationNUMERICYes
strat_avg_days_in_tradeAverage number of days positions were heldDECIMAL(15,4)Yes
strat_alphaAlpha (excess return) of strategyDECIMAL(15,4)Yes
strat_betaBeta (market correlation) of strategyDECIMAL(15,4)Yes
strat_cagr_pctCompound Annual Growth Rate percentageDECIMAL(15,4)Yes
strat_cum_retCumulative return over entire periodDECIMAL(15,4)Yes
strat_max_ddMaximum drawdown percentageDECIMAL(15,4)Yes
strat_avg_ddAverage drawdown percentageDECIMAL(15,4)Yes
strat_avg_dd_daysAverage drawdown duration in daysDECIMAL(15,4)Yes
strat_avg_up_monthAverage return in positive monthsDECIMAL(15,4)Yes
strat_avg_down_monthAverage return in negative monthsDECIMAL(15,4)Yes
strat_longest_dd_daysLongest drawdown duration in daysNUMERICYes
strat_inform_ratioInformation ratioDECIMAL(15,4)Yes
strat_sharpeSharpe ratio (risk-adjusted return)DECIMAL(15,4)Yes
strat_prob_sharpeProbabilistic Sharpe ratioDECIMAL(15,4)Yes
strat_smart_sharpeSmart Sharpe ratioDECIMAL(15,4)Yes
strat_sortinoSortino ratio (downside risk-adjusted return)DECIMAL(15,4)Yes
strat_smart_sortinoSmart Sortino ratioDECIMAL(15,4)Yes
strat_treynorTreynor ratioDECIMAL(15,4)Yes
strat_calmarCalmar ratio (return to max drawdown)DECIMAL(15,4)Yes
strat_omegaOmega ratioDECIMAL(15,4)Yes
strat_r_squaredR-squared (correlation with benchmark)DECIMAL(15,4)Yes
strat_profit_factorRatio of gross profits to gross lossesDECIMAL(15,4)Yes
strat_kelly_criterionKelly criterion (optimal position sizing)DECIMAL(15,4)Yes
strat_ann_volatilityAnnualized volatilityDECIMAL(15,4)Yes
bm_cagr_pctBenchmark Compound Annual Growth Rate percentageDECIMAL(15,4)Yes
bm_cum_retBenchmark cumulative returnDECIMAL(15,4)Yes
bm_max_ddBenchmark maximum drawdown percentageDECIMAL(15,4)Yes
bm_avg_ddBenchmark average drawdown percentageDECIMAL(15,4)Yes
bm_avg_dd_daysBenchmark average drawdown duration in daysDECIMAL(15,4)Yes
bm_avg_up_monthBenchmark average return in positive monthsDECIMAL(15,4)Yes
bm_avg_down_monthBenchmark average return in negative monthsDECIMAL(15,4)Yes
bm_longest_dd_daysBenchmark longest drawdown duration in daysNUMERICYes
bm_inform_ratioBenchmark information ratioDECIMAL(15,4)Yes
bm_sharpeBenchmark Sharpe ratioDECIMAL(15,4)Yes
bm_prob_sharpeBenchmark probabilistic Sharpe ratioDECIMAL(15,4)Yes
bm_smart_sharpeBenchmark smart Sharpe ratioDECIMAL(15,4)Yes
bm_sortinoBenchmark Sortino ratioDECIMAL(15,4)Yes
bm_smart_sortinoBenchmark smart Sortino ratioDECIMAL(15,4)Yes
bm_treynorBenchmark Treynor ratioDECIMAL(15,4)Yes
bm_calmarBenchmark Calmar ratioDECIMAL(15,4)Yes
bm_omegaBenchmark Omega ratioDECIMAL(15,4)Yes
bm_r_squaredBenchmark R-squaredDECIMAL(15,4)Yes
bm_profit_factorBenchmark profit factorDECIMAL(15,4)Yes
bm_kelly_criterionBenchmark Kelly criterionDECIMAL(15,4)Yes
bm_ann_volatilityBenchmark annualized volatilityDECIMAL(15,4)Yes

For AI-Assisted Queries please post the following schema as context to the AI Agents:

CREATE TABLE IF NOT EXISTS backtest_results (
id VARCHAR(40) NOT NULL,
user_id VARCHAR(40) NOT NULL,
created_at DATETIME NOT NULL,
started_at DATETIME,
finished_at DATETIME,
delete_mode VARCHAR(32),
deleted_at DATETIME,
shared BOOLEAN DEFAULT FALSE,
state VARCHAR(128),
name VARCHAR(250),
strategy_name VARCHAR(128),
strategy_definition JSON NOT NULL,
failure_reason TEXT,
start_period DATETIME,
end_period DATETIME,
strat_trade_cnt NUMERIC,
strat_win_cnt NUMERIC,
strat_loss_cnt NUMERIC,
strat_adjustment_cnt NUMERIC,
strat_pt_hit_cnt NUMERIC,
strat_sl_hit_cnt NUMERIC,
strat_max_dit_reached_cnt NUMERIC,
strat_settlement_cnt NUMERIC,
strat_avg_days_in_trade DECIMAL(15,4),
strat_alpha DECIMAL(15, 4),
strat_beta DECIMAL(15, 4),
strat_cagr_pct DECIMAL(15, 4),
strat_cum_ret DECIMAL(15, 4),
strat_max_dd DECIMAL(15, 4),
strat_avg_dd DECIMAL(15, 4),
strat_avg_dd_days DECIMAL(15, 4),
strat_avg_up_month DECIMAL(15, 4),
strat_avg_down_month DECIMAL(15, 4),
strat_longest_dd_days NUMERIC,
strat_inform_ratio DECIMAL(15, 4),
strat_sharpe DECIMAL(15, 4),
strat_prob_sharpe DECIMAL(15, 4),
strat_smart_sharpe DECIMAL(15, 4),
strat_sortino DECIMAL(15, 4),
strat_smart_sortino DECIMAL(15, 4),
strat_treynor DECIMAL(15, 4),
strat_calmar DECIMAL(15, 4),
strat_omega DECIMAL(15, 4),
strat_r_squared DECIMAL(15, 4),
strat_profit_factor DECIMAL(15, 4),
strat_kelly_criterion DECIMAL(15, 4),
strat_ann_volatility DECIMAL(15, 4),
bm_cagr_pct DECIMAL(15, 4),
bm_cum_ret DECIMAL(15, 4),
bm_max_dd DECIMAL(15, 4),
bm_avg_dd DECIMAL(15, 4),
bm_avg_dd_days DECIMAL(15, 4),
bm_avg_up_month DECIMAL(15, 4),
bm_avg_down_month DECIMAL(15, 4),
bm_longest_dd_days NUMERIC,
bm_inform_ratio DECIMAL(15, 4),
bm_sharpe DECIMAL(15, 4),
bm_prob_sharpe DECIMAL(15, 4),
bm_smart_sharpe DECIMAL(15, 4),
bm_sortino DECIMAL(15, 4),
bm_smart_sortino DECIMAL(15, 4),
bm_treynor DECIMAL(15, 4),
bm_calmar DECIMAL(15, 4),
bm_omega DECIMAL(15, 4),
bm_r_squared DECIMAL(15, 4),
bm_profit_factor DECIMAL(15, 4),
bm_kelly_criterion DECIMAL(15, 4),
bm_ann_volatility DECIMAL(15, 4)
);
note

The performance metrics are calculated using our fork of QuantStats

Example Queries

Basic Query - Strategy Performance Metrics

This query retrieves basic strategy performance metrics ordered by Sharpe ratio:

select 
created_at,
name,
strategy_name,
strat_sharpe,
strat_max_dd,
strat_cagr_pct
from backtest_results
order by strat_sharpe desc;

Advanced Query - Strategy Definition Analysis

The strategy_definition column contains the complete MesoSim Strategy Definition as JSON. You can query directly into this JSON structure to analyze how specific parameters affect performance:

with cte as (
select
id,
CAST(JSON_EXTRACT(strategy_definition, '$.Structure.Expirations[0].DTE') AS float) AS exp1,
CAST(JSON_EXTRACT(strategy_definition, '$.Exit.MaxDaysInTrade') AS float) AS MaxDaysInTrade,
strat_sharpe,
strat_cagr_pct,
strat_max_dd
from backtest_results
where 1 = 1
AND strategy_name = "MyStrategy"
),
aggregates as (
select
MaxDaysInTrade,
count(1) as run_cnt,
avg(strat_sharpe) as avg_strat_sharpe,
min(strat_sharpe) as min_strat_sharpe,
max(strat_sharpe) as max_strat_sharpe,
avg(strat_cagr_pct) as avg_strat_cagr_pct,
avg(strat_max_dd) as avg_strat_max_dd,
min(strat_max_dd) as min_strat_max_dd,
max(strat_max_dd) as max_strat_max_dd
from cte
group by MaxDaysInTrade
),
final as (
select
*,
ntile(10) over (order by avg_strat_sharpe) as sharpe_bin
from aggregates
)
select *
from final
order by avg_strat_sharpe;

This advanced query:

  1. Extracts parameters from the MesoSim Strategy Definition
  2. Allows filtering by template name and DTE (Days To Expiration) range
  3. Aggregates results by MaxDaysInTrade parameter
  4. Calculates averages, minimums, and maximums for key performance metrics
  5. Adds a percentile ranking for sharpe ratio

Query Tips

  1. Use JSON_EXTRACT with CAST to query nested parameters in the MesoSim Strategy Definition (strategy_definition) field
  2. You can add Metabase Dashboard Filter parameters (enclosed in double square brackets) for interactive queries
  3. Aggregate results to identify trends across similar configurations
  4. Use window functions like NTILE for percentile rankings
  5. Include both performance metrics and strategy parameters to identify optimal configurations