Hello All...

Sorry, the subject is not very descriptive but that's the only one I think of right now ¯_(ツ)_/¯

I am trying to write a code to return at each bar the shortest MA period (between two predefined periods) where the following condition is true: Sum(C>MA(C,Period))==Limit... Period is the number I am looking for for each bar and Limit is a predefined constant.

What I have currently come up (below) with is a highly inefficient use of resources and I am sure there is a better way... can anyone please guide me a little to improve this and ideally have it all in AFL? I am shooting blanks here .

Much appreciated!

```
// look for the SHORTEST MA period between _start and _End
period_start = 10;
period_end = 50;
period_limit = 20;
// the number of days the C must be above the MA to stop the search.
for( i = 0; i < BarCount; i++ ) // loop through all bars, probably not necessary... can change to visible bars only
{
period_result[i] = 0; // stores the result at each bar
for( p = period_start; p <= period_end; p++ ) // loop between _start and _end periods
{
average = MA( C, p );
count = Sum( C > average, period_limit ); // count number of days the c>ma in _limit periods
if( count[i] == period_limit ) // if all above for _limit periods then
{
period_result[i] = p; // ... store the result
break; // ... break the loop
}
}
}
Plot( period_result, _DEFAULT_NAME(), colorBlack );
```