How do I make a New Chart WIndow That shows a new Candle every 10 Ticks ?

 

I was trying but It doesnt work, I really do not know how to make it, please help.

The Base Code:

//+------------------------------------------------------------------+
//|                                             10TicksEveryCandle.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    CandleTicks    = 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;
int      tickCount = 0;

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_%dticks", sym, CandleTicks);
  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("10TicksCandleGenerator_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_%dticks", _Symbol, CandleTicks);
    ChartSetString(target_chart, CHART_COMMENT, chartLabel);
    ChartSetInteger(target_chart, CHART_AUTOSCROLL, true);
    ChartNavigate(target_chart, CHART_END);

    HideNativeCandles(target_chart);
  }

  datetime now = TimeCurrent();
  double price = SymbolInfoDouble(_Symbol, SYMBOL_BID);

  if(!periodActive)
  {
    currentPeriodStart = now;
    openPrice = highPrice = lowPrice = closePrice = price;
    tickCount = 1;
    periodActive = true;

    if(DebugPrints) PrintFormat("Neue Tick-Periode gestartet %s Open=%.*f Tick=%d", TimeToString(currentPeriodStart, TIME_SECONDS), _Digits, openPrice, tickCount);
    return;
  }

  if(price > highPrice) highPrice = price;
  if(price < lowPrice)  lowPrice  = price;
  closePrice = price;
  tickCount++;

  if(tickCount >= CandleTicks)
  {
    DrawCandle(target_chart, currentPeriodStart, openPrice, highPrice, lowPrice, closePrice);

    currentPeriodStart = now;
    openPrice = highPrice = lowPrice = closePrice = price;
    tickCount = 0;
    periodActive = true;

    if(DebugPrints) PrintFormat("Periode abgeschlossen. Neue Periode %s Open=%.*f", TimeToString(currentPeriodStart, TIME_SECONDS), _Digits, openPrice);
  }
}
//+------------------------------------------------------------------+
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);

  datetime t2 = t + 1;

  bool created = ObjectCreate(chart_id, idBody, OBJ_RECTANGLE, 0, t, o, t2, 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;

  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);
}

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.05
  • 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
 

Your code doesn't even compile :

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

It draws your "candle" one beside the other because the time is not good :

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

Anyway, on a normal M1 chart it will be difficult to have it drawing candles correctly. You should better use a custom symbol (with 'fake' M1 candles you will create yourself).