This tool is designed to automatically identify and display key price levels on a trading chart based on "Fractals"—which are specific patterns in price action that indicate potential points of support or resistance.
Here is a breakdown of how the logic operates:
Core Functionality
The script scans the market data to find the most recent upward and downward fractals. Once identified, it draws two horizontal lines on your chart: one representing a high-level boundary (Resistance) and one representing a low-level boundary (Support).
Key Logic Features
- Dynamic Level Detection: Instead of using static numbers, the tool constantly looks for the nearest valid fractal patterns in the current timeframe. It focuses on finding the most recent ones to ensure the levels stay relevant to current market movements.
- Customizable Offsets: You are not restricted to the exact price where a fractal occurs. The script allows you to define a specific distance (in points) above or below the fractal. This gives you the flexibility to create a "buffer zone" or a specific channel width.
- Visual Indicators & Data Display:
- The tool renders high-visibility horizontal lines with customizable colors.
- If enabled, it displays an on-screen summary (Comment) providing the current Timeframe, the specific prices for both levels, and the total point distance between them (the "Fractal Channel").
- Smart Order Management: The script includes a safety logic regarding open trades. If you have active positions, it can detect them. Depending on your settings, it can automatically hide or manage the lines based on whether your trades match specific "Magic Numbers" (unique IDs for automated trades) or other trading criteria.
- Automatic Maintenance: The script is designed to be efficient; it updates the positions of the horizontal lines in real-time as new fractals are formed, and it includes a cleanup function to remove these objects from the chart when the indicator is turned off.
Summary for the User
Essentially, this tool simplifies technical analysis by automating the "hunt" for support and resistance levels. It creates a visual corridor on your chart, showing you where price has historically pivoted, while giving you the ability to adjust exactly how far away from those pivots you want your trading boundaries to sit.
//+------------------------------------------------------------------+
//| Fractal_Level_Xrust_V2.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 DistanseAp=0;//Äèñòàíöèÿ ëèíèè îò ôðàêòàëà â ïóíêòàõ
extern int DistanseDn=0;//Äèñòàíöèÿ ëèíèè îò ôðàêòàëà â ïóíêòàõ
extern int MagicNumb =0;//
extern string FrApNam="Ap";//Èìÿ ëèíèè ñîîòâ.Ôðàêòàëó ââåðõ
extern string FrDnNam="Dn";//Èìÿ ëèíèè ñîîòâ.Ôðàêòàëó âíèç
extern color ClAp=Blue;//Öâåò ëèíèè ñîîòâ.Ôðàêòàëó ââåðõ
extern color ClDn=Red;//Öâåò ëèíèè ñîîòâ.Ôðàêòàëó âíèç
extern bool comment=true;//Ðàçðåùåíèå íà êîììåíòàðèé
extern bool Orders=true;//
//-----------------------------------------------------------------------------+
double FrPrise;
double FrApPrise=0,FrDnPrise=0;
bool _magic;
//+----------------------------------------------------------------------------+
void init(){
if(MagicNumb==0){_magic=false;}else{_magic=true;}
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;
//-----------------------------------------------------------------------------+
if(Orders){
if(OrdersTotal()>0){
for(i=0;i<OrdersTotal();i++){
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){
if(OrderSymbol()==Symbol()&&OrderType()<2){
if(_magic){
if(OrderMagicNumber()==MagicNumb){
ObjectDelete(FrDnNam);
ObjectDelete(FrApNam);
return(0);}}
ObjectDelete(FrDnNam);
ObjectDelete(FrApNam);
return(0);}}}}}
LineAp();
LineDn();
string FAP=DoubleToStr((FrApPrise-DistanseAp*Point),Digits);
string FDP=DoubleToStr((FrDnPrise+DistanseDn*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-DistanseDn*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+DistanseAp*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