open order gets closed immediately, opened again, closed again and so on

 

Hi community,


so after yesterdays great assistance I tried to finalize my program and added a trailing stop/stop loss part and an order closing part to my EA. But apparently often a position gets opened and closed immediately again, opened again and closed again and so on. Now my first thought was, that the logic behind my OrderClose function was incorrect, but I cannot find it. Is there anything else you could hint me towards? Here is my complete code as of now:


extern double TrailingStop = 3;
extern double StopLoss = 3;

int init()
{

   return (0);
}

int deinit()
{
   return (0);
}

int start()
{
   double presSM = iStochastic (Symbol(), 0, 5, 3, 3, MODE_SMA, 0, MODE_MAIN, 0);
   double presSS = iStochastic (Symbol(), 0, 5, 3, 3, MODE_SMA, 0, MODE_SIGNAL, 0);
   
   int OrdersForMyPair = 0;
   
   for (int i = 0; i < OrdersTotal() ; i++)
   {   
      if (OrderSelect(i, SELECT_BY_POS) && (OrderSymbol() == Symbol() ))
      {
         OrdersForMyPair++;
      }
   }
   if (OrdersForMyPair == 0)
   {   
      if ((presSS < 30) && (presSM > presSS))
      {
         OrderSend (Symbol(), OP_BUY, 0.1, Ask, 10, 0, 0, "Entry", 0, 0, Green);
      }
      else if ((presSS > 70) && (presSM < presSS))
      {
         OrderSend (Symbol(), OP_SELL, 0.1, Bid, 10, 0, 0, "Entry", 0, 0, Green);
      }
   }
   for (int k = 0; k < OrdersTotal(); k++)
   {
      OrderSelect (k, SELECT_BY_POS, MODE_TRADES);
      
      if ((OrderType() == OP_BUY) && (presSM > 70))
      {
         OrderClose(OrderTicket(), OrderLots(), Bid, 10, Red);
      }
      else if ((OrderType() == OP_SELL) && (presSM < 30))
      {
         OrderClose (OrderTicket(), OrderLots(), Ask, 10, Red);
      }
   }
   double PointValue;

   for (int j = 0; j < OrdersTotal(); j++)
   {
      OrderSelect (j, SELECT_BY_POS, MODE_TRADES);
      
      if (MarketInfo(OrderSymbol(), MODE_POINT) == 0.00001) 
      {   
         PointValue = 0.0001;
      }
      else if (MarketInfo(OrderSymbol(), MODE_POINT) == 0.001) 
      {
         PointValue = 0.01;
      }
      else 
      {
         PointValue = MarketInfo (OrderSymbol(), MODE_POINT);
      }
      
      double TSTP = TrailingStop * PointValue;
      
      if ((OrderType() == OP_BUY) && (OrderStopLoss() < (Bid - TSTP)))
      {
         OrderModify (OrderTicket(), OrderOpenPrice(), Bid - TSTP, OrderTakeProfit(), Red);     
      }
      else if ((OrderType() == OP_SELL) && (OrderStopLoss() > (Ask + TSTP)))
      {
         OrderModify (OrderTicket(), OrderOpenPrice(), Ask + TSTP, OrderTakeProfit(), Red);
      }
      else if ((OrderType() == OP_SELL) && (OrderStopLoss() == 0))
      {
         OrderModify (OrderTicket(), OrderOpenPrice(), Ask + TSTP, OrderTakeProfit(), Red);
      }
   }
   return(0);
}
 
How are you testing your codes. Within the strategy tester or live-demo?
 
both
 

I cannot duplicate the problem you're referring to. I'm testing within the back-tester because I can do more faster.

However, I do get allot of: 

 EURUSD,M5: OrderModify error 130

If you're also getting these errors, then you should be using the Documentation to isolate the problem and fix.

Most of your order-modify / order-close-price seem to be very-near the open-price. Also, you Buy@Ask & Sell@Bid. Otherwise, you could just use OrderClosePrice and don't have to worry about whether you need Bid vs Ask.

 

Your logic should like this :

if condition to buy is true, then close any sell and open buy else if condition to sell is true then close any buy and open sell.

Can you code that ? 

 
reXmo:

Hi community,

so after yesterdays great assistance I tried to finalize my program and added a trailing stop/stop loss part and an order closing part to my EA. But apparently often a position gets opened and closed immediately again, opened again and closed again and so on. Now my first thought was, that the logic behind my OrderClose function was incorrect, but I cannot find it. Is there anything else you could hint me towards? Here is my complete code as of now:

One obvious thing,  when you are using a Loop to close or delete Orders you must count down,  read this:  Loops and Closing or Deleting Orders

Also,  consider this code snippet . . .

   if (OrdersForMyPair == 0)
   {   
      if ((presSS < 30) && (presSM > presSS))
      {
         OrderSend (Symbol(), OP_BUY, 0.1, Ask, 10, 0, 0, "Entry", 0, 0, Green);
      }
      else if ((presSS > 70) && (presSM < presSS))
      {
         OrderSend (Symbol(), OP_SELL, 0.1, Bid, 10, 0, 0, "Entry", 0, 0, Green);
      }
   }
   for (int k = 0; k < OrdersTotal(); k++)
   {
      OrderSelect (k, SELECT_BY_POS, MODE_TRADES);
      
      if ((OrderType() == OP_BUY) && (presSM > 70))
      {
         OrderClose(OrderTicket(), OrderLots(), Bid, 10, Red);
      }
      else if ((OrderType() == OP_SELL) && (presSM < 30))
      {
         OrderClose (OrderTicket(), OrderLots(), Ask, 10, Red);
      }
   }

 if presSS is 25 and presSM is 75 a trade will be opened beecause presSS is < 30 and presSS > presSS   . . .  but presSM is also > 70 so the order will immediately be closed,  so maybe you should also check if presSM is greater than 70 when you place the order.

 

You should also check all your return values and report any errors . . .  get into the habit of doing it and you will be glad you did . . . more info:   What are Function return values ? How do I use them ?

 
RaptorUK:

Also,  consider this code snippet . . .

 if presSS is 25 and presSM is 75 a trade will be opened beecause presSS is < 30 and presSS > presSS   . . .  but presSM is also > 70 so the order will immediately be closed,  so maybe you should also check if presSM is greater than 70 when you place the order.


I think what you meant was presSM > presSS ;)


 ... or check if presSM is also less than 50 then open buy 

 
phi.nuts:

I think what you meant was presSM > presSS ;)
Yes it is . . . fat finger day ;-)  as long as you understand my point,  I can live with a typo or two . . . or three  ;-)
 

@ubzen: I think you meant the Ask/Bid during the last part, where I use OrderModify to set stops and trail them. I exchanged all Ask via Bids and vise versa.

@phi.nuts: I might, but that is not the logic I want implemented. I want to close my order some time before the new signal to buy/sell appears.

@RaptorUK: I changed the loop as advised. I also improved my requirements to place an order.

My main problem was fixed due to another addidition:

   int PositionIndex;
  
   for (PositionIndex = OrdersTotal() - 1; PositionIndex >= 0 ; PositionIndex --)
   {
      OrderSelect (PositionIndex, SELECT_BY_POS, MODE_TRADES);
     
      if ((OrderType() == OP_BUY) && (presSM > 70) && (OrderSymbol() == Symbol()))  // <- checking wether the Symbol of the order is the present Symbol, the EA is working on
      {
         OrderClose(OrderTicket(), OrderLots(), Bid, 10, Red);
      }
      else if ((OrderType() == OP_SELL) && (presSM < 30) && (OrderSymbol() == Symbol())) // <- checking wether the Symbol of the order is the present Symbol, the EA is working on
      {
         OrderClose (OrderTicket(), OrderLots(), Ask, 10, Red);
      }
   }
Thanks to all. I will keep an eye on using return values, although I don't see how this would have helped here.
 
reXmo:


Thanks to all. I will keep an eye on using return values, although I don't see how this would have helped here.
It wouldn't, but when you start getting error 130s or other errors and you end up scratching our head for hours you will end up adding the tests and error reporting anyway . . .  you can't go back in time and find out what happened when the error occurred,  so when it occurs in real time print all the info you will need later to find out what happened,  for example, Bid, Ask, ticket number, entry price, etc, etc.
Reason: