Definition of moving average: "average" refers to the arithmetic average of the closing price in the last n days; "Moving" means that we always use the price data of the last n days in our calculations. Therefore, the average array (the closing price of the last n days) moves forward day by day with the change of the new trading day. When we calculate the moving average, we usually use the closing price of the last n days.
Add the new closing price to the array day by day, and the closing price of n+ 1 from bottom to front is deleted. Then, divide the new sum by n, and you will get the new day's average (n-day average).