Strategy · Pine Script v6 · On the chart

PineForge — Eigen Matrix Covariance

by pineforge-4pass· code credited to PineForge

This strategy models market regimes using principal variance concentration between price and volume.

View on GitHub

GitHub stars1on the repository
Pine versionv6current
Lines of code48
Settings5inputs
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

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

strategy.pine
//@version=6
// SPDX-License-Identifier: Apache-2.0
// Copyright 2026 PineForge
// PineForge Owned Strategy: Eigen Matrix Covariance
// Principal variance concentration classifies a two-factor market regime.
strategy("PineForge — Eigen Matrix Covariance", overlay=true,
     initial_capital=100000,
     default_qty_type=strategy.fixed, default_qty_value=2,
     pyramiding=0, commission_type=strategy.commission.percent,
     commission_value=0.05, slippage=1,
     margin_long=100, margin_short=100,
     process_orders_on_close=false, calc_on_order_fills=false)

covarianceLength = input.int(26, "Covariance Length", minval=5, group="Factors")
trendLength = input.int(58, "Trend EMA Length", minval=5, group="Trend")
minimumConcentration = input.float(1.28, "Minimum Principal Eigenvalue", minval=1.0, maxval=2.0, step=0.01, group="Factors")
atrLength = input.int(19, "ATR Length", minval=2, group="Risk")
maximumAtrLoss = input.float(2.6, "Maximum ATR Loss", minval=0.5, step=0.1, group="Risk")

priceReturn = ta.roc(close, 1)
volumeReturn = ta.roc(volume, 1)
factorCorrelation = ta.correlation(priceReturn, volumeReturn, covarianceLength)
safeCorrelation = nz(factorCorrelation, 0.0)

var matrix<float> correlationMatrix = matrix.new<float>(2, 2, 0.0)
correlationMatrix.set(0, 0, 1.0)
correlationMatrix.set(0, 1, safeCorrelation)
correlationMatrix.set(1, 0, safeCorrelation)
correlationMatrix.set(1, 1, 1.0)

eigenValues = correlationMatrix.eigenvalues()
firstEigenvalue = array.size(eigenValues) > 0 ? array.get(eigenValues, 0) : na
secondEigenvalue = array.size(eigenValues) > 1 ? array.get(eigenValues, 1) : na
principalEigenvalue = math.max(firstEigenvalue, secondEigenvalue)
trendLine = ta.ema(close, trendLength)
atrValue = ta.atr(atrLength)

enterLong = not na(principalEigenvalue) and ta.crossover(principalEigenvalue, minimumConcentration) and close > trendLine
regimeEnded = principalEigenvalue < 1.08 or close < trendLine
riskExceeded = strategy.position_size > 0 and close < strategy.position_avg_price - atrValue * maximumAtrLoss

if strategy.position_size == 0 and enterLong
    strategy.entry("Eigen Long", strategy.long)
else if strategy.position_size > 0 and (regimeEnded or riskExceeded)
    strategy.close("Eigen Long", comment=riskExceeded ? "ATR risk" : "Eigen regime")

plot(trendLine, "Trend EMA", color=color.orange)
plot(principalEigenvalue, "Principal Eigenvalue", color=color.purple, display=display.data_window)

Overview

PineForge — Eigen Matrix Covariance builds a 2x2 correlation matrix from price and volume rate-of-change, then calculates its eigenvalues. It enters long positions when the principal eigenvalue crosses above a threshold while price is above an EMA trend line. Positions exit when the eigenvalue regime deteriorates, price falls below the EMA, or loss exceeds an ATR multiplier. It plots the trend EMA on the chart and outputs the eigenvalue to the data window.

Details

Published by

pineforge-4pass on GitHub

Code credit

PineForge, as written in the script's header

Type

Strategy, Pine Script v6

Draws

On the price chart

Category

Trend, Volume, Volatility & bands

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

  • The strategy enters trades without a stop loss

Topics

algorithmic-tradingbacktestingparitypine-scriptpinescriptquantitative-financetest-corpustradingview

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
Covariance Lengthint26covarianceLength
Trend EMA Lengthint58trendLength
Minimum Principal Eigenvaluefloat1.28minimumConcentration
ATR Lengthint19atrLength
Maximum ATR Lossfloat2.6maximumAtrLoss
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

106 stars

This Pine v4 strategy draws on the price chart and filters breakouts with a fixed Supertrend, ATR length, volume moving-average markup, and moving-average alignment, plus short and long high-low periods. Trade direction defaults to long and can include shorts.

Strategy v4 BreakoutTrend
Updated 2 years ago · GPL-3.0 GitHub repository of Breakout Trend Trading - V1

This strategy enters long or short when eight conditions align over the last two bars. It requires EMA 10 and EMA 55 alignment on the one-hour, four-hour and daily charts, a positive ADX slope, matching monitor direction, and price relative to a 400-bar point

Strategy v5 TrendMoving averages
Updated 1 year ago · None stated GitHub repository of Ruckard ABCBitcoin TradingLatino B002
9 stars

VSA Pro: Trend and Rejection is an overlay strategy combining SuperTrend trend filtering with Volume Spread Analysis principles. It calculates SuperTrend direction to enter long positions when a new bullish trend starts and exits all positions when the trend t

Strategy v5 TrendVolume
Updated 4 months ago · MIT GitHub repository of VSA Pro: Trend and Rejection
8 stars

This overlay strategy combines three distinct SuperTrend levels with multiple moving averages and volume profile analytics on the main price chart. It calculates trend directions using three configurable ATR periods and multipliers alongside SMA, EMA, and HMA

Strategy v5 TrendVolatility & bands
Updated 4 years ago · None stated GitHub repository of ATR 3Lvls VP Strategy
6 stars

Capital41 Fusion Core Strategy is an overlay trading strategy that combines an exponential moving average trend filter, RSI, Williams %R, and volume/ATR expansion into a composite momentum oscillator. It executes long trades when the smoothed oscillator crosse

Strategy v6 TrendOscillators & momentum
Updated 8 weeks ago · MIT GitHub repository of Capital41 Fusion Core Strategy
4 stars

This trend-following strategy generates long and short entries when a fast EMA crosses a slow EMA, overlaying directly on the price chart. To reduce false signals, it applies four configurable fakeout filters: volume moving average threshold, consecutive bar c

Strategy v6 TrendMoving averages
Updated 2 months ago · MIT GitHub repository of Fakeout-Confirmed Strategy

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