This paper presents a recursive scheme for optimizing the gain of an exponentiallly weighted moving average (EWMA) controller under stability constraints. The objective is to minimize the asymptotic mean square error in the output with minimal a priori information. The algorithm hinges on a simple representation of the optimal EWMA gain. Both step and drift disturbances are considered. It is shown that the gain sequence generated by the algorithm always yields a stable system. Furthermore, this sequence is shown to converge to a suboptimal value. Extensions to the algorithm to the case where there is model uncertainty are also presented. The algorithm is verified via simulation. Data from a manufacturing implementation is presented.Index Terms-Adaptive optimization, exponentially weighted moving average (EWMA) controller, run-to-run control (R2R), stochastic approximation.