EA removes itself from Chart when finished.

To add comments, please log in or register
dabbler
1091
dabbler  

Here is my self-destructing EA. The idea is that when it is finished doing its job, it removes itself from the chart. And it does do this very nicely and in not too many lines of code.


// Using any sort of PostMessage is a HACK and not recommended, but for otherwise unsupported functionality it can
// be useful. Just check it on your operating system and your MT4 version before committing to it - at your own risk.


//================================================================== THIS is the relevant code to post into your EA
#include <WinUser32.mqh>
#include <stdlib.mqh>

bool abort=false;

#define EA_KILL  33050

void SelfDestruct(){
   abort=true;       // don't put all our eggs in one basket; leave the known, trusted abort code in place as well!
   Print("Self-destruct initiated");

   // We send the message to this Window and let this window send it to its Parent
   // then the Parent knows which Window it came from and therefore what to do
    int hWnd     = WindowHandle( Symbol(),0 );
   
   PostMessageA( hWnd, WM_COMMAND, EA_KILL, 0 );
   return;
}
//=================================================================== THE stuff below is just a test harness

int  count = 0;
bool timeToDie = false;

//----------------------------------------------------------------------------
int init(){
   if( abort )
      return( 0 );
   
   // do some useful stuff here   
   count=0;   
   return(0);
}

//-----------------------------------------------------------------------------
int deinit(){

   return(0);
}

//------------------------------------------------------------------------------
int start(){
   if( abort )
      return( 0 );
   
   // do some useful stuff in here
   
   count++;
   if( count > 9 )     // we have to decide to self-destruct. This particular test is obviously just for testing
      timeToDie = true;
   
   Print(count);
      
   if( timeToDie ){
      SelfDestruct();
      return( 0 );
   }   

   return(0);
}
//------------------------------------------------------------------------------



dabbler
1091
dabbler  

I tried a little indicator to see if it would tell me anything.

#property indicator_chart_window

#include <WinUser32.mqh>
#include <stdlib.mqh>

#import "user32.dll"
   int GetAncestor (int hwnd, int gaFlags);
#import

int start(){
   int hWnd     = WindowHandle( Symbol(),0 );
   int mainHwnd = GetAncestor(hWnd,2);
   Comment( "handle=" + hWnd + "\nparent=" + mainHwnd );

   return(0);
}

Sure enough, two windows with the same symbol and the same timeframe do give different handles on the hWnd. I tried passing this in the PeekMessageA as the lparam

PostMessageA( mainHwnd, WM_COMMAND, EA_KILL, hWnd );

No joy :-(

How does it know which Window to send it to? How about the window with focus? A few more commands required ...


EDIT: It was actually fewer commands! By sending the message to the chart window (which doesn't know how to handle the message and therefore passes it up to its parent to handle) the parent knows who sent the message and therefore what action to take on which chart. It doesn't need input focus by actual test. The original problem of two charts with the same symbol and the same period is therefore resolved :-)

To add comments, please log in or register