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.
#propertyDirectives: 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 theiBullsPowerandiBearsPowerfunctions. 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 whenL0is greater thanL1,L1is greater thanL2, andL2is greater thanL3.CD: Represents the cumulative difference whenL1is greater thanL0,L2is greater thanL1, andL3is greater thanL2.val1[]: An array that will store the calculatedLRSIvalues 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 theval1array with the first index buffer of the indicator. This is necessary for the indicator to draw the data stored inval1on 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 thatCountBarsdoesn'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 isCountBarsbars away from the current bar.- The
whileloop iterates backward through the bars, starting fromCountBars - 1down to 0. - Inside the loop:
L0A = L0; ... L3A = L3;: Copies the current values ofL0throughL3into their "previous value" counterparts (L0AthroughL3A).L0 = ((1 - gamma) * (iBullsPower(...) + iBearsPower(...)) + gamma * L0);: This is the core calculation. It calculatesL0as a weighted average of the sum of bullish and bearish power for the current bar and the previous value ofL0.iBullsPowerandiBearsPowerare 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. CUandCDare calculated by summing the differences between theLvariables when the higherLvariable is greater than the lower one.LRSIis calculated based onCUandCD.val1[index] = LRSI;: The calculatedLRSIvalue is stored in theval1array 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