Stop loss error 130

 

I need some help with this stop loss issue. the following code gives me a 130 error (invalid stop loss).

Even though the stop loss and take profit are above the minimum limit?

i dont get it at all

thanks to anyone who can help

________________________________________________________________________________________________________________________

#property copyright "Copyright © 2011, MetaQuotes Software Corp."

#property link "http://www.metaquotes.net"

extern double Upper_Band =0.00; //value of upper band

extern double Lower_Band =0.00; //value of lower band

extern double Middle_Band =0.00; //value of lower band

extern bool In_Trade = false; //in trade or not?

extern int Entry_Step_Upper ; //Entry stage upper band

extern int Entry_Step_Lower ; //Entry stage lower band

extern double StopLoss =25.00; // SL for an opened order

extern double TakeProfit =50.00; // SL for an opened order

extern double Lots =0.1; // Ammount of Lots in a position

extern int ticket=0 ; //ticket

extern int updateammount=0;


string Position; // Security name


extern bool Ans;



// expert initialization function

int init()

{


return(0);

}


// expert deinitialization function

int deinit()

{

Alert (updateammount);

return(0);

}


// expert start function

int start()

{

if(AccountFreeMargin()<(1000*Lots))

{

Alert("We have no money. Free Margin = ", AccountFreeMargin());

}

if(Bars<100)

{

Alert("bars less than 100");

return(0);

}

Upper_Band = iBands(NULL,0,20,2,0,PRICE_LOW,MODE_UPPER,0);

Lower_Band = iBands(NULL,0,20,2,0,PRICE_LOW,MODE_LOWER,0);

Middle_Band= iBands(NULL,0,20,2,0,PRICE_LOW,MODE_MAIN,0);

//Entering Trade

if(OrdersTotal()<1){

In_Trade=false;

}

else

{

In_Trade=true;

}

if(In_Trade==false)

{

//Selling criterion

//Step1

if(High[0]>Upper_Band&&Entry_Step_Upper==0)

{

Entry_Step_Upper=1;

}

//Step2

if(High[0]<Upper_Band&&Entry_Step_Upper==1)

{

Entry_Step_Upper=2;

}

//Place Short

if (Entry_Step_Upper==2)

{

//place order

RefreshRates();

ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+StopLoss*Point,Bid-TakeProfit*Point,"Bollinger",16384,0,Red);//Opening Sell

if(ticket>0)

{

if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());

Position="sell";

Entry_Step_Upper=0;

In_Trade=true;

}

else Print("Error opening BUY order : ",GetLastError());

}

//Buying criterion

//Step1

if(Low[0]<Lower_Band&&Entry_Step_Lower==0)

{

Entry_Step_Lower=1;

}

//Step2

if(Low[0]>Lower_Band&&Entry_Step_Lower==1)

{

Entry_Step_Lower=2;

}

//Place Long

if (Entry_Step_Lower==2)

{

//place order

RefreshRates();

ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point,"Bollinger",16384,0,Green);//Opening Buy

if(ticket>0)

{

if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());

Position="buy";

Entry_Step_Lower=0;

In_Trade=true;

updateammount+=1;

}

else Print("Error opening BUY order : ",GetLastError());

}

}

//Exit Trade

if(In_Trade==true)

{

//close Sell position

if (Position=="sell")

{

if( Ask<Middle_Band)

{

RefreshRates();

Ans=OrderClose(OrderTicket(),OrderLots(),Ask,2,Violet); // Closing Buy

if (Ans==true) // Success :)

{

Alert ("Closed order Buy ",OrderTicket());

Position=" ";

Entry_Step_Upper=0;

In_Trade=false;

ticket=0;

}

}

}

//Close buy Position

if (Position=="buy")

{

if(Ask>Middle_Band)

{

RefreshRates();

Ans=OrderClose(OrderTicket(),OrderLots(),Bid,2,Violet); // Closing Buy

if (Ans==true) // Success :)

{

Alert ("Closed order Buy ",OrderTicket());

Position=" ";

Entry_Step_Lower=0;

In_Trade=false;

ticket=0;

}

}

}

}

return(0);

}

________________________________________________________________________________________________________________________

 

This is the most common error around here. There are thousands of post showing how to avoid this. Do a Google/Bing search.

ERR_INVALID_STOPS 130 Stops are too close, or prices are ill-calculated or unnormalized (or in the open price of a pending order). The attempt can be repeated only if the error occurred due to the price obsolescense. After 5-second (or more) delay, it is necessary to refresh data using the RefreshRates function and make a retry. If the error does not disappear, all attempts to trade must be stopped, the program logic must be changed.
You get the stop-level by function

MarketInfo(Symbol(),MODE_STOPLEVEL);
 

. . . and please . . .

Please use this to post code . . . it makes it easier to read.

 

You need to adjust in your code for 4 or 5 digit Brokers . . . if you are using a 5 Digit broker your stoploss is 2.5 pips.

Look here for help: 4/5 digits

If your Sell order fails you report it as a failed Buy order . . .

else Print("Error opening BUY order : ",GetLastError());


Is your Broker an ECN Broker ? if they are then you need to send the order with TP & SL set at 0 then do an OrderModify to add the TP & SL, info here: ECN Brokers

You don't seem to be allowing for spread . . . a Sell is opened at Bid but closed (SL & TP) at Ask, a Buy is opened at Ask but closed at Bid.

Happy New Year :-)

 

  1. extern double StopLoss =25.00; // SL for an opened order
    :
    ticket=OrderSend(Symbol(), OP_BUY, Lots, Ask, 3, Ask-StopLoss*Point, Ask+TakeProfit*Point,
                                                          "Bollinger", 16384, 0, Green); //Opening Buy
    a 2.5 pip SL on a 5 digit broker may be below stoplevel. And a 0.3 pip slippage us mostly useless. Will not work on a ECN broker.
    //++++ These are adjusted for 5 digit brokers.
    int     pips2points;    // slippage  3 pips    3=points    30=points
    double  pips2dbl;       // Stoploss 15 pips    0.015      0.0150
    int     Digits.pips;    // DoubleToStr(dbl/pips2dbl, Digits.pips)
    int     init(){
         if (Digits % 2 == 1){      // DE30=1/JPY=3/EURUSD=5 https://www.mql5.com/en/forum/135345
                    pips2dbl    = Point*10; pips2points = 10;   Digits.pips = 1;
        } else {    pips2dbl    = Point;    pips2points =  1;   Digits.pips = 0; }
        // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl
    //---- These are adjusted for 5 digit brokers.
        /* On ECN brokers you must open first and THEN set stops
        int ticket = OrderSend(..., 0,0,...)
        if (ticket < 0)
           Alert("OrderSend failed: ", GetLastError());
        else if (!OrderSelect(ticket, SELECT_BY_TICKET))
           Alert("OrderSelect failed: ", GetLastError());
        else if (!OrderModify(OrderTicket(), OrderOpenPrice(), SL, TP, 0)
           Alert("OrderModify failed: ", GetLastError());
         */
    

  2. if(OrdersTotal()<1){
    In_Trade=false;
    }
    This makes the EA incompatable with other EA (including itself on other charts.) Always filter by magic number.
    int count = 0;
        for(int iPos = OrdersTotal()-1; iPos >= 0; iPos--) if( // Open orders
            OrderSelect(iPos, SELECT_BY_POS)                // Only my orders w/
        &&  OrderMagicNumber()  == Magic.Number             // my magic number
        &&  OrderSymbol()       == chart.symbol             // and my pair.
        ){ count++; }
    In_Trade = count != 0;

Reason: