Check each trade if it is far enought from the market?

 

Hello guy,

could anybody help me? I am trying to write a function that checks every trade opened by the EA if the distance from each trade is more than "GridDistance". GridDistance usually about 20 pips.

Right now I am counting if all the trades match the criteria and that I compare it to OrdersTotal(), which obviously is not perfect. I would like it to work its own.

Could anyone help me please?

 

static int ShortCheck = 0;
                     for (d = OrdersTotal()-1;d>=0;d--) {
                        if (OrderSelect (d, SELECT_BY_POS, MODE_TRADES)){      
                           if ( OrderMagicNumber() == MagicMain ){
                           if  ( OrderSymbol() == Symbol())  { 
                              double Shortline = OrderOpenPrice() - GridDistance*Point;
                              double Longline = OrderOpenPrice() + GridDistance*Point;
                                 if ( (Shortline > Bid) || (Longline < Bid) ){     
                                    ShortCheck++;
                                 }
                              }
                           }     
                        }
                     }
                     if (ShortCheck==OrdersTotal()){
                        Check = OrderSend(Symbol(), OP_SELL, MMLots, Bid, Slippage, SellSL, FirstShortTP, "Blok ", MagicMain+i, 0, OrderColor); 
                        if (Check<0) Print ("Opening new trade error ", GetLastError());
                     }

                     ShortCheck = 0;

 

Thanks a lot,

 

Josif 

 

 

Your code won't work if there are other trades open with a different magic number or symbol because the count will not equal OrdersTotal()

Why not use a bool instead?

bool ShortCheck = true;
                     for (d = OrdersTotal()-1;d>=0;d--) {
                        if (OrderSelect (d, SELECT_BY_POS, MODE_TRADES)){      
                           if ( OrderMagicNumber() == MagicMain ){
                           if  ( OrderSymbol() == Symbol())  { 
                              double Shortline = OrderOpenPrice() - GridDistance*Point;
                              double Longline = OrderOpenPrice() + GridDistance*Point;
                                 if ( (Shortline > Bid || Longline < Bid)==false ){     
                                    ShortCheck=false;
                                    break;
                                 }
                              }
                           }     
                        }
                     }
                     if (ShortCheck){
                        Check = OrderSend(Symbol(), OP_SELL, MMLots, Bid, Slippage, SellSL, FirstShortTP, "Blok ", MagicMain+i, 0, OrderColor); 
                        if (Check<0) Print ("Opening new trade error ", GetLastError());
                     }

 Just a suggestion, not compiled or tested

 
or almost what you had
 if (ShortCheck==OrdersTotal()){
 if (ShortCheck > 0){
 
WHRoeder:
or almost what you had

This wont work. Here is how it should look like: 

  

 

Unfortunately this fails if there is a gap, than it suddenly opens all trades at once. Also, as I stated above, I check it against all trades, which will obviously fail one its on demo and there are more trades. 

 
GumRai:

Your code won't work if there are other trades open with a different magic number or symbol because the count will not equal OrdersTotal()

Why not use a bool instead?

 Just a suggestion, not compiled or tested

Surprisingly (for me), this seems to be at least opening trades.. Unfortunatelly on in one direction, and that is it it is higher.. Look at the image:

 There are missing trades on the way down...

 


I do not understand this though:

if ( (Shortline > Bid || Longline < Bid)==false ){     
                                   

                                  

 Any suggestions? 

 I am really greatefull for any help, this is quite complex ea...

Thanks a lot! 

 
Josif201:

Surprisingly (for me), this seems to be at least opening trades.. Unfortunatelly on in one direction, and that is it it is higher.. Look at the image:

 There are missing trades on the way down...

 


I do not understand this though:

if ( (Shortline > Bid || Longline < Bid)==false )

                                  

 Any suggestions? 

 I am really greatefull for any help, this is quite complex ea...

Thanks a lot! 

 

We have no idea what other criteria you may have coded to open a trade. There is nothing in your posted code to open a Buy trade, so we can't comment on that.

if (Shortline > Bid || Longline < Bid )

 This would check if Bid is more than GridDistance away from the open price of any trade

so

if ( (Shortline > Bid || Longline < Bid)==false )

 this checks if at least one trade's open price is less than GridDistance away from Bid

Reason: