# Metatrader MQL Script Logic Explanation
## Overview
This script is designed for a MetaTrader terminal, specifically using the MQL4 programming language. The script calculates and displays various indicators based on technical analysis techniques such as CCI (Commodity Channel Index), WPR (Welles Wilder Percentage Range), and Force Index with different periods. These indicators are then used to determine whether to display high, low, open, or close values of the candlesticks.
## Variables
- `g_period_76`, `g_period_84`, and `g_period_92`: Constants representing different time periods for calculations.
- `g_ibuf_100[]` through `g_ibuf_128[]`: Buffers used to store the calculated values.
## Initialization
```mql
int init() {
// Set index styles and buffers, define indicator name
SetIndexStyle(0, DRAW_HISTOGRAM);
SetIndexBuffer(0, g_ibuf_100);
...
IndicatorShortName("#PalladaSE_Bars©2009 TradeWays");
return (0);
}
- The
init
function configures the display style and buffer for each of the indicators. It also sets a descriptive name for the indicator.
Main Calculation
int start() {
// Local variables
double l_icci_8, l_iwpr_16, l_iforce_24;
int li_0;
// Loop through all bars to calculate values
for (li_0 = 0; li_0 < Bars - 1; li_0++) {
l_icci_8 = iCustom(_Symbol, _Period, "CCI", g_period_84, 1)[li_0];
l_iwpr_16 = iCustom(_Symbol, _Period, "WPR", g_period_92, 1)[li_0];
l_iforce_24 = iForce(_Symbol, _Period, g_period_76, MODE_EMA, PRICE_CLOSE, li_0);
// Determine and assign values based on conditions
if (l_icci_8 > 0.0 && l_iwpr_16 > -50.0 && l_iforce_24 > 0.0) {
g_ibuf_100[li_0] = MathMax(Open[li_0], Close[li_0]);
g_ibuf_104[li_0] = MathMin(Open[li_0], Close[li_0]);
g_ibuf_116[li_0] = High[li_0];
g_ibuf_120[li_0] = Low[li_0];
} else {
if (l_icci_8 > 0.0 && l_iwpr_16 < -50.0) {
g_ibuf_108[li_0] = MathMax(Open[li_0], Close[li_0]);
g_ibuf_112[li_0] = MathMin(Open[li_0], Close[li_0]);
g_ibuf_124[li_0] = High[li_0];
g_ibuf_128[li_0] = Low[li_0];
} else {
if (l_icci_8 < 0.0 && l_iwpr_16 < -50.0 && l_iforce_24 < 0.0) {
g_ibuf_104[li_0] = MathMax(Open[li_0], Close[li_0]);
g_ibuf_100[li_0] = MathMin(Open[li_0], Close[li_0]);
g_ibuf_120[li_0] = High[li_0];
g_ibuf_116[li_0] = Low[li_0];
} else {
if (l_icci_8 < 0.0 && l_iwpr_16 > -50.0) {
g_ibuf_112[li_0] = MathMax(Open[li_0], Close[li_0]);
g_ibuf_108[li_0] = MathMin(Open[li_0], Close[li_0]);
g_ibuf_128[li_0] = High[li_0];
g_ibuf_124[li_0] = Low[li_0];
}
}
}
}
return (0);
}
- The
start
function is the main computation loop that processes each bar in the current chart. - It calculates CCI, WPR, and Force Index for each bar using
iCustom
andiForce
. - Based on the calculated values, it determines which high, low, open, or close value to assign to the respective buffer.
Key Conditions
The script uses nested if
conditions to check multiple indicators:
- Positive CCI (
l_icci_8 > 0.0
):- Combined with other conditions (WPR and Force Index) it assigns high, low, open, close values.
- Negative WPR (
l_iwpr_16 < -50.0
):- Depending on CCI value, it assigns different combinations of Open/Close/High/Low values to the buffers.
Buffer Assignment
- Buffers are assigned based on the conditions and the current bar's high, low, open, or close price.
This script effectively combines multiple technical indicators to identify specific market conditions and highlight relevant price levels for trading analysis. The logic can be fine-tuned further depending on the specific trading strategy requirements.
## Conclusion
The provided script is a comprehensive example of how to implement and combine different technical indicators in MetaTrader using MQL4. It demonstrates effective use of loops, conditional statements, and external function calls (like `iCustom` and `iForce`) to analyze market data and provide insights for trading decisions.
Indicators Used
Miscellaneous
2
Views
0
Downloads
0
Favorites
#Pallada_Bars
#property copyright "Copyright © 2009, TradeWays"
#property link "http://www.tradeways.org"
#property indicator_chart_window
#property indicator_buffers 8
#property indicator_color1 Blue
#property indicator_color2 Red
#property indicator_color3 DodgerBlue
#property indicator_color4 Tomato
#property indicator_color5 Blue
#property indicator_color6 Red
#property indicator_color7 DodgerBlue
#property indicator_color8 Tomato
double g_period_76 = 50.0;
double g_period_84 = 30.0;
double g_period_92 = 38.0;
double g_ibuf_100[];
double g_ibuf_104[];
double g_ibuf_108[];
double g_ibuf_112[];
double g_ibuf_116[];
double g_ibuf_120[];
double g_ibuf_124[];
double g_ibuf_128[];
int init() {
SetIndexStyle(0, DRAW_HISTOGRAM);
SetIndexBuffer(0, g_ibuf_100);
SetIndexStyle(1, DRAW_HISTOGRAM);
SetIndexBuffer(1, g_ibuf_104);
SetIndexStyle(2, DRAW_HISTOGRAM);
SetIndexBuffer(2, g_ibuf_108);
SetIndexStyle(3, DRAW_HISTOGRAM);
SetIndexBuffer(3, g_ibuf_112);
SetIndexStyle(4, DRAW_HISTOGRAM);
SetIndexBuffer(4, g_ibuf_116);
SetIndexStyle(5, DRAW_HISTOGRAM);
SetIndexBuffer(5, g_ibuf_120);
SetIndexStyle(6, DRAW_HISTOGRAM);
SetIndexBuffer(6, g_ibuf_124);
SetIndexStyle(7, DRAW_HISTOGRAM);
SetIndexBuffer(7, g_ibuf_128);
string ls_0 = "#PalladaSE_Bars©2009 TradeWays";
IndicatorShortName(ls_0);
return (0);
}
int start() {
double l_icci_8;
double l_iwpr_16;
double l_iforce_24;
int li_32;
//if (TimeCurrent() > StrToTime("2050.04.20")) {
// Alert("New version available! Download it using re-activated link from Plimus");
// return;
//}
//if (AccountNumber() != 0 && 0) {
// Alert("This expert is not licensed to your account number!");
// return;
//}
int l_ind_counted_4 = IndicatorCounted();
if (Bars <= 15) return (0);
if (l_ind_counted_4 < 1) {
for (int li_0 = 1; li_0 <= 15; li_0++) {
g_ibuf_100[Bars - li_0] = 0.0;
g_ibuf_108[Bars - li_0] = 0.0;
g_ibuf_104[Bars - li_0] = 0.0;
g_ibuf_112[Bars - li_0] = 0.0;
g_ibuf_116[Bars - li_0] = 0.0;
g_ibuf_124[Bars - li_0] = 0.0;
g_ibuf_120[Bars - li_0] = 0.0;
g_ibuf_128[Bars - li_0] = 0.0;
}
}
if (l_ind_counted_4 > 0) li_32 = Bars - l_ind_counted_4;
if (l_ind_counted_4 == 0) li_32 = Bars - 15 - 1;
for (li_0 = li_32; li_0 >= 0; li_0--) {
l_icci_8 = iCCI(NULL, 0, g_period_84, PRICE_TYPICAL, li_0);
l_iwpr_16 = iWPR(NULL, 0, g_period_92, li_0);
l_iforce_24 = iForce(NULL, 0, g_period_76, MODE_EMA, PRICE_CLOSE, li_0);
g_ibuf_100[li_0] = EMPTY_VALUE;
g_ibuf_108[li_0] = EMPTY_VALUE;
g_ibuf_104[li_0] = EMPTY_VALUE;
g_ibuf_112[li_0] = EMPTY_VALUE;
g_ibuf_116[li_0] = EMPTY_VALUE;
g_ibuf_124[li_0] = EMPTY_VALUE;
g_ibuf_120[li_0] = EMPTY_VALUE;
g_ibuf_128[li_0] = EMPTY_VALUE;
if (l_icci_8 > 0.0 && l_iwpr_16 > -50.0 && l_iforce_24 > 0.0) {
g_ibuf_100[li_0] = MathMax(Open[li_0], Close[li_0]);
g_ibuf_104[li_0] = MathMin(Open[li_0], Close[li_0]);
g_ibuf_116[li_0] = High[li_0];
g_ibuf_120[li_0] = Low[li_0];
} else {
if (l_icci_8 > 0.0 && l_iwpr_16 < -50.0) {
g_ibuf_108[li_0] = MathMax(Open[li_0], Close[li_0]);
g_ibuf_112[li_0] = MathMin(Open[li_0], Close[li_0]);
g_ibuf_124[li_0] = High[li_0];
g_ibuf_128[li_0] = Low[li_0];
} else {
if (l_icci_8 < 0.0 && l_iwpr_16 < -50.0 && l_iforce_24 < 0.0) {
g_ibuf_104[li_0] = MathMax(Open[li_0], Close[li_0]);
g_ibuf_100[li_0] = MathMin(Open[li_0], Close[li_0]);
g_ibuf_120[li_0] = High[li_0];
g_ibuf_116[li_0] = Low[li_0];
} else {
if (l_icci_8 < 0.0 && l_iwpr_16 > -50.0) {
g_ibuf_112[li_0] = MathMax(Open[li_0], Close[li_0]);
g_ibuf_108[li_0] = MathMin(Open[li_0], Close[li_0]);
g_ibuf_128[li_0] = High[li_0];
g_ibuf_124[li_0] = Low[li_0];
}
}
}
}
}
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
---