Download MetaTrader 5
To add comments, please log in or register
Your broker does not support mobile MetaTrader 5? Ask him to buy them!
luismiguelgpc
8
luismiguelgpc 2015.05.31 18:07 

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);
 }}
 
whroeder1
13627
whroeder1 2015.05.31 18:44  
  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
    
    
Demos Stogios
184
Demos Stogios 2015.05.31 20:24  

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 

/
To add comments, please log in or register