Graphically display current trends for all timeframes in one simple panel - MetaTrader 4 Script - MT4/MT5 Resource

This is a graphical indicator based on the Slope Directional Trend indicator, which has been released in several other forms.
Several versions of this indicator have been released, but none have the ability to display all timeframes in a single graphical panel on the screen.
You can search https://www.mql5.com/en/search#!keyword=slope%20direction%20line to see multiple results.
I've taken this indicator and made it into a graphical panel showing all timeframes, with some minor modifications to the original code. One of them is that it allows the user to select a second moving average method for smoothing. The original code does a second pass over the numbers using linear weighting, whereas I gave the user the option to choose a smoothing method. When linear weighting is used for both calculations, it makes the indicator overly sensitive and may give false readings on the current trend. If we use simple as the second averaging method, we get a trend that is more indicative of price action.
Another change I made was to be able to get not only the direction of the trend (up, down, or flat), but also its value. I will discuss this in another article that I plan to publish soon, on how to use the dual SlopeDirection indicator and create an Expert Advisor with it.

But for now, let's take a quick look at the code. I first define all the object names I will use. This is a good practice because every time you put an object name between quotes, it allocates space for it in the program. So if they are defined once (by using a define statement), then you only need to use the space once in your code.
// Define trend direction #define uptrend 1 #define downtrend - 1 #define flat trend 0 //define object #define trend panel "trend panel" #define information hotline 1 "Information hotline 1" #define information hotline 2 "Information hotline 2" #Define trend tag "Trend: M1 M5 M15 M30 H1 H4 D1 W1 MN" #define trend up "\233" #define trend down "\234" #define trend flattening "\232" #define trend unknown "\251" #define statistics object error "%s(%d) cannot create "%s". Error = %d"
Next, I define two arrays. The first contains the pixel positions for each of the 9 time frames, the second contains the time frames. These are defined as global variables.
//Define global variables Integer trend position [] = { 44 , 64 , 88 , 114 , 136 , 156 , 174 , 194 , 216 }; integer trend period [] = { PERIOD_M1 , PERIOD_M5 , PERIOD_M15 , PERIOD_M30 , PERIOD_H1 , PERIOD_H4 , PERIOD_D1 , PERIOD_W1 , PERIOD_MN1 };
Our indicator's OnInit() function calls a function to create the panel and objects inside the panel. It also performs a quick "Dumn Dumn" check on the bars used to calculate the trend. Less than 3 is meaningless.
//+------------------------------------------------------------------+ //|Custom indicator initialization function | //+------------------------------------------------------------------+ Integer initialization () { // Create an indicator panel object if (Create_Panel() && BarCount >= 3 ) Return ( initialization successful ); Return otherwise ( initialization failed ); }
I won't go into detail about how the GetTrend() function actually works, there is already good documentation on the site. Let's just look at the graphics DisplayTrend() function.
//+------------------------------------------------------------------+ //|Display the current trend for all time frames //+------------------------------------------------------------------+ Blank Display trend ( blank ) { integer i, cntr, trend, last trend; string str; for (i= 1 ;i< 10 ;i++) { str = "trend" + doubleString (i, 0 ); Trend = ( integer ) GetTrend(TrendPeriods[i- 1 ], BarCount, method); if (trend == flat trend) { // I'm flat, find the last trend direction center = 1 ; Do { Last Trend = ( integer )GetTrend(TrendPeriods[i- 1 ], BarCount, method, error , cntr++); } Although (last trend == trend); Object set text (str, trend plane, 8 , "Wingding" , (LastTrend == UPTREND ? Green: Red)); object set integer ( 0 , , , OBJPROP_Y distance , 6 ); } other { Object set text (str, (trend == uptrend? trend up: trend down), 8 , "Ying Ding" , (trend == uptrend? green: red)); object set integer ( 0 , , , OBJPROP_Y distance , 5 + (trend == uptrend? 1 :- 1 )); } } }
Basically, we just loop through the TrendPeriods[] array (to get all timeframes) and set the trend arrow based on the trend direction for that timeframe. If the trend is flat, we find the first uneven direction to understand where the trend is coming from so we can display the side arrow in the color of that direction.
The Create_Panel() function in OnInit() creates an object anchored to the lower left corner of the screen and uses TrendPosition[] to place the arrow in the correct position.
//+------------------------------------------------------------------+ //|Create a trend panel in the lower left corner of the screen //+------------------------------------------------------------------+ boolean create panel ( blank ) { integer i; string str; //Create trend indicator window if ( object create (trend panel, OBJ_RECTANGLE_LABEL , 0 , 0 , 0 )) { Object Set Integer ( 0 , TrendPanel, OBJPROP_XDISTANCE , 1 ); object set integer ( 0 , trend panel, OBJPROP_Y distance , 29 ); object set integer ( 0 , trend panel, OBJPROP_XSIZE , 240 ); Object Set Integer ( 0 , TrendPanel, OBJPROP_YSIZE , 26 ); object set integer ( 0 , trend panel, OBJPROP_BGCOLOR , white); Object Set Integer ( 0 , TrendPanel, OBJPROP_BORDER_TYPE , 0 ); object set integer ( 0 , trend panel, OBJPROP_corner , corner lower left ); object set integer ( 0 , trend panel, OBJPROP_COLOR , red); object set integer ( 0 , trend panel, OBJPROP_STYLE , style_entity ); Object Set Integer ( 0 , TrendPanel, OBJPROP_WIDTH , 2 ); Object Set Integer ( 0 , TrendPanel, OBJPROP_BACK , False ); Object Set Integer ( 0 , TrendPanel, OBJPROP_SELECTABLE , False ); Object Set Integer ( 0 , TrendPanel, OBJPROP_SELECTED , False ); ObjectSetInteger ( 0 , TrendPanel, OBJPROP_HIDDEN , True ); Object set string ( 0 , trend panel, OBJPROP_tooltip , "\n" ); } else { print format (statistical object error, __function__ , __line__ , trend panel, get last error ()); return ( error ); } if ( objectCreate (infoLine1, OBJ_LABEL , 0 , 0 , 0 )) { ObjectSet (InformationHotline1, OBJPROP_corner , cornerlowerleft ); ObjectSet (InformationHotline1, OBJPROP_XDISTANCE , 6 ); Object Set (Information Hotline 1, OBJPROP_Y distance , 15 ); object set integer ( 0 , infoline1, OBJPROP_SELECTABLE , false ); ObjectSetInteger ( 0 , infoLine1 , OBJPROP_HIDDEN , true ); Object set string ( 0 , information hotline 1, OBJPROP_tooltip , "\n" ); Object set text (InfoLine1, trend label, 8 , "Arial" , black); } Others { beatprintFormat(statisticObjectError, __function__ , __line__ , infoLine1, getLastError ()); return ( Error ); } if ( object create (infoLine2, OBJ_LABEL , 0 , 0 , 0 )) {objectSet(infoLine2, OBJPROP_corner , corner_lowerleft ); objectSet (infoLine2, OBJPROP_XDISTANCE, 6 ); object set (infoLine2, OBJPROP_Y distance , 5 ); object set integer ( 0 , infoLine2, OBJPROP_SELECTABLE , false ); object set integer ( 0 , infoLine2, OBJPROP_HIDDEN , true ); object set string ( 0 , infoLine2, OBJPROP_ToolTip , "\n" ); object set text (information line 2, "" + double-turn string (number, 0) + "/" + double-turn string ( method , 0 ), 8 , "Song Dynasty" , black ); } else { print format (statistical object error, __function__ , __line__ , information hotline 2, get the last error ()); return ( error ); } // Create a trend object and display the current trend order (i = 1 ; i< 10 ; i++) { str = "trend" + double string (i, 0 ); if ( object create (str, OBJ_LABEL , 0 , 0 , 0 )) { object set(str, OBJPROP_corner , corner lower left ); object set (str, OBJPROP_XDISTANCE , trend position [i- 1 ]); object set (str, OBJPROP_Y distance , 5 ); object set integer ( 0 , , , OBJPROP_SELECTABLE , false ); object set integer ( 0 , , , OBJPROP_HIDDEN , true); object set string ( 0 , , , OBJPROP_SELECTABLE , false ); object set string ( 0 , , , OBJPROP_ToolTip , "\n" ); } else { print format (statistics object error, __function__ , __line__ , , , get the last error ()); return ( error ); } } // Go to display the current trend display trend (); // Everything is fine return ( true ); }
This function returns TRUE if all objects are successfully created, or FALSE if an error occurs while creating the objects. The benefit is that this indicator will display the error code in the EXPERT tab of the console along with the line number of the code where the error occurred.
We also need to update the trend indicator for each new quote received. This is done by calling DisplayTrend() in the start() function.
//+------------------------------------------------------------------+ //|Custom indicator iteration function | //+------------------------------------------------------------------+ integer start() { // Display the current trend for all time ranges displayTrend(); return ( 0 ); }
Finally, when we close the indicator, we destroy all objects created by the indicator.
//+------------------------------------------------------------------+ //|Deinitialization function | //+------------------------------------------------------------------+ Blank solution initialization ( constant integer reason) { // Delete indicator object if ( object search (trend panel) >= 0 ) object delete (trend panel); if ( ObjectFind (InformationLine1) >= 0 ) Object Delete (InformationLine1); if ( ObjectFind (InformationLine2) >= 0 ) ObjectDelete (InformationLine2); for ( integer i = 1 ; i < 10 ; i++) if ( object find ( "trend" + doubletString (i, 0 )) >= 0 ) object delete ( "trend" + doubletString (i, 0 )); }
That's basically it. Now you have a nice indicator panel showing the current trend for all timeframes in the lower left corner of the screen.
Attachment download
📎 slopedirection.mq4 (8.61 KB)
Source: MQL5 #15620
💡 Featured Recommendations
✍️ Latest by the author
- •
- •
- •
- •
- •
- •
📌 Popular topics
- •
- •
- •
- •
- •
- •
- •
- •
🔗 You May Be Interested In
- •
- •
- •
- •
- •
- •