What's wrong with my iMA?

 

Hello, I want to close order when ema5 cross ema10.


I don't know why but don't work.


In function SecondTakeProfit(), I use iMA to close order.


Thank's

pgforex

//+------------------------------------------------------------------+
//|                                               Vrai-Trailling.mq4 |
//|                                  Copyright © 2009, Pascal Gignac |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, Pascal Gignac"
#property link      ""

//---- Input Parameters
extern int  TakeProfit = 20;
extern int  FirstTarget = 15;
extern int  FirstStop = 1;
extern int  StopLoss = 20;
extern int  EMA_Period1 = 5;
extern int  EMA_Period2 = 10;

int ExecuteOnce = 0;
double EMA_1;
double EMA_2;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
   double EMA_1;
   double EMA_2;

   EMA_1 = iMA(Symbol(),0,EMA_Period1,0,MODE_EMA,PRICE_CLOSE,0);
   EMA_2 = iMA(Symbol(),0,EMA_Period2,0,MODE_EMA,PRICE_CLOSE,0);

   MaxStopLoss();
   MoveToBE();
   FirstTakeProfit();
   SecondTakeProfit();
   
   Comment(
   "\nEMA_1:  ", EMA_1,
   "\nEMA_2:  ", EMA_2);

//----
   return(0);
  }
//+------------------------------------------------------------------+

//==== Function StopLoss
void MaxStopLoss()
{
  for(int i = OrdersTotal() - 1; i >= 0; i--)
  {
    if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
    {
      if(OrderMagicNumber() == OrderMagicNumber())
      {
        if(OrderType() == OP_SELL && OrderSymbol() == Symbol())
	     {
	     //---- Max Stop Loss OP_SELL
          if(Bid >= OrderOpenPrice()+(StopLoss*Point))
          {
           OrderClose(OrderTicket(),OrderLots(),Bid,3,Red);
		    }
		  }
		else if(OrderType() == OP_BUY && OrderSymbol() == Symbol())
		  {
		  //---- Max Stop Loss OP_BUY
		    if(Ask <= OrderOpenPrice()-(StopLoss * Point))
          {
           OrderClose(OrderTicket(),OrderLots(),Ask,3,Red);
          }
   	  }
      }
    }
  }
}
//---- End Function

//==== Function Move to Breakeven
void MoveToBE()
{
  for(int i = OrdersTotal() - 1; i >= 0; i--)
  {
    if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
    {
      if(OrderMagicNumber() == OrderMagicNumber())
      {
        if(OrderType() == OP_SELL && OrderSymbol() == Symbol())
	     {
        //---- Modify when hit First Target OP_SELL
          if((Bid <= OrderOpenPrice()-(FirstTarget*Point)) && (OrderStopLoss() != OrderOpenPrice() - (FirstStop*Point)))
          {
           OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice() - (FirstStop*Point),0, 0, Blue);
          }
		  }
		 else if(OrderType() == OP_BUY && OrderSymbol() == Symbol())
		  {
        //---- Modify when hit First Target      
          if((Ask >= OrderOpenPrice()+(FirstTarget*Point)) && (OrderStopLoss() != OrderOpenPrice()+(FirstStop*Point)))
          {
           OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice()+(FirstStop*Point),0,0, Blue);   
          }
		  }
      }
    }
  }
}
//---- End Function

//==== Function First Take Profit
void FirstTakeProfit()
{
  for(int i = OrdersTotal() - 1; i >= 0; i--)
  {
    if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
    {
      if(OrderMagicNumber() == OrderMagicNumber())
      {
        if(OrderType() == OP_SELL && OrderSymbol() == Symbol())
	     {
        //---- First Take Profit half position OP_SELL
          if(Bid <= OrderOpenPrice()-(TakeProfit*Point) && ExecuteOnce<1)
          {
           OrderClose(OrderTicket(),OrderLots() / 2, Bid,3,Green);
           ExecuteOnce=1;   
          }
		  }
		else if(OrderType() == OP_BUY && OrderSymbol() == Symbol())
		  {
        //---- First Take Profit half position OP_BUY
          if(Ask >= OrderOpenPrice()+(TakeProfit*Point) && ExecuteOnce<1)
          {
           OrderClose(OrderTicket(),OrderLots() / 2, Ask,3,Green);
           ExecuteOnce=1;                       
          }
		  }
      }
    }
  }
}
//---- End Function

//==== Function Second Take Profit
void SecondTakeProfit()
{
  for(int i = OrdersTotal() - 1; i >= 0; i--)
  {
    if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
    {
      if(OrderMagicNumber() == OrderMagicNumber())
      {
        if(OrderType() == OP_SELL && OrderSymbol() == Symbol())
	     {
        //---- Take Profit second half position when EMA crosse OP_SELL    
          if(EMA_1 > EMA_2)
          {
           OrderClose(OrderTicket(),OrderLots(),Bid,3,Lime);
          }
		  }
		else if(OrderType() == OP_BUY && OrderSymbol() == Symbol())
		  {
        //---- Take Profit second half position when EMA crosse
          if(EMA_1 < EMA_2)
          {
           OrderClose(OrderTicket(),OrderLots(),Ask,3,Lime);                      
          }
		  }
      }
    }
  }
}
//---- End Function

Files:
 
pgforex wrote >>

Hello, I want to close order when ema5 cross ema10.

I don't know why but don't work.

In function SecondTakeProfit(), I use iMA to close order.

Thank's

pgforex

Your EMa's dont cross, you look only if they are above or below....

 

Ok, how can I do that? I'm very beginner.


On long trade, when ema5 cross down ema10 => close order

On short trade when ema5 cross up ema10 => close order


Can you help me?


Thank's

 

EMA_1 = iMA(Symbol(),0,EMA_Period1,0,MODE_EMA,PRICE_CLOSE,0);
EMA_2 = iMA(Symbol(),0,EMA_Period2,0,MODE_EMA,PRICE_CLOSE,0);
EMA_1_2 = iMA(Symbol(),0,EMA_Period1,0,MODE_EMA,PRICE_CLOSE,1);
EMA_2_2 = iMA(Symbol(),0,EMA_Period2,0,MODE_EMA,PRICE_CLOSE,1);

if (EMA_1>EMA_2&&EMA_1_2<EMA_2_2) Cross Upward .. vice versa for Downward

 

Thank's EADeveloper. I've dreamed to this code during the night and I've seen the same code of you. Thank's to comfirm my dream.


Whit your pseudo, I think you are an EA developer. Just for fun, how much to create an EA like this? Because some poeple offers to

create this code for me, but I think the price is to expensive.


Good day!

pgforex

 

I have a question!


EMA_1 = iMA(Symbol(),0,EMA_Period1,0,MODE_EMA,PRICE_CLOSE,0);
EMA_2 = iMA(Symbol(),0,EMA_Period2,0,MODE_EMA,PRICE_CLOSE,0);
EMA_1_2 = iMA(Symbol(),0,EMA_Period1,0,MODE_EMA,PRICE_CLOSE,1);
EMA_2_2 = iMA(Symbol(),0,EMA_Period2,0,MODE_EMA,PRICE_CLOSE,1);

if (EMA_1>EMA_2&&EMA_1_2<EMA_2_2) Cross Upward .. vice versa for Downward


EMA_1 & EMA_2 is for the current bar

EMA_1_2 & EMA_2_2 is for the previous bar


What happen if EMA_1_2 & EMA_2_2 are equal during the previous 2,3 or more bars.


If I use (EMA_1>EMA_2&&EMA_1_2<EMA_2_2), the condition never meet.


Is in't better like that? (EMA_1>=EMA_2&&EMA_1_2<EMA_2_2)


Thank's

pgforex

Reason: