Set new pending order by an opposite order

 

Hi Guys,

I'm working on my new EA which has an ability to open a pending order in the opposite direction of the last closed order. I've been searching and testing for days now but there's a weird malfunction (sometimes EA is opening a lot pending orders at once, and most of the time not opening any pending orders aymore) and I can't solve it.

Is there anyone who could help me out here?

string lastOrderType;
double lastOrderPrice;
datetime lastOrderTime  = 0;
int  lastOrderTicket = -4;
bool SignalOK;

for(int j = 0 ; j<OrdersTotal() ; j++) 
         {
            if (OrderSelect(j,SELECT_BY_POS,MODE_TRADES)==True)
                  {
                     if(OrderType()<2)
                        {
                             if(OrderOpenTime()> lastOrderTime)
                                   {
                                       lastOrderTime   = OrderOpenTime();
                                       lastOrderType = OrderType();
                                       lastOrderPrice = OrderOpenPrice();
                                       SignalOK = true;
                                       break;
                                   }
                              else     continue;
                         }
                     else   continue;
                  }
             else      continue;
          }
  
    
    


if (lastOrderType == OP_BUY && SignalOK == true)
{
               NewSellTicket = OrderSend(_Symbol,OP_SELLSTOP,LotSize,lastOrderPrice,MaxSlippage,0,Bid-100,"New Sell Ticket 01",MagicNumber,0,clrRed);
               SignalOK = false;
               Sleep(1000);
              
         
       
}

if (lastOrderType == OP_SELL && SignalOK == true)
{      
               NewBuyTicket = OrderSend(_Symbol,OP_BUYSTOP,LotSize,lastOrderPrice,MaxSlippage,0,Ask+100,"New Buy Ticket 01",MagicNumber,0,clrGreen);         
               SignalOK = false;
               Sleep(1000);
               
    
}
}

Thanks in advance! 

 
Bjorn Beerens:

Hi Guys,

I'm working on my new EA which has an ability to open a pending order in the opposite direction of the last closed order. I've been searching and testing for days now but there's a weird malfunction (sometimes EA is opening a lot pending orders at once, and most of the time not opening any pending orders aymore) and I can't solve it.

Is there anyone who could help me out here?

Thanks in advance! 

Why are you checking open orders?



                             if(OrderOpenTime()> lastOrderTime)
                                   {
                                       lastOrderTime   = OrderOpenTime();
                                       lastOrderType = OrderType();
                                       lastOrderPrice = OrderOpenPrice();
                                       SignalOK = true;
                                       break;
                                   }

Why the break after the first run through the loop?

else     continue;

None of these are necessary

if (lastOrderType == OP_BUY && SignalOK == true)
{
               NewSellTicket = OrderSend(_Symbol,OP_SELLSTOP,LotSize,lastOrderPrice,MaxSlippage,0,Bid-100,"New Sell Ticket 01",MagicNumber,0,clrRed);
               SignalOK = false;
               Sleep(1000);
}

You are not checking if the stop order has already been opened

 

Hi Keith,

Thanks for your reply.

The break I added in my search to find a solution, originally it wasn't there... But I thought it was the cause of the 'echo' (a lot of orders at the same moment). 

Same for the Sleep command, just trying to find the cause of the problem...


I'm trying to open a STOP order in the opposite direction of the last closed order but with the opening value of the last closed order. So if the last BUY opened at 1.05 and closed at 1.15 I want to set a SELL_STOP at 1.05 at the moment the buy closes...

Is this possible is an simple way?

 
Bjorn Beerens:

Hi Keith,

Thanks for your reply.

The break I added in my search to find a solution, originally it wasn't there... But I thought it was the cause of the 'echo' (a lot of orders at the same moment). 

Same for the Sleep command, just trying to find the cause of the problem...


I'm trying to open a STOP order in the opposite direction of the last closed order but with the opening value of the last closed order. So if the last BUY opened at 1.05 and closed at 1.15 I want to set a SELL_STOP at 1.05 at the moment the buy closes...

Is this possible is an simple way?

Do it in steps.

First thing to do is to find the last closed order, you won't find it by looping through open trades.

Once you have coded that, print out the trade details so that you know that your code works.

Then and only then move on to the next section of code.

 
hi,
I want to get such expert for pending order and place order on last hit order.Can anybody help me?
 
alitext:
hi,
I want to get such expert for pending order and place order on last hit order.Can anybody help me?

Working on it... :-)

Reason: