Help validating an EA

 

Hello,


I have followed step by step a video on how to create an Moving Average crossover EA,

in the video I followed I see that the writer is able to run the back test, when I compile mine I get no errors but

when I run it there are no trades being entered.

Could someone take a look at it and let me know what I did wrong?


Thanks a lot


#property copyright "737ngx"
#property link      "https://www.737ngxsim.com"
#property version   "1.00"
#property strict

extern int TakeProfit=50;
extern int StopLoss=25;
extern int FastMA=15;
extern int FastMaShift=0;
extern int FastMaMethod=1;
extern int FastMaAppliedTo=0;
extern int SlowMA=50;
extern int SlowMaShift=0;
extern int SlowMaMethod=1;
extern int SlowMaAppliedTo=0;
extern double LotSize = 0.01;
extern int MagicNumber = 1234;
double pips; 
 

//+------------------------------------------------------------------+ 
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
  
double ticksize = MarketInfo(Symbol(), MODE_TICKSIZE);
   if (ticksize == 0.00001 || ticksize == 0.001)
   pips = ticksize*10;
   else pips = ticksize;
return(INIT_SUCCEEDED);
  }
  
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//---
   return (0);
   
  }
  
 //+------------------------------------------------------------------+
//| Expert start function                                 |
//+------------------------------------------------------------------+
 
 
  
  int start()
  {
    double PreviousFast = iMA(NULL,0,FastMA,FastMaShift,FastMaMethod,FastMaAppliedTo,2);
    double CurrentFast =  iMA(NULL,0,FastMA,FastMaShift,FastMaMethod,FastMaAppliedTo,1);
      
    double PreviousSlow = iMA(NULL,0,SlowMA,SlowMaShift,SlowMaMethod,SlowMaAppliedTo,2);
    double CurrrentSlow = iMA(NULL,0,SlowMA,SlowMaShift,SlowMaMethod,SlowMaAppliedTo,1);
    
    if(PreviousFast<PreviousSlow && CurrentFast>CurrrentSlow)
         if(OrdersTotal()==0)
            OrderSend(Symbol(),OP_BUY,LotSize,Ask,3,Ask-(StopLoss*pips),Ask+(TakeProfit*pips),NULL,MagicNumber,0,Green);
         
    if(PreviousFast>PreviousSlow && CurrentFast<CurrrentSlow)
         if(OrdersTotal()==0) 
            OrderSend(Symbol(),OP_SELL,LotSize,Bid,3,Bid+(StopLoss*pips),Bid-(TakeProfit*pips),NULL,MagicNumber,0,Red);
               
   return(0); 
  
  }
  
  
  
  
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   
  }
//+------------------------------------------------------------------+
 
 
Move your code into OnTick instead of start()
 
  1. Why did you post your MT4 question in the Root / MT5 General section instead of the MQL4 section, (bottom of the Root page?)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
    Next time post in the correct place. The moderators will likely move this thread there soon.

  2. Start using the new Event Handling Functions. You can not use both.
              Event Handling Functions - Functions - Language Basics - MQL4 Reference

  3. Check your return codes for errors, report them and you would know why. Don't just silence the compiler, it is trying to help you.
              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
    Only those functions that return a value (e.g. iClose, MarketInfo, etc.) must you call ResetLastError before in order to check after.

  4. You buy at the Ask and sell at the Bid.
    • Your buy order's TP/SL are triggered when the Bid reaches it. Not the Ask.
    • Your sell order's TP/SL will be triggered when the Ask reaches it. To trigger at a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 and MetaTrader 4 - MQL4 programming forum - Page 3
    • The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools -> Options {control-O} -> charts -> Show ask line.)

  5. double ticksize = MarketInfo(Symbol(), MODE_TICKSIZE);
    Don't try to use any price or server related functions in OnInit (or on load,) as there may be no connection/chart yet:
    1. Terminal starts.
    2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
    3. OnInit is called.
    4. For indicators OnCalculate is called with any existing history.
    5. Human may have to enter password, connection to server begins.
    6. New history is received, OnCalculate called again.
    7. New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.

 

You'd better start with an existing EA from the standard packet (see MQL5\Experts\) or from the CodeBase.

And then change one of them according to your ideas!!

 

First of all, I am very new to this so let me apologize for posting in the wrong place. I will be more careful next time.


@Mohamad, your suggestion worked, now the tester works, Thank You.


@whroeder1, Your point 4 I am not understanding it, due to me being new surely, I can I change my code to fix the issue?


Like I said, I took this code from the internet, while is close to what I like, I would like the EA to do something else,

looks like that when an order is opened, no new orders will open until the previous is closed... I think.

What I would like it to do is the following:

If order is open due to cross over, but the price turns and goes fast the other way, creating a new cross over,

I need for the EA to close the existing order and open a new one, the original order should not stay open until it reached the sl.


How can I change the code to do this?


Much appreciated

Nic

 

Is there an existing EA that does what I ask just above?


If not would it be expensive to have someone develop it for me on this site?


Since we are here, can someone point me to a link where I can learn coding it myself, but a well organized course that will take me from the very basics to the more complex things.

This will be a long learning curve for me.


Thanks

Nic

 
Learn coding yourself: https://www.mql5.com/en/docs
MQL5 Reference - How to use algorithmic/automated trading language for MetaTrader 5
MQL5 Reference - How to use algorithmic/automated trading language for MetaTrader 5
  • www.mql5.com
MetaQuotes Language 5 (MQL5) is a high-level language designed for developing technical indicators, trading robots and utility applications, which automate financial trading. MQL5 has been developed by MetaQuotes Software Corp. for their trading platform. The language syntax is very close to C++ enabling programmers to develop applications in...
 
Or order your EA from a developer: https://www.mql5.com/en/job
Trading applications for MetaTrader 5 to order
Trading applications for MetaTrader 5 to order
  • www.mql5.com
Hi, Im wondering what an EA based on a custom indicator would cost, the custom indicator shows buy/sell signals with green/red arrows on the chart.(will supply indicator later) I would also like to have an hedge option. Would like to have it work on different tf and currency pairs. Also possibility to set TP, SL, lot size, choose how many...
 
737ngx:

@whroeder1, Your point 4 I am not understanding it, due to me being new surely, I can I change my code to fix the issue?

How can I change the code to do this?

  1. What part of " buy order's TP/SL are triggered when the Bid reaches it. Not the Ask. " was unclear? Relative to which line did you place them. Look at your code, you understand the issue, you fix your code.
    1. learn to code it. If you don't learn MQL4/5, there is no common language for us to communicate. If we tell you what you need, you can't code it. If we give you the code, you don't know how to integrate it into yours.
    2. or pay (Freelance) someone to code it.
    We're not going to code it for you. We are willing to help you when you post your attempt (using CODE button) and state the nature of your problem.
              No free help
              urgent help.
Reason: