In this video we are going to create an expert advisor that is able to open and close positions based on the values of the RSI indicator. So let’s find out how to do that with MQL4. To get started, please click on a 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 let’s remove the two comment lines here to calculate the RSI value. We can use the included function Irsi. It comes with MQL4 and we need to pass a few parameters.


The first parameter is for the current symbol on the chart. The second one is for the selected period on the chart. We want to calculate the RSI indicator for 14 candles. The result should be based on the close price and this last value here is a shift value. We don’t need that, so we use zero.


First we want to check if we have no open position. That would be the case when the return value for orders total is zero, the entry condition for a buy trade would be when the RSI value is below 30. That is when the blue line here is below the lower dotted line. And if these conditions are true, we are going to use order send for the current symbol on the chart to open a buy trade for ten micro lot. Afterwards we want to call a function that is called Check RSI buy stop and pass the current RSI value as a parameter. And to see what’s going on, we will use a comment output to output the text RSI value and the calculated value on our chart.


So far, so good – with the main module, but this function doesn’t exist so far, so we need to create it now. Our user defined function will use the return type void because we don’t need to return anything. The name is Check RSI buy stop and we need to pass the RSI value as a parameter. Now we use a for loop to go through all the open orders with order select, we can select a position and now we want to know if the order symbol for that position is equal to the symbol on our chart.


If that is the case, it’s the same currency pair.


We want to know if the order type equals Op buy, then it would be a buy order and another condition. We only want to close if the RSI is above 70. If all those conditions are true, we use order close for the current order ticket and the current order lot size to close the order for the bid price. The next parameter here is the so called slippage, and the last one is for the color. Afterwards we want to close the for loop and the function and that’s about it.


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 other basic videos first, or maybe even the premium course on our website might be interesting for you.


This was a suggestion from another Premium course member, and if you are a Premium course member and have an idea for a video like this one, please let me know. For now, please click on the compile button or Press F seven on your keyboard. You shouldn’t get any errors, and if that is the case, you can click on a little button here or Press F four to go back to MetaTrader and in MetaTrader you want to pick an empty chart, please click on Insert, Indicators, Oscillators, Relative Strength Index.


We will go with the default values for a 14 candle period. The result should be calculated based on the close price, so please click on OK. Now you should see the RSI oscillator below the candle chart now, right click into the chart.


Select Template, Save template and save it as Tester TPL. You can override it the current one because Tester TPL is what we will see in the next strategy test. Now please click on View Strategy Tester or press CTRL and R. Please pick the new file simple RSI buystop E X four, mark the option for the visual mode here and start your test.


Here we are. The expert advisor is running. It might take a little while until the first position is opened. That should happen in a few seconds. Here it is.


So now we need to wait until the RSI value is above 70. It looks like it could happen in a few seconds. Now the position was closed because the RSI is above the upper dotted line. So our little expert advisor is working as expected. And in this little video, you have learned how to create an expert advisor that is able to open and close buy positions based on the RSI indicator value, and you have coded it yourself with a few lines of MQL4 code.