Help with EA

 

Hi everyone

When I compiled a get this error: "if" - expressions are not allowed on a global scope, on lines 42 and 50.

Thantks   


//+------------------------------------------------------------------+
//|                                                 trendcatcher.mq4 |
//|                        Copyright 2015, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+

//+ Trend catcher

// Se declaran las variables
extern int TakeProfit = 60;
extern int StopLoss = 20;
extern int FastMA = 5;
extern int SlowMA = 10;
extern int RSI = 10;
extern float LotSize = 0.01f;

int OnInit()
{

return(INIT_SUCCEEDED);
}

void OnDeinit(const int reason)
{

}

void OnTick()
{

double PreviousSloMA = iMA (NULL, 0, SlowMA, 0,MODE_EMA,PRICE_CLOSE, 2);
double CurrentSlowMA = iMA (NULL, 0, SlowMA, 0, MODE_EMA,PRICE_CLOSE, 1);
double PreviousFastMA = iMA (NULL, 0, FastMA, 0, MODE_EMA,PRICE_CLOSE, 2);
double CurrentFastMA = iMA (NULL, 0, FastMA, 0, MODE_EMA, PRICE_CLOSE, 1);
double PreviousRSI = iRSI (NULL, 0, RSI, PRICE_MEDIAN, 2);
double CurrentRSI = iRSI (NULL, 0, RSI, PRICE_MEDIAN, 1);

}    

//orden de entrada compra

if (PreviousFastMA < PreviousSlowMA && CurrentFastMA > CurrentSlowMA)      //line 42
{
if(PreviousRSI < 50.0 && CurrentRSI > 50.0)
{
OrderSend (Symbol(), OP_BUY, LotSize, Ask, 5, Ask-(StopLoss*Point), Ask + (TakeProfit*Point), NULL);
}}
 
//orden de entrada venta
 if(PreviousFastMA > PreviousSlowMA && CurrentFastMA < CurrentSLowMA)   //line 50
 {
 if(PreviousRSI > 50.0 && CurrentRSI < 50.00)
 {
 OrderSend (Symbol(), OP_SELL, LotSize, Bid, 5, Bid + (StopLoss*Point), Bid-(TakeProfit*Point), NULL);
 }}
 
 
  1. Don't paste code
    Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. }                         <<< This terminates OnTick()
    
    //orden de entrada compra <<< Here down are expressions on a global scope
    
    
 

Hello,

some things to have in mind

1. You should read the MQL reference manual, section "MQL programs"

 2. IF statements (and WHILE and similar) , in general all your logic has to be inside a function - be it OnTick(), OnTimer() or your own  aFunction();

3. After reading the manual, you will normally realise that OnTick() is running/executed on every single tick that comes from the market. That means also that all the variables you have declared

   inside there will be declared each and every time OnTick is running (it can be several times per second). Of course, this is not something you would likely want, so I suggest moving the declarations 

   outside of all functions (contrary to the program logic, variable and function declarations can happen outside of every function), in company with your extern ones, like

       double PreviousSloMA;

and then put inside OnTick() only the part

       PreviousSloMA = iMA (NULL, 0, SlowMA, 0,MODE_EMA,PRICE_CLOSE, 2); 

 

best regards 

Reason: