iTrend.mq4 MetaTrader MQL4 Script Description
Overview
The provided MQL4 script iTrend.mq4
is designed to generate two trend lines based on input parameters and historical price data. It uses the iBands
and iBearsPower/iBullsPower
functions from MetaTrader's built-in indicators to calculate a moving average with Bollinger Bands and power prices, respectively.
Indicators and Buffers
- Indicators: The script generates two trend lines (
value[]
andvalue2[]
) that are displayed in the chart. - Colors: The first indicator is colored
LimeGreen
, while the second isRed
.
Input Parameters
The script accepts several input parameters to customize its behavior:
- Bands_Mode_0_2: Determines which mode of Bollinger Bands to use. It can be set to
0
forMODE_MAIN
,1
forMODE_LOW
, or2
forMODE_HIGH
. - Power_Price_0_6: Specifies the type of power price to use, ranging from
0
(CLOSE) to6
(WEIGHTED). - Price_Type_0_3: Indicates which price type to use, ranging from
0
(CLOSE) to3
(LOW). - Bands_Period: The period for the Bollinger Bands.
- Bands_Deviation: The deviation used in calculating the Bollinger Bands.
- Power_Period: The period for the Power indicator.
- CountBars: The number of bars to calculate, with a default value of 20000.
Custom Indicator Initialization (init()
)
The init()
function initializes the script by setting up the required buffers and styles. It ensures that two lines are drawn on the chart, each with its own color specified in MQL4 properties.
Trend Calculation (start()
)
The start()
function performs the core logic of calculating the trend lines based on user input parameters:
- Bands Mode Selection: Adjusts the Bollinger Bands mode according to
Bands_Mode_0_2
. - Power Price Selection: Determines which price type (CLOSE, OPEN, HIGH, LOW, MEDIAN, TYPICAL, WEIGHTED) to use based on
Power_Price_0_6
. - Price Type Selection: Uses the
Open
,High
,Low
, orClose
prices depending onPrice_Type_0_3
.
For each bar in the specified range (CountBars - Bands_Period - 1
to 0
), it calculates the value of the trend line using:
- Bollinger Bands: The
iBands
function, which computes a Bollinger Band based on the selected mode and power price. - Power Indicator: Sums the positive and negative power values from the
iBearsPower
andiBullsPower
functions to create an opposing value for the second trend line.
Logic Summary
- Initialization Check: Ensures that the script has enough bars to compute the Bollinger Bands and Power indicator values.
- Initial Zeroing: Sets initial values if necessary, based on
counted_bars
(number of previously calculated bars). - Loop Through Bars: Iterates through a specified range of historical data, calculating trend line values for each bar using the selected parameters and functions.
//+------------------------------------------------------------------+
//| iTrend.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 2
#property indicator_color1 LimeGreen
#property indicator_color2 Red
//---- input parameters
extern int Bands_Mode_0_2=0; // =0-2 MODE_MAIN, MODE_LOW, MODE_HIGH
extern int Power_Price_0_6=0; // =0-6 PRICE_CLOSE,PRICE_OPEN,PRICE_HIGH,PRICE_LOW,PRICE_MEDIAN,PRICE_TYPICAL,PRICE_WEIGHTED
extern int Price_Type_0_3=0; // =0-3 PRICE_CLOSE,PRICE_OPEN,PRICE_HIGH,PRICE_LOW
extern int Bands_Period=20;
extern int Bands_Deviation=2;
extern int Power_Period=13;
extern int CountBars=20000;
//---- buffers
double value[];
double value2[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
// string short_name;
//---- indicator line
IndicatorBuffers(2);
SetIndexStyle(0,DRAW_LINE);
SetIndexStyle(1,DRAW_LINE);
SetIndexBuffer(0,value);
SetIndexBuffer(1,value2);
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| Trend |
//+------------------------------------------------------------------+
int start()
{
SetIndexDrawBegin(0,Bars-CountBars+Bands_Period+1);
SetIndexDrawBegin(1,Bars-CountBars+Bands_Period+1);
int i,CurrentBar,Bands_Mode,counted_bars=IndicatorCounted();
double Power_Price,CurrentPrice;
//----
if(Bars<=Bands_Period) return(0);
//---- initial zero
if(counted_bars<Bands_Period)
{
for(i=1;i<=Bands_Period;i++) value[Bars-i]=0.0;
for(i=1;i<=Bands_Period;i++) value2[Bars-i]=0.0;
}
//----
i=CountBars-Bands_Period-1;
// if(counted_bars>=Bands_Period) i=Bars-counted_bars-1;
if (Bands_Mode_0_2==1) Bands_Mode=MODE_LOW;
if (Bands_Mode_0_2==2) Bands_Mode=MODE_HIGH;
if (Bands_Mode_0_2==0) Bands_Mode=MODE_MAIN;
if (Power_Price_0_6==1) Power_Price=PRICE_OPEN;
if (Power_Price_0_6==2) Power_Price=PRICE_HIGH;
if (Power_Price_0_6==3) Power_Price=PRICE_LOW;
if (Power_Price_0_6==4) Power_Price=PRICE_MEDIAN;
if (Power_Price_0_6==5) Power_Price=PRICE_TYPICAL;
if (Power_Price_0_6==6) Power_Price=PRICE_WEIGHTED;
if (Power_Price_0_6==6) Power_Price=PRICE_CLOSE;
for (i=CountBars-1; i>=0; i--)
{
if (Price_Type_0_3==1) CurrentPrice=Open[i];
if (Price_Type_0_3==2) CurrentPrice=High[i];
if (Price_Type_0_3==3) CurrentPrice=Low[i];
if (Price_Type_0_3==0) CurrentPrice=Close[i];
value[i]=CurrentPrice-iBands(NULL,0,Bands_Period,Bands_Deviation,0,Bands_Mode,Power_Price,i);
value2[i]=-(iBearsPower(NULL,0,Power_Period,Power_Price,i)+iBullsPower(NULL,0,Power_Period,Power_Price,i));
}
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
---