Okay, here's a breakdown of what this MetaTrader script does, explained in a way that's easy to understand, even if you're not a programmer.
Overall Purpose:
The script attempts to identify potential "squatting fractals" on a price chart and draw trend lines based on the high and low prices of recent bars. A "squatting fractal" is a specific pattern it's looking for that might signal a potential price move. The script uses volume and price range calculations to determine if such a pattern exists.
Here's how it works, step-by-step:
-
Initialization (init):
- Sets up the visual appearance of the script on the chart. It defines the colors (green and red) that will be used to mark potential squatting fractals.
- Sets up buffers to store High and Low for calculations.
- Displays a comment on the chart indicating the script's name and version.
-
Objects Initialization (ObjectsInit):
- It checks if there are enough price data points available before it continues.
- Creates two trend lines ("Up" and "Down") on the chart, initially placing them based on recent High and Low prices. These lines are blue and brown, respectively, and extend to the right indefinitely ("ray" property). These lines will be moved in the
start()
function.
-
Deinitialization (deinit):
- Removes the trend lines ("Up" and "Down") from the chart when the script is stopped or removed.
- Clears any comments displayed by the script.
-
Main Calculation (start):
-
Fractal Identification:
- The script looks at the recent bars (price data) on the chart and identifies where fractals (patterns indicating potential turning points) are located using the built-in
iFractals
function. - It focuses on situations where there's only one fractal on a given bar ? either an upward fractal OR a downward fractal, but not both. If there are both, it ignores that bar.
- The script looks at the recent bars (price data) on the chart and identifies where fractals (patterns indicating potential turning points) are located using the built-in
-
"Squatting" Check:
- It compares successive fractals. Let's say it finds a downward fractal after finding an upward one. Then it performs calculations to determine if a "squatting" condition exists.
-
Volume and Price Range Calculation:
- If the script detects an up or down fractal, it calculates a special value, MFI or Money Flow Index, based on the High and Low ranges as well as trading Volume of the last 5 bars. It adds up the differences between the High and Low prices for each of the 5 bars and divides that sum by the sum of the Volume for those bars. This is done separately for "Up" and "Down" fractal scenarios.
-
Squatting Fractal Determination:
- If the current bar has a Down fractal, and if the previous fractal was an Up fractal, then the script compares the MFI from the Up fractal and the current MFI from the Down fractal. If the
AllMFIDown
is less thanAllMFIUp
andAllVolDown
is more thanAllVolUp
, the script determines that this is a squatting fractal. Similarly, if the current bar has an Up fractal and the previous fractal was a Down fractal, then the script compares the MFI from the Down fractal with the current MFI from the Up fractal. IfAllMFIUp
is less thanAllMFIDown
andAllVolUp
is more thanAllVolDown
, the script determines that this is a squatting fractal.
- If the current bar has a Down fractal, and if the previous fractal was an Up fractal, then the script compares the MFI from the Up fractal and the current MFI from the Down fractal. If the
-
Highlighting:
- If it does identify a squatting fractal, it marks it on the chart with a colored arrow: green for upward squatting fractals and red for downward squatting fractals.
-
Trend Line Adjustment:
- The script attempts to adjust the position of the trend lines ("Up" and "Down") created earlier. It searches for the two most recent High and Low points from the buffered data and repositions the lines to connect them.
-
In simpler terms: Imagine the script is looking for instances where the price seems to "pause" or "squat down" before potentially making a move. It's using fractals to identify possible turning points, and then checking if the volume and price action around those points suggest a genuine opportunity. If it thinks it's found one, it marks the spot and adjusts its trend lines to reflect the current price action.
//+------------------------------------------------------------------+
//| New_Fractals_Lines v1.0.1 |
//| Copyright © -2007, olyakish |
//| plutonia-dmb#yandex.ru |
//+------------------------------------------------------------------+
// èñïðàâèë ãëþê ñ "invalid time value for ObjectMove function"
#property copyright "olyakish"
#property link "plutonia-dmb#yandex.ru"
//----
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Green // Ïðèñåäàþùèé
#property indicator_color2 Red // Ïðèñåäàþùèé
//----
extern int Pips = 5;
double ExtLimeBuffer[];
double ExtRedBuffer[];
//----
double AllHLDown, AllVolDown, AllHLUp, AllVolUp, AllMFIDown, AllMFIUp;
int OldFractal = 2;
bool Up = false, Down = false;
bool objects_initialized=false;
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int init()
{
SetIndexShift(0, 0);
SetIndexShift(1, 0);
//----
SetIndexBuffer(0, ExtLimeBuffer);
SetIndexBuffer(1, ExtRedBuffer);
//----
SetIndexStyle(0, DRAW_ARROW, 0, 1);
SetIndexStyle(1, DRAW_ARROW, 0, 1);
//----
SetIndexArrow(0, 177);
SetIndexArrow(1, 177);
//----
//----
Comment("New_Fractals_Lines v1.0.1");
//----
return(0);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool ObjectsInit()
{
//---- check if we have enough bars
if(ArraySize(High)<3)
{
Print("Timeseries is not ready yet - exit");
return(false);
}
//---
ObjectCreate("Up", OBJ_TREND, 0, iTime(NULL, 0, 2), High[2],
iTime(NULL, 0, 1), High[1],0,0);
ObjectSet("Up", OBJPROP_COLOR, Blue);
ObjectSet("Up", OBJPROP_RAY, true);
//----
ObjectCreate("Down", OBJ_TREND, 0, iTime(NULL, 0, 2), Low[2],
iTime(NULL, 0, 1), Low[1],0,0);
ObjectSet("Down", OBJPROP_COLOR, Brown);
ObjectSet("Down", OBJPROP_RAY, true);
Print("Trend lines created");
//----
return(true);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int deinit()
{
ObjectDelete("Up");
ObjectDelete("Down");
//----
Comment("");
//----
return(0);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int start()
{
if(!objects_initialized)
{
objects_initialized=ObjectsInit();
}
ArrayInitialize(ExtLimeBuffer, NULL);
ArrayInitialize(ExtRedBuffer, NULL);
for(int i = 1000; i >= 3; i--)
{
// íàñ èíòåðåñóåò òîëüêî ñèòóàöèÿ êîãäà íà áàðå îäèí ôðàêòàë
// (â ëþáóþ ñòîðîíó)
// åñëè äâà ôðàêòàëà íà îäíîì áàðå, òî îòñåèâàåì òàêèå ñèòóàöèè
if(iFractals(NULL, 0, MODE_UPPER, i) != 0)
{
Up = true;
Down = false;
}
if(iFractals(NULL, 0, MODE_LOWER, i) != 0)
{
Up = false;
Down = true;
}
// äâà ôðàêòàëà ñðàçó - èãíîðèðóåì
if(iFractals(NULL, 0, MODE_UPPER, i) != 0 &&
iFractals(NULL, 0, MODE_LOWER,i) != 0)
{
Up = false;
Down = false;
}
// íà òåêóùåì áàðå åñòü ôðàêòàë âíèç
if(Up == false && Down == true)
{
AllHLDown = 0;
AllVolDown = 0;
AllHLDown = High[i+2] - Low[i+2] + High[i+1] -
Low[i+1] + High[i] - Low[i] + High[i-1] -
Low[i-1] + High[i-2] - Low[i-2];
AllVolDown = Volume[i+2] + Volume[i+1] + Volume[i] +
Volume[i-1] + Volume[i-2];
AllMFIDown = AllHLDown / AllVolDown;
// ïðåäûäóùèé ôðàêòàë áûë ââåðõ - ñðàâíèâàåì äâà ôðàêòàëà
// (ââåðõ è òåêóùèé âíèç)
if(OldFractal == 1)
{
// èìååì ïðèñåäàþùèé ôðàêòàë (MFI - îáúåì +)
if(AllMFIDown < AllMFIUp && AllVolDown > AllVolUp)
{
// çàïîëíÿåì áóôåðû
ExtLimeBuffer[i] = NULL;
ExtRedBuffer[i] = Low[i]; // - Pips*Point;
}
}
OldFractal = 0;
}
// íà òåêóùåì áàðå åñòü ôðàêòàë ââåðõ
if(Up == true && Down == false)
{
AllHLUp = 0;
AllVolUp = 0;
AllHLUp = High[i+2] - Low[i+2] + High[i+1] - Low[i+1] +
High[i] - Low[i] + High[i-1] - Low[i-1] +
High[i-2] - Low[i-2];
AllVolUp = Volume[i+2] + Volume[i+1] + Volume[i] +
Volume[i-1] + Volume[i-2];
AllMFIUp = AllHLUp / AllVolUp;
// ïðåäûäóùèé ôðàêòàë áûë âíèç - ñðàâíèâàåì äâà ôðàêòàëà
// (ââåðõ è òåêóùèé âíèç)
if(OldFractal == 0)
{
// èìååì ïðèñåäàþùèé ôðàêòàë (MFI - îáúåì +)
if(AllMFIUp < AllMFIDown && AllVolUp > AllVolDown)
{
// çàïîëíÿåì áóôåðû
ExtLimeBuffer[i] = High[i]; // + Pips*Point;
ExtRedBuffer[i] = NULL;
}
}
OldFractal = 1;
}
}
double _Price[2,2];
int _Time[2,2];
ArrayInitialize(_Price, -1);
ArrayInitialize(_Time, -1);
for(i = 3; i <= 1000; i++)
{
//up
if(ExtLimeBuffer[i] != NULL && _Price[0,0] != -1 &&
_Price[1,0] == -1 )
{
_Price[1,0] = ExtLimeBuffer[i];
_Time[1,0] = iTime(NULL, 0, i);
}
if(ExtLimeBuffer[i]!=NULL && _Price[0,0]==-1)
{
_Price[0,0] = ExtLimeBuffer[i];
_Time[0,0] = iTime(NULL, 0, i);
}
// down
if(ExtRedBuffer[i] != NULL && _Price[0,1] != -1 &&
_Price[1,1] == -1)
{
_Price[1,1] = ExtRedBuffer[i];
_Time[1,1] = iTime(NULL, 0, i);
}
if(ExtRedBuffer[i] != NULL && _Price[0,1] == -1)
{
_Price[0,1] = ExtRedBuffer[i];
_Time[0,1] = iTime(NULL, 0, i);
}
}
ObjectMove("Up", 1, _Time[0,0], _Price[0,0]);
ObjectMove("Up", 0, _Time[1,0], _Price[1,0]);
//----
ObjectMove("Down", 1, _Time[0,1], _Price[0,1]);
ObjectMove("Down", 0, _Time[1,1], _Price[1,1]);
//----
return(0);
}
//+------------------------------------------------------------------+
Comments
Markdown Formatting Guide
# H1
## H2
### H3
**bold text**
*italicized text*
[title](https://www.example.com)

`code`
```
code block
```
> blockquote
- Item 1
- Item 2
1. First item
2. Second item
---