Second(s) timeframes on your MT5

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 !!

//+------------------------------------------------------------------+
//|                                              GoldSecondsMT5.mq5  |
//|                     MT5 Native Seconds Chart Generator            |
//|                     Custom Symbol Engine Version                  |
//+------------------------------------------------------------------+
#property strict

input string InpSourceSymbol = "XAUUSD";
input int    InpSeconds      = 10;
input bool   InpAutoOpen     = true;

string CustomSymbolName;

MqlRates CurrentBar;

datetime CurrentBarTime = 0;

long LastTickTimeMSC = 0;

//+------------------------------------------------------------------+
//| Expert initialization                                            |
//+------------------------------------------------------------------+
int OnInit()
{
   if(InpSeconds < 1)
   {
      Print("Seconds must be >= 1");
      return(INIT_FAILED);
   }

   CustomSymbolName = InpSourceSymbol + "_S" + IntegerToString(InpSeconds);

   Print("Creating custom symbol: ", CustomSymbolName);

   CreateCustomSymbol();

   OpenCustomChart();

   EventSetMillisecondTimer(50);

   return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Expert deinitialization                                          |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
   EventKillTimer();
}

//+------------------------------------------------------------------+
//| Millisecond timer                                                |
//+------------------------------------------------------------------+
void OnTimer()
{
   MqlTick tick;

   if(!SymbolInfoTick(InpSourceSymbol, tick))
      return;

   if(tick.time_msc == LastTickTimeMSC)
      return;

   LastTickTimeMSC = tick.time_msc;

   ProcessTick(tick);
}

//+------------------------------------------------------------------+
//| Process incoming tick                                            |
//+------------------------------------------------------------------+
void ProcessTick(MqlTick &tick)
{
   datetime bar_time =
      (datetime)((tick.time / InpSeconds) * InpSeconds);

   //--- First bar
   if(CurrentBarTime == 0)
   {
      StartNewBar(bar_time, tick);
      return;
   }

   //--- New candle
   if(bar_time > CurrentBarTime)
   {
      PushBarToCustomSymbol(CurrentBar);

      StartNewBar(bar_time, tick);
   }
   else
   {
      UpdateCurrentBar(tick);
   }
}

//+------------------------------------------------------------------+
//| Start new candle                                                 |
//+------------------------------------------------------------------+
void StartNewBar(datetime bar_time, MqlTick &tick)
{
   CurrentBarTime = bar_time;

   CurrentBar.time        = bar_time;
   CurrentBar.open        = tick.bid;
   CurrentBar.high        = tick.bid;
   CurrentBar.low         = tick.bid;
   CurrentBar.close       = tick.bid;
   CurrentBar.tick_volume = 1;
   CurrentBar.spread      = 0;
   CurrentBar.real_volume = 0;
}

//+------------------------------------------------------------------+
//| Update current candle                                            |
//+------------------------------------------------------------------+
void UpdateCurrentBar(MqlTick &tick)
{
   if(tick.bid > CurrentBar.high)
      CurrentBar.high = tick.bid;

   if(tick.bid < CurrentBar.low)
      CurrentBar.low = tick.bid;

   CurrentBar.close = tick.bid;

   CurrentBar.tick_volume++;
}

//+------------------------------------------------------------------+
//| Push candle to custom symbol                                     |
//+------------------------------------------------------------------+
void PushBarToCustomSymbol(MqlRates &bar)
{
   MqlRates rates[1];

   rates[0] = bar;

   if(!CustomRatesUpdate(CustomSymbolName, rates))
   {
      Print("CustomRatesUpdate failed. Error: ", GetLastError());
   }
}

//+------------------------------------------------------------------+
//| Create custom symbol                                             |
//+------------------------------------------------------------------+
void CreateCustomSymbol()
{
   //--- already exists
   if(SymbolSelect(CustomSymbolName, true))
   {
      Print("Custom symbol already exists.");
      return;
   }

   //--- create
   if(!CustomSymbolCreate(
      CustomSymbolName,
      "Custom",
      InpSourceSymbol))
   {
      Print("Failed creating custom symbol. Error: ", GetLastError());
      return;
   }

   //--- select
   SymbolSelect(CustomSymbolName, true);

   //--- synchronize properties
   CustomSymbolSetInteger(
      CustomSymbolName,
      SYMBOL_DIGITS,
      (int)SymbolInfoInteger(InpSourceSymbol, SYMBOL_DIGITS)
   );

   CustomSymbolSetDouble(
      CustomSymbolName,
      SYMBOL_POINT,
      SymbolInfoDouble(InpSourceSymbol, SYMBOL_POINT)
   );

   Print("Custom symbol created successfully.");
}

//+------------------------------------------------------------------+
//| Open custom chart                                                |
//+------------------------------------------------------------------+
void OpenCustomChart()
{
   if(!InpAutoOpen)
      return;

   long chart_id = ChartOpen(CustomSymbolName, PERIOD_M1);

   if(chart_id > 0)
   {
      Print("Custom chart opened.");
   }
   else
   {
      Print("Failed opening chart.");
   }
}
//+------------------------------------------------------------------+
here is a new modified version for MT5 >>>>>>>>
//+------------------------------------------------------------------+
//|                                              GoldSecondsMT5.mq5  |
//|                     MT5 Native Seconds Chart Generator            |
//|                     Custom Symbol Engine Version                  |
//+------------------------------------------------------------------+
#property strict

enum ENUM_SOURCE_MODE
{
   SOURCE_MODE_AUTO_GOLD = 0,  // Auto-detect Gold (any XAU/GOLD name)
   SOURCE_MODE_CHART     = 1,  // Chart symbol (works for any instrument)
   SOURCE_MODE_MANUAL    = 2   // Manual symbol (any instrument)
};

input ENUM_SOURCE_MODE InpSourceMode  = SOURCE_MODE_AUTO_GOLD;
input string           InpSourceSymbol = "XAUUSD";
input int              InpSeconds      = 10;
input bool             InpAutoOpen     = true;

string SourceSymbolName;
string CustomSymbolName;

MqlRates CurrentBar;

datetime CurrentBarTime = 0;

long LastTickTimeMSC = 0;

//+------------------------------------------------------------------+
//| Expert initialization                                            |
//+------------------------------------------------------------------+
int OnInit()
{
   if(InpSeconds < 1)
   {
      Print("Seconds must be >= 1");
      return(INIT_FAILED);
   }

   //--- resolve the actual symbol to watch
   SourceSymbolName = ResolveSourceSymbol();

   if(StringLen(SourceSymbolName) == 0)
   {
      Print("Failed to resolve source symbol. Check the inputs.");
      return(INIT_FAILED);
   }

   //--- ticks stream only for symbols selected in Market Watch
   if(!SymbolSelect(SourceSymbolName, true))
   {
      Print("Failed to select symbol in Market Watch: ", SourceSymbolName, " (", GetLastError(), ")");
      return(INIT_FAILED);
   }

   CustomSymbolName = MakeCustomSymbolName(SourceSymbolName, InpSeconds);

   Print("Source symbol: ", SourceSymbolName);
   Print("Creating custom symbol: ", CustomSymbolName);

   CreateCustomSymbol();

   OpenCustomChart();

   EventSetMillisecondTimer(50);

   return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Expert deinitialization                                          |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
   EventKillTimer();
}

//+------------------------------------------------------------------+
//| Millisecond timer                                                |
//+------------------------------------------------------------------+
void OnTimer()
{
   MqlTick tick;

   if(!SymbolInfoTick(SourceSymbolName, tick))
      return;

   if(tick.time_msc == LastTickTimeMSC)
      return;

   LastTickTimeMSC = tick.time_msc;

   ProcessTick(tick);
}

//+------------------------------------------------------------------+
//| Process incoming tick                                            |
//+------------------------------------------------------------------+
void ProcessTick(MqlTick &tick)
{
   datetime bar_time =
      (datetime)((tick.time / InpSeconds) * InpSeconds);

   //--- First bar
   if(CurrentBarTime == 0)
   {
      StartNewBar(bar_time, tick);
      return;
   }

   //--- New candle
   if(bar_time > CurrentBarTime)
   {
      PushBarToCustomSymbol(CurrentBar);

      StartNewBar(bar_time, tick);
   }
   else
   {
      UpdateCurrentBar(tick);
   }
}

//+------------------------------------------------------------------+
//| Start new candle                                                 |
//+------------------------------------------------------------------+
void StartNewBar(datetime bar_time, MqlTick &tick)
{
   CurrentBarTime = bar_time;

   CurrentBar.time        = bar_time;
   CurrentBar.open        = tick.bid;
   CurrentBar.high        = tick.bid;
   CurrentBar.low         = tick.bid;
   CurrentBar.close       = tick.bid;
   CurrentBar.tick_volume = 1;
   CurrentBar.spread      = 0;
   CurrentBar.real_volume = 0;
}

//+------------------------------------------------------------------+
//| Update current candle                                            |
//+------------------------------------------------------------------+
void UpdateCurrentBar(MqlTick &tick)
{
   if(tick.bid > CurrentBar.high)
      CurrentBar.high = tick.bid;

   if(tick.bid < CurrentBar.low)
      CurrentBar.low = tick.bid;

   CurrentBar.close = tick.bid;

   CurrentBar.tick_volume++;
}

//+------------------------------------------------------------------+
//| Push candle to custom symbol                                     |
//+------------------------------------------------------------------+
void PushBarToCustomSymbol(MqlRates &bar)
{
   MqlRates rates[1];

   rates[0] = bar;

   if(!CustomRatesUpdate(CustomSymbolName, rates))
   {
      Print("CustomRatesUpdate failed. Error: ", GetLastError());
   }
}

//+------------------------------------------------------------------+
//| Create custom symbol                                             |
//+------------------------------------------------------------------+
void CreateCustomSymbol()
{
   //--- already exists
   if(SymbolSelect(CustomSymbolName, true))
   {
      Print("Custom symbol already exists.");
      return;
   }

   //--- create
   if(!CustomSymbolCreate(
      CustomSymbolName,
      "Custom",
      SourceSymbolName))
   {
      Print("Failed creating custom symbol. Error: ", GetLastError());
      return;
   }

   //--- select
   SymbolSelect(CustomSymbolName, true);

   //--- synchronize properties
   CustomSymbolSetInteger(
      CustomSymbolName,
      SYMBOL_DIGITS,
      (int)SymbolInfoInteger(SourceSymbolName, SYMBOL_DIGITS)
   );

   CustomSymbolSetDouble(
      CustomSymbolName,
      SYMBOL_POINT,
      SymbolInfoDouble(SourceSymbolName, SYMBOL_POINT)
   );

   Print("Custom symbol created successfully.");
}

//+------------------------------------------------------------------+
//| Open custom chart                                                |
//+------------------------------------------------------------------+
void OpenCustomChart()
{
   if(!InpAutoOpen)
      return;

   long chart_id = ChartOpen(CustomSymbolName, PERIOD_M1);

   if(chart_id > 0)
   {
      Print("Custom chart opened.");
   }
   else
   {
      Print("Failed opening chart.");
   }
}

//+------------------------------------------------------------------+
//| Resolve which symbol the script should watch                     |
//+------------------------------------------------------------------+
string ResolveSourceSymbol()
{
   if(InpSourceMode == SOURCE_MODE_CHART)
      return(_Symbol);

   if(InpSourceMode == SOURCE_MODE_MANUAL)
   {
      if(SymbolSelect(InpSourceSymbol, true))
         return(InpSourceSymbol);
      return("");
   }

   //--- AUTO_GOLD: look for any gold alias, Market Watch first, then all symbols
   string found = FindGoldSymbol(true);

   if(StringLen(found) == 0)
      found = FindGoldSymbol(false);

   if(StringLen(found) == 0)
   {
      Print("No XAU/GOLD symbol found. Falling back to: ", InpSourceSymbol);

      if(SymbolSelect(InpSourceSymbol, true))
         found = InpSourceSymbol;
   }

   return(found);
}

//+------------------------------------------------------------------+
//| Scan terminal symbol list for a gold alias                       |
//+------------------------------------------------------------------+
string FindGoldSymbol(bool market_watch_only)
{
   string best_name  = "";
   int    best_score = 0;

   int total = SymbolsTotal(market_watch_only);

   for(int i = 0; i < total; i++)
   {
      string name = SymbolName(i, market_watch_only);

      if(StringLen(name) == 0)
         continue;

      int score = GoldScore(name);

      if(score > best_score)
      {
         best_score = score;
         best_name  = name;
      }
   }

   return(best_name);
}

//+------------------------------------------------------------------+
//| Score a symbol name as a gold alias (0 = not gold)               |
//+------------------------------------------------------------------+
int GoldScore(string symbol)
{
   string ucase = symbol;
   StringToUpper(ucase);

   int score = 0;

   if(StringFind(ucase, "XAUUSD") >= 0)
      score += 100;
   else if(StringFind(ucase, "XAU") >= 0)
      score += 60;
   else if(StringFind(ucase, "GOLD") >= 0)
      score += 50;
   else
      return(0);

   //--- slight preference for the most common names
   if(ucase == "XAUUSD")    score += 10;
   if(ucase == "XAUUSD.A")  score += 8;
   if(ucase == "XAUUSDM")   score += 8;
   if(ucase == "GOLD")      score += 5;

   return(score);
}

//+------------------------------------------------------------------+
//| Build a valid unique custom symbol name                          |
//+------------------------------------------------------------------+
string MakeCustomSymbolName(string source_symbol, int seconds)
{
   string name = source_symbol + "_S" + IntegerToString(seconds);

   //--- sanitize characters that are unsafe in custom symbol names
   StringReplace(name, ".", "_");
   StringReplace(name, "/", "_");
   StringReplace(name, "\\", "_");
   StringReplace(name, " ", "_");

   //--- keep inside the terminal symbol name length limit
   if(StringLen(name) > 60)
      name = StringSubstr(name, 0, 60);

   return(name);
}
//+------------------------------------------------------------------+
Ynal Al Khalil #:
here is a new modified version for MT5 >>>>>>>>

Doesn't seem to work, I launched the script as is on an EURUSD chart. XAUUSD symbol is existing.


Nice work - the Script is useful and works as described.

Consider publishing it in MQL5 CodeBase so more users can find and benefit from it.

S10
Alain Verleyen #:

Doesn't seem to work, I launched the script as is on an EURUSD chart. XAUUSD symbol is existing.


maybe because you have a slow Internet connection, 

you have to wait !

and there is a drop down in the Input settings (InpSourceMode) , you have to choose from dear Genius !!!
Oleksandr Medviediev #:

Nice work - the Script is useful and works as described.

Consider publishing it in MQL5 CodeBase so more users can find and benefit from it.

I've tried many times to publish it on CodeBase, but couldn't !!!
why ???

I don't know why it can't be published on CodeBase !!!

do I make a mistake on publishing ?????

Ynal Al Khalil #:

I've tried many times to publish it on CodeBase, but couldn't !!!
why ???

I don't know why it can't be published on CodeBase !!!

do I make a mistake on publishing ?????

Just post it in your Blog where you don't have to jump through any hoops nor wait.
Ryan L Johnson #:
Just post it in your Blog where you don't have to jump through any hoops nor wait.
I don't know how to post in my Blog, nor I don't have the time and nerves to learn how to post on my Blog !!!!
Ynal Al Khalil #:
I don't know how to post in my Blog, nor I don't have the time and nerves to learn how to post on my Blog !!!!
  1. Click on "Blog" in the left side of your user Profile.
  2. Click on "Add new post" (up top next to Main).
  3. Upload an image to https://imgresizers.com/image-to-800x200 to download a banner.
  4. Upload the resized image/banner.
  5. Type in your utility description, etc.
  6. Attach your utility.
  7. Check "Publish."
  8. Click on "Update."
Ryan L Johnson #:
  1. Click on "Blog" in the left side of your user Profile.
  2. Click on "Add new post" (up top next to Main).
  3. Upload an image to https://imgresizers.com/image-to-800x200 to download a banner.
  4. Upload the resized image/banner.
  5. Type in your utility description, etc.
  6. Attach your utility.
  7. Check "Publish."
  8. Click on "Update."
and then ????
will I get a trophy ??? 😆
Ynal Al Khalil #:
and then ????
will I get a trophy ??? 😆

Of course. That's the age that we live in. Everyone gets a participation-trophy.😂

🏆 (spray painted plastic)