Enjoy Interactive Tools and Indicators

Friday, September 7, 2012

3.1 Repainting and not-repainting indicators. Custom indicator: the initialization order
Do you really need to see something painted by your custom indicator? Probably not, if your goal is to write a profitable EA, completely automatic. Probably yes, when you need some signs on the charts(signals)to do semi-automatic trading or just manual trading. Some sounds and even emails are other options to receive the signals. First of all, how much can you draw using one custom indicator? Good question. For Metatrader4 you can draw up to eight indicator lines using one custom indicator. That's the limit. There are some concepts to be assimilated before you even think to make your custom indicator a Picasso. Take a look at the following picture:
What is the most frequent word you saw in the picture? Is buffer. If you don't have programming background grab a chair. A buffer is a region of a physical memory storage used to temporarily hold data while it is being moved from one "place" to another, between processes, but bottom line, it's typically used when there is a difference between the rates of the data received and the data consumed. Either the program that consumes data is waiting for more data and you need a buffer to be filled and then pass the data from the buffer filled or the program is receiving too much data and you have to establish a pace depending on how much data can be read at once without breaking the program. When the buffer is filled with too much information you have buffer overflow and that's usually bad programming because the buffer it suppose to solve this problem and not to become the same problem itself. Usually, for the buffer implementation we are using an array to have the buffer data accessible using an index. You got the picture :
custom indicator --> indicator lines(<=8 lines)--> buffers for indicators lines --> arrays for indicator buffers
//--------------------------------------------------------------------
// userindicator.mq4 
// The code should be used for educational purpose only.
//--------------------------------------------------------------------

//---- indicator settings
#property indicator_chart_window    // Indicator is drawn in the main window
#property indicator_buffers 2       // Number of buffers
#property indicator_color1 Blue     // Color of the 1st line
#property indicator_color2 Red      // Color of the 2nd line 

 double Buffer0[],Buffer1[];             // Declaring arrays (for indicator buffers)
//--------------------------------------------------------------------
int init()                          // Special function init()
{    
 SetIndexBuffer(0,Buffer0);// Assigning an array to a buffer: mapping           
 SetIndexStyle (0,DRAW_LINE,STYLE_SOLID,2);// Line style

 SetIndexBuffer(1,Buffer1);         // Assigning an array to a buffer: mapping
 SetIndexStyle (1,DRAW_LINE,STYLE_DOT,1);// Line style   
 
 return;// Exit the special funct. init()  
 }
//--------------------------------------------------------------------
 int start()                         // Special function start()
 {   int i,                           // Bar index
     Counted_bars;                // Number of counted bars
//--------------------------------------------------------------------
  Counted_bars = IndicatorCounted(); // Number of counted bars
  i = Bars-Counted_bars-1;           // Index of the first uncounted bar
  while(i >= 0)                      // Loop for uncounted bars
  {      Buffer0[i] = High[i];       // Value of 1st buffer on i bar
         Buffer1[i] = Low[i];              // Value of 2nd buffer on i bar
    i--;                          // Calculating index of the next bar     
  }
//--------------------------------------------------------------------
  return;                          // Exit the special funct. start() 
        }
//--------------------------------------------------------------------
Let's nail down the sections that will be common to all scripts, customer indicators and expert adviser. The understanding of these special sections is crucial for future coding.
 //+------------------------------------------------------------------+
//|                                                 barenaked.mq4 |
//|                                Copyright @ 2012 aimetatraders |
//+------------------------------------------------------------------+
#property  copyright "Copyright @ 2012 Aimetatraders galactic corporation"
#property  link      "http://aimetatraders.blogspot.com"
#property indicator_separate_window

//---- indicator settings

//---- indicator parameters

//---- indicator buffers (global declaration)


Print("This is global declaration place");


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
  //---- drawing settings
  
  //---- indicator buffers mapping ( initialization of the buffers)
  
  //---- initialization done
  
   Print("Code: Initialize!");
   return(0);
  }
  
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//---- TODO: add your code here
   
//----
   Print("Code: Deinitialize!");
   return(0);
  }

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   Print("Code: Start before counting!");
   int limit;
   int counted_bars=IndicatorCounted();
//---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;  
   limit=Bars-counted_bars;
   Print("Code: Start after counting!");
    
   return(0);
  }
//+------------------------------------------------------------------+
The special functions are returning in the case of success always 0. Attaching the indicator to a chart and then change the chart period. Let's see how the special functions are called: First, our intention is to add this customer indicator, barenaked on the chart:
Drag and drop the indicator barenaked, you can observe that the tab Experts appeared on the status bar.
When adding an indicator, pay attention how the special functions are called:
1)First the global declaration place is analyzed (looking for the global declarations)
2)init() special function is called
3)start() is beginning the iteration
Interesting things are happening when you change the chart period: 1) First the deinit() special function is called
2) Then the global declaration area is analyzed
3) is the init() time, special function init() is called
4) the iteration is starting in start() special function begins (you can notice here that because the number of candles/bars modified the start() function "iterates" many times -meaning is called many times)
Now, let's say we want this indicator out: just delete the custom indicator from the chart (press Delete button).
The console is again explicit:
only the deinit() special function is called upon exit.
So, now we have a clue about the order how the special functions are called and when:
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
  int init(){
   //TODO
   return(0);
  }
The purpose of the special function init() in the case of a custom indicator is to have some code executed immediately after:
* the client terminal starts (when you start Metatrader),
* you change the chart period (M1,M5,M15,M30 etc),
* after you recompile the program in MetaEditor.
* after changing some parameters of the indicator (we will get to that)
 
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
  int deinit(){
   //TODO
   return(0);
  }
The purpose of the special function deinit() in the case of a customer indicator when the indicator is to have code when the custom indicator is removed from the chart. Also, when changing the chart period deinit() is called again. There is a limit of the execution for this special function deinit(): the execution should not be more than 2.5 seconds otherwise the customer indicator will be removed forcibly with the stopping of the execution of the deinit().
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
 int start(){
   //TODO
   return(0);
  }
The purpose of the start() function is to hold your business logic, why you wanted to have this customer indicator in the first place. Iteration is the keyword here: after a new tick comes (new quotes) this function is executed (your algorithms) - moreover,start() is called every time a new tick comes Obviously the execution of start()will be:
* when you attached your custom indicator to the chart,
* next time execution: if you attached your customer indicator to the chart and you close the terminal, next time when you open the terminal, close the client terminal
* when you change the chart period etc.
You can see that there is a zone before these special functions, where you defined:
 
//---- indicator settings

//---- indicator parameters

//---- indicator buffers (global declaration)
Declaring the buffers' arrays before the special functions is related to visibility. These arrays should be global (in global area), declared before any other functions.The declarations should take place before any special function to be seen and used from the rest of the code: the special functions or any other functions you may write just to help your business logic code(from start()) or initialization or deinitialization of your customer indicator.
  double Buffer0[],Buffer1[]; //declaration of the buffers' arrays
As you may know, when you are declaring you are not giving initial values, you are not mapping these arrays to some values. This is taken place in the init() function body. To be continued.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.