In this video we are going to create an expert advisor that is able to open a sell trade when the RSI Oscillator is above 70 and to close the trade when it reaches a value below 30. So let’s find out how to do that with MQL five. To get started, please click on the little button here or Press F four on your keyboard. Now you should see the Meta Editor window and here you want to click on File, new File, Expert Advisor from Template. Continue.


I will call this file simple RSI Sell 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 one comes with MQL 5 and we use it to create an instance of the class C trade that will be called trade, and we are going to use it to open a test position. Inside of the on tick function we need to calculate the ask and the bid price. That can be done by using symbol info double for the current symbol on the chart. We use either symbol ask or symbol bid, and with normalize double and underscore digits, we calculate the number of digits behind the dot. Let’s continue and create an RSI array. We use array set as series to sort our array from the current candle downwards and with the built in function Irsi we can calculate the RSI value for the current symbol on the chart and the currently select period on that chart.


We want to calculate it for 14 candles based on the close price and with copy buffer, we can fill our RSI array for the current buffer zero starting from the current candle zero and store the values for free candles in our array according to the RSI definition that we have created here.


That makes it possible to calculate the current RSI value by looking into candle zero for our RSI array, I used normalize double and two to create an output with two digits because that is what the RSI looks like. And to have a demo position, we want to know if positions total equals zero. And if the RSI value is greater than 70. If both conditions apply, we use Trade sell to open a sell trade for ten microlot. Afterwards we want to check the RSI sell stop. That is a function that doesn’t exist so far, so we need to create it in a few seconds and we will pass the RSI value as a parameter here.


But before we do that, let’s create a chart output by using the comment statement to output the RSI value on the chart. Now let’s continue with the user defined function that is called Check RSI Selll stop. We pass a double parameter for the RSI value here. Now we use a for loop to go through all the open orders. We want to check if the current position symbol equals the position symbol for the current counter value in the for loop.


We use position get integer to get the position ticket and we also use position get integer to get the position type. Now let’s check if the position type equals position underscore type underscore sell. If that is true, we have a sell trade and that’s when we want to check if the RSI is below 30. That is done with this if statement. And if this is true, we use trade position close to close the current position ticket.


Now let’s close the if 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 is good for, you maybe want to watch one of the other videos first, or maybe you even want to consider to become a premium member. You can find the premium course on our website. For now, please click on the compile button or press F seven. I have an error here. I need to remove the additional bracket.


Now let’s recompile the code and this time it works without any errors. And if that is the case, you can click on a little button here or press F four to go back to Meta Trader and in Meta Trader, please click on View, Strategy Tester or Press Control and R. Now we want to pick the new file. Simple RSI sellstop EX five. Mark the option for the visible mode here and start a test. Okay, the expert advisor is running.


I will speed this one up a little bit, and now we have an open trade that should be closed when the blue line is below the lower dotted line. That should happen in a few seconds. Yes, our little expert advisor works as expected. And in this little video, you have learned how to create an expert advisor that is able to open a sell trade and to close it based on the RSI value. And you have coded it yourself with a few lines of MQL five code.