Help with my first EA

 

Hello, I have recently started exploring mql4 language and here is my first, very simple EA.
For a starter I tried to emulate this candle pattern: http://www.candlesticker.com/Cs59.asp
But when it comes to testing, I have no results at all. Did I miss something?

I was wondering if anyone would be willing to give me some hints/comments and point out my mistakes. Big thanks in advance.

//+------------------------------------------------------------------+
//| bullish morning star                                             |
//+------------------------------------------------------------------+

//+--- external options ---------------------------------------------+
extern double lots = 0.1;
extern int takeProfit = 50;
extern int stopLoss = 50;
extern int maxPositions = 1;

//+--- expert init and deinit ---------------------------------------+
int init()
  {
   return(0);
  }
//+-------------------------------+
int deinit()
  {
   return(0);
  }

//+--- functions ----------------------------------------------------+
bool findBMS()
{
   if((downtrend() == true) &&
      (Open[3] - Close[3] >= 200*Point) &&
      (Open[2] < Close[3]) &&
      (Close[2] < Close[3]) &&
      (Close[1] - Open[1] >= 120*Point) &&
      (Open[1] > Open[2]) &&
      (Open[1] > Close[2]) &&
      (Open[1] < Close[3]))
      {
      return(true);
      }
   return(false);
}

bool downtrend()
{
   if (Open[6] > Close[4])
      {
      return(true);
      }
   return(false);
}

bool isAvailableOrder()
{
   if (OrdersTotal() < maxPositions)
      {
      return(true);
      }
   return(false);
}

//+--- expert start function ----------------------------------------+

int start()
{
   if (isAvailableOrder())
   {
      if (findBMS())
      {
         OrderSend(Symbol(), OP_BUY, lots, Ask, 0, Ask-stopLoss*Point, Ask+takeProfit*Point);
      }
   }
   return(0);
}
 

Maybe your OrderSend is being called but fails . . . you need to check return values from functions . . . then if it fails you can report the error back to the journal/expert tab via Print(). With an ECN Brkoer you have to send the order with SL & TP = 0.0 then do an OrderModify . . . ECN

 
Thanks. How do I "check return values from functions"?
 
remyg:
Thanks. How do I "check return values from functions"?

LOL: You are already doing it with your isAvailableOrder function when you do

 if (isAvailableOrder())

You should check and report when happens when you do the OrderSend

int value = OrderSend(Symbol(), OP_BUY, lots, Ask, 0, Ask-stopLoss*Point, Ask+takeProfit*Point);
if( value < 0 ){
  Comment("That didn't work. WTF? Error=" + GetLastError() );
}

Or you could use a print rather than a comment.

 

This is too complicated to understand and debug ...

bool findBMS()
{
   if((downtrend() == true) &&
      (Open[3] - Close[3] >= 200*Point) &&
      (Open[2] < Close[3]) &&
      (Close[2] < Close[3]) &&
      (Close[1] - Open[1] >= 120*Point) &&
      (Open[1] > Open[2]) &&
      (Open[1] > Close[2]) &&
      (Open[1] < Close[3]))
      {
      return(true);
      }
   return(false);
}

What I would recommend is having just one test at a time. If it fails the test at any stage then return false. This allows you to put a print in before the return so you know where the program gets to and why it is not working.

 
if (OrdersTotal() < maxPositions)
will fail if other EA has opened a trade or if there are pending and/or manual trades open
 

OK, thanks all for comments, you are very helpful.
But I am clearly missing something and it drives me crazy.

So I decided to take a few steps back and create the most basic program there can be. It is supposed to check for 5 points difference and cast buy or sell order.
There should be plenty of positions open, but still, I plug it into strategy tester and nothing :/

Please tell me what the heck am I doing wrong, thanks.

//+--- external options ---------------------------------------------+
extern double lots = 0.1;
extern int takeProfit = 100;
extern int stopLoss = 30;
extern int maxPositions = 1;
//+--- expert init and deinit ---------------------------------------+
int init()
   {return(0);}
//+-------------------------------+
int deinit()
   {return(0);}
//+--- functions ----------------------------------------------------+
bool up()
   {if (Close[1] - Open[1] > 5*Point)
      {return(true);}
   return(false);}

bool down()
   {if (Open[1] - Close[1] > 5*Point)
      {return(true);}
   return(false);}
//+--- expert start function ----------------------------------------+
int start()
   {if (up())
      {OrderSend(Symbol(), OP_BUY, lots, Ask, 0, Ask-stopLoss*Point, Ask+takeProfit*Point);}
   else if (down())
      {OrderSend(Symbol(), OP_SELL, lots, Ask, 0, Ask-stopLoss*Point, Ask+takeProfit*Point);}
   return(0);}

BTW, as you can see, I have folded the code as much as possible. Parentheses can be a huge pain in the ass for me since the only other programming language I was learning is super-neat Python

 
remyg:


So I decided to take a few steps back and create the most basic program there can be. It is supposed to check for 5 points difference and cast buy or sell order.
There should be plenty of positions open, but still, I plug it into strategy tester and nothing :/

Please tell me what the heck am I doing wrong, thanks.

It seems highly unlikely that it is doing "nothing". I tried it and got a Journal log full of OrderSend Error 130

You are doing both BUY and SELL at ASK, which should be giving you errors. Likewise your stoploss is wrong on one of those orders.

Put some print statements in here and there so you can see which parts of the code are being exercised.

PS: init and deinit are optional so you can omit them if you are not using them.

 

This should be enough to get you going again ...

extern double lots = 0.1;
extern int takeProfit = 100;
extern int stopLoss =   100;
extern int maxPositions = 1;
//+--- expert init and deinit ---------------------------------------+
int init()
   {return(0);}
//+-------------------------------+
int deinit()
   {return(0);}
//+--- functions ----------------------------------------------------+
bool up()
   {if (Close[1] - Open[1] > 5*Point)
      {return(true);}
   return(false);}

bool down()
   {if (Open[1] - Close[1] > 5*Point)
      {return(true);}
   return(false);}
//+--- expert start function ----------------------------------------+
int start(){

   static datetime lastBar = 0;
   
   if( lastBar==Time[0] )
      return( 0 );
   
   lastBar = Time[0];

   if (up())
      {OrderSend(Symbol(), OP_BUY, lots, Ask, 0, Ask-stopLoss*Point, Ask+takeProfit*Point);}
   //else if (down())
   //   {OrderSend(Symbol(), OP_SELL, lots, Ask, 0, Ask-stopLoss*Point, Ask+takeProfit*Point);}
   return(0);}

Trading not more than once per bar now rather than every tick.

 

Alright, this will be one nooby question: how do I access journal log? :)

ps. wow thanks for the edit!

 

OFFS!




CLICK THE JOURNAL TAB

Reason: