Calculates the correlation coefficient and covariance between the current crypto asset and a reference market index, such as CRYPTOCAP:TOTAL2, using higher-timeframe data.
1Copy the scriptPress Copy script above, or the copy button on the code below.
2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
3Paste over the templateCreate a new strategy, select all of the template text and paste. Press Save and give it a name.
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
63 lines of Pine Script v4 from Altcoin correlation Meter.pine, published under GPL-3.0. Keep the author's credit and licence lines when you reuse it.
Altcoin correlation Meter.pine
// @version=4
strategy("Altcoin correlation Meter", overlay=false, pyramiding=0, default_qty_type= strategy.percent_of_equity, default_qty_value = 100, calc_on_order_fills=false, slippage=0,commission_type=strategy.commission.percent,commission_value=0.04)
// Get user input
resolution_sec = input(title="resolution of reference pair (in min, hr or day)", type=input.string, defval="120")
lookback = input(title="Lookback", type=input.integer, defval=5)
lengthHMA = input(title="length of Hull MA, for opening long/short", type=input.integer, defval=5)
source_type = input(title="Source", type=input.source, defval=close)
reference_pair = security(syminfo.tickerid, resolution=resolution_sec, expression=source_type)
reference_TOTAL = input(title="Reference Market", type=input.string, defval="CRYPTOCAP:TOTAL2") //"binance:ethusdtperp" //"CRYPTOCAP:TOTAL"
correlationclose = input(title="if correlation above this number, close long/short", type=input.float, defval=0.95)
TPpercentage = input(title="minimum percentage in profit to close long/short", type=input.float, defval=1)
// Get % change of reference data source
referenceData_TOTAL = security(symbol=reference_TOTAL, resolution=resolution_sec, expression=source_type)
referenceChange = ((referenceData_TOTAL - referenceData_TOTAL[1]) / referenceData_TOTAL[1]) * 100
// Get % change of current market
currentData = reference_pair
currentChange = ((currentData - currentData[1]) / currentData[1]) * 100
// Declare arrays
var referenceArray = array.new_float(lookback)
var currentArray = array.new_float(lookback)
// Shift (remove) the last (first entered) value from our arrays (FIFO)
array.shift(referenceArray)
array.shift(currentArray)
// Add the current values to our arrays
array.push(referenceArray, referenceChange)
array.push(currentArray, currentChange)
hma_REF = hma(referenceData_TOTAL,lengthHMA)
hma_pair = hma(reference_pair,lengthHMA)
// Determine & plot our covariance relationship
covariance = array.covariance(currentArray, referenceArray)
plot(covariance, color=color.purple, style=plot.style_area, transp=0, display=display.none, title="Covariance")
// Plot our reference data
plot(referenceChange, color=color.red, style=plot.style_columns, transp=10, display=display.none, title="Reference Market")
plot(currentChange, color=color.blue, style=plot.style_histogram, linewidth=4, display=display.none, title="Current Market")
// Determine the standard deviation of our arrays
referenceDev = array.stdev(referenceArray)
currentDev = array.stdev(currentArray)
correlation = covariance / (referenceDev * currentDev)
correlation1 = correlation[1]
plot(correlation, color=color.yellow, linewidth=2, style=plot.style_stepline, title="Correlation Strength")
plot(correlation1, color=color.green, linewidth=2, style=plot.style_stepline, title="Correlation1 Strength")
// Plot reference lines
hline(price=0.9, color=color.red)
hline(price=0.5, color=color.white)
hline(price=-0.5, color=color.white)
hline(price=-0.9, color=color.red)
hline(price=0.0)
long = hma_pair > hma_pair[2] and correlation<0
short = hma_pair < hma_pair[2] and correlation<0 //crossover(correlation, correlation1) and close>open and close>close[1] and correlation<0.9
closelong = correlation>correlationclose
closeshort = correlation>correlationclose
TPlong = (100-TPpercentage)/100
TPshort =(100+TPpercentage)/100
if long
strategy.entry("long", strategy.long)
if short
strategy.entry("short", strategy.short)
if closelong and close*TPlong>strategy.position_avg_price
strategy.close("long")
if closeshort and close*TPshort<strategy.position_avg_price
strategy.close("short")
Expand (75 lines)
Overview
Altcoin Correlation Meter tracks percentage price changes in rolling arrays to compute correlation alongside a Hull Moving Average. The strategy enters long or short positions when the asset's Hull Moving Average aligns with a negative correlation status, and exits trades when correlation rises above a user-defined threshold. Plotted in a separate pane, it highlights correlation levels and relative market changes. It lacks a default stop-loss mechanism.
Pearson correlation calculation via rolling price change arrays
The inputs you can change from the gear icon on the chart. Defaults are the author's.
Setting
Type
Default
Variable
resolution of reference pair (in min, hr or day)
string
120
resolution_sec
Lookback
int
5
lookback
length of Hull MA, for opening long/short
int
5
lengthHMA
Source
source
close
source_type
Reference Market
string
CRYPTOCAP:TOTAL2
reference_TOTAL
if correlation above this number, close long/short
float
0.95
correlationclose
minimum percentage in profit to close long/short
float
1
TPpercentage
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.
HA UnivLong&Short Futures calculates Heikin Ashi candles and builds a channel using simple moving averages of Heikin Ashi highs and lows. It enters long positions when the upper channel line crosses above the lower channel line and opens short positions on the
This strategy calculates the Moving Average Convergence Divergence indicator and plots the MACD line, signal line, and histogram in a separate pane. It enters long positions when the MACD line crosses above the signal line and enters short positions when it cr
This strategy combines an EMA ribbon with MACD confirmation and plots in a separate pane. It enters when ribbon alignment, optional MACD zero or signal crosses, volume, smart-money, and multi-timeframe filters agree, then exits with percentage stop loss and ta
This strategy plots a comparative relative strength line against a benchmark symbol in a separate pane. It calculates price and volume simple moving averages alongside a multi-period highest high. Long trades enter using full equity when price exceeds its movi
This strategy calculates two simple moving averages using periods 34 and 68 on the close price. It detects bullish crossovers to enter long positions of 10 contracts and bearish crossovers to enter short positions of 10 contracts after the 100th bar. Opposite
This strategy is a Pine Script implementation of the NostalgiaForInfinity crypto strategy. It calculates indicator metrics across multiple timeframes and compares price action against a master pair such as BTC/USDT. Settings allow traders to configure informat