Download MetaTrader 5

Help with code?

To add comments, please log in or register
Added support for the Spanish language. Come and check it out!
jurcekmpt
489
jurcekmpt 2014.12.15 09:20 

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! 

Carl Schreiber
6610
Carl Schreiber 2014.12.15 10:10  

How shall we know why this happens?

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

jurcekmpt
489
jurcekmpt 2014.12.15 11:03  

Will post code when I get home.

Maybe any idea how to code such EA?

jurcekmpt
489
jurcekmpt 2014.12.15 19:58  
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?
Carl Schreiber
6610
Carl Schreiber 2014.12.15 21:59  

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..

jurcekmpt
489
jurcekmpt 2014.12.21 14:08  

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? 

SYED NAUFAL GADDAFI
1401
SYED NAUFAL GADDAFI 2014.12.21 15:11  
Of course. If you logic correctly reflect your idea though.
jurcekmpt
489
jurcekmpt 2014.12.21 18:38  

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! 

whroeder1
14544
whroeder1 2014.12.21 19:49  
01005379: for order that was closed last?
Go through history and find the last closed order.
jurcekmpt
489
jurcekmpt 2014.12.23 10:20  
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?
Keith Watford
Moderator
9648
Keith Watford 2014.12.23 10:30  

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 

12
To add comments, please log in or register