Job finished
Specification
It frustrates me when the currency pair is going up, my indicator is going down. I want to create a measure of goodness of fit between the indicator and the close price, so I can select the appropriate window length of the indicator that has the most similar shape as price. So what is a good fit? If the change over the interval has the same sign: if both the close price and the indicator value have increased over the interval, or if both the close price and the indicator value have decreased over the interval, then this is good. This fitness should be on an adjustable window.
say for fitness function window length 3: you have 3 bars that you are going to compare to the value in bar i:
close indicator result
i-1 > > 1
i-2 < < 1
i-3 < < 1
so here you have 3 values, close in i was > than the close in i-1, close in i was < than close in i-2, and close in i was < than close in i-3. Similar for the indicator value: indicator in i was > than indicator in i-1, ... So here you have a perfect fit: Then we calculate: if the sign is the same, give it a value of 1 in the result buffer. So in this example you have 3 times the same sign, so calculate the % of the time that it was equal: count the number of 1's and divide by the number of results: 3/3 = 100%, so the fitness buffer has value of 1, the maximum.
But instead of only looking at the last value versus all other values in the window, we need to be looking at all values in the window and compare them to each other: this gives you a sort of triangular matrix: if you look at the close price for a window of 5 (for example): (the > and <'s are chosen randomly: just an example)
x i i-1 i-2 i-3 i-4 i-5
i x
i-1 > x
i-2 > < x
i-3 > > < x
i-4 < < < < x
i-5 > > > < > x
There are no values on the diagonal because you can't compare an element with itself, and all the values that were not filled out are the opposite of their symmetric elements: if a>b then b<a so there is no point in filling them in. So this gives you a sort of triangular matrix for the close prices, now you can do the same for the indicator values, then check the number of equal signs, and divide by the number of signs, and then this gives you a percentage of fit: a value between 0 and 1.
You should be able to fill in the name of the custom indicator that you want to compare to the close price (no need for external variable, just comment in the code): iCustom(0,0,"...",....,i);
The result of this indicator will almost never be 1, because there is always going to be a bit of a difference between price and the indicator: that's the point of the indicator: to smooth price and make the trend more clear. But it does tell me what window sizes are bad in a quantitative, objective way.
A second step can be that you rank the changes by absolute value from high to low, and give them linear or exponential weights: so equality of sign of change for the biggest change has the biggest impact on the fitness percentage, and the smallest change has very little impact on the fitness percentage. We can discuss that later.