Download MetaTrader 5

Using a script in all opened chart windows (user32.dll)

To add comments, please log in or register
mar
1147
mar  

Hi,

I think most of you know that script below. You can drag it into a chart, select a timeframe and all opened chart windows will be set to this timeframe.

//+------------------------------------------------------------------+
//|                                                    ChgTF-All.mq4 |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010, zznbrm"

#property show_inputs

#import "user32.dll"
   int      PostMessageA(int hWnd,int Msg,int wParam,int lParam);
   int      GetWindow(int hWnd,int uCmd);
   int      GetParent(int hWnd);
#import

extern int eintTF = PERIOD_D1;
                   
int start()
{      
   bool blnContinue = true;   
   int intParent = GetParent( WindowHandle( Symbol(), Period() ) );   
   int intChild = GetWindow( intParent, 0 );  
   int intCmd;

// ---  
   
   
   switch( eintTF )
   {
      case PERIOD_M1:   intCmd = 33137;  break;
      case PERIOD_M5:   intCmd = 33138;  break;
      case PERIOD_M15:  intCmd = 33139;  break;
      case PERIOD_M30:  intCmd = 33140;  break;
      case PERIOD_H1:   intCmd = 35400;  break;
      case PERIOD_H4:   intCmd = 33136;  break;
      case PERIOD_D1:   intCmd = 33134;  break;
      case PERIOD_W1:   intCmd = 33141;  break;
      case PERIOD_MN1:  intCmd = 33334;  break;
   }
   
   if ( intChild > 0 )   
   {
      if ( intChild != intParent )   PostMessageA( intChild, 0x0111, intCmd, 0 );
   }
   else      blnContinue = false;   
   
   while( blnContinue )
   {
      intChild = GetWindow( intChild, 2 );   
   
      if ( intChild > 0 )   
      { 
         if ( intChild != intParent )   PostMessageA( intChild, 0x0111, intCmd, 0 );
      }
      else   blnContinue = false;   
   }
   
   // Now do the current window
   PostMessageA( intParent, 0x0111, intCmd, 0 );

   return(0);
}



I have no knowledge about Win32 programming and so I tried to use that frame/structure and use my own code which should be executed in every opened chart window. For the first test I wanted to place a comment with the current Symbol. This is my modification:

 

//+------------------------------------------------------------------+
//|                                                    ChgTF-All.mq4 |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010, zznbrm"

#import "user32.dll"
   int      PostMessageA(int hWnd,int Msg,int wParam,int lParam);
   int      GetWindow(int hWnd,int uCmd);
   int      GetParent(int hWnd);
#import
                   
int start()
{      
   bool blnContinue = true;   
   int intParent = GetParent( WindowHandle( Symbol(), Period() ) );   
   int intChild = GetWindow( intParent, 0 );  
   int intCmd;

// ---  
   
   if ( intChild > 0 )   
   {
      if ( intChild != intParent )   PostMessageA( intChild, 0x0111, intCmd, 0 );
   }
   else      blnContinue = false;   
   
   while( blnContinue )
   {
      intChild = GetWindow( intChild, 2 );   
   
      if ( intChild > 0 )   
      { 
         if ( intChild != intParent )   PostMessageA( intChild, 0x0111, intCmd, 0 );
      }
      else   blnContinue = false;   
   }
   
   // Now do the current window
   Comment(Symbol());
   return(0);
}


 

Unfortunately, it only works for the window I drag the script into. Can someone please tell me how to fix it? Thanks!! 

whroeder1
15304
whroeder1  
Don't use PostMessage, use the new Chart Operations - MQL4 Documentation
mar
1147
mar  
Thank you! It works!
mar
1147
mar  

I made a script to change all timeframes of the opened charts to the chart's timeframe the script is dragged into. That also works but I always get a system message:

 

But this only happens when I change the timeframes. With all other scripts (change templates, plot a line etc.) I don't see any system message.

Do you have an idea what that means?

Here is my code:

void OnStart()
  {
//---
   int ChartPer = ChartPeriod();
   long prevChart, currChart = ChartFirst();
   int i = 0;
   while(i < CHARTS_MAX) {
//---
      ChartSetSymbolPeriod(currChart, ChartSymbol(currChart), ChartPer);
//---
      prevChart = currChart;
      currChart = ChartNext(prevChart);
      if(currChart < 0) break;
      i++;
   }
  }
Alain Verleyen
Moderator
31975
Alain Verleyen  
mar:

I made a script to change all timeframes of the opened charts to the chart's timeframe the script is dragged into. That also works but I always get a system message:

 

But this only happens when I change the timeframes. With all other scripts (change templates, plot a line etc.) I don't see any system message.

Do you have an idea what that means?

Here is my code:

 

You are trying to change to timeframe of the current chart where the script is running.
mar
1147
mar  
Yes, of course!! Thank you!! Fixed it.
To add comments, please log in or register