Thursday, 4 October 2012

input from chart object in mql 5 ,

Well , wrote one post on my  mql4examples.blogspot.com   and found writing problems on blog is  fun !!
Leave it for now ...
lets get back to business , my client want  me to develop an EA which takes input from a pop up 
window and I suggested a form attached to charts , using mql5 object library .

But It seems he is not satisfied with the way  GUI looks and takes input , forget it , I know the other solutions too  : ) 

Let me explain you how I coded to take input to EA from GUI :

I gone though the Mql5 site , tried to found different solutions users suggested and  in essence I wrote something like this :

#property copyright "Copyright 2012, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#property version   "1.00"

#include <IncGUI_v3.mqh>
 

very familiar snippet code : just inclued the IncGUI_v3.mqh ,which I found on mql5   website , 
class CForm1: public CFormBase{
   public:
   //1. Declaring the controls  
   
      
   CInputBox in1;
   CInputBox in2;
   CLabel lblAuto;
   CButton buttYes;
   CButton buttNo;
  
 
  protected: 
      // 2. Declaring the variables to restore the control values
       string m_InputBoxValue;
       string m_InputBoxValue2;
       void MainProperties(){
            // Setting the form parameters
            m_Name         =  "Form1";                                                  // Form name. The names of all the controls should start with it.
            m_Width        =  300;           // Form width
            m_Height       =  100;           // Form height
            m_Type         =  1;                                                                 m_Caption      =  "Enter Max Profit & Max Loss"; // Form caption
            m_Movable      =  false;         
            m_Resizable    =  false;                  
            m_CloseButton  =  false;         
         }
        
 void OnInitEvent(){
              
            frm1.in1.Init("in1",50,4,"  Max Profit");
            frm1.in2.Init("in2",50,4,"  Max Loss");  
            frm1.lblAuto.Init("labAuto","Set AutoTrade : ");
            frm1.buttYes.Init("yesBut",40,20,"Enable");
            frm1.buttNo.Init("noBut",40,20,"Disable");        
            in1.SetValue(GlobalVariableGet("gprofitLimit"));
            in2.SetValue(GlobalVariableGet("glossLimit"));
         }
   
         void OnShowEvent(int aLeft,int aTop){
            // 6. Calling the Show(x,y) method for all the controls of the form
         frm1.in1.Show(aLeft+30,aTop+10);
         frm1.in2.Show(aLeft+30,aTop+30);
         frm1.lblAuto.Show(aLeft+30,aTop+50);
         frm1.buttYes.Show(aLeft+30,aTop+70);
         frm1.buttNo.Show(aLeft+90,aTop+70);
         
         
         }

         void OnHideEvent(){
            // 7. Calling the Hide() method for all the controls of the form
             in1.Hide();
             in2.Hide();
             buttNo.Hide();
             buttYes.Hide();
             lblAuto.Hide();
         }

         void OnWindowChangeEvent(int aSubWindow){
          // 8. Calling the SetSubWindow() method for all the controls of the form.            The subwindow number is conveyed by the aSubWindow parameter.
           in1.SetSubWindow(aSubWindow);
           in2.SetSubWindow(aSubWindow);
           lblAuto.SetSubWindow(aSubWindow);
           buttNo.SetSubWindow(aSubWindow);
           buttYes.SetSubWindow(aSubWindow);
            
         }
       
void EventsHandler(const int id,const long& lparam,const double& dparam,const string& sparam){
            // 9. Calling the Event() method of all the controls. 
            // 10. Handling the control events, where necessary
          
            in1.Event(id,lparam,dparam,sparam);
            in2.Event(id,lparam,dparam,sparam);
           int eventYes = buttYes.Event(id,lparam,dparam,sparam);
           int eventNo = buttNo.Event(id,lparam,dparam,sparam);
           if(eventYes==1){GlobalVariableSet("gautoTrade",1);                                                   MessageBox("Enabled  !","Alert !"); }
           if(eventNo==1){GlobalVariableSet("gautoTrade",0);                                                   MessageBox("Disabled  !","Alert !"); } 
            
            
         }

        
         bool OnApplyEvent(){
            // 11. Checking the control values upon closing by the "Apply" button. In             order to reject the closing of the form, false should be returned from               this method.
            // 12. Saving the parameters.
            string val=in1.ValueString(); // entered value in input box
            string val2=in2.ValueString();//entered value in input box 2
            StringTrimLeft(val);
            StringTrimRight(val);
               Alert("apply alert ");
     
           m_InputBoxValue=in1.ValueString(); // Save the value in a variable   of     the form in order to restore the value in the future in case the cancel button has   been pressed
          m_InputBoxValue2=in2.ValueString();
  Alert(" Value entered: "+in1.ValueString(),"  value entered 2 "+in2.ValueString());
                  GlobalVariableSet("gprofitLimit",StringToDouble(m_InputBoxValue));
                  GlobalVariableSet("glossLimit",StringToDouble(m_InputBoxValue2));
                  return(true);
         }
onApplyEvent() : here we will be catching the entered value when Apply button is     pressed . see I have set my global variable gProfitLimit and gLossLimit using those  entered input.                
     bool OnCancelEvent(){
            // 13. Checks upon closing by the "Cancel" or "Close" buttons. In order     to reject the closing of the form, false should be returned from this method.
               
               if(MessageBox("Close? ",Caption(),MB_YESNO)!=IDYES){
                 // GlobalVariableSet("gautoTrade",0);
                  return(false);
               }
            //in1.SetValue(m_InputBoxValue); // Pressed cancel, restoring the value
          ///  in2.SetValue(m_InputBoxValue2);
            return(true);
         }
    
         
};
CForm1 frm1;  // declaration of reference to form class.
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
//---
  frm1.Event(id,lparam,dparam,sparam);  
  }
So using GUI library and writing the form and attaching two inputbox , one level ,   two button , the library provide apply and cancel button inbuilt in code .            
how turn to call this form from our EA code , can be from init() , start () as per   your wish , I loaded form inside EA  init() , it code look like following code       snippet 
 void init()  //EA init method
{
 frm1.Init(); 
fm1.Show(350,250) } // thats it here your form will be shown on you chart . Enter the value and see !

Hmm !!! hoping to put more problem here in future ,  till then 

Take Care 
Happy Coding .