MQL5 Tutorial

MQL5 TUTORIAL BASICS – 120 SIMPLE RSI BUY STOP


Listen Later

In this video we are going to create an Expert Advisor that is able to use the RSI indicator to open a position when the value is below 30 and to close it when the value is above 70. So let’s find out how to do that with MQL5. To get started, please click on the little icon here or Press F four on your keyboard. Now you should see the MetaEditor window and here you want to click on File, new file, Expert Advisor from template. Continue.   I will call this file simple RSI buy stop. Click on continue, continue and finish. Now you can delete everything above the on tick function and the two comment lines here. We start with an include statement to include the FILE TRADE.MQH. This is an included trade library and it makes it possible to use a few simple trade functions. To do that, we create an instance of the class Ctrade that will be called trade. Inside of the on tick function we need to calculate the ask price and the bid price, that can be done by using symbol info double for the current symbol on the chart. We use symbol ask or symbol bid, and with normalize double and underscore digits, we can automatically calculate the right number of digits behind the dot. Let’s continue and create an RSI array. Afterwards we want to sort the RSI array with array set as series, that will sort the array from the current candle downwards and the included function Irsi will make it possible to calculate the current value for the current symbol on the chart and the currently selected period on our chart.   We will do that for 14 candles and the calculation should be done based on the close price. With copy buffer we can now fill our RSI array with data according to the definition that we have created here. We do it for buffer zero. We start with the current candle zero and we want to copy the values for three candles and store them in the RSI array. And that makes it possible to calculate the current RSI value by using normalize double for candle zero in our RSI array. I would like to have two digits behind the dot because that is also how the RSI will show up on our chart, and if we have no open position, that would mean that the return value for positions total equals zero, and if the RSI is below 30, that would mean we have a buy signal.   We use trade buy to open a test position for ten microlot.   Afterwards we want to call a function that is called Check RSI buy stop and pass the current RSI value as a parameter. Let’s add a comment statement to show the RSI value and the calculated value on our chart. This is the end of the main function, but this function doesn’t exist, so we need to create it now. The name of our user defined function will be check RSI buy stop, here we will get the parameter RSI value that we need to calculate if we want to close the position.   Now we use a for loop to go through all the open orders and for each counter value, we want to get the position symbol. That is done by using position get symbol for the current counter value.   Let’s remove this German comment here. We also want to get the position ticket, that is done by using position get integer and as a parameter we use position underscore ticket here. Finally, we want to know the position type. And we can do that by using position get integer and the parameter this time is position type, because that will make it possible to check if the current position type equals position type buy. That would mean that we have a buy trade. And if that is the case, we want to check if the RSI value is bigger than 70.   And if that is true, we use trade position close to close the current position ticket. Finally, please close the if statement and the user defined function here and that’s about it. Well, if this was too fast for you or if you have no idea what all the code here does, maybe you want to watch one of the beginner videos, or maybe even the Premium course on our website might be interesting for you. That’s where you also can find some links to down...
...more
View all episodesView all episodes
Download on the App Store

MQL5 TutorialBy Raimund Bauer