Download MetaTrader 5

Need Help In A Moving Average EA

To add comments, please log in or register
ziaalhassan
27
ziaalhassan  

Hi Friends, I Am Not A Mql4 Programmer But I Am Stuck In a Situation. I Have A Simple Moving Average Robot. Only I Want Is That If I Have Placed A Order manually Then My Robot Should Wait For The Order To Close Before It Start Trading.

What Line Of Code I Should Add In My Ea So That It Only Places A New Order After The Order Which i Have Placed manually Is Closed Either By hitting TP/Sl Or Closed By Me Manually.

I Hope I Have Clearly Explained My Question.

I Will Be Very thankful To You All For Your Quick Replies

whroeder1
15934
whroeder1  
  1. Why did you post your MT4 question in the Root / MT5 EA section instead of the MQL4 section, (bottom of the Root page?) General rules and best pratices of the Forum. - General - MQL5 programming forum

  2. It's not a line of code. OrderSelect loop looking for magic number zero and the symbol being the same as the chart's symbol.
    Using OrdersTotal directly and/or no Magic number filtering on your OrderSelect loop means your code is incompatible with every EA (including itself on other charts and manual trading.) Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 and MetaTrader 4 - MQL4 programming forum
ziaalhassan
27
ziaalhassan  

First Of all Very Sorry For Posting In Wrong Section.

As I Told That I Am Not A programmer.

I Am Using MT4's Moving Average Ea

Can You Please Tell me the Code And Where Should I Enter It In EA So That It May Work Exactly I Need.

I Will be Very Thankful To You For This....

whroeder1
15934
whroeder1  
ziaalhassan: As I Told That I Am Not A programmer.
You have only four choices:
We're not going to code it for you (although it could happen if you are lucky or the problem is interesting.) We are willing to help you when you post your attempt (using SRC) and the nature of your problem. No free help
urgent help.


ziaalhassan
27
ziaalhassan  
whroeder1:

You have only four choices:
We're not going to code it for you (although it could happen if you are lucky or the problem is interesting.) We are willing to help you when you post your attempt (using SRC) and the nature of your problem. No free help
urgent help.


Thanks Sir. But As i Told Before I am Not A Programmer And I Need Help From Senior Programmers If Possible.

To Start Learning And Then Writing My Own Code Will Take A Lot Of Time For Me As I have No Experience In Programming.

Please Provide Direct Help If Possible,

Thank You Very Much


Here Is My Code.

extern int     TrailingStop  = 30;
extern int     StopLoss      = 70;
extern int     TakeProfit    = 100;
extern bool    MM            = FALSE;
extern double  Risk          = 1;
extern double  LotManual     = 0.1;
extern int     MovingPeriod  = 12;
extern int     MovingShift   = 0;
extern int     Diffrence     = 10;
extern int     Magic         = 2008;



 void MoveTrailingStop()
{
   int cnt,total=OrdersTotal();
   for(cnt=0;cnt<total;cnt++)
   {
      OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
      if(OrderType()<=OP_SELL&&OrderSymbol()==Symbol())
      {
         if(OrderType()==OP_BUY)
         {
                      if( TrailingStop > 0 ) {                 
               if(Bid - OrderOpenPrice() > Point * TrailingStop) {
                  if(OrderStopLoss() < Bid - Point * TrailingStop) {
                     OrderModify(OrderTicket(), OrderOpenPrice(), Bid - Point * TrailingStop, OrderTakeProfit(), 0, MediumSeaGreen);
                    
                     continue;
                  }
               }
            }
         } else {
    
            //Trailing stop
            if( TrailingStop > 0 ) {                 
               if((OrderOpenPrice() - Ask) > (Point * TrailingStop)) {
                  if((OrderStopLoss() > (Ask + Point * TrailingStop)) || (OrderStopLoss() == 0)) {
                     OrderModify(OrderTicket(), OrderOpenPrice(), Ask + Point * TrailingStop, OrderTakeProfit(), 0, DarkOrange);
                   
                     continue;
                  }
               }
            }
         }
      }
   }}

/****************************************-*/


int start()
{
   double ticket; 
   double SL,TP;
   int i,Total;
     int Dig=MarketInfo(Symbol(),MODE_DIGITS);
   double Ld_16 = 10.0 * Point;
   double Ld_24 = 5.0 * Point;
  
 /*****************************************************************************/
   double ma;
   
   ma=iMA(Symbol(),0,MovingPeriod,MovingShift,MODE_SMA,PRICE_CLOSE,0);
 
/*---------------------------------------------------------------------------*/

  if (MyOrdersTotal(Magic)==0  )
     {
  if ( Bid - ma > Diffrence * Point )
     {
  ticket=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,0,0,"",Magic,0,Green);
     }}
  
        if (MyOrdersTotal(Magic)==0   )
         {
     if (  ma - Ask > Diffrence*Point )
         {
        ticket=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,0,0,"",Magic,0,Red);
         }}

 
  if(TrailingStop>0)MoveTrailingStop();  
  ///////////////////////////////////
     Total=OrdersTotal();
  if(Total>0)
  {
     for(i=Total-1; i>=0; i--) 
     {
        if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)==true) 
        {
           if(OrderSymbol()==Symbol() && OrderType()==OP_SELL && OrderStopLoss()==0 && OrderTakeProfit()==0) 
           {
              if(StopLoss>0)SL=OrderOpenPrice()+StopLoss*Point;else SL=0;
              if(TakeProfit>0)TP=OrderOpenPrice()-TakeProfit*Point;else TP=0;
              OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(SL,Dig),NormalizeDouble(TP,Dig),OrderExpiration(),CLR_NONE);
           }
           if(OrderSymbol()==Symbol() && OrderType()==OP_BUY && OrderStopLoss()==0 && OrderTakeProfit()==0) 
           {
              if(StopLoss>0)SL=OrderOpenPrice()-StopLoss*Point;else SL=0;
              if(TakeProfit>0)TP=OrderOpenPrice()+TakeProfit*Point;else TP=0;
              OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(SL,Dig),NormalizeDouble(TP,Dig),OrderExpiration(),CLR_NONE);
           }
        }
     }
  } 
   
  
 ///////////////////////////////////// 
   return(0);
   }



int CloseOrders(int Magic)
{
  int total  = OrdersTotal();
  
  for (int cnt = total-1 ; cnt >= 0 ; cnt--)
  {
    OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
    if (OrderMagicNumber() == Magic && OrderSymbol()==Symbol())
    {
      if (OrderType()==OP_BUY)
      {
        OrderClose(OrderTicket(),OrderLots(),Bid,3);
      }
      
      if (OrderType()==OP_SELL)
      {
        OrderClose(OrderTicket(),OrderLots(),Ask,3);
      }
    }
  }
///////////////////////////////  

//////////////////////////////  
  return(0);
}

int MyOrdersTotal(int Magic)
{
  int c=0;
  int total  = OrdersTotal();

  for (int cnt = 0 ; cnt < total ; cnt++)
  {
    OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
    if (OrderMagicNumber() == Magic && OrderSymbol()==Symbol())
    {
      c++;
    }
  }
  return(c);
}

double LotsOptimized()
 {
   double margin=MarketInfo(Symbol(), MODE_MARGINREQUIRED);
   double lot=0; 
   if (MM==true){  
   lot=NormalizeDouble((AccountBalance()*Risk/10000), 2) ;}
   else lot=LotManual;
   if (lot>MarketInfo(Symbol(),MODE_MAXLOT)) { lot=MarketInfo(Symbol(),MODE_MAXLOT); }
   if (lot<MarketInfo(Symbol(),MODE_MINLOT)) { lot=MarketInfo(Symbol(),MODE_MINLOT); } 
  
   return (lot);
}


whroeder1
15934
whroeder1  
ziaalhassan:

But As i Told Before I am Not A Programmer And I Need Help From Senior Programmers If Possible.

To Start Learning And Then Writing My Own Code Will Take A Lot Of Time For Me As I have No Experience In Programming.

Please Provide Direct Help If Possible,

  1. What part of "is no common language for us to communicate" was unclear? It's not possible, because you don't speak the language.
  2. What part of "learn to code, or pay someone" was unclear?
  3. What part of "We're not going to code it for you" was unclear? There are no slaves here, it's not going to happen.
  4. As far as your posted code goes.
    In the presence of multiple orders (one EA multiple charts, multiple EAs, manual trading,) because while you are waiting for the current operation (closing, deleting, modifying) to complete, any number of other operations on other orders could have concurrently happened and changed the position indexing:
    1. For non-FIFO (US brokers,) (or the EA only opens one order per symbol,) you can simply count down in a position loop, and you won't miss orders. Get in the habit of always counting down. Loops and Closing or Deleting Orders - MQL4 and MetaTrader 4 - MQL4 programming forum

    2. For FIFO (US brokers,) and you (potentially) process multiple orders, you must count up and on a successful operation, reprocess all positions (set index to -1 before continuing.)
    3. and check OrderSelect. What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
      Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
    4. and if you (potentially) process multiple orders, must call RefreshRates() after server calls if you want to use the Predefined Variables (Bid/Ask) or OrderClosePrice() instead, on the next order/server call.

  5. Code breaks on 4 digit brokers, exotics (e.g. USDZAR where spread is over 500 points,) and metals. Compute what a pip is and use it, not points. How to manage JPY pairs with parameters? - MQL4 and MetaTrader 4 - MQL4 programming forum

  6. Risk depends on your initial stop loss and lot size.
    • You place the stop where it needs to be - where the reason for the trade is no longer valid. E.g. trading a support bounce the stop goes below the support.
    • Account Balance * percent/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerLot + CommissionPerLot) (Note OOP-OSL includes the SPREAD, and DeltaPerLot is usually around $10/pip but it takes account of the exchange rates of the pair vs. your account currency.)
    • Do NOT use TickValue by itself - DeltaPerLot
    • You must normalize lots properly and check against min and max.
    • You must also check FreeMargin to avoid stop out
To add comments, please log in or register