How to make your own expert?

expert advisor
Expert Advisor

You have chosen a set of rules and you want to create your own expert.

These rules should include a condition of entry, exit condition, number of trades, open at the same time. When you press the button  Create New , select Expert Advisor and Meta Trader will generate an initial code.

It looks like this:

int init()
{
//----

//----
return(0);
}
//+---------------------------------------------------------+
//| expert deinitialization function |
//+-------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}
//+------------------------------------------------------+
//| expert start function |
//+-------------------------------------------------+
int start()
{
//----

//----
return(0);
}
//+---------------------------------------------------+ 

This initial code generates three main functions used by Meta Trader. These are init (), deinit () and start (). You need to describe what makes each of them (you can leave blank spaces for the first two).

– In the function init () you need to describe what follows to happen on the first call of the expert. Here, you can set initial values of variables, parameters, to create new files, etc.

– In the function deinit(), you need to describe what follows to happen on the last call of the expert.

– The function start () is the call of each new quotation arrived. In this function you should describe your logic.

There are many options to realize the same logic. Usually the logic looks like this:

  1. Verification of the signal that opens a new position
    You need to check if there are open positions, because, even your strategy includes more than one position, you will not be willing to open a deal on every tick (the experts would be called on every tick).
  2. Verification if there is a signal for closing open positions / pending orders
    This check may be absent if the positions are closing on limit and stop levels.

In this situation, your start function should look like this:

int start() {
If there is a condition of opening the positions  (this is a general condition, for example – not to have open positions)
{
If there is a condition for the long
{
opening of the long/ placing of the pending order
}

If there is a condition for short
{
opening of the short/placing of the pending order
}
}

Navigating of the open positions
{
If the position is long
{
If there is condition for closing
{
Closing of the position
}
}

If the position is short
{
If there is condition for closing
{
Closing of the position
}
}

} 

As an example we are going to realize on that structure the logic that buys, when the price becomes higher than the highest one for the previous bar, and sells when the price becomes lower than the lowest one from the previous bar. It holds maximum one open position, and it closes on a former signal.

int start() // Beginning of the <em>Start Function</em>
{

if (OrdersTotal()<1) // a condition not to have open positions
{
if(Ask>High[1]) //condition for long entrance
{
OrderSend(Symbol(),OP_BUY,0.1 ,Ask,0,0,0,"",10101,0,Blue); //buy
}

if(Bid<Low[1]) //condition of short entrance
{
OrderSend(Symbol(),OP_SELL,0.1 ,Bid,0,0,0,"",10101,0,Red); //sell
}
}

for(int cnt=0; cnt<=OrdersTotal()-1; cnt++) // Navigating of all orders and positions
if(OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES)) // Navigating of all orders and positions
if(OrderSymbol() == Symbol()) // Navigating of all orders and positions
{
if(OrderType()==OP_BUY) //if the position is long
{
if(Bid<Low[1]) //if there is a condition for closing
{
OrderClose(OrderTicket(), OrderLots(), Bid, 1,Red); //closing of long position
}
}

if(OrderType()==OP_SELL) //If the position is short
{
if(Ask>High[1]) //if there is a condition for closing
{
OrderClose(OrderTicket(), OrderLots(), Ask, 1,Red); //Closing of the short position
}
}

} //End of position navigating

return(0); //End of the <em>Start Function</em>
} 

Like this post? Please share to your friends:
ForexZig - Professional forex trading