i-MorningRange

Download script
Author: E�? E?�?� A. aka KimIV
i-MorningRange

This script is designed for the MetaTrader trading platform to help traders visualize specific price action patterns based on a defined time range during the trading day. It essentially identifies and marks the highest and lowest prices reached between the start of the day (midnight) and a specific time that you choose.

Core Logic Overview

The primary function of this tool is to "box in" the price movement from the very beginning of the daily session up until a significant morning cutoff point. By identifying these extremes, traders can better understand the initial volatility or the range established during the early hours of the market.

Key Features and Functionality

  • Customizable Time Window: You can set a specific "Check Time" (for example, 08:00). The script automatically looks back from that time to the start of the day (00:00) and identifies the highest peak and the lowest valley hit during that period.
  • Real-Time Horizon Lines: The tool places horizontal lines on your current chart at these two extreme price points. This allows you to see instantly where the "morning range" sits relative to the current market price.
  • Historical Data Visualization: Beyond just showing today's data, the script can look backward in time. You can configure it to display these morning ranges for a specific number of previous days. It draws horizontal segments (trend lines) that visually represent where those price extremes were on past dates.
  • Automated Calendar Logic: The script includes internal logic to handle calendar complexities, such as different month lengths and leap years, ensuring that when it looks back at history, it accurately identifies previous trading days without manual adjustment.
  • Visual Customization: The tool provides distinct colors for the high and low markers (Blue and Orange), making it easy to distinguish between the upper and lower boundaries of the morning range at a glance.

Practical Use Case

For a trader, this script serves as a visual anchor. Instead of manually scanning the charts to find out how high or low the price moved during the early session, the script does this automatically. It highlights the "boundaries" established in the morning, helping traders decide if the current price is breaking out of that initial range or if it is staying contained within it.

10 Views
0 Downloads
0 Favorites
i-MorningRange
//+------------------------------------------------------------------+
//|                                               i-MorningRange.mq4 |
//|                                           Eè? E?î?ü A. aka KimIV |
//|                                              http://www.kimiv.ru |
//|                                                                  |
//|  08.02.2006  E??èêà?î? ????????î ?èàïàçî?à.                      |
//+------------------------------------------------------------------+
#property copyright "Eè? E?î?ü A. aka KimIV"
#property link      "http://www.kimiv.ru"

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Blue
#property indicator_color2 Orange

//------- A????è? ïà?à????û è??èêà?î?à -------------------------------
extern string CheckTime    = "08:00";  // A???? ï?îâ??êè ?èàïàçî?à
extern bool   ShowHistory  = True;     // Iîêàçûâà?ü è??î?è÷??êè? ??îâ?è
extern int    NumberOfDays = 5;        // Eîëè÷???âî ???é è??î?èè
extern bool   ShowComment  = True;     // Iîêàçûâà?ü êî?????à?èè

//------- ??ô??û è??èêà?î?à ------------------------------------------
double dBuf0[], dBuf1[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
void init() {
  SetIndexArrow (0, 217);
  SetIndexBuffer(0, dBuf0);
  SetIndexStyle (0, DRAW_ARROW, 1, 1);
  SetIndexArrow (1, 218);
  SetIndexBuffer(1, dBuf1);
  SetIndexStyle (1, DRAW_ARROW, 1, 1);

  DeleteLines();
  for (int i=0; i<2; i++) {
    ObjectCreate("HLine"+i, OBJ_HLINE, 0, 0,0);
  }

  for (i=0; i<NumberOfDays; i++) {
    CreateLines("upLine"+i, indicator_color1);
    CreateLines("dnLine"+i, indicator_color2);
  }
}

//+------------------------------------------------------------------+
//| ?îç?à?è? î???ê?îâ Eè?è? ï?î?ëû? ???é                             |
//| Ià?à????û:                                                       |
//|   no - ?àè???îâà?è? ëè?èè                                        |
//|   cl - ?â?? ëè?èè                                                |
//+------------------------------------------------------------------+
void CreateLines(string no, color cl) {
  ObjectCreate(no, OBJ_TREND, 0, 0,0, 0,0);
  ObjectSet(no, OBJPROP_STYLE, STYLE_SOLID);
  ObjectSet(no, OBJPROP_WIDTH, 1);
  ObjectSet(no, OBJPROP_COLOR, cl);
  ObjectSet(no, OBJPROP_RAY, False);
}

//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
void deinit() {
  DeleteLines();
  Comment("");
}

//+------------------------------------------------------------------+
//| ??àë??è? ?î?èçî??àëü?û? ëè?èé.                                   |
//+------------------------------------------------------------------+
void DeleteLines() {
  for (int i=0; i<2; i++) {
    ObjectDelete("HLine"+i);
  }

  for (i=0; i<NumberOfDays; i++) {
    ObjectDelete("upLine"+i);
    ObjectDelete("dnLine"+i);
  }
}

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
void start() {
  datetime t1, t2, dt;
  double   p1, p2;
  int      b1, b2, sd=0;

  t1=StrToTime(TimeToStr(CurTime(), TIME_DATE)+" 00:00");
  t2=StrToTime(TimeToStr(CurTime(), TIME_DATE)+" "+CheckTime);
  b1=iBarShift(NULL, 0, t1);
  b2=iBarShift(NULL, 0, t2);
  p1=High[Highest(NULL, 0, MODE_HIGH, b1-b2+1, b2)];
  p2=Low [Lowest (NULL, 0, MODE_LOW , b1-b2+1, b2)];

  SetHLine(0, p1);
  SetHLine(1, p2);

  if (ShowHistory) {
    dt=decDateTradeDay(CurTime());
    for (int i=0; i<NumberOfDays; i++) {
      DrawLines(dt, i);
      dt=decDateTradeDay(dt);
      while (TimeDayOfWeek(dt)<1 || TimeDayOfWeek(dt)>5) dt=decDateTradeDay(dt);
    }
  }

  if (ShowComment) Comment("CheckTime="+CheckTime);
}

//+------------------------------------------------------------------+
//| ???à?àâëèâà?? ??êâèçè?û ?î?èçî??àëü?îé ëè?èè ??ê?ù??î ???.       |
//+------------------------------------------------------------------+
void SetHLine(int nl, double pp) {
  color cl;

  if (pp!=EMPTY_VALUE) {
    switch (nl) {
      case 0: cl=indicator_color1; break;
      case 1: cl=indicator_color2; break;
    }
    ObjectSet("HLine"+nl, OBJPROP_PRICE1, pp);
    ObjectSet("HLine"+nl, OBJPROP_COLOR , cl);
  }
}

//+------------------------------------------------------------------+
//| I?î?è?îâêà ëè?èé ?à ??àôèê?                                      |
//| Ià?à????û:                                                       |
//|   dt - ?à?à ?î??îâî?î ???                                        |
//|   nd - ?î??? ??? (?ë? ?????à?èè î???ê?îâ)                        |
//+------------------------------------------------------------------+
void DrawLines(datetime dt, int nd) {
  datetime t1, t2;
  double   p1, p2;
  int      b1, b2;

  t1=StrToTime(TimeToStr(dt, TIME_DATE)+" 00:00");
  t2=StrToTime(TimeToStr(dt, TIME_DATE)+" "+CheckTime);
  b1=iBarShift(NULL, 0, t1);
  b2=iBarShift(NULL, 0, t2);
  p1=High[Highest(NULL, 0, MODE_HIGH, b1-b2+1, b2)];
  p2=Low [Lowest (NULL, 0, MODE_LOW , b1-b2+1, b2)];

  ObjectSet("upLine"+nd, OBJPROP_TIME1 , t1);
  ObjectSet("upLine"+nd, OBJPROP_PRICE1, p1);
  ObjectSet("upLine"+nd, OBJPROP_TIME2 , t2);
  ObjectSet("upLine"+nd, OBJPROP_PRICE2, p1);

  ObjectSet("dnLine"+nd, OBJPROP_TIME1 , t1);
  ObjectSet("dnLine"+nd, OBJPROP_PRICE1, p2);
  ObjectSet("dnLine"+nd, OBJPROP_TIME2 , t2);
  ObjectSet("dnLine"+nd, OBJPROP_PRICE2, p2);
}

//+------------------------------------------------------------------+
//| ????ü???è? ?à?û ?à î?è? ?î??îâûé ???ü                            |
//| Ià?à????û:                                                       |
//|   dt - ?à?à ?î??îâî?î ???                                        |
//+------------------------------------------------------------------+
datetime decDateTradeDay(datetime dt) {
  int ty=TimeYear(dt);
  int tm=TimeMonth(dt);
  int td=TimeDay(dt);
  int th=TimeHour(dt);
  int ti=TimeMinute(dt);

  td--;
  if (td==0) {
    tm--;
    if (tm==0) {
      ty--;
      tm=12;
    }
    if (tm==1 || tm==3 || tm==5 || tm==7 || tm==8 || tm==10 || tm==12) td=31;
    if (tm==2) if (MathMod(ty, 4)==0) td=29; else td=28;
    if (tm==4 || tm==6 || tm==9 || tm==11) td=30;
  }
  return(StrToTime(ty+"."+tm+"."+td+" "+th+":"+ti));
}
//+------------------------------------------------------------------+

Comments