//+------------------------------------------------------------------+
//|                                              AutoReFresh S&N.mq4 |
//|                                             Copyright 2014, S&&N |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, S&&N"
#property version   "1.00"
#property strict
#property indicator_chart_window


#import "user32.dll"
  int   RegisterWindowMessageA(string lpString);
  int   SetTimer(int hWnd,int nIDEvent,int uElapse,int& lpTimerFunc[]);
  bool  KillTimer(int hWnd,int uIDEvent);
  
#import "kernel32.dll"
  int   GetModuleHandleA(string lpModuleName);
  int   GetProcAddress(int hModule,string lpProcName);  
  bool  VirtualProtect(int lpAddress, int dwSize, int flNewProtect, int& lpflOldProtect[]);
#import
  
extern int RefreshPeriod  = 1000; //Периодичность обновления, миллисекунд
int TimerId=666;
int TimerCode[7];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   SetMyTimer(RefreshPeriod);  // интервал в миллисекундах   
   return(INIT_SUCCEEDED);
  }

int deinit()
{
   KillMyTimer();
   //Comment("");
   return(0);
}  

//+------------------------------------------------------------------+
//| program start function                                           |
//+------------------------------------------------------------------+
int start()
{
  static int n=0;
  n++; 
  //Comment("tick:  ",n);  
  return(0);
}
//-------------------------------------------------------------------

int SetMyTimer(int Interval)
{    
  int hWnd= WindowHandle(Symbol(),Period());
  int MT4InternMsg= RegisterWindowMessageA("MetaTrader4_Internal_Message");
  int PostMsgAddr= GetProcAddress(GetModuleHandleA("user32.dll"),"PostMessageA");
  if (PostMsgAddr==0 || hWnd==0) return(0);
  // push ebp; move ebp,esp; push 01; push 02; push MT4InternMsg; push hWnd; mov eax,PostMsgAddr; call eax; pop ebp; ret 0010;    
  int value[]={ 0x55, 0x8B,0xEC, 0x6A,01, 0x6A,02, 0x68,0000, 0x68,0000, 0xB8,0000, 0xFF,0xD0, 0x5D, 0xC2,0x10 };
  int len[]=  { 1,    1,   1,    1,   1,  1,   1,  1,   4,    1,   4,    1,   4,    1,   1,    1,    1,   2 };
  value[8]=MT4InternMsg;  value[10]=hWnd;  value[12]=PostMsgAddr;
  ArrayInitialize(TimerCode,0);
  int byte=0;  
  for (int i=0;  i<ArraySize(value);  i++)
    for (int j=0;  j<len[i];  j++, byte++)
      TimerCode[byte/4] |= value[i]>>(8*j)&0xFF<<(byte%4*8);

  return ( SetTimer(hWnd, TimerId, Interval, TimerCode) );
}

//---------------------------------------------------

bool KillMyTimer()
{
  return( KillTimer(WindowHandle(Symbol(),Period()), TimerId) );
}