newbie crossing MA question.... help needed

 

Hi, I am a newbie in MQL4 programming, I am having a problem applying my crossing MA Expert Advisor in my live trade?

I made the program, successfully compile it, backtest it, quite happy with the result, ready to test it in the real live trading..... only to find this illogical problem...

which is that the point where my EA open position (using iMA() with specified periode of fast and slow MAs) is not the same as where it should be opened according to the crossing of 2 Moving average visible in the chart...

the period of MA and timeframe of the chart are both the same in the iMA() and the chart timeframe where I activate my EA....

can someone please give me some explanation about this..... thanx in advance

 

btw I forgot to mention also that... after a few live trade has been made, I backtested again my EA to see if the position openned is the same both in live and back test... and guess what... it is not....

so please again could any one gimme a clue ...

btw.... this is the iMA() I use (should you need to know):

fast_1=iMA(NULL,0,MA_Fast,0,MODE_SMA,PRICE_CLOSE,1);

fast_2=iMA(NULL,0,MA_Fast,0,MODE_SMA,PRICE_CLOSE,2);

slow_1=iMA(NULL,0,MA_Slow,0,MODE_SMA,PRICE_CLOSE,1);

slow_2=iMA(NULL,0,MA_Slow,0,MODE_SMA,PRICE_CLOSE,2);

 
fjtj:

btw I forgot to mention also that... after a few live trade has been made, I backtested again my EA to see if the position openned is the same both in live and back test... and guess what... it is not....

so please again could any one gimme a clue ...

btw.... this is the iMA() I use (should you need to know):

fast_1=iMA(NULL,0,MA_Fast,0,MODE_SMA,PRICE_CLOSE,1);

fast_2=iMA(NULL,0,MA_Fast,0,MODE_SMA,PRICE_CLOSE,2);

slow_1=iMA(NULL,0,MA_Slow,0,MODE_SMA,PRICE_CLOSE,1);

slow_2=iMA(NULL,0,MA_Slow,0,MODE_SMA,PRICE_CLOSE,2);


Your asking people to guess. What does not the same mean? The EA is making trades too early, too late. Post a screen shot? Attach you EA and someone will probably be able to help you. Have you looked and any of the sample MA crossover EA's on the site and compared your code to them. It might not be the iMA's you have above but something else in your code.

 

Here is attached the screenshot.

yes, in fact I just modify the function parameter of crossing MA from one of the site that provide a sample of simple crossing MA codes

 
 
Here is the source... very simple newbie EA though :)
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
   
   int all_trades=OrdersTotal();    Determine_TradeDirection_&_Place_Orders();
   
   return(0);
  }
//+------------------------------------------------------------------+

void Determine_TradeDirection_&_Place_Orders()
  {
   // see if we have already opened trades in the current bar
   datetime time1=GlobalVariableGet(_time_opened_1);
   datetime time2=GlobalVariableGet(_time_opened_2);
   
   //the opening time of the current bar 
   datetime _bar_open_time=iTime(Symbol(),0,0);
   
   //if the open time of the last trade is greater than the open time of the current bar then
   //the system has already opened a trade in this bar and there is no need to open another trade
   if ((time1-_bar_open_time)>=0) return(0);
   if ((time2-_bar_open_time)>=0) return(0);
   //Moving Average Calculations
   double fast_1=iMA(NULL,0,MA_Fast,0,MODE_SMA,PRICE_CLOSE,1);
   double fast_2=iMA(NULL,0,MA_Fast,0,MODE_SMA,PRICE_CLOSE,2);
   double slow_1=iMA(NULL,0,MA_Slow,0,MODE_SMA,PRICE_CLOSE,1);
   double slow_2=iMA(NULL,0,MA_Slow,0,MODE_SMA,PRICE_CLOSE,2);
      
   //corss up means trade long
   if ((fast_2<=slow_2)&&(fast_1>slow_1))PlaceTrades(1);
   
   //corss down means trade shor
   if ((fast_2>=slow_2)&&(fast_1<slow_1)) PlaceTrades(-1);
   

   //if none of the conditions are met no trades
   return(0);
  }
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//| PlaceTrades function                                             |
//+------------------------------------------------------------------+
void PlaceTrades(int trade_dir)
  {
   //the following lines make the code compatible with fractioanl pips
   //we need to replace Point with _my_point in the rest of the code
   
   RefreshRates();
      
   //long trades
   if (trade_dir==1)
      {
         double SL=Ask-stop_loss;  
         double TP=Ask+take_profit_buy; 
         double TP2=Ask+take_profit_buy_defense;
         int ticket=OrderSend(Symbol(),OP_BUY,LotsPerTrade,Ask,5,NULL,NULL,NULL,magic_number,0,Green);         
         
         int ticket2=OrderSend(Symbol(),OP_BUY,LotsPerTrade*2,Ask,5,NULL,NULL,NULL,magic_number,0,Green);
         
         OrderModify(ticket, OrderOpenPrice(), SL, TP, NULL, Green);
         OrderModify(ticket2, OrderOpenPrice(), SL, TP2, NULL, Green);
              
         if (ticket>=0) 
            {
               OrderSelect(ticket,SELECT_BY_TICKET);
               datetime open_time=OrderOpenTime();
               GlobalVariableSet(_time_opened_1,open_time);
               //previousTradeTime = open_time;
               previousTradeTimeBuy = open_time;            
            }
         if (ticket2>=0) 
            {
               OrderSelect(ticket2,SELECT_BY_TICKET);
               datetime open_time2=OrderOpenTime();
               GlobalVariableSet(_time_opened_2,open_time2);               
            }   
      }//end of long trades
   
   //short trades
   if (trade_dir==-1)
      {
         SL=Bid+stop_loss; 
         TP=Bid-take_profit_sell; 
         TP2=Bid-take_profit_sell_defense; 
         ticket=OrderSend(Symbol(),OP_SELL,LotsPerTrade,Bid,5,NULL,NULL,NULL,magic_number,0,Red);
                  
         ticket2=OrderSend(Symbol(),OP_SELL,LotsPerTrade*2,Ask,5,NULL,NULL,NULL,magic_number,0,Red);
         
         OrderModify(ticket, OrderOpenPrice(), SL, TP, NULL, Red);
         OrderModify(ticket2, OrderOpenPrice(), SL, TP2, NULL, Red);
         
         if (ticket>=0) 
            {
               OrderSelect(ticket,SELECT_BY_TICKET);
               open_time=OrderOpenTime();
               GlobalVariableSet(_time_opened_1,open_time);
               //previousTradeTime = open_time;
               previousTradeTimeSell = open_time; 
            }
         if (ticket2>=0) 
            {
               OrderSelect(ticket2,SELECT_BY_TICKET);
               open_time2=OrderOpenTime();
               GlobalVariableSet(_time_opened_2,open_time2);               
            }
      }//end of short trades
   
   return;
  }
//+------------------------------------------------------------------+
 
fjtj:


Add some prrint statements to your code like this:

   if ((fast_2<=slow_2)&&(fast_1>slow_1))
   {
      Print("Long fast_1 #### = " + DoubleToStr(fast_1,Digits));
      Print("Long fast_2 #### = " + DoubleToStr(fast_2,Digits));
      Print("Long slow_1 #### = " + DoubleToStr(slow_1,Digits)); 
      Print("Long slow_2 #### = " + DoubleToStr(slow_2,Digits));   
      PlaceTrades(1);      
    }   
 
   if ((fast_2>=slow_2)&&(fast_1<slow_1))
   {
      Print("Short fast_1 #### = " + DoubleToStr(fast_1,Digits));
      Print("Short fast_2 #### = " + DoubleToStr(fast_2,Digits));
      Print("Short slow_1 #### = " + DoubleToStr(slow_1,Digits)); 
      Print("Short slow_2 #### = " + DoubleToStr(slow_2,Digits));  
      PlaceTrades(-1);
   }

When the order is placed compare the print values with the parameters in the condition see they are true, which I am pretty sure they will be. I had a bunch of issues trying to run you EA becasue you left out the variables and other stuff. When I finally got a frankenstien version of the EA running I was opening trades at the correct time, with most of the datetime code you have ripped out.

Here is what I would do.

First, check the return codes from all of your trading functions!!! orderSend() orderModify (). You need to catch errors on these functions.

Second, check to make sure your MA's on your chart are the same period, etc.. as MA_Fast and MA_Slow and the other parameter in your EA's iMa's. I know you said they are but you never know. That would be the most likely problem. If they are the same then go on to step three.

Third, comment out all of your datetime code, replace it with a simple flag that gets turned on by a successful ticket and turned off by something like (!orderTotal>0). If that works the nyou know your issue is in the time code.

You really need to look at some examples of code on the forum for OrderSend and OrderModifiy. They are not stuctured right in your EA. You are asking for problems they way they are coded now. I hope you are running this on a demo account and not a real $$ account.

Lastly, this function statement compiled for you ? Determine_TradeDirection_&_Place_Orders() I had to change the name because, identifiers are limited to 31 characters and "&" is not one of them.

Reason: