Problems with orders, take profit and stop loss: Error 130

 

I have a simple price action EA. I think the "business logic" works fine, from what I've seen in the tester, but I think there is a problem with the creation and modification of orders. This is for example the Buy function:

 

void Buy(string comm)

  {

      double op=0,sl=0,tp=0; 

      op = NormalizeDouble(Ask,Digits);  

      //CalcLot();            

      if (AccountFreeMarginCheck(Symbol(),OP_BUY,Lots)<=0 || GetLastError()==134) 

      {

        Print("Νε δξρςΰςξχνξ ρβξαξδνϋυ ρπεδρςβ δλ ξςκπϋςθ ξπδεπΰ BUY ξαϊεμξμ: " + DoubleToStr(Lots,2));

        return;

      }        

      Print("Βϋρςΰβλεμ ξπδεπ BUY (v=" + DoubleToStr(Lots, 2) +"), ρ οΰπΰμεςπΰμθ: OP=" + DoubleToStr(op,Digits) +"; SL=" + DoubleToStr(sl,Digits) + "; TP=" + DoubleToStr(tp,Digits));

      int Ticket = OrderSend(Symbol(), OP_BUY, Lots, op, Slippage, 0, 0, comm, Magic, 0 ,Blue); 

      if (PrintError(357)==0)

      {        

        if (OrderSelect( Ticket, SELECT_BY_TICKET)==false) return;

        op = NormalizeDouble(OrderOpenPrice(),Digits);          

        tp = NormalizeDouble(op + (TakeProfit)*Point,Digits);      

        if (TakeProfit==0) tp=0;

        sl = NormalizeDouble(op - (StopLoss)*Point,Digits);   

        if (StopLoss==0) sl=0;

        if (sl!=0 || tp!=0) 

        {

          Print("Μξδθτθφθπσεμ βϋρςΰβλεννϋι ξπδεπ BUY #" + IntegerToString(Ticket));

          if (OrderModify( Ticket, op, sl, tp, 0, Yellow)==false)    

            PrintError(369);                                               

        }  

      }        

  } 

 

This is with the following default tp and sl:

 

extern int       TakeProfit=30;       

extern int       StopLoss=15;  

 

A little information, I am using this EA with Pepperstone, who say they are an ECN broker, with 5 digits. I think the way this code is written it caters for the ECN part, meaning I think that everything up to the Ordersend call which sets stop loss and take profit to 0, works.  So the EA is able to initiate orders. My problem is that, from what it seems, the OrderModify doesn't work. This means that any order that is made, there is no stop loss or take profit (I think OrderModify is responsible for this). Unfortunately I don't remember whether I saw the error 369 on the Experts tab, so something else might be the problem too. I am setting stop loss to 8 pips and take profit to 2.5, but it doesn't work. By the way, Pepperstone has stop_level 0 so I think that I can set this stop loss and tp with them. Anyone have any idea what might be wrong?

 

 

Update: It seems I am getting the Error 130: Invalid stops. Can someone please help with this? 

 
Iason Solomos:

I have a simple price action EA. I think the "business logic" works fine, from what I've seen in the tester, but I think there is a problem with the creation and modification of orders. This is for example the Buy function:

 

void Buy(string comm)

  {

      double op=0,sl=0,tp=0; 

      op = NormalizeDouble(Ask,Digits);  

      //CalcLot();            

      if (AccountFreeMarginCheck(Symbol(),OP_BUY,Lots)<=0 || GetLastError()==134) 

      {

        Print("Νε δξρςΰςξχνξ ρβξαξδνϋυ ρπεδρςβ δλ ξςκπϋςθ ξπδεπΰ BUY ξαϊεμξμ: " + DoubleToStr(Lots,2));

        return;

      }        

      Print("Βϋρςΰβλεμ ξπδεπ BUY (v=" + DoubleToStr(Lots, 2) +"), ρ οΰπΰμεςπΰμθ: OP=" + DoubleToStr(op,Digits) +"; SL=" + DoubleToStr(sl,Digits) + "; TP=" + DoubleToStr(tp,Digits));

      int Ticket = OrderSend(Symbol(), OP_BUY, Lots, op, Slippage, 0, 0, comm, Magic, 0 ,Blue); 

      if (PrintError(357)==0)

      {        

        if (OrderSelect( Ticket, SELECT_BY_TICKET)==false) return;

        op = NormalizeDouble(OrderOpenPrice(),Digits);          

        tp = NormalizeDouble(op + (TakeProfit)*Point,Digits);      

        if (TakeProfit==0) tp=0;

        sl = NormalizeDouble(op - (StopLoss)*Point,Digits);   

        if (StopLoss==0) sl=0;

        if (sl!=0 || tp!=0) 

        {

          Print("Μξδθτθφθπσεμ βϋρςΰβλεννϋι ξπδεπ BUY #" + IntegerToString(Ticket));

          if (OrderModify( Ticket, op, sl, tp, 0, Yellow)==false)    

            PrintError(369);                                               

        }  

      }        

  } 

 

This is with the following default tp and sl:

 

extern int       TakeProfit=30;       

extern int       StopLoss=15;  

 

A little information, I am using this EA with Pepperstone, who say they are an ECN broker, with 5 digits. I think the way this code is written it caters for the ECN part, meaning I think that everything up to the Ordersend call which sets stop loss and take profit to 0, works.  So the EA is able to initiate orders. My problem is that, from what it seems, the OrderModify doesn't work. This means that any order that is made, there is no stop loss or take profit (I think OrderModify is responsible for this). Unfortunately I don't remember whether I saw the error 369 on the Experts tab, so something else might be the problem too. I am setting stop loss to 8 pips and take profit to 2.5, but it doesn't work. By the way, Pepperstone has stop_level 0 so I think that I can set this stop loss and tp with them. Anyone have any idea what might be wrong?

 

 

Update: It seems I am getting the Error 130: Invalid stops. Can someone please help with this? 


The problem lay on the following line of code:

int Ticket = OrderSend(Symbol(), OP_BUY, Lots, op, Slippage, 0, 0, comm, Magic, 0 ,Blue);

it should be:

int Ticket = OrderSend(Symbol(), OP_BUY, Lots, op, Slippage, NULL, NULL, comm, Magic, 0 ,Blue);

problem solved!

 
Chris Lazarius:

The problem lay on the following line of code:

int Ticket = OrderSend(Symbol(), OP_BUY, Lots, op, Slippage, 0, 0, comm, Magic, 0 ,Blue);

it should be:

int Ticket = OrderSend(Symbol(), OP_BUY, Lots, op, Slippage, NULL, NULL, comm, Magic, 0 ,Blue);

problem solved!

No, you should not be using "NULL", instead of 0 for a "double". The OP's usage of "0" is correct.

 
Fernando Carreiro:

No, you should not be using "NULL", instead of 0 for a "double". The OP's usage of "0" is correct.


What could be the proper formula to insert instead of using NULL value, could you please type it for us, how to calculate ST or TP?

Let say TP = 60 and SL = 75
 
Iason Solomos: I have a simple price action EA. I think the "business logic" works fine, from what I've seen in the tester, but I think there is a problem with the creation and modification of orders. This is for example the Buy function:

 

This is with the following default tp and sl:A little information, I am using this EA with Pepperstone, who say they are an ECN broker, with 5 digits. I think the way this code is written it caters for the ECN part, meaning I think that everything up to the Ordersend call which sets stop loss and take profit to 0, works.  So the EA is able to initiate orders. My problem is that, from what it seems, the OrderModify doesn't work. This means that any order that is made, there is no stop loss or take profit (I think OrderModify is responsible for this). Unfortunately I don't remember whether I saw the error 369 on the Experts tab, so something else might be the problem too. I am setting stop loss to 8 pips and take profit to 2.5, but it doesn't work. By the way, Pepperstone has stop_level 0 so I think that I can set this stop loss and tp with them. Anyone have any idea what might be wrong?

Update: It seems I am getting the Error 130: Invalid stops. Can someone please help with this? 

When you get an error you should be specific about where the error is occurring. In this case, it is most probably with the OrderModify() line, since that is where you are setting the stops.

However, your code does not show any signs of you checking for the Brokers Stops Level conditions. Also, please do not use NormalizeDouble, but instead use proper price alignment to the Tick Size.

Here is quote from another user which clarifies things:

Forum on trading, automated trading systems and testing trading strategies

MT4 Stop Loss and Take Profit formula

whroeder1, 2017.09.11 02:40

Risk depends on your initial stop loss, lot size, and the value of the pair.
  • You place the stop where it needs to be - where the reason for the trade is no longer valid. E.g. trading a support bounce the stop goes below the support.
  • Account Balance * percent/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerLot + CommissionPerLot) (Note OOP-OSL includes the SPREAD, and DeltaPerLot is usually around $10/pip but it takes account of the exchange rates of the pair vs. your account currency.)
  • Do NOT use TickValue by itself - DeltaPerLot
  • You must normalize lots properly and check against min and max.
  • You must also check FreeMargin to avoid stop out
Most pairs are worth about $10 per PIP. A $5 risk with a (very small) 5 PIP SL is $5/$10/5=0.1 Lots maximum.

Forum on trading, automated trading systems and testing trading strategies

Start time lag when using TimeCurrent or market watch feed

whroeder1, 2017.07.19 16:48


  1. If there is no new tick, nothing has changed. Why didn't you do it last tick? If nothing has changed, why are you going to do something now?

  2. There can be minutes between ticks during the Asian session. "Free-of-Holes" Charts - MQL4 Articles
    No candle if open = close ? - MQL4 and MetaTrader 4 - MQL4 programming forum

  3. Start using the new Event Handling Functions Event Handling Functions - Functions - Language Basics - MQL4 Reference

  4. newSl = NormalizeDouble(price - TrailingStopPoints * point, Digits);
    Do NOT use NormalizeDouble, EVER. For ANY Reason. It's a kludge, don't use it. It's use is always wrong

  5. Code breaks on 4 digit brokers, exotics (e.g. USDZAR where spread is over 500 points,) and metals. Compute what a pip is and use it, not points. How to manage JPY pairs with parameters? - MQL4 and MetaTrader 4 - MQL4 programming forum

Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial
Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial
  • book.mql4.com
Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial
 
Chris Lazarius: What could be the proper formula to insert instead of using NULL value

Already answered! The use of "0" or "0.0" is the correct way to define an order without a S/L or T/P! Also:

Void Type and NULL Constant

Syntactically the void type is a fundamental type along with types of char, uchar, bool, short, ushort, int, uint, color, long, ulong, datetime, float, double and string. This type is used either to indicate that the function does not return any value, or as a function parameter it denotes the absence of parameters.

The predefined constant variable NULL is of the void type. It can be assigned to variables of any other fundamental types without conversion. The comparison of fundamental type variables with the NULL value is allowed.

Chris Lazarius: ... could you please type it for us, how to calculate ST or TP?Let say TP = 60 and SL = 75

See, previous post about stops!
 
Fernando Carreiro:

When you get an error you should be specific about where the error is occurring. In this case, it is most probably with the OrderModify() line, since that is where you are setting the stops.

However, your code does not show any signs of you checking for the Brokers Stops Level conditions. Also, please do not use NormalizeDouble, but instead use proper price alignment to the Tick Size.

Here is quote form another user which clarifies things:




Thanks much, it make sense