DAY6: IN-Trade Management

Home / Howtos / DAY6: IN-Trade Management

DAY6: IN-Trade Management

Introduction

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

DAY6: Objective

Find it hard to use MT4s built in Terminal for your IN-trade management!?
Want to have an easy way to move your Stoploss- & Takeprofit levels?
Lets look how MT4GUI can help you out with quick buttons as well as visual lines!

DAY6: Functions covered

MT4GUI

  • MT4GUI checkbox (enable/disable IN-trade button panel)
  • MT4GUI buttons (enable/disable visual lines, move TPs and SL-breakeven
  • Look and feel (Size, color, positioning etc of GUI objects)
  • MT4 (MQL4)

  • Usage of different colored horizontal lines (movable) for TP/SL
  • EA property inputs (set default TP/SL-levels and GUI panel Y-position)
  • CODEBASE:

    // mt4gui-INTradeManagement.mq4
    // Created by Thommy Tikka (thommy_tikka@hotmail.com) 
    // Version 0.1, 20130409
    
    // Include library file with common functions for mt4gui
    #include <mt4gui.mqh>
    
    // Declare global variables
    int hwnd = 0;
    int enableINTradeBtn, visualLinesBtn, bEBtn;
    int tP1Btn, tP2Btn, tP3Btn;
    int gUIXPos;
    double actualTP, actualSL;
    
    //Set default IN-trade variables, possible to change in the EA Properties
    extern int  DefaultTP = 200, 
                DefaultSL = 100,
                TPLevel1  = 300,
                TPLevel2  = 200,
                TPLevel3  = 100,
                
    // Y-Position the guiButtons
                gUIYPos   = 50;       
    
    
    // MT4 function called during the module initialization
    //+-------------------------------------------------------------------------------------------+
    int init()
      {
    	hwnd = WindowHandle(Symbol(),Period());	
    	guiRemoveAll(hwnd);	
    	
    	// Automatic X-Postion to the right 
       gUIXPos = guiGetChartWidth(hwnd)-180;
    	
    	// Delete visual lines from chart
    	DeleteVisualLines();
    
       // Add screenshot button to chart function call
       CreateButtons();
       
       // Disable all gui Buttons
       ChangeINTradeState();
      
       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);
       
       // Delete visual lines from chart
       DeleteVisualLines();
       
       // Remove eventual Comment
       Comment ("");
       
       return(0);
      }
    
    // MT4 function called on every MT4 tick
    //+-------------------------------------------------------------------------------------------+
    int start()
      {
       // Call function ManageEvents on every MT4 tick
       ManageEvents();
       
       // Call function Move TPSL based on lines
       ChangeTPSL();
       
       // Show actual SL and TP settings if not 0  
       if (actualTP!=0 || actualSL!=0) Comment ("Actual TP: "+ DoubleToStr(actualTP,5)+ "\nActual SL: "+DoubleToStr(actualSL,5));
       else Comment ("");
      }
    
    
    // MT4GUI functions to create Button
    //+-------------------------------------------------------------------------------------------+
    void CreateButtons()
    {
    	// Position and size of the button ,x ,y ,a ,b (x, y = position from top left corner. a, b = size of button)
    	enableINTradeBtn	= guiAdd(hwnd, "checkbox", gUIXPos, gUIYPos, 120, 30, "Enable INTrade"); guiSetBgColor(hwnd, enableINTradeBtn, LightGray);guiSetTextColor(hwnd, enableINTradeBtn, Black);	
    
       // Add visual Lines Btn
    	visualLinesBtn = guiAdd(hwnd, "button", gUIXPos, gUIYPos+30, 120, 30, "TP/SL-lines");
    
       // Add BreakEven button
    	bEBtn = guiAdd(hwnd, "button", gUIXPos, gUIYPos+120, 120, 30, "BreakEven");
    	
    	// Add Radio 1-3 for TP-move
       tP1Btn = guiAdd(hwnd, "button", gUIXPos, gUIYPos+60, 120, 20, "TP1 ("+TPLevel1+")"); 
    	tP2Btn = guiAdd(hwnd, "button", gUIXPos, gUIYPos+80, 120, 20, "TP2 ("+TPLevel2+")"); 
    	tP3Btn = guiAdd(hwnd, "button", gUIXPos, gUIYPos+100, 120, 20, "TP3 ("+TPLevel3+")"); 	
    }
    
    
    // MT4GUI functions to capture Button Events
    //+-------------------------------------------------------------------------------------------+
    void ManageEvents()
    {		
    
    	// If IN-Trade Btn is checked, enable IN-Trade actions
    	if (guiIsChecked(hwnd, enableINTradeBtn)) {guiSetBgColor(hwnd, enableINTradeBtn, Green);}
       else {guiSetBgColor(hwnd, enableINTradeBtn, Aqua);}
       ChangeINTradeState();
    
       // If visualLinesBtn is clicked call function to draw lines
       if (guiIsClicked(hwnd, visualLinesBtn)) 
       {
          PlaySound("click.wav"); 
          if (ObjectFind("INT.TPLine")==-1)
             DrawVisualLines();
          else
             DeleteVisualLines();
       }
    
       // If tPButtons are clicked call function to move TP
       if (guiIsClicked(hwnd, tP1Btn)) {PlaySound("click.wav"); ChangeTP(1);}
       if (guiIsClicked(hwnd, tP2Btn)) {PlaySound("click.wav"); ChangeTP(2);}
       if (guiIsClicked(hwnd, tP3Btn)) {PlaySound("click.wav"); ChangeTP(3);}
    
       // If beBtn is clicked call function to move SL to BreakEven
       if (guiIsClicked(hwnd, bEBtn)) {PlaySound("click.wav"); MoveSLBE();}
    }
    
    
    // MT4 function draw TP/SL visual lines
    //+-------------------------------------------------------------------------------------------+
    void DrawVisualLines()
    {
          if (actualTP==0) ObjectCreate("INT.TPLine", OBJ_HLINE, 0, 0, Bid+DefaultTP*Point);
          else ObjectCreate("INT.TPLine", OBJ_HLINE, 0, 0, actualTP);
          ObjectSet("INT.TPLine", OBJPROP_STYLE, 1);
          ObjectSet("INT.TPLine", OBJPROP_WIDTH, 1);
          ObjectSet("INT.TPLine", OBJPROP_COLOR, Blue);
          ObjectCreate("INT.TPLineLabel", OBJ_TEXT, 0, Time[5], ObjectGet("INT.TPLine", OBJPROP_PRICE1) );
          ObjectSetText("INT.TPLineLabel", "TP", 8, "Arial", Blue);
      
          if (actualSL==0) ObjectCreate("INT.SLLine", OBJ_HLINE, 0, 0, Bid-DefaultSL*Point);
          else ObjectCreate("INT.SLLine", OBJ_HLINE, 0, 0, actualSL);
          ObjectSet("INT.SLLine", OBJPROP_STYLE, 1);
          ObjectSet("INT.SLLine", OBJPROP_WIDTH, 1);
          ObjectSet("INT.SLLine", OBJPROP_COLOR, Red);
          ObjectCreate("INT.SLLineLabel", OBJ_TEXT, 0, Time[5], ObjectGet("INT.SLLine", OBJPROP_PRICE1) );
          ObjectSetText("INT.SLLineLabel", "SL", 8, "Arial", Red);
    }
    
    
    // MT4 function delete TP/SL visual lines
    //+-------------------------------------------------------------------------------------------+
    void DeleteVisualLines()
    {
       for(int i = ObjectsTotal(); i >= 0; i--)
         if(StringSubstr(ObjectName(i), 0, 4) == "INT.")
           ObjectDelete(ObjectName(i));
    }
    
    
    // MT4 function move SL to BreakEven (add your own move SL to BreakEven function here)
    //+-------------------------------------------------------------------------------------------+
    void MoveSLBE()
    {
       actualSL= Bid; //Just an example where the SL & the SL line changes based on variable
       ObjectMove("INT.SLLine", 0, Time[0], actualSL);
    }
    
    
    // MT4GUI function, change button states
    //+-------------------------------------------------------------------------------------------+
    void ChangeINTradeState()
    {
       if (guiIsChecked(hwnd,enableINTradeBtn))
       {
          guiEnable(hwnd, visualLinesBtn, 1);
          guiEnable(hwnd, bEBtn, 1);
          guiEnable(hwnd, tP1Btn, 1);
          guiEnable(hwnd, tP2Btn, 1);
          guiEnable(hwnd, tP3Btn, 1);
          guiSetBgColor(hwnd, bEBtn, LimeGreen);
          guiSetBgColor(hwnd, visualLinesBtn, LimeGreen);
          guiSetBgColor(hwnd, tP1Btn, MediumSpringGreen);
          guiSetBgColor(hwnd, tP2Btn, MediumSpringGreen);  
          guiSetBgColor(hwnd, tP3Btn, MediumSpringGreen);   
       }
       else
       {
          guiEnable(hwnd, visualLinesBtn, 0);
          guiEnable(hwnd, bEBtn, 0);
          guiEnable(hwnd, tP1Btn, 0);
          guiEnable(hwnd, tP2Btn, 0);
          guiEnable(hwnd, tP3Btn, 0);
          guiSetBgColor(hwnd, bEBtn, LightGray);
          guiSetBgColor(hwnd, visualLinesBtn, LightGray);
          guiSetBgColor(hwnd, tP1Btn, LightGray);
          guiSetBgColor(hwnd, tP2Btn, LightGray);  
          guiSetBgColor(hwnd, tP3Btn, LightGray); 
          DeleteVisualLines();
       }
    }
    
    
    // MT4 function, change TP/SL based on visual line move
    //+-------------------------------------------------------------------------------------------+
    void ChangeTPSL()
    {
       if (!ObjectFind("INT.SLLine")==-1) actualSL = ObjectGet("INT.SLLine", OBJPROP_PRICE1);
       if (!ObjectFind("INT.TPLine")==-1) actualTP = ObjectGet("INT.TPLine", OBJPROP_PRICE1);
       ObjectMove("INT.SLLineLabel", 0, Time[5], ObjectGet("INT.SLLine", OBJPROP_PRICE1));
       ObjectMove("INT.TPLineLabel", 0, Time[5], ObjectGet("INT.TPLine", OBJPROP_PRICE1));
    }
    
    
    // MT4 function, change TP based on radio button
    //+-------------------------------------------------------------------------------------------+
    void ChangeTP(int level)
    {
       if (level==1) actualTP = Bid+TPLevel1*Point;
       else if (level==2) actualTP = Bid+TPLevel2*Point;
       else if (level==3) actualTP = Bid+TPLevel3*Point;
       ObjectMove("INT.TPLine", 0, Time[0], actualTP);
    }
    

    VIDEOCLIP:

    Tomorrow I will show you how you can use MT4GUI functionality for “PRE-trade”-actions!

    Thats all folks!

    Leave a comment