SessionsFlat

Download script
Author: Scriptong
SessionsFlat

This script is designed to identify and visualize specific price boundaries during three major global trading sessions: the Asian, European, and American sessions. By automatically detecting these timeframes, the tool helps traders see the "highs" and "lows" established during each period, allowing for a clearer understanding of market structure throughout the day.

Core Functionality

The logic operates by dividing the 24-hour trading day into three distinct windows. For each session, the script identifies the highest and lowest price points reached between the designated start and end times. These values are then projected onto your chart as visual markers (arrows).

Key Features and Logic Flow:

  • Session Segmentation: The tool is configured to monitor three specific periods:
    • Asian Session
    • European Session
    • American Session
  • Automatic Boundary Detection: Instead of just marking a single point in time, the script scans the historical price data for each session. It finds the absolute highest price (ceiling) and the lowest price (floor) reached during that specific window of time.
  • Visual Feedback: The results are displayed on the chart using different colors to distinguish between sessions:
    • Yellow markers represent the Asian Session boundaries.
    • Blue markers represent the European Session boundaries.
    • Red markers represent the American Session boundaries.
  • Smart Validation: To ensure accuracy and prevent visual clutter, the script includes a validation layer. It checks to make sure the user-defined session times do not overlap. If overlapping times are detected (which would cause conflicting data), the script will alert the user and disable itself to maintain data integrity.
  • Timezone Awareness: The logic is built to calculate the start of each day correctly, ensuring that the boundaries are anchored to the appropriate daily candles regardless of where the historical data sits on the chart.

User Experience

When you load the script, it performs an initial check to ensure your chart is set to a compatible timeframe (it is designed for use on Hourly charts or lower). It then briefly synchronizes with the history of the current day. Once active, it automatically populates the chart with the boundaries, providing a professional "map" of where each global market's price action peaked and bottomed out.

Miscellaneous
Implements a curve of type %1
14 Views
0 Downloads
0 Favorites
SessionsFlat
//+------------------------------------------------------------------+
//|                                                 SessionsFlat.mq4 |
//|                                                        Scriptong |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Scriptong"
#property link      ""

#property indicator_chart_window
#property indicator_buffers 6
#property indicator_color1 Yellow
#property indicator_color2 Yellow
#property indicator_color3 Blue
#property indicator_color4 Blue
#property indicator_color5 Red
#property indicator_color6 Red

extern int AzianFlatStart = 0;
extern int AzianFlatEnd = 8;
extern int EuropeanFlatStart = 8;
extern int EuropeanFlatEnd = 16;
extern int AmericanFlatStart = 16;
extern int AmericanFlatEnd = 24;
extern double TargetLevel = 161.8;
extern color UpColor = Turquoise;
extern color DnColor = Red;
extern color TargetUpColor = Gold;
extern color TargetDnColor = Silver;

//---- buffers
double Up1[], Up2[], Up3[];
double Dn1[], Dn2[], Dn3[];
double TargetUp[];
double TargetDn[];
bool Activate = False;
int Start[3], Finish[3];
//+-------------------------------------------------------------------------------------+
//| Custom indicator initialization function                                            |
//+-------------------------------------------------------------------------------------+
int init()
  {
// - 1 - === Ïðîâåðêà ïðàâèëüíîñòè ïåðèîäà ãðàôèêà ======================================
   if(Period() > PERIOD_H1) 
     {
      Comment("Èíäèêàòîð ðàáîòàåò íà òàéìôðåéìàõ ìåíüøå H4!");
      return(0);   
     }
// - 1 - === Îêîí÷àíèå áëîêà ============================================================
   
// - 2 - == Îáíîâëåíèå äàííûõ äíåâíîãî ÒÔ ===============================================   
   Comment("Ïîäîæäèòå, èäåò îáíîâëåíèå äàííûõ äíåâíîãî òàéìôðåéìà...");
   int P = 0;
   while (P < 120)
     {
      iTime(Symbol(), PERIOD_D1, 0);
      if (GetLastError() != 4066) break;
      Sleep(1000);
      P++;
     }
   if (P == 120)
     {
      Comment("Îáíîâëåíèå äàííûõ çàâåðøèëîñü ñ îøèáêîé. Ñîâåòíèê îòêëþ÷åí!");
      Print("Îáíîâëåíèå äàííûõ çàâåðøèëîñü ñ îøèáêîé. Ñîâåòíèê îòêëþ÷åí!");
      return(0);
     }  
   Comment("");  
// - 2 - == Îêîí÷àíèå áëîêà =============================================================

// - 3 - === Ïåðåêîìïîíîâêà âðåìåíè ñåññèé â ìàññèâû è ïðîâåðêà êîððåêòíîñòè äàííûõ =====
   if (!ToArray(0, AzianFlatStart, AzianFlatEnd) || 
       !ToArray(1, EuropeanFlatStart, EuropeanFlatEnd) ||
       !ToArray(2, AmericanFlatStart, AmericanFlatEnd))
     return(0);  
// - 3 - === Îêîí÷àíèå áëîêà ============================================================
     
// - 4 - === Èíèöèàëèçàöèÿ áóôåðîâ èíäèêàòîðà ===========================================
   SetIndexStyle(0, DRAW_ARROW);                                      // Àçèàòñêàÿ ñåññèÿ
   SetIndexBuffer(0, Up1);
   SetIndexArrow(0, 108);                                      
   SetIndexStyle(1, DRAW_ARROW);
   SetIndexBuffer(1, Dn1);
   SetIndexArrow(1, 108);                                      
   SetIndexStyle(2, DRAW_ARROW);                                    // Åâðîïåéñêàÿ ñåññèÿ
   SetIndexBuffer(2, Up2);
   SetIndexArrow(2, 108);                                      
   SetIndexStyle(3, DRAW_ARROW);
   SetIndexBuffer(3, Dn2);
   SetIndexArrow(3, 108);                                      
   SetIndexStyle(4, DRAW_ARROW);                                   // Àìåðèêàíñêàÿ ñåññèÿ
   SetIndexArrow(4, 108);                                      
   SetIndexBuffer(4, Up3);
   SetIndexStyle(5, DRAW_ARROW);
   SetIndexBuffer(5, Dn3);
   SetIndexArrow(5, 108);                                      
   SetIndexEmptyValue(0, 0.0);
   SetIndexEmptyValue(1, 0.0);
   SetIndexEmptyValue(2, 0.0);
   SetIndexEmptyValue(3, 0.0);
   SetIndexEmptyValue(4, 0.0);
   SetIndexEmptyValue(5, 0.0);
// - 4 - === Îêîí÷àíèå áëîêà ============================================================
   

   Activate = True;                                       // èíèöèàëèçàöèÿ ïðîøëà óñïåøíî

   return(0);
  }
  
//+------------------------------------------------------------------+
//| Ïåðåêîìïîíîâêà âõîäíûõ ïàðàìåòðîâ â ìàññèâ                       |
//+------------------------------------------------------------------+
bool ToArray(int Num, int St, int End)
{
 if (St < 0 || End < 0 || St > 24 || End > 24 || St >= End)
     {
      Comment("Çíà÷åíèÿ âðåìåíè Start è End äîëæíû ëåæàòü â äèàïàçîíå îò 0 äî 24 è Start < End.");
      Print("Çíà÷åíèÿ âðåìåíè Start è End äîëæíû ëåæàòü â äèàïàçîíå îò 0 äî 24 è Start < End.");
      return(False);
     } 
 int i = Num-1;    
 while (i >= 0)
   {
    if (Start[i] >= St || Finish[i] >= End || Finish[i] > St)
      {
       Comment("Îáíàðóæåíî ïåðåñå÷åíèå ñåññèé. Èíäèêàòîð ðàáîòàòü íå áóäåò.");
       Print("Îáíàðóæåíî ïåðåñå÷åíèå ñåññèé. Èíäèêàòîð ðàáîòàòü íå áóäåò.");
       return(False);
      }
    i--;
   }    
   
 Start[Num] = St;
 Finish[Num] = End;
 return(True);  
}
  
  
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   // Óäàëåíèå  îáúåêòîâ-öåíîâûõ ìåòîê
   for (int i = ObjectsTotal()-1; i >= 0; i--)
     { 
      string Name = ObjectName(i);
      if (StringSubstr(Name, 0, 3) == "Lab")
        ObjectDelete(Name);
     }
   Comment("");
//----
   return(0);
  }
  
//+-------------------------------------------------------------------------------------+
//| Custom indicator iteration function                                                 |
//+-------------------------------------------------------------------------------------+
int start()
  {
//----
   if(!Activate) return(0);

   int counted_bars=IndicatorCounted();
   if (counted_bars>0) counted_bars--;
   int i = iBarShift(Symbol(), 0, iTime(Symbol(), PERIOD_D1, 
                         iBarShift(Symbol(), PERIOD_D1, Time[Bars-counted_bars])));

   while (i >= 0)
     {
      Up1[i] = 0.0;
      Up2[i] = 0.0;
      Up3[i] = 0.0;
      Dn1[i] = 0.0;
      Dn2[i] = 0.0;
      Dn3[i] = 0.0;
      int DayBar = iBarShift(Symbol(), PERIOD_D1, Time[i]);           // Íîìåð áàðà íà D1

      int k = 0;
      for (k = 0; k < 3; k++)
        if (TimeHour(Time[i]) >= Start[k] && TimeHour(Time[i]) < Finish[k])
          {
           if (k == 0)
             {
              DayBar++;
              int j = 2;
             } 
            else
             j = k - 1; 
           datetime BeginDay = iTime(Symbol(), PERIOD_D1, DayBar);    // Ðàñ÷åò âðåìåíè íà÷àëà ñóòîê
           if (!(TimeDayOfWeek(BeginDay) == 5 && k == 0))
             {
              int StartBar = iBarShift(Symbol(), 0, BeginDay+Start[j]*3600);   // Áàð, ñîîòâåòñâóþùèé íà÷àëó ñóòîê ïëþñ ñìåùåíèå â ÷àñàõ
              int FinishBar = iBarShift(Symbol(), 0, BeginDay+Finish[j]*3600)+1; // Áàð, ñîîòâåòñòâóþùèé ïîñëåäíåìó áàðó ïðåäûäóùåé ñåññèè
              double LowV = Low[iLowest(Symbol(), 0, MODE_LOW, StartBar-FinishBar+1, FinishBar)];  // Íèæíÿÿ ãðàíèöà
              double HighV = High[iHighest(Symbol(), 0, MODE_HIGH, StartBar-FinishBar+1, FinishBar)]; // Âåðõíÿÿ ãðàíèöà
             }
            else
             {
              LowV = 0;
              HighV = 0;
             }  
           break;
          } 
          
      while (TimeHour(Time[i]) >= Start[k] && TimeHour(Time[i]) < Finish[k] && i >= 0)
        {
         switch (k)
           {
            case 0:
              Up1[i] = HighV;
              Dn1[i] = LowV;
            break;  
            case 1:
              Up2[i] = HighV;
              Dn2[i] = LowV;
            break;  
            case 2:
              Up3[i] = HighV;
              Dn3[i] = LowV;
            break;  
           }
         i--;  
        }    
     }  

//----
   return(0);
  }
//+------------------------------------------------------------------+

Comments