Skip to main content

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

  1. Open a chart in Edit mode
  2. Click the + Add button, then select Add Formula
  3. Enter a label (e.g., "7-day SMA of Price")
  4. Enter an expression (e.g., sma(m1, 7))
  5. 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

FunctionSyntaxDescription
smasma(series, period)Simple Moving Average — arithmetic mean over the trailing N data points
emaema(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
medianmedian(series, period)Rolling median (middle value) over N periods
sumsum(series, period)Rolling sum over the trailing N periods
stdstd(series, period)Rolling standard deviation over N periods

Cumulative Functions

Expanding-window functions that use all data from the start up to each point:

FunctionSyntaxDescription
cumsumcumsum(series)Expanding cumulative sum from start of data
cummeancummean(series)Expanding cumulative mean from start of data
cummediancummedian(series)Expanding cumulative median from start of data
cumstdcumstd(series)Expanding cumulative standard deviation from start of data
cummaxcummax(series)Cumulative all-time maximum up to each point
cummincummin(series)Cumulative all-time minimum up to each point

Change Functions

FunctionSyntaxDescription
percent_changepercent_change(series, period)Percentage change over N periods. Values are returned as decimals (e.g., 0.20 = +20% growth)
diffdiff(series, period)Absolute value change over N periods: value_t - value_(t-N)

Math Functions

FunctionSyntaxDescription
absabs(series)Absolute value of all data points
powpow(series, n)Raise all data points to the power n
loglog(series)Base-10 logarithm (returns null for non-positive values)
roundround(series, digits)Round values to N decimal places
maxmax(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)
minmin(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

FunctionSyntaxDescription
rsirsi(series, period)Relative Strength Index (0–100) using Wilder's smoothing method over N periods
corrcorr(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)
drawdowndrawdown(series)Relative drawdown from the all-time high. Returns negative decimals (e.g., -0.30 = 30% below ATH)

Risk & Return

FunctionSyntaxDescription
mean_returnmean_return(series, period)Annualized rolling mean return over N periods (based on daily log returns × 365)
realized_volrealized_vol(series, period)Annualized realized volatility over N periods (daily log return std dev × √365)
sharpe_ratio_arithmeticsharpe_ratio_arithmetic(series, period)Annualized Sharpe ratio using arithmetic mean of returns over N periods
sharpe_ratio_geometricsharpe_ratio_geometric(series, period)Annualized Sharpe ratio using geometric mean of returns over N periods

Series Manipulation

FunctionSyntaxDescription
shiftshift(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)
ifif(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.