SELLLIMIT & BUYLIMIT Orders - Reopening Issue

 

Hi. I need some help with this code.

When I push play, the EA opens SELLIMIT orders and BUYLIMIT orders, that's ok.

When the price goes up and reaches SELLLIMIT order #1, the order is triggered, and becomes a SELL position, that's ok.

The problem is when the price return (goes down), below the price of position #1, the EA set another SELLLIMIT order at the same price of the SELL order #1 already opened. I would like to have only one order for each price, not duplicated orders.

I've been trying some changes but still can't figure out. So any help is greatly appreciated.

This is an example of sell limits orders set at the start:

//----Set orders Buys
 if(!ExistPositions(Symbol(),OP_BUY,MagicNumber)){
   for(int z =0;z<quantity;z++)
    {
    double levels_b = level_buy+z*steps*Point; 
    if(!ExistOrdersByPrice(Symbol(), OP_SELLLIMIT, MagicNumber, levels_b) && 
       !ExistPosByPrice(Symbol(), OP_BUY, MagicNumber, levels_b) && 
       levels_b - Ask > STOPLEVEL)
    {
    if (StopLoss  >0)  sl=levels_b-StopLoss*Point;   else sl=0;
    if (TakeProfit >0) tp=levels_b+TakeProfit*Point; else tp=0;
    SetOrder(Symbol(), OP_SELLLIMIT, Lots, levels_b, sl, tp, MagicNumber );
    }        
    }
   //----Delete if there is excess of BuyLimit orders
   for(int zd =0;zd<=10;zd++)
    {
    double levels_bd = level_buy+(zd+quantity)*steps*Point; 
    if(ExistOrdersByPrice(Symbol(), OP_BUYLIMIT, MagicNumber, levels_bd))
    CloseOrderBySelect();       
    }
 }
//----
//----Set orders Sells
 if(!ExistPositions(Symbol(),OP_SELL,MagicNumber)){
   for(int y =0;y<quantity;y++)
    {
    double levels_s = level_sell-y*steps*Point; 
    if(!ExistOrdersByPrice(Symbol(), OP_BUYLIMIT, MagicNumber, levels_s) && 
       !ExistPosByPrice(Symbol(), OP_SELL, MagicNumber, levels_s) && 
       Bid - levels_s > STOPLEVEL)
    {
    if (StopLoss  >0)  sl=levels_s+StopLoss*Point;   else sl=0;
    if (TakeProfit >0) tp=levels_s-TakeProfit*Point; else tp=0;
    SetOrder(Symbol(), OP_BUYLIMIT, Lots, levels_s, sl, tp, MagicNumber);
    }        
    }
   //----Delete if there is excess of SellLimit orders
   for(int yd =0;yd<=10;yd++)
    {
    double levels_sd = level_sell-(yd+quantity)*steps*Point; 
    if(ExistOrdersByPrice(Symbol(), OP_SELLLIMIT, MagicNumber, levels_sd))
    CloseOrderBySelect();       
    }
 }
 

In more simple words. For example, if there is a SELLLIMIT order at 1.3500, then the price reaches that order, and becomes a SELL order. So now I have a SELL order at 1.3500 but if the price goes down, the EA opens a new SELLLIMIT order at 1.3500. And now I have one SELL order at 1.3500 and one SELLLIMIT order at 1.3500.

I would like to avoid duplicated orders at the same price. I mean, if 1.3500 is opened, then don't open a new SELLLIMIT order at that same price (1.3500 in this example).

Some help please. Thanks!

 
af1: I would like ...

So what's stopping you? Find your sell limit and or sell orders in a orderSelect loop. Remember the prices. Decide if and where to open another.

 
WHRoeder:

So what's stopping you? Find your sell limit and or sell orders in a orderSelect loop. Remember the prices. Decide if and where to open another.


Thanks for answer WHRoeder. That could be a solution, or maybe just maintaining always opened certain amount of SELLLIMIT orders above the price level. 10 orders for example. In this way at the moment of a SELLLIMIT order become a SELL order, then the EA detects that there is only 9 SELLLIMIT orders, then the EA place another SELLLIMIT order to complete 10. And so on.

For example:


 
af1:


Thanks for answer WHRoeder. That could be a solution, or maybe just maintaining always opened certain amount of SELLLIMIT orders above the price level. 10 orders for example. In this way at the moment of a SELLLIMIT order become a SELL order, then the EA detects that there is only 9 SELLLIMIT orders, then the EA place another SELLLIMIT order to complete 10. And so on.

For example:


so you can explain it as you want.....
place only new selllimit for prices higher then highest selllimitprice

and place only new buylimit for prices lower then lowest buylimitprice

check prices .....

 
deVries:

so you can explain it as you want.....
place only new selllimit for prices higher then highest selllimitprice

and place only new buylimit for prices lower then lowest buylimitprice

check prices .....


Hi deVries, thanks for helping also. Yes, placing new selllimit orders higher than the highest order could solve this. The code below is where I made changes for SELLLIMIT orders. But the EA keeps open SELLLIMIT orders when price return (goes down).

//----Set orders Sell limit
 if(!ExistPositions(Symbol(),OP_SELL,MagicNumber)){
   for(int z =0;z<quantity;z++)
    {
    double levels_b = level_buy+z*steps*Point; 
    if(!ExistOrdersByPrice(Symbol(), OP_SELLLIMIT, MagicNumber, levels_b) && 
       !ExistPosByPrice(Symbol(), OP_BUY, MagicNumber, levels_b) && 
       levels_b - Ask > STOPLEVEL)
    {
    if (StopLoss  >0)  sl=levels_b-StopLoss*Point;   else sl=0;
    if (TakeProfit >0) tp=levels_b+TakeProfit*Point; else tp=0;
    SetOrder(Symbol(), OP_SELLLIMIT, Lots, levels_b, sl, tp, MagicNumber );
    }        
    }
 
af1:


Hi deVries, thanks for helping also. Yes, placing new selllimit orders higher than the highest order could solve this. The code below is where I made changes for SELLLIMIT orders. But the EA keeps open SELLLIMIT orders when price return (goes down).


show how you count number open trades different ordertype

and how you get highest open selllimitprice

and how you get lowest open buylimitprice

so a simple orderloop checking the open trades of your EA on your account

 
deVries:


show how you count number open trades different ordertype

and how you get highest open selllimitprice

and how you get lowest open buylimitprice

so a simple orderloop checking the open trades of your EA on your account


int i = 0;
 double level_buy =Displacement*Point, level_sell = 0;
   
 while(level_buy<Bid){level_buy += steps*Point;i++;}//search the first level above the current price
 level_sell = level_buy - steps*Point; // first level below the price
 
af1:
int i = 0;
 double level_buy =Displacement*Point, level_sell = 0;
   
 while(level_buy<Bid){level_buy += steps*Point;i++;}//search the first level above the current price
 level_sell = level_buy - steps*Point; // first level below the price

Can you tell me with that code how many buylimittrades you have ??

and the number of selllimittrades ??

the price of highest selllimitprice ??

the price of lowest buylimittrades ??

for that you have to use a simple orderloop checking the open trades of your EA on your account

with inside a line like

     for(int i = OrdersTotal()-1; i >= 0 ; i--)
        {//2

so come on show what you can....

 

I'm gonna make a wild guess and say that your ExistOrdersByPrice() is failing because of a Price != Price issue. Again this is just a guess as I'm not seeing whats within the function. In general, you'll want to use better debugging techniques. If its getting OK from an if statement || function() which it shouldn't, then you'll want to isolate the function or statement ... with more Print Commands.

If thats not the problem then sorry. Post more of your functions so I wouldn't have to guess whats inside.

 
deVries:

Can you tell me with that code how many buylimittrades you have ??

and the number of selllimittrades ??

the price of highest selllimitprice ??

the price of lowest buylimittrades ??

for that you have to use a simple orderloop checking the open trades of your EA on your account

with inside a line like

so come on show what you can....

Here is the part of the code where the Expert set SELLIMIT and BUYLIMIT orders.

The Expert set 10 SELLLIMIT orders above the Price Level, and set 10 BUYLIMIT orders below the Price Level.

Based on that, the Expert allways keep 10 limit orders ready for each side, and that is fine. I just want to change the price level where the Expert set the first limit orders for both sides.

In simpler words,
SELLLIMIT orders should be set above the highest SELL opened order.
BUYLIMIT orders should be set below the lowest BUY opened order.

extern string ____1___         = "Settings orders";
extern int    Displacement     = 0;            // offset the price levels of orders 
extern int    steps            = 10;           // distance between the orders, a step rastanovki
extern bool   Line             = false;        // The first line of levels for visual inspection
extern int    quantity         = 10;           // number of orders in the same direction

//===============================================================
 int i = 0;
 double level_buy =Displacement*Point, level_sell = 0;
   
 while(level_buy<Bid){level_buy += steps*Point;i++;}//search the first level above the current price
 level_sell = level_buy - steps*Point; // first level below the price
      
 if(Line){SetHLine(Lime,"r", level_buy);SetHLine(Red,"s", level_sell);}//check the initial levels   
//----
 double sl = 0, tp = 0;
//----Set orders SellLimit
 if(!ExistPositions(Symbol(),OP_BUYLIMIT,MagicNumber)){
   for(int z =0;z<quantity;z++)
    {
    double levels_b = level_buy+z*steps*Point; 
    if(!ExistOrdersByPrice(Symbol(), OP_SELLLIMIT, MagicNumber, levels_b) && 
       !ExistPosByPrice(Symbol(), OP_BUY, MagicNumber, levels_b) && 
       levels_b - Ask > STOPLEVEL)
    {
    if (StopLoss  >0)  sl=levels_b-StopLoss*Point;   else sl=0;
    if (TakeProfit >0) tp=levels_b+TakeProfit*Point; else tp=0;
    SetOrder(Symbol(), OP_SELLLIMIT, Lots, levels_b, sl, tp, MagicNumber );
    }        
    }
   //----Delete if there is excess of orders
   for(int zd =0;zd<=10;zd++)
    {
    double levels_bd = level_buy+(zd+quantity)*steps*Point; 
    if(ExistOrdersByPrice(Symbol(), OP_SELLLIMIT, MagicNumber, levels_bd))
    CloseOrderBySelect();       
    }
 }
//----
//----Set orders BuyLimit
 if(!ExistPositions(Symbol(),OP_SELLLIMIT,MagicNumber)){
   for(int y =0;y<quantity;y++)
    {
    double levels_s = level_sell-y*steps*Point; 
    if(!ExistOrdersByPrice(Symbol(), OP_BUYLIMIT, MagicNumber, levels_s) && 
       !ExistPosByPrice(Symbol(), OP_SELL, MagicNumber, levels_s) && 
       Bid - levels_s > STOPLEVEL)
    {
    if (StopLoss  >0)  sl=levels_s+StopLoss*Point;   else sl=0;
    if (TakeProfit >0) tp=levels_s-TakeProfit*Point; else tp=0;
    SetOrder(Symbol(), OP_BUYLIMIT, Lots, levels_s, sl, tp, MagicNumber);
    }        
    }
   //----Delete if there is excess of orders
   for(int yd =0;yd<=10;yd++)
    {
    double levels_sd = level_sell-(yd+quantity)*steps*Point; 
    if(ExistOrdersByPrice(Symbol(), OP_BUYLIMIT, MagicNumber, levels_sd))
    CloseOrderBySelect();       
    }
 }
//-----------
   return(0);
  }