Formulas
Formulas let you create custom calculated series from chart metrics. Combine, transform, and analyze metrics using arithmetic operators and built-in functions.
Adding a Formula
- Open a chart in Edit mode
- Click the + Add button, then select Add Formula
- Enter a label (e.g., "7-day SMA of Price")
- Enter an expression (e.g.,
sma(m1, 7)) - Click Add Formula
The formula result appears as a new series on the chart with its own color and styling.
Syntax
Metric and Formula References
m1,m2, ... reference chart metrics by position (first metric = m1)f1,f2, ... reference earlier formulas by position (first formula = f1)
Formulas are evaluated in order, so f2 can reference f1 but not the other way around.
Arithmetic Operators
Standard operators with normal precedence (* and / before + and -):
m1 + m2 # Addition
m1 - m2 # Subtraction
m1 * m2 # Multiplication
m1 / m2 # Division (returns null if divisor is 0)
(m1 + m2) * m3 # Parentheses for grouping
Numbers and Horizontal Lines
Constant values can be used in expressions. A standalone number draws a horizontal line at that value:
42000 # Horizontal line at 42,000
m1 * 100 # Scale a metric
m1 / 1000000 # Convert to millions
0.5 # Horizontal line at 0.5 (useful as threshold)
Functions
Moving Averages & Rolling Statistics
| Function | Syntax | Description |
|---|---|---|
sma | sma(series, period) | Simple Moving Average — arithmetic mean over the trailing N data points |
ema | ema(series, period) | Exponential Moving Average — EMA_t = value_t × k + EMA_(t-1) × (1 - k), where k = 2 / (period + 1). Gives more weight to recent values |
median | median(series, period) | Rolling median (middle value) over N periods |
sum | sum(series, period) | Rolling sum over the trailing N periods |
std | std(series, period) | Rolling standard deviation over N periods |
Cumulative Functions
Expanding-window functions that use all data from the start up to each point:
| Function | Syntax | Description |
|---|---|---|
cumsum | cumsum(series) | Expanding cumulative sum from start of data |
cummean | cummean(series) | Expanding cumulative mean from start of data |
cummedian | cummedian(series) | Expanding cumulative median from start of data |
cumstd | cumstd(series) | Expanding cumulative standard deviation from start of data |
cummax | cummax(series) | Cumulative all-time maximum up to each point |
cummin | cummin(series) | Cumulative all-time minimum up to each point |
Change Functions
| Function | Syntax | Description |
|---|---|---|
percent_change | percent_change(series, period) | Percentage change over N periods. Values are returned as decimals (e.g., 0.20 = +20% growth) |
diff | diff(series, period) | Absolute value change over N periods: value_t - value_(t-N) |
Math Functions
| Function | Syntax | Description |
|---|---|---|
abs | abs(series) | Absolute value of all data points |
pow | pow(series, n) | Raise all data points to the power n |
log | log(series) | Base-10 logarithm (returns null for non-positive values) |
round | round(series, digits) | Round values to N decimal places |
max | max(a, b, ...) | Pointwise maximum — returns the highest value among all arguments at each data point. Arguments can be series or constants, e.g. max(m1, m2, 0) |
min | min(a, b, ...) | Pointwise minimum — returns the lowest value among all arguments at each data point. Arguments can be series or constants, e.g. min(m1, m2, 100) |
Technical Indicators
| Function | Syntax | Description |
|---|---|---|
rsi | rsi(series, period) | Relative Strength Index (0–100) using Wilder's smoothing method over N periods |
corr | corr(series1, series2, period) | Pearson's correlation coefficient between two series over a trailing window of N periods. Returns values from -1 (inverse) to +1 (perfectly correlated) |
drawdown | drawdown(series) | Relative drawdown from the all-time high. Returns negative decimals (e.g., -0.30 = 30% below ATH) |
Risk & Return
| Function | Syntax | Description |
|---|---|---|
mean_return | mean_return(series, period) | Annualized rolling mean return over N periods (based on daily log returns × 365) |
realized_vol | realized_vol(series, period) | Annualized realized volatility over N periods (daily log return std dev × √365) |
sharpe_ratio_arithmetic | sharpe_ratio_arithmetic(series, period) | Annualized Sharpe ratio using arithmetic mean of returns over N periods |
sharpe_ratio_geometric | sharpe_ratio_geometric(series, period) | Annualized Sharpe ratio using geometric mean of returns over N periods |
Series Manipulation
| Function | Syntax | Description |
|---|---|---|
shift | shift(series, period) | Shift the series right by N periods. Positive period shows past values at the current position (i.e., each data point displays the value from N periods earlier). Negative period shifts left (shows future values) |
if | if(a, "op", b, then, else) | Conditional: evaluates the comparison a op b at each data point, returns then if true, else if false. The op argument is a comparison operator passed as a quoted string: "=", "!=", ">", ">=", "<", "<=" |
Examples
Moving Averages
sma(m1, 7) # 7-day simple moving average of first metric
sma(m1, 30) # 30-day SMA
ema(m1, 21) # 21-day exponential moving average
median(m1, 14) # 14-day rolling median
SMA Crossover Detection
f1: sma(m1, 7) # Short-term SMA
f2: sma(m1, 30) # Long-term SMA
f3: f1 - f2 # Difference (positive = short above long)
Ratio Analysis
m1 / m2 # Ratio between two metrics
Drawdown from All-Time High
drawdown(m1) # Drawdown as negative decimal (-0.30 = 30% below ATH)
Bollinger Bands
f1: sma(m1, 20) # Middle band
f2: f1 + 2 * std(m1, 20) # Upper band (+2 standard deviations)
f3: f1 - 2 * std(m1, 20) # Lower band (-2 standard deviations)
Period-over-Period Change
percent_change(m1, 7) # 7-day percentage change (decimal)
diff(m1, 30) # 30-day absolute change
RSI
rsi(m1, 14) # 14-period RSI
30 # Oversold threshold line
70 # Overbought threshold line
Clamping Values
max(m1, 0) # Floor at zero (remove negative values)
min(m1, 100) # Cap at 100
max(m1, m2) # Higher of two metrics at each point
Correlation
corr(m1, m2, 30) # 30-day rolling correlation between two metrics
Volatility & Risk
realized_vol(m1, 30) # 30-day annualized volatility
sharpe_ratio_arithmetic(m1, 90) # 90-day annualized Sharpe ratio
Styling
After adding a formula, click on it in the legend (in Edit mode) to configure:
- Chart style: Line, Area, or Bar
- Color: Pick from presets or custom
- Y-axis: Assign to any axis
- Line width and fill opacity
- Visibility: Show/hide toggle
Persistence
Formulas are saved with the chart configuration. When you save a chart to "My Charts", all formulas are preserved and restored when you reload the chart.
Export
Formula values are included in CSV and JSON exports. Each formula column uses the formula label as its header.