Ordermodify error 130

 

Hi,

I have created an EA, that works well for me, but I tried to change it a little bit, but can not manage to make it work. All I want it to do is to open an order with a trailing stop. The problem is, the trailing stop is different for each trade (it depends on the high of the current candle). It always gives ordermodify error 130, which means that the stop loss is too close. That is not possible, cause I trade daily and the stop loss is always at least 100 pips. I guess the problem could be that it somehow always calculates the trailing stop 0, cause the trailing stop is always calculated right after the trade is placed and the ordermodify function is started after that. But I tried to put the trailing stop 150 for instance and it still gives me that error ( but this time only sometimes). If anyone could help, I would be really pleased. Thx here is the code (it is not whole, it wouldnt fit here)



  
  
//----BULLISH 
   if( (ma1<ma2 && 
       O>=C && 2*MathAbs(C-O) < (C-L) && 2*(H-O) < C-L &&
       iATR(NULL,0,10,0) < 2*(H-L) )
       ||
       ( ma1<ma2 && 
        C>O && 2*MathAbs(C-O) < (O-L) && 2*(H-C) < (O-L) &&
       iATR(NULL,0,10,0) < 2*(H-L) )
      )
      {
   
   double price = H + iATR(NULL,0,10,0)/10;
   double stoploss = L - iATR(NULL,0,10,0)/10;
   double profit = H + 3*(price - stoploss);
   double expiration = CurTime() + PERIOD_D1*60;
   price = NormalizeDouble(price,4);
   stoploss = NormalizeDouble(stoploss,4); 
   double Trailingstop = price - stoploss;                              // CALCULATION OF TRAILING STOP
   Trailingstop = NormalizeDouble(Trailingstop,4) * 10000;      
   profit = NormalizeDouble(profit,4);
   StopLossPoints = (price - stoploss)*10000;
   lots = (MarginAmount)/(StopLossPoints  * MarketInfo( Symbol(), MODE_TICKVALUE ));
   lots = NormalizeDouble(lots, 1);
   

   
       
   ticket = OrderSend(Symbol(),OP_BUYSTOP,lots,price,0,stoploss,profit,"BULLISH",123,expiration,Green);
   if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
           }
         else Print("Error opening BUY order : ",GetLastError()); 
         return(0); 
      }
   
   
 //----BEARISH  
   if( (ma1>ma2 && 
       O>=C && 2*MathAbs(C-O) < (H-O) && 2*(C-L) < H-O &&
       iATR(NULL,0,10,0) < 2*(H-L) )
       ||
       ( ma1 >ma2 && 
        C>O && 2*MathAbs(C-O) < (H-O) && 2*(C-L) < (H-O) &&
       iATR(NULL,0,10,0) < 2*(H-L) )
      )
      
      {
      

   price = L - iATR(NULL,0,10,0)/10;
   stoploss = H + iATR(NULL,0,10,0)/10;
   profit = L - 3*(stoploss - price);
   expiration = CurTime() + PERIOD_D1*60;
   price = NormalizeDouble(price,4);
   stoploss = NormalizeDouble(stoploss,4);
    Trailingstop = stoploss - price;                     // CALCULATION OF TRAILING STOP
   Trailingstop = NormalizeDouble(Trailingstop,4)*10000;
   profit = NormalizeDouble(profit,4);
    Print( Trailingstop);
   
   StopLossPoints = (stoploss - price)*10000;
   lots = (MarginAmount)/(StopLossPoints  * MarketInfo( Symbol(), MODE_TICKVALUE ));
   lots = NormalizeDouble(lots, 1);
      
   ticket = OrderSend(Symbol(),OP_SELLSTOP,lots,price,0,stoploss,profit,"BEARISH",123,expiration,Green);
   if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
           }
         else Print("Error opening SELL order : ",GetLastError()); 
         return(0); 
      }
   }
   }
   
   if (OrdersTotal() > 0 )    // IF THERE IS AN ORDER.... THERE CAN BE ONLY ONE ORDER AT THE SAME TIME
   {
   

       
         
         for(cnt=0;cnt<total;cnt++) 
      {
         OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
         
         if (OrderType() == OP_BUY)
           {
           
           if (Bid - OrderStopLoss() > Trailingstop*Point )  
           {
            OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Trailingstop*Point,OrderTakeProfit(),0,Yellow);
           
           }
           }
           if (OrderType() == OP_SELL)
           {
           if (OrderStopLoss() - Ask > Trailingstop*Point )
           {
            OrderModify(OrderTicket(),OrderOpenPrice(),Ask + Trailingstop*Point ,OrderTakeProfit(),0,Yellow);
           
           }
           }
         
   }      
   
   }
//----
   return(0);
  }
  
//+------------------------------------------------------------------+
 
I think you have to Normalize expressions "Bid-Trailingstop*Point" and "Ask + Trailingstop*Point" because Trailingstop is double.
 
Roger:
I think you have to Normalize expressions "Bid-Trailingstop*Point" and "Ask + Trailingstop*Point" because Trailingstop is double.

Didnt help :-/

the problem has to be somewhere else...but thank you anyway ;-)

 
So, put operator Print:
Print("Bid - ",Bid," TrailingStop - ",TrailingStop," OrderStopLoss - ",OrderStopLoss()," Bid-Trailingstop*Point - ",DoubleToStr(Bid-Trailingstop*Point,6));
after OrderModify(...)
 
Boocha:

Didnt help :-/

the problem has to be somewhere else...but thank you anyway ;-)

Hard to be certain, because you say you haven't posted the whole of the code. However, by the looks of it, Trailingstop isn't a static or global variable. It's declared in the "bullish" section. Therefore, its value will get set at the time an order is placed, but won't persist after that. On subsequent calls to start(), when an order is modified, the Trailingstop variable should contain zero. Therefore, the call to OrderModify() will be trying to set the stop to either Ask + 0 * Point or Bid + 0 * Point.

 

Hi

Try putting a slippage other than zero and check that 100 points is what you think and not 10 points on a 5 decimal place platform. Print out the values just after the trade to see they are correct maybe indicator is returning dodgy values sometimes.

 
jjc:

Hard to be certain, because you say you haven't posted the whole of the code. However, by the looks of it, Trailingstop isn't a static or global variable. It's declared in the "bullish" section. Therefore, its value will get set at the time an order is placed, but won't persist after that. On subsequent calls to start(), when an order is modified, the Trailingstop variable should contain zero. Therefore, the call to OrderModify() will be trying to set the stop to either Ask + 0 * Point or Bid + 0 * Point.


EXACTLY.... That is what I have thought, but I dont see any way, how to solve it. Unfortunately, I havent been programming for a long time. So do you see any way, how to maintain the value of Trailingstop for the next repetition of the program? I guess that would push a little bit forward with this EA ..... And thank you all for your responses

 
Roger:
So, put operator Print:
Print("Bid - ",Bid," TrailingStop - ",TrailingStop," OrderStopLoss - ",OrderStopLoss()," Bid-Trailingstop*Point - ",DoubleToStr(Bid-Trailingstop*Point,6));
after OrderModify(...)

2009.05.18 22:31:51 1999.05.17 08:54 Rady EURUSD,Daily: Bid - 1.0674 TrailingStop - 0 OrderStopLoss - 1.1087 Bid-Trailingstop*Point - 1.067400


Trailing stop equals 0. That confirms JJC'S post. So how do I maintain the value of the Trailingstop for another program run? I was thinking about giving that value into order ticket or magic number, but I am sure that there are better ways, how to do it.

 

And this is the rest of the code.

#property copyright "Boocha"
int cnt;
int Trailingstop;    


int start()                                     // Special function 'start'
  {
  
    int total = OrdersTotal();
    int cnt;
    

   
               
if ( OrdersTotal() ==0)            // IF THERE IS NO ORDER
{

   int timeold;
   if (timeold!=Time[0])
   {
        
  timeold=Time[0];
  double O=Open[1];
  double C=Close[1];
  double L=Low[1];
  double H=High[1];
  int ticket;
  double Risk = 0.02; 
  double StopLossPoints, lots;
  double ma1 = iMA(NULL,PERIOD_W1,20,0,MODE_SMA,PRICE_CLOSE,6);
  double ma2 = iMA(NULL,PERIOD_W1,20,0,MODE_SMA,PRICE_CLOSE,1);
  double MarginAmount = AccountEquity()*Risk;

  
  
//----BULLISH 
 
Ruptor:

Hi

Try putting a slippage other than zero and check that 100 points is what you think and not 10 points on a 5 decimal place platform. Print out the values just after the trade to see they are correct maybe indicator is returning dodgy values sometimes.


2009.05.18 22:50:02 2005.02.01 00:00 Rady EURUSD,Daily: open #2 buy stop 0.20 EURUSD at 1.3071 sl: 1.2963 tp: 1.3382 ok
2009.05.18 22:50:02 2005.02.01 00:00 Rady EURUSD,Daily: TS-108

This part should be ok. Trailing stop is really 108 points.

 
Boocha:

Trailing stop equals 0. That confirms JJC'S post. So how do I maintain the value of the Trailingstop for another program run? I was thinking about giving that value into order ticket or magic number, but I am sure that there are better ways, how to do it. 

Try declaring Trailingstop as static double rather than just double. That should survive everything other than reloads of the EA. To handle restarts, you'll need to put the value in a global variable (GlobalVariableSet), or in a text object on the chart, or store it in a file.

Reason: