Moving Average Envelope (ENV) : Python Matplotlib Finance and Math Tutorials
Skills:
ML Maths Basics80%
Key Takeaways
The video demonstrates the Moving Average Envelope (ENV) indicator using Python and Matplotlib, covering its calculation, plotting, and application in stock price prediction and algorithmic trading. The tutorial provides a step-by-step guide on how to implement the ENV strategy using Python and Matplotlib.
Full Transcript
well everybody welcome to another Python mathematics and Finance tutorial video this video we're gonna be talking about the moving average envelope this one's pretty simple and simple enough to where I can condense it all into one video so first let me just show you guys this envelope it looks a lot like maybe these Keltner channels or Bollinger Bands so let's take a look at let's look at Toyota so here they are for Toyota it's the actually being plotted here right on price and so the idea of these is to you know encapsulate price and you can find when there's been you know like a breakout like for example we zoom in to maybe this area here we can see where price has broken out here and that's obviously a very bullish indicator but continues breaking out still bullish and then finally here it breaks out now we've got a bearish signal and again bullish bowl as well as bearish right so these are actually pretty simple what you're doing is you're just taking a simple moving average and then you're multiplying that simple moving or adding you know maybe a quarter or even like a quarter of a quarter times that simple moving average you've just Plus that and minus that to get the direction right so that also it will adjust itself by volatility right so like here you can see it's you know not too volatile as far as you know comparison to here is how much price it's been swinging around by right so the gap is much smaller here than it is here it gets much wider so so will automatically adjust and it's just like I said it's like really really simple and you can see it really comes down here it gets really thin there and then it comes back up again but anyway very simple because it's just a simple moving average and then you add like a multiple of that simple moving average to the simple moving average and boom you've got yourself some channels so yeah so let's go ahead and get into the actual programming of this to do that let's go to syntax calm the sample code is in there's a link for it in the description that should take you to here just come up to the script and copy and paste everything scrip and then go to an empty Python script and paste it in so I'll bring it over here let me just fit it to scream [Music] copy-paste and normally if you have been following along everything gets done right in here but for this video we're not going to do that because we want to plot it on that on the price right not on that bottom axes so we're going to come all the way up to basically here and just hit enter like a bunch make some space and then we're going to go ahead and comment out these two lines and because we've done that the legend is going to like scream at us so do a control F and in decibel box type in legend and you'll find this little block of code right here comment it all out so it will go one two three and one two three so now we don't have to worry about those prices being shown all that's going to come up now on that middle axes is the candlestick next what we want to do now is do this moving average envelope so tab over a couple times and we'll define env for envelope and this is going to have two parameters that we're gonna pass through prices and a time frame so whatever envelope time frame we want it's gonna have it's gonna generate three lines right so we need an upper line equals empty lower line equals empty and then we want that middle line and that'll be equal empty as well for now then we're gonna be using simple moving averages so I'm gonna say SMAS equals moving average whoops prices time frame now moving average we've already covered that in a previous video so if you want to know more about how calculated moving average check out that video next get rid of that slide next we want to say for each SMA in SMAS what do we want to do well we're gonna say upper lines so ul s equals each SMA plus and then in here you can put whatever value you want 0.05 seems to work pretty decent so we'll say 0.05 times each SMA so as you can imagine that just adds a little bit to the ESM right and that'll kind of help us create these like gaps right plus while it does that it's also going to react to that simple moving average depending on how much how fast it's changing so that'll make the gap either get wider or thinner and which is also kind of an indication of volatility change anyway next thing we want to do is lower lines equals and this is going to be the same thing only a minus so I'm just going to copy and paste this line and instead of adding you're subtracting and then middle lines is just equal to each SMA next we're going to say upper line dot append upper lines lower line dot append lines and middle line up and little eyes finally at the end once we've exhausted this for loop what do we want to do we want to return upper line lower line middle line so that's that now what we want to do is calm down Twitter cool so now what we want to say is upper line lower Lyme middle line equals env right EMP and then prices time frame so we're gonna use closed price and a time frame of ten so now all we really need to do is a x1 dot plot and we want to do date - SP : that's just our starting point if you wanna know more about that check out the charting stocks in python tutorial series they upper line don't forget your - SP : oops - SP : and in fact I'm just gonna copy and paste X well in your mind we're gonna copy and paste this whole function and we want that line to be white now I'm going to copy and paste this whole thing paste that twice bam bam and now upper line cool lower line and then middle line and that's really it we should be able to plot this now I believe looks good let's do it here we go so stock to Apple I'm trying of a stock we haven't actually plotted yet it's like a bajillion stocks and I just like you never think of it recently let's do let's do it big for Big Lots all right so obviously the lower indicator is empty because that was kind of like the way we left it so we could do other videos but this one as we can see we definitely definitely got it this one is obviously a pretty volatile stock it's got a lot of breaking out both on the top and the bottom going on here quite the swinging stock let me look at that that's pretty interesting stock if you could time those swings lots of money made there anyway so that's Big Lots as you can see that it did indeed work out for us I've got another one let's do Google and again so here's Google fit it over here a lot of breaking out for Google as well so if you happen to if you don't want to see like so much like cuz that's that's really where the signal comes from is where you have either a breakout on the bottom or a breakout you know on the top for bearish and bullish breaking out right so if you don't that you can change that obviously about coming over to your formula and say you wanted to see like you only wanted like a little like a solid breakout and that was the only thing you'd be interested in you it would raise this number okay so let's say 0.07 now now let's look at Google again and see if we have just as many or Big Lots probably another good one to look at anyway you have a lot less breaking out going on so if you wanted to create like an algorithmic trading system on this that said hey buy you would get a lot less buy signals but they'd probably be a little more solid when it came to the buying signal itself so like a good example of like the trading system that people would build off of this would be like right here right so every time it breaks up out you buy into it and then as soon as it comes back in you would sell out of it and then again you would have done the same thing here bought in and then you'd sold out up here and you know you would have made a decent amount of profit there the real question is did you make more profit on that time frame rather than just buying in here and holding all the way till today right and doing nothing so and probably not you probably made more money holding so anyway those are your moving average envelopes pretty interesting very basic and very easy to implement into any basically any trading system and also as far as like breakouts are concerned and buying you know you could just strictly calculate that upper line and every time price exceeded that upper line that would be a buy signal right and then you would hold that until price got back below that lower line sell signal and you're good to go so anyways that's going to conclude the moving average envelope in fact that kind of makes me want to save that idea for later and maybe we'll will back test the system of buying at you can use machine learning to figure out the best little line there or the best number to multiply by anyway back test that idea see how see if that actually works and see if that does actually beat buying and holding anyway ideas for the future hopefully you guys enjoyed hopefully you learned something new as always thanks for watching thanks for all the support and subscriptions until the next video
Original Description
This tutorial video teaches you about the Moving Average Envelope indicator, how to program it in Python, and how to chart it in Matplotlib.
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/sentiment-analysisbig-data-and-python-tutorials-algorithmic-trading/python-matplotlib-sample-code-charting-stocks-python/
Python: http://python.org
Numpy: http://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy
Matplotlib: http://matplotlib.org/downloads.html
Percent Change Video:
https://www.youtube.com/watch?v=7fIY_5m1yH0
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Matplotlib Python Tutorial Part 1: Basics and your first Graph!
sentdex
Python Encryption Tutorial with PyCrypto
sentdex
Python's Logging Function
sentdex
wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
sentdex
wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
sentdex
wxPython Programming Tutorial 3: Menu Bar and Menu Button
sentdex
wxPython Programming Tutorial 4: Panels
sentdex
wxPython Programming Tutorial 5: User Input Saved To Variables
sentdex
wxPython Programming Tutorial 6: Multiple Choice Input
sentdex
wxPython Programming Tutorial 7: Adding Static Text and Colors
sentdex
wxPython Programming Tutorial 8: Custom Button Images
sentdex
wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
sentdex
Basic PHP Tutorial 13: Multi-dimensional Array
sentdex
Basic PHP Tutorial 15: Functions and Global Variables
sentdex
Basic PHP Tutorial 12: Associative Array
sentdex
Basic PHP Tutorial 14: Foreach loop
sentdex
Basic PHP Tutorial 16: Include and Require
sentdex
Basic PHP Tutorial 7: Assignment, comparison and Logical operators
sentdex
Basic PHP Tutorial 4: Variables and Comments
sentdex
Basic PHP Tutorial 11: Arrays part 1, basic array
sentdex
Basic PHP Tutorial 6: If else and else if conditionals cont'd
sentdex
Basic PHP Tutorial 1: Intro to PHP
sentdex
Basic PHP Tutorial 3: HTML with PHP
sentdex
Basic PHP Tutorial 9: While Loop
sentdex
Basic PHP Tutorial 10: Switch Statement
sentdex
Basic PHP Tutorial 2: Print and Echo
sentdex
Basic PHP Tutorial 5: If else and else if conditional statements
sentdex
Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
sentdex
Basic PHP Tutorial 17: User Input Form Example / String Manipulation
sentdex
Basic PHP Tutorial 18: HTML Entities and forms cont'd
sentdex
Basic PHP Tutorial 19: Finding words in strings
sentdex
Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
sentdex
Basic PHP Programming Tutorial 22: Hashing part 2: salting
sentdex
Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
sentdex
Basic PHP Programming Tutorial 21: MD5 Hashing For Security
sentdex
Basic PHP Programming Tutorial 24: String similarity
sentdex
Basic PHP Programming Tutorial 25: Time and Time stamps
sentdex
Basic PHP Programming Tutorial 26: Die and Exit
sentdex
Basic PHP Programming Tutorial 27: MySQL Databases Part 1
sentdex
Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
sentdex
Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
sentdex
Basic PHP Programming Tutorial 30: MySQL database in Use
sentdex
Django Tutorial Web Development with Python Part 1: Installing Django
sentdex
Python Tutorial: File Deletion and Folder Deletion / directory deletion
sentdex
Python Tutorial: How to Rename Files and Move Files with Python
sentdex
3D Graphs in Matplotlib for Python: Basic 3D Line
sentdex
3D Plotting in Matplotlib for Python: 3D Scatter Plot
sentdex
3D Charts in Matplotlib for Python: Multiple datasets scatter plot
sentdex
Sikuli Tutorial 1: Visually programming in python!
sentdex
Sikuli Tutorial 2: Program visually in python!
sentdex
Sikuli Tutorial 3: Program visually in python!
sentdex
3D Bar Charts in Python and Matplotlib
sentdex
3D Plane wire frame Graph Chart in Python
sentdex
Raspberry Pi Part 1 Introduction
sentdex
Raspberry Pi Part 8: First Download and Update! (Firmware)
sentdex
Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
sentdex
Raspberry Pi Part 11: Remote Desktop
sentdex
Twitter Analysis: How to rank a user's influence
sentdex
GPIO Tutorial for Pi Part 2 - Programming the GPIO
sentdex
GPIO Tutorial for Raspberry Pi Part 1 - Setting up
sentdex
More on: ML Maths Basics
View skill →
🎓
Tutor Explanation
DeepCamp AI