Second(s) timeframes on your MT4

ALkhalil78

Trader
Jan 5, 2026
10
3
9
48
this script converts the chart's ticks into any Second(s)timeframes !!!

the Script works only for XAUUSD, you can modify it to work with other pairs or other names of Gold pair.

Example custom periods:

S5
S10
S15
S20
S30
S45
S50

Once the Second(s) chart is generated, you can apply almost everything you normally use on regular charts:

✅ Templates ( .tpl )
✅ Indicators
✅ Expert Advisors
✅ Objects & drawings
✅ Trendlines
✅ Fibonacci tools
✅ Moving averages
✅ Oscillators
✅ Custom indicators
✅ Price action tools



Enjoy the Script !!

MQL4:
//+------------------------------------------------------------------+
//|                                             XAUUSD_Seconds.mq4   |
//|                        Custom Seconds Offline Chart Generator    |
//|                        For MT4 Build 600+                        |
//+------------------------------------------------------------------+
#property strict
#property show_inputs
 
input int   InpSecondsPeriod = 10;      // Seconds timeframe
input bool  InpAutoOpenChart = true;    // Auto open offline chart
input bool  InpShowComment   = true;    // Show status comment
 
int      ExtHandle      = -1;
long     OfflineChartID = 0;
int      OfflinePeriod  = 20000;
 
datetime CurrentBarTime = 0;
 
MqlRates Rate;
 
string OfflineFileName;
 
//+------------------------------------------------------------------+
//| Script start                                                     |
//+------------------------------------------------------------------+
void OnStart()
{
   if(Symbol() != "XAUUSD")
   {
      Alert("Attach this script to XAUUSD only.");
      return;
   }
 
   if(InpSecondsPeriod < 1)
   {
      Alert("Seconds period must be >= 1");
      return;
   }
 
   OfflinePeriod += InpSecondsPeriod;
 
   OfflineFileName = Symbol() + (string)OfflinePeriod + ".hst";
 
   ExtHandle = FileOpenHistory(
      OfflineFileName,
      FILE_BIN|FILE_WRITE|FILE_SHARE_WRITE|FILE_SHARE_READ
   );
 
   if(ExtHandle < 0)
   {
      Alert("Failed to create offline history file.");
      return;
   }
 
   WriteHistoryHeader();
 
   WaitForFirstTick();
 
   BuildInitialBar();
 
   if(InpAutoOpenChart)
   {
      OpenOfflineChart();
   }
 
   MainLoop();
}
 
//+------------------------------------------------------------------+
//| Wait for first market tick                                       |
//+------------------------------------------------------------------+
void WaitForFirstTick()
{
   while(!IsStopped())
   {
      if(RefreshRates())
      {
         if(TimeCurrent() > 0)
            break;
      }
 
      Sleep(100);
   }
}
 
//+------------------------------------------------------------------+
//| Build first candle                                               |
//+------------------------------------------------------------------+
void BuildInitialBar()
{
   datetime now = TimeCurrent();
 
   CurrentBarTime = NormalizeSecondTime(now);
 
   Rate.time         = CurrentBarTime;
   Rate.open         = Bid;
   Rate.high         = Bid;
   Rate.low          = Bid;
   Rate.close        = Bid;
   Rate.tick_volume  = 1;
   Rate.spread       = MarketInfo(Symbol(), MODE_SPREAD);
   Rate.real_volume  = 0;
 
   FileWriteStruct(ExtHandle, Rate);
   FileFlush(ExtHandle);
}
 
//+------------------------------------------------------------------+
//| Main processing loop                                             |
//+------------------------------------------------------------------+
void MainLoop()
{
   datetime last_refresh = 0;
 
   while(!IsStopped())
   {
      RefreshRates();
 
      datetime now = TimeCurrent();
 
      datetime normalized = NormalizeSecondTime(now);
 
      if(normalized > CurrentBarTime)
      {
         FinalizeCurrentBar();
 
         StartNewBar(normalized);
      }
      else
      {
         UpdateCurrentBar();
      }
 
      if(InpShowComment)
      {
         Comment(
            "XAUUSD Seconds Chart Generator\n",
            "Seconds TF : S", InpSecondsPeriod, "\n",
            "Offline ID : ", OfflinePeriod, "\n",
            "Status     : RUNNING\n",
            "Bid        : ", DoubleToString(Bid, Digits)
         );
      }
 
      if(TimeLocal() - last_refresh >= 2)
      {
         RefreshOfflineChart();
         last_refresh = TimeLocal();
      }
 
      Sleep(20);
   }
 
   Comment("");
}
 
//+------------------------------------------------------------------+
//| Start new candle                                                 |
//+------------------------------------------------------------------+
void StartNewBar(datetime bar_time)
{
   CurrentBarTime = bar_time;
 
   Rate.time         = CurrentBarTime;
   Rate.open         = Bid;
   Rate.high         = Bid;
   Rate.low          = Bid;
   Rate.close        = Bid;
   Rate.tick_volume  = 1;
   Rate.spread       = MarketInfo(Symbol(), MODE_SPREAD);
   Rate.real_volume  = 0;
 
   FileWriteStruct(ExtHandle, Rate);
   FileFlush(ExtHandle);
}
 
//+------------------------------------------------------------------+
//| Update current candle                                            |
//+------------------------------------------------------------------+
void UpdateCurrentBar()
{
   if(Bid > Rate.high)
      Rate.high = Bid;
 
   if(Bid < Rate.low)
      Rate.low = Bid;
 
   Rate.close = Bid;
 
   Rate.tick_volume++;
 
   int struct_size = sizeof(MqlRates);
 
   FileSeek(ExtHandle, -struct_size, SEEK_END);
 
   FileWriteStruct(ExtHandle, Rate);
 
   FileFlush(ExtHandle);
}
 
//+------------------------------------------------------------------+
//| Finalize candle                                                  |
//+------------------------------------------------------------------+
void FinalizeCurrentBar()
{
   int struct_size = sizeof(MqlRates);
 
   FileSeek(ExtHandle, -struct_size, SEEK_END);
 
   FileWriteStruct(ExtHandle, Rate);
 
   FileFlush(ExtHandle);
}
 
//+------------------------------------------------------------------+
//| Normalize time                                                   |
//+------------------------------------------------------------------+
datetime NormalizeSecondTime(datetime t)
{
   return (t / InpSecondsPeriod) * InpSecondsPeriod;
}
 
//+------------------------------------------------------------------+
//| Write HST header                                                 |
//+------------------------------------------------------------------+
void WriteHistoryHeader()
{
   int    version = 401;
   string copyright;
   string symbol = Symbol();
   int    period = OfflinePeriod;
   int    digits = Digits;
   int    unused[13];
 
   ArrayInitialize(unused, 0);
 
   copyright = "Custom Seconds Chart";
 
   FileWriteInteger(ExtHandle, version, LONG_VALUE);
   FileWriteString(ExtHandle, copyright, 64);
   FileWriteString(ExtHandle, symbol, 12);
   FileWriteInteger(ExtHandle, period, LONG_VALUE);
   FileWriteInteger(ExtHandle, digits, LONG_VALUE);
   FileWriteInteger(ExtHandle, 0, LONG_VALUE);
   FileWriteInteger(ExtHandle, 0, LONG_VALUE);
 
   FileWriteArray(ExtHandle, unused, 0, 13);
}
 
//+------------------------------------------------------------------+
//| Open offline chart                                               |
//+------------------------------------------------------------------+
void OpenOfflineChart()
{
   OfflineChartID = ChartOpen(Symbol(), OfflinePeriod);
 
   Sleep(1000);
 
   RefreshOfflineChart();
}
 
//+------------------------------------------------------------------+
//| Refresh chart                                                    |
//+------------------------------------------------------------------+
void RefreshOfflineChart()
{
   if(OfflineChartID <= 0)
      return;
 
   ChartSetSymbolPeriod(
      OfflineChartID,
      Symbol(),
      OfflinePeriod
   );
 
   ChartRedraw(OfflineChartID);
}
 
//+------------------------------------------------------------------+
//| Cleanup                                                          |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
   Comment("");
 
   if(ExtHandle >= 0)
   {
      FileClose(ExtHandle);
      ExtHandle = -1;
   }
}
//+------------------------------------------------------------------+
 
Last edited by a moderator:
  • 👍
Reactions: Enivid
It looks like this script will just convert the real-time incoming ticks into a seconds chart. It doesn't convert any historical data, right?
Yes
Post automatically merged:

It looks like this script will just convert the real-time incoming ticks into a seconds chart. It doesn't convert any historical data, right?
run it once, forget it and let it go

you will have Second(s) charts the same as on OlympTrade site.