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
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.
// 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;
}
}
Thats all folks!
Tomorrow I will show you how you can add “login functionality (user/pass)” with MT4GUI.
You must be logged in to post a comment.