Download MetaTrader 5

Need help with Expert Advisor

To add comments, please log in or register
All articles are available from MetaTrader 5. Contribute your own one!
arroganztrader
86
arroganztrader 2013.12.30 12:55 

Hello community,

I programmed an Expert Advisor that sets the SL to 2 Points for only ONE order at a time. When the order goes into profit it takes the SL to +2 Points and trails every +1 Point to 2.8 Points, 3.6 Points and so on.

That's the logic behind this program. But it only works for one order at a time. How can I modify this EA that it sets SL and trails it for every open order of the current chart symbol?

Below you see my code. Feel free to ask questions. Thank you very much!


//--- input parameters
extern double    trailingStop=0.8;
extern double    initialStopLoss=2.0; // StopLoss bei OrderOpen
extern double    inProfitStopLoss=2.0; // StopLoss der im Profit gezogen wird
bool isStopLoss;
double lastStopLoss;
bool isTrailing;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   isStopLoss = false;
   isTrailing = false;
   
   if(Digits == 2) {
      initialStopLoss = initialStopLoss * 100;
      inProfitStopLoss = inProfitStopLoss * 100;
   }
   
   //trailingStop = trailingStop * 100;
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   // nothing here
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
      if (OrdersTotal() == 0) {
         isStopLoss = false;
         isTrailing = false;
      }

      for(int i=0; i<=OrdersTotal(); i++) {
 
         if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) {
            continue; 
         }
         
         if(OrdersTotal() == 0) {
            continue;
         }
        
         OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
         
         if(isStopLoss == true) {
            if(OrderType() == OP_BUY) {
               if(iClose(Symbol(), 0, 0) >= (OrderOpenPrice()+inProfitStopLoss*Point)) { // Zieht bei 2.0 Pips nach! - 200 = 2.0 Pips
                  if(isTrailing == true) {
                     if(iClose(Symbol(), 0, 0) >= (lastStopLoss+1)) { // 1 Punkt vorher, davon 0.8 mitnehmen
                        OrderModify(OrderTicket(), OrderOpenPrice(), (lastStopLoss+trailingStop), 0,0,0);
                        lastStopLoss = OrderStopLoss();
                        //isTrailing = true;
                        continue;
                     }
                     continue;
                  }
                     OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+inProfitStopLoss*Point,0,0,0);
                     if(OrderStopLoss() >= OrderOpenPrice()) {
                        isTrailing = true;
                     }
                     lastStopLoss = OrderStopLoss(); // komisch, denn er zieht trailing hoch und runter
               }
            } else if(OrderType() == OP_SELL) {
               if(iClose(Symbol(), 0, 0) <= (OrderOpenPrice()-inProfitStopLoss*Point)) { 
                  if(isTrailing == true) {
                     if(iClose(Symbol(), 0, 0) <= (lastStopLoss-1)) { // vorher trailingStop
                        OrderModify(OrderTicket(), OrderOpenPrice(), (lastStopLoss-trailingStop), 0,0,0);
                        lastStopLoss = OrderStopLoss();
                        //isTrailing = true;
                        continue;
                     }
                     continue;
                  }
                     OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-inProfitStopLoss*Point,0,0,0); // 200 ursprünglich
                     if(OrderStopLoss() <= OrderOpenPrice()) {
                        isTrailing = true; // vorher false
                     }
                     lastStopLoss = OrderStopLoss();
               }
            }
            lastStopLoss = OrderStopLoss();
            continue;           
         }
         if(OrderType() == OP_BUY) {
            isStopLoss = true; 
            OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-initialStopLoss*Point,0,0,0); 
         } else if(OrderType() == OP_SELL) {
            isStopLoss = true; 
            OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+initialStopLoss*Point,0,0,0);
         }
         lastStopLoss = OrderStopLoss();
      }
//----
   return(0);
  }
//+------------------------------------------------------------------+
whroeder1
14477
whroeder1 2013.12.30 15:31  
      for(int i=0; i<=OrdersTotal(); i++) {
 
         if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) {
            continue; 
         }
         
         if(OrdersTotal() == 0) {
            continue;
         }
        
         OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
  1. OrderSelect when i == OrdersTotal will ALWAYS fail. positions are [0 .. total-1]
  2. When closing you must count down.
  3. What is the point of selecting the order twice?
  4. If you fix your loop, it does nothing when OrdersTotal == 0, so no need to check.
          for(int i=OrdersTotal()-1; i>=0; i--) if(
             OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) {
    
  5. Drop your isStopLoss, lastStopLoss they are unnecessary.
  6. Check your return codes What are Function return values ? How do I use them ? - MQL4 forum
  7. Filter by pair/magic number so it is compatible with other EAs including itself on other charts and manual trading.
  8. initialStopLoss*Point means the EA doesn't adjust for 4/5 digit brokers.

arroganztrader
86
arroganztrader 2013.12.30 20:35  

Very big thank you for your kind reply. Will try to modify the EA and post a newer version here.

Wish you the very best for the upcoming year.

arroganztrader
86
arroganztrader 2014.01.01 14:42  

Ok, I cleaned up my code a bit.

But I even don't know how to fix the problem, that the EA trails every order with a stoploss. The problem is that I have the variables "lastStopLoss" e.g.

My idea is that I write them into an array and check every order with this method. What can I do better so the program works how I want it?


Best regards

To add comments, please log in or register