DAY2: Preset Comments

Home / Howtos / DAY2: Preset Comments

DAY2: Preset Comments

Introduction

Welcome to Blog No 2 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

DAY2: Objective

Have you had trouble to follow up the results of your different trading strategies!?
Don’t worry, today I will show you how you can add preset comments as well as manual ones to your trades.

DAY2: Functions covered

MT4GUI

  • MT4 menu functions (Adding Options menu with submenu for enable/disable preset comment listbox and textfield on chart)
  • MT4GUI listbox (6 preset list items + a manual one)
  • MT4GUI textfield (to be able to manually type a trade comment)
  • MT4GUI button (Execute trade button on MT4 chart)
  • Look and feel (Size, color, positioning etc of GUI objects and menu)
  • MT4 (MQL4)

  • EA property inputs (set X/Y position of GUI and preset comments)
  • Alert (pop-up box confirming user actions)
  • CODEBASE:

    // mt4gui-PresetComments.mq4
    // Created by Thommy Tikka (thommy_tikka@hotmail.com)
    // Version 0.1, 20130402
    
    // Include library file with common functions for mt4gui
    #include
    
    // Declare global variables
    int hwnd = 0;
    int executeTradeBtn;
    int menuOptions,menuSubComments;
    int presetCommentList, presetCommentTextfield;
    
    //Default position of buttons (adjustable in the experts properties/inputs)
    extern int    GUIXPosition   = 10,
                  GUIYPosition   = 60;
    
    // Default 6 x PresetComments (adjustable in the experts properties/inputs)
    extern string PresetComment1 = "ManualStrategy1",
                  PresetComment2 = "ManualStrategy2",
                  PresetComment3 = "ManualStrategy3",
                  PresetComment4 = "AutotradeStrategy1",
                  PresetComment5 = "AutotradeStrategy2",
                  PresetComment6 = "AutotradeStrategy3",
                  ManualComment  = "";
    
    // MT4 function called during the module initialization
    //+-------------------------------------------------------------------------------------------+
    int init()
      {
    	hwnd = WindowHandle(Symbol(),Period());
    	guiRemoveAll(hwnd);
    
    	// Add execute trade button to chart function call
       CreateExecuteTradeBtn();
    
       // Add options menu to MT4 function call
       CreateOptionsMenu();
    
       // Add interface with listbox and text field 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();
    
       //Check if something new has been typed into the manual comment text field and replace the list of comments if it is
       if (guiGetText(hwnd,presetCommentTextfield)!=ManualComment)
       {
       ManualComment = guiGetText(hwnd,presetCommentTextfield);
    
       //Removes the list to be able to rebuilt it with new manual comment (function BuildInterface())
       guiRemoveListAll(hwnd,presetCommentList);
       BuildInterface();
       }
      }
    
    // MT4GUI functions to create Menu
    //+-------------------------------------------------------------------------------------------+
    void CreateOptionsMenu()
    {
       // Add "Options" menu to MT4 menu
    	menuOptions = guiAddMenu(hwnd,"Options",0,0);
    
    	// Add sub-menu PresetComments with enable/disable
    	menuSubComments = guiAddMenu(hwnd,"PresetComments",menuOptions,1);
    
    	// Set Options menu background color to Blue
    	guiSetMenuBgColor(hwnd,menuOptions,Blue);
    
       // Set sub-menu Comments background color to Aqua and text to Black
    	guiSetMenuBgColor(hwnd,menuSubComments,Aqua);
    	guiSetMenuTextColor(hwnd,menuSubComments,Black);
    
    	// Set Comment menu item to enable
    	guiCheckMenu(hwnd,menuSubComments,true);
    }
    
    // MT4GUI functions to capture Menu & Button Events
    //+-------------------------------------------------------------------------------------------+
    void ManageEvents()
    {
    	// If executeTradeBtn is clicked execute function ExecuteTrade()
    	if (guiIsClicked(hwnd,executeTradeBtn)) ExecuteTrade();
    
       // If menuitem SubComments is clicked check status, remove/create Screenshot button
       if (guiIsMenuClicked(hwnd,menuSubComments))
       {
          if (!guiIsMenuChecked(hwnd,menuSubComments))
          {
          guiRemove(hwnd,presetCommentList);
          guiRemove(hwnd,presetCommentTextfield);
          }
          else
          {
          presetCommentList=0;
          presetCommentTextfield=0;
          BuildInterface();
          }
       }
    }
    
    // MT4GUI functions to capture build Interface with textfield and list box
    //+-------------------------------------------------------------------------------------------+
    void BuildInterface()
    {
    	// Textfield Object
       if (presetCommentTextfield==0)
       {
       //Add a comment text field for manual input using user adjustable X/Y-position (size fixed to 150x20)
    	presetCommentTextfield = guiAdd(hwnd,"text",GUIXPosition,GUIYPosition+30,150,20,ManualComment);
    
    	//Set background and text color for comment text field
    	guiSetBgColor(hwnd,presetCommentTextfield,Yellow);guiSetTextColor(hwnd,presetCommentTextfield,Black);
    	}
    
    	// List box	with all the PresetComments listed. Position based on user adjustable X/Y-position, size fixed.
    	if (presetCommentList==0) presetCommentList = guiAdd(hwnd,"list",GUIXPosition,GUIYPosition,150,300,"PresetComments");
        guiAddListItem(hwnd,presetCommentList,PresetComment1);
    	guiAddListItem(hwnd,presetCommentList,PresetComment2);
    	guiAddListItem(hwnd,presetCommentList,PresetComment3);
    	guiAddListItem(hwnd,presetCommentList,PresetComment4);
    	guiAddListItem(hwnd,presetCommentList,PresetComment5);
    	guiAddListItem(hwnd,presetCommentList,PresetComment6);
    	guiAddListItem(hwnd,presetCommentList,ManualComment);
    
    	//Sets the first (0) list item to default
    	guiSetListSel(hwnd,presetCommentList,0);
    }
    
    // MT4GUI functions to create Button
    //+-------------------------------------------------------------------------------------------+
    void CreateExecuteTradeBtn()
    {
    	// Position and size of the button ,x ,y ,a ,b (x, y = position from top left corner. a, b = size of button)
    	executeTradeBtn = guiAdd(hwnd,"button",GUIXPosition+160,GUIYPosition,70,50,"");
    
    	// Execute trade button Border color
    	guiSetBorderColor(hwnd,executeTradeBtn,White);
    
    	// Execute trade button Background color
    	guiSetBgColor(hwnd,executeTradeBtn, Blue);
    
    	// Execute trade button Text color
    	guiSetTextColor(hwnd,executeTradeBtn,White);
    
    	// Add text to execute trade button
    	guiSetText(hwnd,executeTradeBtn,"Exec",25,"Arial Bold");
    }
    
    // MT4 function to execute a trade (only simulation, replace with your own execution code)
    //+-------------------------------------------------------------------------------------------+
    void ExecuteTrade()
    {
       Alert ("This could have executed a trade with comment: ", IndexToComment());
    }
    
    // Function to convert presetCommentList index number to corresponding Comment
    //+-------------------------------------------------------------------------------------------+
    string IndexToComment()
    {
       //Gets the selected list item index (0-6) and converts it to corresponding preset comment
       switch (guiGetListSel (hwnd, presetCommentList))
       {
          case 0:
                return(PresetComment1);
                break;
          case 1:
                return(PresetComment2);
                break;
          case 2:
                return(PresetComment3);
                break;
          case 3:
                return(PresetComment4);
                break;
          case 4:
                return(PresetComment5);
                break;
          case 5:
                return(PresetComment6);
                break;
          case 6:
                return(ManualComment);
                break;
       }
    }
    

    VIDEOCLIP:

    Thats all folks!

    Tomorrow I will show you how you can add “login functionality (user/pass)” with MT4GUI.

    2 Comments Responses to DAY2: Preset Comments
    Atomic.Kidd2013-04-03 22:50:11Reply
    This is truly amazing - I'm exploring many things in MQ4 and unfortunately missing items for scripting in Selecting and working with a custom Profile and diff profile names - Missing code - able to select custom templates "Missing" Able to Select charts by what is configured and simply minimized on Chart bar - Access to this "Missing" very barbaric code to implement - ability to perform multi-trade multi-modify within 1 script execution - missing ( so far I've been unable to get 2 or more entries with TP and SL modify by one script execution. ) I can so far I am able to create 2 or more entries with various currency names but only 1 will get modified. I have work around drag to chart modify scripts - (thanks to the wonderful code sharing on MQL4-Document Site ) some very kind coders there. Looking to create some neat stuff and this MT4-Gui stuff will be the cat's meow ! cheers. Keep up the great works - God bless you all ! the original " Atomic.Kidd " ..
    Thommy Tikka2013-04-12 09:56:46Reply
    Hi, I am sure you will find MT4GUI to be a great addition to your developers tools-kit. Good luck in your future coding. Thommy
    Leave a comment