Download MetaTrader 5
To add comments, please log in or register
Find out how to use widgets at MQL5.community
rme0108
29
rme0108 2015.11.09 21:48 

Hi,

I'm working on an EA and I'd like to be able to pause trading without deselecting Autotrading Button or deleting it from the chart since it is holding important data in memory in form of arrays that are needed to continue after unpausing:

I'm using the following code...

//+------------------------------------------------------------------+
//| Check for Chart Events like mouse click                          |
//+------------------------------------------------------------------+
void OnChartEvent (const int id, const long & x, const double& y, const string& name)  
   {
   Print ("Now inside OnChartEvent");
   if(name=="PauseButton")
     {
     PauseEA=1;
     Print("The mouse has been clicked on the object with name '"+name+"'");
     }
   if (id!=0)PauseEA=1;
   };

 I have created the corresponding button using the following code:

//+------------------------------------------------------------------+
//| Create Pause Button to stop EA from trading                      |
//+------------------------------------------------------------------+   
void CreatePauseButton ()
   {
   ObjectCreate(0,"PauseButton",OBJ_BUTTON,0,0,0);
   ObjectSetInteger(0,"PauseButton",OBJPROP_XDISTANCE,530);
   ObjectSetInteger(0,"PauseButton",OBJPROP_YDISTANCE,55);
   ObjectSetInteger(0,"PauseButton",OBJPROP_XSIZE,100);
   ObjectSetInteger(0,"PauseButton",OBJPROP_YSIZE,50);
   
   ObjectSetString(0,"PauseButton",OBJPROP_TEXT,"Pause EA");
   }

 Im new to mql4 so that is why I seem to be unable to detect why I don't get any return value when clicking on the button, neither do I get the Print response. PauseEA doesn't change to 1.

Any help would be appreciated.

Thanks in advance 

honest_knave
951
honest_knave 2015.11.09 21:59  
void OnChartEvent(const int id, const long& lparam, const double& dparam, const string& sparam)
  {
   if(id==CHARTEVENT_OBJECT_CLICK)
     {
      Print ("Now inside OnChartEvent");
      if(sparam=="PauseButton")
        {
         PauseEA=1;
         Print("The mouse has been clicked on the object with name '"+sparam+"'");      
        }
     }
  }
rme0108
29
rme0108 2015.11.10 08:04  

Hi honest knave,

I tried your code but still don't get PauseEA to show 1 en clicked.

It shouldn't matter where in my EA's code this part is put, should it?

What else could I check?

Ah, I had a weird problem with the arrays, when I used void OnTick arrays wouldn't load any values at all. Only after eliminating the OnTick function would arrays work properly.

Thanks for help and advice 

honest_knave
951
honest_knave 2015.11.10 08:21  

I always like to try and simplify things when I can't figure out what is going wrong.

Here is a bare-bones version of your code:

#property strict
#property indicator_chart_window

int OnInit()
  {
   CreatePauseButton();
   return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason)
  {
   ObjectDelete("PauseButton");
  }

void CreatePauseButton ()
   {
   ObjectCreate(0,"PauseButton",OBJ_BUTTON,0,0,0);
   ObjectSetInteger(0,"PauseButton",OBJPROP_XDISTANCE,530);
   ObjectSetInteger(0,"PauseButton",OBJPROP_YDISTANCE,55);
   ObjectSetInteger(0,"PauseButton",OBJPROP_XSIZE,100);
   ObjectSetInteger(0,"PauseButton",OBJPROP_YSIZE,50);
   
   ObjectSetString(0,"PauseButton",OBJPROP_TEXT,"Pause EA");
   }


void OnChartEvent(const int id, const long& lparam, const double& dparam, const string& sparam)
  {
   if(id==CHARTEVENT_OBJECT_CLICK)
     {
      Print ("Now inside OnChartEvent");
      if(sparam=="PauseButton")
        {
         Print("The mouse has been clicked on the object with name '"+sparam+"'");      
        }
     }
  }

int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
   return(rates_total);
  }

 

When applied to a chart, the button is drawn and the print statements work as expected:

 

rme0108
29
rme0108 2015.11.10 16:10  

Hi again :)

when I add

#property indicator_chart_window

to my EA it goes berserk :)

my displayed text and the button never even get created. it seems the EA doesn't even enter the function(s) to do so. So I stopped editing for now. I got to find out first what went wrong in my code that screws up things, like the array problem when using ontick... 

it was so much work and I just hope i didn't screw the whole thing up.

i added the EA as a attached file, maybe you could have a quick peek at it. I would thank you in advance for your time and effort and kindness.

please know that i'm a '67 novice mql4 coder, so expect old style commodore basic style coding :)

Files:
Ali Akcaagac
169
Ali Akcaagac 2015.11.10 17:01  

Two things come to my mind:

  1. You need to enable the EventHandler in the OnInit() function
  2. Use StringCompare rather than name == "Pausebutton"
ChartSetInteger(0, CHART_EVENT_MOUSE_MOVE, true);  // example enables mouse move events

honest_knave
951
honest_knave 2015.11.10 17:58  
rme0108:

Hi again :)

when I add

to my EA it goes berserk :)

my displayed text and the button never even get created. it seems the EA doesn't even enter the function(s) to do so. So I stopped editing for now. I got to find out first what went wrong in my code that screws up things, like the array problem when using ontick... 

it was so much work and I just hope i didn't screw the whole thing up.

i added the EA as a attached file, maybe you could have a quick peek at it. I would thank you in advance for your time and effort and kindness.

please know that i'm a '67 novice mql4 coder, so expect old style commodore basic style coding :)

 

 

The code I wrote was an indicator (sorry, I didn't see you were working with an EA). However, the principle is exactly the same. This in uncompiled as I'm on the move, but it should work as an EA:

#property strict

int OnInit()
  {
   CreatePauseButton();
   return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason)
  {
   ObjectDelete("PauseButton");
  }

void CreatePauseButton ()
   {
   ObjectCreate(0,"PauseButton",OBJ_BUTTON,0,0,0);
   ObjectSetInteger(0,"PauseButton",OBJPROP_XDISTANCE,530);
   ObjectSetInteger(0,"PauseButton",OBJPROP_YDISTANCE,55);
   ObjectSetInteger(0,"PauseButton",OBJPROP_XSIZE,100);
   ObjectSetInteger(0,"PauseButton",OBJPROP_YSIZE,50);
   
   ObjectSetString(0,"PauseButton",OBJPROP_TEXT,"Pause EA");
   }


void OnChartEvent(const int id, const long& lparam, const double& dparam, const string& sparam)
  {
   if(id==CHARTEVENT_OBJECT_CLICK)
     {
      Print ("Now inside OnChartEvent");
      if(sparam=="PauseButton")
        {
         Print("The mouse has been clicked on the object with name '"+sparam+"'");      
        }
     }
  }

void OnTick()
  {
  }

 I can't take a proper look at your EA at the moment (cutting and pasting is about as advanced as I can go on the move!)

rme0108
29
rme0108 2015.11.11 14:28  

Hi honest knave,

i tried a bit more but didn't get any results.

i just don't want to mess up my existing code...

if you don't mind i'll wait till u get the time to have a look. but i really don't want to take up to much of your time so just let me know if i should wait... thanks

honest_knave
951
honest_knave 2015.11.12 16:59  

Hello rme0108,

I compiled your indicator "as is" and applied it to a chart. The OnChartEvent code is working fine:

 

 

I apologise if I'm telling you something you already know, but Print statements don't popup on the screen. They are only written to the Expert log. Perhaps that is why you thought it isn't working?

As an aside, you may want to consider using the strict directive as it will help keep your code on the straight'n'arrow:

#property strict
rme0108
29
rme0108 2015.11.12 17:55  

OOOps,

so sorry, I never tried on a life chart. My programming and testing was done only on the Strategy Tester and using backtesting.

I had no idea that chart events only work on forward testing when attached to  a chart.

sorry

thanks for the help 

honest_knave
951
honest_knave 2015.11.12 18:30  

No problem, I'm glad you got it sorted.

I'm going back a while so it may have changed, but my workaround in backtesting was this:

The buttons still click in backtesting.

So each tick, I had to check the state of the button using

ObjectGetInteger(0,ObjName,OBJPROP_STATE); 
/
To add comments, please log in or register