https://mql5tutorial.com/?s=spread
In this video, we are going to create an Expert Advisor that trades based on the spread and the price movement of the last few candles. Let’s see how we can do that. First, we start Metaeditor by clicking the little icon or pressing F4. We include the Trade.mqh library, which provides trading functions. We create an instance of the CTrade class called trade. We also define a user input parameter MaxSpreadAllowed, which sets the maximum spread value allowed for trading. In the OnTick function, we begin by calculating the Ask price using the SymbolInfoDouble function and normalizing it to the number of digits for the current symbol. Similarly, we calculate the Bid price. We then create a string variable named signal to store our trading signal. Next, we create an array of MqlRates structures called PriceArray to store price data. We set the array to be sorted from the current candle downwards using ArraySetAsSeries. We fill the array with price data for the last three candles using the CopyRates function. We calculate the current spread using the SymbolInfoInteger function and store it in the variable CurrentSpread.
We create an empty string called SpreadFilter to store the spread filter status. We then check if the current spread is above or below the allowed spread. If the current spread is greater than or equal to MaxSpreadAllowed, we set SpreadFilter to indicate that trading is not allowed. If the current spread is less than or equal to MaxSpreadAllowed, we set SpreadFilter to indicate that trading is allowed. We analyze the price movement by comparing the closing prices of the last two candles. If the closing price of the previous candle is higher than the one before it, we set the signal to “buy”. If the closing price of the previous candle is lower, we set the signal to “sell”. If trading is allowed based on the spread filter, we proceed with the trading logic. If the signal is “sell” and there are no open positions, we execute a sell order for 0.10 lots (10 micro lots) at the Bid price with a stop loss set 150 points below the Bid price.
If the signal is “buy” and there are no open positions, we execute a buy order for 0.10 lots (10 micro lots) at the Ask price with a stop loss set 150 points above the Ask price. Finally, we create a chart output using the Comment function to display the current spread, the allowed spread, and the trading status based on the spread filter. We press F7 to compile the code. If this was too fast for you or if you don’t understand what all the code is doing, you may want to check out the Premium course on our website, or watch one of the basic videos first. If the compilation works, we go back to MetaTrader by pressing F4 or clicking on the icon. Back in MetaTrader, we press Control and R to start the strategy tester, pick the Expert Advisor that we have just created, enable the visual mode, and start a strategy test. We should see the Expert Advisor on the chart. If you are already a Premium course member and have an idea for a video like this one, you can send us an email. In this video, we have learned how to create an Expert Advisor that trades based on the spread and the price movement of the last few candles with just a few lines of MQL code. Thanks for watching, and I will see you in the next video.