Been struggling for 3 weeks to get this basic EA to work - please help

 

Hi Everyone

 

While not new to trading, I am new to programming.  I want to learn how to do it and I do have the MQL4 book and reading it.  I thought that I would start with something really simple and basic and use this as a base to build a more sophisticated EA, but I cannot even get this EA to work properly.  I have been struggling for 3 weeks now, and realized it is time to ask for help.  I simply want the EA to open both a buy and sell order at the same time, but only do so when no other orders are open.  And I want it to open the orders when the closing price of the current bar is higher than the closing price of the previous bar.  It appears to me as if the EA ignores this and places the order as soon as I add it to the chart.  And then some other times it appears as if it won't trade at all.  There are no warnings or errors.  Will someone please, please take a look for me.  I want to learn.  Thank you.  Gary

//+------------------------------------------------------------------+
//| mo_bidir_v0_1.mq4                                                |
//|                                                                  |
//| - Works best in 5M timeframe                                     |
//| - Bug fix to stop_loss in line 22 2010.04.07                     |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010 - Monu Ogbe"

#define MAGIC  1234
#define IDENT  "mo_bidir_v0_1"

extern double  lots           = 0.01;
extern double  stop_loss      = 80;   // (8 pips) optimise 50-2000
extern double  take_profit    = 750;  // (75 pips) optimise 50-2000

 int    ticket,total;
 
int start(){
   total=OrdersTotal();
   if(total<1)
     {
     if(Close[0]>Close[1])
         ticket=OrderSend(Symbol(), OP_BUY, lots ,Ask, 3, Ask - stop_loss * Point, Bid + take_profit * Point, IDENT, MAGIC, 0, Blue);
         ticket=OrderSend(Symbol(), OP_SELL, lots ,Bid, 3, Bid + stop_loss * Point, Ask - take_profit * Point, IDENT, MAGIC, 0, Red);
   } 
   return(0);
}
 

This isn't the original one - correct?

So compare your changes against the original.

You can even start the debugger to see what if-clause might be the reason...

Learn mql4 by reading the references--

 
     if(Close[0]>Close[1])
         ticket=OrderSend(Symbol(), OP_BUY, lots ,Ask, 3, Ask - stop_loss * Point, Bid + take_profit * Point, IDENT, MAGIC, 0, Blue);
         ticket=OrderSend(Symbol(), OP_SELL, lots ,Bid, 3, Bid + stop_loss * Point, Ask - take_profit * Point, IDENT, MAGIC, 0, Red);
  1. Why to you open a buy when Bid > last candle close but always open a sell?
  2. Check your return codes. What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
 
gooly:

This isn't the original one - correct?

So compare your changes against the original.

You can even start the debugger to see what if-clause might be the reason...

Learn mql4 by reading the references--

Thank you for your reply.  No its not the original code.  Here is the original code:

//| mo_bidir_v0_1.mq4                                                |
//|                                                                  |
//| - Works best in 5M timeframe                                     |
//| - Bug//+------------------------------------------------------------------+
 fix to stop_loss in line 22 2010.04.07                     |
//+------------------------------------------------------------------+
#property copyright "Copyright ɠ2010 - Monu Ogbe"

#define MAGIC  1234
#define IDENT  "mo_bidir_v0_1"

extern double  lots           = 1;
extern double  stop_loss      = 80;   // (8 pips) optimise 50-2000
extern double  take_profit    = 750;  // (75 pips) optimise 50-2000

int            last_bar       = 0;

int start(){
   if (last_bar == Bars) return(0);
   last_bar = Bars;
   if (OrdersTotal() == 0){
         OrderSend(Symbol(), OP_BUY, lots ,Ask, 3, Ask - stop_loss * Point, Bid + take_profit * Point, IDENT, MAGIC, 0, Blue);
         OrderSend(Symbol(), OP_SELL, lots ,Bid, 3, Bid + stop_loss * Point, Ask - take_profit * Point, IDENT, MAGIC, 0, Red);
   } 
   return(0);
}

 

I basically replaced the last bar function under int start with (Close[0]>Close[1] because I do not want the EA to indiscriminately open a buy and sell order as soon as I attach the EA to the chart.  Instead I want to add my own priceaction parameters as per the insert in post #1.  At the moment it completely ignores the closing prices and still opens a buy trade as soon as I attach the EA.  It ignores the function I added.  Do I need to define what "Close" means as a Global variable, perhaps?

This modified code returns no errors or warnings.  I also ran the debug function as suggested (thanks), and there were no errors in the journal.  

 Thank you also for the links to the return functions, but mine appear to be correct because there are no errors/ warnings.

 

Thank you for any help 

 
Asked and answered: "Why to you open a buy when Bid > last candle close but always open a sell?"
if(Close[0]>Close[1])
         ticket=OrderSend(Symbol(), OP_BUY, lots ,Ask, 3, Ask - stop_loss * Point, Bid + take_profit * Point, IDENT, MAGIC, 0, Blue);
         ticket=OrderSend(Symbol(), OP_SELL, lots ,Bid, 3, Bid + stop_loss * Point, Ask - take_profit * Point, IDENT, MAGIC, 0, Red);
Compare the two versions. There are two differences. One is "Close[0]>Close[1]"
   if (OrdersTotal() == 0){
         OrderSend(Symbol(), OP_BUY, lots ,Ask, 3, Ask - stop_loss * Point, Bid + take_profit * Point, IDENT, MAGIC, 0, Blue);
         OrderSend(Symbol(), OP_SELL, lots ,Bid, 3, Bid + stop_loss * Point, Ask - take_profit * Point, IDENT, MAGIC, 0, Red);
   }
 
Trader3000:

Hi Everyone

 

I simply want the EA to open both a buy and sell order at the same time, but only do so when no other orders are open.  And I want it to open the orders when the closing price of the current bar is higher than the closing price of the previous bar.  It appears to me as if the EA ignores this and places the order as soon as I add it to the chart.  

 

Hi, just add brackets "{..}" as below and try again:

     if(Close[0]>Close[1])
     {
         ticket=OrderSend(Symbol(), OP_BUY, lots ,Ask, 3, Ask - stop_loss * Point, Bid + take_profit * Point, IDENT, MAGIC, 0, Blue);
         ticket=OrderSend(Symbol(), OP_SELL, lots ,Bid, 3, Bid + stop_loss * Point, Ask - take_profit * Point, IDENT, MAGIC, 0, Red);
      }
 

Thank you WHRoeder for pointing that out.  I did see the issue with the brackets and thank you jollydragon for pointing that out as well.  I have added the brackets and also added another trade condition.  It compiles with no errors and two warnings namely that the return value of both the Ordersend functions should be checked.  So as it stands, no trades are being placed even if any of the trade conditions are met (candle closing higher or lower).  Thank you very much for your help so far ...... it feels as if I'm on the brink of a breakthrough in my learning process.

//+------------------------------------------------------------------+
//| mo_bidir_v0_1.mq4                                                |
//|                                                                  |
//| - Works best in 5M timeframe                                     |
//| - Bug fix to stop_loss in line 22 2010.04.07                     |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010 - Monu Ogbe"

#define MAGIC  1234
#define IDENT  "mo_bidir_v0_1"

extern double  lots           = 0.01;

 int    ticket,total;
 
int start(){
   total=OrdersTotal();
   if(total<1)
     {
     if(Close[0]>Close[1])    //
         OrderSend(Symbol(), OP_BUY, lots ,Ask, 3, 0, 0, IDENT, MAGIC, 0, Blue);
         
         if(Close[0]<Close[1])    //
         OrderSend(Symbol(), OP_SELL, lots ,Bid, 3, 0, 0, IDENT, MAGIC, 0, Red);
   } 
   return(0);
}
 
Trader3000 It compiles with no errors and two warnings namely that the return value of both the Ordersend functions should be checked. 
Asked and answered (#2)
 
WHRoeder:
Trader3000 It compiles with no errors and two warnings namely that the return value of both the Ordersend functions should be checked. 
Asked and answered (#2)

Okay thank you for your advise.  I will study those materials.  I want to try and figure it out by myself.  Best way to learn.  It will take a few days, so I will report back next week.  Thanks
 
I have now read and studied all the materials and found much more info.  I have modified my code accordingly and it now looks like this below.  It compiles with no errors or warnings, however, it seems to ignore the trade functions for when to open a trade based on closing price.  It appears as if it indiscriminately opens a trade regardless of closing prices.  Is there still something wrong?  Thanks
//+------------------------------------------------------------------+
//| mo_bidir_v0_1.mq4                                                |
//|                                                                  |
//| - Works best in 5M timeframe                                     |
//| - Bug fix to stop_loss in line 22 2010.04.07                     |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010 - Monu Ogbe"

#define MAGIC  1234
#define IDENT  "mo_bidir_v0_1"

extern double  lots           = 0.01;

 int    result,total;
 
int start(){
   total=OrdersTotal();
   if(total<1)
     {
     if(Close[0]>Close[1])
         result=OrderSend(Symbol(), OP_BUY, lots ,Ask, 3, 0, 0, IDENT, MAGIC, 0, Blue);
         
     if(Close[0]<Close[1])
         result=OrderSend(Symbol(), OP_SELL, lots ,Bid, 3, 0, 0, IDENT, MAGIC, 0, Red);
   } 
   return(0);
}
 

Close[0] is the current Bid price. Bar[0] hasn't closed yet so it can only be the last available price

It may be that what you are actually trying to do is

     if(Close[1]>Close[2])
         result=OrderSend(Symbol(), OP_BUY, lots ,Ask, 3, 0, 0, IDENT, MAGIC, 0, Blue);

 which is the last completed bar closed higher than the previous bar's close

Reason: