In this video we are going to create an expert advisor that is able to open a sell position when the RSI value is above 70 and to close the position when it is below 30. That should happen in a few seconds. So let’s find out how to create that with MQL Four. To get started, please click on a 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 let’s also remove the two comment lines here. We will start by creating a string variable for the signal, but we don’t assign a value here. Let’s continue and calculate the RSI value. That is done by using Irsi for the current symbol on the chart and the currently selected period on the chart. We want to calculate the value for 14 candles and it should be based on the close price.


The shift value is zero because we don’t need it. Now let’s check if we have any open orders. That can be done by checking the return value for orders total, and if that returns zero, we don’t have any open orders, and when the RSI value is above 70, we want to use order send to open a test order for ten microlot. It should be a sell position. Afterwards we want to check the RSI Sell stop and pass the RSI value that we have calculated here as a parameter, but this function doesn’t exist, so we need to create it in a few seconds.


Before we do that, please add a comment statement that will output the RSI value directly on the chart.


Now let’s create this function. The name of the function will be Check RSI Sell stop. We pass a variable called RSI value and the return type is void because we don’t need to return anything. Now let’s use a for loop to go through all the open orders. We select each trade by using Order Select.


Now we check if the order symbol is equal to the current symbol on the chart and we want to know if the order type for the current order equals Op sell. If that is true, it’s a sell position. Let’s add another condition. We only want to close the open position if the RSI value is below 30. If that is the case, we use Order close for the current order ticket and the current order lot size.


This expression here will give us the current ask price. This is our slippage and the color will be red. Finally, please close the for loop and the function 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, you maybe want to watch one of the other basic videos, or maybe even the premium course might be interesting for you. You can find that one on our website. For now please click the compile button here or Press F seven on your keyboard. You shouldn’t get any errors here, and if that is the case, please click the little button here or Press F four to go back to Meta Trader. And in Meta Trader, please open an empty chart. Select Insert, Indicators, Oscillators, Relative Strength Index. We want to go with the default values here. That is for 14 candles.


Apply to the close price. Now let’s click on OK. Here is the RSI. Now let’s right click into the chart. Select Template, Save template and save it as tester TPL because that is what you are going to see in the next strategy test.


Now please click on View, Strategy Tester or Press Control and R. Please pick the new file simple RSI Sell Stopp Exfour. Mark the option for the visible mode here and start your test. Here we are. The RSI is above 70, so here is our sell trade.


Now let’s wait until it is below 30. That’s when it should close the current position. I will speed that up a little bit. It should be closed in a few seconds. And yes, the 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 position when the RSI value is above 70 and to close it when it is below 30. And you have coded it yourself with a few lines of MQL four code.