OrderSend error 130 (tried everything!)

 
HI

i'm trying to programm an EA that places orders at a certain time of the day and close it at another.

The EA works but i have error messages (OrderSend error 130 which means wrong stopLoss) but i cannot find where the error is. I've tried several stopLoss points to see if it came from that, but i couldn't make the error messages go away.

the weird thing is, apart from these error messages, the code works ok. But i'd rather like the code without errors.

I'm backtesting the code with Alapri UK.

thanks for any help

Jeff

enum buysell
{
    buy,
    sell
};
enum pointsprice
{
   points,
   price
};
extern string g = "+++ Enter your CHART GMT offset +++";
extern string h = "+++ Will calculate GMT hour as ChartHour+GMTOffset +++";
extern int GMTOffset = -1;
extern string q = "+++ Set points or price +++";
extern pointsprice PriceOrPoints = points;
extern double PriceOrPointsValue = 0;
extern buysell BuyOrSell = buy;
extern string d = "+++ Set stop loss/take profit in points +++";
extern int StopLossPoints = 0;
extern int TakeProfitPoints = 0;
extern string j = "+++ Number of lots to open +++";
extern double Lots = 0.1;
extern string z = "+++ Use automatic risk percentage lot adjustment +++";
extern bool AutoLot = false;
extern double EquityRiskPercentage = 0.01;
extern string w = "+++ Time Order/stop +++";
extern string OpenTime = "00:00:00";
extern string CloseTime = "23:30:00";

datetime prevTime;
bool dayPassed = true;
int OpenSell = -1;
int OpenBuy = -1;
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
{
   
   datetime time = iTime(NULL,PERIOD_H1,0);
   
   
   int heuresOpen = StringSubstr(OpenTime, 0, 2)+GMTOffset;
   int minutesOpen = StringSubstr(OpenTime, 3, 2);
   int secondesOpen = StringSubstr(OpenTime, 6, 2);
   
   if (heuresOpen < 0) heuresOpen = heuresOpen + 24;
   if (heuresOpen >= 24) heuresOpen = heuresOpen - 24;


   if (TimeDay(prevTime)< TimeDay(time))
   {
      dayPassed = true;
      
   }

         
   if (dayPassed == true && OpenSell == -1 && OpenBuy == -1 && TimeHour(time) >= heuresOpen && TimeMinute(time) >= minutesOpen && TimeSeconds(time) >= secondesOpen)
   {
   

      if (PriceOrPoints == points)
      {
         if (BuyOrSell == buy)
            PriceOrPointsValue = Bid+PriceOrPointsValue*Point;
         else
            PriceOrPointsValue = Ask+PriceOrPointsValue*Point;
      }
      
      //calculate lots
      double spread = Ask-Bid;
      if (AutoLot && EquityRiskPercentage < 1 && EquityRiskPercentage > 0)
      {
         Lots = (AccountEquity()*EquityRiskPercentage)/StopLossPoints;
      }
      
      if (BuyOrSell == buy)
      {
         double p1 = PriceOrPointsValue+spread;
         p1 = int(p1 * 10000)/10000.;


         OpenBuy = OrderSend(Symbol(), OP_BUYSTOP, Lots, p1, 3, PriceOrPointsValue-StopLossPoints*Point*2+spread
                                       ,PriceOrPointsValue+TakeProfitPoints*Point+spread,"woohoo",8675309,
                                       time+3600*1,Green);
                   
                   
      }
      else
      { 
         double p2 = PriceOrPointsValue;
         p2 = int(p2 * 10000)/10000.;
         OpenSell = OrderSend(Symbol(), OP_SELLSTOP, Lots, p2, 3, PriceOrPointsValue+StopLossPoints*Point-spread,
                                       PriceOrPointsValue-TakeProfitPoints*Point-spread,"woohooooo",8675309,
                                       time+3600*1,Green);  
    
      }                 
   }
   

   
  
   int heuresClose = StringSubstr(CloseTime, 0, 2);
   int minutesClose = StringSubstr(CloseTime, 3, 2);
   int secondesClose = StringSubstr(CloseTime, 6, 2);
   
   int maxDuration = (heuresClose-heuresOpen)*3600 + (minutesClose - minutesOpen) * 60 + (secondesClose - secondesOpen);
   
   for(int pos = OrdersTotal()-1; pos >= 0 ; pos--) if (
       OrderSelect(pos, SELECT_BY_POS)            // Only my orders w/
   &&  OrderMagicNumber() == 8675309         // my magic number
   &&  OrderSymbol()      == Symbol() ){               // and period and symbol
       int duration = TimeCurrent() - OrderOpenTime();
       if (duration >= maxDuration)
       {
            OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 3*1);
            OpenSell = -1;
            OpenBuy = -1;   
            
            dayPassed = false;                  
       }
   }
   
   prevTime = time;
   
   return(0);
}

and here is a typical log:

2014.10.27 15:07:33.283 EURUSD,H1: 306921 tick events (1145 bars, 307921 bar states) processed within 390 ms (total time 4805 ms)
2014.10.27 15:07:33.282 2014.10.24 23:30  maxima88x_1 EURUSD,H1: close #6 buy 0.01 EURUSD at 1.26480 sl: 1.16480 tp: 1.31480 at price 1.26685
2014.10.27 15:07:33.221 2014.10.24 00:00  Tester: order #6, buy 0.01 EURUSD is opened at 1.26480
2014.10.27 15:07:33.221 2014.10.24 00:00  maxima88x_1 EURUSD,H1: open #6 buy stop 0.01 EURUSD at 1.26480 sl: 1.16480 tp: 1.31480 ok
2014.10.27 15:07:33.221 2014.10.24 00:00  maxima88x_1 EURUSD,H1: OrderSend error 130
2014.10.27 15:07:33.221 2014.10.23 23:30  maxima88x_1 EURUSD,H1: close #5 buy 0.01 EURUSD at 1.26500 sl: 1.16500 tp: 1.31500 at price 1.26457
2014.10.27 15:07:33.164 2014.10.23 00:00  Tester: order #5, buy 0.01 EURUSD is opened at 1.26500
2014.10.27 15:07:33.164 2014.10.23 00:00  maxima88x_1 EURUSD,H1: open #5 buy stop 0.01 EURUSD at 1.26500 sl: 1.16500 tp: 1.31500 ok
2014.10.27 15:07:33.164 2014.10.23 00:00  maxima88x_1 EURUSD,H1: OrderSend error 130
2014.10.27 15:07:33.164 2014.10.23 00:00  maxima88x_1 EURUSD,H1: OrderSend error 130
2014.10.27 15:07:33.164 2014.10.23 00:00  maxima88x_1 EURUSD,H1: OrderSend error 130
2014.10.27 15:07:33.164 2014.10.23 00:00  maxima88x_1 EURUSD,H1: OrderSend error 130
2014.10.27 15:07:33.164 2014.10.23 00:00  maxima88x_1 EURUSD,H1: OrderSend error 130
2014.10.27 15:07:33.164 2014.10.22 23:30  maxima88x_1 EURUSD,H1: close #4 buy 0.01 EURUSD at 1.27160 sl: 1.17161 tp: 1.32161 at price 1.26415
2014.10.27 15:07:33.101 2014.10.22 00:00  Tester: order #4, buy 0.01 EURUSD is opened at 1.27160
2014.10.27 15:07:33.101 2014.10.22 00:00  maxima88x_1 EURUSD,H1: open #4 buy stop 0.01 EURUSD at 1.27160 sl: 1.17161 tp: 1.32161 ok
2014.10.27 15:07:33.101 2014.10.22 00:00  maxima88x_1 EURUSD,H1: OrderSend error 130
2014.10.27 15:07:33.101 2014.10.22 00:00  maxima88x_1 EURUSD,H1: OrderSend error 130
2014.10.27 15:07:33.100 2014.10.21 23:30  maxima88x_1 EURUSD,H1: close #3 buy 0.01 EURUSD at 1.28010 sl: 1.18011 tp: 1.33011 at price 1.27157
2014.10.27 15:07:33.030 2014.10.21 00:00  Tester: order #3, buy 0.01 EURUSD is opened at 1.28010
2014.10.27 15:07:33.030 2014.10.21 00:00  maxima88x_1 EURUSD,H1: open #3 buy stop 0.01 EURUSD at 1.28010 sl: 1.18011 tp: 1.33011 ok
2014.10.27 15:07:33.029 2014.10.20 23:30  maxima88x_1 EURUSD,H1: close #2 buy 0.01 EURUSD at 1.27510 sl: 1.17511 tp: 1.32511 at price 1.28049
2014.10.27 15:07:32.959 2014.10.20 00:00  Tester: order #2, buy 0.01 EURUSD is opened at 1.27510
2014.10.27 15:07:32.959 2014.10.20 00:00  maxima88x_1 EURUSD,H1: open #2 buy stop 0.01 EURUSD at 1.27510 sl: 1.17511 tp: 1.32511 ok
2014.10.27 15:07:32.959 2014.10.20 00:00  maxima88x_1 EURUSD,H1: OrderSend error 130
2014.10.27 15:07:32.959 2014.10.20 00:00  maxima88x_1 EURUSD,H1: OrderSend error 130
2014.10.27 15:07:32.959 2014.10.20 00:00  maxima88x_1 EURUSD,H1: OrderSend error 130
2014.10.27 15:07:32.959 2014.10.20 00:00  maxima88x_1 EURUSD,H1: OrderSend error 130
2014.10.27 15:07:32.958 2014.10.20 00:00  maxima88x_1 EURUSD,H1: OrderSend error 130
2014.10.27 15:07:32.958 2014.10.20 00:00  maxima88x_1 EURUSD,H1: OrderSend error 130
2014.10.27 15:07:32.958 2014.10.20 00:00  maxima88x_1 EURUSD,H1: OrderSend error 130
2014.10.27 15:07:32.958 2014.10.20 00:00  maxima88x_1 EURUSD,H1: OrderSend error 130
2014.10.27 15:07:32.958 2014.10.20 00:00  maxima88x_1 EURUSD,H1: OrderSend error 130
2014.10.27 15:07:32.958 2014.10.20 00:00  maxima88x_1 EURUSD,H1: OrderSend error 130
2014.10.27 15:07:32.958 2014.10.20 00:00  maxima88x_1 EURUSD,H1: OrderSend error 130
2014.10.27 15:07:32.958 2014.10.20 00:00  maxima88x_1 EURUSD,H1: OrderSend error 130
2014.10.27 15:07:32.958 2014.10.20 00:00  maxima88x_1 EURUSD,H1: OrderSend error 130
2014.10.27 15:07:32.958 2014.10.20 00:00  maxima88x_1 EURUSD,H1: OrderSend error 130
2014.10.27 15:07:32.958 2014.10.20 00:00  maxima88x_1 EURUSD,H1: OrderSend error 130
2014.10.27 15:07:32.958 2014.10.20 00:00  maxima88x_1 EURUSD,H1: OrderSend error 130
2014.10.27 15:07:32.958 2014.10.20 00:00  maxima88x_1 EURUSD,H1: OrderSend error 130
2014.10.27 15:07:32.958 2014.10.20 00:00  maxima88x_1 EURUSD,H1: OrderSend error 130
2014.10.27 15:07:32.958 2014.10.20 00:00  maxima88x_1 EURUSD,H1: OrderSend error 130
2014.10.27 15:07:32.958 2014.10.20 00:00  maxima88x_1 EURUSD,H1: OrderSend error 130
2014.10.27 15:07:32.958 2014.10.20 00:00  maxima88x_1 EURUSD,H1: OrderSend error 130
2014.10.27 15:07:32.958 2014.10.20 00:00  maxima88x_1 EURUSD,H1: OrderSend error 130
2014.10.27 15:07:32.958 2014.10.20 00:00  maxima88x_1 EURUSD,H1: OrderSend error 130
2014.10.27 15:07:32.958 2014.10.20 00:00  maxima88x_1 EURUSD,H1: OrderSend error 130
2014.10.27 15:07:32.958 2014.10.20 00:00  maxima88x_1 EURUSD,H1: OrderSend error 130
2014.10.27 15:07:32.958 2014.10.20 00:00  maxima88x_1 EURUSD,H1: OrderSend error 130
2014.10.27 15:07:32.958 2014.10.20 00:00  maxima88x_1 EURUSD,H1: OrderSend error 130
2014.10.27 15:07:32.958 2014.10.20 00:00  maxima88x_1 EURUSD,H1: OrderSend error 130
2014.10.27 15:07:32.958 2014.10.20 00:00  maxima88x_1 EURUSD,H1: OrderSend error 130
2014.10.27 15:07:32.958 2014.10.17 23:30  maxima88x_1 EURUSD,H1: close #1 buy 0.01 EURUSD at 1.28090 sl: 1.18097 tp: 1.33097 at price 1.27589
2014.10.27 15:07:32.889 2014.10.17 00:00  Tester: order #1, buy 0.01 EURUSD is opened at 1.28090
2014.10.27 15:07:32.889 2014.10.17 00:00  maxima88x_1 EURUSD,H1: open #1 buy stop 0.01 EURUSD at 1.28090 sl: 1.18097 tp: 1.33097 ok
2014.10.27 15:07:32.887 maxima88x_1 inputs: GMTOffset=-1; PriceOrPoints=0; PriceOrPointsValue=8; BuyOrSell=0; StopLossPoints=5000; TakeProfitPoints=5000; Lots=0.01; AutoLot=0; EquityRiskPercentage=0.01; 
Files:
eatest.mq4  5 kb
 

Could it be that you calc. the buy-price from Bid while for buy you have to use Ask and therefore the order-price is too close or even smaller than Ask?:


if (BuyOrSell == buy)
            PriceOrPointsValue = Bid+PriceOrPointsValue*Point;
...
 

ionone: error 130

extern int StopLossPoints = 0;
extern int TakeProfitPoints = 0;
:
OpenBuy = OrderSend(Symbol(), OP_BUYSTOP, Lots, p1, 3, PriceOrPointsValue - StopLossPoints * Point * 2 + spread, PriceOrPointsValue + TakeProfitPoints * Point + spread, "woohoo", 8675309, time + 3600 * 1, Green);

OpenSell = OrderSend(Symbol(), OP_SELLSTOP, Lots, p2, 3, PriceOrPointsValue + StopLossPoints * Point - spread, PriceOrPointsValue - TakeProfitPoints * Point - spread, "woohooooo", 8675309, time + 3600 * 1, Green);
  1. I never got stops with SL/TP to work when I tried it years ago. I think the terminal was comparing the SL to current market not pending price. A) Open the order then set the stops. Or B) Don't use stops. Humans must because they can't watch the screen every second, EA's can. Just wait for market to reach the trigger.
  2. For a buy you open at the Ask, the SL/TP are relative to the Bid. If P1/PriceOrPointsValue is a Bid price, you must add the spread to the pending price (to get the Ask) and not add it to the SL/TP.
  3. For a sell you open at the Bid. P2/PriceOrPointsValue must be a Bid price and you must add, not subtract, the spread to make the SL/TP relative to the Ask.
  4. If the SLP is zero your SL is the pending Ask plus the spread. SL must be below the pending Bid by at least MODE_STOPLEVEL*Point thus error 130
 
ionone:
HI

i'm trying to programm an EA that places orders at a certain time of the day and close it at another.

The EA works but i have error messages (OrderSend error 130 which means wrong stopLoss) but i cannot find where the error is. 

Test for errors and report them and any associated variables . . . track down your error methodically.

 What are Function return values ? How do I use them ?

Requirements and Limitations in Making Trades

Strategy tester error 130 

 

wow thanks for your answers!

@gooly:

hmm that's good (i'm a real noobie so i didn't noticed that error), i tried that and i've got a new error. Now it doesn't close the buy. here is the log:

2014.10.27 18:25:48.026 EURUSD,H1: 306921 tick events (1145 bars, 307921 bar states) processed within 234 ms (total time 4618 ms)
2014.10.27 18:25:47.798 2014.10.17 01:00  Tester: #1 deleted due expiration
2014.10.27 18:25:47.797 2014.10.17 00:00  maxima88x_1 EURUSD,H1: open #1 buy stop 0.01 EURUSD at 1.28110 sl: 1.23117 tp: 1.33117 ok
2014.10.27 18:25:47.796 maxima88x_1 inputs: GMTOffset=-1; PriceOrPoints=0; PriceOrPointsValue=8; BuyOrSell=0; StopLossPoints=5000; TakeProfitPoints=5000; Lots=0.01; AutoLot=0; EquityRiskPercentage=0.0

@RaptorUK:

i already did that i was printing on the screen the sl values and trying to understand why some values were passing through and others wouldn't. And i couldn't find a pattern in these errors.


As i'm buying at a determined time, i suppose i don't need to use pending orders? i can use OP_BUY instead of OP_BUYSTOP? And when i do that, i end up with a "138" error that i get rid by augmenting the slippage from 3 to 10 (as i'm not scalping it's okay).

and everything works without errors.

Is it the right way to go?

thanks again.

Jeff.

 

"Is it the right way to go?"

It depends on your code and your strategy - I can't tell you anything so far.

But - it might be better not to continue trying and but look for an existing EA and open source code and copy its order management - google for trade management?

 

yes i still have a lot to learn.


The code i've got so far has parts from other people, i didn't code a lot (though i'm originally a C++/Java programmer)


so i finally updated the code and it's final, had to fix some bugs and there you go!


enum buysell
{
    buy,
    sell
};
enum pointsprice
{
   points,
   price
};
extern string g = "+++ Enter your CHART GMT offset +++";
extern string h = "+++ Will calculate GMT hour as ChartHour+GMTOffset +++";
extern int GMTOffset = 1;
extern string q = "+++ Set points or price +++";
extern pointsprice PriceOrPoints = points;
extern double PriceOrPointsValue = 10;
extern buysell BuyOrSell = buy;
extern string d = "+++ Set stop loss/take profit in points +++";
extern int StopLossPoints = 5000;
extern int TakeProfitPoints = 5000;
extern string j = "+++ Number of lots to open +++";
extern double Lots = 0.1;
extern string z = "+++ Use automatic risk percentage lot adjustment +++";
extern bool AutoLot = false;
extern double EquityRiskPercentage = 0.01;
extern string w = "+++ Time Order/stop +++";
extern string OpenTime = "10:00:00";
extern string CloseTime = "23:30:00";

datetime prevTime;
bool dayPassed = true;
int OpenSell = -1;
int OpenBuy = -1;




int init()
  {
//----

    // 
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
{
   IndicatorDigits(Digits+2);
    
      
   datetime time = iTime(NULL,PERIOD_M1,0);
   
   
   int heuresOpen = StrToInteger(StringSubstr(OpenTime, 0, 2))+GMTOffset;
   int minutesOpen = StrToInteger(StringSubstr(OpenTime, 3, 2));
   int secondesOpen = StrToInteger(StringSubstr(OpenTime, 6, 2));

   
   if (heuresOpen < 0) heuresOpen = heuresOpen + 24;
   if (heuresOpen >= 24) heuresOpen = heuresOpen - 24;


   if (TimeDay(prevTime)< TimeDay(time))
   {
      dayPassed = true;
      
   }
 


              
   if (dayPassed == true && OpenSell == -1 && OpenBuy == -1 && TimeHour(time) >= heuresOpen && TimeMinute(time) >= minutesOpen && TimeSeconds(time) >= secondesOpen)
   {
RefreshRates();
      if (PriceOrPoints == points)
      {
         if (BuyOrSell == buy)
            PriceOrPointsValue = Ask+PriceOrPointsValue*Point;
         else
            PriceOrPointsValue = Bid+PriceOrPointsValue*Point;
      }
      
      //calculate lots
      double spread = Ask-Bid;
      if (AutoLot && EquityRiskPercentage < 1 && EquityRiskPercentage > 0)
      {
         Lots = (AccountEquity()*EquityRiskPercentage)/StopLossPoints;
      }
      
      if (BuyOrSell == buy)
      {
         double p1 = PriceOrPointsValue+spread;
         p1 = int(p1 * 10000)/10000.;



         
         OpenBuy = OrderSend(Symbol(), OP_BUY, Lots, p1, 10, PriceOrPointsValue-StopLossPoints*Point+spread,
                                       PriceOrPointsValue+TakeProfitPoints*Point+spread, "woohoo", 8675309,
                                       time+3600*1, Green);
                   
                   
      }
      else
      { 
         double p2 = PriceOrPointsValue-spread;
         p2 = int(p2 * 10000)/10000.;
         
         
         OpenSell = OrderSend(Symbol(), OP_SELL, Lots, p2, 10, PriceOrPointsValue+StopLossPoints*Point-spread,
                                        PriceOrPointsValue-TakeProfitPoints*Point-spread, "woohooooo", 8675309,
                                        time+3600*1, Green);  
    
      }                 
   }
   

   
  
   int heuresClose = StrToInteger(StringSubstr(CloseTime, 0, 2))+GMTOffset;
   int minutesClose = StrToInteger(StringSubstr(CloseTime, 3, 2));
   int secondesClose = StrToInteger(StringSubstr(CloseTime, 6, 2));
   
   if (heuresClose < 0) heuresClose = heuresClose + 24;
   if (heuresClose >= 24) heuresClose = heuresClose - 24;
   
   
   int maxDuration = (heuresClose-heuresOpen)*3600 + (minutesClose - minutesOpen) * 60 + (secondesClose - secondesOpen);
   
   for(int pos = OrdersTotal()-1; pos >= 0 ; pos--) if (
       OrderSelect(pos, SELECT_BY_POS)            // Only my orders w/
   &&  OrderMagicNumber() == 8675309         // my magic number
   &&  OrderSymbol()      == Symbol() ){               // and period and symbol
       int duration = TimeCurrent() - OrderOpenTime();
       if (duration >= maxDuration)
       {
            OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 3*1);
            OpenSell = -1;
            OpenBuy = -1;   
            
            dayPassed = false;                  
       }
   }
   
   prevTime = time;
   
   return(0);
}
//+------------------------------------------------------------------+
 

and i didn't find any already made code that would do what i need


the two i found are:

CAP Pending Order at Time

https://www.mql5.com/en/market/product/3095


and

Expert Pending Orders

https://www.mql5.com/en/market/product/4591


but i couldn't make it work with MT4 Alpari UK, and i'm not looking for pending orders anyway.


Jeff

 
https://www.mql5.com/en/job
 
ionone:


@RaptorUK:

i already did that i was printing on the screen the sl values and trying to understand why some values were passing through and others wouldn't. And i couldn't find a pattern in these errors.

I don't see the Print() statements in your code . . .  did you verify that your trades meet the   Requirements and Limitations in Making Trades ?
 

@deysmacro:


yes i could hire someoie dto do the job but it wouldn't be as customizable as doing it yourself, plus i'm a programmer so i might as well learn how to do it myself


@RaptorUK:

do i have to check for all these requirements each time i want to put a new order? in other EA i've watched, there wasn't these tests...


Jeff