The code of the complete indicator :
It draws this :
The code used in this indicator is different (different, faster, way of preparing and passing array elements but the results are the same) The 2 lines of code that need optimization remain the same (as it is obvious, since, practically, it is all that remains in the function calculation code)
double QuantRegCoeff=(coeffSize%2==1) ?(CoeffBuffer[(coeffSize-1)/2]) : (0.5*(CoeffBuffer[coeffSize/2]+CoeffBuffer[coeffSize/2-1]));
But as Alain pointed, size * (size-1) is always even.
So at my surprise, I didn't succeed. The main optimization idea I had was to reduce the number of division operations needed :
As this operation is done in a loop for every candle i, there are a lot of repetitions of the exact same operation. So the idea was to do all the operations once and to memorize them (see attached how I did). However it doesn't improve this speed, even while the numbers of operations was reduced by a factor 16 !
From 64 millions to 4 millions division operations, but no change in execution time. I didn't expect that. That means double arithmetic CPU is very efficient and cached very well all the results.
Also, though this imbricated loop with division operations is time consuming, the main bottleneck in the ArraySort(), the speed impact is more than 3 times the one of the loops. So even if the "division" optimization had worked that global impact would have been low (~20% max).
That was an interesting exercise, even if it failed.
Attached the code (as we are on week-end I didn't pay attention to "live" update).
This makes more sense to me:
Done in SAS.
Is it feasible in MQL5? Does anyone know? I cannot try it because I am offshore and cannot use MQL5... :(