How can I create an Chart-Window, like USD/EUR but a new candle shows up every 10 ticks or better every 10 sec?

 

I am tying to make a EA that creates a new Chart Window And It works but the candles do not show up every 10 seconds only like One Minute because it is taking the base information from the 1Min lets say USD/EUR Chart:(Here is the code):

//+------------------------------------------------------------------+

//|                                             10SekEveryCandle.mq5 |

//|                                  Copyright 2026, MetaQuotes Ltd. |

//|                                             https://www.mql5.com |

//+------------------------------------------------------------------+

#property copyright "Copyright 2026, MetaQuotes Ltd."

#property link      "https://www.mql5.com"

#property version   "1.00"

#property strict



input int    CandleSeconds = 10;     

input int    MaxCandles     = 200;  

input color  BullColor      = clrLime;

input color  BearColor      = clrRed;

input color  WickColor      = clrWhite;

input bool   DebugPrints    = false;



datetime currentPeriodStart = 0;

double   openPrice, highPrice, lowPrice, closePrice;

bool     periodActive = false;



long     target_chart = 0;



string PREFIX_BODY      = "CBody_";

string PREFIX_WICK_UP   = "CWickUp_";

string PREFIX_WICK_DOWN = "CWickDown_";





int OnInit()

{

  string sym = _Symbol;

  target_chart = ChartOpen(sym, PERIOD_M1);

  if(target_chart == 0)

  {

    if(DebugPrints) Print("ChartOpen failed for ", sym);

    return(INIT_FAILED);

  }



  string chartLabel = StringFormat("%s_%ds", sym, CandleSeconds);

  ChartSetString(target_chart, CHART_COMMENT, chartLabel);



  ChartSetInteger(target_chart, CHART_AUTOSCROLL, true);

  ChartNavigate(target_chart, CHART_END);



  HideNativeCandles(target_chart);



  CleanupOldObjects(target_chart, 0);



  if(DebugPrints) PrintFormat("10sCandleGenerator_MT5 initialisiert. Zielchart=%d Label=%s", target_chart, chartLabel);

  return(INIT_SUCCEEDED);

}

void OnDeinit(const int reason)

{

  if(DebugPrints) PrintFormat("Deinit reason=%d", reason);

void OnTick()

{

  if(target_chart == 0)

  {

    target_chart = ChartOpen(_Symbol, PERIOD_M1);

    if(target_chart == 0)

    {

      if(DebugPrints) Print("Re-ChartOpen failed");

      return;

    }

    string chartLabel = StringFormat("%s_%ds", _Symbol, CandleSeconds);

    ChartSetString(target_chart, CHART_COMMENT, chartLabel);

    ChartSetInteger(target_chart, CHART_AUTOSCROLL, true);

    ChartNavigate(target_chart, CHART_END);



    HideNativeCandles(target_chart);

  }



  datetime now = TimeCurrent();

  datetime periodStart = now - (now % CandleSeconds);



  double price = SymbolInfoDouble(_Symbol, SYMBOL_BID);



  if(!periodActive || periodStart != currentPeriodStart)

  {

    if(periodActive)

      DrawCandle(target_chart, currentPeriodStart, openPrice, highPrice, lowPrice, closePrice);



    currentPeriodStart = periodStart;

    openPrice = highPrice = lowPrice = closePrice = price;

    periodActive = true;



    if(DebugPrints) PrintFormat("Neue Periode %s Open=%.*f", TimeToString(currentPeriodStart, TIME_SECONDS), _Digits, openPrice);

  }

  else

  {

    if(price > highPrice) highPrice = price;

    if(price < lowPrice)  lowPrice  = price;

    closePrice = price;

  }

}

//+------------------------------------------------------------------+

void DrawCandle(long chart_id, datetime t, double o, double h, double l, double c)

{

  string idBody = PREFIX_BODY + IntegerToString((int)t);

  string idWickUp = PREFIX_WICK_UP + IntegerToString((int)t);

  string idWickDown = PREFIX_WICK_DOWN + IntegerToString((int)t);



  bool created = ObjectCreate(chart_id, idBody, OBJ_RECTANGLE, 0, t, o, t + CandleSeconds, c);

  if(!created)

  {

    if(DebugPrints) PrintFormat("ObjectCreate Body failed (%s) Err=%d", idBody, GetLastError());

    ResetLastError();

  }

  else

  {

    color bodyColor = (c >= o) ? BullColor : BearColor;

    ObjectSetInteger(chart_id, idBody, OBJPROP_COLOR, bodyColor);

    ObjectSetInteger(chart_id, idBody, OBJPROP_BACK, false); // vorne sichtbar

    ObjectSetInteger(chart_id, idBody, OBJPROP_WIDTH, 1);

    ObjectSetInteger(chart_id, idBody, OBJPROP_SELECTABLE, false);

  }



  datetime wickTime = t + CandleSeconds/2;

  created = ObjectCreate(chart_id, idWickUp, OBJ_TREND, 0, wickTime, MathMax(o,c), wickTime, h);

  if(!created)

  {

    if(DebugPrints) PrintFormat("ObjectCreate WickUp failed (%s) Err=%d", idWickUp, GetLastError());

    ResetLastError();

  }

  else

  {

    ObjectSetInteger(chart_id, idWickUp, OBJPROP_COLOR, WickColor);

    ObjectSetInteger(chart_id, idWickUp, OBJPROP_WIDTH, 1);

    ObjectSetInteger(chart_id, idWickUp, OBJPROP_SELECTABLE, false);

  }



  created = ObjectCreate(chart_id, idWickDown, OBJ_TREND, 0, wickTime, MathMin(o,c), wickTime, l);

  if(!created)

  {

    if(DebugPrints) PrintFormat("ObjectCreate WickDown failed (%s) Err=%d", idWickDown, GetLastError());

    ResetLastError();

  }

  else

  {

    ObjectSetInteger(chart_id, idWickDown, OBJPROP_COLOR, WickColor);

    ObjectSetInteger(chart_id, idWickDown, OBJPROP_WIDTH, 1);

    ObjectSetInteger(chart_id, idWickDown, OBJPROP_SELECTABLE, false);

  }



  ChartRedraw(chart_id);

  CleanupOldObjects(chart_id, MaxCandles);

}

// Versucht, native Kerzen weniger sichtbar zu machen durch Wechsel in Linienmodus.

// Diese Methode ist robust gegenüber MT5‑Builds (vermeidet direkte Farbkonstanten).

void HideNativeCandles(long chart_id)

{

  ChartSetInteger(chart_id, CHART_MODE, 1); 

  bool ok = ChartSetInteger(chart_id, CHART_MODE, CHART_MODE_LINE);



  if(DebugPrints)

  {

    if(ok) PrintFormat("Chart %d: CHART_MODE set to LINE", chart_id);

    else PrintFormat("Chart %d: CHART_MODE set failed", chart_id);

  }



  ChartNavigate(chart_id, CHART_END);

  ChartRedraw(chart_id);

}

//+------------------------------------------------------------------+

void CleanupOldObjects(long chart_id, int maxCount)

{

  int total = ObjectsTotal(chart_id);

  if(total <= 0) return;



  string names[];

  ArrayResize(names, 0);

  int ownCount = 0;



  for(int i = 0; i < total; i++)

  {

    string nm = ObjectName(chart_id, i);

    if(nm == "") continue;

    if(StringFind(nm, PREFIX_BODY) == 0 || StringFind(nm, PREFIX_WICK_UP) == 0 || StringFind(nm, PREFIX_WICK_DOWN) == 0)

    {

      ArrayResize(names, ownCount + 1);

      names[ownCount] = nm;

      ownCount++;

    }

  }



  int keep = (maxCount <= 0) ? 0 : maxCount;



  while(ownCount > keep)

  {

    string toDelete = names[0];

    bool del = ObjectDelete(chart_id, toDelete);

    if(!del)

    {

      if(DebugPrints) PrintFormat("ObjectDelete failed (%s) Err=%d", toDelete, GetLastError());

      ResetLastError();

      break;

    }

    // shift array

    for(int j = 1; j < ownCount; j++) names[j-1] = names[j];

    ArrayResize(names, ownCount - 1);

    ownCount--;

  }

}


Discover new MetaTrader 5 opportunities with MQL5 community and services
Discover new MetaTrader 5 opportunities with MQL5 community and services
  • 2026.02.04
  • www.mql5.com
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions