Trailing stop multipair EA - question/help

 

Hi

Im new to mql4 and Im still in learning state.. Im trying to code my multipair EA with trailing stop. Problem is that this code wont add the trailing stop to opened trades.. What am I missing here ?

Here is what I got:

void trail()
{

for(int j=0; j<ArraySize(iPairs); j++)

   { 
      for (int i = 0; i < OrdersTotal(); i++) {
         OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
         string cur=iPairs[j];
         if ( OrderSymbol()==cur )
         {
            if (OrderType() == OP_BUY) {
               if (Bid - OrderOpenPrice() > TrailingStop * MarketInfo(OrderSymbol(), MODE_POINT)) {
                  if (OrderStopLoss() < Bid - TrailingStop * MarketInfo(OrderSymbol(), MODE_POINT)) {
                     OrderModify(OrderTicket(), OrderOpenPrice(), Bid - TrailingStop * MarketInfo(OrderSymbol(), MODE_POINT), OrderTakeProfit(), Red);
                  }
               }
            } else if (OrderType() == OP_SELL) {
               if (OrderOpenPrice() - Ask > TrailingStop * MarketInfo(OrderSymbol(), MODE_POINT)) {
                  if ((OrderStopLoss() > Ask + TrailingStop * MarketInfo(OrderSymbol(), MODE_POINT)) ||
                     (OrderStopLoss() == 0)) {
                     OrderModify(OrderTicket(), OrderOpenPrice(),
                     Ask + TrailingStop * MarketInfo(OrderSymbol(), MODE_POINT), OrderTakeProfit(), Red);
                  }
               }
            }
         }
      }
   }
}

 
suikka:

Hi

Im new to mql4 and Im still in learning state.. Im trying to code my multipair EA with trailing stop. Problem is that this code wont add the trailing stop to opened trades.. What am I missing here ?

Here is what I got:

Add error checking and debug your code.

OrderSelect() and OrderModify() are functions which return a value, check them. See documentation.

 
angevoyageur:

Add error checking and debug your code.

OrderSelect() and OrderModify() are functions which return a value, check them. See documentation.

Thank you for your reply.. I debugged this code further and now Im little bit wiser but still dont know what is wrong here.

if (OrderStopLoss() < Bid - TrailingStop * MarketInfo(OrderSymbol(), MODE_POINT)) {
    Print(OrderSelect(i, SELECT_BY_POS, MODE_TRADES), OrderSymbol(), OrderModify(OrderTicket(), OrderOpenPrice(), Bid - TrailingStop * MarketInfo(OrderSymbol(), MODE_POINT), OrderTakeProfit(), Red));     
    OrderModify(OrderTicket(), OrderOpenPrice(), Bid - TrailingStop * MarketInfo(OrderSymbol(), MODE_POINT), OrderTakeProfit(), Red);
}

I added this Print() here, it prints

true  EURUSD false.

So this means...

OrderModify(OrderTicket(), OrderOpenPrice(), Bid - TrailingStop * MarketInfo(OrderSymbol(), MODE_POINT), OrderTakeProfit(), Red);

.. this isn't working, but I have no idea why.


I adjusted TrailingStop to very low but still now trailing stop.. damn..

 
suikka:

Thank you for your reply.. I debugged this code further and now Im little bit wiser but still dont know what is wrong here.


I added this Print() here, it prints

true  EURUSD false.

So this means...

.. this isn't working, but I have no idea why.


I adjusted TrailingStop to very low but still now trailing stop.. damn..

OrderSelect() and OrderModify() are functions which return a value, check them. See documentation.

 

Error in OrderModify(). Error code=130.

"If the program tries to change the position of a stop order in such a way that it is placed closer to the market than the allowed minimum distance, such trade request will be rejected by the client terminal and the execution of the function OrderModify() will fail (error 130). This is why you should provide a special block in your program, which will consider this limitation."


Could somebody please explain this to me ? What does this mean : "This is why you should provide a special block in your program, which will consider this limitation."

I found this on other forum:

"Too close stops. Your TrailingStop value is less than freeze level. To know freeze level use MarketInfo function."


Maybe somebody could help me little bit with this one ?

 
MarketInfo(OrderSymbol(), MODE_POINT)  is returning "1e-005". I think this is the problem, but I still dont know how to fix this.
 
suikka:
MarketInfo(OrderSymbol(), MODE_POINT)  is returning "1e-005". I think this is the problem, but I still dont know how to fix this.
1e-005 is the same as 0.00001, it's correct.
 
suikka:

Error in OrderModify(). Error code=130.

"If the program tries to change the position of a stop order in such a way that it is placed closer to the market than the allowed minimum distance, such trade request will be rejected by the client terminal and the execution of the function OrderModify() will fail (error 130). This is why you should provide a special block in your program, which will consider this limitation."


Could somebody please explain this to me ? What does this mean : "This is why you should provide a special block in your program, which will consider this limitation."

I found this on other forum:

"Too close stops. Your TrailingStop value is less than freeze level. To know freeze level use MarketInfo function."


Maybe somebody could help me little bit with this one ?

What is the value of TrailingStop ? What's the value of MarketInfo(_Symbol,MODE_STOPLEVEL) ?

See https://book.mql4.com/appendix/limits

Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial
  • book.mql4.com
Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial
 
angevoyageur:

What is the value of TrailingStop ? What's the value of MarketInfo(_Symbol,MODE_STOPLEVEL) ?

See https://book.mql4.com/appendix/limits

Thank you for being kind and helping..

MarketInfo(_Symbol,MODE_STOPLEVEL) = 0


for(int j=0; j<ArraySize(iPairs); j++)

   { 
      for (int i = 0; i < OrdersTotal(); i++) {
         OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
         string cur=iPairs[j];
         if ( OrderSymbol()==cur )
         {
            if (OrderType() == OP_BUY) {
               if (Bid - OrderOpenPrice() > TrailingStop * MarketInfo(OrderSymbol(), MODE_POINT)) {
                  if (OrderStopLoss() < Bid - TrailingStop * MarketInfo(OrderSymbol(), MODE_POINT)) {
                     bool res=OrderModify(OrderTicket(), OrderOpenPrice(), Bid - TrailingStop * MarketInfo(OrderSymbol(), MODE_POINT), OrderTakeProfit(), Red);
                     int MinStopDist = MarketInfo(cur,MODE_STOPLEVEL);
                    
                     if(!res){
                        Print("Error in OrderModify. CurrencyPair = " + cur + "  Error code=",GetLastError());
                        Print("CurrencyPair = " + cur + "  MODE_STOPLEVEL = " +MinStopDist);
                        
                     }else{
                        Print("Order modified successfully.");
                     }
                     
                  }
               }
            } else if (OrderType() == OP_SELL) {
               if (OrderOpenPrice() - Ask > TrailingStop * MarketInfo(OrderSymbol(), MODE_POINT)) {
                  if ((OrderStopLoss() > Ask + TrailingStop * MarketInfo(OrderSymbol(), MODE_POINT)) ||
                     (OrderStopLoss() == 0)) {
                     OrderModify(OrderTicket(), OrderOpenPrice(),
                     Ask + TrailingStop * MarketInfo(OrderSymbol(), MODE_POINT), OrderTakeProfit(), Red);
                  }
               }
            }
         }
      }
   }
}


This returns error: 

Error in OrderModify.CurrencyPair = EURCAD Error code=130

CurrencyPair = EURCAD MODE_STOPLEVEL = 0

I have been trying with TrailingStop values of 10 - 400. Everytime I got the same error. Even tho trade is 500 points positive.

 
suikka:

Thank you for being kind and helping..

MarketInfo(_Symbol,MODE_STOPLEVEL) = 0



This returns error: 

Error in OrderModify.CurrencyPair = EURCAD Error code=130

CurrencyPair = EURCAD MODE_STOPLEVEL = 0

I have been trying with TrailingStop values of 10 - 400. Everytime I got the same error. Even tho trade is 500 points positive.

You have to normalize your price, try :

NormalizeDouble(Bid - TrailingStop * MarketInfo(OrderSymbol(), MODE_POINT),MarketInfo(OrderSymbol(), MODE_DIGIT))
 
angevoyageur:

You have to normalize your price, try :

Tried that and didn't help. Same error again.

BTW, this code works on single chart/currencypair EA perfectly.

void trail()
{
   for (int i = 0; i < OrdersTotal(); i++) {
   OrderSelect(i, SELECT_BY_POS, MODE_TRADES);

   if ( OrderSymbol()==Symbol() )
   {
      if (OrderType() == OP_BUY) {
         if (Bid - OrderOpenPrice() > TrailingStop * MarketInfo(OrderSymbol(), MODE_POINT)) {
              if (OrderStopLoss() < Bid - TrailingStop * MarketInfo(OrderSymbol(), MODE_POINT)) {
                  OrderModify(OrderTicket(), OrderOpenPrice(), Bid - TrailingStop * MarketInfo(OrderSymbol(), MODE_POINT), OrderTakeProfit(), Red);
               }
         }
      } else if (OrderType() == OP_SELL) {
         if (OrderOpenPrice() - Ask > TrailingStop * MarketInfo(OrderSymbol(), MODE_POINT)) {
            if ((OrderStopLoss() > Ask + TrailingStop * MarketInfo(OrderSymbol(), MODE_POINT)) ||
               (OrderStopLoss() == 0)) {
               OrderModify(OrderTicket(), OrderOpenPrice(),
               Ask + TrailingStop * MarketInfo(OrderSymbol(), MODE_POINT), OrderTakeProfit(), Red);
               }
           }
      }
   }
   }
}


All I did is added this for loop to it and it wont work anymore..

for(int j=0; j<ArraySize(iPairs); j++)