Help with Buy/Sell Positions in EA

 

Dear Forumers,

I am beginning my EA codng journey by using the sample Moving Average given in the default MT4 to modify some basic functions. However, i am facing a problem when I ran the Strategy Tester. It seems like the BUY trade is not happening and only SELL trades are occurring. Can anyone help me to identify what seems to be the issue?


//+------------------------------------------------------------------+
//| Check for open order conditions                                  |
//+------------------------------------------------------------------+
void CheckForOpen()
  {
   int    res;
//--- go trading only for first tiks of new bar
   if(Volume[0]>1) return;
   
//--- get Moving Average 
   double HA1 = iCustom(NULL,TrendPeriod,"Heiken Ashi MA",MAMethod,HAPeriod,0,0);

//--- sell conditions
     {
      if(Open[1]>HA1 && Close[1]<HA1)
      res=OrderSend(Symbol(),OP_SELL,0.1,Bid,3,0,0,"",MAGICMA,0,Red);
      return;
     }
     
//--- buy conditions
     {
      if(Open[1]<HA1 && Close[1]>HA1)
      res=OrderSend(Symbol(),OP_BUY,0.1,Ask,3,0,0,"",MAGICMA,0,Blue);
      return;
     }
     
     
//---
The Fundamentals of Testing in MetaTrader 5
The Fundamentals of Testing in MetaTrader 5
  • www.mql5.com
The idea of ​​automated trading is appealing by the fact that the trading robot can work non-stop for 24 hours a day, seven days a week. The robot does not get tired, doubtful or scared, it's is totally free from any psychological problems. It is sufficient enough to clearly formalize the trading rules and implement them in the algorithms, and...
 
tango1983:

Hello,

When you post code please use Alt+S

code_insert

 
Kenneth Parling:

Hello,

When you post code please use Alt+S



Done, thanks for the tip.

 
//+------------------------------------------------------------------+
//| Check for open order conditions                                  |
//+------------------------------------------------------------------+
void CheckForOpen()
  {
   bool res=false;
//--- go trading only for first tiks of new bar
   if(Volume[0]>1)
      return;

//--- get Moving Average
   double HA1 = iCustom(NULL,TrendPeriod,"Heiken Ashi MA",MAMethod,HAPeriod,0,0);

//--- sell condition
   if(Open[1]>HA1 && Close[1]<HA1)
      res=OrderSend(Symbol(),OP_SELL,0.1,Bid,3,0,0,"",MAGICMA,0,Red);
   if(!res)
     {
      Print("Order send error");
      return;
     }
//--- buy conditions
   if(Open[1]<HA1 && Close[1]>HA1)
      res=OrderSend(Symbol(),OP_BUY,0.1,Ask,3,0,0,"",MAGICMA,0,Blue);
   if(!res)
     {
      Print("Order send error");
      return;
     }
  }

i edited your code a little bit, try this one ;-)

 
Kenneth Parling:

i edited your code a little bit, try this one ;-)

Hi Kenneth, 

thanks for your prompt assist. i added the print command to help me differentiate the BUY and SELL order errors. Looks like there is no BUY order error at all. Instead, there were plenty of SELL Order errors. Very strange.\


void CheckForOpen()
  {
   bool res=false;
//--- go trading only for first tiks of new bar
   if(Volume[0]>1)
      return;

//--- get Moving Average
   double HA1 = iCustom(NULL,TrendPeriod,"Heiken Ashi MA",MAMethod,HAPeriod,0,0);

//--- sell condition
   if(Open[1]>HA1 && Close[1]<HA1)
      res=OrderSend(Symbol(),OP_SELL,0.1,Bid,3,0,0,"",MAGICMA,0,Red);
   if(!res)
     {
      Print("Sell Order send error");
      return;
     }
//--- buy conditions
   if(Open[1]<HA1 && Close[1]>HA1)
      res=OrderSend(Symbol(),OP_BUY,0.1,Ask,3,0,0,"",MAGICMA,0,Blue);
   if(!res)
     {
      Print("Buy Order send error");
      return;
     }
  }
 
tango1983:

Hi Kenneth, 

thanks for your prompt assist. i added the print command to help me differentiate the BUY and SELL order errors. Looks like there is no BUY order error at all. Instead, there were plenty of SELL Order errors. Very strange.\


void CheckForOpen()
  {
   bool res=false;
//--- go trading only for first tiks of new bar
   if(Volume[0]>1)
      return;

//--- get Moving Average
   double HA1 = iCustom(NULL,TrendPeriod,"Heiken Ashi MA",MAMethod,HAPeriod,0,0);

//--- sell condition
   if(Open[1]>HA1 && Close[1]<HA1)
     {
      res=OrderSend(Symbol(),OP_SELL,0.1,Bid,3,0,0,"",MAGICMA,0,Red);
      if(!res)
        {
         Print("Sell Order send error");
         return;
        }
     }
//--- buy conditions
   if(Open[1]<HA1 && Close[1]>HA1)
     {
      res=OrderSend(Symbol(),OP_BUY,0.1,Ask,3,0,0,"",MAGICMA,0,Blue);
      if(!res)
        {
         Print("Buy Order send error");
         return;
        }
     }
  }
//+------------------------------------------------------------------+

this one should work

 
  1. Why did you post your MT4 question in the Root / MT5 EA section instead of the MQL4 section, (bottom of the Root page?)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
    Next time post in the correct place. The moderators will likely move this thread there soon.

  2. if(Volume[0]>1) return;

    For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart,) volume is unreliable (miss ticks,) Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.

    I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
              New candle - MQL4 programming forum

  3. You are reading buffer zero from "Heiken Ashi MA." Do you expect us to know which specific version you are using, and where that can be found? There are no mind readers here and our crystal balls are cracked.

  4. double HA1 = iCustom(NULL,TrendPeriod,"Heiken Ashi MA",MAMethod,HAPeriod,0,0);
    On MT4: Unless the current chart is that specific symbol(s)/TF(s) referenced, you must handle 4066/4073 errors before accessing candle/indicator values.
              Download history in MQL4 EA - Forex Calendar - MQL4 programming forum - Page 3 #26 № 4

  5.  if(Open[1]>HA1 && Close[1]<HA1)
          res=OrderSend(Symbol(),OP_SELL,0.1,Bid,3,0,0,"",MAGICMA,0,Red);
       if(!res)
    Why are you looking at your res variable if you havent't tried to open?
 
William Roeder:
  1. Why did you post your MT4 question in the Root / MT5 EA section instead of the MQL4 section, (bottom of the Root page?)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
    Next time post in the correct place. The moderators will likely move this thread there soon.

  2. For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart,) volume is unreliable (miss ticks,) Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.

    I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
              New candle - MQL4 programming forum

  3. You are reading buffer zero from "Heiken Ashi MA." Do you expect us to know which specific version you are using, and where that can be found? There are no mind readers here and our crystal balls are cracked.

  4. On MT4: Unless the current chart is that specific symbol(s)/TF(s) referenced, you must handle 4066/4073 errors before accessing candle/indicator values.
              Download history in MQL4 EA - Forex Calendar - MQL4 programming forum - Page 3 #26 № 4

  5. Why are you looking at your res variable if you havent't tried to open?

Hi William, 

Thank you for your advise. I am still learning the coding and am taking the sample EA from the default MT4 to try to learn on the run. I will look into your tips and improve on the coding to make it work. 

Reason: