Strategy · Pine Script v4 · Own pane

Altcoin Correlation Meter

by hasnocool

Calculates the correlation coefficient and covariance between the current crypto asset and a reference market index, such as CRYPTOCAP:TOTAL2, using higher-timeframe data.

View on GitHub

Repaints

GitHub stars106on the repository
Pine versionv4older, still runs
Lines of code63
Settings7inputs
LicenceGPL-3.0
Last update2 years ago18 Sep 2024

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

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.

Details

Published by

hasnocool on GitHub

Type

Strategy, Pine Script v4

Draws

Separate pane below the chart

Category

Trend, Moving averages

Markets

Crypto

Timeframes

M15-H1, H4 and above

Risk Notes

  • Signals can change on the live bar or after it closes (repainting)
  • The strategy enters trades without a stop loss

Topics

analysisbtcusdtcryptocurrencydatapandaspythonsimulationtradingtradingview

Settings

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

SettingTypeDefaultVariable
resolution of reference pair (in min, hr or day)string120resolution_sec
Lookbackint5lookback
length of Hull MA, for opening long/shortint5lengthHMA
Sourcesourceclosesource_type
Reference MarketstringCRYPTOCAP:TOTAL2reference_TOTAL
if correlation above this number, close long/shortfloat0.95correlationclose
minimum percentage in profit to close long/shortfloat1TPpercentage
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

330 stars

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

Strategy v5 TrendMoving averages
Updated 2 months ago · None stated GitHub repository of HA UnivLong&Short Futures

MACD Crossover (tutorial)

by pineforge-4pass
193 stars

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

Strategy v6 Oscillators & momentumMoving averages
Updated today · Apache-2.0 GitHub repository of MACD Crossover (tutorial)

EMA Ribbon MACD

by iamrichardD
57 stars

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

Strategy v6 TrendMoving averages
Updated 1 year ago · Apache-2.0 GitHub repository of EMA Ribbon MACD

NFI v7.2.1

by koradiyakaushal
12 stars

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

Strategy v4 TrendMoving averages
Updated 5 years ago · MIT GitHub repository of NFI v7.2.1

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