Download MetaTrader 5
To add comments, please log in or register
Publish your article. We will translate it into other languages!
Lawrencema0829
56
Lawrencema0829 2016.10.19 03:30 

Hello everyone,

I have written the code for Trailing Stop based on Pending Orders. Could anyone please have a look on it? it has warning but allowing execution. I have no idea does it work or not. And the Trailing Stop sets at 30, does it mean 30 pips or 30 fractional pips? 

Thank you very much if someone could rewrite it. 

P.S.: the purpose of the code below is setting Buy Stop & Sell Stop with Trailing Stop  

Lawrence

//+------------------------------------------------------------------+
//|                                      TestingTrailing+Pending.mq4 |
//|                        Copyright 2016, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+

#property copyright "Copyright 2016, WidiPramana."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

extern string  Name_EA                 = "PendingOrder";
extern int     TP                      = 100;    
extern int     SL                      = 100;      
extern double  Lots                    = 0.01;
extern int     Distance                = 10;      
extern int     Magic                   = 69;
extern double  TrailStop               = 30;      

int      digit=0;
double slb,tpb,sls,tps,pt;
int res,wt,wk,tiket,ticet;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   if(Digits==3 || Digits==5) pt=10*Point;   else   pt=Point;
//----
   return(0);
  }
  
  
  // ---- Trailing Stops
void TrailStops()
{        
    int total=OrdersTotal();
    for (int cnt=0;cnt<total;cnt++)
    {
     OrderSelect(cnt, SELECT_BY_POS);  
     int mode=OrderType();    
        if ( OrderSymbol()==Symbol() )
        {
            if ( mode==OP_BUY )
            {
               if ( Bid-OrderOpenPrice()>Point*TrailStop )
               {
               double BuyStop = OrderOpenPrice();
               OrderModify(OrderTicket(),OrderOpenPrice(),
                           NormalizeDouble(BuyStop, digit),
                           OrderTakeProfit(),0,LightGreen);
                              }
                           }
            if ( mode==OP_SELL )
            {
               if ( OrderOpenPrice()-Ask>Point*TrailStop )
               {
               double SellStop = OrderOpenPrice();
               OrderModify(OrderTicket(),OrderOpenPrice(),
                                  NormalizeDouble(SellStop, digit),
                                  OrderTakeProfit(),0,Yellow);      
               }    
            }
         }  
      }
}

// ---- Scan Trades
int ScanTrades()
{  
   int total = OrdersTotal();
   int numords = 0;
      
   for(int cnt=0; cnt<total; cnt++)
   {        
   OrderSelect(cnt, SELECT_BY_POS);            
   if(OrderSymbol() == Symbol() && OrderType()<=OP_SELL)
   numords++;
   }
   return(numords);
}


//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
label();

   digit  = MarketInfo(Symbol(),MODE_DIGITS);

   if(Hour_trade()==1){
      if(totalorder(4)==0){res=OrderSend(Symbol(), OP_BUYSTOP,NR(Lots) , Ask+Distance*Point, 3, Ask+Distance*Point-SL*Point,Ask+Distance*Point+TP*Point, "", Magic, 0, Blue);}
      if(totalorder(5)==0){res=OrderSend(Symbol(), OP_SELLSTOP,NR(Lots) , Bid-Distance*Point, 3, Bid-Distance*Point+SL*Point,Bid-Distance*Point-TP*Point, "", Magic, 0, Red);}
      if (ScanTrades()<1) return(0);
      else
      if (TrailStop>0) TrailStops();
           }
   return(0);
  }
//+------------------------------------------------------------------+

int Hour_trade()
{
   bool trade = true;
   return (trade);
}

int totalorder( int tipe)
{
int total=0;
for(int i=0; i<OrdersTotal(); i++)
  {
      if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;
      if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=Magic || OrderType()!=tipe) continue;
     total++;
  }

return(total);
}
double NR(double thelot)
{
    double maxlots = MarketInfo(Symbol(), MODE_MAXLOT),
    minilot = MarketInfo(Symbol(), MODE_MINLOT),
    lstep = MarketInfo(Symbol(), MODE_LOTSTEP);
    double lots = lstep * NormalizeDouble(thelot / lstep, 0);
    lots = MathMax(MathMin(maxlots, lots), minilot);
    return (lots);
}

void label()
{
Comment("\n ",
   "\n ",
   "\n ------------------------------------------------",
   "\n :: Pending+Order",
   "\n ------------------------------------------------",
   "\n :: Spread                 : ", MarketInfo(Symbol(), MODE_SPREAD),
   "\n :: Leverage               : 1 : ", AccountLeverage(),
   "\n :: Equity                 : ", AccountEquity(),
   "\n :: Hour Server             :", Hour(), ":", Minute(),
   "\n ------------------------------------------------");
}


 

Biantoro Kunarto
14803
Biantoro Kunarto 2016.10.19 04:29  
Please check this EA, I already fix the warning.
Files:
Check_EA.mq4 6 kb
Lawrencema0829
56
Lawrencema0829 2016.10.19 08:28  
Biantoro Kunarto:
Please check this EA, I already fix the warning.

Hi Biantoro Kunarto,

 Thank you for your fixing the EA, I am going to test it later. Could you please tell me the Trailing Stop sets at 30, does it mean 30 pips or 30 fractional pips?  And does the Trailing Stop have any signal showing when trading?

 Thank you once again for your kindly help!

 Look forward to your reply.

Lawrence 

Jose Francisco Casado Fernandez
2414
Jose Francisco Casado Fernandez 2016.10.19 08:51  
30 points (or fractional pips like you call them). If you want pips, you should replace 'Point', at all sites where it appears, by 'pt' variable. Regards.
Biantoro Kunarto
14803
Biantoro Kunarto 2016.10.19 10:11  
Lawrencema0829:

Hi Biantoro Kunarto,

 Thank you for your fixing the EA, I am going to test it later. Could you please tell me the Trailing Stop sets at 30, does it mean 30 pips or 30 fractional pips?  And does the Trailing Stop have any signal showing when trading?

 Thank you once again for your kindly help!

 Look forward to your reply.

Lawrence 

Trailing stop, Stop loss & Take profit are in points.

I don't understand about "does the Trailing Stop have any signal showing when trading?", what do you mean ?

Biantoro Kunarto
14803
Biantoro Kunarto 2016.10.19 10:15  
If you want to change to pips, just like  Jose Francisco Casado Fernandez  said : you must change the Point with pt . Please check below EA, I already change it too
Files:
Lawrencema0829
56
Lawrencema0829 2016.10.19 11:30  
Biantoro Kunarto:

Trailing stop, Stop loss & Take profit are in points.

I don't understand about "does the Trailing Stop have any signal showing when trading?", what do you mean ?

Hi Biantoro, 

Buy Stop and Sell Stop are green line, SL and TP are red lines, I am wondering does Trailing Stop has a color line to show up when trading?

Unifying Trailing Stop, SL and TP in points is good, I just couldn't see the parameter of Trailing Stop under the Trade Panel.

Lawrence 

Biantoro Kunarto
14803
Biantoro Kunarto 2016.10.20 09:15  
Lawrencema0829:

Hi Biantoro, 

Buy Stop and Sell Stop are green line, SL and TP are red lines, I am wondering does Trailing Stop has a color line to show up when trading?

Unifying Trailing Stop, SL and TP in points is good, I just couldn't see the parameter of Trailing Stop under the Trade Panel.

Lawrence 

For trailing stop there is no color, but you can see the SL lines (red line )

About the trailing stop, you can see the setting at this picture :


Lawrencema0829
56
Lawrencema0829 2016.10.20 09:40  
Biantoro Kunarto:

For trailing stop there is no color, but you can see the SL lines (red line )

About the trailing stop, you can see the setting at this picture :


Thank you! it works well!
/
To add comments, please log in or register