Best MACD Trading Strategy MT5 Expert Advisor Programming (Highly Effective)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey this is renee and in this video i want to show you how to write a fully working macd or make the expert advisor that trades the signals or the crossovers of the macdn signal line so in the background here i already prepared the metatrader 5 with um with a us dollar japanese yen chart and the macd attached to it you see the macd indicator down here in this separate window and you um if you do not know this indicator it prints two values pretty much one is this signal line which is red in this case and the other one is the macd value or um yeah the macd line pretty much um which is um yeah the other value and you can see these two values cross at some point for example i marked um the crossovers here with this vertical lines and you can see whenever there is a crossover there is a chance to make a trade for example if the macd line crosses the signal line from below then with a close of the bar that um finishes this crossover we start so with the open of the following bar we then enter a bite rate and we follow this trade as long as the macd line stays above the signal line and if the signal line drops below the macd line then we turn around this trade and we enter a citrate so for example we would make this by trade from this point pretty much to this point then we would enter a sale trade here and close it yeah around here there are of course not always winning trades because there is a huge problem for this system which is market without clear trends for example in this area here you can see there are a lot of lines a lot of vertical lines because the macd and signal line cross yeah like really often in a short period of time and this is always if there is a market without a clear trend direction in this case you would of course lose money because here you would buy for example here you would sell which is a loss then here you would just you would have another small loss and yeah another small loss um so in in in markets without clear and directions you make losses but then in trend markets you make or you can make huge profits and the question is pretty much what do we see more of trend markets or markets without a clear trend and to evaluate this it always makes sense to write a little program or expert advisor in the metatrader 5 to be able to use this program in the strategy tester and check if there is a setting for the strategy that generates profits so let us write this program first and to do so we click on ide or we click on tools metacore's language editor which opens the editor for us then we can click on new in the upper left corner click on expert advisor click on next and we can provide a name for the system for example macd system we click on next next and finish and the metatrader5 automatically generates a template for an expert advisor we then remove all the lines that we do not need which is these gray lines they are just comment lines and they do not have any impact on the functionality of a program so we simply remove them and then i always like to rearrange the brackets just a little bit so it looks cleaner for me i mean this is just like a habit of mine you might have a different style of placing your brackets but i always put the opening bracket behind a function name and the closing bracket at the very first or at the at the line after the last statement inside of a function then we have three properties left which we can simply erase because they also do not impact this program so what we have to do first is we have to make sure that we receive these two values the macd value and the signal line value so to do so we need to create a handle for this indicator because there is a function in the metatrader5 which is called imacd and this little eye here stands for indicator i think and macd is of course the name of the indicator and this function um demands several parameters first of all we have to provide the symbol that we want to calculate this um handle for so we simply provide underscore symbol as a system variable which always holds the um symbol name of the current chart and by the way you can click on any word or you can move your cursor inside of any word or function name or variable name and you can click on f1 in your keyboard and this should automatically open your mql5 reference if it doesn't work for you you can also click on help and mql5 reference and search for the entry using this index search here for example you can search for the underscore symbol variable here and yeah this will also work afterwards we have to provide a period and in this case we can just choose the current chart period by providing the period current id here from this chart time frames enumeration you can also look this up in the mql5 reference if you click on period current and click on f1 so afterwards we then have to provide the parameters of this moving average indicator because if you open the settings of this moving average indicator you can see there are four inputs that are requested here so we can provide all these four inputs now simply in the order they appear here for the indicator inputs first of all the fast ema period the slow emi period the signal sma period and the applied price so we can simply copy the values here which is oh i forgot already i think 12 26 and 9 so we got 12 26 and 9 and you can also see the function or the media editor always provides a the signature of a function if you enter the the parentheses after the identifier for this function um if you if you fill in the parameter variables here or the values for the parameter variables and then we have the applied price which is typically the price clause which means that we base our calculations on the close prices of the bus so what we did now is we created a handle and if we compile this program you can see that it compiles without any errors so we create a handle for a specific macd indicator and we store this handle which is simply a integer value inside of this handle variable the handle variable was declared above the on init function on a global scale so we can use it in any function and yeah this is an integer type variable so this is it for the two lines that we wrote until now so a handle is simply a pretty much a description for a specific indicator because here we provide all the necessary information for the meter trader to know what exact macd we want to we want to have for this for our calculation so if we now go ahead and use the copy buffer function which is a function and we can enter the documentation again i will not um yeah pretty much explain everything in detail but this function takes a handle a indicator handle which we have here of course and receives the values from this specific indicator and stores it inside of an array so what we can do here is we can provide or handle as a first parameter then we can provide a buffer number and to receive the correct buffer number it is always a good idea to look inside of the documentation here because sometimes you will find a note here which says that for example um the buffer number of the main line is zero or simply the identifier main line and for the signal line it is one or the identifier signal line so we can do here is we can provide the main line identifier to make sure that we will receive the values of the main or macd line here and then we start at position one and we have a count of two what this means um yeah we'll explain in a second but first of all we have to create a macd array here which we can then provide as a last parameter for the copybuffer function because we have to provide a double array where we store the values in so an array is simply yeah pretty much a field that holds several values of the same type for example in this macd array we can store multiple double values and we can access the values inside of this array by providing the index of the requested value for example if i write a comment in the chart which will appear in the upper left corner which you will see in a second we can write for example macd0 and then we can go ahead and write macd 0 which is then the value inside of this macd array at index 0. and we can write another line macd 1 for example which will then be the mscd value at index 1 inside of this array so if we attach this expert advisor to the chart and we can simply um look at the navigator navigator there and we will find the mrcd system in the expert advisors folder here so we can double click it and attach it to the chart and you will see in the upper left corner we have two text outputs which is um yeah two values and we we can compare these values here to the values in the data window here and if you look at the [Music] this value here it should be the value that you can see here in the data window so we can see this value is minus 0.0510 and this should be the value that that we see here it is rounded of course so what we can do here is we can round the output so we can write double to string for example which will then show only i think it's five digits it's four digits okay four digits like this and we can say double to string again and also make sure that the second value is only displayed with four digits like this so you can see that this is um the mscd value for this bar which is stored at index one inside of a ray and this should be the other value at index 0 and this is working indeed so we can do the same for simply simply copy these two lines for the signal line so we can say we want to receive the signal line values we simply have to provide another buffer number here and we also want to receive the values for the last two bars and store them inside of this signal array here then we can also go ahead and oops copy this line one two times and we can print the signal line values for the last two bars so let me adjust this real quick signal and zero and signal at index one if we do this you should see that we also oops we have to erase one of these closing brackets here so you can see we will now receive the signal line value and the macd value and there it is there's the update and yeah you can see we receive the values for the two last bars here what we have to do now is we have to check if there is a new bar in the chart and if there is a new bar then we have to check if we have to open a trade so to check if there's a new bar in the chart the easiest way is to um get to keep track of the total amount of bars and you can do this by for example creating a global variable and we can initialize this in the unlimited function and we will initialize it with a return value of the ibus function the ibar function is a function that returns the amount of bars the amount of bars for a specific symbol in a specific time frame so if we do it like this we will then have the amount of bars stored inside inside of this bus total variable and we can for example to visualize this i can print it in the chart here by adding another line to our comment so you can see inside of this chart we currently have uh 48 000 blah blah blah bars so a little bit more than 48 thousand and what we can do now is we can do the same calculation we can make the same calculation here in the untick function so we can check if i or we can receive the the current value which is returned by the ibas function and whenever there's a new bar the value inside of this bus variable will be greater than the value inside of this bar's total variable so we can check if bars is greater than borrow's total and if this is true then we want to update the bar's total variable and yes store the value of the bus variable inside of it so this ensures that we will only calculate everything inside of this if statement here only once if you do not know what an if statement is and what it does check out the other tutorials on this channel that i already published where i explain if statements um in in more detail so if we do it like this we should only update this or we should only do this calculation here once if there's a new bar so what we can do here is we can check if there's a signal for us so we check if the if the macd value of the last bar is below or above the signal value of the last bar and the macd value of the bar before the last bar is below the signal value of the bar before the last bar if this is true then we can be pretty sure that there was a crossover which is a by crossover or a long crossover so we can write a a print statement or we can provide a print statement like this which will then [Music] print a message in the expert's journal for example by crossover and if this is not true then we can check if there was a say crossover by simply changing the operator's theorem so we check for the opposite event which is a cell crossover like this and if we print function [Music] this again we can then say this was a cell crossover like this um function okay if we do it like this we can already run this program in the tester so to open the tester you click on view strategy tester in the strategy tester we can then use this drop down to select a expert advisor that we want to test of course of course we will choose the macd system and we can provide a symbol and a time frame i will just leave it as it is because i simply want to test if the crossovers are yeah identified by the expert advisor and documented in the journal here so let us wait until the test is loaded i can clear the journal real quick and what we have to do now is we have to wait for a crossover here so once there is a crossover we should then see um messages in the journal here and you can see there are messages in the journal so let us check the last messages for example we can have a look at this message which which was at 845 which is here and you can see there was a crossover indeed so the previous course over was here i think 730 and this is also correct and we have 605 which is here so we can say that this program is working so we realize the crossovers what we have to do now is we have to open positions and whenever we want to open positions in the uh metatrader 5 it is the easiest way is to use the trade class and you can find this class in the trade.mqh file which is located in the include folder here in the trade subfolder you will find the trade.mqh file which then holds this ctrade class here and this is a kind of confusing file i think so the easiest way to explain this is this helps us to open close or manage our trades and orders so simply include this file by using this include statement here and providing the path to this file which is trade and then a slash and a trade.mqh which is the file name you put this in these um yeah graders greater than smaller than signs so the expert advisor knows what file you want to include so if you did this you have you then have access to the ctrade class and we can create a object variable of this class which we can simply name trade for example we can now go ahead and say trade buy and we can open a buy trade if there's a buy crossover so we can say the volume for this trade is for example 0.1 we can say the symbol shall be the current chart symbol and we can also provide a price a asl entertp and a comment so we can say for example the price shall be the market price so we can simply provide zero and yeah first of all we also provide zero for the tp and sl then we copy this line here for the cell crossover and we simply exchange the buy method or function of this trade class here and use the sale function instead which will open a cell trade if we compile this this will already work but what we also want to do is we also want to make sure that whenever there is a oh wait let us test this real quick but afterwards we want to make sure that whenever there is a buy signal or a sales sell signal that before we execute a new trade we want to cl close a previous trade if there is still one um open which um yeah which trades in the opposite direction and yeah you can see there is a bite right now since there was a by signal and now we have a cell signal directly afterwards which will then open a cell trade and yeah you can see there is um yeah there are several signals and um several trades open because of these signals so what we have to take care of is um closing a by trade if there's a cell signal and closing as saturated there's a bicycle so back to our code we can simply check if there's a open by position before we open a citrate or what we can do is we can pretty much create a variable here which will be of type unsigned long which is just a a really big number and we can say this is the ticket of all um positions that we open okay there is a problem i think we cannot use ticket because it is used in the cta class already so we can say uh position or position ticket i don't i don't know like this maybe and we can say if if if we open a trade um we want to check if this was successful so we can wrap this inside of a if statement and if this was successful we want to update our position ticket variable and say we want to have the reside uh order ticket and store this order tickets inside of the position ticket variable and we can receive the result order ticket by using the result order function of this um yeah c trade class and we can do the same if we open a sale position reside order like this okay if we did this we now have the the position ticket of the [Music] currently open position stored inside of this position ticket variable i can attach this to the to the comment in the chart and you can see whenever there is a position opened the position ticket should be updated and we can see it in the upper left corner so there is a first position which is position ticket 2 and if there is a new position we see that the position ticket in the upper left corner is updated so what we can do now is we can use this position ticket to select a position um before we open a new position so we can check if position ticket is greater or greater than zero and if we are able to select a position position select um using this the second number so we can check if we're able to select a position and once we selected a position we then have access to all the position properties so we can say for example we can check if position get integer position type is equal to position type cell so we can check if this currently open position is a save position and if this is true then we want to check if we are able to close this position we use the position close function which is part of the c tray class and which we can access by using the trade object here and we can then provide the position ticket here to close this position if this was successful we can then reset the position ticket to zero and maybe we can also yeah right write a new information in the journal sale trade closed cell trade or sale position closed closed so we always know what happened and we can also provide for example the position ticket so we know what position was closed and we can do the same before we open a sale trade so we can just copy and paste it we can check if the currently open position is a byposition and if this is true we close it and have a information printed in the journal what we also have to do now is we have to check before we open a new position if the position id or the the value in the position ticket variable is smaller or equal to zero so in this case we can be pretty sure that there is no open position position ticket and there's one more thing that we can do whoops let me rearrange this we can also check inside of this if statement here or we can say if if we are not able to select a position then we want to reset the position ticket anyways because um it might be or it might happen that the user closes a trade or that a trade reaches the tp or sl so we can say position blah blah blah was closed already for example and we do the same of course before we open it by trade okay so let us restart this program make sure to always compile after you did some changes so these changes will be applied to the executable file so we just restart the program and then um yeah let us see after we open the first byte rate if this buy trade is closed um when there is a signal so whoops okay the first rate is the safe rate and this sale trade is closed and something doesn't seem to work because we only see cell traits so let's get back to our code oh okay of course this is because there we only check if yeah we have to compare the position ticket to zero and check if it is smaller or equal zero before we open a by trade so if we do it like this and we start the program we should see that it is working hopefully and that it is opening buy and sell positions so what we do here is we can speed it up a little bit we check if there is a buy position which is then closed once the cell signal appears okay the by position is there and let's wait a second sell signal by position closed you can always read it in the in the journal here um save position number four is closed okay so we have a new buy position already which is um not updated in the chart yet yeah but what you can see here is um the uh it is pretty pretty easy to see that this system is working so whenever there's a buy crossover we open a buy trade if there's a sale crossover we open a citrate um but what we what we already realized is we might have huge profits like these trades here or this trade was also a huge profit but we also have trades like this trade for example where where the market moves in the right direction for a short period of time but then comes back and closes um with a loss so to prevent this from happening we can already catch the profits using atek profit we can do this by providing a take profit to the buy or sell function here so what we have to do here is we have to calculate a by end tp so what we do is we take the ask price of the current symbol which we can receive using the symbol info double function here and providing the symbol ask identifier and we can then take this ask price and calculate it t3 tp so for example we can say ask plus 100 points um this shall be rtp price and what is always recommended if you work with prices you should always round them because there might be problems if you have digits because the pc um yeah sometimes adds a one for example at the um x at the at the um yeah at some point in in your um in your double and this may cause problems so you should also always round prices like the buy ntp price so what we do here is we do the same thing for the sa so we also want to have 100 points distance from the open price which is the ask price of course for a buy position as asl price and then we simply have to provide the sl here and the tp like this and this should take care of the sl and tp placement so we do the same stuff of course before we open a sale position what we have to do here is we have to receive the bid price because sale positions are always open at the current bid price if it is a market position and then for the tp we have to subtract the tp points from the bit price of course and for the sl we have to add the points because the sl is always above the current price for a sale position and yeah it is the other way around for a buy position make sure to then update these parameters for your buy and sell function by providing the sl and tp variables that we just created here so if we compile this and restart the program we can then see that there is a as lntp placed whenever a trade is open so let's speed it up a little bit and yeah it always takes some time for the tester to load so what we want to do here is we want to check um if there is atp and sl and this is indeed happening so we have atp and sl placed whenever there is a trade opened and yeah you can now go ahead and use this tester to make backtest because whenever you test you of course receive all the um information for example in the graph or if you end this test then you will have a another tab here which shows all the important key figures so you can just go ahead and take this program here and yeah test the macd strategy yeah and you can figure out if it is better to use atp and sl or if it is maybe better to simply yeah use the the basic macd strategy so um and and what you can also do is you can also play around with the macd and sl values for example we could say i want to have a bigger tp of for example one 500 points so i simply adjust the calculation here and if i compile this and restart the program you will then see that the tp is um is far away and the sl is pretty close so we have a higher chance than uh than than we risk so wait a second for the first trade to open and you can see the tp is rather far away and the the sl is really close so this might also be a good solution so um yeah i mean you have to make sure that the tp is reached this was maybe a little bit too far away but yeah you can you can already see you can play around with this system as soon as you created a basic skeleton for the strategy this was really easy it was under 100 lines of code make sure to copy every thing as i did here to make sure that you receive the same program and yeah then just go ahead test it and play around with the system and yeah maybe you'll find settings or um a simple time frame combination that works for you and that generates profits so let's talk about this program just one more time first of all we have to include the trade dot mqh file to um to get access to the c trade class make sure that you write these um smaller than greater than sign and the correct path because otherwise you would um get an arrow and you are unable to include this ctrade class then we create two integer variables on the global scale which is the handle variable for our imacd return value which is the handle for the macd indicator and then we have another integer variable which is the total amount of bars inside of a chart because we want to keep track of new bars appearing in the chart so yeah we have two more global variables which is the c trade class variable so in an object variable which is named trade and which is of type c citrate and then we have a unsigned long which is just a really big number variable which is called position ticket so in our on init function which is called whenever the program starts we then create a macd handle so we provide all the information to tell the pc what exact macd we want to use for this program and if you adjust these values the output of your test would already be different so you can play around with these values here to use different macd indicators to have different outputs maybe it would be better to have a faster macd or a slower macd and you can adjust the values and you will yeah of course receive different outputs then yeah we initialize the bars total variable here because we do not want to calculate as soon as the test starts we simply want to calculate if there is a new bar and in the untick function which is called whenever there's a new tick on the chart we then check if the current amount of bars in the charts is greater than the value stored in the bar's total variable if this is true then we want to update the bus total variable and we want to make all the necessary calculations to check if we have a signal first of all we have to receive the macd values for the last two bars we use the copy buffer function to to achieve this and we provide the main line and signal line identifier to tell the pc what exact values we want to receive from this macd indicator which is identified by the handle that we can provide here and we store the um the calculated values inside of two arrays here that we created and they are called macd and signal and we can yet then use the values inside of this these arrays and we can access them by using the indexes we can use these values to check if there was a crossover so if we have a byquez or one we first of all check if there is an open position and if there's no open position we reset the position ticket number to zero and if there is a open position we check if this is a sale position and if it is sa position we use the position close function which is part of the c trade class and we can provide a position ticket which is of course the position ticket that is stored inside of our position ticket variable here to close a existing sale position if this was successful we then reset the position ticket variable afterwards we check if the position ticket variable is smaller or equal zero and if this is true we then calculate the tp and sl4 trade and we use the buy function which is also part of the ct class to open a by position in the current symbol and make sure to play around with these values here for example you can adjust the volume you can say you want to trade a little bit a smaller volume or you can say you want to provide different tp or sl values so just go ahead and adjust these values here same is uh is possible of course for the cell crossover you can also adjust the tp and sl price here you can also adjust um the the lot size here and yeah you can see what changes um for your testing reside and yeah in the end we simply have a chart comment which is just a information in the chart in the upper left corner so we always know what the program does okay i think this was a yeah really cool program uh really short using the macd indicator to um yeah to check if it is a profitable system um so make sure to make your own testing and your own adjustments to this program to find your perfect setting if you want to learn more about automated trading in the metatrader 5 make sure to check out the first link in the description which is a fully a complete course on metatrader5 programming where we talk about basic topics like variables and classes in much more detail so for this video it took me like maybe 30 minutes to explain a whole program but in the metatrader 5 master class it would take me like 30 minutes to explain everything you didn't you need to know about variables so it is far more detailed you really get deep into the process of writing automated trading strategies for for them for the metis trader 5 and i guarantee that you will write your own programs after finishing this course so go ahead check it out and i hope that you yeah i can use this program wish you all the best good luck and good trades i'm out bye
Info
Channel: René Balke
Views: 695
Rating: undefined out of 5
Keywords: Trading, Programming, expert advisor, EA, automated trading, mt4, metatrader 4, mt5, metatrader 5, forex robot, robo trading, algotrading, forex, cfd
Id: DlVPBfNbuag
Channel Id: undefined
Length: 40min 2sec (2402 seconds)
Published: Fri Nov 12 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.