currently, the Correlation Indicator is practically useless because the secondary bar series is processed after the primary bar. So the indicator compares the current price of the primary bar series with the price of secondary bar series of one bar ago since the current secondary bar series is not loaded. So the indicator is comparing different timestamps and therefore lagging.
I changed the code of the indicator to make it processes the data only in BarsInProgress == 1 to make sure that the comparison takes place only when the all data is loaded to compare the prices at the same timestamp. I tested it by adding the same instrument like the primary series to see if the value is always 1. However, I am still getting sometimes a value of 0 although it should always be 1 since I am comparing the the exact same instruments / prices.
Here is the code of the indicator (everything remained is unchanged).
protected override void OnBarUpdate()
{
if (BarsInProgress == 1)
{
if (SessionIterator == null || CurrentBars[0] < Period || CurrentBars[1] < Period)
return;
if (Bars.BarsType.IsIntraday && !SessionIterator.IsInSession(Times[0][0], true, true))
return;
avg1 = SMA(BarsArray[1], Period)[0];
avg0 = SMA(BarsArray[0], Period)[0];
//Print(String.Format("{0}; {1}; {2}", avg0, avg1, avg0 == avg1));
double nominator = 0;
double denominator1 = 0;
double denominator2 = 0;
for (int i = 0; i < Period; i++)
{
nominator += (avg0 - Inputs[0][i]) * (avg1 - Inputs[1][i]);
denominator1 += (avg0 - Inputs[0][i]) * (avg0 - Inputs[0][i]);
denominator2 += (avg1 - Inputs[1][i]) * (avg1 - Inputs[1][i]);
}
double denominator = Math.Sqrt(denominator1) * Math.Sqrt(denominator2);
Value[0] = denominator.ApproxCompare(0) == 0 ? 0 : nominator / denominator;
}
}
Thanks in advance!

Comment