Okay, here's a detailed explanation of the logic within the provided MQL4 script, BullsBearsEyes.mq4
. I will focus on the what and how of the code's operation, without attempting to fix or improve it.
Overall Purpose
The script appears to be a custom indicator designed to generate a value (likely representing a "BullsBears Eyes" metric) based on a series of calculations involving bullish and bearish power, and a weighted average. It aims to provide a visual representation of the relative strength of bullish and bearish momentum.
1. Preprocessing and Properties
- Copyright and Link: Standard copyright and link information.
#property
Directives: These directives define the indicator's characteristics:indicator_separate_window
: Indicates that the indicator will be displayed in a separate window.indicator_minimum
,indicator_maximum
: Sets the minimum and maximum values for the indicator's scale.indicator_color1
: Sets the color of the indicator line.indicator_level2
-indicator_level6
: Defines levels for horizontal lines on the indicator window, likely used for visual reference.
- Input Parameters (Extern Variables):
periods
: A numerical value used within theiBullsPower
andiBearsPower
functions. It likely represents the period for calculating the bullish and bearish power.timeperiods
: A numerical value, likely related to the time frame used for the power calculations.gamma
: A smoothing factor (between 0 and 1). It controls the weight given to the current calculation versus the previous value in the weighted average. A value closer to 0 gives more weight to the current value, while a value closer to 1 gives more weight to the previous value.CountBars
: The number of bars to be processed by the indicator. It's limited to the number of bars available in the chart.
2. Variable Declarations
L0
,L1
,L2
,L3
: These variables store intermediate values in a series of weighted averages. They are likely representing different stages of a smoothing process.L0A
,L1A
,L2A
,L3A
: These variables store the previous values ofL0
,L1
,L2
, andL3
, respectively. They are used in the weighted average calculations.LRSI
: Likely stands for "Relative Strength Index" (or a similar concept). It represents the ratio of bullish to bearish power.CU
: Represents the cumulative difference whenL0
is greater thanL1
,L1
is greater thanL2
, andL2
is greater thanL3
.CD
: Represents the cumulative difference whenL1
is greater thanL0
,L2
is greater thanL1
, andL3
is greater thanL2
.val1[]
: An array that will store the calculatedLRSI
values for each bar. This array is used to draw the indicator line on the chart.
3. init()
Function
- This function is called once when the indicator is initialized.
SetIndexBuffer(0, val1)
: This line associates theval1
array with the first index buffer of the indicator. This is necessary for the indicator to draw the data stored inval1
on the chart.- The function returns 0, indicating successful initialization.
4. deinit()
Function
- This function is called once when the indicator is removed from the chart.
- It's currently empty, meaning no specific cleanup is performed.
- The function returns 0, indicating successful deinitialization.
5. start()
Function
- This is the core function where the calculations are performed. It's called repeatedly as new data becomes available.
if (CountBars > Bars) CountBars = Bars;
: Ensures thatCountBars
doesn't exceed the number of bars available on the chart.SetIndexDrawBegin(0, Bars - CountBars);
: Sets the starting point for drawing the indicator line on the chart. It starts drawing from the bar that isCountBars
bars away from the current bar.- The
while
loop iterates backward through the bars, starting fromCountBars - 1
down to 0. - Inside the loop:
L0A = L0; ... L3A = L3;
: Copies the current values ofL0
throughL3
into their "previous value" counterparts (L0A
throughL3A
).L0 = ((1 - gamma) * (iBullsPower(...) + iBearsPower(...)) + gamma * L0);
: This is the core calculation. It calculatesL0
as a weighted average of the sum of bullish and bearish power for the current bar and the previous value ofL0
.iBullsPower
andiBearsPower
are likely built-in functions that calculate the bullish and bearish power for a given period.- Similar weighted average calculations are performed for
L1
,L2
, andL3
. CU
andCD
are calculated by summing the differences between theL
variables when the higherL
variable is greater than the lower one.LRSI
is calculated based onCU
andCD
.val1[index] = LRSI;
: The calculatedLRSI
value is stored in theval1
array at the appropriate index.
In Summary:
The indicator calculates a series of smoothed values (L0
- L3
) based on bullish and bearish power. These smoothed values are then used to calculate a relative strength index (LRSI
), which is stored in an array and displayed on the chart. The gamma
parameter controls the smoothing effect, and the iBullsPower
and iBearsPower
functions are crucial for determining the bullish and bearish momentum. The CU
and CD
variables are used to determine the relative strength of the bullish and bearish momentum.
//+-------------------------------------------------------------------------+
//| BullsBearsEyes.mq4 |
//| EmeraldKing , transport_david |
//| http://finance.groups.yahoo.com/group/MetaTrader_Experts_and_Indicators/|
//+-------------------------------------------------------------------------+
#property copyright "Copyright © 2005"
#property link "http://finance.groups.yahoo.com/group/MetaTrader_Experts_and_Indicators/"
#property indicator_separate_window
#property indicator_minimum -0.05
#property indicator_maximum 1.05
#property indicator_color1 Orange
#property indicator_level2 1.0
#property indicator_level3 0.75
#property indicator_level4 0.50
#property indicator_level5 0.25
#property indicator_level6 0.0
//---- input parameters
extern double periods=13;
extern double timeperiods=0;
extern double gamma=0.6;
extern int CountBars=300;
double L0 = 0;
double L1 = 0;
double L2 = 0;
double L3 = 0;
double L0A = 0;
double L1A = 0;
double L2A = 0;
double L3A = 0;
double LRSI = 0;
double CU = 0;
double CD = 0;
double val1[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
//----
SetIndexBuffer(0,val1);
return(0);
}
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//---- TODO: add your code here
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
if (CountBars>Bars) CountBars=Bars;
SetIndexDrawBegin(0,Bars-CountBars);
int i;
int counted_bars=IndicatorCounted();
i=CountBars-1;
while(i>=0)
{
L0A = L0;
L1A = L1;
L2A = L2;
L3A = L3;
L0 = ((1 - gamma)*(iBullsPower(NULL, timeperiods, periods,PRICE_CLOSE,i)
+iBearsPower(NULL, timeperiods, periods,PRICE_CLOSE,i))) + (gamma*L0A);
L1 = - gamma *L0 + L0A + gamma *L1A;
L2 = - gamma *L1 + L1A + gamma *L2A;
L3 = - gamma *L2 + L2A + gamma *L3A;
CU = 0;
CD = 0;
if (L0 >= L1) CU = L0 - L1; else CD = L1 - L0;
if (L1 >= L2) CU = CU + L1 - L2; else CD = CD + L2 - L1;
if (L2 >= L3) CU = CU + L2 - L3; else CD = CD + L3 - L2;
if (CU + CD != 0) LRSI = CU / (CU + CD);
val1[i] = LRSI;
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
---