Download MetaTrader 5

Open Order immediately after last Oder hit Stop Loss

To add comments, please log in or register
Enable MQL5 Storage to store your source codes. It is free!
saro2018
29
saro2018 2015.06.19 20:47 

Hello everyone, I have a question.

Can anyone help me coding the following thing.

I want to open a Market Order immeadiately after the last Order hit Stop Loss. How can I do that? I have my market order conditions and then I want right after one of the condition order hit sl, open a new order the opposite direction. The Stop should be the the same like the last one and the Take Profit should be the Pips from the last Order that went into SL.

Can anyone help me with that?


Thanks for any help :)

DeanDeV
330
DeanDeV 2015.06.20 13:50  
saro2018:

Hello everyone, I have a question.

Can anyone help me coding the following thing.

I want to open a Market Order immeadiately after the last Order hit Stop Loss. How can I do that? I have my market order conditions and then I want right after one of the condition order hit sl, open a new order the opposite direction. The Stop should be the the same like the last one and the Take Profit should be the Pips from the last Order that went into SL.

Can anyone help me with that?


Thanks for any help :)

You should post your attempt with your post! 

for(int n=OrdersHistoryTotal()-1; n>=0; n--)        //Search history
 {
  if(!OrderSelect(n,SELECT_BY_POS,MODE_HISTORY)) continue;
   if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
       if(TimeCurrent()-OrderCloseTime()==0)      //When order closes
        {
         if(OrderType()==OP_BUY && OrderClosePrice()<=OrderOpenPrice())  //If buy order and close was less than open --> stop loss
           { 
            OrderSend()
           }
         
         else if(OrderType()==OP_SELL && OrderClosePrice()>=OrderOpenPrice())    //If sell order and close was greater than open --> stop loss
           {
            OrderSend()
           }
        }
 }
saro2018
29
saro2018 2015.06.20 14:11  

Here ist my attempt, I have tried to add this code below the conditions. 

Unfortunatley it isnt working..

Or should I add the condition to another place?

void escalateOrders()
   {  
      // if reahces SL and trade is loss, then escalate trade
      for (int i = OrdersTotal(); i > 0; i--)
         {
            if(!OrderSelect(i, SELECT_BY_POS)) Print("Error: Unable to select order! code #", GetLastError());
            
            if(OrderMagicNumber()==123456)
               {
                  if(OrderType() == OP_BUY)
                     {
                        if(Bid <= StopLoss) // Stoploss has been hit
                           {
                                  if(!OrderSelect(ticket, SELECT_BY_TICKET)) Print("Error: Unable to select automatically closed order! code #", GetLastError());                                                                                        
                                    // Open order in opposite direction
                                    
                                     if(OrderProfit() < 0) // Position is a loss
                                     {
                                    
                                    ticket=!OrderSend(Symbol(),OP_SELL,Lots,Bid,2,25,10); 
                                    total++;
                                     }
                                 }// end if
                           }// end if
                     // end if
                  if(OrderType() == OP_SELL)
                     {
                        if(Ask >= StopLoss) // Stoploss has been hit
                           {
                              
                              
                              if(!OrderSelect(ticket, SELECT_BY_TICKET)) Print("Error: Unable to select automatically closed order! code #", GetLastError());
                            
                              
                              if(OrderProfit() < 0) // Position is a loss
                                 {
                                   
                                    
                                   
                                    
                                    // Open order in opposite direction
                                  
                                    ticket=!OrderSend(Symbol(),OP_BUY,Lots,Bid,2,25,20);
                                    ticket++;
                                   
                                 }// end if
                           }// end if Ask <= OrderStopLoss
                     }// end if OrderType == OP_SELL
               }// end if OrderMagicNumber < 20
}}
Thanks :)
whroeder1
14514
whroeder1 2015.06.20 20:36  
saro2018: Unfortunatley it isnt working..
 for (int i = OrdersTotal(); i > 0; i--)
         {
            if(!OrderSelect(i, SELECT_BY_POS)) Print("Error: Unable to select order! code #", GetLastError());
            
            :
                                  if(!OrderSelect(ticket, SELECT_BY_TICKET)) 
  1. "Doesn't work" is meaningless - just like saying the car doesn't work. Doesn't start, won't go in gear, no electrical, missing the key, flat tires - meaningless. There are no mind readers here.
  2. Positions are numbers [0 .. Total-1] You will always get an error the first iteration.
  3. Why do you continue processing when you have not selected an order?
  4. Why are you selecting a ticket when you just selected an order?
  5.                                     ticket=!OrderSend(Symbol(),OP_BUY,Lots,Bid,2,25,20);
                                        ticket++;
    Why are you taking a ticket number (orderSend,) converting it to Bool (!,) converting it to an int (ticket), and then incrementing it?
  6. If you want want to open after the last order closes, then count how many there are in your order select filtered loop. Don't do anything there.
saro2018
29
saro2018 2015.06.20 22:29  

Hey WHRoeder,

yes your true, I just meant the code has an error ;)

So, if I want to open a new order after the last order went into Stop Loss, I need to count how many Orders I have in my order select filtered loop? Do I need to embed the select filtered loop into the order contitions loop, or extern? Also if I have then count the Orders, what do I need then to do? And what should I dont do?

saro2018
29
saro2018 2015.06.22 20:07  
WHRoeder:
  1. "Doesn't work" is meaningless - just like saying the car doesn't work. Doesn't start, won't go in gear, no electrical, missing the key, flat tires - meaningless. There are no mind readers here.
  2. Positions are numbers [0 .. Total-1] You will always get an error the first iteration.
  3. Why do you continue processing when you have not selected an order?
  4. Why are you selecting a ticket when you just selected an order?
  5. Why are you taking a ticket number (orderSend,) converting it to Bool (!,) converting it to an int (ticket), and then incrementing it?
  6. If you want want to open after the last order closes, then count how many there are in your order select filtered loop. Don't do anything there.
any advices for me?
whroeder1
14514
whroeder1 2015.06.23 02:03  
saro2018: any advices for me?
Answer the questions asked!
JD4
1101
JD4 2015.06.23 05:07  
WHRoeder:
  1. "Doesn't work" is meaningless - just like saying the car doesn't work. Doesn't start, won't go in gear, no electrical, missing the key, flat tires - meaningless. There are no mind readers here.
  2. Positions are numbers [0 .. Total-1] You will always get an error the first iteration.
  3. Why do you continue processing when you have not selected an order?
  4. Why are you selecting a ticket when you just selected an order?
  5. Why are you taking a ticket number (orderSend,) converting it to Bool (!,) converting it to an int (ticket), and then incrementing it?
  6. If you want want to open after the last order closes, then count how many there are in your order select filtered loop. Don't do anything there.
1 isn't totally true.  Doesn't start (and the others) is a lot more helpful than plain "doesn't work".  With those you at least have an idea of where to start looking for problems.  Anything that helps you narrow down the problem to a more specific part can be helpful.
whroeder1
14514
whroeder1 2015.06.23 14:29  
JD4: 1 isn't totally true.  Doesn't start (and the others) is a lot more helpful than plain "doesn't work".  With those you at least have an idea of where to start looking for problems.  Anything that helps you narrow down the problem to a more specific part can be helpful.

You just proved that #1 is totally true. The others are more helpful, the "doesn't work" gives no information at all, so no help can be provided.

Tell me specifically what the OP meant when he posted:

saro2018: Unfortunatley it isnt working..
saro2018
29
saro2018 2015.06.23 19:21  

Okay I diagnosed the error in my EA. The following message always pops up if my EA tries to open a opposite Order immediately: "OrderSend error 130"

I have already tried to change the market order into a pending order to get a certain delay, but without any success. Does anyone can help me with that error code?

Thanks

Aron

saro2018
29
saro2018 2015.06.25 10:49  

NO One can Help me with this Problem? our any suggestion for that?

Thanks

Aron

12
To add comments, please log in or register