MQL5 TUTORIAL - Color Changer explained (in 4 min)

Опубликовано: 15 Октябрь 2024
на канале: MQL5 Tutorial
151
5

https://mql5tutoriial.com/?s=chart

In this video, we are going to create an Expert Advisor that changes the appearance of the chart based on the movement of the last two candles. Let's see how we can do that.

We start by opening Metaeditor, which can be done by clicking the little icon or pressing F4. The code begins with including the Trade library using the #include Trade\Trade.mqh directive, which provides trading functions. We then create an instance of the CTrade class, named trade, which allows us to execute trading operations, although it is not used in this specific code.

Inside the OnTick function, we create an array named PriceInformation to store price data. The ArraySetAsSeries function is used to sort this array from the current candle downwards. We then copy the price data for the last three candles into the array using the CopyRates function. The parameters for CopyRates are the symbol name, the period, the starting position (shift value), the number of candles to copy, and the array to store the data.

Next, we retrieve the close prices for the last two candles. Close1 is the close price for candle 1, and Close2 is the close price for candle 2.

We then check the direction of the price movement. If the price has increased, meaning Close2 is less than Close1, we change the chart's appearance to indicate an upward movement. The ChartSetInteger function is used to set the body and outer colors of both bull and bear candles to green, the candle type to bars, the grid to visible, the foreground color to yellow, and the background color to black.

If the price has decreased, meaning Close2 is greater than Close1, we change the chart's appearance to indicate a downward movement. The ChartSetInteger function is used to set the body and outer colors of both bull and bear candles to red, the candle type to candles, the grid to hidden, the foreground color to magenta, and the background color to white.

We also retrieve the maximum and minimum prices on the chart using the ChartGetDouble function with the CHART_PRICE_MAX and CHART_PRICE_MIN parameters.

Finally, we output the close prices of the last two candles, as well as the maximum and minimum prices on the chart, using the Comment function.

Once the code is complete, we press F7 to compile it. If this was too fast for you or if you don't understand what all the code is doing, you might want to check out the Premium course on our website or watch one of the basic videos first.

If everything 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 then 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, feel free to send us an email.

In this video, we have learned how to create an Expert Advisor that dynamically changes the chart's appearance based on the movement of the last two candles using a few lines of MQL code. Thanks for watching, and I will see you in the next video.


Смотрите видео MQL5 TUTORIAL - Color Changer explained (in 4 min) онлайн без регистрации, длительностью часов минут секунд в хорошем качестве. Это видео добавил пользователь MQL5 Tutorial 15 Октябрь 2024, не забудьте поделиться им ссылкой с друзьями и знакомыми, на нашем сайте его посмотрели 151 раз и оно понравилось 5 людям.