This script is designed for the MetaTrader 4 platform to automatically identify and visualize key price levels based on "fractals." In technical analysis, fractals are patterns that help traders identify potential turning points in the market—essentially marks where the price has reached a temporary high or low.
Core Functionality
The primary goal of this tool is to detect the most recent significant peaks (highs) and valleys (lows) on your chart and project them as horizontal lines. This helps you visualize "support" and "resistance" levels more clearly without having to draw them manually.
Key Features and Logic
- Fractal Detection: The script continuously scans the current price history to find the nearest "Up" fractal (a peak) and "Down" fractal (a valley). It looks backward through recent price bars to ensure it identifies the most relevant data points for your current view.
- Dynamic Level Offsetting: One of the specific features of this script is a "Distance" setting. Instead of placing the horizontal line exactly on the fractal point, you can define a specific number of points (pips) above or below that fractal. This allows for a buffer zone, which can be useful depending on your specific trading strategy.
- Visual Automation:
- Upper Levels: The script identifies an upward fractal and draws a blue horizontal line at the designated price level.
- Lower Levels: The script identifies a downward fractal and draws a red horizontal line at the designated price level.
- Automatic Updates: The lines are not static; if the market creates a new, closer fractal that qualifies as a turning point, the horizontal lines will automatically jump to these new coordinates.
- On-Screen Information (Optional): If enabled in the settings, the script provides a text summary in the corner of your chart. This summary displays:
- The specific TimeFrame being analyzed.
- The exact price coordinates for the Upper and Lower levels.
- The total "Fractal Channel" size (the distance between the upper and lower points).
User Customization
The tool provides several settings to tailor the visual experience:
- TimeFrame: Allows you to choose which time period the script should monitor.
- Distance: Adjusts how far away from the actual fractal point the horizontal line is placed.
- Colors & Names: You can customize the colors and labels of the lines to match your chart's aesthetic.
- Comments: A toggle to turn the detailed price data on or off in the corner of the screen.
//+------------------------------------------------------------------+
//| Fractal_Level_Xrust.mq4 |
//| Copyright © 2008, XrustSolution. |
//| http://xrust.ucoz.net |
//+------------------------------------------------------------------+
#property copyright "#Copyright © 2008, XrustSolution.#"
#property link "#xrust.ucoz.net#"
#property indicator_chart_window
extern int TimeFrame=0;//Åñëè=0, òî òåêóøèé
extern int Distanse=0;//Äèñòàíöèÿ ëèíèè îò ôðàêòàëà â ïóíêòàõ
extern string FrApNam="Ap";//Èìÿ ëèíèè ñîîòâ.Ôðàêòàëó ââåðõ
extern string FrDnNam="Dn";//Èìÿ ëèíèè ñîîòâ.Ôðàêòàëó âíèç
extern color ClAp=Blue;//Öâåò ëèíèè ñîîòâ.Ôðàêòàëó ââåðõ
extern color ClDn=Red;//Öâåò ëèíèè ñîîòâ.Ôðàêòàëó âíèç
extern bool comment=false;//Ðàçðåùåíèå íà êîììåíòàðèé
//-----------------------------------------------------------------------------+
double FrPrise;
double FrApPrise=0,FrDnPrise=0;
//+----------------------------------------------------------------------------+
void init(){
if(TimeFrame==0){TimeFrame=Period();}
IndicatorShortName("Factal_Level_Xrust"+TimeFrame);
FrApNam=StringConcatenate(FrApNam,TimeFrame);
FrDnNam=StringConcatenate(FrDnNam,TimeFrame);
return;}
void deinit(){
ObjectDelete(FrDnNam);
ObjectDelete(FrApNam);
Comment(" ");
return;}
//+----------------------------------------------------------------------------+
int start()
{
int counted_bars=IndicatorCounted();
int limit;
double tmp;
int i, j,k;
if(counted_bars<0) return(-1);
if(counted_bars>0) counted_bars--;
limit=Bars-counted_bars;
//-----------------------------------------------------------------------------+
LineAp();
LineDn();
string FAP=DoubleToStr((FrApPrise-Distanse*Point),Digits);
string FDP=DoubleToStr((FrDnPrise+Distanse*Point),Digits);
int diap=MathRound((FrApPrise-FrDnPrise)/Point);
if(comment){Comment("TimeFrame="+TimeFrame+
"\nApPrise ="+FAP+
"\nDnPrise ="+FDP+
"\nFractal Channel ="
+diap+"Point");}
return(0);
}
//-----------------------------------------------------------------------------+
// Èùåò ãîðèçîíòàëüíóþ ëèíèþ ïî èìåíè ïåðåðèñîâûâàåò åñëè èçìåíèëàñü öåíà |
//-----------------------------------------------------------------------------+
void LineDn(){
FrPrise=NormalizeDouble(FindNearFractal(0,TimeFrame,MODE_LOWER),MarketInfo(Symbol(),MODE_DIGITS));
FrPrise=NormalizeDouble(FrPrise-Distanse*Point,MarketInfo(Symbol(),MODE_DIGITS));
//Comment(FrPrise);
if(ObjectFind(FrDnNam)==0){
if(ObjectGet(FrDnNam,OBJPROP_PRICE1)==FrPrise){return;}}
FrDnPrise=FrPrise;
ObjectDelete(FrDnNam);
SetHLine(ClDn,FrDnNam,FrDnPrise,0,1);
WindowRedraw();
return;}
//-----------------------------------------------------------------------------+
// Èùåò ãîðèçîíòàëüíóþ ëèíèþ ïî èìåíè ïåðåðèñîâûâàåò åñëè èçìåíèëàñü öåíà |
//-----------------------------------------------------------------------------+
void LineAp(){
FrPrise=NormalizeDouble(FindNearFractal(0,TimeFrame,MODE_UPPER),MarketInfo(Symbol(),MODE_DIGITS));
FrPrise=NormalizeDouble(FrPrise+Distanse*Point,MarketInfo(Symbol(),MODE_DIGITS));
if(ObjectFind(FrApNam)==0){
if(ObjectGet(FrApNam,OBJPROP_PRICE1)==FrPrise){return;}}
FrApPrise=FrPrise;
ObjectDelete(FrApNam);
SetHLine(ClAp,FrApNam,FrApPrise,0,1);
WindowRedraw();
return;}
//-----------------------------------------------------------------------------+
// Êèìó Ðåñïåêò è óâàæóõà !!! |
//+----------------------------------------------------------------------------+
//| Àâòîð : Êèì Èãîðü Â. aka KimIV, http://www.kimiv.ru |
//+----------------------------------------------------------------------------+
//| Âåðñèÿ : 07.10.2006 |
//| Îïèñàíèå : Ïîèñê áëèæàéøåãî ôðàêòàëà. Âîçâðàùàåò öåíîâîé óðîâåíü. |
//+----------------------------------------------------------------------------+
//| Ïàðàìåòðû: |
//| sy - íàèìåíîâàíèå èíñòðóìåíòà ("" èëè NULL - òåêóùèé ñèìâîë) |
//| tf - òàéìôðåéì ( 0 - òåêóùèé ÒÔ) |
//| mode - òèï ôðàêòàëà (MODE_LOWER|MODE_UPPER) |
//+----------------------------------------------------------------------------+
double FindNearFractal(string sy="0", int tf=0, int mode=MODE_LOWER) {
if (sy=="" || sy=="0") sy=Symbol();
double f=0;
int d=MarketInfo(sy, MODE_DIGITS), s;
if (d==0) if (StringFind(sy, "JPY")<0) d=4; else d=2;
for (s=2; s<100; s++) {
f=iFractals(sy, tf, mode, s);
if (f!=0) return(NormalizeDouble(f, d));
}
Print("FindNearFractal(): Ôðàêòàë íå íàéäåí");
return(0);
}
//+----------------------------------------------------------------------------+
//| Àâòîð : Êèì Èãîðü Â. aka KimIV, http://www.kimiv.ru |
//+----------------------------------------------------------------------------+
//| Âåðñèÿ : 30.03.2008 |
//| Îïèñàíèå : Óñòàíîâêà îáúåêòà OBJ_HLINE ãîðèçîíòàëüíàÿ ëèíèÿ |
//+----------------------------------------------------------------------------+
//| Ïàðàìåòðû: |
//| cl - öâåò ëèíèè |
//| nm - íàèìåíîâàíèå ("" - âðåìÿ îòêðûòèÿ òåêóùåãî áàðà) |
//| p1 - öåíîâîé óðîâåíü (0 - Bid) |
//| st - ñòèëü ëèíèè (0 - ïðîñòàÿ ëèíèÿ) |
//| wd - øèðèíà ëèíèè (0 - ïî óìîë÷àíèþ) |
//+----------------------------------------------------------------------------+
void SetHLine(color cl, string nm="", double p1=0, int st=0, int wd=1) {
if (nm=="") nm=DoubleToStr(Time[0], 0);
if (p1<=0) p1=Bid;
if (ObjectFind(nm)<0) ObjectCreate(nm, OBJ_HLINE, 0, 0,0);
ObjectSet(nm, OBJPROP_PRICE1, p1);
ObjectSet(nm, OBJPROP_COLOR , cl);
ObjectSet(nm, OBJPROP_STYLE , st);
ObjectSet(nm, OBJPROP_WIDTH , wd);
}
Comments