Newbie: How to visually display the EA at work?

 

Hi All,

This is my first post on this forum so pls forgive me if I am talking about something well-known or asking the wrong questions...

I am trying to write an EA based on the example in Mql 4- MetaTrader 4 Development Course and then "see it" in action in Metatrades's strategy tester.

OK, so my idea is simple so far: I want to open a BUY trade when EMA crosses the price line from below... Then when viewing this on the graph I would actually want to see that this has happened!

My code is as follows:

I have a function which checks for two lines if they have crossed:

int Crossed (double line1, double line2)
{
//static means that variables hold values between repeated calls
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) //changed
{
last_direction = current_dirction;
return (last_direction);
}
else
{
return (0);
}
}

Then, in my start function I have:

int start()
{
//----
int cnt,ticket,total;
double lineEMA;

total = OrdersTotal();

lineEMA = iMA(NULL,0,12,0,MODE_EMA,PRICE_CLOSE,0);
int isCrossed = Crossed (lineEMA,Ask);

//So, if my MA - lineEMA crosses the Ask price I want to open a BUY trade:

if(total < 1)
{
if(isCrossed == 1)
{
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,"My EA - BUY",12345,0,Green);
// the above should give us the ticket number but if fails it will be eg. -1
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)==true)
Print("BUY order opened : ",OrderOpenPrice());
}
else Print("Error opening BUY order : ",GetLastError());
return(0);
}
if(isCrossed == 2)
{
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,Bid-TakeProfit*Point,"My EA - SELL",12345,0,Red);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
Print("SELL order opened : ",OrderOpenPrice());
}
else Print("Error opening SELL order : ",GetLastError());
return(0);
}
// the following exit when the lines have not crossed yet!
return(0);
}

When I run this in my MT Strategy Tester I can see the open trade but it is nowhere near the EMA line...

Can anyone help a beginner with this?

Regards,

Pawel.

 
  1. MT4 charts are bid charts, the EMA is the ma of Bid. Try Crossed(Bid, lineEMa)

  2. total = OrdersTotal();
    if(total < 1)
    This makes the EA incompatible with any other including itself on other charts.
    total=0;
        for(pos = OrdersTotal()-1; pos >= 0 ; pos--) if (
            OrderSelect(pos, SELECT_BY_POS)                 // Only my orders w/
        &&  OrderMagicNumber()  == magic.number             // my magic number
        &&  OrderSymbol()       == Symbol() ){              // and my pair.
            total++;
        }
    if (total<0)

  3. OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,Bid-TakeProfit*Point
    EAs must adjust for 5 digit brokers, TP, SL, AND slippage. On ECN brokers you must open the order and THEN set stops.
    //++++ These are adjusted for 5 digit brokers.
    int     pips2points;    // slippage  3 pips    3=points    30=points
    double  pips2dbl;       // Stoploss 15 pips    0.0015      0.00150
    int     Digits.pips;    // DoubleToStr(dbl/pips2dbl, Digits.pips)
    int     init(){
        if (Digits == 5 || Digits == 3){    // Adjust for five (5) digit brokers.
                    pips2dbl    = Point*10; pips2points = 10;   Digits.pips = 1;
        } else {    pips2dbl    = Point;    pips2points =  1;   Digits.pips = 0; }
        // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl
    

  4. if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)==true)
    if(true==true) is redundant