Let's say we have the following terms in a string (there is no formula for the next terms) $$\begin{array}{c|c} \text{ms} & \text{value} \\\hline 1&1 \\\hline 2&2 \\\hline 3&3 \\\hline 4&4 \\\hline 5&5 \\\hline 6&6 \\\hline 7&9 \\\hline 8&10 \\\hline 9&12 \\\hline 10&13 \\\hline 11&7 \\\hline 12&6 \\\hline 13&5 \\\hline 14&11 \\\hline 15&13 \\\hline 16&15 \\\hline 17&17 \\\hline 18&20 \\\hline 19&20 \\\hline 20&20 \\\hline 21&18 \\\hline 22&17 \\\hline 23&16 \\\hline 24&15 \\\hline 25&13 \\\hline 26&11 \\\hline 27&10 \\\hline 28&9 \\\hline 29&8 \\\hline 30&7 \\\hline 31&5 \\\hline 32&3 \\\hline 33&1 \\\hline 34&0 \\\hline \end{array}$$ For this table we have the following input signal:
From the table and from the picture it can be observed that the maximum it is around $18[\text{ms}]$ when the graph have the biggest value equals with $20$.
What I want to try is to create a "filter" such that the signal to be smoother and also the maximum of the "filter" to be almost in the same position with the input signal. The ideal case is the red curve from the following picture:

I am aware that the ideal case is very hard to be obtained, so I would like to obtain a formula which helps me to transform the input signal in another one smoother and with the maximum almost in the same position.
What I have tried was to sum all the time the first $8$ values and then to divide by 8, then to sum the elements starting from $2$[ms] to $9$[ms] and divided by 8, $\ldots$ and so on until starting from $27$[ms] to $34$[ms] and divided by 8, then starting from $28$[ms] to $34$[ms] and divided by 8, $\ldots$ $33$[ms] to $34$[ms] and divided by 8, $34$[ms] and divided by 8.
So, I obtain the following picture which is smoother but the maximum is not in almost the same position.
Later edit
I want to say that there is a formula, but this formula is no so good because the maximum of the input is not in the same time with the maximum for the filter.
So,
$$\text{Filter}=\sum_{i=t-\text{buffer}}^{t}{\text{Input}_{t}},$$ where $\text{buffer}$ can be any value.



I suppose you would like a method that you can apply to a wide class of input signals with the minimum of taylored adjustment of the parameters.
For that, I would proceed as follows:
For instance, for the example you give a
m. avg. pre-f. of 2 st, a raise to power 4 and centered m. avg.filtering with 5 st, would give the following result
Notes
From the structure of the formula above, you can recognize a convolution between the signal and a "box" (rectangular window) of width $n$ centered at the origin.
That to indicate that you have further choices for the window to use beyond the rectangular one: each with its special features and target applications. A summary list of them is given here.
Literature to refer to is quite vast. You may start from this wikipedia at the point where it states : "For a number of applications, it is advantageous to avoid the shifting induced by using only 'past' data. Hence a central moving average can be computed, using data equally spaced on either side of the point in the series where the mean is calculated" and follow the links given there.
In particular the link to the Savitzky–Golay filter, one the cited application for which is "..Location of maxima and minima in experimental data curves. This was the application that first motivated Savitzky....".
Final consideration is that, to orient yourself among the math/signal processing tools available, much (I would say all) depends on the physical considerations governing the input signal and intended application of the output.