Here's a detailed explanation of the logic of the provided MetaTrader MQL4 script, presented in Markdown format. This explanation focuses on what the script does, not how to improve it.
Overview: The Alligator Indicator
This MQL4 script implements Bill Williams' Alligator indicator. The Alligator is a technical analysis indicator designed to identify trends and potential trading opportunities. It consists of three lines: the "Jaws," the "Teeth," and the "Lips." The script also calculates and displays a trend line.
Script Structure and Initialization (init()
function)
-
Properties: The script begins by defining several properties:
copyright
: Specifies the copyright information.link
: Provides a link to the MetaQuotes website.indicator_separate_window
: Indicates that the indicator should be displayed in a separate window.indicator_buffers
: Defines the number of indicator buffers used (4 in this case).indicator_color1
,indicator_color2
,indicator_color3
: These are commented out, but they would define the colors used for the indicator lines if uncommented.
-
Input Parameters: The script defines several external input parameters that users can adjust:
JawsPeriod
: The period used to calculate the "Jaws" line.JawsShift
: The shift applied to the "Jaws" line.TeethPeriod
: The period used to calculate the "Teeth" line.TeethShift
: The shift applied to the "Teeth" line.LipsPeriod
: The period used to calculate the "Lips" line.LipsShift
: The shift applied to the "Lips" line.trend
: The period used to calculate the trend line.trendShift
: The shift applied to the trend line.
-
Indicator Buffers: Four arrays (
ExtBlueBuffer
,ExtRedBuffer
,ExtLimeBuffer
,TrendBuffer
) are declared to store the calculated values for the indicator lines. These are the buffers that will be drawn on the chart. -
init()
Function: This function is called once when the indicator is loaded. It performs initialization tasks:SetIndexShift()
: Sets the shift for each indicator line. This determines how much the lines are offset from the current bar.SetIndexDrawBegin()
: Specifies the bar from which each indicator line should begin to be drawn.SetIndexBuffer()
: Associates each indicator buffer (array) with a specific indicator line.SetIndexStyle()
: Defines the drawing style for each indicator line (histogram for Jaws, Teeth, and Lips; line for the trend).SetIndexLabel()
: Assigns labels to each indicator line for display in the indicator's window.
Calculation and Drawing (start()
function)
-
start()
Function: This function is called on each new bar. It performs the core calculations and updates the indicator buffers. -
Error Handling: The function first checks for errors during the counting of bars.
-
Calculating the Number of Bars: The script determines the number of bars to process based on the total number of bars and the number of bars already counted.
-
Main Calculation Loop: The
for
loop iterates through the bars to be processed. Inside the loop:iBullsPower()
: Calculates the Bulls Power (typically representing upward price movement) using theiBullsPower
function. The parameters specify the symbol, shift, period, price type (typical), and the current bar index.iBearsPower()
: Calculates the Bears Power (typically representing downward price movement) using theiBearsPower
function. The parameters are similar toiBullsPower
.ExtLimeBuffer[i]=ExtRedBuffer[i]+ExtBlueBuffer[i];
: Calculates the Lime buffer by summing the Red and Blue buffers.
Summary
In essence, the script calculates three lines (Jaws, Teeth, Lips) and a trend line based on Bulls Power and Bears Power calculations. The init()
function sets up the drawing parameters, and the start()
function performs the calculations and updates the indicator buffers for each new bar, which are then displayed on the chart. The user can adjust the periods and shifts of the lines to fine-tune the indicator's sensitivity.
/*-----------------------------+
| |
| Shared by www.Aptrafx.com |
| |
+------------------------------*/
//+------------------------------------------------------------------+
//| Alligator.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_separate_window
#property indicator_buffers 4
//#property indicator_color1 Blue
#property indicator_color2 Red
//#property indicator_color3 Lime
//---- input parameters
extern int JawsPeriod=5;
extern int JawsShift=0;
extern int TeethPeriod=8;
extern int TeethShift=0;
extern int LipsPeriod=5;
extern int LipsShift=0;
extern int trend=0;
extern int trendShift=0;
//---- indicator buffers
double ExtBlueBuffer[];
double ExtRedBuffer[];
double ExtLimeBuffer[];
double TrendBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- line shifts when drawing
SetIndexShift(0,JawsShift);
SetIndexShift(1,TeethShift);
SetIndexShift(2,LipsShift);
SetIndexShift(3,trendShift);
//---- first positions skipped when drawing
SetIndexDrawBegin(0,JawsShift+JawsPeriod);
SetIndexDrawBegin(1,TeethShift+TeethPeriod);
SetIndexDrawBegin(2,LipsShift+LipsPeriod);
SetIndexDrawBegin(3,trend);
//---- 3 indicator buffers mapping
SetIndexBuffer(0,ExtBlueBuffer);
SetIndexBuffer(1,ExtRedBuffer);
SetIndexBuffer(2,ExtLimeBuffer);
SetIndexBuffer(3,TrendBuffer);
//---- drawing settings
SetIndexStyle(0,DRAW_HISTOGRAM);
SetIndexStyle(1,DRAW_HISTOGRAM);
SetIndexStyle(2,DRAW_HISTOGRAM);
SetIndexStyle(3,DRAW_LINE);
//---- index labels
SetIndexLabel(0,"Gator Jaws");
SetIndexLabel(1,"Gator Teeth");
SetIndexLabel(2,"Gator Lips");
SetIndexLabel(3,"trend");
//---- initialization done
return(0);
}
//+------------------------------------------------------------------+
//| Bill Williams' Alligator |
//+------------------------------------------------------------------+
int start()
{
int limit;
int counted_bars=IndicatorCounted();
//---- check for possible errors
if(counted_bars<0) return(-1);
//---- last counted bar will be recounted
if(counted_bars>0) counted_bars--;
limit=Bars-counted_bars;
//---- main loop
for(int i=0; i<limit; i++)
{
//---- ma_shift set to 0 because SetIndexShift called abowe
ExtBlueBuffer[i]=iBullsPower(NULL, 0, 6,PRICE_TYPICAL,i);
ExtRedBuffer[i]=iBearsPower(NULL, 0, 6,PRICE_TYPICAL,i);//iBearsPower(NULL, 0, 11,PRICE_CLOSE,i)+;iBullsPower(NULL, 0, 11,PRICE_CLOSE,i);
ExtLimeBuffer[i]=ExtRedBuffer[i]+ExtBlueBuffer[i];
}
//---- done
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
---