Problem with OrderModify error 130

 
extern double StopLoss_v_pipech=50;
extern double Profit_target_v_pipech=200;
extern double Velikost_pozice=0.01;
extern double Posun_na_BE_v_pipech=50;

int MyDigits = 10;

int Magic_number=1009;
bool ticket,close,modify;
bool obchodovat=true;

int D,m,h;
double SL,PT,BE,H,L,SLev,NewSL,NewTP;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
   D=DayOfWeek();
   m = TimeMinute(TimeCurrent());
   h = TimeHour(TimeCurrent());

   SL = StopLoss_v_pipech*Point;
   PT = Profit_target_v_pipech*Point;
   BE = Posun_na_BE_v_pipech*Point;
   SLev=MarketInfo(Symbol(),MODE_STOPLEVEL)*Point;

   H = iHigh(Symbol(),PERIOD_D1,0);
   L = iLow(Symbol(), PERIOD_D1, 0);


   if(D==1 && h<=02 && m<10)
     {
      obchodovat=true;
     }

   for(int p=0; p<OrdersTotal(); p++)
     {
      if(OrderSelect(p,SELECT_BY_POS,MODE_TRADES)==true && OrderMagicNumber()==Magic_number)
        {
         if(D>=5 && h>=21 && m>=30)
           {
            zavritPoziceNaKonciTydne();
           }

         if(OrderType()==OP_BUY)
           {
            if(Bid>=OrderOpenPrice()+BE && OrderStopLoss()<OrderOpenPrice())
              {
               modify=OrderModify(OrderTicket(),
                                  OrderOpenPrice(),
                                  OrderOpenPrice(),
                                  OrderTakeProfit(),
                                  0,
                                  clrAqua);
              }




            if(NormalizeDouble(H-OrderOpenPrice(),MyDigits)>
               NormalizeDouble(BE,MyDigits) && 
               
               NormalizeDouble(H-SL,MyDigits)>
               NormalizeDouble(OrderStopLoss(),MyDigits))
              {

               NewSL = NormalizeDouble(H-SL, MyDigits);
               NewTP = OrderTakeProfit();

               Print(NewTP+" - "+NewSL+" > "+SLev+" = "+(NewTP-NewSL));

               if(NormalizeDouble(NewTP-NewSL, MyDigits)>NormalizeDouble(SLev, MyDigits))
                 {
                  modify=OrderModify(OrderTicket(),
                                     OrderOpenPrice(),
                                     NewSL,
                                     NewTP,
                                     0,
                                     clrYellow);
                 }
              }
           }

and result

2014.04.20 22:29:59.759 2014.01.06 21:50 px EURJPY,Daily: OrderModify error 130

2014.04.20 22:29:59.759 2014.01.06 21:50 px EURJPY,Daily: 143.83 - 142.27 > 0.06 = 1.56

MODE_STOPLEVEL is 0.06

and TakeProfit - StopLoss is 1.56

why I have styll error 130????

 

What does TakeProfit - Stoploss have to do with anything?

What was the Bid price when you tried to modify the order?

You're probably trying to set the SL at a price higher than the current price.

 
143.83 - 142.27 > 0.06 = 1.56

That logic is wrong when setting up the value for SL, I think.


OR the point is close to the Bid/Ask

 
deysmacro:

That logic is wrong when setting up the value for SL, I think.


OR the point is close to the Bid/Ask


The logic is most definitely wrong, there is no doubt about that

Let's use an example, if the japan pair has only 2 digits

Open a Buy order at 144.00

SL is 143.50

H =145.00 (Day high)

if(NormalizeDouble(H-OrderOpenPrice(),MyDigits)> //if(145.00-144.00>0.50) Yes, that's true
               NormalizeDouble(BE,MyDigits) &&    
               
               NormalizeDouble(H-SL,MyDigits)>    //if(145.00-0.50>143.50) Yes, true as well
               NormalizeDouble(OrderStopLoss(),MyDigits))
              {                                   //Why normalise to 10 Digits??????

               NewSL = NormalizeDouble(H-SL, MyDigits);//145.00-0.50= 144.50  BUT normalised to 10 Digits
               NewTP = OrderTakeProfit();

               Print(NewTP+" - "+NewSL+" > "+SLev+" = "+(NewTP-NewSL));

               if(NormalizeDouble(NewTP-NewSL, MyDigits)>NormalizeDouble(SLev, MyDigits))
                 {
                  modify=OrderModify(OrderTicket(),
                                     OrderOpenPrice(),  //Open price is 144.00
                                     NewSL,             //NewSL is 144.50
                                     NewTP,             //No where has OrderClosePrice or Bid been checked,
                                     0,                 //So while Bid is below 144.50 the modify will fail
                                     clrYellow);
                 }
              }
 
Yup, it is just logic and normalization. Common mistake in doing logic. Easy fix.
Reason: