Download MetaTrader 5

My first EA Help Please

To add comments, please log in or register
forexwinter
11
forexwinter  

Greetings,

Firstly, Im new to this forum, so hopefully this is the right section to post.

Secondly, I am new to coding and MQL4. I have read the book as well as the reference from MetaEditor and I do get the concepts. I tried to write a very basic EA, of a Moving Average Crossover, for now only opening buy orders. For some reason, when I put it into strategy tester, it doesnt seem to open any orders at all. Can someone please review my code and tell me whats wrong? Thank you.

//--- input parameters
input int      MA_FastPeriod=5;
input int      MA_SlowPeriod=10;
input int      TakeProfit=20;
input int      StopLoss=10;
input double LOTS=0.01; 
double MA1=iMA(Symbol(),0,MA_FastPeriod,0,MODE_SMA,PRICE_CLOSE,0);
double MA2=iMA(Symbol(),0,MA_SlowPeriod,0,MODE_SMA,PRICE_CLOSE,0);
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick() {
   if (MA1>MA2)
    {
      if (OrdersTotal()<1)
        OrderSend (Symbol(), OP_BUY, LOTS,Ask,1,Ask+StopLoss*Point, Ask+TakeProfit*Point, NULL,0, 0,clrNONE);   
    } 
   return;
    }
     
  //+------------------------------------------------------------------+

After compiling, the toolbox says, at line 41, "return value of OrderSend should be checked" ..please, what does this mean? Yes, OrderSend does return an integer value, particularly the ticket number of the order, but why would I want it to be checked? Wont I see the ticket number in my terminal?

Most importantly though, the EA doesnt seem to open any orders at all... whats wrong?

Thank you.

whroeder1
17084
whroeder1  
forexwinter: Firstly, Im new to this forum, so hopefully this is the right section to post.


I tried to write a very basic EA, of a Moving Average Crossover, 

After compiling, the toolbox says, at line 41, "return value of OrderSend should be checked" ..please, what does this mean? Yes, OrderSend does return an integer value, particularly the ticket number of the order, but why would I want it to be checked? Wont I see the ticket number in my terminal?

Most importantly though, the EA doesnt seem to open any orders at all... whats wrong?

Welcome
  1. You post MT4 questions here, the MQL4 section (bottom of the Root page.)
  2. if (MA1>MA2)
    Is not a cross over, just one above the other. Look for a cross.
    double aPrev = ..., aCurr = ...,
           bPrev = ..., bCurr = ...;
    bool   wasUp = aPrev > bPrev,
            isUp = aCurr > bCurr,
           isCross = isUp != wasUp;

  3. OrderSend (Symbol(), OP_BUY, LOTS,Ask,1,Ask+StopLoss*Point, Ask+TakeProfit*Point, NULL,0, 0,clrNONE);
    What if you have a problem and the OrderSend doesn't work? You have no idea why. Check your return codes and find out why. 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
  4. Your SL is 10 points. That is 1 pip on a 5 digit broker, 10 on a 4. You are not adjusting SL, TP, and slippage; for 4/5 digit brokers and for JPY pairs.
    double   pip          = StringFind(_Symbol,"JPY") < 00.010.0001;
    int      pipsToPoints = int(pip / _Point);
    int      pipDigits    = (int)MathLog10(pipsToPoints);
    int      slippage     = 3 * pipsToPoints;
  5. You also need to verify that StopLoss/TakeProfit are larger than STOP_LEVEL Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial
  6. You can use NULL in place of _Symbol only in those calls that the documentation specially says you can. iHigh does, MarketInfo does not. OrderSend does not.

    Don't use NULL (except for pointers where you explicitly check for it.) Use _Symbol and _Period, that is minimalist as possible and more efficient.

    Zero is the same as PERIOD_CURRENT which means _Period.

    No need for a function call with iHigh(NULL,0,s) just use the predefined arrays, i.e. High[]


  7. double MA1=iMA(Symbol(),0,MA_FastPeriod,0,MODE_SMA,PRICE_CLOSE,0);
    double MA2=iMA(Symbol(),0,MA_SlowPeriod,0,MODE_SMA,PRICE_CLOSE,0);
    These variables are not changing. Assign them in OnTick.
forexwinter
11
forexwinter  

Thank you for your response :)

For now, I would just like to get a buy order opened.

For the beggining I will keep it simple with just a plain condition of one over other, so MA1>MA2

Shouldnt my code open a buy order if that condition is true, and there is no other order open at the moment?

I know Im not making some adjustments for 4/5 digit brokers and JPY pairs, for now this will be tested only on EURUSD., with a 5 digit broker. The aim of this EA is not profitability or super-functioning across JPY pairs, I would just like to open a buy order when MA1>MA2, and have no more than 1 order at a time (this is a 1st time coding test EA :) ).

The NULL in my OrderSend is for the "const string comment=NULL" part. Since I dont want any comment, I just kept NULL there, ..is that wrong for some reason?

Therefore, I dont really get what I messed up.. (maybe the MA variables can be under OnTick, yes, or some other details now and there modified).. but I dont get why this doesnt open orders when I think code-wise I have written everything correctly... If you see any syntax errors.. or where does it simply go wrong? (if there would be syntax errors, I guess it wouldnt compile? )


Thank you :)

Osama Shaban
1584
Osama Shaban  
Change this ...
OrderSend(Symbol(), OP_BUY, LOTS,Ask,1,Ask+StopLoss*Point, Ask+TakeProfit*Point, NULL,0, 0,clrNONE); 

To this ...
OrderSend(Symbol(), OP_BUY, LOTS,Ask,10,Bid-StopLoss*Point, Ask+TakeProfit*Point, NULL,0, 0, Blue); 
whroeder1
17084
whroeder1  

forexwinter:

Shouldnt my code open a buy order if that condition is true, and there is no other order open at the moment?

The NULL in my OrderSend is for the "const string comment=NULL" part. Since I dont want any comment, I just kept NULL there, ..is that wrong for some reason?

Therefore, I dont really get what I messed up.. (maybe the MA variables can be under OnTick, yes, or some other details now and there modified).. but I dont get why this doesnt open orders when I think code-wise I have written everything correctly...

If you see any syntax errors.. or where does it simply go wrong? (if there would be syntax errors, I guess it wouldnt compile? )

Previously answered!
  1. The condition may or may not have been true. If you weren't in a up trend when you attached it to the chart it will never open an order. But you don't know why, (because of #1.7 or #1.4 and #1.5,) because you didn't find out (#1.3)
  2. That works in that specific case, but I advise don't use NULL generally. If you don't want a comment, use an empty string ("".)
  3. You don't know why (#1.3)
  4. Compiler catches syntax errors. It does not catch logic errors (#1.2 #1.7) or run time errors. (#1.4 #1.5)
To add comments, please log in or register