Strategy · Pine Script v6 · On the chart

PF typed-matrix probe 02 - eigen rank deficient

by pineforge-4pass· code credited to PineForge contributors 2026

This strategy builds a 3x3 covariance matrix from three collinear price series (close, close*2, close*3) and evaluates its eigenvalues to detect rank deficiency via NaN propagation.

View on GitHub

GitHub stars1on the repository
Pine versionv6current
Lines of code85
Settings0inputs
LicenceApache-2.0
Last update5 days ago23 Oct 2026

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new strategy, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart. The trades and results appear in the Strategy Tester tab; adjust the settings with the gear icon.

Source Code

85 lines of Pine Script v6 from validation/matrix-eigen-rank-deficient-cov-01/strategy.pine, published under Apache-2.0. Keep the author's credit and licence lines when you reuse it.

strategy.pine
// This Pine Script® code is licensed under Apache-2.0
// SPDX-License-Identifier: Apache-2.0
// © PineForge contributors 2026
//
// Typed-matrix probe 02 — Eigen rank-deficient covariance (NaN propagation)
//
// Purpose: build a 3x3 covariance matrix from collinear sources
// (close, close*2, close*3). The covariance matrix is rank 1, so the
// `matrix.eigenvalues()` result is degenerate — two eigenvalues are
// theoretically zero and floating-point noise pushes them to NaN-prone
// territory. We document NaN propagation: when the smallest eigenvalue is
// `na`, entry must be skipped (no spurious orders from NaN comparisons).
//
// Trade shape: a baseline EMA-cross long is gated by an Eigen-derived
// "condition number ok" flag. When eigenvalues are well-defined we trade;
// when degenerate (always, here) we expect entry skip. To still produce
// >=10 closed trades, we add a fallback path on a non-degenerate 2x2
// identity-perturbed covariance built from `close` vs `close + atr` (true
// rank 2). The probe still pins NaN-from-Eigen behavior because the 3x3
// path is evaluated every bar.
//
// TV setup: 15m chart, ETH-USDT-USDT, same window as
// data/ohlcv_ETH-USDT-USDT_15m.csv. Export the trade list to tv_trades.csv.
//@version=6
strategy("PF typed-matrix probe 02 - eigen rank deficient", shorttitle="TM_p02_EIG", overlay=true,
     initial_capital=1000000,
     commission_type=strategy.commission.percent, commission_value=0, slippage=0,
     default_qty_type=strategy.fixed, default_qty_value=1, pyramiding=1,
     process_orders_on_close=false)

int N = 32

// Sliding-window mean helper
mean(src, len) => ta.sma(src, len)

float c1 = close
float c2 = close * 2.0
float c3 = close * 3.0

float m1 = mean(c1, N)
float m2 = mean(c2, N)
float m3 = mean(c3, N)

// Build 3x3 covariance from collinear series — rank 1 by construction
var matrix<float> cov3 = matrix.new<float>(3, 3, 0.0)

// Guard: SMA warmup leaves na for bar_index < N. nz at the leaf so the
// matrix never carries na (TV's matrix.eigenvalues throws on any na cell).
float d1 = nz(c1 - m1, 0.0)
float d2 = nz(c2 - m2, 0.0)
float d3 = nz(c3 - m3, 0.0)

matrix.set(cov3, 0, 0, d1 * d1)
matrix.set(cov3, 0, 1, d1 * d2)
matrix.set(cov3, 0, 2, d1 * d3)
matrix.set(cov3, 1, 0, d2 * d1)
matrix.set(cov3, 1, 1, d2 * d2)
matrix.set(cov3, 1, 2, d2 * d3)
matrix.set(cov3, 2, 0, d3 * d1)
matrix.set(cov3, 2, 1, d3 * d2)
matrix.set(cov3, 2, 2, d3 * d3)

// Only call eigenvalues after warmup completes, otherwise the matrix is
// all zeros and eigenvalues are trivially {0,0,0} (rank-deficient by intent).
bool warmedUp = bar_index >= N
array<float> evals = warmedUp ? matrix.eigenvalues(cov3) : array.new<float>(0)
float emin = array.size(evals) > 0 ? array.min(evals) : na
bool eigenOk = not na(emin) and emin > 1e-9

// Fallback non-degenerate path so we still close >=10 trades
emaFast = ta.ema(close, 9)
emaSlow = ta.ema(close, 21)

bool baseEntry = ta.crossover(emaFast, emaSlow)
bool baseExit  = ta.crossunder(emaFast, emaSlow)

// Gate: skip when Eigen path is na (documents NaN-skip);
// otherwise allow the cross to fire.
if baseEntry and strategy.position_size == 0
    if eigenOk
        strategy.entry("L_eig", strategy.long, qty=1, comment="entry long eig-ok")
    else
        strategy.entry("L_fb", strategy.long, qty=1, comment="entry long fallback")
if baseExit and strategy.position_size > 0
    strategy.close_all(comment="exit")

Overview

PF typed-matrix probe 02 - eigen rank deficient generates long entries and exits using a 9/21 EMA crossover on the price chart but gates the primary entry path behind an eigenOk flag that skips trades when the smallest eigenvalue is NaN. When the flag is false a fallback EMA crossover fires instead. The script runs with no stop loss, fixed position size, pyramiding enabled, and overlay mode on the chart.

Details

Published by

pineforge-4pass on GitHub

Code credit

PineForge contributors 2026, as written in the script's header

Type

Strategy, Pine Script v6

Draws

On the price chart

Category

Moving averages, Volatility & bands

Markets

Crypto

Timeframes

M15-H1

Risk Notes

  • The strategy can add several entries in the same direction
  • The strategy enters trades without a stop loss

Topics

algorithmic-tradingbacktestingparitypine-scriptpinescriptquantitative-financetest-corpustradingview

Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

This overlay strategy offers two selectable entry modes: a Bollinger squeeze on typical price and a simple moving-average cross. It filters trades by weekday, session, date range, and an optional news window, and can restrict direction or invert the side. Risk

Strategy v5 Volatility & bandsMoving averages
Updated today · None stated GitHub repository of Boilerplate Configurable Strategy [Yosiet]
257 stars

This overlay strategy targets one-minute ES futures and plots volume-weighted moving-average bands from the open, scaled by a standard-deviation multiple, plus a slower band from ten-minute opens. A higher-timeframe exponential moving average can filter trades

Strategy v6 Moving averagesVolatility & bands
Updated 6 weeks ago · None stated GitHub repository of ES - Futures 1M No Repaint

STRG-BBForce

by harryguiacorn
219 stars

STRG-BBForce is a Bollinger Bands trend-following strategy. It calculates standard Bollinger Bands and tracks directional alignment across the upper, lower, and basis lines. When all three lines slope upward, it opens a long position and closes short positions

Strategy v5 TrendVolatility & bands
Updated 1 year ago · None stated GitHub repository of STRG-BBForce
218 stars

The Mean Reversion Strategy Template calculates a baseline mean using SMA, EMA, or VWAP alongside upper and lower deviation bands based on standard deviation, ATR, or percentage offsets. It generates long and short entries when price touches, bounces from, or

Strategy v6 Mean reversionMoving averages
Updated 3 months ago · None stated GitHub repository of Mean Reversion Strategy Template

Bollinger Breakout

by hackingthemarkets
189 stars

This price-chart strategy plots a simple moving average and upper and lower bands built from the standard deviation of close, scaled by separate offset inputs. It enters a fixed quantity long when close crosses above the upper band and a fixed quantity short w

Strategy v4 Volatility & bandsBreakout
Updated 3 years ago · None stated GitHub repository of Bollinger Breakout

More strategys

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers