Help with OrderClose for ECN broker

 

I would like that the maximum slippage for the OrderClose function to be the distance between the current price action and the preset STOP LOSS

I set it like this but it doesnt work,it gives me an error 4051 for invalid slippage please help:

FOR BUY:

    for(i=0; i<OrdersTotal(); i++)
   { OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
 // if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=magic) continue;
    if(OrderType()==OP_BUY && OrderProfit()<0){OrderClose(OrderTicket(),OrderLots(),Ask,MathAbs(Ask-(STOPLOSSPIPS*Point*10))*(1/Point),CLR_NONE);}
 

AND FOR SELL:

    for(i=0; i<OrdersTotal(); i++)
   { OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
  // if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=magic) continue;
    if(OrderType()==OP_SELL && OrderProfit()<0){OrderClose(OrderTicket(),OrderLots(),Bid,MathAbs(Bid-(STOPLOSSPIPS*Point*10))*(1/Point),CLR_NONE);}
   }

The STOPLOSSPIPS is the external variable to set the stop loss and it's multiplied by 10 because i have fractional pips so thats not the problem i guess!

 
Proximus:

I would like that the maximum slippage for the OrderClose function to be the distance between the current price action and the preset STOP LOSS

I set it like this but it doesnt work,it gives me an error 4051 for invalid slippage please help:

FOR BUY:

AND FOR SELL:

The STOPLOSSPIPS is the external variable to set the stop loss and it's multiplied by 10 because i have fractional pips so thats not the problem i guess!

Slippage is an int not a double . . . read the documentation.
 

You have to count down closing and deleting your orders

Take a look https://www.mql5.com/en/forum/139654 to see why you have to work this way

 
RaptorUK:
Slippage is an int not a double . . . read the documentation.


Should i round it down first? And how to round it down to and int (in an other variable of course)
 
Proximus:

I would like that the maximum slippage for the OrderClose function to be the distance between the current price action and the preset STOP LOSS

I set it like this but it doesnt work,it gives me an error 4051 for invalid slippage please help:

FOR BUY:

AND FOR SELL:

The STOPLOSSPIPS is the external variable to set the stop loss and it's multiplied by 10 because i have fractional pips so thats not the problem i guess!


If I understand you correctly, you have no need of Ask- or Bid- in your calculations.

Remove them and see how it goes

 

Ok i made some corrections implementing what you guys said so please confirm if this is correct now:

Initialization

extern int STOPLOSSPIPS=14;
int PositionIndex; 
int TotalNumberOfOrders;
double slippagex;

SELL close

 TotalNumberOfOrders = OrdersTotal();
for(PositionIndex = 0; PositionIndex < TotalNumberOfOrders; PositionIndex++)  //  <-- for loop to loop through all Orders
   {
   if( ! OrderSelect(PositionIndex, SELECT_BY_POS, MODE_TRADES) ) continue;   // <-- if the OrderSelect fails advance the loop to the next PositionIndex
   
   if( OrderMagicNumber() == magic      // <-- does the Order's Magic Number match our EA's magic number ? 
      && OrderSymbol() == Symbol()         // <-- does the Order's Symbol match the Symbol our EA is working on ? 
      && ( OrderType() == OP_SELL          // <-- is the Order a SELL Order ? 
      && OrderProfit()<0) )      // <-- PROFITABLE OR NOT ?
      
slippagex=MathRound(MathAbs(Bid-(STOPLOSSPIPS*Point))*(1/Point));
      if ( ! OrderClose( OrderTicket(), OrderLots(), Bid,slippagex ) )               // <-- try to close the order
         Print("Order Close failed, order number: ", OrderTicket(), " Error: ", GetLastError() );  // <-- if the Order Close failed print some helpful information 
      
   } //  end of For loop 
  

And BUY close:

TotalNumberOfOrders = OrdersTotal();
for(PositionIndex = 0; PositionIndex < TotalNumberOfOrders; PositionIndex++)  //  <-- for loop to loop through all Orders
   {
   if( ! OrderSelect(PositionIndex, SELECT_BY_POS, MODE_TRADES) ) continue;   // <-- if the OrderSelect fails advance the loop to the next PositionIndex
   
   if( OrderMagicNumber() == magic      // <-- does the Order's Magic Number match our EA's magic number ? 
      && OrderSymbol() == Symbol()         // <-- does the Order's Symbol match the Symbol our EA is working on ? 
      && ( OrderType() == OP_BUY           // <-- is the Order a Buy Order ? 
      && OrderProfit()<0) )      // <-- PROFITABLE O NOT ?
      
slippagex=MathRound(MathAbs(Ask-(STOPLOSSPIPS*Point))*(1/Point));
      if ( ! OrderClose( OrderTicket(), OrderLots(), Ask,slippagex ) )               // <-- try to close the order
         Print("Order Close failed, order number: ", OrderTicket(), " Error: ", GetLastError() );  // <-- if the Order Close failed print some helpful information 
      
   } //  end of For loop 



The STOPLOSSPIPS i've set it without multiplying it with 10 since it is more clear with fractional pips so now 14 initial value means 1.4 pips


==================================================================================================

GumRai:


If I understand you correctly, you have no need of Ask- or Bid- in your calculations.

Remove them and see how it goes


NOPE, If a trade goes wrong i want to close the order with the max slippage value in pips,of the pips left between the current price,in case of sell he bid price,in case of buy the ask price; and the stop loss,

so the slippage is the distance between the current bid or ask price and the stop loss, that means that my losses wont be greater with the close order than the STOPLOSS.
 
Proximus:

Should i round it down first? And how to round it down to and int (in an other variable of course)

Type Cast it to an int type variable

This is not an int . . .

double slippagex;
 
RaptorUK:

Type Cast it to an int type variable

This is not an int . . .


Ok some quick hotfixes

int slippagex;

And i realised what stupidity and complicated stuff i did with the slippage calculation so it should go like this

FOR SELL CLOSE

slippagex=MathAbs(Bid-OrderStopLoss())/Point;
Print("this is the slippage: ",slippagex);
      if ( ! OrderClose( OrderTicket(), OrderLots(), Bid,slippagex ) )               // <-- try to close the order
         Print("Order Close failed, order number: ", OrderTicket(), " Error: ", GetLastError() );  // <-- if the Order Close failed print some helpful information 
      

AND FOR BUY CLOSE

slippagex=MathAbs(Ask-OrderStopLoss())/Point;
Print("this is the slippage: ",slippagex);
      if ( ! OrderClose( OrderTicket(), OrderLots(), Ask,slippagex ) )               // <-- try to close the order
         Print("Order Close failed, order number: ", OrderTicket(), " Error: ", GetLastError() );  // <-- if the Order Close failed print some helpful information 
      

So far it shows from 3-85 (fractional) pip values for the slippage at 99 fractional pip TP and SL settings so i guess it is correct now.

 
Proximus:

Ok some quick hotfixes

And i realised what stupidity and complicated stuff i did with the slippage calculation so it should go like this

FOR SELL CLOSE

AND FOR BUY CLOSE

So far it shows from 3-85 (fractional) pip values for the slippage at 99 fractional pip TP and SL settings so i guess it is correct now.

Did you change the loops to count down ?

deVries:

You have to count down closing and deleting your orders

Take a look https://www.mql5.com/en/forum/139654 to see why you have to work this way


 
RaptorUK:

Did you change the loops to count down ?



Opps i copied the wrong one,thanks for pointing out.So the full picture looks like this now, I VERY HOPE ITS CORRECT NOW!

INITIALIZATION

///correction
int PositionIndex; 
int TotalNumberOfOrders;
int slippagex;


BUY CLOSE:

TotalNumberOfOrders = OrdersTotal();
for(PositionIndex = TotalNumberOfOrders - 1; PositionIndex >= 0 ; PositionIndex --)  //  <-- for loop to loop through all Orders . .   COUNT DOWN TO ZERO !
   {
   if( ! OrderSelect(PositionIndex, SELECT_BY_POS, MODE_TRADES) ) continue;   // <-- if the OrderSelect fails advance the loop to the next PositionIndex
   
   if( OrderMagicNumber() == magic      // <-- does the Order's Magic Number match our EA's magic number ? 
      && OrderSymbol() == Symbol()         // <-- does the Order's Symbol match the Symbol our EA is working on ? 
      && ( OrderType() == OP_BUY           // <-- is the Order a Buy Order ? 
      && OrderProfit()<0) )      // <-- PROFITABLE O NOT ?
      
slippagex=MathAbs(Ask-OrderStopLoss())/Point;
Print("this is the slippage: ",slippagex);
      if ( ! OrderClose( OrderTicket(), OrderLots(), Ask,slippagex ) )               // <-- try to close the order
         Print("Order Close failed, order number: ", OrderTicket(), " Error: ", GetLastError() );  // <-- if the Order Close failed print some helpful information 
      
   } //  end of For loop 

AND SELL CLOSE

  TotalNumberOfOrders = OrdersTotal();
for(PositionIndex = TotalNumberOfOrders - 1; PositionIndex >= 0 ; PositionIndex --)  //  <-- for loop to loop through all Orders . .   COUNT DOWN TO ZERO !
   {
   if( ! OrderSelect(PositionIndex, SELECT_BY_POS, MODE_TRADES) ) continue;   // <-- if the OrderSelect fails advance the loop to the next PositionIndex
   
   if( OrderMagicNumber() == magic      // <-- does the Order's Magic Number match our EA's magic number ? 
      && OrderSymbol() == Symbol()         // <-- does the Order's Symbol match the Symbol our EA is working on ? 
      && ( OrderType() == OP_SELL          // <-- is the Order a SELL Order ? 
      && OrderProfit()<0) )      // <-- PROFITABLE OR NOT ?
      
slippagex=MathAbs(Bid-OrderStopLoss())/Point;
Print("this is the slippage: ",slippagex);
      if ( ! OrderClose( OrderTicket(), OrderLots(), Bid,slippagex ) )               // <-- try to close the order
         Print("Order Close failed, order number: ", OrderTicket(), " Error: ", GetLastError() );  // <-- if the Order Close failed print some helpful information 
      
   } //  end of For loop 

Please if you find other errors tell me,thanks much!

 
Proximus:


==================================================================================================

NOPE, If a trade goes wrong i want to close the order with the max slippage value in pips,of the pips left between the current price,in case of sell he bid price,in case of buy the ask price; and the stop loss,

so the slippage is the distance between the current bid or ask price and the stop loss, that means that my losses wont be greater with the close order than the STOPLOSS.

The point that I was trying to make was that you were calculating using Bid/Ask + - stoploss in pips which wouldn't work

I see that you have now changed it to Bid/Ask + - order stoploss.