ERROR 130 Invalid Stops | OrderSend() returns -1 output even when TP and SL are 100 pips are away.

 

Hi All,

I have wracked my brains on this and i know there has been a lot written on NormalizeDouble() to send valid stops and take profits.  But I just cant get this to work.  Here is the code.  Its sending a limit order.  I have hard coded entry level prices to make sure i dont place an order too close the bid/ask.  OrderSend will always send a -1 result.


void sendLimitOrder(string symbolInstrument, int opType, bool isBuyOrder, string comment, double entryPrice, double takeProfit, double stopLoss) {
   if(positionCheck()==true) {
      int ticketNumber = 0;
      int lastError = 0;

      if(isBuyOrder) {
         entryPrice = ask - 0.0050;
         takeProfit = ask + 0.0100;
         stopLoss = ask - 0.0100;
      } else {
         entryPrice = bid + 0.0050;
         takeProfit = bid - 0.0100;
         stopLoss = bid + 0.0100;
      }

      Print(symbolInstrument, isBuyOrder, entryPrice, takeProfit, stopLoss +","+bid + ","+ask);
      if(isBuyOrder) {
         ticketNumber = OrderSend(getSuffixCurrencyPair(symbolInstrument),OP_BUYLIMIT,getVolume(),entryPrice,MAGICNO,stopLoss,takeProfit,comment,0,0,clrGreen);
         if(ticketNumber != -1) {
            int arraySize = ArraySize(MagicNoArr);
            MagicNoArr[arraySize+1] = MAGICNO;
            MAGICNO++;
         }
         Print("ERROR CODE :: ", GetLastError());
      } else {
         ticketNumber = OrderSend(getSuffixCurrencyPair(symbolInstrument),OP_SELLLIMIT,getVolume(),entryPrice,MAGICNO,stopLoss,takeProfit,comment,0,0,clrBlue);
         if(ticketNumber != -1) {
            int arraySize = ArraySize(MagicNoArr);
            MagicNoArr[arraySize+1] = MAGICNO;
            MAGICNO++;
         }
         Print("ERROR CODE :: ", GetLastError());
      }
   }
}

The watch window for the above shows the following outputs

entryPrice 1.1982300000000001

stopLoss 1.19323

takeProfit 1.21323

ask 1.20323

isBuyOrder true


The error code produced is:

2021.03.02 14:04:01.713 RegressionTrader EURUSD,M1: ERROR CODE :: 130


I would really appreciate some help on this?

Thanks,
JJ


 
JJSingh:

Hi All,

I have wracked my brains on this and i know there has been a lot written on NormalizeDouble() to send valid stops and take profits.  But I just cant get this to work.  Here is the code.  Its sending a limit order.  I have hard coded entry level prices to make sure i dont place an order too close the bid/ask.  OrderSend will always send a -1 result.


The watch window for the above shows the following outputs

entryPrice 1.1982300000000001

stopLoss 1.19323

takeProfit 1.21323

ask 1.20323

isBuyOrder true


The error code produced is:

2021.03.02 14:04:01.713 RegressionTrader EURUSD,M1: ERROR CODE :: 130


I would really appreciate some help on this?

Thanks,
JJ


Using NormalizeDouble.   entryPrice 1.1982300000000001 is not valid . 

 entryPrice =  NormalizeDouble((ask - 0.0050),Digits);
 if(isBuyOrder) {
         entryPrice =  NormalizeDouble((ask - 0.0050),Digits);
         takeProfit =  NormalizeDouble((ask + 0.0100),Digits);
         stopLoss =  NormalizeDouble((ask - 0.0100),Digits);
      } else {
         entryPrice =  NormalizeDouble((bid + 0.0050),Digits);
         takeProfit =  NormalizeDouble((bid - 0.0100),Digits);
         stopLoss =  NormalizeDouble((bid + 0.0100),Digits);
      }
 
Mehmet Bastem:

Using NormalizeDouble.   entryPrice 1.1982300000000001 is not valid . 

Thanks for the note.

I tried what you suggested:

void sendLimitOrder(string symbolInstrument, int opType, bool isBuyOrder, string comment, double entryPrice, double takeProfit, double stopLoss) {
   if(positionCheck()==true) {
      int ticketNumber = 0;
      int lastError = 0;
      int slippage = 1;
     if(isBuyOrder) {
         entryPrice =  NormalizeDouble((ask - 0.0050),Digits);
         takeProfit =  NormalizeDouble((ask + 0.0100),Digits);
         stopLoss =  NormalizeDouble((ask - 0.0100),Digits);
      } else {
         entryPrice =  NormalizeDouble((bid + 0.0050),Digits);
         takeProfit =  NormalizeDouble((bid - 0.0100),Digits);
         stopLoss =  NormalizeDouble((bid + 0.0100),Digits);
      }

      Print(symbolInstrument, isBuyOrder, entryPrice, takeProfit, stopLoss +","+bid + ","+ask);
      if(isBuyOrder) {
         ticketNumber = OrderSend(getSuffixCurrencyPair(symbolInstrument),OP_BUYLIMIT,getVolume(),entryPrice,slippage,stopLoss,takeProfit,comment,MAGICNO,0,clrGreen);
         if(ticketNumber != -1) {
            int arraySize = ArraySize(MagicNoArr);
            MagicNoArr[arraySize+1] = MAGICNO;
            MAGICNO++;
         }
         Print("ERROR CODE :: ", GetLastError());
      } else {
         ticketNumber = OrderSend(getSuffixCurrencyPair(symbolInstrument),OP_SELLLIMIT,getVolume(),entryPrice,slippage,stopLoss,takeProfit,comment,MAGICNO,0,clrGreen);
         if(ticketNumber != -1) {
            int arraySize = ArraySize(MagicNoArr);
            MagicNoArr[arraySize+1] = MAGICNO;
            MAGICNO++;
         }
         Print("ERROR CODE :: ", GetLastError());
      }
   }
}

The output in the watch window was

entryPrice 1.20036999999999

stopLoss 1.19537

takeProfit 1.21537000000001

ask 1.205370000000000001

isBuyOrder true

ticketNumber -1

2021.03.03 13:41:36.728 RegressionTrader EURUSD,M1: ERROR CODE :: 130

2021.03.03 13:41:17.063 RegressionTrader EURUSD,M1: EURUSD true1.200371.215371.19537,1.2053,1.20537


As you can see the problem has not gone away. :(

Any other suggestions?

 
JJSingh:

Thanks for the note.

I tried what you suggested:

The output in the watch window was

entryPrice 1.20036999999999

stopLoss 1.19537

takeProfit 1.21537000000001

ask 1.205370000000000001

isBuyOrder true

ticketNumber -1

2021.03.03 13:41:36.728 RegressionTrader EURUSD,M1: ERROR CODE :: 130

2021.03.03 13:41:17.063 RegressionTrader EURUSD,M1: EURUSD true1.200371.215371.19537,1.2053,1.20537


As you can see the problem has not gone away. :(

Any other suggestions?


can you run this code and upload the screenshot @ JJSingh


void sendLimitOrder(string symbolInstrument, int opType, bool isBuyOrder, string comment, double entryPrice, double takeProfit, double stopLoss) {
   if(positionCheck()==true) {
      int ticketNumber = 0;
      int lastError = 0;
      int slippage = 1;
     if(isBuyOrder) {
         entryPrice =  NormalizeDouble((ask - 0.0050),Digits);
         takeProfit =  NormalizeDouble((ask + 0.0100),Digits);
         stopLoss =  NormalizeDouble((ask - 0.0100),Digits);
      } else {
         entryPrice =  NormalizeDouble((bid + 0.0050),Digits);
         takeProfit =  NormalizeDouble((bid - 0.0100),Digits);
         stopLoss =  NormalizeDouble((bid + 0.0100),Digits);
      }

     // Print(symbolInstrument, isBuyOrder, entryPrice, takeProfit, stopLoss +","+bid + ","+ask);
      if(isBuyOrder) {
         ticketNumber = OrderSend(getSuffixCurrencyPair(symbolInstrument),OP_BUYLIMIT,getVolume(),entryPrice,slippage,stopLoss,takeProfit,comment,MAGICNO,0,clrGreen);
         if(ticketNumber != -1) {
            int arraySize = ArraySize(MagicNoArr);
            MagicNoArr[arraySize+1] = MAGICNO;
            MAGICNO++;
         }
          Print("BUY Limit Error: ",GetLastError()," Symbol: ",getSuffixCurrencyPair(symbolInstrument)," Volume ",getVolume()," Entry Price: ",entryPrice," SL: ",stopLoss," TP ",takeProfit);
        
        // Print("ERROR CODE :: ", GetLastError());
      } else {
         ticketNumber = OrderSend(getSuffixCurrencyPair(symbolInstrument),OP_SELLLIMIT,getVolume(),entryPrice,slippage,stopLoss,takeProfit,comment,MAGICNO,0,clrGreen);
         if(ticketNumber != -1) {
            int arraySize = ArraySize(MagicNoArr);
            MagicNoArr[arraySize+1] = MAGICNO;
            MAGICNO++;
         }

         Print("SELL  Limit Error: ",GetLastError()," Symbol: ",getSuffixCurrencyPair(symbolInstrument)," Volume ",getVolume()," Entry Price: ",entryPrice," SL: ",stopLoss," TP ",takeProfit);
        
         // Print("ERROR CODE :: ", GetLastError());
      }
   }
}
 
Mehmet Bastem:


can you run this code and upload the screenshot @ JJSingh


Hi Mehmet,
Please see the attached output from a screenshot.


2021.03.03 15:34:18.999 RegressionTrader EURUSD,M1: EURUSD,Entry:1.20596,TP:1.20617,SL:1.20581,sOpen:1.20609404915364,sClose:1.206183460129576,fWidth:2.073048919133402

2021.03.03 15:34:18.999 RegressionTrader EURUSD,M1: BUY Limit Error: 130 Symbol: EURUSDx Volume 100.0 Entry Price: 1.20091 SL: 1.19591 TP 1.21591

2021.03.03 15:34:13.745 RegressionTrader EURUSD,M1: 2021.03.03 17:34:13,EURUSD,canTrade,true

2021.03.03 15:34:01.306 RegressionTrader EURUSD,M1: EURUSD,Entry:1.20617,TP:1.20596,SL:1.20485,sOpen:1.20609404915364,sClose:1.206183460129576,fWidth:2.073048919133402

2021.03.03 15:33:55.682 RegressionTrader EURUSD,M1: initialized

2021.03.03 15:33:55.678 RegressionTrader EURUSD,M1: Initialising 2021.03.03 15:33:55

2021.03.03 15:33:55.656 RegressionTrader EURUSD,M1 inputs: isProduction=false; isDebug=true; isOptimise=false; emaSlowPeriod=35; emaMediumPeriod=15; emaFastPeriod=2; emaSlowPeriod_Close=21; emaMediumPeriod_Close=7; emaFastPeriod_Close=4; symbol=EURUSD; maxDrawdown=15.0; sourceSeriesSlow=; bBandsSlowPeriods=21; bBandSlowStdDev=2.5; bBandShiftSlow=1; Quantity=100.0; maxOpenPositions=5; timeFrame=1; suffix=x; 

2021.03.03 15:33:49.955 Expert AzurianCapital\RegressionTrader EURUSD,M1: loaded successfully


 
JJSingh:

Hi Mehmet,
Please see the attached output from a screenshot.


2021.03.03 15:34:18.999 RegressionTrader EURUSD,M1: EURUSD,Entry:1.20596,TP:1.20617,SL:1.20581,sOpen:1.20609404915364,sClose:1.206183460129576,fWidth:2.073048919133402

2021.03.03 15:34:18.999 RegressionTrader EURUSD,M1: BUY Limit Error: 130 Symbol: EURUSDx Volume 100.0 Entry Price: 1.20091 SL: 1.19591 TP 1.21591

2021.03.03 15:34:13.745 RegressionTrader EURUSD,M1: 2021.03.03 17:34:13,EURUSD,canTrade,true

2021.03.03 15:34:01.306 RegressionTrader EURUSD,M1: EURUSD,Entry:1.20617,TP:1.20596,SL:1.20485,sOpen:1.20609404915364,sClose:1.206183460129576,fWidth:2.073048919133402

2021.03.03 15:33:55.682 RegressionTrader EURUSD,M1: initialized

2021.03.03 15:33:55.678 RegressionTrader EURUSD,M1: Initialising 2021.03.03 15:33:55

2021.03.03 15:33:55.656 RegressionTrader EURUSD,M1 inputs: isProduction=false; isDebug=true; isOptimise=false; emaSlowPeriod=35; emaMediumPeriod=15; emaFastPeriod=2; emaSlowPeriod_Close=21; emaMediumPeriod_Close=7; emaFastPeriod_Close=4; symbol=EURUSD; maxDrawdown=15.0; sourceSeriesSlow=; bBandsSlowPeriods=21; bBandSlowStdDev=2.5; bBandShiftSlow=1; Quantity=100.0; maxOpenPositions=5; timeFrame=1; suffix=x; 

2021.03.03 15:33:49.955 Expert AzurianCapital\RegressionTrader EURUSD,M1: loaded successfully



Can you please try this code without changing it? @ JJSingh


        void sendLimitOrder(string symbolInstrument, int opType, bool isBuyOrder, string comment, double entryPrice, double takeProfit, double stopLoss) {
   if(positionCheck()==true) {
      int ticketNumber = 0;
      int lastError = 0;
      int slippage = 1;
     if(isBuyOrder) {
         entryPrice =  NormalizeDouble((ask - 0.0050),Digits);
         takeProfit =  NormalizeDouble((ask + 0.0100),Digits);
         stopLoss =  NormalizeDouble((ask - 0.0100),Digits);
      } else {
         entryPrice =  NormalizeDouble((bid + 0.0050),Digits);
         takeProfit =  NormalizeDouble((bid - 0.0100),Digits);
         stopLoss =  NormalizeDouble((bid + 0.0100),Digits);
      }
      int Dgt=Digits;
      bool ticket;
      if(isBuyOrder) {
       //  ticketNumber = OrderSend(getSuffixCurrencyPair(symbolInstrument),OP_BUYLIMIT,getVolume(),entryPrice,slippage,stopLoss,takeProfit,comment,MAGICNO,0,clrGreen);
       ticketNumber = OrderSend(getSuffixCurrencyPair(symbolInstrument),OP_BUYLIMIT,getVolume(),entryPrice,slippage,0,0,comment,MAGICNO,0,clrGreen);
       
       if(OrderSelect(ticketNumber, SELECT_BY_TICKET, MODE_TRADES))
       ticket= OrderModify(OrderTicket(), OrderOpenPrice(),OrderStopLoss(),
        NormalizeDouble(( OrderOpenPrice()-100*Point),int(Dgt)), 
        NormalizeDouble(( OrderOpenPrice()+100*Point),int(Dgt)), 
            
         CLR_NONE);
         if(ticketNumber != -1) {
            int arraySize = ArraySize(MagicNoArr);
            MagicNoArr[arraySize+1] = MAGICNO;
            MAGICNO++;
         }
          Print("BUY Limit Error: ",GetLastError()," Symbol: ",getSuffixCurrencyPair(symbolInstrument)," Volume ",getVolume()," Entry Price: ",entryPrice," SL: ",stopLoss," TP ",takeProfit);
        
        
      } else {
         //ticketNumber = OrderSend(getSuffixCurrencyPair(symbolInstrument),OP_SELLLIMIT,getVolume(),entryPrice,slippage,stopLoss,takeProfit,comment,MAGICNO,0,clrGreen);
         ticketNumber = OrderSend(getSuffixCurrencyPair(symbolInstrument),OP_SELLLIMIT,getVolume(),entryPrice,slippage,0,0,comment,MAGICNO,0,clrGreen);
         if(OrderSelect(ticketNumber, SELECT_BY_TICKET, MODE_TRADES))
       ticket= OrderModify(OrderTicket(), OrderOpenPrice(),OrderStopLoss(),
        NormalizeDouble(( OrderOpenPrice()+100*Point),int(Dgt)), 
        NormalizeDouble(( OrderOpenPrice()-100*Point),int(Dgt)), 
            
         CLR_NONE);
         
         if(ticketNumber != -1) {
            int arraySize = ArraySize(MagicNoArr);
            MagicNoArr[arraySize+1] = MAGICNO;
            MAGICNO++;
         }
         Print("SELL  Limit Error: ",GetLastError()," Symbol: ",getSuffixCurrencyPair(symbolInstrument)," Volume ",getVolume()," Entry Price: ",entryPrice," SL: ",stopLoss," TP ",takeProfit);
        
         
      }
   }
}
JJSingh
JJSingh
  • 2021.03.02
  • www.mql5.com
Trader's profile
 
Mehmet Bastem:


Can you please try this code without changing it? @ JJSingh


Hi Mehmet,

I haven't changed your code.  It doesn't compile.  

Dgt - undeclared identifier

Thanks,
JJ

 
JJSingh:

Hi Mehmet,

I haven't changed your code.  It doesn't compile.  

Dgt - undeclared identifier

Thanks,
JJ

int Dgt=Digits;
 
Mehmet Bastem:

Hey Mehmet - Thanks for you help.  I have solved the problem.  The EA was attaching to a symbol on a chart which did not have the correct suffix which the broker was allowing to trade on.  Hence why all the errors.

Thanks

JJ

 
JJSingh:

Hey Mehmet - Thanks for you help.  I have solved the problem.  The EA was attaching to a symbol on a chart which did not have the correct suffix which the broker was allowing to trade on.  Hence why all the errors.

Thanks

JJ

It has nothing to do with what you wrote. If it were as you typed it would have informed you of the 4112 error code. xcxc

Reason: