This script is designed to automate the visual identification of specific price action zones and Fibonacci retracement levels on a trading chart. It focuses on capturing market behavior during a predefined time window to help traders identify potential entry points and profit targets.
Core Logic and Functionality
1. Time-Based Price Monitoring The tool operates by looking at a specific "establishment window" each day (for example, between 04:00 and 07:15). Rather than analyzing every random price movement, it isolates this specific period to identify significant price swings that occur during those hours.
2. Automated Range Detection During the designated time window, the script automatically scans all price data to find the highest point (peak) and the lowest point (valley). By identifying these two extremes, it defines a "price box." This allows a trader to see the total range of movement for that specific session at a glance.
3. Visual Mapping (Boxes) Once the high and low points are found, the script draws a physical rectangle on your chart. This highlights the "trading area" for that day, making it easy to visualize where the most intense price action occurred during the establishment window.
4. Fibonacci Level Projection The script goes beyond just drawing a box; it automatically projects a series of Fibonacci levels based on the range discovered in the previous step. It calculates several key technical zones, including:
- Retracement Levels: Identifying where the price might "bounce" or pull back.
- Extensions: Projecting potential future price targets.
5. Custom Trading Labels To make the data actionable for a human trader, the script attaches specific descriptions to each Fibonacci level. Instead of just seeing numbers (like 1.618), it labels them with trading-specific terms such as:
- Entry points
- Initial Stop Losses
- Take Profit targets (TP1, TP2, TP3, and TP4)
6. Multi-Day History The script is capable of looking back over multiple days. It repeats this logic for each day in the history sequence, creating a series of boxes and Fibonacci sets on the chart so you can analyze historical patterns.
7. Automatic Cleanup To keep the trading workspace clean and organized, the script includes a self-cleaning mechanism. It identifies its own drawn objects (the boxes and Fibos) and ensures that when the indicator is removed or reset, these elements are cleared from the chart so they don't clutter your view.
//+------------------------------------------------------------------+
//| BoxFibo.mq4 |
//+------------------------------------------------------------------+
#property indicator_chart_window
extern string StartTime = "04:00"; // time for start of price establishment window
extern string EndTime = "07:15"; // time for end of price establishment window
extern int NumDays = 2;
extern color BoxColor = LightGray;
extern color FiboColor = Black;
//+------------------------------------------------------------------+
int init() {
//+------------------------------------------------------------------+
del_obj();
return(0);
}
//+------------------------------------------------------------------+
int deinit() {
//+------------------------------------------------------------------+
del_obj();
return(0);
}
//+------------------------------------------------------------------+
void start() {
//+------------------------------------------------------------------+
datetime dt1 = StrToTime(TimeToStr(TimeCurrent(),TIME_DATE) + " " + StartTime + ":00");
datetime dt2 = StrToTime(TimeToStr(TimeCurrent(),TIME_DATE) + " " + EndTime + ":00");
int dys = 0, NumBars = (dt2-dt1)/60/Period();
for (int i=0; i< NumDays * 96 /*Bars-NumBars */; i++)
{
dt1 = StrToTime(TimeToStr(Time[i],TIME_DATE) + " " + StartTime + ":00");
dt2 = StrToTime(TimeToStr(Time[i],TIME_DATE) + " " + EndTime + ":00");
int ib1 = iBarShift(NULL,0,dt1);
int ib2 = iBarShift(NULL,0,dt2);
if (dt2 != Time[i]) continue;
double vHigh = 0, vLow = 999;
for (int j=ib1; j>ib2; j--) {
vHigh = MathMax(vHigh,High[j]);
vLow = MathMin(vLow,Low[j]);
}
string objname = "BF-Box-" + i;
ObjectCreate(objname,OBJ_RECTANGLE,0,dt1,vHigh,dt2-60,vLow);
ObjectSet(objname,OBJPROP_COLOR,BoxColor);
objname = "BF-Fibo-" + i;
ObjectCreate(objname,OBJ_FIBO,0,dt2-7200,vHigh,dt2,vLow);
ObjectSet(objname,OBJPROP_RAY,false);
ObjectSet(objname,OBJPROP_COLOR,BoxColor);
ObjectSet(objname,OBJPROP_LEVELCOLOR,FiboColor);
ObjectSet(objname,OBJPROP_FIBOLEVELS,15);
ObjectSet(objname,OBJPROP_LEVELSTYLE,STYLE_SOLID);
ObjectSet(objname,OBJPROP_FIRSTLEVEL,0.0);
ObjectSet(objname,OBJPROP_FIRSTLEVEL+1,0.5);
ObjectSet(objname,OBJPROP_FIRSTLEVEL+2,1.0);
ObjectSet(objname,OBJPROP_FIRSTLEVEL+3,-0.236);
ObjectSet(objname,OBJPROP_FIRSTLEVEL+4,-0.382);
ObjectSet(objname,OBJPROP_FIRSTLEVEL+5,-0.618);
ObjectSet(objname,OBJPROP_FIRSTLEVEL+6,-1.0);
ObjectSet(objname,OBJPROP_FIRSTLEVEL+7,1.236);
ObjectSet(objname,OBJPROP_FIRSTLEVEL+8,1.382);
ObjectSet(objname,OBJPROP_FIRSTLEVEL+9,1.618);
ObjectSet(objname,OBJPROP_FIRSTLEVEL+10,2.0);
ObjectSet(objname,OBJPROP_FIRSTLEVEL+11,-1.382);
ObjectSet(objname,OBJPROP_FIRSTLEVEL+12,2.382);
ObjectSet(objname,OBJPROP_FIRSTLEVEL+13,-1.618);
ObjectSet(objname,OBJPROP_FIRSTLEVEL+14,2.618);
ObjectSetFiboDescription(objname,0,"Box Edge = %$");
ObjectSetFiboDescription(objname,1,"Initial Stop = %$");
ObjectSetFiboDescription(objname,2,"Box Edge = %$");
ObjectSetFiboDescription(objname,3,"Entry = %$");
ObjectSetFiboDescription(objname,4,"TP2 Stop = %$");
ObjectSetFiboDescription(objname,5,"TP1 and TP3 Stop = %$");
ObjectSetFiboDescription(objname,6,"TP2 = %$");
ObjectSetFiboDescription(objname,7,"Entry = %$");
ObjectSetFiboDescription(objname,8,"TP2 Stop = %$");
ObjectSetFiboDescription(objname,9,"TP1 and TP3 Stop = %$");
ObjectSetFiboDescription(objname,10,"TP2 = %$");
ObjectSetFiboDescription(objname,11,"TP3 = %$");
ObjectSetFiboDescription(objname,12,"TP3 = %$");
ObjectSetFiboDescription(objname,13,"TP4 = %$");
ObjectSetFiboDescription(objname,14,"TP4 = %$");
//dys++; if (dys >= NumDays) break;
}
WindowRedraw();
return(0);
}
//+------------------------------------------------------------------+
void del_obj() {
//+------------------------------------------------------------------+
int k=0;
while (k<ObjectsTotal()) {
string objname = ObjectName(k);
if (StringSubstr(objname,0,3) == "BF-")
ObjectDelete(objname);
else
k++;
}
return(0);
}
Comments