CAN SOMEONE HELP ME CORRECT THIS CODE

 

Hi All,

Below is th code of a simple EA I coded using the MACD Oscilator indicator.Unfortunately I have not been able to make it close a losing trade.My intentions iare to go long when the indicator give a value greater than 0 at the close of the bar and go short when it gives a value below 0.Exit is by take profit or by the arrival of an opposite signal(whichever comes first).I would apprecaite it if some cn help look at these coded and correct me where I may have made an error

Thanks in advance,

COOL31

//+------------------------------------------------------------------+
//| Osma.mq4 |
//| |
//+------------------------------------------------------------------+

extern double TakeProfit = 50;
extern double Lots = 0.01;
extern double TrailingStop = 30;
extern double Fast_Ema=3;
extern double Slow_Ema=2;
extern double Macd_SMA=26;
extern double magic_num=90554;

//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int start()
{
double signal1, signal2;
int cnt, ticket, count,orders,Slippage,total;

if(Bars<100)
{
Print("bars less than 100");
return(0);
}
if(TakeProfit<10)
{
Print("TakeProfit less than 10");
return(0); // check TakeProfit
}
// to simplify the coding and speed up access
// data are put into internal variables
signal1=iOsMA( NULL,0, Fast_Ema,Slow_Ema, Macd_SMA,PRICE_CLOSE, 1) ;
signal2=iOsMA( NULL,0, Fast_Ema,Slow_Ema, Macd_SMA,PRICE_CLOSE, 2) ;

total=OrdersTotal();
if(total<1)
{
// no opened orders identified
if(AccountFreeMargin()<(1000*Lots))
{
Print("We have no money. Free Margin = ", AccountFreeMargin());
return(0);
}
// check for long position (BUY) possibility
if(signal1 > 0 && signal2<0)
{
for (count=0; count< orders; count++)

OrderSelect(count,SELECT_BY_POS,MODE_TRADES);

if (OrderMagicNumber()==magic_num && OrderType()==OP_SELL)
{
OrderClose(OrderTicket(),OrderLots(),Bid, Slippage,Red);
}

ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,"Osma",magic_num,0,Green);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
}
else Print("Error opening BUY order : ",GetLastError());
return(0);
}
// check for short position (SELL) possibility
if(signal1 < 0 && signal2 > 0)

{
for (count=0; count<orders; count++)

OrderSelect(count,SELECT_BY_POS,MODE_TRADES);
//order_id=OrderTicket();
if (OrderMagicNumber()==magic_num && OrderType()==OP_BUY)
{
OrderClose(OrderTicket(),OrderLots(),Ask, Slippage,Red);
}
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,Bid-TakeProfit*Point,"Osma",magic_num,0,Red);
{
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
}
else Print("Error opening SELL order : ",GetLastError());
return(0);
}
return(0);

}
}
}

 

Try this

//+------------------------------------------------------------------+
//| Osma.mq4 |
//| |
//+------------------------------------------------------------------+

extern double TakeProfit = 50;
extern double Lots = 0.01;
extern double TrailingStop = 30;
extern double Fast_Ema=3;
extern double Slow_Ema=2;
extern double Macd_SMA=26;
extern double magic_num=90554;

//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int start()
{
double signal1, signal2;
int cnt, ticket, count,orders,Slippage,total;

if(Bars<100)
{
Print("bars less than 100");
return(0); 
}
if(TakeProfit<10)
{
Print("TakeProfit less than 10");
return(0); // check TakeProfit
}
// to simplify the coding and speed up access
// data are put into internal variables
signal1=iOsMA( NULL,0, Fast_Ema,Slow_Ema, Macd_SMA,PRICE_CLOSE, 1) ;
signal2=iOsMA( NULL,0, Fast_Ema,Slow_Ema, Macd_SMA,PRICE_CLOSE, 2) ;

total=OrdersTotal();
if(total<1) 
{
// no opened orders identified
if(AccountFreeMargin()<(1000*Lots))
{
Print("We have no money. Free Margin = ", AccountFreeMargin());
return(0); 
}
// check for long position (BUY) possibility
if(signal1 > 0 && signal2<0)
{
for (count=0; count< orders; count++)

OrderSelect(count,SELECT_BY_POS,MODE_TRADES);

if (OrderMagicNumber()==magic_num && OrderType()==OP_SELL)
{
OrderClose(OrderTicket(),OrderLots(),Ask, Slippage,Red); 
} 
RefreshRates();
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,"Osma",magic_num,0,Green);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
}
else Print("Error opening BUY order : ",GetLastError()); 
return(0); 
}
// check for short position (SELL) possibility
if(signal1 < 0 && signal2 > 0)

{
for (count=0; count<orders; count++)

OrderSelect(count,SELECT_BY_POS,MODE_TRADES);
//order_id=OrderTicket();
if (OrderMagicNumber()==magic_num && OrderType()==OP_BUY)
{
OrderClose(OrderTicket(),OrderLots(),Bid, Slippage,Red); 
}
RefreshRates();
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,Bid-TakeProfit*Point,"Osma",magic_num,0,Red);
{
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
}
else Print("Error opening SELL order : ",GetLastError()); 

}
return(0);

}
}
} 


 
Roger wrote >>

Try this

Hi Rogers,

thksforyouinput but it still does not close open trades on arrival of opposite signal

 
I have found mistakes
//+------------------------------------------------------------------+
//| Osma.mq4 |
//| |
//+------------------------------------------------------------------+

extern double TakeProfit = 50;
extern double Lots = 0.01;
extern double TrailingStop = 30;
extern double Fast_Ema=3;
extern double Slow_Ema=2;
extern double Macd_SMA=26;
extern double magic_num=90554;

//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int start()
{
double signal1, signal2;
int cnt, ticket, count,orders,Slippage,total;

if(Bars<100)
{
Print("bars less than 100");
return(0); 
}
if(TakeProfit<10)
{
Print("TakeProfit less than 10");
return(0); // check TakeProfit
}
// to simplify the coding and speed up access
// data are put into internal variables
signal1=iOsMA( NULL,0, Fast_Ema,Slow_Ema, Macd_SMA,PRICE_CLOSE, 1) ;
signal2=iOsMA( NULL,0, Fast_Ema,Slow_Ema, Macd_SMA,PRICE_CLOSE, 2) ;

total=OrdersTotal();
if(total==0)
   {
   if(signal1 > 0 && signal2<0)
      {
      ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,"Osma",magic_num,0,Green);
      if(ticket>0)
         {
         if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
         }
      else Print("Error opening BUY order : ",GetLastError()); 
      return(0); 
      }
   // check for short position (SELL) possibility
   if(signal1 < 0 && signal2 > 0)

      {
      ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,Bid-TakeProfit*Point,"Osma",magic_num,0,Red);
      if(ticket>0)
         {
         if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
         }
      else Print("Error opening SELL order : ",GetLastError()); 
      return(0);
      }
   }
if(total>0) 
   {
   // no opened orders identified
   if(AccountFreeMargin()<(1000*Lots))
      {
      Print("We have no money. Free Margin = ", AccountFreeMargin());
      return(0); 
      }
   // check for long position (BUY) possibility
   if(signal1 > 0 && signal2<0)
      {
      for (count=0; count< orders; count++)
         {
         OrderSelect(count,SELECT_BY_POS,MODE_TRADES);

         if (OrderMagicNumber()==magic_num && OrderType()==OP_SELL)
            {
            Print("OrderTicket - ",OrderTicket());
            OrderClose(OrderTicket(),OrderLots(),Ask, Slippage,Red); 
            RefreshRates();
            ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,"Osma",magic_num,0,Green);
            if(ticket>0)
               {
               if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
               }
            else Print("Error opening BUY order : ",GetLastError()); 
            return(0); 
            } 
         }
      }
   // check for short position (SELL) possibility
   if(signal1 < 0 && signal2 > 0)

      {
      for (count=0; count<orders; count++)
         {
         OrderSelect(count,SELECT_BY_POS,MODE_TRADES);
         //order_id=OrderTicket();
         if (OrderMagicNumber()==magic_num && OrderType()==OP_BUY)
            {
            Print("OrderTicket - ",OrderTicket());
            OrderClose(OrderTicket(),OrderLots(),Bid, Slippage,Red); 
            }
         RefreshRates();
         ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,Bid-TakeProfit*Point,"Osma",magic_num,0,Red);
            {
            if(ticket>0)
               {
               if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
               }
            else Print("Error opening SELL order : ",GetLastError()); 
            }
         }
      return(0);

      }
   }
} 


 

Hi Rogers,

Thanks once gin for your effort.While the EA opens buy and sell order perfectly it still doesn't closetrades that did not reach its profit limit by the time the opposite signal arrives.Eit issupposed tobe by take profit or on arrival of opposite signal.For eample, if it opens aseel positon and b4 it reaches its profit limit a buy signal comes in, it is upposed toautomatically close the sellorder andopen a buy order.Right it doesn't close open orders at the arrival of the opposite signal.rather itleaves it opened giving a drawing down or some times it manages to reach profit limit.iwnt trdes tobe closed at profit limit or arrival of opposite signal.I would apprecite it if cn check it out for me.I usually test it on a 1 minute chart for quick observation.

Thnk you.

Cool31