preview
How to deal with lines using MQL5

How to deal with lines using MQL5

MetaTrader 5Trading | 8 November 2022, 16:42
3 242 0
Mohamed Abdelmaaboud
Mohamed Abdelmaaboud

Introduction

We all as traders draw many lines while trading to help us to observe some important levels to take a suitable trading decision based on them. So, these lines are very important for us as traders and we may wonder if we have a method that can be used to draw these lines or take a suitable decision based on them automatically because I think that it will help us a lot. The answer is yes, we have a method to do that by MQL5 (MetaQuotes Language 5).

This article is an example to learn how we can deal with these lines by the MQL5 because there are many methods to do something like this and these methods depend on what you need to do. There are many types of lines but we will mention in this article three of them only because of their popularity and they are trend lines, support, and resistance lines.

We will cover them through the following topics:

  1. Trend lines and MQL5
  2. Support and MQL5
  3. Resistance and MQL5
  4. Conclusion

Generally speaking, there are many methods that can be used to deal with lines by MQL5 we will share here one simple method and you may find that you need to develop it to match your strategy we will share here a method to detect price levels then drawing an updated type of line.

We will learn what are lines in detail by identifying their types, how we can draw them manually, and how we can use them in trading. We learn how we can use MQL5 to deal with trend lines, support, and resistance levels to help us to ease our trading process and enhance our trading results by automation things. We will use MQL5 (MetaQuotes Language 5) to code our programs which is built-in with the MetaTrader 5 trading terminal and if you do not know how to use them, you can read the topic of Writing MQL5 code in MetaEditor from a previous article to learn more about that.

My advice here is to try to write and apply what you read by yourself as this step is very important to improve your coding skills and it will deepen your understanding in addition to getting more insights about the topic of this article and/or any related topic which can be useful to develop and enhance your trading results.

Disclaimer: All information provided 'as is' only for educational purposes and is not prepared for trading purposes or advice. The information does not guarantee any kind of result. If you choose to use these materials on any of your trading accounts, you will do that at your own risk and you will be the only responsible.


Trendlines and MQL5

Lines are technical tools that can be used for trading to get more effective results. These lines can be drawn around important prices or levels that can be used as triggers to take effective decisions and there are many types of these lines. We will mention three of these important lines in this article and they are trend lines, support, and resistance levels.

Now, we will start identifying and dealing with Trend lines. Trend lines are lines that can be used to visualize price levels that can be used as rejection levels to up or down. It can be drawn above or below prices based on the direction of the market. So, we can say that the Trend line may help us to detect the direction of the market trend. If there is a price bounce to the upside from three points as a minimum on the same levels of the drawn line, it will be an upward trend line. Vice versa, if there is a bounce to the downside from three points as a minimum on the same levels of the drawn line, it will be a downtrend line. It is important to mention here that a trend line is just a tool and you can recognize and draw it correctly or not.

We can use the Trend line in trading by placing our orders based on the trend line type. If there is an upward trend line, we may expect the price moves down to test this trend line from above then rebounding to up and then we can place our buying order around this trend line. Vice versa, if there is a downward trend line, we may that the price moves up to test this trend line from below then rebounding to down and then we can place our shorting or selling order around this downward trend line.

The following is for the upward trend line:

 Up trendline

We can see that it is clear in the previous figure that we have an up movement and if we try to connect between the last three lows we can find that they are on the same line heading up.

The following is an example of the upward trend line from the market:

Up trendline

As we can see in the previous chart we have an upward trend line that touched many times and rebounded after finding the buying power to head up which confirms the existence of an uptrend.

The following is for the downtrend line:

Down trendline

We can see that it is clear in the previous figure that we have an up movement and if we try to connect between the last three highs we can find that they are on the same line move to down.

The following is an example of a downtrend line from the market:

Down trendline

As we can see in the previous chart we have a downward trend line that touched many times and rebounded after finding the selling power to move down which confirms the existence of a downtrend.


    1. Upward Trendline System

      We need to create a program that can be used to create an upward trend line by the MetaTrader 5 automatically that can be seen below prices for potential up movement. We need the program to check price lows and if there is any upward trend line every tick. Then deleting the previous upward trend line, creating an updated blue one below the lows of the price. 

      To do that, we can follow the below steps as one of the methods that can be used.

      Creating first candle detection by creating an integer variable to be equal to the returned long type of value of the corresponding property of the current chart by using the "ChartGetInteger" function and its parameters are:

      • chart_id: to determine the chart and we will use (0) to represent the current chart.
      • prop_id: to determine the chart property ID and we can use here one of the (ENUM_CHART_PROPERTY_INTEGER) values. We will use (CHART_FIRST_VISIBLE_BAR).
      • sub_window=0: to determine the number of chart sub-window. We will use (0) to represent the main chart window.
      int candles=ChartGetInteger(0,CHART_FIRST_VISIBLE_BAR,0);

      Creating an array of candles of low prices by creating a double variable type for pLow.

      double pLow[];

      Sorting the created array from the current data by using the "ArraySetAsSeries" function to return a boolean value which will be true on success or false in the other case.

      ArraySetAsSeries(pLow,true);

      Filling the created pLow array with data by using the " CopyLow" function to get into the lowest price of the current symbol of the current time. Its parameters are:

      • symbol_name: to determine the symbol, we will use (_Symbol) to be applied for the current symbol.
      • timeframe: to determine the timeframe, we will use (_Period) to be applied for the current time frame.
      • start_pos: to determine the start position of element to copy, we will use (0).
      • count: to determine the data count to copy, we will use the pre created variable ( candles).
      • low_array[]: to determine the target array to copy, we will use ( pLow) array.
      CopyLow(_Symbol,_Period,0,candles,pLow);

      Creating and calculating the high of the candle variable by creating an integer variable of " candleLow" to be equal to the minimum value of the array and we will use the "ArrayMinimum" function to return the lowest element in the array. Its parameters:

      • array[]: to determine the array, we will use the ( pLow) array.
      • start=0: to determine the index to start checking with, we will use the (0) value.
      • count=WHOLE_ARRAY: to determine the number of checked elements, we will use the (candles).
      int candleLow = ArrayMinimum(pLow,0,candles);

      Creating an array of price "pArray" by using the "MqlRates" function which stores information about prices, volume, and spread.

      MqlRates pArray[];

      Sorting the price array of "pArray" by using the " ArraySetAsSeries" function.

      ArraySetAsSeries(pArray,true);

      Copying price data to the price array after creating an integer variable of "Data" and then using the " CopyRates" to get the historical data of the MqlRates structure. Its parameters are:

      • symbol_name: to determine the symbol, we will use (_Symbol) to be applied to the current chart.
      • timeframe: to determine the time frame, we will use (_Symbol) to be applied to the current time frame.
      • start_pos: to determine the start position, we will use (0) value.
      • count: to determine the data count to copy, we will use the ( candles) created variable.
      • rates_array[]: to determine the target array to copy, we will use the ( pArray).
      int Data = CopyRates(_Symbol,_Period,0,candles,pArray);

      Deleting any previously created trend line to be updated by using the " ObjectDelete" function to remove any specified object from any specified chart. Its parameters are:

      • chart_id: to determine the chart identifier, we will use (_Symbol) to be applied for the current symbol.
      • name: to specify the name of the object that we want to remove, we will specify the ("UpwardTrendline") as a name.
      ObjectDelete(_Symbol,"UpwardTrendline");

      Creating a new trend line by using the " ObjectCreate" to create a trend line object. Its parameters are:

      • chart_id: to determine the symbol, we will use (_Symbol) to be applied for the current symbol.
      • name: to specify the object name, we will specify the ("UpwardTrendline") as a name.
      • type: to determine the object type, we will use the (OBJ_TREND) to create a trend line.
      • nwin: to determine the window index, we will use (0) value to use the main chart window.
      • time1: to determine number of the chart subwindow, we will use the (pArray[candleLow].time).
      • price1: to determine the time of the first anchor, we will use the (pArray[candleLow].low).
      • timeN: to determine the time of the N of anchor point, we will use the (pArray[0].time).
      • priceN: to determine the price of the N of anchor point, we will use the (pArray[0].low).
      ObjectCreate(_Symbol,"UpwardTrendline",OBJ_TREND,0,pArray[candleLow].time,pArray[candleLow].low,pArray[0].time,pArray[0].low);

      Determining trend line color by using the " ObjectSetInteger" function to set the value as a color of the object. Its parameters are:

      • chart_id: the  chart identifier, we will use (0) value which means the current chart.
      • name: the object name, we will use the ("UpwardTrendline") which is the predefined name of the object.
      • prop_id: the ID of the object property, we will use one of the ENUM_OBJECT_PROPERTY_INTEGER which is the (OBJPROP_COLOR).
      • prop_value: the value of the property, we will use the (Blue) as a color of the created trend line.
      ObjectSetInteger(0,"UpwardTrendline",OBJPROP_COLOR,Blue);

      Determining trend line style by using the "ObjectSetInteger" function also but we will use another one of the  NUM_OBJECT_PROPERTY_INTEGER which is the ( OBJPROP_STYLE) and the prop_value will be (STYLE_SOLID) as a style of the created trend line.

      ObjectSetInteger(0,"UpwardTrendline",OBJPROP_STYLE,STYLE_SOLID);

      Determining trend line width by using the "ObjectSetInteger" function also but we will use another one of the NUM_OBJECT_PROPERTY_INTEGER which is the ( OBJPROP_WIDTH) and the prop_value will be (3) as a width of the created trend line.

      ObjectSetInteger(0,"UpwardTrendline",OBJPROP_WIDTH,3);

      Determining trend line ray by using the "ObjectSetInteger" function also but we will use another one of NUM_OBJECT_PROPERTY_INTEGER which is the (OBJPROP_RAY_RIGHT) and the value of the prop will be true.

      ObjectSetInteger(0,"UpwardTrendline",OBJPROP_RAY_RIGHT,true);

      And the following is for the full code to create this system to create an upward trend line automatically:

      //+------------------------------------------------------------------+
      //|                                       UpwardTrendline System.mq5 |
      //+------------------------------------------------------------------+
      void OnTick()
        {
         int candles=ChartGetInteger(0,CHART_FIRST_VISIBLE_BAR,0);
         double pLow[];
         ArraySetAsSeries(pLow,true);
         CopyLow(_Symbol,_Period,0,candles,pLow);
         int candleLow = ArrayMinimum(pLow,0,candles);
         MqlRates pArray[];
         ArraySetAsSeries(pArray,true);
         int Data = CopyRates(_Symbol,_Period,0,candles,pArray);
         ObjectDelete(_Symbol,"UpwardTrendline");
         ObjectCreate(_Symbol,"UpwardTrendline",OBJ_TREND,0,pArray[candleLow].time,pArray[candleLow].low,pArray[0].time,pArray[0].low);
         ObjectSetInteger(0,"UpwardTrendline",OBJPROP_COLOR,Blue);
         ObjectSetInteger(0,"UpwardTrendline",OBJPROP_STYLE,STYLE_SOLID);
         ObjectSetInteger(0,"UpwardTrendline",OBJPROP_WIDTH,3);
         ObjectSetInteger(0,"UpwardTrendline",OBJPROP_RAY_RIGHT,true);
        }
      //+------------------------------------------------------------------+

      After writing this code we will compile it, it must resulting no errors.  After that we will find the Expert Advisor (EA) in the navigator window the same as the following:

       Lines nav

      By dragging and dropping the EA on the chart, we will find the window of the expert the same as the following:

       Lines Win

      After ticking next to the "Allow Algo Trading" and pressing "OK", we will the EA is attached to the chart and we will be ready to see the desired action based on this EA the same as the following example:

       Upward trendline example

      As we can see in the previous chart that we have the EA is attached to the chart the same as it is obvious in the top right corner and we have the blue upward trend line is drawn below prices.


        2. Downward Trendline System

          We need to create a program that can be used to create a downward trend line by the MetaTrader 5 automatically that can be seen above prices for potential down movement. We need the program to check price highs and if there is any downward trend line every tick. Then delete the previous downward trend line and create an updated blue one above the highs of the price. 

          To do that, we can follow the below full code as one of the methods that can be used.

          //+------------------------------------------------------------------+
          //|                                     DownwardTrendline System.mq5 |
          //+------------------------------------------------------------------+
          void OnTick()
            {
             int candles=ChartGetInteger(0,CHART_FIRST_VISIBLE_BAR,0);
             double pHigh[];
             ArraySetAsSeries(pHigh,true);
             CopyHigh(_Symbol,_Period,0,candles,pHigh);
             int candleHigh = ArrayMaximum(pHigh,0,candles);
             MqlRates pArray[];
             ArraySetAsSeries(pArray,true);
             int Data = CopyRates(_Symbol,_Period,0,candles,pArray);
             ObjectDelete(_Symbol,"DnwardTrendline");
             ObjectCreate(_Symbol,"DnwardTrendline",OBJ_TREND,0,pArray[candleHigh].time,pArray[candleHigh].high,pArray[0].time,pArray[0].high);
             ObjectSetInteger(0,"DnwardTrendline",OBJPROP_COLOR,Blue);
             ObjectSetInteger(0,"DnwardTrendline",OBJPROP_STYLE,STYLE_SOLID);
             ObjectSetInteger(0,"DnwardTrendline",OBJPROP_WIDTH,3);
             ObjectSetInteger(0,"DnwardTrendline",OBJPROP_RAY_RIGHT,true);
            }
          //+------------------------------------------------------------------+

          Differences in this code:

          Creating an array of candles of high prices.

          double pHigh[];

          Sorting the created array from current data.

          ArraySetAsSeries(pHigh,true);

          Filling the array with data.

          CopyHigh(_Symbol,_Period,0,candles,pHigh);

          Creating and calculating the high of the (candle) variable

          int candleHigh = ArrayMaximum(pHigh,0,candles);

          Deleting any previous downward trend line

          ObjectDelete(_Symbol,"DnwardTrendline");

          Creating a new downward trend line

          ObjectCreate(_Symbol,"DnwardTrendline",OBJ_TREND,0,pArray[candleHigh].time,pArray[candleHigh].high,pArray[0].time,pArray[0].high);

          Determining the downward trend line color to be blue by using the "OBJPROP_COLOR"

          ObjectSetInteger(0,"DnwardTrendline",OBJPROP_COLOR,Blue);

          Determining the downward trend line style to be solid by using "OBJPROP_STYLE" and STYLE_SOLID

          ObjectSetInteger(0,"DnwardTrendline",OBJPROP_STYLE,STYLE_SOLID);

          Determining the downward trend line width to be 3 value by using the "OBJPROP_WIDTH"

          ObjectSetInteger(0,"DnwardTrendline",OBJPROP_WIDTH,3);

          Determining the downward trend line ray by using the "OBJPROP_RAY_RIGHT"

          ObjectSetInteger(0,"DnwardTrendline",OBJPROP_RAY_RIGHT,true);

          After compiling without any error we will find the expert also in the navigator window the same as we mentioned before. After attaching the EA we will be ready to get the required action based on this EA and we can find the downward trend line is drawn the same as the following:

          Downward trendline example

          As we can see on the chart in the top left corner we have the EA is attached and we have the downtrend line drawn above prices as there is a buying power around this level.


          Support levels and MQL5

          The support level is a price level or zone that can be found below current prices and we can find a rebound to up around it because there is a buying power around these levels. So, they are very important as we can trigger them as buying points. There are many forms of support levels and one of them is the horizontal support level. The following is what this form looks like:

           Support lvl

          The following is an example of the support level from the market:

          Support lvl 1

          As we can see in the previous chart we have a green support line below prices that can be used as a rejection level to the upside.

          We need the program to check price lows and if there is any support line every tick. Then delete the previous support line and create an updated green one below the lows of the price. We can draw and deal with this support line by MQL5 the same as the full below code:

          //+------------------------------------------------------------------+
          //|                                          Support Line System.mq5 |
          //+------------------------------------------------------------------+
          void OnTick()
            {
             int candles=ChartGetInteger(0,CHART_FIRST_VISIBLE_BAR,0);
             double pLow[];
             ArraySetAsSeries(pLow,true);
             CopyLow(_Symbol,_Period,0,candles,pLow);
             int candleLow = ArrayMinimum(pLow,0,candles);
             MqlRates pArray[];
             ArraySetAsSeries(pArray,true);
             int Data = CopyRates(_Symbol,_Period,0,candles,pArray);
             ObjectDelete(_Symbol,"supportLine");
             ObjectCreate(_Symbol,"supportLine",OBJ_HLINE,0,pArray[candleLow].time,pArray[candleLow].low,pArray[0].time,pArray[0].low);
             ObjectSetInteger(0,"supportLine",OBJPROP_COLOR,Green);
             ObjectSetInteger(0,"supportLine",OBJPROP_STYLE,STYLE_SOLID);
             ObjectSetInteger(0,"supportLine",OBJPROP_WIDTH,3);
             ObjectSetInteger(0,"supportLine",OBJPROP_RAY,true);
            }
          //+------------------------------------------------------------------+

          Difference in this code:

          Deleting any previous support lines with the name "supportLine"

          ObjectDelete(_Symbol,"supportLine");

          Creating a new support line by using "OBJ_HLINE" as a type of the required object

          ObjectCreate(_Symbol,"supportLine",OBJ_HLINE,0,pArray[candleLow].time,pArray[candleLow].low, pArray[0].time,pArray[0].low);

          Determining the support line color by using the "OBJPROP_COLOR"

          ObjectSetInteger(0,"supportLine",OBJPROP_COLOR,Green);

          Determining the support line style by using OBJPROP_STYLE and STYLE_SOLID

          ObjectSetInteger(0,"supportLine",OBJPROP_STYLE,STYLE_SOLID);

          Determining the support line width by using the "OBJPROP_WIDTH"

          ObjectSetInteger(0,"supportLine",OBJPROP_WIDTH,3);

          Determining the support line ray by using the "OBJPROP_RAY"

          ObjectSetInteger(0,"supportLine",OBJPROP_RAY,true);

          After compiling this code without any error, we can attach it to the chart the same as we mentioned before. After that, we will find it attached to the chart and we can find its action as we can find the support line is drawn the same as the below example:

          Support line

          As we can see in the previous chart that we have the Support System EA is attached the same as what we find in the top right corner and we have a support line below prices.


          Resistance levels and MQL5

          The resistance level is a price level or zone that can be found above current prices and we can find a rebound to down around it because there is a selling power around these levels. So, they are very important as we can trigger them as selling points. There are many forms of these resistance levels and one of them is the horizontal resistance level the following is what this form looks like:

           Resistance lvl

          The following is an example of the resistance from the market:

           Resistance lvl 1

          As we can see in the previous chart we have a red resistance line above prices that can be used as a rejection level to the downside as there is a buying power around this level

          We need the program to check price highs and if there is any resistance line every tick. Then delete the previous resistance line and create an updated red one above the highs of the price. We can draw and deal with this resistance line by MQL5 the same as the full below code:

          //+------------------------------------------------------------------+
          //|                                       Resistance Line System.mq5 |
          //+------------------------------------------------------------------+
          void OnTick()
            {
             int candles=ChartGetInteger(0,CHART_FIRST_VISIBLE_BAR,0);
             double pHigh[];
             ArraySetAsSeries(pHigh,true);
             CopyHigh(_Symbol,_Period,0,candles,pHigh);
             int candleHigh = ArrayMaximum(pHigh,0,candles);
             MqlRates pArray[];
             ArraySetAsSeries(pArray,true);
             int Data = CopyRates(_Symbol,_Period,0,candles,pArray);
             ObjectDelete(_Symbol,"resistanceLine");
             ObjectCreate(_Symbol,"resistanceLine",OBJ_HLINE,0,pArray[candleHigh].time,pArray[candleHigh].high,pArray[0].time,pArray[0].high);
             ObjectSetInteger(0,"resistanceLine",OBJPROP_COLOR,Red);
             ObjectSetInteger(0,"resistanceLine",OBJPROP_STYLE,STYLE_SOLID);
             ObjectSetInteger(0,"resistanceLine",OBJPROP_WIDTH,3);
             ObjectSetInteger(0,"DnwardTrendline",OBJPROP_RAY_RIGHT,true);
            }
          //+------------------------------------------------------------------+

          Difference in this code:

          Deleting any previous resistance line with the name "resistanceLine"

          ObjectDelete(_Symbol,"resistanceLine");

          Creating a new resistance line by using "OBJ_HLINE" as a type of the required object

          ObjectCreate(_Symbol,"resistanceLine",OBJ_HLINE,0,pArray[candleHigh].time,pArray[candleHigh].high,pArray[0].time,pArray[0].high);

          Determining the resistance line color by using OBJPROP_COLOR

          ObjectSetInteger(0,"resistanceLine",OBJPROP_COLOR,Red);

          Determining the resistance line style by using OBJPROP_STYLE and STYLE_SOLID

          ObjectSetInteger(0,"resistanceLine",OBJPROP_STYLE,STYLE_SOLID);

          Determining the resistance line width by using OBJPROP_WIDTH

          ObjectSetInteger(0,"resistanceLine",OBJPROP_WIDTH,3);

          Determining the resistance line ray by using OBJPROP_RAY_RIGHT

          ObjectSetInteger(0,"DnwardTrendline",OBJPROP_RAY_RIGHT,true);

          After compiling this code without any error, we can attach it to the chart the same as we mentioned before. After that, we will find the Resistance System  EA is attached to the chart and we can find its action the same as the following:

          Resistance line

          As we can see in the previous chart that we have the Resistance System EA is attached the same as what we can find in the top right corner and we have a resistance line below the prices.


          Conclusion

          Now, it is supposed that you understand three of the important lines that can be used in our trading in detail. The trend lines (upward, and downward trend lines), Support, and resistance lines as we learned what they are and how we can use them. We learned also how we can deal with them by the MQL5 to draw them automatically by creating our own system for every one of them. You can also develop this code by adding sending orders to execute trades based on them and you must test them before using them on your real account to make sure that they are profitable as the main objective of this article is educational only.

          I hope that you find this article useful for you and you learned how to use these mentioned tools in your favor by using them as a part of another advanced system or individually. So, I hope that you got useful insights into the topic of this article or any related topic to get better trading results and enhance them with useful and profitable tools in addition to hoping that you tried to test and write mentioned codes by yourself to get benefit and improve your coding skills.

          If you find this article useful and you like to read more articles about the MQL5 and how to design a trading system using the most popular technical indicators you can read my previous article to learn more about that as you can find articles like how to design a trading system based on the most popular technical indicators like The Moving Average, RSI, MACD, Stochastic, Parabolic SAR, etc.

          If you want also to learn more also about some of the basics of MQL5 and why and how to design your algorithmic trading system, you can also read my previous article about that.

          Developing a trading Expert Advisor from scratch (Part 29): The talking platform Developing a trading Expert Advisor from scratch (Part 29): The talking platform
          In this article, we will learn how to make the MetaTrader 5 platform talk. What if we make the EA more fun? Financial market trading is often too boring and monotonous, but we can make this job less tiring. Please note that this project can be dangerous for those who experience problems such as addiction. However, in a general case, it just makes things less boring.
          Neural networks made easy (Part 27): Deep Q-Learning (DQN) Neural networks made easy (Part 27): Deep Q-Learning (DQN)
          We continue to study reinforcement learning. In this article, we will get acquainted with the Deep Q-Learning method. The use of this method has enabled the DeepMind team to create a model that can outperform a human when playing Atari computer games. I think it will be useful to evaluate the possibilities of the technology for solving trading problems.
          DoEasy. Controls (Part 20): SplitContainer WinForms object DoEasy. Controls (Part 20): SplitContainer WinForms object
          In the current article, I will start developing the SplitContainer control from the MS Visual Studio toolkit. This control consists of two panels separated by a vertical or horizontal movable separator.
          Neural networks made easy (Part 26): Reinforcement Learning Neural networks made easy (Part 26): Reinforcement Learning
          We continue to study machine learning methods. With this article, we begin another big topic, Reinforcement Learning. This approach allows the models to set up certain strategies for solving the problems. We can expect that this property of reinforcement learning will open up new horizons for building trading strategies.