Correlation

View as Markdown

Description

The correlation indicator will plot the correlation of the data series to a desired instrument. Values close to 1 indicate movement in the same direction. Values close to -1 indicate movement in opposite directions. Values near 0 indicate no correlation.

Syntax

Correlation(int period, string correlationSeries)

Correlation(ISeries\<double\> input, int period, string correlationSeries)

Return Value

double; Accessing this method via an index value int barsAgo returns the indicator value of the referenced bar.

Parameters

ParameterDescription
inputIndicator source data (Series<T>)
periodNumber of bars used in the calculation
correlationSeriesThe data series to compare to

Examples

1// The correlation data series must be added to OnStateChange() as this indicator runs off the correlation data series data
2else if (State == State.Configure)
3{
4 AddDataSeries("SPY");
5}
6
7// Checks the bars in progress and prints the current correlation to the SPY
8if (BarsInProgress == 0)
9{
10 double value = Correlation(20, "SPY")[0];
11 Print("The current correlation to the SPY is " + value.ToString());
12}

If the correlation series does not plot during a time the input series plots, a value of zero would plot in the above example. You may consider ignoring zero values.

Source Code

You can view this indicator method source code by selecting the menu New > NinjaScript Editor > Indicators within the NinjaTrader Control Center window.