Author: ������ ������ aka MikeZTN
m-Candles

This script is designed for the MetaTrader 4 platform to provide traders with a unique visual perspective on historical market data. Its primary function is to overlay "higher timeframe" candles onto a lower timeframe chart, allowing you to see the broader price action without constantly switching between different time periods.

How it Works

The logic operates by fetching data from a larger time interval (for example, 4-hour candles) and drawing them as distinct shapes on your current, smaller time interval chart (such as a 1-hour chart).

1. User Configuration When you load the script, you can define three main settings:

  • The Target Period: You choose which larger timeframe you want to see (e.g., Daily, 4-Hour, or 1-Hour).
  • The Quantity: You decide how many of these historical candles should be displayed at once.
  • Color Coding: You can set specific colors for "Up" candles (bullish) and "Down" candles (bearish) to make the trend easy to read at a glance.

2. Validation Logic The script includes built-in checks to ensure the data remains accurate:

  • It verifies that you have selected a standard timeframe.
  • It ensures that the timeframe you are trying to view is larger than the one you are currently looking at. If you try to display a "smaller" candle on a "larger" chart, the script will provide an on-screen warning.

3. Visual Construction For every historical candle requested, the script performs the following steps:

  • Data Retrieval: It identifies the exact opening time, closing time, high price, and low price for each candle of the larger timeframe.
  • Drawing the Body: It creates a solid rectangle to represent the "body" of the candle (the area between the open and close prices).
  • Drawing the Shadow: It calculates the midpoint of the candle's time duration and draws a thick vertical line to represent the "shadow" or "wick" (the high and low price points).
  • Dynamic Coloring: The script automatically checks if the price closed higher or lower than it opened. Based on this, it applies your chosen color for either an upward or downward movement.

Summary for the User

Instead of manually clicking through different tabs to see what the 4-hour trend looks like while you are trading on a 1-hour chart, this script draws those 4-hour candles directly onto your screen. It creates a multi-layered visual history that helps you identify the "big picture" and current market momentum simultaneously.

Price Data Components
Series array that contains open prices of each barSeries array that contains close prices for each barSeries array that contains the highest prices of each barSeries array that contains the lowest prices of each bar
7 Views
0 Downloads
0 Favorites
m-Candles
//+------------------------------------------------------------------+
//|                                                    M-Candles.mq4 |
//|         îðèãèíàëüíàÿ èäåÿ äëÿ H1 è âûøå - Êèì Èãîðü Â. aka KimIV |
//|                                              http://www.kimiv.ru |
//|            Ïåðåïèñàë äëÿ ñòàíäàðòíûõ òàéìôðåéìîâ - Ìèõàèë Æèòíåâ |
//|                                                    ICQ 138092006 |
//|         2008.09.05  Íà ëþáîì ãðàôèêå ïîêàçûâàåò ñâå÷è ñòàðøèõ ÒÔ |
//+------------------------------------------------------------------+
#property copyright "Æèòíåâ Ìèõàèë aka MikeZTN"
#property link      "ICQ 138092006"

#property indicator_chart_window

//------- Âíåøíèå ïàðàìåòðû ------------------------------------------
extern int TFBar       = 240;           // Ïåðèîä ñòàðøèõ ñâå÷åê
extern int NumberOfBar = 100;           // Êîëè÷åñòâî ñòàðøèõ ñâå÷åê
extern color ColorUp   = 0x003300;      // Öâåò âîñõîäÿùåé ñâå÷è
extern color ColorDown = 0x000033;      // Öâåò íèñõîäÿùåé ñâå÷è

//------- Ãëîáàëüíûå ïåðåìåííûå --------------------------------------

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
void init() {
  int i;

  for (i=0; i<NumberOfBar; i++) {
    ObjectDelete("BodyTF"+TFBar+"Bar"+i);
    ObjectDelete("ShadowTF"+TFBar+"Bar" + i);
  }
  for (i=0; i<NumberOfBar; i++) {
    ObjectCreate("BodyTF"+TFBar+"Bar"+i, OBJ_RECTANGLE, 0, 0,0, 0,0);
    ObjectCreate("ShadowTF"+TFBar+"Bar"+i, OBJ_TREND, 0, 0,0, 0,0);
  }
  Comment("");
}

//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
void deinit() {
  // Óäàëåíèå îáúåêòîâ
  for (int i=0; i<NumberOfBar; i++) {
    ObjectDelete("BodyTF"+TFBar+"Bar"+i);
    ObjectDelete("ShadowTF"+TFBar+"Bar" + i);
  }
  Comment("");
}

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start() {
  int shb=0, sh1=1, d;
  double   po, pc;       // Öåíû îòêðûòèÿ è çàêðûòèÿ ñòàðøèõ ñâå÷åê
  double   ph=0, pl=500; // Öåíû õàé è ëîó ñòàðøèõ ñâå÷åê
  datetime to, tc, ts;   // Âðåìÿ îòêðûòèÿ, çàêðûòèÿ è òåíåé ñòàðøèõ ñâå÷åê

  
  bool OK_Period=false;   
  switch (TFBar)
  {    
    case 1:OK_Period=true;break;
    case 5:OK_Period=true;break;
    case 15:OK_Period=true;break;
    case 30:OK_Period=true;break;
    case 60:OK_Period=true;break;
    case 240:OK_Period=true;break;
    case 1440:OK_Period=true;break;
    case 10080:OK_Period=true;break;
    case 43200:OK_Period=true;break;
  }
  if (OK_Period==false)
     {
       Comment("Âû ââåëè íåñòàíäàðòíóþ öèôðó òàéìôðåéìà TFBar! Íåîáõîäèìî ââåñòè îäíó èç ñëåäóþùèõ: 1,5,15,30,60,240,1440 è ò.ä.");   
       return(0);
     }
  if (Period()>TFBar) 
  {
    Comment("Çàäàâàåìûé ñòàíäàðòíûé ïåðèîä äîëæåí áûòü áîëüøå òåêóùåãî! (Òåêóùèé ðàâåí " + Period() + ")");
    return(0);
  }
    
    shb=0;
    // Áåæèì ïî ñòàðøèì ñâå÷êàì  
    while (shb<NumberOfBar) 
    {
      to = iTime(Symbol(), TFBar, shb);
      tc = iTime(Symbol(), TFBar, shb) + TFBar*60;
      po = iOpen(Symbol(), TFBar, shb);
      pc = iClose(Symbol(), TFBar, shb);
      ph = iHigh(Symbol(), TFBar, shb); 
      pl = iLow(Symbol(), TFBar, shb); 
      //óñòàíàâëèâàåì  ðåêòàíãåëû
      ObjectSet("BodyTF"+TFBar+"Bar"+shb, OBJPROP_TIME1, to);  //âðåìÿ îòêðûòèÿ
      ObjectSet("BodyTF"+TFBar+"Bar"+shb, OBJPROP_PRICE1, po); //öåíà îòêðûòèÿ
      ObjectSet("BodyTF"+TFBar+"Bar"+shb, OBJPROP_TIME2, tc);  //âðåìÿ çàêðûòèÿ
      ObjectSet("BodyTF"+TFBar+"Bar"+shb, OBJPROP_PRICE2, pc); //öåíà çàêðûòèÿ
      ObjectSet("BodyTF"+TFBar+"Bar"+shb, OBJPROP_STYLE, STYLE_SOLID);
      ObjectSet("BodyTF"+TFBar+"Bar"+shb, OBJPROP_BACK, True);
      //óñòàíàâëèâàåì òåíè
      ts = to + MathRound((TFBar*60)/2);
      ObjectSet("ShadowTF"+TFBar+"Bar"+shb, OBJPROP_TIME1, ts);
      ObjectSet("ShadowTF"+TFBar+"Bar"+shb, OBJPROP_PRICE1, ph);
      ObjectSet("ShadowTF"+TFBar+"Bar"+shb, OBJPROP_TIME2, ts);
      ObjectSet("ShadowTF"+TFBar+"Bar"+shb, OBJPROP_PRICE2, pl);
      ObjectSet("ShadowTF"+TFBar+"Bar"+shb, OBJPROP_STYLE, STYLE_SOLID);
      ObjectSet("ShadowTF"+TFBar+"Bar"+shb, OBJPROP_WIDTH, 3);
      ObjectSet("ShadowTF"+TFBar+"Bar"+shb, OBJPROP_BACK, True);
      ObjectSet("ShadowTF"+TFBar+"Bar"+shb, OBJPROP_RAY, False);            
      //óñòàíàâëèâàåì öâåòà äëÿ âñåõ îáúåêòîâ
      if (po<pc) {
          ObjectSet("BodyTF"+TFBar+"Bar"+shb, OBJPROP_COLOR, ColorUp);
          ObjectSet("ShadowTF"+TFBar+"Bar"+shb, OBJPROP_COLOR, ColorUp);
        } else {
          ObjectSet("BodyTF"+TFBar+"Bar"+shb, OBJPROP_COLOR, ColorDown);
          ObjectSet("ShadowTF"+TFBar+"Bar"+shb, OBJPROP_COLOR, ColorDown);
        }
      shb++;
     }       
      
  
  return(0);
}
//+------------------------------------------------------------------+

Comments