Quick Help for a new MQL4 user

 

Hello,

I just started learning about MQL4 and have written my first program. However, I am have a hard time trouble shooting a few of the errors that I encountered when I complied my EA.

Below is the code that I have written. The strategy is simple:

OpenLong: If 10EMA >20MEA, %K>%D, ADX >25, and there is not already an open long position, then OpenLong

CloseLong: If there is an open long trade, 5EMA< 20EMA then CloseLong. Or, if there is an open long trade and ADX < 25, then CloseLong.

OpenShort: If 10EMA <20MEA, %K<%D, ADX >25, and there is not already an open short position, then OpenShort

CloseShort: If there is an open short trade, 5EMA< 20EMA then CloseShort. Or, if there is an open short trade and ADX < 25, then CloseShort.

Here is the code:

#property copyright "Zachary Hadaway"
#property link "https://www.metaquotes.net//"

//ExternalVariables
extern double LotSize = 0.5;
extern int StopLoss = 20;
extern int Slippage = 5;
extern int MagicNumber = 123;

//ADXExternalVariables
extern int ADXPeriod = 14;

//EMAExternalVariables
extern int FastEMAPeriod = 10;
extern int SlowEMAPeriod = 20;
extern int CloseEMAPeriod = 5;

//StochasticOsillatorExternalVariables
extern int KPeriod = 14;
extern int DPeriod = 3;
extern int SlowingPeriod = 3;

//GlobalVariables
int BuyTicket;
int SellTicket;
double UsePoint;
double UseSlippage;

//InitFuntion
int init()
{
UsePoint = PipPoint(Symbol());
UseSlippage = GetSlippage(Symbol(),Slippage);
}

//StartFunction
int start()
{
//CallADX
double ADX = iADX(NULL,0,ADXPeriod,MODE_CLOSE,MODE_MAIN,0);

//CallFast,Slow,andCloseEMAs
double FastEMA = iMA(NULL,0,FastEMAPeriod,0,MODE_EMA,MODE_CLOSE,0);
double SlowEMA = iMA(NULL,0,SlowEMAPeriod,0,MODE_EMA,MODE_CLOSE,0);
double CloseEMA = iMA(NULL,0,CloseEMAPeriod,0,MODE_EMA,MODE_CLOSE,0);

//CallStochasticOsillator
double KStochastic = iStochastic(NULL,0,KPeriod,DPeriod,SlowingPeriod,MODE_SMA,0,MODE_MAIN,0);
double DStochastic = iStochastic(NULL,0,KPeriod,DPeriod,SlowingPeriod,MODE_SMA,0,MODE_SIGNAL,0);

//OpenLong
if(ADX >= 25 && FastEMA > SlowEMA && KStochastic > DStochastic && BuyTicket == 0)
{
double OpenLongPrice = Ask;

//Calculate StopLoss
if(StopLoss > 0) double OpenLongStopLoss = (OpenLongPrice - (StopLoss * UsePoint); //<--- This is where the errors are

//Open long order
BuyTicket = OrderSend(Symbol(),OP_BUY,LotSize,OpenLongPrice,UseSlippage,OpenLongStopLoss,0,"Open Long Order",MagicNumber,0,Green); //<-----This is where the errors are
}

//CloseLong
if(CloseEMA < SlowEMA || ADX < 25)
{
OrderSelect(BuyTicket,SELECT_BY_TICKET);
if(OrderCloseTime() == 0)
{
double LongCloseLots = OrderLots();
double LongClosePrice = Bid;
bool LongClosed = OrderClose(BuyTicket,LongCloseLots,LongClosePrice,UseSlippage,Red);
BuyTicket = 0;
}
}
//OpenShort
if(ADX >= 25 && FastEMA < SlowEMA && KStochastic < DStochastic && SellTicket == 0)
{
double OpenShortPrice = Bid;

//Calculate StopLoss
if(StopLoss > 0) double OpenShortStopLoss = OpenShortPrice + (StopLoss * UsePoint);

//Open short order
SellTicket = OrderSend(Symbol(),OP_SELL,LotSize,OpenShortPrice,UseSlippage,OpenShortStopLoss,0,"Open Short Order",MagicNumber,0,Green);
}

//CloseShort
if(CloseEMA > SlowEMA || ADX < 25)
{
OrderSelect(SellTicket,SELECT_BY_TICKET);
if(OrderCloseTime() == 0)
{
double ShortCloseLots = OrderLots();
double ShortClosePrice = Ask;
bool ShortClosed = OrderClose(SellTicket,ShortCloseLots,ShortClosePrice,UseSlippage,Red);
SellTicket = 0;
}
}
//ReturnControl
return(0);

}

//PipPointFunction
double PipPoint(string Currency)
{
int CalcDigits = MarketInfo(Currency,MODE_DIGITS);
if(CalcDigits == 2 || CalcDigits == 3) double CalcPoint = 0.01;
if(CalcDigits == 4 || CalcDigits == 5) CalcPoint = 0.0001;
return(CalcPoint);
}

//GetSlippageFunction
double GetSlippage(string Currency, int SlippagePips)
{
int CalcDigits = MarketInfo(Currency,MODE_DIGITS);
if(CalcDigits == 2 || CalcDigits == 4) double CalcSlippage = SlippagePips;
if(CalcDigits == 3 || CalcDigits == 5) CalcSlippage = SlippagePips * 10;
return(CalcSlippage);
}

Here are the errors I got back.

'=' - illegal assignment used C:\Program Files (x86)\FXCM MT4 powered by BT\experts\Trending EA.mq4 (65, 23)
'=' - assignment expected C:\Program Files (x86)\FXCM MT4 powered by BT\experts\Trending EA.mq4 (62, 54)
'=' - semicolon expected C:\Program Files (x86)\FXCM MT4 powered by BT\experts\Trending EA.mq4 (65, 23)
'=' - unexpected token C:\Program Files (x86)\FXCM MT4 powered by BT\experts\Trending EA.mq4 (65, 23)

Also, If I comment out the long open and close portion of the ea, and I back test, I get a ton of errors. I'm not really sure how to remidy the errors in the back test.

I would be very appreshative if someone could help me. Thank you very much!

Zachary.

 
zacharyh1:

Hello,

I just started learning about MQL4 and have written my first program. However, I am have a hard time trouble shooting a few of the errors that I encountered when I complied my EA.

Below is the code that I have written. The strategy is simple:

........................

Also, If I comment out the long open and close portion of the ea, and I back test, I get a ton of errors. I'm not really sure how to remidy the errors in the back test. I would be very appreshative if someone could help me. Thank you very much!

Zachary.

Hi Zachary,

Most errors are not on the line indicated. They are usually on the lines before the error code.

Regarding the number of errors listed. Don't worry. It usually is only one error that has a domino effect on other code. Just focus on the top error first.

Use your arrow back and keep tracking back your changes and keep recompiling until you find the error.

Fix the error and then recompile EACH TIME you make any new changes. That way you know your code is correct each step.

In your error example - you are missing one right parenthesis ) in your StopLoss statement.....

//OpenLong
if(ADX >= 25 && FastEMA > SlowEMA && KStochastic > DStochastic && BuyTicket == 0)
{
double OpenLongPrice = Ask;

//Calculate StopLoss

if(StopLoss > 0) double OpenLongStopLoss = (OpenLongPrice - (StopLoss * UsePoint) ) ; //<--- This is where the errors are

*** The error here seems to be a missing right paratheses at the end of your formula.

Hope this helps,

Robert





 

  1. Extra, useless parenthese.
    if(StopLoss > 0) double OpenLongStopLoss = (OpenLongPrice - (StopLoss * UsePoint);
    or simply
    if(StopLoss > 0) double OpenLongStopLoss = OpenLongPrice - StopLoss * UsePoint;
 

Thank you both!

That cleared up the errors.

But even now, with no errors after compiling, the ea is not running. Once I apply it to a chart, every tick that goes by creates a message that reads "2011.07.24 20:40:49 Trending EA EURUSD,M5: invalid ticket for OrderClose function"

I have tried messing around with where and how I define the BuyTicket and SellTicket variables, but nothing seems to work and I am running out of ideas. Googling the problem has not yeilded much fruit either.

With gratitude,

Zachary

 

So you have an " invalid ticket for OrderClose function" what is the value of BuyTicket and SellTicket when this happens ? did the original order get placed or did it fail making the ticket value -1 ? You need to check for, and handle, errors. Functions like

OrderSelect(BuyTicket,SELECT_BY_TICKET);

return a bool, either True or False to say if it worked or didn't, you need to take heed of this returned value.

 
  1. OrderSelect(SellTicket,SELECT_BY_TICKET);
    If this fails, so will everything that follows. ALWAYS test return codes.

  2. If the terminal crashes or you accidentally close it or the chart, OS reboots, power fails, etc. the EA must be able to recover. Your SellTicket will be zero on restart, no recovery.
        for(pos = OrdersTotal()-1; pos >= 0 ; pos--) if (
            OrderSelect(pos, SELECT_BY_POS)                 // Only my orders w/
        &&  OrderMagicNumber()  == magic.number             // my magic number
        &&  OrderSymbol()       == Symbol() ){              // and my pair.
            if (OrderType() == OP_SELL){ SellTicket = OrderTicket(); ...

Reason: