Here's a detailed explanation of the logic within the provided MetaTrader MQL4 script, presented in Markdown format. This explanation focuses on what the script does, not how to improve it.
Overview
The script, named "BullsBearsEyes," is a custom indicator designed to generate a series of values (likely intended for visual representation on a chart) based on a complex calculation involving bullish and bearish power calculations. It appears to be attempting to quantify the relative strength of bullish and bearish momentum over a defined period. The indicator is designed to be displayed in a separate window.
Input Parameters
The script utilizes several external input parameters that allow the user to customize its behavior:
periods
: This parameter defines the period used in theiBullsPower
andiBearsPower
functions. It likely controls the length of the moving average or other calculation used to determine bullish and bearish power.timeperiods
: This parameter is passed toiBullsPower
andiBearsPower
. Its purpose isn't immediately clear from the code snippet alone, but it likely influences the time frame considered for the bullish and bearish power calculations.gamma
: This parameter acts as a smoothing factor. It determines the weight given to the previous value (L0A
,L1A
, etc.) in the calculation of the current value (L0
,L1
, etc.). A lowergamma
value results in more smoothing.CountBars
: This parameter limits the number of bars the indicator will process. It prevents the indicator from calculating values for the entire chart history, which could be computationally expensive. It's capped to the number of available bars.
Variables
The script declares several variables, which can be categorized as follows:
L0
,L1
,L2
,L3
: These variables represent intermediate values in a recursive calculation. They appear to be part of a smoothed representation of bullish and bearish power.L0A
,L1A
,L2A
,L3A
: These variables store the previous values ofL0
,L1
,L2
, andL3
, respectively. They are used in the smoothing calculation.LRSI
: This variable is intended to represent a "relative strength index" (RSI)-like value, calculated from theCU
andCD
variables.CU
: This variable accumulates the difference between bullish and bearish power when the bullish power is greater.CD
: This variable accumulates the difference between bullish and bearish power when the bearish power is greater.val1[]
: This is an array that stores the calculatedLRSI
values. This array is used by the indicator to draw the values on the chart.
Functions
The script defines three primary functions: init()
, deinit()
, and start()
.
init()
: This function is called once when the indicator is initialized. It sets up the index buffer (val1
) for drawing the calculated values on the chart.deinit()
: This function is called once when the indicator is removed from the chart. It's currently empty, indicating that no cleanup is performed during deinitialization.start()
: This is the core function of the indicator. It performs the calculations and generates theLRSI
values.
Logic within start()
-
Bar Limit: The code first limits the number of bars processed to
CountBars
. -
Looping Through Bars: The code then loops backward through the chart bars, starting from
CountBars - 1
down to0
. -
Recursive Calculation: Inside the loop, the
L0
,L1
,L2
, andL3
values are calculated recursively. Each value is a weighted average of the previous value (L0A
,L1A
, etc.) and the result of theiBullsPower
andiBearsPower
functions. Thegamma
parameter controls the weighting. -
Calculating
CU
andCD
: TheCU
andCD
variables are calculated.CU
accumulates the difference when bullish power exceeds bearish power, andCD
accumulates the difference when bearish power exceeds bullish power. -
Calculating
LRSI
: TheLRSI
value is calculated as the ratio ofCU
to the sum ofCU
andCD
. This is analogous to the calculation of a standard RSI. -
Storing
LRSI
: The calculatedLRSI
value is stored in theval1[]
array at the current bar index.
iBullsPower
and iBearsPower
The script relies on two built-in functions, iBullsPower
and iBearsPower
. These functions are not defined within the script itself. They are assumed to be provided by the MetaTrader platform and are used to calculate bullish and bearish power for a given bar. The exact implementation of these functions is not visible in the provided code.
Overall Purpose
The indicator appears to be designed to provide a smoothed representation of the relative strength of bullish and bearish momentum, using a recursive calculation and a ratio-based metric (LRSI
) similar to an RSI. The iBullsPower
and iBearsPower
functions are crucial for determining the underlying bullish and bearish power values. The indicator's usefulness depends heavily on the specific implementations of those built-in functions.
//+-------------------------------------------------------------------------+
//| 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
---