//+------------------------------------------------------------------+
//|                                            AutoRefresh_Chart.mq4 |
//|                                          Copyright 2022 mql5.com |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022 mql5.com"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window

// First you need this:
#include <WinUser32.mqh>

// Then you must define this:
int hWindow = 0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping

// Then you initialise this:
 hWindow = WindowHandle(Symbol(),Period());
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
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[])
  {
//---
    // Then you need this:
    int message;
    
    
// Finally you put the rest into your specific if(){ } conditions.
   if(IsNewBar((ENUM_TIMEFRAMES)_Period))
    { 
    switch( Period() )
    {
      case 1:
        message = 33137;
        break;
      case 5:
        message = 33138;
        break;
      case 15:
        message = 33139;
        break;
      case 30:
        message = 33140;
        break;
      case 60:
        message = 33135;
        break;
      case 240:
        message = 33136;
        break;
      case 1440:
        message = 33134;
        break;
      case 10080:
        message = 33141;
        break;
      case 43200:
        message = 33334;
        break; 
      default:
        message = 33137; // m1, if we can't identify current TF
        break;
    }

    PostMessageA( hWindow, WM_COMMAND, 33141, 0 ); // switch to W1 TF
    ObjectsDeleteAll();
    PostMessageA( hWindow, WM_COMMAND, message, 0 ); // switch to original TF 
    }
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
bool IsNewBar(ENUM_TIMEFRAMES per)
{ 
  static datetime Trend_Candle_prevTime1 = -1;
  
  if(Trend_Candle_prevTime1 != iTime(_Symbol,per,6))
  { 
   Trend_Candle_prevTime1 = iTime(_Symbol,per,6); 
       
   return(true);  
  } 

  return(false); 
}
//+------------------------------------------------------------------+
