Correlation

ICE Data Services -

Correlation.efs  
EFSLibrary - Discussion Board  

File Name: Correlation.efs

Description:
Correlation

Formula Parameters:

  • External Symbol : EUR A0-FX
  • Length : 20
  • Source of Price : Close

Notes:
FOREX Correlation Indicator

Download File:
Correlation.efs


EFS Code:

/*********************************
Provided By:  
    eSignal (Copyright c eSignal), a division of Interactive Data 
    Corporation. 2009. All rights reserved. This sample eSignal 
    Formula Script (EFS) is for educational purposes only and may be 
    modified and saved under a new file name.  eSignal is not responsible
    for the functionality once modified.  eSignal reserves the right 
    to modify and overwrite this EFS file with each new release.

Description:        
    Correlation    
Version:            1.0  06/10/2009
 
Formula Parameters:                     Default:
    External Symbol                     EUR a0-fx
    Length                              20
    Source of Price                     Close
    
Notes:
    FOREX Correlation Indicator
    
**********************************/
var fpArray = new Array();
var bInit = false;

function preMain(){
    setPriceStudy(false);
    setShowCursorLabel(true);
    setShowTitleParameters(false);
    setStudyTitle("Correlation");
    setCursorLabelName("Correlation", 0);
    setDefaultBarFgColor(Color.red, 0);
    setPlotType(PLOTTYPE_HISTOGRAM, 0);
    setDefaultBarThickness(2, 0);
    var x = 0;
    fpArray[x] = new FunctionParameter("Length", FunctionParameter.NUMBER);
    with(fpArray[x++]) {
        setLowerLimit(1);
        setDefault(20);
    }    
    fpArray[x] = new FunctionParameter("ExternalSymbol", FunctionParameter.STRING);
    with(fpArray[x++]) {
        setName("External Symbol");
        setDefault("EUR a0-fx");
    }        
    fpArray[x] = new FunctionParameter("sPrice", FunctionParameter.STRING);
	with(fpArray[x++]){
        setName("Source of Price");
        addOption("open"); 
        addOption("high");
        addOption("low");
        addOption("close");
        addOption("hl2");
        addOption("hlc3");
        addOption("ohlc4"); 
        setDefault("close"); 
    }    
}

var xCorrelation = null;

function main(ExternalSymbol, Length, sPrice) {
var nBarState = getBarState();
var nCorrelation = 0;
    if (nBarState == BARSTATE_ALLBARS) {
        if (ExternalSymbol == null) ExternalSymbol = "EUR a0-fx";
        if (Length == null) Length = 20;
        if (sPrice == null) sPrice = "close";
	}
	if (bInit == false) {
        setStudyTitle("Correlation "+ExternalSymbol+" ("+sPrice+")");
        xCorrelation = efsInternal("Calc_Correlation", ExternalSymbol, Length, sPrice);
        bInit = true;
	}
	nCorrelation = xCorrelation.getValue(0);
	if (nCorrelation == null) return;
	return nCorrelation;
}

var bSecondInit = false;
var xOS_PriceValue = null;
var xPriceValue = null;
var xOS_MAPriceValue = null;
var xMAPriceValue = null;

function Calc_Correlation(ExternalSymbol, Length, sPrice) {
var nRes = 0;
var nCor1 = 0;
var nCor2 = 0;
var nCor3 = 0;
var i = 0;
var nX = 0;
var nY = 0;
var nSX = 0;
var nSY = 0;
    if (bSecondInit == false) {
        xPriceValue = eval(sPrice)();
        xMAPriceValue = sma(Length, xPriceValue);
        xOS_PriceValue = eval(sPrice)(sym(ExternalSymbol));
        xOS_MAPriceValue = sma(Length, xOS_PriceValue);
        bSecondInit = true;
    }
    nSX = xMAPriceValue.getValue(0);
    nSY = xOS_MAPriceValue.getValue(0);
    if (nSX == null || nSY == null) return;
    for (i = 0; i < Length; i++) {
        nX = xPriceValue.getValue(-i);
        nY = xOS_PriceValue.getValue(-i);
        nCor1 += (nX - nSX) * (nY - nSY);
        nCor2 += (nX - nSX) * (nX - nSX);
        nCor3 += (nY - nSY) * (nY - nSY);
    }
    nRes = nCor1 / Math.sqrt(nCor2) / Math.sqrt(nCor3);
    return nRes;
}