Buy and Sell EA when line crosses

 

I'm trying to test my EA with a MA line. If it crosses to the upside it's a buy and if it crosses to the downside it's a sell.

I'm testing if this works before going on by letting it comment buy or sell. It's not working so far.

extern int     Price          = 0;  //Apply to Price(0-Close;1-Open;2-High;3-Low;4-Median price;5-Typical price;6-Weighted Close) 
extern int     Length         = 52;  //Period of NonLagMA
extern int     Displace       = 0;  //DispLace or Shift 
extern double  PctFilter      = 0;  //Dynamic filter in decimal
extern int     Color          = 1;  //Switch of Color mode (1-color)  
extern int     ColorBarBack   = 1;  //Bar back for color mode
extern double  Deviation      = 0;  //Up/down deviation        
extern int     AlertMode      = 0;  //Sound Alert switch (0-off,1-on) 
extern int     WarningMode    = 0;  //Sound Warning switch(0-off,1-on)
extern int Shift = 1;

double macurrent = iCustom(NULL, 0, "NonLagMA_v7.1",Price,Length,Displace, PctFilter, Color, ColorBarBack, Deviation, AlertMode, WarningMode, 0,Shift);
double maprevious = iCustom(NULL, 0, "NonLagMA_v7.1",Price,Length,Displace, PctFilter, Color, ColorBarBack, Deviation, AlertMode, WarningMode, 0,Shift+1);

double close_current = iClose (NULL,0,Shift);
double close_previous = iClose (NULL,0,Shift+1);



//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
     if (close_current <= macurrent && close_previous >= maprevious){
         Comment("SELL");
         }
     if (close_current >= macurrent && close_previous <= maprevious){
         Comment("BUY");
         }
     else Comment("ERROR");
     


     
     return(INIT_SUCCEEDED);
   }

When using strategy tester, it's only giving me the comment ERROR. Hope someone can help or push me in the right direction. 

 
double macurrent = iCustom(NULL, 0, "NonLagMA_v7.1",Price,Length,Displace, PctFilter, Color, ColorBarBack, Deviation, AlertMode, WarningMode, 0,Shift);
double maprevious = iCustom(NULL, 0, "NonLagMA_v7.1",Price,Length,Displace, PctFilter, Color, ColorBarBack, Deviation, AlertMode, WarningMode, 0,Shift+1);

double close_current = iClose (NULL,0,Shift);
double close_previous = iClose (NULL,0,Shift+1);
Those are not assignments; it's initialization of a common (globally declared,) or static variable with a constant. They work exactly the same way in MT4/MT5/C/C++.
  1. They are initialized once on program load.

  2. They don't update unless you assign to them.

  3. In C/C++ you can only initialize them with constants, and they default to zero. In MTx you should only initialize them with constants. There is no default in MT5, or MT4 with strict (which you should always use.)

    MT4/MT5 actually compiles with non-constants, but the order that they are initialized is unspecified and 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.

  4. Unlike indicators, EAs are not reloaded on chart change so you must reinitialize them, if necessary.
              external static variable - MQL4 programming forum
 

@William Roeder 

Thanks for the info. I see that it's basic logic I'm lacking. 

 

I am also working to the same kind of EA. Have you got solution for that? If, will  then you provide the code.

Thanks

 
You have described in good way that can be easily understand. You can check my code, as by that EA is buying and selling but only on some instances. I require some more improvement so that it can take position on each cross over.
 
Please I am coding an EA that buys and sell as the moving average crosses... I use the EA on different markets like (synthetic indices e.g Boom 500 and Crash 500) but when one position is opened in a market another one does not open ... I want it to run as ... It should open only one position for each market every time the crossing happens even if one position is opened in crash 500 .. It should still open for a crossing on boom 500... Please help me and share the code here 
 
Kelvinprinz:

Do not double/triple post!

I have deleted your duplicated posts!

 
Keith Watford #:

Do not double/triple post!

I have deleted your duplicated posts!

Can someone help me how to unmute notifications on communities and terminals pls
Reason: