Download MetaTrader 5

Trailing stop multipair EA - question/help

To add comments, please log in or register
Deposit to your account via PayPal
suikka
77
suikka 2015.01.29 10:25 

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);
                  }
               }
            }
         }
      }
   }
}

Alain Verleyen
Moderator
30737
Alain Verleyen 2015.01.29 17:17  
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.

suikka
77
suikka 2015.01.29 18:04  
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..

Alain Verleyen
Moderator
30737
Alain Verleyen 2015.01.29 19:10  
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.

suikka
77
suikka 2015.01.29 20:43  

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 ?

suikka
77
suikka 2015.01.29 21:35  
MarketInfo(OrderSymbol(), MODE_POINT)  is returning "1e-005". I think this is the problem, but I still dont know how to fix this.
Alain Verleyen
Moderator
30737
Alain Verleyen 2015.01.29 23:15  
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.
Alain Verleyen
Moderator
30737
Alain Verleyen 2015.01.29 23:17  
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
suikka
77
suikka 2015.01.30 05:51  
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.

Alain Verleyen
Moderator
30737
Alain Verleyen 2015.01.30 09:00  
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))
suikka
77
suikka 2015.01.30 09:26  
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++)
To add comments, please log in or register