DAY4: Terminal & Charting Functions

Home / Howtos / DAY4: Terminal & Charting Functions

DAY4: Terminal & Charting functions

Introduction

Welcome to Blog No 4 in this miniseries of 10 daily blogs in which I will exploit MT4GUIs functionalities.

I hope you find it both interesting as well as valuable!

Sincerely

Thommy Tikka

DAY4: Objective

Ever needed a quick way to switch between pairs or periods in your MT4 chart!?
Or adjust terminal size, close terminal or chart quickly from a button!?
Check this out, you are a couple of code blocks from doing it!

DAY4: Functions covered

MT4GUI

  • MT4GUI labels (build a control panel with labels)
  • MT4GUI radiobutton (to quickly switch between alternatives)
  • MT4GUI button (Terminal & Chart buttons “control panel” on MT4 chart)
  • MT4GUI terminal function (get chart width for default GUI positioning)
  • Look and feel (Size, color, positioning etc of GUI objects)
  • MT4 (MQL4)

  • EA property inputs (set default pairs, periods to switch between & GUI colors)
  • Convert numeric Period to String equivalent
  • CODEBASE:

    // mt4gui-TerminalChartingFunctions.mq4
    // Created by Thommy Tikka (thommy_tikka@hotmail.com) 
    // Version 0.1, 20130404
    
    // Include library file with common functions for mt4gui
    #include <mt4gui.mqh>
    
    
    // Declare global variables
    int hwnd = 0;
    int menuTerminalBtn, menuChartBtn;
    int controlPanelHeader, controlPanelMenuTerminal, controlPanelMenuChart;
    bool chartMenuOpen=false, terminalMenuOpen=false;
    int RChart1,RChart2,RChart3,RChart4,RChart5,RChart6,RChart7,RChart8;
    int RTerm1, RTerm2, RTerm3, RTerm4;
    int gUIXPosition, gUIYPosition;
    
    // Declare variables that are adjustable in the EA properties inputs
    extern string MENUPAIRS         = "--- Choose menu pairs ---",
                  MenuPair1         = "EURUSD",
                  MenuPair2         = "USDCHF",
                  MenuPair3         = "GBPUSD",
                  MenuPair4         = "USDJPY";
    extern string MENUPERIODS       = "--- Choose menu periods ---";
    extern int    MenuPeriod1       = 1,     //1Min
                  MenuPeriod2       = 15,    //15Min
                  MenuPeriod3       = 60,    //1Hour
                  MenuPeriod4       = 1440;  //1Day                    
    extern string GUICOLORS         = "--- Color settings ---";
    extern color  ChartBtnColor     = Red,
                  ChartMenuColor    = OrangeRed,
                  ChartMenuColor2   = Orange,
                  TerminalBtnColor  = Blue,
                  TerminalMenuColor = RoyalBlue;
    extern string GUIOFFSET         = "--- Offset from borders ---";
    extern int    GUIXOffset        = -180,
                  GUIYOffset        = 30;
    
    
    // MT4 function called during the module initialization
    //+-------------------------------------------------------------------------------------------+
    int init()
      {
    	hwnd = WindowHandle(Symbol(),Period());	
    	guiRemoveAll(hwnd);
    	
       // Measures chart width and height and sets default GUI X/Y-positions to top right of chart (with an offset)
       AutoPositionControlPanel();
    
    
       // Add interface with panel and buttons to chart function call
       BuildInterface();
    
       return(0);
      }
    
    
    // MT4 function called during deinitialization of the module
    //+-------------------------------------------------------------------------------------------+
    int deinit()
      {
       // Very important to cleanup and remove all gui items from chart
       if (hwnd>0) guiRemoveAll(hwnd);   	
       guiCleanup(hwnd);
       
       return(0);
      }
    
    
    // MT4 function called on every MT4 tick
    //+-------------------------------------------------------------------------------------------+
    int start()
      {   
       // Call function ManageEvents on every MT4 tick
       ManageEvents();
      }
    
    
    // MT4GUI functions to capture Menu & Button Events
    //+-------------------------------------------------------------------------------------------+
    void ManageEvents()
    {		
    	// If menuTerminalBtn is clicked execute function OpenTerminalMenu()
    	if (guiIsClicked(hwnd,menuTerminalBtn)) TerminalMenu();
    
    	// If chartBtn is clicked execute function OpenTerminalMenu()
    	if (guiIsClicked(hwnd,menuChartBtn)) ChartMenu();
    	
    	//If Radio chart buttons is checked change chart
    	CheckRadioButton();
    }
    
    
    // MT4GUI functions to build Interface with labels, buttons & textfields
    //+-------------------------------------------------------------------------------------------+
    void BuildInterface()
    {
       // Build control panel (labels) and set look and feel for it
     	controlPanelHeader  = guiAdd(hwnd,"label",gUIXPosition,gUIYPosition,120,20,"    ControlPanel");				
                                         
       // Create control panel menu buttons, Terminal & Chart and set look and feel for them
    	menuTerminalBtn = guiAdd(hwnd,"button",gUIXPosition,gUIYPosition+20,60,20,""); 
    	guiSetBorderColor(hwnd,menuTerminalBtn,TerminalMenuColor);
    	guiSetBgColor(hwnd,menuTerminalBtn, TerminalBtnColor);
    	guiSetTextColor(hwnd,menuTerminalBtn,White);
    	guiSetText(hwnd,menuTerminalBtn,"Terminal",10,"Arial Bold");
    	
       menuChartBtn = guiAdd(hwnd,"button",gUIXPosition+60,gUIYPosition+20,60,20,""); 
    	guiSetBorderColor(hwnd,menuChartBtn,ChartMenuColor);
    	guiSetBgColor(hwnd,menuChartBtn,ChartBtnColor);
    	guiSetTextColor(hwnd,menuChartBtn,Black);
    	guiSetText(hwnd,menuChartBtn,"Chart",10,"Arial Bold");
    }
    
    
    // MT4GUI functions to change chart settings within GUI
    //+-------------------------------------------------------------------------------------------+
    void ChartMenu()
    {
       if (chartMenuOpen)
       {
       RemoveCPMC();
       }
       else
       {
          RemoveCPMT();
          controlPanelMenuChart  = guiAdd(hwnd,"label",gUIXPosition,gUIYPosition+40,120,80,"");
          guiSetBgColor(hwnd,controlPanelMenuChart,ChartMenuColor);
          chartMenuOpen=true;
          
    	   // Radio Buttons for chart functions
    	   RChart1    = guiAdd(hwnd,"radio",gUIXPosition,gUIYPosition+40,120,20,MenuPair1); 
    	   RChart2    = guiAdd(hwnd,"radio",gUIXPosition,gUIYPosition+60,120,20,MenuPair2); 
    	   RChart3    = guiAdd(hwnd,"radio",gUIXPosition,gUIYPosition+80,120,20,MenuPair3);
    	   RChart4    = guiAdd(hwnd,"radio",gUIXPosition,gUIYPosition+100,120,20,MenuPair4);
    
    	   RChart5    = guiAdd(hwnd,"radio",gUIXPosition,gUIYPosition+120,120,20,PeriodToText(MenuPeriod1)); 
    	   RChart6    = guiAdd(hwnd,"radio",gUIXPosition,gUIYPosition+140,120,20,PeriodToText(MenuPeriod2)); 
    	   RChart7    = guiAdd(hwnd,"radio",gUIXPosition,gUIYPosition+160,120,20,PeriodToText(MenuPeriod3));
    	   RChart8    = guiAdd(hwnd,"radio",gUIXPosition,gUIYPosition+180,120,20,PeriodToText(MenuPeriod4));	   
    	   
    	   // Look and feel for chart radio buttons
          guiSetBgColor(hwnd,RChart1,ChartMenuColor);guiSetBgColor(hwnd,RChart2,ChartMenuColor);guiSetBgColor(hwnd,RChart3,ChartMenuColor);guiSetBgColor(hwnd,RChart4,ChartMenuColor);
          guiSetBgColor(hwnd,RChart5,ChartMenuColor2);guiSetBgColor(hwnd,RChart6,ChartMenuColor2);guiSetBgColor(hwnd,RChart7,ChartMenuColor2);guiSetBgColor(hwnd,RChart8,ChartMenuColor2);
       }
    }
    
    
    // MT4GUI functions to change terminal actions within GUI
    //+-------------------------------------------------------------------------------------------+
    void TerminalMenu()
    {
       if (terminalMenuOpen)
       {
          RemoveCPMT();
       }
       else
       {
          RemoveCPMC();
          controlPanelMenuTerminal  = guiAdd(hwnd,"label",gUIXPosition,gUIYPosition+40,120,80,"");
          guiSetBgColor(hwnd,controlPanelMenuTerminal,TerminalMenuColor);
          terminalMenuOpen=true;
          
          // Radio Buttons for terminal functions
          RTerm1    = guiAdd(hwnd,"radio",gUIXPosition,gUIYPosition+40,120,20,"Min terminal"); 
    	   RTerm2    = guiAdd(hwnd,"radio",gUIXPosition,gUIYPosition+60,120,20,"Max terminal"); 
    	   RTerm3    = guiAdd(hwnd,"radio",gUIXPosition,gUIYPosition+80,120,20,"Close terminal");
    	   RTerm4    = guiAdd(hwnd,"radio",gUIXPosition,gUIYPosition+100,120,20,"Close chart");
          // Look and feel for terminal radio buttons
          guiSetBgColor(hwnd,RTerm1,TerminalMenuColor);guiSetBgColor(hwnd,RTerm2,TerminalMenuColor);guiSetBgColor(hwnd,RTerm3,TerminalMenuColor);guiSetBgColor(hwnd,RTerm4,TerminalMenuColor);}
    }
    
    
    // MT4GUI functions to remove controlPanelMenuTerminal
    //+-------------------------------------------------------------------------------------------+
    void RemoveCPMC()
    {
       guiRemove(hwnd,controlPanelMenuChart);
       guiRemove(hwnd,RChart1);guiRemove(hwnd,RChart2);guiRemove(hwnd,RChart3);guiRemove(hwnd,RChart4);
       guiRemove(hwnd,RChart5);guiRemove(hwnd,RChart6);guiRemove(hwnd,RChart7);guiRemove(hwnd,RChart8);
       chartMenuOpen=false;  
    }
    
    
    // MT4GUI functions to remove controlPanelMenuChart
    //+-------------------------------------------------------------------------------------------+
    void RemoveCPMT()
    {
       guiRemove(hwnd,controlPanelMenuTerminal);
       guiRemove(hwnd,RTerm1);guiRemove(hwnd,RTerm2);guiRemove(hwnd,RTerm3);guiRemove(hwnd,RTerm4);
       terminalMenuOpen=false; 
    }
    
    
    // MT4GUI functions check radio buttons and change chart pair/period or execute terminal function
    //+-------------------------------------------------------------------------------------------+
    void CheckRadioButton()
    {
       string tempdel = CharToStr(8)+CharToStr(8)+CharToStr(8)+CharToStr(8)+CharToStr(8)+CharToStr(8)+CharToStr(8)+CharToStr(8)+CharToStr(8)+CharToStr(8)+CharToStr(8)+CharToStr(8);
    	if (guiIsChecked(hwnd, RChart1) && Symbol()!=MenuPair1) {guiChangeSymbol(hwnd, tempdel+MenuPair1); RemoveCPMC();}
    	if (guiIsChecked(hwnd, RChart2) && Symbol()!=MenuPair2) {guiChangeSymbol(hwnd, tempdel+MenuPair2); RemoveCPMC();}
    	if (guiIsChecked(hwnd, RChart3) && Symbol()!=MenuPair3) {guiChangeSymbol(hwnd, tempdel+MenuPair3); RemoveCPMC();}
    	if (guiIsChecked(hwnd, RChart4) && Symbol()!=MenuPair4) {guiChangeSymbol(hwnd, tempdel+MenuPair4); RemoveCPMC();}
    	if (guiIsChecked(hwnd, RChart5) && Period()!=MenuPeriod1) {guiChangeSymbol(hwnd, tempdel+Symbol()+","+MenuPeriod1); RemoveCPMC();}
    	if (guiIsChecked(hwnd, RChart6) && Period()!=MenuPeriod2) {guiChangeSymbol(hwnd, tempdel+Symbol()+","+MenuPeriod2); RemoveCPMC();}
    	if (guiIsChecked(hwnd, RChart7) && Period()!=MenuPeriod3) {guiChangeSymbol(hwnd, tempdel+Symbol()+","+MenuPeriod3); RemoveCPMC();}
    	if (guiIsChecked(hwnd, RChart8) && Period()!=MenuPeriod4) {guiChangeSymbol(hwnd, tempdel+Symbol()+","+MenuPeriod4); RemoveCPMC();}	
    	
    	
       //If Radio terminal buttons is checked call terminal function
    	if (guiIsChecked(hwnd, RTerm1)) {guiMinimizeTerminal(); RemoveCPMT();}  //Minimize terminal
    	if (guiIsChecked(hwnd, RTerm2)) {guiMaximizeTerminal(); RemoveCPMT();}  //Maximize terminal
    	if (guiIsChecked(hwnd, RTerm3)) {guiCloseTerminal(); RemoveCPMT();}  //Close terminal
    	if (guiIsChecked(hwnd, RTerm4)) {guiCloseChart(hwnd); RemoveCPMT();}  //Close chart	
    }
    
    
    // MT4GUI Function for getting Chart Width and postition X/Y for ControlPanel (GUI)
    //+-------------------------------------------------------------------------------------------+
    void AutoPositionControlPanel()
    {
       gUIXPosition = guiGetChartWidth(hwnd)+GUIXOffset;
       gUIYPosition = GUIYOffset;
    }
    
    
    // MT4/MQL4 Function to convert period number to text string
    //+-------------------------------------------------------------------------------------------+
    string PeriodToText(int period)
    {
       switch (period)
       {
          case 1:
                return("M1");
                break;
          case 5:
                return("M5");
                break;
          case 15:
                return("M15");
                break;
          case 30:
                return("M30");
                break;
          case 60:
                return("H1");
                break;
          case 240:
                return("H4");
                break;
          case 1440:
                return("D1");
                break;
          case 10080:
                return("W1");
                break;
          case 43200:
                return("MN1");
                break;
       }
    }
    

    VIDEOCLIP:

    Thats all folks!

    Tomorrow I will show you several ways to use links in MT4, with MT4GUI.

    Leave a comment