In your particular case the coding would be modified. Basically you find the EMA 50 value for today, multiply by 1.02 & 0.98 to get the plus / minus 2%. Then set the values of the EMA 50 for the past ten days to be within that range, ie max for 10 days is less than the +2% (1.02)and min for 10 days to be greater than the -2% (0.98).