Python: Exponential Moving Average (EMA) Mathematics and Stock Indicators

sentdex · Beginner ·🛠️ AI Tools & Apps ·12y ago

Key Takeaways

Calculates an exponential moving average within Python using EMA mathematics and stock indicators

Full Transcript

what's going on everybody Welcome to the next uh video in our mathematics and stock indicators with Python tutorial Series where we left off we were doing uh just a simple moving average here and now I want to show you guys how to do an exponential moving average so moving right along uh we're just going to Define exp moving average again it's going to have a values and a window don't forget your colon then we do weights equals numpy X for exponential mp. Lin space and within here a negative one with a decimal point and a zero with a decimal point and then a window um just like before I think I'll leave out the explanation of lens space there we just want to keep this quick and basic uh weights uh is going to actually be a divided equals and what this work how this works is is the easier way to explain this is you'll probably see a plus equals a lot and so plus equals just basically means weights the original version of Weights plus one and then it redefines that variable so at the same point as the negative equals it's just like weights negative equals what weights. sum um and that's that's basically how that's working uh so now the next thing we want to do is now we're going to do a equal mp. convolve and values weights again and then this creates basically an array and then the way we can specify um what part of that array we want is like this so everything up to the length of value so how many values do we have this goes to the length of those values subsequent quently um we've got a and then to the window length equals a window and then again we just return a and if you'll notice we actually uh use the convolve function with its default so we didn't say valid we could do it either way it's not as big of a deal with exponential moving average so we'll just leave it uh as the default so again now we'll just come down here and we'll print exp moving average and again we'll do data set and three save that run that and now the first thing it printed out was our the same simple moving average we have already uh done and then the next thing it printed out was uh our exponential moving average so the idea of the exponential moving average versus the simple moving average is the exponential moving average is taking more recent data and applying a a larger weight to that more recent data than the older data so right that's like our weights here and we applied the weight here so um just to explain a couple things now uh numpy's convolved function what it's doing is it's it's returning a linear convolution right it's convolving two sequences right so in this case it's returning the two sequences with the length um or at least to the length of those values now the reason why I kind of just want to leave uh convolve out of it and same thing even with numpy's exponential uh functionality convolve is basically what's running our exponential or it's it's what's doing the moving average part for us and the reason why I just kind of want to leave those out for now is it just makes it way more efficient for us to do it this way since moving average is used and if you like say you're using like a 200 moving average which is used pretty pretty commonly like 20 and 200 um this saves a lot of processing power so numpy a lot of times comes in to save the day when it comes to processing um but to explain what's going on behind the scenes with convolve is is somewhat confusing and so there's not too much uh reason to cover it because I think moving average the definition of a moving average is simple enough to the point where I think you guys understand what a moving average is uh and the use using of numpy to make it a lot more efficient isn't really as necessary right so when you do a moving average you just know like okay I'm going to use convolve if you are curious you can go into the source files and just look at the convolve function and see what it's doing same thing with the exponential function here uh but anyway I'm not going to cover that because really what I'm trying to do in this series is just simply uh teach you guys how to do the simple equations and so a lot of the L latter equations that we're going to be covering here um to do moving averages we're going to use this simply because moving averages become very processing intensive so using numpy solves that problem uh for the moving average calculation but for the rest of the calculations we aren't doing that so if you feel like you're really lost at these uh first two I almost didn't want to cover them right away but you pretty much have to because they're like the most basic formulas out there um if you feel like you're a little confused at this point don't worry it's probably uh pretty common just understand that we're using these uh purely to save on processing if anybody wants me to I could cover a uh just a pure python without numpy uh example of doing a moving average but it just doesn't make too much sense cuz it's really processing intensive so anyway uh hopefully I didn't lose too many of my viewers right away since this numai stuff can get kind of confusing um the exponential that fairly uh basic it just does the exponential what of all of the elements in the input array whatever you're stuffing in there and then the numpy repeat option option is a way to multiply um something right so we're just saying what we want to multiply and then we're dividing it by that window so again like if you ran a repeat function on let's say we ran a repeat function on data set and we said repeat uh three right so if we did np. repeat data set 3 it would do 111 555 777 222 666 777 888 and so on and so that's what's happening in the repeat function again it we're just using numpy uh because it's very efficient so that's going to include uh both of the moving average and exponential moving average now in the next videos we're going to be uh moving right along and starting to conquer a bunch of the typical stock indicator formulas so anyway hopefully uh you guys are interested hopefully like I said I didn't lose too many people with the numpy stuff uh even though moving average and exponential moving average is easy um and the you know coming up formulas are in theory more complex it actually programming wise gets easier so anyway um as always thanks for watching thanks for the support the subscriptions and until next time

Original Description

This video teaches you how to calculate an exponential moving average within python. The idea of an exponential moving average is to value more recent data more heavily, while also smoothing lines. The EMA is used heavily with stocks, forex, futures and general engineering. The purpose of this series is to teach mathematics within python. To do this, we will be working with a bunch of the more popular stock indicators used with technical analysis. With most of the indicators, we will first discuss them, their purpose, then teach how to program them into python, then actually display them on a chart. The basic charting application comes from a previous tutorial series, here: http://www.youtube.com/playlist?list=PLQVvvaa0QuDcR-u9O8LyLR7URiKuW-XZq Required files: Sample Code for the actual charting parts: http://sentdex.com/startingPoint.py Python: http://python.org Numpy: http://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy Matplotlib: http://matplotlib.org/downloads.html Sentdex.com Facebook.com/sentdex Twitter.com/sentdex
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from sentdex · sentdex · 0 of 60

← Previous Next →
1 Matplotlib Python Tutorial Part 1: Basics and your first Graph!
Matplotlib Python Tutorial Part 1: Basics and your first Graph!
sentdex
2 Python Encryption Tutorial with PyCrypto
Python Encryption Tutorial with PyCrypto
sentdex
3 Python's Logging Function
Python's Logging Function
sentdex
4 wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
sentdex
5 wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
sentdex
6 wxPython Programming Tutorial 3: Menu Bar and Menu Button
wxPython Programming Tutorial 3: Menu Bar and Menu Button
sentdex
7 wxPython Programming Tutorial 4: Panels
wxPython Programming Tutorial 4: Panels
sentdex
8 wxPython Programming Tutorial 5: User Input Saved To Variables
wxPython Programming Tutorial 5: User Input Saved To Variables
sentdex
9 wxPython Programming Tutorial 6: Multiple Choice Input
wxPython Programming Tutorial 6: Multiple Choice Input
sentdex
10 wxPython Programming Tutorial 7: Adding Static Text and Colors
wxPython Programming Tutorial 7: Adding Static Text and Colors
sentdex
11 wxPython Programming Tutorial 8: Custom Button Images
wxPython Programming Tutorial 8: Custom Button Images
sentdex
12 wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
sentdex
13 Basic PHP Tutorial 13: Multi-dimensional Array
Basic PHP Tutorial 13: Multi-dimensional Array
sentdex
14 Basic PHP Tutorial 15: Functions and Global Variables
Basic PHP Tutorial 15: Functions and Global Variables
sentdex
15 Basic PHP Tutorial 12: Associative Array
Basic PHP Tutorial 12: Associative Array
sentdex
16 Basic PHP Tutorial 14: Foreach loop
Basic PHP Tutorial 14: Foreach loop
sentdex
17 Basic PHP Tutorial 16: Include and Require
Basic PHP Tutorial 16: Include and Require
sentdex
18 Basic PHP Tutorial 7: Assignment, comparison and Logical operators
Basic PHP Tutorial 7: Assignment, comparison and Logical operators
sentdex
19 Basic PHP Tutorial 4: Variables and Comments
Basic PHP Tutorial 4: Variables and Comments
sentdex
20 Basic PHP Tutorial 11: Arrays part 1, basic array
Basic PHP Tutorial 11: Arrays part 1, basic array
sentdex
21 Basic PHP Tutorial 6: If else and else if conditionals cont'd
Basic PHP Tutorial 6: If else and else if conditionals cont'd
sentdex
22 Basic PHP Tutorial 1: Intro to PHP
Basic PHP Tutorial 1: Intro to PHP
sentdex
23 Basic PHP Tutorial 3: HTML with PHP
Basic PHP Tutorial 3: HTML with PHP
sentdex
24 Basic PHP Tutorial 9: While Loop
Basic PHP Tutorial 9: While Loop
sentdex
25 Basic PHP Tutorial 10: Switch Statement
Basic PHP Tutorial 10: Switch Statement
sentdex
26 Basic PHP Tutorial 2: Print and Echo
Basic PHP Tutorial 2: Print and Echo
sentdex
27 Basic PHP Tutorial 5: If else and else if conditional statements
Basic PHP Tutorial 5: If else and else if conditional statements
sentdex
28 Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
sentdex
29 Basic PHP Tutorial 17: User Input Form Example / String Manipulation
Basic PHP Tutorial 17: User Input Form Example / String Manipulation
sentdex
30 Basic PHP Tutorial 18: HTML Entities and forms cont'd
Basic PHP Tutorial 18: HTML Entities and forms cont'd
sentdex
31 Basic PHP Tutorial 19: Finding words in strings
Basic PHP Tutorial 19: Finding words in strings
sentdex
32 Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
sentdex
33 Basic PHP Programming Tutorial 22: Hashing part 2: salting
Basic PHP Programming Tutorial 22: Hashing part 2: salting
sentdex
34 Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
sentdex
35 Basic PHP Programming Tutorial 21: MD5 Hashing For Security
Basic PHP Programming Tutorial 21: MD5 Hashing For Security
sentdex
36 Basic PHP Programming Tutorial 24: String similarity
Basic PHP Programming Tutorial 24: String similarity
sentdex
37 Basic PHP Programming Tutorial 25: Time and Time stamps
Basic PHP Programming Tutorial 25: Time and Time stamps
sentdex
38 Basic PHP Programming Tutorial 26: Die and Exit
Basic PHP Programming Tutorial 26: Die and Exit
sentdex
39 Basic PHP Programming Tutorial 27: MySQL Databases Part 1
Basic PHP Programming Tutorial 27: MySQL Databases Part 1
sentdex
40 Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
sentdex
41 Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
sentdex
42 Basic PHP Programming Tutorial 30: MySQL database in Use
Basic PHP Programming Tutorial 30: MySQL database in Use
sentdex
43 Django Tutorial Web Development with Python Part 1: Installing Django
Django Tutorial Web Development with Python Part 1: Installing Django
sentdex
44 Python Tutorial: File Deletion and Folder Deletion / directory deletion
Python Tutorial: File Deletion and Folder Deletion / directory deletion
sentdex
45 Python Tutorial: How to Rename Files and Move Files with Python
Python Tutorial: How to Rename Files and Move Files with Python
sentdex
46 3D Graphs in Matplotlib for Python: Basic 3D Line
3D Graphs in Matplotlib for Python: Basic 3D Line
sentdex
47 3D Plotting in Matplotlib for Python: 3D Scatter Plot
3D Plotting in Matplotlib for Python: 3D Scatter Plot
sentdex
48 3D Charts in Matplotlib for Python: Multiple datasets scatter plot
3D Charts in Matplotlib for Python: Multiple datasets scatter plot
sentdex
49 Sikuli Tutorial 1: Visually programming in python!
Sikuli Tutorial 1: Visually programming in python!
sentdex
50 Sikuli Tutorial 2: Program visually in python!
Sikuli Tutorial 2: Program visually in python!
sentdex
51 Sikuli Tutorial 3: Program visually in python!
Sikuli Tutorial 3: Program visually in python!
sentdex
52 3D Bar Charts in Python and Matplotlib
3D Bar Charts in Python and Matplotlib
sentdex
53 3D Plane wire frame Graph Chart in Python
3D Plane wire frame Graph Chart in Python
sentdex
54 Raspberry Pi Part 1 Introduction
Raspberry Pi Part 1 Introduction
sentdex
55 Raspberry Pi Part 8: First Download and Update! (Firmware)
Raspberry Pi Part 8: First Download and Update! (Firmware)
sentdex
56 Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
sentdex
57 Raspberry Pi Part 11: Remote Desktop
Raspberry Pi Part 11: Remote Desktop
sentdex
58 Twitter Analysis: How to rank a user's influence
Twitter Analysis: How to rank a user's influence
sentdex
59 GPIO Tutorial for Pi Part 2 - Programming the GPIO
GPIO Tutorial for Pi Part 2 - Programming the GPIO
sentdex
60 GPIO Tutorial for Raspberry Pi Part 1 - Setting up
GPIO Tutorial for Raspberry Pi Part 1 - Setting up
sentdex

Related Reads

📰
How I Built a Free Online Image & PDF Processing Platform with Vue 3 + FastAPI
Learn how to build a free online image and PDF processing platform using Vue 3 and FastAPI, and discover the benefits of combining these technologies for efficient file processing
Dev.to · IAMUU
📰
I Built a Free AI-Powered YouTube SEO Toolkit With Zero Budget. Here’s What Actually Happened.
Learn how a solo dev built a free AI-powered YouTube SEO toolkit with zero budget and the lessons they learned from the experience
Medium · Startup
📰
How to Create a Second Version of Yourself Inside Obsidian Using AI (Step-by-Step Guide)
Learn to create a second version of yourself inside Obsidian using AI with a step-by-step guide
Medium · ChatGPT
📰
How to prepare for Spain civil service TIC exam using AI in 2026
Learn how to prepare for the Spain civil service TIC exam using AI in 2026, boosting your chances of success with technology-driven study techniques
Dev.to · David García
Up next
I Asked Gemini to Build a Dashboard... I Didn't Expect This
Patech
Watch →