Enjoy Interactive Tools and Indicators

Monday, October 7, 2013

Return with a vengeance.

It's been more than one year since nothing new was added to this blog-to-be-a-training. The market became more and more a traders' thing, volatile and dumb. This is why is important for all the traders to have new weapons that is to develop new strategies, new indicators and new robots.

Sunday, September 9, 2012

3.2 Repainting and not-repainting indicators. Custom indicator: make it paint

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.

Wednesday, September 5, 2012

2.2. Where you are now: the big picture

You are in front of the terminal client, trying to make sense from the chaos the market is made of. Your dealing center or your broker is there also, you connect to them to put your orders on market and they give ticks or quotes, materialized in the bars, candlesticks or simply a moving line on a chart (quotes = the cost of one currency expressed in terms of another currency) The left part of the picture is where you will spend your mql programming time and is representing the application you just worked with: the editor.

Tuesday, September 4, 2012

2.1 The making of "Hello World" custom indicator: it's not AI

The reason for this chapter is to get familiar with the usage of Metalang or Metaquotes language editor. We assume that you install your Metatrader terminal and you made an account, you logged in and now you are staring at the markets as they are ticking. Usually, you did it the script kiddie's way: you took an indicator from the web and you copied under the ...experts\indicators folder, you started Metatrader again and then, you did the drag & drop move to put the indicator on the chart. Sometimes it works and it shows you some signals but usually the market is evolving and it's changing its DNA and the signals are no longer good. Or no signal at all? Or just some mumbo-jumbo hard to comprehend? Now you want to solve this problem by understanding what the indicators are really doing. And not only the indicators! Any decent expert adviser is relying on the signals provided by indicators: some indicators are custom, some are predefined by the guys who built the Metatrader platform. So, you are in Metatrader client terminal. That's right, is a client for the Metatrader server installed on your broker side (between you and the market). What you see is a customized Metatrader client terminal to have some icon or name of the broker. Some brokers are offering in-house made indicators. Whatever... Either you press F4 or you go to Tools/MetaQuotes Language Editor the result will be the same:
There you have it. Now you follow the steps of the File/New or you press the icon with the plus sign. The result is the following (just choose Custom Indicator check button before you click Next):
Now it's your choice to sign the customer indicator details with the indicator name which is important, otherwise you will not pass the Next step.
So, you have to give a name for the indicator: in this case "Hello World" seems perfectly reasonable. The other details like "Author" and "Link" will not influence at all how the customer indicator will perform: are just some informations to make you feel better about what your are doing:
You see also some parameters table which is scary at this point. What parameters? So, just pretend you know what you are doing that you don't need any parameters and click again "Next". Then, you see something again scarier if you are a beginner in this:
Just check the "Indicator in separate window". That means that your indicator window will appear in a separate window than the market is on Metatrader. Instead of another "Next" you got this time "Finish". Press it with confidence and look at your creation:
You can see on the Navigator window (you are still in Metalang editor) if you expand the indicators your custom indicator. Now move your eyes to the code. It's difficult, right? You didn't right any of these lines and yet it's there and it's generated. You can recognize though, the name: "Hello World" but with a funny extension : mq4. That's the reason you are looking at it in Metalang editor, 'cause it's a source file and it needs to be compiled. If you move a little bit higher your eyes (can you take your eyes of your creation?) you see in the toolbar the magic word "Compile". You need to compile your source code because Metatrader terminal doesn't understand this readable mq4 form as you have in Metalang. And the result will be "Hello World.mq4" ---> "Hello World.ex4" Press Compile and cross your fingers :
It should be perfect: no errors and no warnings. If you look on right, on the Navigator little window you don't see anything. Where is your Hello World.ex4 file? In the moment it was compiled successfully, your Metatrader client terminal is able to see it(and to use it). Just expand the Metatrader's Navigator Custom Indicator this time and not the Metalang's Navigator. You go back to the market staring again:
Now you can do what you did until now with the indicators you picked up from the web: drag and drop the Hello World custom indicator on the market screen. It has its separate window as you ask it to have(remember?) and:
Press the tab named "Experts" (from the status bar of the Metatrader) and you will see some info: your indicator loaded successfully and it's initialized. But is not doing anything! Because you programmed it like this? To do nothing. To have some message on the console we have to go back to the Metalang editor. You didn't close it, right? If you did, just press F4 like before and you have again the source code of your custom indicator named "Hello World". Just for you to sleep well you need this indicator to do something, to write something at the console at least... and that's exactly the purpose of all "Hello World"'s in the programming languages tutorials : to give you confidence to continue because it's a long road to Forbes magazine. If you take out all the comments from the code (the lines starting with //) you can see it better:
#property copyright "Copyright 2012, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

#property indicator_separate_window

int init()
  {

   return(0);
  }

int deinit()
  {

   return(0);
  }

int start()
  {
   int    counted_bars=IndicatorCounted();

    Print("Hello World");

   return(0);
  }
So, basically you have one line which somehow tells Metatrader that you want for this indicator to have its own window:
#property indicator_separate_window
And then you have 3 functions: init(), deinit(), start() returning happily 0 . In the body of the start(), meaning between the { }, just add this line of code (something intelligent ending with ;)
Print("Hello World");
What you have to do now? Don't look yet at Metatrader terminal, you didn't finish the job: you have to compile your code. You modified it, right? Metatrader doesn't know about your genius line of code that can break the markets. Just press Compile: Again 0 errors and 0 warnings and if you look now on the Experts tab from Metatrader you customer indicator is printing the "Hello World" message continuously.
This concludes the introduction.

Monday, September 3, 2012

2. Introduction should be always short 


How much you understand from the code below? It's MetaQuotes Language(MQL) code. If you have some programming experience you know that this is C-like code. If you don't have any programming experience this might be your time to learn something. If you always wanted to understand what indicators and EA's are doing and maybe to write your own stuff you are in the right place. Cutting to the chase: there is a lot of information regarding programming indicators and expert advisers (and scripts!) out there but is either too technical and assumes that you are working for Goldman-Sachs or it's for retards and it stops always when it should continue. The process of learning needs practice more than theory and something like "Thinking in MQL" was never done before. This is our intention.
//+------------------------------------------------------------------+
//|                                                   HelloWorld.mq4 |
//|                        Copyright 2012, Aimetraders               |
//|                                http://aimetatraders.blogspot.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2012, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

#property indicator_separate_window
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int counted_bars=IndicatorCounted();
//----
   string myString = StringConcatenate("counting bars: ",counted_bars);
   Print(myString);
//----
   return(0);
  }
//+------------------------------------------------------------------+
The result at the "console": the experts tab is showing the indicator named Hello World counting bars as they are drawn by the Metatrader:
Every tutorial in any programming language has to start with its own "Hello World" example. They say that MQL is an algorithmic language. We strongly disagree: it's as algorithmic as your native language. More of the making of "Hello World" next time.

Thursday, August 30, 2012

1. Getting real : money matters.
If you read this blog it means that you went in your tiny, little vacation, you felt like a king
 for a couple of days and you came back to work feeling fortunate that you have a job.
You noticed during your vacation some people being in vacation for couple of months already.
You find out that they are working during a year as much you have vacation and they
have vacation as long as you are working. They are rich, you said. Lottery winners.
They are not.The lottery winners already lost their money buying and doing stuff they don't need it.
These guys you met are rich by themselves. Part of them are entrepreneurs, part of them investors,
part of them independent traders. They have one thing in common, they like to invest, they like to multiply their money and not to preserve the money as you were told by the others like you. They play on the markets. And they are doing it with the same stress as you have when you are playing tennis or poker with your friends. They are the winners. They are just enjoying their time. The rest of world is in the rat race doing the same shit every day and dreaming about the next vacation in the process.

This blog is dedicated to trading. We will start a blog dedicated to all traders using Metatrader platform and we will add some AI (artificial intelligence stuff) on the way. We will grow slowly and hopefully we will reach our next vacation in a position where we can stay longer as we all really want to.