Jump to content

Recommended Posts

Posted

I started learning MQL last week. Because I was already familiar with Java and the programming concepts, it did not take long to learn MQL up to the standard that I could create an Expert Advisor.

 

However, I have a problem executing pending orders on my EA: I get the error code #130, which means invalid stops. I researched and found that it could be caused from: a stop loss level an invalid distance from the order price; or an unnormalized variable set as a parameter.

 

I looked over my EA and unless I have made an error which I cannot spot then the stop loss is greater than the required distance. Also, I have tried in many different ways to use the normalize function, which unless I'm using it incorrectly, does not seem to be correcting the error.

 

Therefore, I have hit a wall and cannot see how to fix the problem. Below is the part of the EA code that is generating the error.

void OpenTradeConditions()
  {
     //Support and resistance values from Dyn_S_R indicator
     double R0=iCustom(NULL, 0, "Dyn_s_r",1,0); //Resistance (DynR) Current Bar
     double R1=iCustom(NULL, 0, "Dyn_s_r",1,1); //Resistance (DynR) Previous Bar
  
     double S0=iCustom(NULL, 0, "Dyn_s_r",2,0); //Support (DynS) Current Bar
     double S1=iCustom(NULL, 0, "Dyn_s_r",2,1); //Support (DynS) Previous Bar
     //Incase cannot place pending order because its not far enough away from current price
     double MinimumPendingDistance = 3*PipValue;
     //Open trades
     ClosePendingOrdersWhenSRChanges();
     CheckOpenTrades();
     
     if(R0==R1 && OpenBuyOrders==false)
     {
        if(R0-MinimumPendingDistance<0)
        {
           //Place a buy order
           int ticket = 0;
           ticket=OrderSend(Symbol(),OP_BUY,LotSize,Ask,3,Ask-100*Point,0,"2 Percent Buy",0,0,Green);
           
           if(ticket<0)
           {
              Print("OrderB failed with error #",GetLastError());
              return(0);
           }

  
        }
     
        else
        {
           //Place pending buy stop order
           double BuyStopPrice = R0 + PipValue;
           double StopLoss1 = BuyStopPrice-(100*Point);
           int ticket2 = 0;
           double NewBS = NormalizeDouble(BuyStopPrice,5);
           double NewSL = NormalizeDouble(StopLoss1,5);
           double NewLS = NormalizeDouble(LotSize,1);
           
           
           ticket2=OrderSend(Symbol(),OP_BUYSTOP,NewLS,NewBS,3,NewSL,NULL,"2 Percent BuyStop",NULL,NULL,Lime);
           
           if(ticket2<0)
           {
              Print("OrderBS failed with error #",GetLastError());
              return(0);
           }          
        }
  
     } 

 

This is the specific section of the above method/function that is the problem:

else
        {
           //Place pending buy stop order
           double BuyStopPrice = R0 + PipValue;
           double StopLoss1 = BuyStopPrice-(100*Point);
           int ticket2 = 0;
           double NewBS = NormalizeDouble(BuyStopPrice,5);
           double NewSL = NormalizeDouble(StopLoss1,5);
           double NewLS = NormalizeDouble(LotSize,1);
           
           
           ticket2=OrderSend(Symbol(),OP_BUYSTOP,NewLS,NewBS,3,NewSL,NULL,"2 Percent BuyStop",NULL,NULL,Lime);
           
           if(ticket2<0)
           {
              Print("OrderBS failed with error #",GetLastError());
              return(0);
           }          
        }

 

If anyone could help me understand why the error is occurring and how to fix it, I would appreciate it.

 

(P.S. The EA was programmed specifically for a 5 digit broker, so the stop loss level or 100*Point is actually 10 normal pips)

Posted (edited)

Update: I have fixed this first problem I had. If anyone has a similar problem, the solution I had is that I had selected the wrong IndexBuffer for my custom indicator value, so my pending order position was invalid.

 

I now have another problem, which is that the EA places several buy stop/sell stop orders, but I only want it to place one of each. I think I will be able to solve this problem using an if statement. I will write my suggested algorithm in the code box below using non language dependent semi-pseudo-code in order to help anyone with a similar problem.

 

I will place this in my Start function

 

if(no. of open buy stop orders==0 && no. of open buy orders ==0)
{
  OpenTradeConditionsBuy(); //This is the name of my function which creates the order
}

if(no. of open sell stop orders==0 && no. of open sell orders==0)
{
  OpenTradeConditionsSell();
}

 

Update: I would appreciate if anyone could tell me how to write this in MQL, thankyou

if(no. of open buy stop orders==0 && no. of open buy orders ==0)

Edited by chrisbenjy
Posted
bool Have.Open.Buy.Orders()
{
  bool result = false;
  if(OrdersTotal()>0){
     for(int i=OrdersTotal()-1;i>=0;i--){
        OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
        if(OrderType() == OP_BUY && OrderMagicNumber() == magic){
           result = true;
           break;
        }
     }
  }
  return (result);
}

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...