X0diagram1_v2

Download script
Author: Copyright � 2005, 4e
X0diagram1_v2

This script is designed as a visual analysis tool for MetaTrader 4, specifically intended to map out price movements by identifying and plotting structural "reversals" in the market. Instead of standard trend lines, it uses a geometric logic to represent price action as a series of steps or "boxes."

Core Logic and Functionality

1. Movement Identification The script scans historical price data (starting from a user-defined number of previous candles) to determine the direction of the market. It identifies whether the price is trending upward or downward based on the relationship between opening and closing prices.

2. Reversal Detection The logic operates by looking for significant "reversals." A reversal is triggered when the price moves away from its current high or low point by a specific distance (defined by the user as a multiple of "boxes").

  • If the market is trending up, the script looks for a drop large enough to qualify as a downward reversal.
  • If the market is trending down, the script looks for a rise large enough to qualify as an upward reversal.

3. Geometric Visualization (The "Box" System) Rather than just drawing a line, the script calculates the magnitude of each movement. It determines how many "boxes" tall or deep a move is based on the price difference between the starting point and the reversal point.

  • Upward Moves: These are visualized using one specific set of visual bars (histograms) that represent the height gained.
  • Downward Moves: These are visualized using a different set of visual bars to distinguish them from upward movements.
  • Spacing: The script automatically calculates "empty spaces" or gaps between these figures to ensure the diagram remains proportional and readable on the chart.

4. Dynamic Calculation The tool is designed to be dynamic. Every time a reversal is detected, the script "shifts" its starting point to that new peak or valley. It then continues to monitor the market from that new position until the next significant reversal occurs. This allows the diagram to build itself chronologically as new price data arrives.

User Configuration Summary

The logic relies on three main user-defined variables to customize the look and behavior:

  • BoxSize: Defines the unit of measurement for the price height/depth.
  • ReversalBox: Determines how much "movement" is required before the script recognizes a change in direction (the sensitivity of the tool).
  • Candles: Sets the historical depth the script will look back at to build the initial diagram.

In summary, this script acts as a structural mapper that simplifies complex price fluctuations into a clean, stepped geometric diagram, highlighting significant trend changes based on user-defined price thresholds.

Miscellaneous
Implements a curve of type %1
15 Views
0 Downloads
0 Favorites
X0diagram1_v2
//+------------------------------------------------------------------+
//|                                                    X0diagram.mq4 |
//|                                             Copyright © 2005, 4e |
//|                                                                  |
//+------------------------------------------------------------------+

///////////////////////////////////////////////////////////////////////
//                            Äèàãðàììà Õ0
// Ðèñóåò äèàãðàììû Õ0 ïî öåíàì çàêðûòèÿ
// Âõîäíûå ïàðàìåòðû: BoxSize - ðàçìåð áîêñà â ïóíêòàõ
//                    ReversalBox - êîë-âî áîêñîâ äëÿ ðàçâîðîòà
//                    Candles = êîë-âî ñâå÷åé äëÿ ïîäñ÷åòà  
//                    
//  Èçìåíèòå ïàðàìåòð indicator_color3 öâåòîì âàøåé öâåòîâîé ñõåìû 
///////////////////////////////////////////////////////////////////////





#property copyright "Copyright © 2005, 4e"
#property link      "4ebur@mail.ru"

#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1 Red
#property indicator_color2 Blue
#property indicator_color3 White



extern double BoxSize = 3;
extern int ReversalBox = 2;
extern int Candles = 300;



double PointArray[];
double FigureArray[];
double ZeroArray[];



//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
SetIndexBuffer(0,PointArray);
SetIndexBuffer(1,FigureArray);
SetIndexBuffer(2,ZeroArray);


SetIndexStyle(0,DRAW_HISTOGRAM);
SetIndexStyle(1,DRAW_HISTOGRAM);
SetIndexStyle(2,DRAW_HISTOGRAM);

SetIndexEmptyValue(0,0);
SetIndexEmptyValue(1,0);
SetIndexEmptyValue(2,0);

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
     int    limit;
   int    counted_bars=IndicatorCounted();
 
//---- last counted bar will be recounted
   //if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars+Candles;
   
//----

double BoxPointSize = BoxSize * Point; //Ðàçìåð áîêñà â ïóíòàõ
double ReversalSize = BoxPointSize * ReversalBox; // Ðàçìåð ðàçâîðîòà â ïóíêòàõ
  
  
  
  
  
  // Èíèöèàëèçàöèÿ ìàññèâîâ
  for(int i=limit;i>0;i--)
    {
      PointArray[i] = 0;
      FigureArray[i] = 0;
      ZeroArray[i] = 0;
    
    
    }
    
    
  int  beginPoint; // Òî÷êà îòñ÷åòà
  int  CurPoint; //Òåêóùàÿ òî÷êà
  bool Direction; // 1  - ââåðõ, òèïà Õ, 0-âíèç, òèïà 0
  int LocalÌin; // Ëîêàëüíûé ìèíèìóì
  int LocalMax; // Ëîêàëüíûé ìàêñèìóì
  double beginIndex; // èíäåõ íà÷àëà îòñ÷åòà
  
  // Àëãîðèòì ðàñ÷åòà òàêîâ: Êàê òîëüêî ïðîèçîøåë ðàçâîðîò, ðèñóåì ïðîøëóþ äèàãðàììó, è æäåì ñëåäóþùåãî ðàçâîðîòà
  // Íà÷àëüíîå íàïðàâëåíèå èäåíòèôèöèðóåòñÿ ïî ïåðâîé ñâå÷å(áåëàÿ - çíà÷èð ââåðõ, ÷åðíàÿ - âíèç)

  
  Direction = (Close[Candles]-Open[Candles]) > 0;// Çàäàåì íà÷àëüíîå íàïðàâëåíèå
  beginPoint = Candles; // Íà÷àëüíàÿ òî÷êà îòñ÷åòà
  CurPoint = Candles;   // Íà÷àëüíàÿ òåêóùàÿ òî÷êà
  LocalMax = Candles; // Íà÷àëüíûé ìàêñèìóì
  LocalÌin = Candles; // Íà÷àëüíûé ìèíèìóì
  beginIndex = Close[beginPoint]; // Íà÷àëüíûé èíäåêñ
   
   
   
   for( i=Candles-1;i>0;i--)
    {
      CurPoint = i; // Ñäâèã òåêóùåé òî÷êè
      LocalMax = Highest(NULL,0,MODE_CLOSE,beginPoint-CurPoint,CurPoint);
      LocalÌin = Lowest(NULL,0,MODE_CLOSE,beginPoint-CurPoint,CurPoint);
      
         if(Direction) // Ââåðõ
            {
               if(Close[LocalMax]-Close[CurPoint] >= ReversalSize) // Ðàçâîðîò âíèç
               {
                    int boxes  = MathFloor(((Close[LocalMax]-Close[beginPoint])/(BoxSize*Point))); // âû÷èñëÿåì êîë-âî áîêñîâ
                     
                     for(int j=beginPoint;j>LocalMax-1;j--)
                     {
                        PointArray[j] = beginIndex + BoxSize*boxes*Point; //Ðèñóåì èêñû
                        ZeroArray[j] = beginIndex;  // Ïðîáåëû
                     
                     }
                     
                   beginPoint = LocalMax-1; // Ñäâèãàåì íà÷àëüíóþ òî÷êó íà áûâøèé ìàêñèìóì
                   beginIndex = beginIndex + BoxSize*(boxes-1)*Point; // Ñäâèãàåì èíäåêñ íà îäèí áîêñ âíèç
                   LocalÌin = CurPoint;
                   Direction = 0; // Óêàçûâàåì ÷òî íà÷àëîñü äâèæåíèå âíèç  
               }
            }
            
         
          else  // Âíèç
            {
               if(Close[CurPoint]-Close[LocalÌin] >=ReversalSize) // Ðàçâîðîò ââåðõ
               {
                     
                      boxes  = MathFloor(((Close[beginPoint]-Close[LocalÌin])/(BoxSize*Point)));// âû÷èñëÿåì êîë-âî áîêñîâ
                     
                     for( j=beginPoint;j>LocalÌin-1;j--)
                     {
                        FigureArray[j] = beginIndex; //Ðèñóåì íóëè
                        ZeroArray[j] = beginIndex-BoxSize*boxes*Point;   // Ïðîáåëû
                     
                     }
                     
                   beginPoint = LocalÌin-1; // Ñäâèãàåì íà÷àëüíóþ òî÷êó íà áûâøèé ìàêñèìóì
                   Direction = 1; // Óêàçûâàåì ÷òî íà÷àëîñü äâèæåíèå âíèç  
                   LocalMax = CurPoint; 
                   beginIndex = beginIndex - BoxSize*(boxes-1)*Point; // Ñäâèãàåì èíäåêñ íà îäèí áîêñ ââåðõ
                     
                     
               }
            }
    }
   
   
   
   
   // Ðèñóåì òåêóùèé ðàçâîðîò
  
  
   
   
   if(Direction)
   {
         LocalMax = Highest(NULL,0,MODE_CLOSE,beginPoint-CurPoint,CurPoint);
         boxes  = MathFloor(((Close[LocalMax]-Close[beginPoint])/(BoxSize*Point))); // âû÷èñëÿåì êîë-âî áîêñîâ
         
         for(j=beginPoint;j>0;j--)
                     {
                        PointArray[j] = beginIndex + BoxSize*boxes*Point; //Ðèñóåì èêñû
                        ZeroArray[j] = beginIndex;  // Ïðîáåëû
                     
                     }
      
   
   }
   
   else
   {
       LocalÌin = Lowest(NULL,0,MODE_CLOSE,beginPoint-CurPoint,CurPoint);
      boxes  = MathFloor(((Close[beginPoint]-Close[LocalÌin])/(BoxSize*Point)));// âû÷èñëÿåì êîë-âî áîêñîâ
   
      for(j=beginPoint;j>0;j--)
                     {
                        FigureArray[j] =  beginIndex;//Ðèñóåì íóëè
                        ZeroArray[j] = beginIndex - BoxSize*boxes*Point;  // Ïðîáåëû
                     
                     }
   
   
   }
   
   
//----
   return(0);
  }
//+------------------------------------------------------------------+

Comments