Help with code?

 

Hello!

I would like to write an EA which will set pending orders. I will set pending orders manualy. Let's say at 1.3000 BuyStop and 1.2950 SellLimit (curreny price is below). When/if pending orders are reached nothing happened. When pending order close at TP or SL, EA has to place same pending order, at same price (where last pending order was), SL and TP.

Thank you! 

 

How shall we know why this happens?

Error messages? Code? Please use the SRC-button beside the camera!

 

Will post code when I get home.

Maybe any idea how to code such EA?

 
extern double Buy1=1.3400;

for(int cnt1=0;cnt1<OrdersTotal();cnt1++){
      OrderSelect(cnt1,SELECT_BY_POS,MODE_TRADES);
      if(  OrderOpenPrice()==Buy1)
         bool Buy1_placed=true;
      else
         Buy1_placed=false;
   }
      
   if(!Buy1_placed){
      ticket1=-1;
      while (ticket1<0){
         RefreshRates();
         if(Bid<Buy1){
            ticket1=OrderSend(Symbol(),OP_BUYSTOP,Loti,Buy1,5*places,0,0, "EA_pending",MagicNumber,0,Green);            
            //Sleep(100);
         }
         else
            ticket1=OrderSend(Symbol(),OP_BUYLIMIT,Loti,Buy1,5*places,0,0, "EA_pending",MagicNumber,0,Green);
      }
   }
Any idea why EA opens more then one order at same price?
 

There are many reasons due to your code!

1) Look at the examples in the Book for proper examples!

2) Every open trade will probably cause a new position due to this:

OrderSelect(cnt1,SELECT_BY_POS,MODE_TRADES);
if(  OrderOpenPrice()==Buy1)

The reasons are

a) the troubles with the doubles, b) you do not filter for open trades that do not belonging to "Buy1" (see the examples) c) if OrderSelect(..) fails ..!

a) can be solved like the way of the article, I'd prefer:

if( fabs(OrderOpenPrice()-Buy1) < Point )

b) can be catched by add things like:

if ( OrderSymbol() != _Symbol ) continue;
if ( OrderMagicNumber() != MyMagic ) continue;
 

 c) what if

OrderSelect(cnt1,SELECT_BY_POS,MODE_TRADES);

fails? You don't check and the OrderopenPrice() could be 0.0 and cause a new position..

 

Hey!

 

Thanks for the answer.

I would like to know what do you think about this idea.

I will place orders with scripts and every order will have unique MAGIC NUMBER. Then with EA I will go through all orders - open and pending. If there will be no order with
one of my magic number I will open another one.

 

Will this work? 

 
Of course. If you logic correctly reflect your idea though.
 

Will try and see. :)

I have Buy with magic number buy1. How can I get this order SL and TP when this order close?
I know I have to go true orders history, but what if I have more orders with same magic number and I want only SL and TP for order that was closed last?

Thanks! 

 
01005379: for order that was closed last?
Go through history and find the last closed order.
 
for(int i=OrdersHistoryTotal()-1;i>=0;i--){
   OrderSelect(i, SELECT_BY_POS,MODE_HISTORY);  //error was here
   if(OrderSymbol()==Symbol() && OrderMagicNumber()==magic){
       int LastType=OrderType();
       double LastOpenPrice=OrderOpenPrice();
       double LastSL=OrderStopLoss();
       double LastTP=OrderTakeProfit();
       int LastMagic=OrderMagicNumber();
   }
}

if(LastType == 0){
   if(Ask < LastOpenPrice){
      OrderSend(Symbol(),OP_BUYSTOP,0.1,LastOpenPrice,slippage,LastSL,LastTP,"",LastMagic,0,CLR_NONE);
   }
   else if(Ask > LastOpenPrice){
      OrderSend(Symbol(),OP_BUYLIMIT,0.1,LastOpenPrice,slippage,LastSL,LastTP,"",LastMagic,0,CLR_NONE);
   }
}
else if( LastType == 1){
   if(Bid < LastOpenPrice){
      OrderSend(Symbol(),OP_SELLLIMIT,0.1,LastOpenPrice,slippage,LastSL,LastTP,"",LastMagic,0,CLR_NONE);
   }
   else if(Bid > LastOpenPrice){
      OrderSend(Symbol(),OP_SELLSTOP,0.1,LastOpenPrice,slippage,LastSL,LastTP,"",LastMagic,0,CLR_NONE);
   }
}
I would like to open last closed order again. Is this the right way to do it?
 

Does this even compile without the ()  ??

       double LastSL=OrderStopLoss ;
       double LastTP=OrderTakeProfit ;

 You are not checking to see if the selected order is the last order or not

Compare OrderCloseTimes to find the last closed order 

Reason: