YTread Logo
YTread Logo

Algorithmic Trading Using Money Flow Index (MFI) and Python

Algorithmic Trading Using Money Flow Index (MFI) and Python
hello everyone and welcome to this video on the

python

programming language so in this video i will be programming or at least attempting to program a

trading

strategy called

money

flow

index

to determine when to buy and sell stock now for those of you who don't know anything about the

money

flow

index

well it is also known as volume weighted rsi and it is an oscillator that uses price and volume data for identifying overbought or oversold signals in an asset now a

money

flow

index

level above 80 is considered overbought which is an indication to sell and a level below 20 is considered oversold which is an indication to buy also note that levels of 90 and 10 are also used as thresholds now currently i'm on google's website it's called collab.research.google.com and i'm on it because it makes it really easy to start programming in

python

so all you have to do is go to this website and then log in

using

your google account and get started writing your

python

code so if you are ready to start programming be sure to hit that like button and if you're new to this channel then hit that subscribe button and let's start programming so the first thing that you're going to want to do is click on file and click on new notebook and a new tab will open up for you and then eventually a new cell will open up for you and in this cell i'm going to put a description and comments about the program before i write any logic to the code so let's go ahead and...
algorithmic trading using money flow index mfi and python
do that now i'm just going to type description okay so i'm going to put this program uses the

money

flow

index

to determine when to buy and sell stock all right and let me show them spelling description correctly okay so that looks good next i'm going to create a new cell by clicking this code button here in the top left and in this cell i'm going to import the libraries that i plan on

using

throughout this program so i'm going to import warnings next i'm going to import numpy as mp i'm going to import pandas as pd and i'm going to import matplotlibe dot pi plot as plt okay and i'm going to give the plot a style so i'm going to type plt.style.use and i'm going to use the 5 38 style and next i'm going to filter some warnings so i'm just going to type warnings dot filter warnings and i'm going to input ignore and let's go ahead and run this cell all right so no errors that's going to create a new cell and in this i'm going to load the apple stock data so from google.colab i'm going to import files and then i'm going to type files dot upload and let's go ahead and run this cell to upload the file and let's click on choose files and then i'm going to upload the aapl.csv file okay so it's going to create a new cell okay now in this cell i'm going to get the data so i'm just going to type df and set it equal to pd.read underscore csv and then input the name of the file which is...
algorithmic trading using money flow index mfi and python
aapl.csv and then i'm going to show the the data so just type df and let's run this okay so now we can see the date open price high price low price close price to just close price and volume and we can see that we have 238 rows of data and seven columns and the date range is from october 1st 2010 to 9 9 2011. okay and we have our

index

here as integer values all right so i actually want to change the integers so i'm going to set the

index

to be the date instead of those integer values so just type df and set it equal to df.set underscore

index

and we're going to input pd dot and then we bring that down okay so i'm gonna put pd.date time

index

and i'm gonna input df date dot values and let's rerun this again and now we can see that we have the date as the

index

okay so it's going to create a new cell and in this cell i want to visually show the data so just type plt dot figure and we give our figure a figure size so we'll set figure size equal to 12.2 by 4.5 and let's plot the data point so type plt dot plot and then i'm going to plot the closing price and i'm going to give this a label so i'm going to call it close price all right next i'm going to give the plot a title so i'm going to type lt.title and i'm going to call this close price and actually i'm going to call it apple close price apple close price okay all right next i'm going to give the x access a label so type lt.x label and i'm going to...
algorithmic trading using money flow index mfi and python
call it date and let's give the y access the labels to type plt.y label and i'm going to put close price usd and put a little dollar sign okay next i want to give it a legend so type realty.legend and type df dot columns.values and i'm going to put this in the upper left location so type lock equals upper left and then type plt.show and let's run this okay and here is our chart so we can see the price of apple stock is or was pretty much increasing at least for this data it was pretty much increasing so let's go ahead and create a new cell and now in this cell we're going to start doing some preparation for the calculations for the mfi calculations so i'm going to calculate the typical price and actually yeah let's calculate the typical price so i'm just going to um create a variable called typical underscore price i'm going to set this equal to df close plus df high plus df low okay all of that divided by three and let's go ahead and type typical price to take a look at it and let's run this okay so now we have the typical price it's going to create a new cell in this cell i'm going to to basically um get the period so typically the

money

flow

index

uses a period of 14 days so i'm going to create a variable cost period and set it equal to 14 and let's go ahead and run this let's create a new cell okay now in this cell i'm going to calculate the

money

flow

i'll create a variable called

money

...
underscore

flow

and i'm going to set this equal to the typical price typical underscore price times df volume okay and then let's go ahead and show the

money

flow

and let's run this okay so that's because i misspelled

money

flow

here and now let's run this all right so now we have the

money

flow

calculated we have the typical price calculated let's go ahead and create a new cell and in this cell i'm going to get all of the positive and negative

money

flow

s all right so create an empty list called positive underscore

flow

i'm going to set it equal to empty list and i'm going to create a list called negative underscore

flow

and set it equal to empty list okay um next i'm going to loop through the typical price so here i'm going to put this loop through the typical price all right so for i and range one to the length of the typical price we're going to check a few conditions so we're going to check if the typical price at position i so this is the current typical price we're going to check to see if it's greater than the previous typical price so typical price at position i minus one and if it is then we're going to append to positive

flow

so positive

flow

dot a pin we're going to append the

money

flow

okay so we're gonna pin the

money

flow

at position i minus one all right and since we append it to the negative

flow

i'm sorry since we append it to the positive

flow

we're now going to append to the...
negative

flow

a value of zero all right so let's check the next condition so else if i'm just going to kind of copy this here by highlighting it and then pasting it here and i'm going to change just a few things so i want to check to see if the current typical price is less than uh yesterday's or the previous typical price and if it is then i'm going to append this time to the negative

flow

i'm going to append the

money

flow

the previous

money

flow

okay and then i'm going to append a value of 0 to the positive

flow

all right else else i'm just going to append 0 to both the positive and the negative

flow

so positive

flow

dot of pin zero and then here negative negative negative underscore

flow

dollar pin okay so now we have our positive

flow

and our negative

flow

list so let's go ahead and run this all right so everything seems to have worked so let's go ahead and create a new cell now i'm going to get all of the positive and negative

money

flow

s but this time within the time period so within the 14 days okay so let's create a few variables here so i'll create a variable called positive underscore mf which would be short for

money

flow

i'm going to set this equal to an empty list i'm going to do the same thing for negative underscore

money

flow

i'm going to set that equal to an empty list now for i and range period minus 1 to the length of the positive underscore

flow

i want to append to my positive underscore...

money

flow

list so what am i going to append i'm going to append the sum from positive

flow

i want everything from let me think i want everything from period minus no no i want everything from i plus one minus the period to i plus one okay so i believe that's going to get periods uh 0 to 13 something like that don't quote me on it i think i'm not sure if it's inclusive or not i really forget sometimes but um it should be 0 to 13 so it should be 14 values all right so that looks good what i'm going to do is i'm going to just copy this and if i'm wrong then we'll change it later on and i'm going to paste this here and let's put a negative here negative and and then negative and negative okay so now i'm getting the positive and negative

money

flow

s within the time period so i'm hoping i am getting this correct and i think i am so let's go ahead and run this all right so there's a few things that are incorrect here it looks like i am misspelling something here so um where am i spelling misspelling right here so let's go ahead and get rid of that and let's run this again okay so now that looks good so let's go ahead and go back and kind of recap first i'm going to calculate the typical price so let's run this and then i'm going to get the period and i'm going to calculate the

money

flow

and then we're going to get all of the positive and negative

money

flow

s and then we're gonna get all...
the positive and negative

money

flow

s within the time period which is uh 14 in this case right let's go ahead and run this cell okay and now we're going to actually calculate the

money

flow

index

so i'm going to create a variable called mfi and i'm going to set this equal to 100 times mp.array and i'm going to use the positive

money

flow

that i just created let's bring that down and i'm going to divide it by the positive

money

flow

plus it's going to be the negative

money

flow

so i'm just going to change this here to be negative and then let's go ahead and show this so just type mfi and let's run this okay so there we go so now we have our mf5 values everything looks good it looks like we did all the calculations properly let's go ahead and create a new cell now and let's do some visualization so we're going to visually show the

money

flow

index

so just um let's see let's create a data frame so type df2 and set it equal to pd dot data frame okay and we're going to give this data frame a column so type df2 and i'm going to call this column mfi and we're going to set this equal to mfi and now let's go ahead and create the plot so just type plt dot figure and i'm going to give it a figure size actually you know what i'm going to do i'm going to come back up here where we already created a chart i'm just going to copy this data i'm going to highlight it and copy it and come back...
down here and just make a few changes just to make things go a little bit quicker okay so here i'm going to change this to df2 and i'm going to change this to mfi and i'm going to change this here to mfi and then the title will just be mfi and i'm going to get rid of the x-axis label and then the y-axis label will be mfi values okay and i want to plot a few more things so i'm just going to type plt dot a x h line and i'm going to input the value 10 because i just want i want to create the overbought line and the um oversold lines so that's what i'm creating here so just type line style and set it equal to dash dash and let's give it a color so i'm going to call the color i'm going to use the color orange all right i'm going to take this line i'm going to copy it

using

ctrl c and i'm going to paste it about four more times and just make a few changes so make this 20 make this 80 and then make that 90 and then i'm going to change the color here to be blue and the color here to be blue as well so hopefully i didn't mess up on anything let's go ahead and run this all right so there we go um this legend here is incorrect so for now i'm just going to get rid of that legend and let's run this okay so now that looks a lot better so we have our mfi values here to the left on the y-axis and we can see what the chart looks like over time okay and we can see that it goes past some of these um overbought and...
oversold signals okay so i think that i think that looks really good all right so let's go ahead and create a new cell and we're going to create a new data frame all right so create a variable called new underscore df and i'm going to set this equal to pd dot data frame and we're going to set it equal to so a new data frame equal to our old data frame so i want all of that data from our old data frame but this time starting at period and then i want to get all the rest of the rows all right and then i'm going to create a new column on this data frame it's going to be called mfi and i'm going to set it equal to mfi the

money

flow

index

that we created and next i want to next i want to i guess i can show it here i want to show the new data frame all right so just type new underscore df and let's run this okay so now we can see the

money

flow

index

here and then we see all of that data that we had before so it looks good but this time you can see that we only have 224 rows but we do have um one extra column now so let's go ahead and create a new cell and in this i'm going to create a function to get the buy and sell signals all right so it's going to create that function i'm going to call it get underscore signal and it's going to take in some data it's going to take in a high value this will be the the the lines that determine if it's over bought or oversold so that's high and low okay so it's going to create...
these two lists so one will be a buy list and call it buy underscore signal and i'm going to set it equal to an empty list and then i'm going to create a cell underscore signal and i'm going to set it equal to an empty list as well now for i and range length of the data set and i'll be more specific with the mfi column if the data at if the if the mf5 at this position i is greater than whatever we input for high then we know to buy so that's a buy signal so buy underscore or i'm sorry that's a sell signal so our buy underscore signal will append basically a nand value all right so nothing but because it's a sell signal our cell signal will append that value so that's going to be um data uh it won't append the mfi but it's going to append the the close price value is what i want so just type data dot or sorry data and then close at position i okay so i think that looks good i'm just going to copy this and let's paste this here so let's put else if else if it's lower than the low then the opposite is going to happen so i'm just going to copy this value here i'm going to cut it and i'm going to paste it here so now that's our buy signal so we're going to append the close price and then our cell signal will contain nand all right else both of our signals our buy and sell signal will contain nands so i'm just going to copy these up here and i'm going to paste them here all right and now that...
i'm done i'm going to return our two lists so i'm going to return the buy underscore signal and then the cell underscore signal and let's run this and let's create a new cell and let's see how well we did so i'm going to basically add two new columns to our data frame so here we're going to add new columns so they're going to be the buy and sell columns so just type new underscore df and create the by column so i'm going to set the by column equal to git underscore signal and we're going to input new underscore df and then 80 and 20 right those are the the signals that tells us um if our stuff is or if our assets are overbought or oversold i'm just going to copy this here and then paste it and i'm going to create a cell column and this time i'm going to return position one that's the cell signal okay and last but not least i'm going to show the data so just type new underscore df and let's run this all right so now we can see the two new columns over here and right now is a bunch of nand values but hopefully it has some actual close price values in there so it's going to create a new cell and the whole reason why we need to do that is so we can see it when we're plotting this so let's go ahead and plot the plot the data so what i'm going to do since i'm going to plot this um i'm just going to take what we i'm going to show both plots together now and we're going to show the buy...
and sell signal so i'm going to go all the way back up here and i'm going to just copy this here again so i'm going to highlight it and then copy

using

ctrl c and come back down here okay and i'm going to paste it all right so that looks good um this time i'm going to give this an alpha so i'm going to say the alpha equal to 0.5 and i'm going to add in a few more things now now i'm going to add in that or add in the buy and sell signal so just type plt.scatter and then input new underscore df and then put dot

index

and then new underscore df by column and let's give it a color i'm gonna set the color equal to green that tells us that it's you know that we should buy it the green usually stands for go right so go ahead and buy the asset and i'm gonna give it a label so i'm gonna say buy signal and i'm going to give it a marker so i'm going to set the marker equal to this up character i'm going to give it an alpha equal to one as long as the alpha's greater than this alpha it should look good all right and i'm going to change this from saying df2 to new underscore df and then same thing for down here new underscore df for now i'm going to just copy this and i'm going to paste it here and change a few things so i'm going to change this to cell and i'm going to change that green to red and i'm going to change this by to cell i'm going to change the marker to a v to show that it's...
going down and i think that looks good so there we go and i'm going to change this legend here i'm going to get rid of this and just put the location since i already have the labels and i think that'll do it so let's run that by itself okay so i think it looks really good so now we can see our buy and sell signals so if we take a look closely here we can easily see um the cell signal here so it's telling us to sell the stock data i'm sorry sell the uh sell the apple stock at around 12.5 usd right around here okay we can see between this time period which is pretty good and then right around here it tells us to uh buy and that obviously is a good time period to buy because we could see that the price went up and here it also tells us that we should buy the apple stock data and then we should sell it here so if we had bought here and we had bought here and sold here we would have profited right but then it goes even further and tells us that we should sell here so these are this strategy seems pretty decent it looks like we would not have lost

money

in this case this very specific case so let me see um if we had bought i'm going to say i'm just going to say 12 here 12 usd is actually lower than 12 vsd here and let's say we bought here which was about 1150 usd um this one here is a little higher let's just say we bought one share here and one share here so that's about uh 23.5 usd right and then we sold them both here for about i'm...
just going to round down to 12.5 um we would have profited right because we would had two assets that we would have sold for 12.5 so we would have profited all right and what else i want to do is i want to basically take the chart here and copy it and bring it down here and kind of put them all together just just to kind of see what it looks like together so i'm going to change this to new new underscore df and i think that looks good and everything else seems to look pretty decent so there we go so let's go ahead and run this okay and now we can see the two charts basically right here um really not on this one screen i kind of scroll down a little bit but if i were to zoom out we can see that this looks pretty good so let me let me go ahead and zoom out just a little bit so there we go so we have our two charts right here all right so we can also see that the signals are correlating with the the buy and sell signals here so i think everything looks really really good so down here is telling us to buy telling us to buy up here is telling us to sell up here telling us to sell and we can see that correlation with the with the chart so i think it looks really good but anyways that's basically it you can test out the strategy on other data sets and i would definitely test it out on more data different different companies and yeah i really hope you enjoyed this video i hope you found it uh helpful and entertaining i hope you learned something from it so thanks everyone...
for watching the video and a special thanks to everyone supporting this channel on patreon.com if you want to support this channel on patreon.com i will leave a link below in the description and i hope everyone has a great day and i will see you all in the next video