Close all positions at a line

 

Hi

Before i request this in freelance, does anyone have a MT4 script that will close all my positions at a certain level?

(even if i add more positions).   I have had a thorough look, and can see scripts for pretty much everything apart

from what i need :-) 

Thanks.

Kate

 

Hi Kate,

Not yet but it's easy and fast to do it.


<link removed>


Regards,

David.

 

If you want to close orders at a given price, something like this should work:

 

#property strict
#property show_inputs

input double price = 0; // Here you set required price 
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
      for(int i=OrdersTotal()-1;i>=0;i--)
        {
         if(OrderSelect(i, SELECT_BY_POS))
            if(OrderSymbol() == Symbol() && OrderOpenPrice() == price)
               bool ok = OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 30, clrRed);         
        }
  }

 

Drop this script on the chart. It will close order(s) of the symbol on the chart that are opened at given price.

 
Drazen Penic:

If you want to close orders at a given price, something like this should work:

There should be at least a minimum and maximum range. It is possible for the market to skip the exact price level.
 

I think the code should check every order type and use a condition as Mr. Enrico suggest ...

Like this ...

#property strict
#property show_inputs

input double price = 0; // Here you set required price 
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart(){
//---
      for(int i=OrdersTotal()-1;i>=0;i--){

         if(OrderSelect(i, SELECT_BY_POS))
            if(OrderSymbol() == Symbol() && OrderType()==OP_BUY && OrderOpenPrice() >= price && price != 0)
               bool ok = OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 30, Blue);         

            if(OrderSymbol() == Symbol() && OrderType()==OP_SELL && OrderOpenPrice() <= price && price != 0)
               bool ok = OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 30, RED);         

        }
  }

Added Order type to check the position of price related to the order type.

Added price condition to not be 0 to prevent false executions. 

 I think, this will work !!!. 

 
Many many thanks, i will try this out. (I'm away for a few days first :-) Hugs Kate xx