Skip to Content
Learn Algorithmic Trading
book

Learn Algorithmic Trading

by Sebastien Donadio, Sourav Ghosh
November 2019
Beginner content levelBeginner
394 pages
10h 31m
English
Packt Publishing
Content preview from Learn Algorithmic Trading

Implementation of the simple moving average

In this section, the code demonstrates how you would implement a simple moving average, using a list (history) to maintain a moving window of prices and a list (SMA values) to maintain a list of SMA values:

import statistics as statstime_period = 20 # number of days over which to averagehistory = [] # to track a history of pricessma_values = [] # to track simple moving average valuesfor close_price in close: history.append(close_price) if len(history) > time_period: # we remove oldest price because we only average over last 'time_period' prices   del (history[0]) sma_values.append(stats.mean(history))goog_data = goog_data.assign(ClosePrice=pd.Series(close, index=goog_data.index))goog_data = goog_data.assign(Simple20DayMovingAverage=pd.Series(sma_values, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

High-Frequency Trading: A Practical Guide to Algorithmic Strategies and Trading Systems

High-Frequency Trading: A Practical Guide to Algorithmic Strategies and Trading Systems

Irene Aldridge
Developing High-Frequency Trading Systems

Developing High-Frequency Trading Systems

Sebastien Donadio, Sourav Ghosh, Romain Rossier

Publisher Resources

ISBN: 9781789348347Supplemental Content