Hedge EA WILL NOT WORK: ERROR 130, 4107, AND 4109

 

Can someone please help me with this code. It is a hedge strategy I am trying to write but I can not get it to work. It is suppose to open a buy and sell simultaneously. When either side is stopped out, the stop of the winning order should move to the opening price of the order(Break-even) then trail until taget hit or trailed stop hit. Here is the code, thanks much in advance.

Code:


//---- input parameters

extern double Lots= 0.3;
extern double Slip= 3;
extern int TakeProfit = 1000; // 0: disable; >0: enable TakeProfit of 200 > 20 pips
extern int StopLoss = 300; //0: disable; >0: enable StopLoss of 200 > 20 pips
extern int TrailStop = 300; //0: disable; >0: enable (StopLoss must be enabled too)


//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{int ticket;
int total =OrdersTotal();
double TP=TakeProfit*Point;
double SL=StopLoss*Point;
double TS=TrailStop*Point;

double bid =MarketInfo(Symbol(),MODE_BID); // Request for the value of Bid
double ask =MarketInfo(Symbol(),MODE_ASK); // Request for the value of Ask
double point =MarketInfo(Symbol(),MODE_POINT);//Request for Point

//Modifying stop-loss of open orders------------------------------------------------------+
if(StopLoss>0 && TrailStop>0)
for(int i=0;i<total;i++)
{Print(" i ", i);
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderType()==OP_BUY)
{Print("inside buy ",Bid-OrderOpenPrice(), TS) ;
if(Bid-OrderOpenPrice()>TS)
if(OrderStopLoss()<Bid-TS)
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TS,OrderTakeProfit(),0,Blue);
}
else if(OrderType()==OP_SELL)
{Print("inside sell ",OrderOpenPrice()-Ask, TS) ;
if(OrderOpenPrice()-Ask>TS)
if(OrderStopLoss()>Ask+TS)
OrderModify(OrderTicket(),OrderOpenPrice(),Ask+TS,OrderTakeProfit(),0,Red);
}
if(total < 1)
{
ticket = OrderSend(Symbol(),OP_BUY, Lots, Ask, Slip, Ask-SL, Ask+TP,"HedgeMeBuy", 4061, 0, Green);
ticket = OrderSend(Symbol(),OP_SELL, Lots, Bid, Slip, Bid+SL, Bid-TP,"HedgeMeSell", 4062, 0, Red);
Alert(GetLastError());
}
//----
// Print("stoplossafter = ", SL, " takeprofit = ", TP, " slipage = ", Slip);
return;
}
//+------------------------------------------------------------------+

 

Replace

OrderModify(OrderTicket(),OrderOpenPrice(),Ask+TS,OrderTakeProfit(),0,Red);
}
if(total < 1)

to

OrderModify(OrderTicket(),OrderOpenPrice(),Ask+TS,OrderTakeProfit(),0,Red);
}
}
if(total < 1)
 
ERR_INVALID_STOPS
130Invalid stops.

Stops to close to price. are u using 5 digit broker?

ERR_INVALID_PRICE_PARAM4107Invalid price.

invalid price, use RefreshRates()

ERR_TRADE_NOT_ALLOWED4109Trade is not allowed. Enable checkbox "Allow live trading" in the expert properties.

u havn't activated "allow live trading" in expert.

 
enotrek:
ERR_INVALID_STOPS
130Invalid stops.

Stops to close to price. are u using 5 digit broker?

ERR_INVALID_PRICE_PARAM4107Invalid price.

invalid price, use RefreshRates()

ERR_TRADE_NOT_ALLOWED4109Trade is not allowed. Enable checkbox "Allow live trading" in the expert properties.

u havn't activated "allow live trading" in expert.



I am using fxcm metatrader demo account, I believe is 5 digit. Does this mean I have to write in the stops a certain way? I have come to figure out the 4109 error, my mistake. Since I am newbie coder, could you explain how to use RefreshRates()? Thanks for help.
 
Roger:

Replace

to



Thanks. I don't get the errors anymore, but it still does not open the buy/sell orders. Is there something wrong with the OrderSend? Thanks a lot for help?
 

Error 130 invalid stops

in 3 & 5 digit brokers, 1 pip is 10 points.

example:

if you have SL to 5 points it will be 0.5 pips = to close to price: error 130

https://www.mql5.com/en/forum/123369


Error 4109 Invalid price

use RefreshRates() before sending an order into market to ger correct price,

Examle

RefreshRates();
int Ticket;
Ticket = OrderSend(...);
if(Ticket > 0)
   {
     Print("OrderSended correctly, Ticket# ",Ticket);
   }
else
   {
     int error = getlasterror();
     print("error in send order, error#" , error);
   }


(example code only, havn't compiled it)


Make sure you use the right price when you lay orders:

Buy/Long = Ask

Sell/Short = bid


reverse when you close them

ie.

buy, bid

sell, ask.

 

  1. Since you are using a 5 digit broker you must adjust TP, SL, and slippage
    //++++ These are adjusted for 5 digit brokers.
    double  pips2points,    // slippage  3 pips    3=points    30=points
            pips2dbl;       // Stoploss 15 pips    0.0015      0.00150
    int     Digits.pips;    // DoubleToStr(dbl/pips2dbl, Digits.pips)
    int init(){
        if (Digits == 5 || Digits == 3){    // Adjust for five (5) digit brokers.
                    pips2dbl    = Point*10; pips2points = 10;   Digits.pips = 1;
        } else {    pips2dbl    = Point;    pips2points =  1;   Digits.pips = 0; }
        // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl
    

  2. double bid =MarketInfo(Symbol(),MODE_BID); // Request for the value of Bid
    double ask =MarketInfo(Symbol(),MODE_ASK); // Request for the value of Ask
    double point =MarketInfo(Symbol(),MODE_POINT);//Request for Point
    Why not just use the predefined variables Bid, Ask, Point
  3. You must RefreshData() between every server call. Between the two opens and between the two modifies