Newbie can not interpret the EA chart.

 

I typed in Coder's Guru Expert Advisor and ran it and I don't understand the final results on the graph. I modified the following lines from his original and the EA shows that I had two profitable trades between 2009 /08/ 28 to 2009/08/30 but I had 7 crossings of the moving averages. Also, what does the red / blue connecting and dash lines mean when the EA draws them? Why would he put some of the code after the return(0) in the int deinit() function, how can the computer read this code??? Thank you for the HELP.

shortEma = iMA(NULL,0,14,0,MODE_EMA,PRICE_OPEN,0); longEma = iMA(NULL,0,14,0,MODE_EMA,PRICE_CLOSE,0);

//+------------------------------------------------------------------+
//| My_First_EA.mq4 |
//| Tigger |
//| https://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Tigger"
#property link "https://www.metaquotes.net/"

//---- input parameters
extern double TakeProfit=200.0;
extern double Lots=0.1;
extern double TrailingStop=30.0;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----

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

//----
return(0);
}
int Crossed (double line1, double line2)
{
static int last_direction = 0;
static int current_dirction =0;

if(line1 > line2) current_dirction = 1; // up
if(line1 < line2) current_dirction = 2; // down



if(current_dirction != last_direction)
{
last_direction = current_dirction;
return(last_direction);
}
else
{
return(0);
}
} // int Crossed (double line1, double line2)
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{ // 1 Begin of Start

int cnt, ticket, total;
double shortEma, longEma;

if(Bars < 100)
{ // 2
Print("bars less than 100");
return(0);
} // 2
if(TakeProfit < 10)
{ //3
Print("TakeProfit less than 10");
return(0); // check TakeProfit
} //3



shortEma = iMA(NULL,0,14,0,MODE_EMA,PRICE_OPEN,0);
longEma = iMA(NULL,0,14,0,MODE_EMA,PRICE_CLOSE,0);

int isCrossed = Crossed (shortEma, longEma);

total = OrdersTotal();

if(total < 1)
{ //4
if(isCrossed == 1)
{ //5


ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit+Point,"My EA =",12345,0,Green);
if(ticket > 0)
{ //6
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
Print("Buy order opened :",OrderOpenPrice());
} //6
else Print("Error opening BUY order : ",GetLastError());
return(0);
} // 5 if(isCrossed == 1)


if(isCrossed == 2)
{ // 7
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,Bid-TakeProfit*Point," My EA =",12345,0,Red);
if(ticket > 0)
{ //8 begin

if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
Print("Select order opened : ",OrderOpenPrice());
} // 8 if(ticket > 0)
else Print("Error opening Sell order : ",GetLastError());
return(0);
} // 7 if(isCrossed == 2);
} // 4 if(total < 1)

for (cnt = 0; cnt < total; cnt++)
{ //9
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType() <= OP_SELL && OrderSymbol() == Symbol())
{ //10
if(OrderType() == OP_BUY) // Long position is Open
{ //11
// should it be closed

if(isCrossed == 2)
{ //12
OrderClose(OrderTicket(), OrderLots(),Bid,3,Violet);
// Close Position
return(0); // exit
} // 12 if(isCrossed == 2)
if(TrailingStop > 0)
{ //13
if(Bid-OrderOpenPrice() > Point*TrailingStop)
{ // 14
if(OrderStopLoss() < Bid-Point*TrailingStop)
{ //15
OrderModify(OrderTicket(), OrderOpenPrice(), Bid - Point*TrailingStop, OrderTakeProfit(),0,Green);
return(0);

} // 15 if(OrderStopLoss() < Bid-Point*TrailingStop)
} // 14 if(Bid-OrderOpenPrice() > Point*TrailingStop)
} // 13 if(TrailingStop > 0)
} // 10 if(isCrossed == 2)

else // go to short position
{ // 16
// should it be closed

if(isCrossed == 1)
{ //17
OrderClose(OrderTicket(), OrderLots(), Ask,3,Violet);
// closed position
return(0); // exit

} // 17 if(isCrossed == 1)
if(TrailingStop > 0)
{ // 18
if((OrderOpenPrice()-Ask) > (Point*TrailingStop))
{ // 19
if((OrderStopLoss() > (Ask+Point+TrailingStop)) || (OrderStopLoss() == 0))
{ //20
OrderModify(OrderTicket(), OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red);
return(0);

} // 20 if(OrderStopLoss() > (Ask+Point&TrailingStop)) ||
} // 19 if(OrderOpenPrice()-Ask) > (Point*TrailingStop))
} // 18 if(TrailingStop > 0)
} // 16 else // go to short position
} // 9 if(OrderType() == OP_BUY) // Long position is Open ++++++

}// 9 for (cnt = 0,cnt < total; cnt++)

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

 

Tigger,


Have you properly read the documentation and looked through the sample code to fully appraise yourself of how simple EAs work, line by line? I think not. Your question about the code following the deinit() function exposes an absolutely fundamental mis-understanding with regard to the structure of an EA. You do need to clear this up before progressing.


Each function block in the code is independent and it makes no difference where in the mq4 file each function appears.

The init() function is executed once on initialization of the EA.

The start() function is executed on each incoming tick (when idle).

The deinit() function will run for a maximum of 2500 milliseconds on uninitialization of the EA.

Other functions will run when they are called by a calling function which could be start(), init(), deinit() or any other function.

Return passes back to the calling function with the return() statement, unless in the case of init(), start() or deinit() in which case return() passes control back to the platform.


CB

 
cloudbreaker wrote >>

Tigger,

Have you properly read the documentation and looked through the sample code to fully appraise yourself of how simple EAs work, line by line? I think not. Your question about the code following the deinit() function exposes an absolutely fundamental mis-understanding with regard to the structure of an EA. You do need to clear this up before progressing.

Each function block in the code is independent and it makes no difference where in the mq4 file each function appears.

The init() function is executed once on initialization of the EA.

The start() function is executed on each incoming tick (when idle).

The deinit() function will run for a maximum of 2500 milliseconds on uninitialization of the EA.

Other functions will run when they are called by a calling function which could be start(), init(), deinit() or any other function.

Return passes back to the calling function with the return() statement, unless in the case of init(), start() or deinit() in which case return() passes control back to the platform.

CB

cloudbreaker, thanks for your comment. I have read a lot of documentation and don't remember seeing what you said about max time in the == deinit() function. thanks for filling in this piece of information. My confusion is once the =denit() function is called and it excecutes the first return(0) then it returns to the calling function but how does the rest of the code in the =deini() is excecuted? If, I understand it right even afer if the calling function calls the =deini() again it would return to the calling function upon excecution the first return(0) as it is shown above. That is the piece code that I am not clear about. Apperently it does get Iexcecuted. ===== The other piece of the mis-understanding is when I changed the line === shortEma = iMA(NULL,0,14,0,MODE_EMA,PRICE_OPEN,0); longEma = iMA(NULL,0,14,0,MODE_EMA,PRICE_CLOSE,0); === it made 7 crossing but only showed two trades? The best way would is to attach a picture. I tried to attach a picture using ms paint it was the wrong file extension. Is is possible to attach a screen shot directly from metatrader? if not, how else? Thank you,

 

"Functions of start(), init(), and deinit() can be called from any point of the module according to the common rules, equally to other functions."

 

"My confusion is once the =denit() function is called and it excecutes the first return(0) then it returns to the calling function but how does the rest of the code in the =deini() is excecuted?"


There IS no further code in the deinit() function. In fact, there is no code in it at all (apart from the return() statement).

The next piece of code is the Crossed() function.

Followed by the start() function.


CB

 
phy wrote >>

"Functions of start(), init(), and deinit() can be called from any point of the module according to the common rules, equally to other functions."

Phy thanks for the reply. When the function gets called it needs to start of the function then go down. please see below. Thanks

//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit() /*
{
//----

//----
return(0); */ So when it reaches this return it goes back to the calling program
}
int Crossed (double line1, double line2) /* BUt you are saying that the calling program could start here??? Is that what you are saying????
{
static int last_direction = 0;
static int current_dirction =0;

if(line1 > line2) current_dirction = 1; // up
if(line1 < line2) current_dirction = 2; // down



if(current_dirction != last_direction)
{
last_direction = current_dirction;
return(last_direction);
}
else
{
return(0);
}
} // int Crossed (double line1, double line2)

 
tigger wrote >>

Phy thanks for the reply. When the function gets called it needs to start of the function then go down. please see below. Thanks

//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit() /*
{
//----

//----
return(0); */ So when it reaches this return it goes back to the calling program
}
int Crossed (double line1, double line2) /* BUt you are saying that the calling program could start here??? Is that what you are saying????
{
static int last_direction = 0;
static int current_dirction =0;

if(line1 > line2) current_dirction = 1; // up
if(line1 < line2) current_dirction = 2; // down



if(current_dirction != last_direction)
{
last_direction = current_dirction;
return(last_direction);
}
else
{
return(0);
}
} // int Crossed (double line1, double line2)

Thank both you for clearing this up for me. I thought that the cross() function was part of the == = int deinit() thank you very much.

Reason: