This script is designed to visualize "Darvas Boxes" on a trading chart. For those unfamiliar with the technical terms, this tool helps identify specific price ranges where the market appears to be consolidating or establishing a stable boundary before moving in a particular direction.
How the Logic Works
The primary goal of this script is to scan historical price data to identify significant highs and lows that form "ceiling" and "floor" boundaries. Here is a breakdown of how it processes information:
1. Establishing Boundaries The script works by scanning every price bar on your chart sequentially. It looks for two key pieces of information: the highest point (the top) and the lowest point (the bottom) reached during specific segments of time.
2. Tracking Price Movement (State Logic) Instead of simply drawing a box around every move, the script uses a "state-based" logic. This means it evaluates the price in different stages:
- It first establishes a starting point based on a recent high.
- It then monitors subsequent price action to see if the price is breaking out above that high or staying within the established range.
- If the price stays below the current "ceiling," the script begins looking for a stable "floor."
3. Dynamic Box Updating The logic is programmed to be reactive. If the market creates a new, higher peak than the one currently being tracked, the script automatically adjusts the top boundary of the box. Similarly, it monitors whether the bottom of the range is being breached or if a new, lower floor is being formed.
4. Visual Representation As the script moves through the history of the chart, it plots these calculated highs and lows as distinct lines. This allows you to see the "box" where price action has been contained.
Summary for the User
Think of this script as an automated way to draw boundaries on your chart. It filters out the "noise" of minor price fluctuations to show you the broader price zones. By identifying where the price is currently "trapped" or held between a ceiling and a floor, it helps traders visualize potential areas of support and resistance where the market might experience a breakout or a reversal.
//+------------------------------------------------------------------+
//| Darvas Boxes.mq4 |
//| Copyright © 2004, MetaQuotes Software Corp. |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2004, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net"
//----
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 RoyalBlue
#property indicator_color2 RoyalBlue
//----
double ind_buffer1[];
double ind_buffer2[];
//----
bool allow_buy;
bool allow_sell;
double price;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
SetIndexStyle(0,DRAW_LINE);
SetIndexDrawBegin(0,0);
SetIndexBuffer(0, ind_buffer1);
//
SetIndexStyle(1,DRAW_LINE);
SetIndexDrawBegin(1,2);
SetIndexBuffer(1, ind_buffer2);
//
allow_buy=true;
allow_sell=false;
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//---- TODO: add your code here
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int counted_bars=IndicatorCounted();
//---- TODO: add your code here
double box_top=0;
double box_bottom=0;
int state=1;
//----
for(int i=Bars - 1; i > 0; i--)
{
if (state==1)
{
box_top=High[i];
}
else if (state==2)
{
if (box_top > High[i])
{
}
else
{
box_top=High[i];
}
}
else if (state==3)
{
if (box_top > High[i])
{
box_bottom=Low[i];
}
else
{
box_top=High[i];
}
}
else if (state==4)
{
if (box_top > High[i])
{
if (box_bottom < Low[i])
{
}
else
{
box_bottom=Low[i];
}
}
else
{
box_top=High[i];
}
}
else if (state==5)
{
if (box_top > High[i])
{
if (box_bottom < Low[i])
{
}
else
{
box_bottom=Low[i];
}
}
else
{
box_top=High[i];
}
state=0;
}
ind_buffer1[i]=box_top;
ind_buffer2[i]=box_bottom;
state++;
}
ind_buffer1[0]=EMPTY_VALUE;
ind_buffer2[0]=EMPTY_VALUE;
//----
return(0);
}
//+------------------------------------------------------------------+----+
Comments