create account

Calculating the Simple Moving Average in Python by chasmic-cosm

View this thread on: hive.blogpeakd.comecency.com
· @chasmic-cosm ·
$1.05
Calculating the Simple Moving Average in Python
![simple moving average 1.png](https://images.hive.blog/DQmdtGVzkhvk4tQning8iGQutk6FUezBS9kQuFyzn2T75Rv/simple%20moving%20average%201.png)

# 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.

![sma vs typical price.png](https://images.hive.blog/DQmeEgx1hMTZbpRPYngj3JKFeLhCkWcYkLhgj1UofB7DAuC/sma%20vs%20typical%20price.png)

It is also worth comparing moving averages of different periods:

![sma 10 vs 20.png](https://images.hive.blog/DQmVQU3P7yAYxLRBdZ5UAVTMLTCjdBhYZaG7Ygq5PHErTZ2/sma%2010%20vs%2020.png)

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)
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 39 others
properties (23)
authorchasmic-cosm
permlinkcalculating-the-simple-moving-average-in-python
categoryhive-167922
json_metadata{"tags":["investing","trading","finance","maths","programming","leofinance"],"image":["https://images.hive.blog/DQmdtGVzkhvk4tQning8iGQutk6FUezBS9kQuFyzn2T75Rv/simple%20moving%20average%201.png","https://images.hive.blog/DQmeEgx1hMTZbpRPYngj3JKFeLhCkWcYkLhgj1UofB7DAuC/sma%20vs%20typical%20price.png","https://images.hive.blog/DQmVQU3P7yAYxLRBdZ5UAVTMLTCjdBhYZaG7Ygq5PHErTZ2/sma%2010%20vs%2020.png"],"links":["https://hive.blog/investing/@chasmic-cosm/calculating-the-typical-price-of-an-asset-in-python"],"app":"leofinance/0.1","format":"markdown","canonical_url":"https://leofinance.io/@chasmic-cosm/calculating-the-simple-moving-average-in-python"}
created2020-07-24 11:38:57
last_update2020-07-24 11:38:57
depth0
children3
last_payout2020-07-31 11:38:57
cashout_time1969-12-31 23:59:59
total_payout_value0.528 HBD
curator_payout_value0.524 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length2,058
author_reputation4,159,775,155,640
root_title"Calculating the Simple Moving Average in Python"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id98,692,136
net_rshares3,565,257,204,371
author_curate_reward""
vote details (103)
@cardboard ·
@tipu curate :)
properties (22)
authorcardboard
permlinkqdzcn0
categoryhive-167922
json_metadata{"users":["tipu"],"app":"hiveblog/0.1"}
created2020-07-24 15:35:24
last_update2020-07-24 15:35:24
depth1
children1
last_payout2020-07-31 15:35:24
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length15
author_reputation31,522,757,177,122
root_title"Calculating the Simple Moving Average in Python"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id98,695,475
net_rshares0
@tipu ·
<a href="https://tipu.online/hive_curator?cardboard" target="_blank">Upvoted  &#128076;</a> (Mana: 0/21)
properties (22)
authortipu
permlinkre-qdzcn0-20200724t153534
categoryhive-167922
json_metadata""
created2020-07-24 15:35:33
last_update2020-07-24 15:35:33
depth2
children0
last_payout2020-07-31 15:35:33
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length104
author_reputation55,914,556,170,175
root_title"Calculating the Simple Moving Average in Python"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id98,695,479
net_rshares0
@upvotebank ·
<center>UpvoteBank</center> | <center>Your upvote bank</center>
------------ | -------------
![__2.jpg](https://steemitimages.com/DQmfDxvv4NLs3knYT7B2mHgE5ArnkheNmDSLydKKTfBwokj/__2.jpg) | This post have been upvoted by the @UpvoteBank service. Want to know more and receive "free" upvotes click [here](https://steemit.com/steemit/@upvotebank/gzsr5aw6)
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 319 others
👎  , , ,
properties (23)
authorupvotebank
permlink20200724t114214981z
categoryhive-167922
json_metadata{"tags":["comment"],"app":"steemjs/comment"}
created2020-07-24 11:42:15
last_update2020-07-24 11:42:15
depth1
children0
last_payout2020-07-31 11:42:15
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length354
author_reputation44,533,286,469,385
root_title"Calculating the Simple Moving Average in Python"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id98,692,183
net_rshares-3,376,743,687
author_curate_reward""
vote details (387)