Why OrderClose does not work at my code?

 

Hello,

I would like to close my positions in my EA, but it does not work.can you help me,wher is a problemm?


here is a part of my code:


if(Bid < shortEma && DownTrend > 0 && Bid <= DownTrend )
{
ticket=OrderSend(Symbol(),OP_SELL,1,Bid,3,0,Ask-TakeProfit*Point,"moj prvy obchod",1234,0,Red);

-
-
-

for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()<=OP_SELL && OrderSymbol()==Symbol())
{
if(OrderType()==OP_BUY)

{
if(Bid <= shortEma)
{
OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet);

return(0);

}


OrederClose doesnot close my opened positions,when Bid <= shortEma. Why it does not work?Thanks.

 
jozino23:

Hello,

I would like to close my positions in my EA, but it does not work.can you help me,wher is a problemm?


here is a part of my code:


if(Bid < shortEma && DownTrend > 0 && Bid <= DownTrend )
{
ticket=OrderSend(Symbol(),OP_SELL,1,Bid,3,0,Ask-TakeProfit*Point,"moj prvy obchod",1234,0,Red);

-
-
-

for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()<=OP_SELL && OrderSymbol()==Symbol())
{
if(OrderType()==OP_BUY)

{
if(Bid <= shortEma)
{
OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet);

return(0);

}


OrederClose doesnot close my opened positions,when Bid <= shortEma. Why it does not work?Thanks.


if(OrderType()<=OP_SELL && OrderSymbol()==Symbol()) 

you should not use <=OP_SELL, use ==

also, your statement,

if (Bid <= shortEma) 

will never be executed because it is following the opposite condition

for(cnt=0;cnt<total;cnt++) 
  {
   OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
   if(OrderType()<=OP_SELL && OrderSymbol()==Symbol())
     {
      if(OrderType()==OP_BUY)

        {
         if(Bid<=shortEma)
           {
            OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet);

            return(0);

           }
this layout shows that you are saying if it is a sell order and if it is a buy order...
 
serpentsnoir:

you should not use <=OP_SELL, use ==

also, your statement,

will never be executed because it is following the opposite condition

this layout shows that you are saying if it is a sell order and if it is a buy order...

Thanks serpentsnoir,here is my new code,bit it also does not work,the problemm is the same.

if( Ask > shortEma && UpTrend > 0 && Ask >= UpTrend )
{
ticket=OrderSend(Symbol(),OP_BUY,1,Ask,3,0,Ask+TakeProfit*Point,"moj prvy obchod",12345,0,Green);
if(ticket>0)
return(0);
}

=

=

if(Bid < shortEma && DownTrend > 0 && Bid <= DownTrend )
{
ticket=OrderSend(Symbol(),OP_SELL,1,Bid,3,0,Ask-TakeProfit*Point,"moj prvy obchod",1234,0,Red);
if(ticket>0)

=

=

=

=
for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

if(OrderType()==OP_BUY)

{
if(Bid <= shortEma)
{
OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet);

return(0); }

 

  1. Always count down in the presence of multiple orders (multiple charts) Filter by magic number. What is the value of total?
        for(pos = OrdersTotal()-1; pos >= 0 ; pos--) if (
            OrderSelect(pos, SELECT_BY_POS)                 // Only my orders w/
        &&  OrderMagicNumber()  == magic.number             // my magic number
        &&  OrderSymbol()       == Symbol() ){              // and my pair.
            if((OrderType()==OP_BUY  && Bid<=shortEma)
            || (OrderType()==OP_SELL && Bid>=shortEma)
            ){
                OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,Violet);
            }
        }
        return(0);

  2. EA's must adjust TP, SL AND slippage for 5 digit brokers. On ECN brokers you must orderSend and THEN set stops.
    //++++ These are adjusted for 5 digit brokers.
    int     pips2points;    // slippage  3 pips    3=points    30=points
    double  pips2dbl;       // Stoploss 15 pips    0.0015      0.00150
    int     Digits.pips;    // DoubleToStr(dbl/pips2dbl, Digits.pips)
    int     init(){
        if (Digits == 5 || Digits == 3){    // Adjust for five (5) digit brokers.
                    pips2dbl    = Point*10; pips2points = 10;   Digits.pips = 1;
        } else {    pips2dbl    = Point;    pips2points =  1;   Digits.pips = 0; }
        // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl
    

 

 UpTrend = iCustom(NULL, 0, "SuperTrend",Periods,Multiplier,0,0);
     DownTrend = iCustom(NULL, 0, "SuperTrend",Periods,Multiplier,1,0);
    if(UpTrend>5000)
         {
         UpTrend = 0;
         TrendDirection = "Down";
         }
      if(DownTrend > 5000)
         {
         DownTrend = 0;
         TrendDirection = "Up";
         }
      
      shortEma =iMA(NULL,0,14,0,MODE_EMA,PRICE_CLOSE,0);
      SAR0 = iSAR(NULL,0,0.06,0.6,0);
      SAR1 = iSAR(NULL,0,0.06,0.6,1);
      STOP = iMA(NULL,0,7,0,MODE_EMA,PRICE_CLOSE,0);
      double range = (High [1] - Low[1])*2/3;
      double takeprofit = Open[0] + range;
      
      total=OrdersTotal();
      if(total<10)
      {      
         if( Ask > shortEma && UpTrend > 0 && Ask >= UpTrend )
    
              {
               ticket=OrderSend(Symbol(),OP_BUY,1,Ask,3,0,0,"first trade",12345,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);
                }

       
         if(Bid < shortEma && DownTrend > 0 && Bid <= DownTrend ) 
         //&& DownTrend > 0 && Close[0] <= DownTrend && Close[1] > DownTrend
         //SAR1 < Low[1] && SAR0 > High[0]  && Bid < shortEma && Close[1] > DownTrend 
            {
            ticket=OrderSend(Symbol(),OP_SELL,1,Bid,3,0,0,"first trade",1234,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);
      }
for(pos = OrdersTotal()-1; pos >= 0 ; pos--) if (
        OrderSelect(pos, SELECT_BY_POS)                 // Only my orders w/
    &&  OrderMagicNumber()  == magic.number             // my magic number
    &&  OrderSymbol()       == Symbol() ){              // and my pair.
        if((OrderType()==OP_BUY  && Bid<=shortEma)
        || (OrderType()==OP_SELL && Bid>=shortEma))
        {
            OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet);
        }
    }
    return(0);           
  
  return(0);
}
hello,it stil does not work.It still did not close my positions :-(I dnont know where is a problemm.Can you help me?Thanks
 

Try used same magic number:

ticket=OrderSend(Symbol(),OP_BUY,1,Ask,3,0,0,"first trade",magic.number,0,Green);
ticket=OrderSend(Symbol(),OP_SELL,1,Bid,3,0,0,"first trade",magic.number,0,Red);
Reason: