Calculating the Simple Moving Average in Python
hive-167922·@chasmic-cosm·
0.000 HBDCalculating the Simple Moving Average in Python
 # Theory Moving averages are used as a basis for many technical indicators. A moving average is a sort of *local* average around a given point. In signal processing terms the moving average acts as a lowpass filter, removing high frequency "noise" from the signal. # Implementation The `signal` parameter is a one dimensional array. It makes sense to use the [typical price](https://hive.blog/investing/@chasmic-cosm/calculating-the-typical-price-of-an-asset-in-python) as this input. ```python import numpy as np def simple_moving_average(signal, points): """ Calculate the N-point simple moving average of a signal Inputs: signal: numpy array - A sequence of price points in time points: int - The size of the moving average Outputs: moving_average: numpy array - The moving average at each point in the signal """ moving_average = np.zeros(len(signal)) for i in range(points): moving_average[i] = np.sum(signal[0:i + 1])/(i+1) for i in range(points, len(signal)): moving_average[i] = np.sum(signal[i + 1 - points:i + 1])/(points) return moving_average ``` # Results Comparing the simple moving average to the typical price reveals the behaviour of the SMA: it is smoother than the typical price signal and it lags behind the typical price signal.  It is also worth comparing moving averages of different periods:  Note that the 20 point moving average is smoother than the 10 point moving average and lags even further behind the typical price. Posted Using [LeoFinance](https://leofinance.io/@chasmic-cosm/calculating-the-simple-moving-average-in-python)
👍 toni.curation, adamsidney, laissez-faire, goldbloger1, coinchek, goldbloger3, myanmarvds, goldbloger4, tabraiz.jutt, herokiller269, eadenworld, goldenmyanmar, aungkyawhtay, godenlight, prometheus21, leontz, kozwe, zwezayarlin, tharthar0016, roadbet, zawmin, flowernice, hayabusayakata, zayarzayar018, khard, kozwe2, pink.angle9, jenelifepoolguar, smt2, powweed420, mgking3, mgking2, mgking1, mamyawin, kyawthuu, minmin007, minnkhant261, mgmgnaing128, lulin44, mgking5, kyawlay1, mgmgnaing122, sulfer, armandoandre, karful, niyannnn, elnathan, soosenkasper, waizin, sunendra, nyanlynnhtay, rocketblog, mrblueberry, crantar, danishmobeen, farelsteem, anonek, zabatar, collect1, phungminhtuan, decebal2dac, afeliseth, hazem90, lemoney, brwhale, jack333, waihein, nullnull, a-x, klaranlage, savetheday, adanike, archiv, killeroase, halli-hallo, hellenita, alan369, stellafranco, jonnyla08, sirjaxxy, mickbit, stalexmkl, justasperm, cryptocoinkb, kunschj, cardboard, reward.app, tipu, mxzn, happy-soul, kryptogames, jalentakesphotos, dappstats, cryptogambit, bartheek, jacuzzi, longer, leo.voter, gadrian, engrsayful, investprosper, mcoinz79, donald.porter,