I'm reading Wiki's entry on a moving average and have questions on the derivation of $\textit{SMA}_{k, next}$. Specifically, does anyone know step (2) was derived from step (1)? The terms in the right cancel, but I still am confused how (2) is derived. Thanks
$$ \begin{align} \textit{SMA}_{k} &= \frac{p_{n-k+1} + p_{n-k+2} \cdots + p_{n}}{k} \\ &= \frac{1}{k} \sum_{i=n-k+1}^{n} p_{i} \end{align} $$ $$ \begin{align} \textit{SMA}_{k, next} &= \frac{1}{k} \sum_{i=n-k+2}^{n+1} p_{i} \\ &= \frac{1}{k} \Big( \underbrace{ p_{n-k+2} + p_{n-k+3} + \dots + p_{n} + p_{n+1} }_{ \sum_{i=n-k+2}^{n+1} p_{i} } + \underbrace{ p_{n-k+1} - p_{n-k+1} }_{= 0} \Big) && (1)\\ &= \underbrace{ \frac{1}{k} \Big( p_{n-k+1} + p_{n-k+2} + \dots + p_{n} \Big) }_{= \textit{SMA}_{k, prev}} - \frac{p_{n-k+1}}{k} + \frac{p_{n+1}}{k} && (2)\\ &= \textit{SMA}_{k, prev} + \frac{1}{k} \Big( p_{n+1} - p_{n-k+1} \Big) && (3) \end{align} $$
The idea of this demonstration is to write $SMA_{k,n+1}$ in terms of $SMA_{k,n}$ (probably for computation reasons, its easier to add a few numbers than a lot).
They started writing $SMA_{k,n+1}$ and if you compare this with $SMA_{k,n}$, you will notice that the term $p_{n-k+1}$ (the first term in $SMA_{k,n}$) is missing, and the last term $p_{n+1}$ that is not in $SMA_{k,n}$, so they added and substracted the term $p_{n-k+1}$ and then separated the term $p_{n+1}$ from the summation. Probably the confusion comes from the fact that the the term $p_{n-k+1}$ was located in the first position in the summation in (2).