Download MetaTrader 5
To add comments, please log in or register
Are you used to trading from your smartphone? Mobile version of MetaTrader 5 will suit you best!
Opengates
165
Opengates 2015.03.13 21:21 

Dear Everybody in the house,

Please I need your assistance over this issue: I created an ea to open multiple orders (3 orders) simultaneously with the same stoploss point for all but different takeprofit for each, when the price gets to certain points it is expected of it to modify the stop loss to breakeven and then after a few points, to start trailing the price thereafter until the price reverses to hit the trailing loss. Actually, it is opening all the orders quite alright, it is also preventing anymore orders from opening until the existing orders were closed but the only problem now is that it is not modifying the orders as supposed at all. Please help me look at the codes for adjustments, it is posted below:

//------------------------------------------------------------------
extern double        TakeProfit1 = 600;   // Take Profit for Order 1
extern double        TakeProfit2 = 1000;  // Take Profit for Order 2
extern double        TakeProfit3 = 50000; // Take Profit for Order 3
extern double        StopLoss   = 300;    // Stop Loss
extern double        BrkEvn     = 300;
extern double        TrailPoint = 350;    // Trailing Stop
extern double        Lot        = 0.1;    // Lot size
extern int             MA1        = 1;      // Period of MA1
extern int             MA2        = 5;      // Period of MA2
//----------------------------------------------------------------
 string Text,Symb; 
 int  
 Total,                           // Amount of orders in a window   
 Tip=-1,                          // Type of selected order (B=0,S=1)  
 Ticket,
 Ticket1,
 Ticket2,
 Ticket3,                          // Order number 
 Error;
 double
 MA_1,                             
 MA_2,
 Main,
 Signal,
 ADXPlus,
 ADXMinus,
 bid,
 ask,                             
 lot,                             // Amount of lots in a selected order  
 Price,
 SL,                              // SL of a selected order
 OrderP,Order1,
 Order2,
 Order3,
 TP,
 TP1,
 TP2,
 TP3;                              // TP за a selected order  
 bool
 Res,
 Opn_B=false,                     // Criterion for opening Buy  
 Opn_S=false;                     // Criterion for opening Sell
//---------------------------------------------------------------
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
 int init()
  {
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
 int deinit()
  {
   return(0);
  }
//+------------------------------------------------------------------+
//We will launch the program here to start execution                 |
//+------------------------------------------------------------------+
 int start()
 {
 //-----------------------------------------------------------------
 if(AccountNumber()!=135003681)   // 210245-----65061514-----135003680----863175-----135002070------
 {
 Alert("Unauthorized User! Order for your own copy @ www.fxdominion.com/245dea.php today!",GetLastError());
 return(0);
 }
 //======================================================================
  //----------------------------------------------------------------------
 MA_1=iMA(NULL,0,MA1,0,MODE_SMA,PRICE_CLOSE,0); // МА_1  
 MA_2=iMA(NULL,0,MA2,0,MODE_SMA,PRICE_CLOSE,0); // МА_2
 Main=iStochastic(NULL,0,14,7,3,MODE_SMA,0,MODE_MAIN,0);// 0 bar  
 Signal=iStochastic(NULL,0,14,7,3,MODE_SMA,0,MODE_SIGNAL,0);// 0 bar
 ADXPlus=iADX(NULL,0,14,PRICE_CLOSE,MODE_PLUSDI,0);
 ADXMinus=iADX(NULL,0,14,PRICE_CLOSE,MODE_MINUSDI,0);
 
 if(MA_1 > MA_2 && Main > Signal && ADXPlus > ADXMinus) // If difference between    
 {                                          // ..MA 1 and 2 is large     
 Opn_B=true;       
 }  
        
//==================================================================

 if(MA_1 < MA_2 && Main < Signal && ADXPlus < ADXMinus) // If difference between   
 {                                          // ..MA 1 and 2 is large     
 Opn_S=true;
 }
//--------------------------------------------------------------------
//Condition to stop further opening of more orders
 Symb=Symbol();
 bool liveorder=false; //didn't find any existing order
 for(int i=1; i<=OrdersTotal(); i++) {
      if (OrderSelect(i-1,SELECT_BY_POS)==true) {
      if (OrderSymbol()==Symb) {
         Tip=OrderType();
         Ticket=OrderTicket();
         Lot=OrderLots();
         SL=OrderStopLoss();
         TP=OrderTakeProfit();
         liveorder=true;
         Print("One live order is still running! No more order opening!");
         break;
      } else {
         liveorder=false;
      }
   }
}
//Opening Orders
 
if(liveorder==false)
   {
   //=============== Open Buy order here ========================
 if(Opn_B==true)
  {
 
    RefreshRates();                        // Refresh rates
    bid = NormalizeDouble(MarketInfo(Symb,MODE_BID),Digits); // Request for the value of Bid     
    SL=bid-StopLoss*Point;     // Calculating SL of opened    
    TP1=bid+TakeProfit1*Point;   // Calculating TP of opened
    TP2=bid+TakeProfit2*Point;   // Calculating TP of opened
    TP3=bid+TakeProfit3*Point;   // Calculating TP of opened 
    Ticket1=OrderSend(Symb,OP_BUY,Lot,Ask,2,SL,TP1); //Opening Buy
    Ticket2=OrderSend(Symb,OP_BUY,Lot,Ask,2,SL,TP2); //Opening Buy
    Ticket3=OrderSend(Symb,OP_BUY,Lot,Ask,2,SL,TP3); //Opening Buy     
     if (Ticket1>0 && Ticket2>0 && Ticket3>0) // Success      
    {       
    Print("All the Orders for BUYs were opened ",Ticket);       
    return(0);         // Exit start()     
     }    
    if (Error==1)      // Processing errors      
    return(1);                                // Exit start()    
   }    
  
   //======= Open Sell order here =======================================
 if (Opn_S==true)
  {
  
    RefreshRates();                        // Refresh rates
    ask = NormalizeDouble(MarketInfo(Symb,MODE_ASK),Digits); // Request for the value of Ask   
    SL=ask+StopLoss*Point;     // Calculating SL of opened    
    TP1=ask-TakeProfit1*Point;   // Calculating TP of opened
    TP2=ask-TakeProfit2*Point;   // Calculating TP of opened
    TP3=ask-TakeProfit3*Point;   // Calculating TP of opened
    Ticket1=OrderSend(Symb,OP_SELL,lot,Ask,2,SL,TP1); //Opening Sell     
    Ticket2=OrderSend(Symb,OP_SELL,lot,Ask,2,SL,TP2); //Opening Sell
    Ticket3=OrderSend(Symb,OP_SELL,lot,Ask,2,SL,TP3); //Opening Sell
   if (Ticket1>0)
    {     
    Print("The Order 1 for SELL was opened ",Ticket);
    Order1=Ticket1;       
    return(0);                             // Exit start()     
     }  
if (Ticket2>0)
    {     
    Print("The Order 2 for SELL was opened ",Ticket);
    Order2=Ticket2;       
    return(0);                             // Exit start()     
     }  
  if (Ticket3>0)
    {     
    Print("The Order 3 for SELL was opened ",Ticket);
    Order3=Ticket3;       
    return(0);                             // Exit start()     
     }  
   if (Error==1)      // Processing errors     
    return(1);                                // Exit start()    
     }     
   }

//--------We will then modify our open orders accordingly -----------------------
 for(int i=1; i<=OrdersTotal(); i++)          // Cycle searching in orders    
 {     
 if (OrderSelect(i-1,SELECT_BY_POS)==true)              
 {    
 double Profit=OrderTakeProfit();
        Tip=OrderType();
        SL=OrderStopLoss();
        Order1=Ticket1;
        Order2=Ticket2;
        Order3=Ticket3;
        Symb=Symbol();
 if (OrderSymbol()!=Symb)continue;
//-----  MoveStopLoss Strategies  -------- 
      while(true)                            //
           {
            double TS=TrailPoint;                //
            double Min_Dist=MarketInfo(Symb,MODE_STOPLEVEL); //
            if (TS<Min_Dist)                    //
               TS=Min_Dist;                     //
            //---------------------------------------------------
            bool Modify=false;                  //
            switch(Tip)                         //
              {
                case 0 :  //  Buy ==
              OrderP=OrderOpenPrice()+NormalizeDouble(MarketInfo(Symbol(),MODE_SPREAD),Digits);
              if (NormalizeDouble(OrderP,Digits)< NormalizeDouble(Bid-BrkEvn*Point,Digits))
                    {
                     SL=OrderP; //  Modify this to breakeven+spread
                    } else {
              if (NormalizeDouble(OrderP,Digits)< NormalizeDouble(Bid-TS*Point,Digits))
              {
                     SL=Bid-TS*Point;    // Set this to start trailing the price
                     Text="Buy ";        // Buy
                     Modify=true;               //
                    }
              }
                                          // switch

               case 1 :                         // Sell
             OrderP=OrderOpenPrice()-NormalizeDouble(MarketInfo(Symbol(),MODE_SPREAD),Digits);
             if (NormalizeDouble(OrderP,Digits)> NormalizeDouble(Ask+BrkEvn*Point,Digits) || NormalizeDouble(OrderP,Digits)==0) //
                    {
                     SL=OrderP;
                    } else {
              if (NormalizeDouble(OrderP,Digits)> NormalizeDouble(Ask+TS*Point,Digits)|| NormalizeDouble(OrderP,Digits)==0) //
              {
                     SL=Ask+TS*Point;
                     Text="Sell ";              // Sell
                     Modify=true;               //
                    }
              }
                    }

               if (Modify==false)               //
               break;                           //  while

  //----------------------------------------------------
                   TP    =OrderTakeProfit();    // TP
                   Price =OrderOpenPrice();     //
                   Ticket=OrderTicket();        //
  //========================================================================
               if(Order1>0)  {
               Res1=OrderModify(Ticket,Price,SL,TP,0); 
               Print("Order1 ",Text,Ticket," was modified!"); break; } //
  //-------------------------------------------------------------------------
               if(Order2>0)  { 
               Res2=OrderModify(Ticket,Price,SL,TP,0); 
               Print("Order2 ",Text,Ticket," was modified!"); break; } //
  //-------------------------------------------------------------------------
               if(Order3>0)  {
               Res3=OrderModify(Ticket,Price,SL,TP,0); 
               Print("Order3 ",Text,Ticket," was modified!"); break; } //
  //-------------------------------------------------------------------------
            Error=GetLastError();           //
            switch(Error)                       //
              {
              case 130:Print("Wrong stops. Retrying.");
                  RefreshRates();               //
                  continue;                     //
              case 135:Print("The price has changed. Retrying..");           
                  RefreshRates();                     // Update data           
                  return(true);                         // At the next iteration              
              case 136:Print("No prices. Waiting for a new tick..");           
              while(RefreshRates()==false)        // Up to a new tick              
                   Sleep(1);                        // Cycle delay   
                  continue;                     //
               case 146:Print("Trading subsystem is busy. Retrying ");
                  Sleep(500);                   //
                  RefreshRates();               //
                  continue;                     //
                  //
               case 2 : Print("Common error.");
                  break;                        //
               case 5 : Print("Old version of the client terminal.");
                  break;                        //
               case 64: Print("Account is blocked.");
                  break;                        //
               case 133:Print("Trading is prohibited");
                  break;                        //
               default: Print("Occurred error ",Error); //Other errors
              }
               Profit=OrderProfit();
               if(Profit>0 && Order1>0)
               {
               Print("The profit for Order 1 ",Text," ",Ticket," has reached ",Profit," now!");
               break;
               }
               if(Profit>0 && Order2>0)
               {
               Print("The profit for Order 2 ",Text," ",Ticket," has reached ",Profit," now!");
               break;
               }
               if(Profit>0 && Order3>0)
               {
               Print("The profit for Order 3 ",Text," ",Ticket," has reached ",Profit," now!");
               break;
               }
                   }
                      }  }
            return(0);
             }
//-----------------------------------------------------------------------------------------------

Thank you while waiting for your assistance

whroeder1
13593
whroeder1 2015.03.14 01:13  
  1. Don't paste code
    Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. price gets to certain points it is expected of it to modify the stop loss to breakeven and then after a few points, to start trailing the price thereafter until the price reverses to hit the trailing loss. Actually, it is opening all the orders quite alright, it is also preventing anymore orders from opening until the existing orders were closed but the only problem now is that it is not modifying the orders as supposed at all. Please help me look at the codes for adjustments, it is posted below:
    Perhaps I'm too drunk to read that. All I see is a run on sentence.
  3.  for(int i=1; i<=OrdersTotal(); i++){         // Cycle searching in orders    
     if (OrderSelect(i-1,SELECT_BY_POS)==true)       
    When i == OrdersTotal, the OrderSelect will never be true/ You must count down when closing/deleting in a position loop. Get in the habit of always counting down. Loops and Closing or Deleting Orders - MQL4 forum You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So Don't write if(bool == true), just use if(bool) or if(!bool). Code becomes self documenting when you use meaningful variable names, like bool isLongEnabled. Long_Entry sounds like a trigger price or a ticket number and "if long entry" is an incomplete sentence.
  4. NormalizeDouble(MarketInfo(Symbol(),MODE_SPREAD),Digits);
    MI(spread) is the spread in the number of points. to digits of 5 is 23.00000. Bogus. Do NOT use NormalizeDouble, EVER. For ANY Reason. It's a kludge, don't use it. It's use is always wrong
  5.               if(Order1>0)  {
                   Res1=OrderModify(Ticket,Price,SL,TP,0); 
                   Print("Order1 ",Text,Ticket," was modified!"); break; } //
      //-------------------------------------------------------------------------
                   if(Order2>0)  { 
                   Res2=OrderModify(Ticket,Price,SL,TP,0); 
                   Print("Order2 ",Text,Ticket," was modified!"); break; } //
      //-------------------------------------------------------------------------
                   if(Order3>0)  {
                   Res3=OrderModify(Ticket,Price,SL,TP,0); 
                   Print("Order3 ",Text,Ticket," was modified!"); break; } //
  6. Why are you modifying the same ticket three times. And why aren't you checking your return codes (OrderModify) What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles If Res1 is false, you've lost the reason if you call Res2....
  7.             Error=GetLastError();           //
                switch(Error)                       //
    
    Do NOT call GLE unless you have already detected an error.
Opengates
165
Opengates 2015.03.20 13:15  


Hi WHRoeder,



Thanks so much, I did all you asked me to do and everything is working fine now.


I am indeed very grateful.





 

Thanks a lot

Cristiano Konrad
247
Cristiano Konrad 2015.05.12 19:25  
.
/
To add comments, please log in or register