MACD, again

 

Hi again,  

 OK, still having problems on something that should be a piece of cake.

please see attached screenshot for a start.  All I'm trying to do is code a simple EA to go long on macd cross over and go short when it crosses over the other way. But I seem to be having some really silly trades going through, the odd one seems successful but in the most part, it seems the trade in the wrong direction.

As Im quite new to MQL my EA was starting to look better quality in terms of condition handling and error handling etc, but as I've got this stupid problem I decided to start over again with the very basics, so the code shown below is somewhat primitive and I appreciate probably not the best way to achieve what Im doing. Yes I could pinch someone elses MACD code but I wont learn about MQL doing that.

 Appreciate any thoughts. also why am I seeing people declare variables in start(), surely they must get re-declared on each new tick? or create endless clones?

 (code few posts down)

 

 
idh:

Hi again,  

 OK, still having problems on something that should be a piece of cake.

please see attached screenshot for a start.  All I'm trying to do is code a simple EA to go long on macd cross over and go short when it crosses over the other way. But I seem to be having some really silly trades going through, the odd one seems successful but in the most part, it seems the trade in the wrong direction.

As Im quite new to MQL my EA was starting to look better quality in terms of condition handling and error handling etc, but as I've got this stupid problem I decided to start over again with the very basics, so the code shown below is somewhat primitive and I appreciate probably not the best way to achieve what Im doing. Yes I could pinch someone elses MACD code but I wont learn about MQL doing that.

 Appreciate any thoughts. also why am I seeing people declare variables in start(), surely they must get re-declared on each new tick? or create endless clones?

 

<CODE DELETED >

Please edit your post . . .    please use the   SRC   button to post code: How to use the   SRC   button. 
 
//+------------------------------------------------------------------+
//|                                                      MACDian.mq4 |
//|                        Copyright 2013, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2013, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"
bool LPositionOpen= FALSE;
bool SPositionOpen = FALSE;
int ticket;
double MACD, MACDPrevious;
double MACDSignal, MACDSignalPrevious;
double MACDHistogram, MACDHistogramPrevious;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
MACD = iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,0);
MACDSignal = iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,0);
MACDHistogram = MACD - MACDSignal;

MACDPrevious = iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1);
MACDSignalPrevious = iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1);
MACDHistogramPrevious = MACDPrevious - MACDSignalPrevious;





if( MACD > MACDSignal && MACDPrevious < MACDSignalPrevious)
{
   if(SPositionOpen == TRUE)
   {
      OrderClose(ticket, 100,Ask,0,Red);
      SPositionOpen = FALSE;
      return(0);
   }
   
   if(LPositionOpen == FALSE)
   {
      ticket = OrderSend(Symbol(),OP_BUY,100,Ask,0,0,0,NULL,0,Green);
      LPositionOpen=TRUE;
      return(0);
   }
}

if( MACD < MACDSignal && MACDPrevious > MACDSignalPrevious )
{
   if(LPositionOpen == TRUE)
   {
      OrderClose(ticket, 100,Bid,0,Green);
      LPositionOpen = FALSE;
      return(0);
   }
   
   if(SPositionOpen == FALSE)
   {
      ticket = OrderSend(Symbol(),OP_SELL,100,Bid,0,0,0,NULL,0,Red);
      SPositionOpen=TRUE;
      return(0);
   }
}



   
//----
   return(0);
  }
//+------------------------------------------------------------------+
Files:
macdian.mq4  3 kb
 
idh:

Hi again,  

 OK, still having problems on something that should be a piece of cake.

please see attached screenshot for a start.  All I'm trying to do is code a simple EA to go long on macd cross over and go short when it crosses over the other way. But I seem to be having some really silly trades going through, the odd one seems successful but in the most part, it seems the trade in the wrong direction.

What were the values of   MADC and MACDSignal at 16:11  ?  not 16:00 or 17:00,  but at 16:11 ?  you are using the values for bar 0 and those values can change from tick to tick to tick . . .  now you are looking at the completed chart which shows the values for the closed bar and you say "the trade in the wrong direction !"  well you can't know from looking at the completed bars,  what was the situation when the trade was placed at 16:11 ?

MACD = iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,   0    );
MACDSignal = iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,   0   );
 

Right ok thanks, I understand what you're saying. I'll do some debugging using print command to dump out vars and come back tomorrow.

As a quick test I set the bars to 1 and 2, so not using 0 and I things are looking better already. Is there a better way to do this, 3 bars behind if more reliable but slower. Andrew youngs book has a bit of code that checks for a new bar before doing any checks, so the checks are only done once.  

cheers 

 
idh:

Right ok thanks, I understand what you're saying. I'll do some debugging using print command to dump out vars and come back tomorrow.

As a quick test I set the bars to 1 and 2, so not using 0 and I things are looking better already. Is there a better way to do this, 3 bars behind if more reliable but slower. Andrew youngs book has a bit of code that checks for a new bar before doing any checks, so the checks are only done once.  

cheers 

If you use any bars other than bar 0 the values will not change,  just using older bars is no more or less reliable than bars 1 and 2.
 

Hi

Not sure I quite follow;

"If you use any bars other than bar 0 the values will not change"  I think you're saying that if I use the closed (or other price such as Open) on the previous bar in my calculation, the calc for MACD wont flip around so wildly, it's more stable.

 

" just using older bars is no more or less reliable than bars 1 and 2"  when I say reliable, I mean to prevent whipsawing as macd fluctuates above and below signal, by checking previous closed bar and bar prior to that, you would count just one cross over (in most cases) rather than say 10.

 

Have I missed something ? sometimes I have difficulty following one liners :-) 

 
idh:

Hi

Have I missed something ? sometimes I have difficulty following one liners :-) 

Essentially you got what I was trying to say.  One more thing,  don't confuse a closed bar with the close price.  All bars except bar 0 are closed,  bar 0 never closes,  instead it becomes bar 1.  Close[0] = Bid.
 

Hi Raptor


Looks like you were right, at 16:11 MACD was below signal so a short was entered when the chart was going the other way. However I'm puzzled, if you take a look at the attached logfile output, you can see my code thinks MACD is still below the signal all the way to 18:48 where you see this entry:

 2013.04.29 18:48  MACDian UK100,M5: MACD is ABOVE signal

Yet the MACD indictor shows the opposite, so my if check must be flawed ?

 

(note: the test was using current macd on Bar 0, and previous macd set to 1 bar behind) 

Files:
 
idh: (note: the test was using current macd on Bar 0, and previous macd set to 1 bar behind) 

On the first tick of a new bar macd 1 and macd 0 are almost identical. A slight downturn would trigger a sell, when the macd 0 moves below it signal. (signal moves much less)

Stop using bar zero for your trigger.

Reason: