ma crossover problem

 

Dear All,

I tried to write a 2 ma crossover ea. It seems I don't get into making trades for some reason - can anyone help by looking at my code which is very short and explain what the problem could be please?

//+------------------------------------------------------------------+
//|                                                            X.mq4 |
//|                        Copyright 2013, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2013, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"
double ma_period_slow = 8;
double ma_period_fast = 5;
extern double LotSize=0.1;
extern double StopLoss=100;
extern double TakeProfit=200;
extern int MagicNumber=1234;
double pips = 0.00001;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {


//----
double fastmaprevious,slowmaprevious,fastmanow,slowmanow,fastmaafter,slowmaafter;

fastmaafter = iMA(Symbol(),0,ma_period_fast,0,MODE_SMA,PRICE_CLOSE,0);
slowmaafter = iMA(Symbol(),0,ma_period_fast,0,MODE_SMA,PRICE_CLOSE,0);
fastmanow = iMA(Symbol(),0,ma_period_fast,0,MODE_SMA,PRICE_CLOSE,1);
slowmanow = iMA(Symbol(),0,ma_period_fast,0,MODE_SMA,PRICE_CLOSE,1);
fastmaprevious = iMA(Symbol(),0,ma_period_fast,0,MODE_SMA,PRICE_CLOSE,2);
slowmaprevious = iMA(Symbol(),0,ma_period_fast,0,MODE_SMA,PRICE_CLOSE,2);



//check open orders
if(OrdersTotal()==1)
{
//////////////////////////////////////////////////////////////////////////////////////////////////////
//check for exit
if ((fastmanow > slowmanow) && (fastmaprevious < slowmaprevious) && (fastmaafter > slowmaafter))// CrossedUp
{
OrderClose(MagicNumber,LotSize,Ask,3,Aqua);
}
if ((fastmanow < slowmanow) && (fastmaprevious > slowmaprevious) && (fastmaafter < slowmaafter))// CrossedDown
{
OrderClose(MagicNumber,LotSize,Ask,3,Aqua);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
}
if(OrdersTotal()==0)
{
///////////////////////////////////////////////////////////////////////////////////////////////////////
//check for entry
if ((fastmanow > slowmanow) && (fastmaprevious < slowmaprevious) && (fastmaafter > slowmaafter))// CrossedUp
{
{OrderSend(Symbol(),OP_BUY,LotSize,Ask,3,Ask-(StopLoss*pips),Ask+(TakeProfit*pips),NULL,MagicNumber,0,Green);} //send buy
}
if ((fastmanow < slowmanow) && (fastmaprevious > slowmaprevious) && (fastmaafter < slowmaafter))// CrossedDown
{
{OrderSend(Symbol(),OP_SELL,LotSize,Bid,3,Bid+(StopLoss*pips),Bid-(TakeProfit*pips),NULL,MagicNumber,0,Red);}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
}
//----
   return(0);
  }
//+------------------------------------------------------------------+
 
You use ma_period_fast in all of your iMA calls. So the slow values are the same as the fast.
 
szgy74:
You use ma_period_fast in all of your iMA calls. So the slow values are the same as the fast.
Thanks for your help, your a machine! :)
 
szgy74:
You use ma_period_fast in all of your iMA calls. So the slow values are the same as the fast.

Also I have run the backtest and noticed that the trades appear at the wrong time instead of just after or on the ma crossover, could you spread some further light on this for me too? see attached image. I run a 21 and 55 sma on this image on the 15min chart of GBP/USD.

Thanks in advance.

 
Read this topic and see if that helps.
 
angevoyageur:
Read this topic and see if that helps.

Hi I read over the topic, works fine now you too are a genius, thanks for your help. I attach the code with only closed candles being compared too i then implemented a isnewcandle subroutine. see code attached.

//+------------------------------------------------------------------+
//|                                                            X.mq4 |
//|                        Copyright 2013, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2013, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"
double ma_period_slow = 21;
double ma_period_fast = 55;
extern double LotSize=0.1;
extern double StopLoss=100;
extern double TakeProfit=200;
extern int MagicNumber=1234;
double pips = 0.00001;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
if(IsNewCandle())
{
//----
double fastmaprevious,slowmaprevious,fastmanow,slowmanow,fastmaafter,slowmaafter;

fastmaafter = iMA(Symbol(),0,ma_period_fast,0,MODE_SMA,PRICE_CLOSE,0);
slowmaafter = iMA(Symbol(),0,ma_period_slow,0,MODE_SMA,PRICE_CLOSE,0);
fastmanow = iMA(Symbol(),0,ma_period_fast,0,MODE_SMA,PRICE_CLOSE,1);
slowmanow = iMA(Symbol(),0,ma_period_slow,0,MODE_SMA,PRICE_CLOSE,1);
fastmaprevious = iMA(Symbol(),0,ma_period_fast,0,MODE_SMA,PRICE_CLOSE,2);
slowmaprevious = iMA(Symbol(),0,ma_period_slow,0,MODE_SMA,PRICE_CLOSE,2);



//check open orders
if(OrdersTotal()==1)
{
//////////////////////////////////////////////////////////////////////////////////////////////////////
//check for exit
if ((fastmanow > slowmanow) && (fastmaprevious < slowmaprevious))// && (fastmaafter > slowmaafter))// CrossedUp
{
OrderClose(MagicNumber,LotSize,Ask,3,Aqua);
}
if ((fastmanow < slowmanow) && (fastmaprevious > slowmaprevious))// && (fastmaafter < slowmaafter))// CrossedDown
{
OrderClose(MagicNumber,LotSize,Ask,3,Aqua);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
}
if(OrdersTotal()==0)
{
///////////////////////////////////////////////////////////////////////////////////////////////////////
//check for entry
if ((fastmanow > slowmanow) && (fastmaprevious < slowmaprevious))// && (fastmaafter > slowmaafter))// CrossedUp
{
{OrderSend(Symbol(),OP_BUY,LotSize,Ask,3,Ask-(StopLoss*pips),Ask+(TakeProfit*pips),NULL,MagicNumber,0,Green);} //send buy
}
if ((fastmanow < slowmanow) && (fastmaprevious > slowmaprevious))// && (fastmaafter < slowmaafter))// CrossedDown
{
{OrderSend(Symbol(),OP_SELL,LotSize,Bid,3,Bid+(StopLoss*pips),Bid-(TakeProfit*pips),NULL,MagicNumber,0,Red);}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
}
//----
   return(0);
  }
  }
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//insuring its a new candle function
//+------------------------------------------------------------------+
bool IsNewCandle()
{
   static int BarsOnChart=0;
        if (Bars == BarsOnChart)
        return (false);
        BarsOnChart = Bars;
        return(true);
}

//+------------------------------------------------------------------+

look at my pretty picture .................

However, I also have noticed that it does not close out the trades based on the crossover see below pic:

Any ideas as to why that may be I am not sure.

 
zmalick:

Hi I read over the topic, works fine now you too are a genius, thanks for your help. I attach the code with only closed candles being compared too i then implemented a isnewcandle subroutine. see code attached.

look at my pretty picture .................

However, I also have noticed that it does not close out the trades based on the crossover see below pic:

Any ideas as to why that may be I am not sure.

You are still using iMA on bar 0 which is current open bar, and you check your condition on new bar. But what you see on this chart for history data is value of iMA on closed bar.
 
angevoyageur:
You are still using iMA on bar 0 which is current open bar, and you check your condition on new bar. But what you see on this chart for history data is value of iMA on closed bar.

I have updated the code a bit, see below it works:

//+------------------------------------------------------------------+
//|                                                            X.mq4 |
//|                        Copyright 2013, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2013, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"
double ma_period_slow = 30;
double ma_period_fast = 80;
extern double LotSize=0.1;
extern double StopLoss=500;
extern double TakeProfit=1000;
extern int MagicNumber=1234;
double pips = 0.00001;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
if(IsNewCandle())
{
//----
double fastmaprevious,slowmaprevious,fastmanow,slowmanow;
int order;
fastmanow = iMA(Symbol(),0,ma_period_fast,0,MODE_SMA,PRICE_CLOSE,1);
slowmanow = iMA(Symbol(),0,ma_period_slow,0,MODE_SMA,PRICE_CLOSE,1);
fastmaprevious = iMA(Symbol(),0,ma_period_fast,0,MODE_SMA,PRICE_CLOSE,2);
slowmaprevious = iMA(Symbol(),0,ma_period_slow,0,MODE_SMA,PRICE_CLOSE,2);



//check open orders
if(OrdersTotal()==1)
{
//////////////////////////////////////////////////////////////////////////////////////////////////////
//check for exit
if ((fastmanow > slowmanow) && (fastmaprevious < slowmaprevious))// CrossedUp
   {
if(OrderSelect(0, SELECT_BY_POS)==true)
      {
order=OrderTicket();
OrderClose(order,LotSize,Bid,3,White);
      }
   }
if ((fastmanow < slowmanow) && (fastmaprevious > slowmaprevious))
   {
if(OrderSelect(0, SELECT_BY_POS)==true)
      {
order=OrderTicket();
OrderClose(order,LotSize,Ask,3,Yellow);
      }
   }
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
if(OrdersTotal()==0)
{
///////////////////////////////////////////////////////////////////////////////////////////////////////
//check for entry
if ((fastmanow > slowmanow) && (fastmaprevious < slowmaprevious))// CrossedUp
{
if(Ask>fastmanow)
{
{OrderSend(Symbol(),OP_BUY,LotSize,Ask,3,Ask-(StopLoss*pips),Ask+(TakeProfit*pips),NULL,MagicNumber,0,Green);} //send buy
}
if(Bid<fastmanow)
{
{OrderSend(Symbol(),OP_SELL,LotSize,Bid,3,Bid+(StopLoss*pips),Bid-(TakeProfit*pips),NULL,MagicNumber,0,Red);}
}
}
if ((fastmanow < slowmanow) && (fastmaprevious > slowmaprevious))// CrossedDown
{

if(Ask>fastmanow)
{
{OrderSend(Symbol(),OP_BUY,LotSize,Ask,3,Ask-(StopLoss*pips),Ask+(TakeProfit*pips),NULL,MagicNumber,0,Green);} //send buy
}
if(Bid<fastmanow)
{
{OrderSend(Symbol(),OP_SELL,LotSize,Bid,3,Bid+(StopLoss*pips),Bid-(TakeProfit*pips),NULL,MagicNumber,0,Red);}
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
}
//----
   return(0);
  }
  }
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//insuring its a new candle function
//+------------------------------------------------------------------+
bool IsNewCandle()
{
   static int BarsOnChart=0;
        if (Bars == BarsOnChart)
        return (false);
        BarsOnChart = Bars;
        return(true);
}

//+------------------------------------------------------------------+

On GBP/USD M15 it looks good twists and turns as it was intended. Output is below from the backtesting which was for 6months. I get the Order close error 138 from time to time is that due to requotes in this case or do you think my close is wrong. Also can you confirm im using the right bars again im not sure if the isnewcandle function should be changed from what you said. thanks.

 
zmalick:

I have updated the code a bit, see below it works:

On GBP/USD M15 it looks good twists and turns as it was intended. Output is below from the backtesting which was for 6months. I get the Order close error 138 from time to time is that due to requotes in this case or do you think my close is wrong. Also can you confirm im using the right bars again im not sure if the isnewcandle function should be changed from what you said. thanks.

Can you explain this code ?

//check for exit
if ((fastmanow > slowmanow) && (fastmaprevious < slowmaprevious))// CrossedUp
   {
if(OrderSelect(0, SELECT_BY_POS)==true)
      {
order=OrderTicket();
OrderClose(order,LotSize,Bid,3,White);
      }
   }
if ((fastmanow < slowmanow) && (fastmaprevious > slowmaprevious))
   {
if(OrderSelect(0, SELECT_BY_POS)==true)
      {
order=OrderTicket();
OrderClose(order,LotSize,Ask,3,Yellow);
      }
   }
}

An OP_BUY is closed with a Sell at Bid, an OP_SELL is closed with a Buy at Ask . . . check your trade types using OrderType() and close at the correct price or just use OrderClosePrice() instead.

 
zmalick:

I have updated the code a bit, see below it works:

On GBP/USD M15 it looks good twists and turns as it was intended. Output is below from the backtesting which was for 6months. I get the Order close error 138 from time to time is that due to requotes in this case or do you think my close is wrong. Also can you confirm im using the right bars again im not sure if the isnewcandle function should be changed from what you said. thanks.

Also your IsNewCandle() function use Bars, but in some cases it's not reliable, it's better to use Time.
 
RaptorUK:

Can you explain this code ?

An OP_BUY is closed with a Sell at Bid, an OP_SELL is closed with a Buy at Ask . . . check your trade types using OrderType() and close at the correct price or just use OrderClosePrice() instead.

OrderClosePrice is easy so I used it, thanks, I have no errors now when trades close.
//check for exit
if ((fastmanow > slowmanow) && (fastmaprevious < slowmaprevious))// CrossedUp
   {
if(OrderSelect(0, SELECT_BY_POS)==true)//search for order 0  
      {
order=OrderTicket();//get ticket number
OrderClose(order,LotSize,OrderPriceClose(),3,White);//close the trade
      }
   }
if ((fastmanow < slowmanow) && (fastmaprevious > slowmaprevious))//  Crossed down
   {
if(OrderSelect(0, SELECT_BY_POS)==true)//search for order 0
      {
order=OrderTicket();//get ticket number
OrderClose(order,LotSize,OrderPriceClose(),3,Yellow);//close the trade
      }
   }
}
I have commented the code to explain my thinking, see above.