Author: Copyright � 2006, Keris2112

Your code snippet appears to be for a MetaTrader 4 Expert Advisor or script. It defines two main functions: the OnStart function and the GetCustomIndicatorValue function. Below is an explanation of each part of your code, along with some suggestions for improvement:

Explanation

  1. Global Variables:

    • extern string IndicatorName[4]; declares four external strings that are used to store names of custom indicators.
    • extern int LineNumber[4]; holds the line numbers of those indicators.
    • extern int Parameter1[4], Parameter2[4], Parameter3[4], Parameter4[4], Parameter5[4]; stores parameters for each indicator, allowing customization.
  2. OnStart Function:

    • This function is called when the script starts executing.
    • It retrieves values of all external variables using get_string, get_integer, and get_long.
    • The loop runs four times (i.e., once per indicator) to get the necessary data for each custom indicator.
  3. GetCustomIndicatorValue Function:

    • This function calculates the value of a custom indicator at a given shift.
    • It uses a switch statement (case) to handle different indicators based on their indices (0-3).
    • For each case, it checks which parameters are set (default is -100, indicating an unset parameter) and calls iCustom with appropriate arguments.
  4. iCustom Function:

    • This function retrieves values from custom indicators in MetaTrader.
    • The function call's structure changes based on how many parameters are set.

Suggestions for Improvement

  1. Code Clarity:

    • Use more descriptive variable names to improve readability, e.g., replace Parameter1 with indicatorParam1.
    • Consider adding comments within your code to explain complex logic or important steps.
  2. Error Handling:

    • Add error handling around the get_string, get_integer, and iCustom function calls to handle cases where data might not be retrieved correctly.
  3. Avoid Redundancy:

    • The switch statement contains repetitive code for calling iCustom. Consider creating a helper function to avoid redundancy.
  4. Dynamic Indicator Handling:

    • Instead of using hardcoded loops (e.g., for (int i = 0; i < 4; i++)), you might consider dynamically determining the number of indicators and parameters.
  5. Parameter Checking:

    • Simplify the parameter checks by creating a list or array to hold your indicator data, which can make it easier to iterate over them and call iCustom.

Here is an example of how you could refactor some parts for clarity:

void OnStart() {
    string indicators[4];
    int lines[4], params1[4], params2[4], params3[4], params4[4], params5[4];

    if (get_string(indicators)) {
        get_integer(lines);
        get_long(params1, 0);
        get_long(params2, 1);
        get_long(params3, 2);
        get_long(params4, 3);
        get_long(params5, 4);

        for (int i = 0; i < ArraySize(indicators); i++) {
            IndicatorName[i] = indicators[i];
            LineNumber[i] = lines[i];
            Parameter1[i] = params1[i];
            Parameter2[i] = params2[i];
            Parameter3[i] = params3[i];
            Parameter4[i] = params4[i];
            Parameter5[i] = params5[i];
        }
    }
}

double GetCustomIndicatorValue(int indicatorIndex, int shift) {
    double value;
    string name = IndicatorName[indicatorIndex];
    int line = LineNumber[indicatorIndex];

    // Collect parameters into an array
    int params[5] = {Parameter1[indicatorIndex], Parameter2[indicatorIndex],
                     Parameter3[indicatorIndex], Parameter4[indicatorIndex], 
                     Parameter5[indicatorIndex]};

    value = iCustom(NULL, 0, name, line, ArraySize(params) - 1);
    
    for (int i = 0; i < ArraySize(params); i++) {
        if (params[i] != -100)
            value = iCustom(NULL, 0, name, line, params[i], shift);
        else
            break;
    }

    return value;
}

These changes enhance readability and maintainability while preserving the original functionality.

Miscellaneous
Implements a curve of type %1
0 Views
0 Downloads
0 Favorites
#MTF_LPB_2
//+------------------------------------------------------------------+
//|                                             Linear Price Bar.mq4 |
//|                                      Copyright © 2006, Keris2112 |
//|                                                             none |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, Keris2112"
#property link      "none"

#property indicator_separate_window
#property indicator_buffers 4
#property indicator_color1 Blue
#property indicator_color2 Red
#property indicator_color3 Blue
#property indicator_color4 Red

//---- input parameters
/*************************************************************************
PERIOD_M1   1
PERIOD_M5   5
PERIOD_M15  15
PERIOD_M30  30 
PERIOD_H1   60
PERIOD_H4   240
PERIOD_D1   1440
PERIOD_W1   10080
PERIOD_MN1  43200
You must use the numeric value of the timeframe that you want to use
when you set the TimeFrame' value with the indicator inputs.
**************************************************************************/
extern int TimeFrame=0;
/*************************************************************************

DRAW_LINE      0     Drawing line. 
DRAW_SECTION   1     Drawing sections. 
DRAW_HISTOGRAM 2     Drawing histogram. 
DRAW_ARROW     3     Drawing arrows (symbols). 
DRAW_ZIGZAG    4     Drawing sections between even and odd indicator buffers. 
DRAW_NONE      12    No drawing.
**************************************************************************/ 


extern string Indicator_Name_1="Linear Price Bar";
extern int Line_1=0;
extern int Draw_1=2;
extern double Parameter1_1 = -100;
extern double Parameter2_1 = -100;
extern double Parameter3_1 = -100;
extern double Parameter4_1 = -100;
extern double Parameter5_1 = -100;

extern string Indicator_Name_2="Linear Price Bar";
extern int Line_2=1;
extern int Draw_2=2;
extern double Parameter1_2 = -100;
extern double Parameter2_2 = -100;
extern double Parameter3_2 = -100;
extern double Parameter4_2 = -100;
extern double Parameter5_2 = -100;

extern string Indicator_Name_3="Linear Price Bar";
extern int Line_3=2;
extern int Draw_3=2;
extern double Parameter1_3 = -100;
extern double Parameter2_3 = -100;
extern double Parameter3_3 = -100;
extern double Parameter4_3 = -100;
extern double Parameter5_3 = -100;

extern string Indicator_Name_4="Linear Price Bar";
extern int Line_4=3;
extern int Draw_4=2;
extern double Parameter1_4 = -100;
extern double Parameter2_4 = -100;
extern double Parameter3_4 = -100;
extern double Parameter4_4 = -100;
extern double Parameter5_4 = -100;
//---- buffers

double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
double ExtMapBuffer4[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {

//---- indicators
   SetIndexStyle(0,Draw_1);
   SetIndexBuffer(0,ExtMapBuffer1);
   SetIndexStyle(1,Draw_2);
   SetIndexBuffer(1,ExtMapBuffer2);
   SetIndexStyle(2,Draw_3);
   SetIndexBuffer(2,ExtMapBuffer3);
   SetIndexStyle(3,Draw_4);
   SetIndexBuffer(3,ExtMapBuffer4);
  
//---- name for DataWindow and indicator subwindow label   
   switch(TimeFrame)
   {
      case 1 : string TimeFrameStr="Period_M1"; break;
      case 5 : TimeFrameStr="Period_M5"; break;
      case 15 : TimeFrameStr="Period_M15"; break;
      case 30 : TimeFrameStr="Period_M30"; break;
      case 60 : TimeFrameStr="Period_H1"; break;
      case 240 : TimeFrameStr="Period_H4"; break;
      case 1440 : TimeFrameStr="Period_D1"; break;
      case 10080 : TimeFrameStr="Period_W1"; break;
      case 43200 : TimeFrameStr="Period_MN1"; break;
      default : TimeFrameStr="Current Timeframe";
   }
   IndicatorShortName("MTF_Linear_2 Price Bar ("+TimeFrameStr+")");   
  }
//----
   return(0);
 
//+------------------------------------------------------------------+
//| MTF Parabolic Sar                                         |
//+------------------------------------------------------------------+
int start()
  {
   datetime TimeArray[];
   int    i,limit,y=0,counted_bars=IndicatorCounted();

// Plot defined time frame on to current time frame
   ArrayCopySeries(TimeArray,MODE_TIME,Symbol(),TimeFrame); 
   
   limit=Bars-counted_bars;
   limit=Bars-counted_bars;
   for(i=0,y=0;i<limit;i++)
   {
   if (Time[i]<TimeArray[y]) y++;

/***********************************************************   
   Add your main indicator loop below.  You can reference an existing
      indicator with its iName  or iCustom.
   Rule 1:  Add extern inputs above for all neccesary values   
   Rule 2:  Use 'TimeFrame' for the indicator time frame
   Rule 3:  Use 'y' for your indicator's shift value
 **********************************************************/  
 
   ExtMapBuffer1[i]=GetIcustomValue(0,y);
   ExtMapBuffer2[i]=GetIcustomValue(1,y); 
   ExtMapBuffer3[i]=GetIcustomValue(2,y); 
   ExtMapBuffer4[i]=GetIcustomValue(3,y);  
   
   }  
   return(0);
  }
  
  double GetIcustomValue(int level ,int shift)
  {
      double value =0;
      
      switch(level)
      {
         case 1 :
         {  
            if(Parameter1_1==-100 && Parameter2_1==-100 && Parameter3_1==-100 && Parameter4_1==-100 && Parameter5_1==-100)
               value = iCustom(NULL,TimeFrame,Indicator_Name_1,Line_1,shift);
            if(Parameter1_1!=-100 && Parameter2_1==-100 && Parameter3_1==-100 && Parameter4_1==-100 && Parameter5_1==-100)
               value = iCustom(NULL,TimeFrame,Indicator_Name_1,Parameter1_1,Line_1,shift);
            if(Parameter1_1!=-100 && Parameter2_1!=-100 && Parameter3_1==-100 && Parameter4_1==-100 && Parameter5_1==-100)
               value = iCustom(NULL,TimeFrame,Indicator_Name_1,Parameter1_1,Parameter2_1,Line_1,shift);
            if(Parameter1_1!=-100 && Parameter2_1!=-100 && Parameter3_1!=-100 && Parameter4_1==-100 && Parameter5_1==-100)
               value = iCustom(NULL,TimeFrame,Indicator_Name_1,Parameter1_1,Parameter2_1,Parameter3_1,Line_1,shift);
            if(Parameter1_1!=-100 && Parameter2_1!=-100 && Parameter3_1!=-100 && Parameter4_1!=-100 && Parameter5_1==-100)
               value = iCustom(NULL,TimeFrame,Indicator_Name_1,Parameter1_1,Parameter2_1,Parameter3_1,Parameter4_1,Line_1,shift);
            if(Parameter1_1!=-100 && Parameter2_1!=-100 && Parameter3_1!=-100 && Parameter4_1!=-100 && Parameter5_1!=-100)
               value = iCustom(NULL,TimeFrame,Indicator_Name_1,Parameter1_1,Parameter2_1,Parameter3_1,Parameter4_1,Parameter5_1,Line_1,shift);
            break;
         }
      
         case 2 :
         {  
            if(Parameter1_2==-100 && Parameter2_2==-100 && Parameter3_2==-100 && Parameter4_2==-100 && Parameter5_2==-100)
               value = iCustom(NULL,TimeFrame,Indicator_Name_2,Line_2,shift);
            if(Parameter1_2!=-100 && Parameter2_2==-100 && Parameter3_2==-100 && Parameter4_2==-100 && Parameter5_2==-100)
               value = iCustom(NULL,TimeFrame,Indicator_Name_2,Parameter1_2,Line_2,shift);
            if(Parameter1_2!=-100 && Parameter2_2!=-100 && Parameter3_2==-100 && Parameter4_2==-100 && Parameter5_2==-100)
               value = iCustom(NULL,TimeFrame,Indicator_Name_2,Parameter1_2,Parameter2_2,Line_2,shift);
            if(Parameter1_2!=-100 && Parameter2_2!=-100 && Parameter3_2!=-100 && Parameter4_2==-100 && Parameter5_2==-100)
               value = iCustom(NULL,TimeFrame,Indicator_Name_2,Parameter1_2,Parameter2_2,Parameter3_2,Line_2,shift);
            if(Parameter1_2!=-100 && Parameter2_2!=-100 && Parameter3_2!=-100 && Parameter4_2!=-100 && Parameter5_2==-100)
               value = iCustom(NULL,TimeFrame,Indicator_Name_2,Parameter1_2,Parameter2_2,Parameter3_2,Parameter4_2,Line_2,shift);
            if(Parameter1_2!=-100 && Parameter2_2!=-100 && Parameter3_2!=-100 && Parameter4_2!=-100 && Parameter5_2!=-100)
               value = iCustom(NULL,TimeFrame,Indicator_Name_2,Parameter1_2,Parameter2_2,Parameter3_2,Parameter4_2,Parameter5_2,Line_2,shift);
            break;
         }
      
         case 3 :
         {  
            if(Parameter1_3==-100 && Parameter2_3==-100 && Parameter3_3==-100 && Parameter4_3==-100 && Parameter5_3==-100)
               value = iCustom(NULL,TimeFrame,Indicator_Name_3,Line_3,shift);
            if(Parameter1_3!=-100 && Parameter2_3==-100 && Parameter3_3==-100 && Parameter4_3==-100 && Parameter5_3==-100)
               value = iCustom(NULL,TimeFrame,Indicator_Name_3,Parameter1_3,Line_3,shift);
            if(Parameter1_3!=-100 && Parameter2_3!=-100 && Parameter3_3==-100 && Parameter4_3==-100 && Parameter5_3==-100)
               value = iCustom(NULL,TimeFrame,Indicator_Name_3,Parameter1_3,Parameter2_3,Line_3,shift);
            if(Parameter1_3!=-100 && Parameter2_3!=-100 && Parameter3_3!=-100 && Parameter4_3==-100 && Parameter5_3==-100)
               value = iCustom(NULL,TimeFrame,Indicator_Name_3,Parameter1_3,Parameter2_3,Parameter3_3,Line_3,shift);
            if(Parameter1_3!=-100 && Parameter2_3!=-100 && Parameter3_3!=-100 && Parameter4_3!=-100 && Parameter5_3==-100)
               value = iCustom(NULL,TimeFrame,Indicator_Name_3,Parameter1_3,Parameter2_3,Parameter3_3,Parameter4_3,Line_3,shift);
            if(Parameter1_3!=-100 && Parameter2_3!=-100 && Parameter3_3!=-100 && Parameter4_3!=-100 && Parameter5_3!=-100)
               value = iCustom(NULL,TimeFrame,Indicator_Name_3,Parameter1_3,Parameter2_3,Parameter3_3,Parameter4_3,Parameter5_3,Line_3,shift);
            break;
         }
         
         case 4 :
         {  
            if(Parameter1_4==-100 && Parameter2_4==-100 && Parameter3_4==-100 && Parameter4_4==-100 && Parameter5_4==-100)
               value = iCustom(NULL,TimeFrame,Indicator_Name_4,Line_4,shift);
            if(Parameter1_4!=-100 && Parameter2_4==-100 && Parameter3_4==-100 && Parameter4_4==-100 && Parameter5_4==-100)
               value = iCustom(NULL,TimeFrame,Indicator_Name_4,Parameter1_4,Line_4,shift);
            if(Parameter1_4!=-100 && Parameter2_4!=-100 && Parameter3_4==-100 && Parameter4_4==-100 && Parameter5_4==-100)
               value = iCustom(NULL,TimeFrame,Indicator_Name_4,Parameter1_4,Parameter2_4,Line_4,shift);
            if(Parameter1_4!=-100 && Parameter2_4!=-100 && Parameter3_4!=-100 && Parameter4_4==-100 && Parameter5_4==-100)
               value = iCustom(NULL,TimeFrame,Indicator_Name_4,Parameter1_4,Parameter2_4,Parameter3_4,Line_4,shift);
            if(Parameter1_4!=-100 && Parameter2_4!=-100 && Parameter3_4!=-100 && Parameter4_4!=-100 && Parameter5_4==-100)
               value = iCustom(NULL,TimeFrame,Indicator_Name_4,Parameter1_4,Parameter2_4,Parameter3_4,Parameter4_4,Line_4,shift);
            if(Parameter1_4!=-100 && Parameter2_4!=-100 && Parameter3_4!=-100 && Parameter4_4!=-100 && Parameter5_4!=-100)
               value = iCustom(NULL,TimeFrame,Indicator_Name_4,Parameter1_4,Parameter2_4,Parameter3_4,Parameter4_4,Parameter5_4,Line_4,shift);
            break;
         }
      }
       
      return (value);

  }
//+------------------------------------------------------------------+

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---