Help needed with Exper advisor

 

Hi everybody.

I am a newbie in the forex area and have started trading acouple of months back ina demo account. I got bold and tried out something and in the manual backtesting it gave me some good results (or so i think). Now i tried to make an EA for this strategy with the help of ExpertAdvisorBuilder tool but it is not working properly. The strategy is simple and here it is:

Buy When EMA 5 is above EMA8 and +DI14 crosses up -DI14. Close when +DI14 crosses down -DI14. This buy order repeats itself whenever +DI14 crosses up -DI14 while EMA 5 is above EMA8.

Sell When EMA 5 is below EMA8 and -DI14 crosses up +DI14. Close when -DI14 crosses down +DI14. This sell order repeats itself whenever -DI14 crosses up +DI14 while EMA 5 is above EMA8.

The code is given here and it is not working on the strategy tester of MT4 and gives no result.

The code is given in the next post as a limit of 10000 characters is set here.

Any help would be greatly appreciated as i am very keen on this.

Thanks everybody

Amit

 

Code first half

//+------------------------------------------------------------------+

//| This MQL is generated by Expert Advisor Builder |

//| Expert Advisor Builder for MetaTrader 4 |

//| |

//| In no event will author be liable for any damages whatsoever. |

//| Use at your own risk. |

//| |

//+------------------- DO NOT REMOVE THIS HEADER --------------------+

#define SIGNAL_NONE 0

#define SIGNAL_BUY 1

#define SIGNAL_SELL 2

#define SIGNAL_CLOSEBUY 3

#define SIGNAL_CLOSESELL 4

#property copyright "Expert Advisor Builder"

#property link "http://sufx.core.t3-ism.net/ExpertAdvisorBuilder/"

extern int MagicNumber = 0;

extern bool SignalMail = False;

extern bool EachTickMode = True;

extern double Lots = 1.0;

extern int Slippage = 3;

extern bool UseStopLoss = False;

extern int StopLoss = 100;

extern bool UseTakeProfit = False;

extern int TakeProfit = 400;

extern bool UseTrailingStop = False;

extern int TrailingStop = 30;

int BarCount;

int Current;

bool TickCheck = False;

//+------------------------------------------------------------------+

//| expert initialization function |

//+------------------------------------------------------------------+

int init() {

BarCount = Bars;

if (EachTickMode) Current = 0; else Current = 1;

return(0);

}

//+------------------------------------------------------------------+

//| expert deinitialization function |

//+------------------------------------------------------------------+

int deinit() {

return(0);

}

//+------------------------------------------------------------------+

//| expert start function |

//+------------------------------------------------------------------+

int start() {

int Order = SIGNAL_NONE;

int Total, Ticket;

double StopLossLevel, TakeProfitLevel;

if (EachTickMode && Bars != BarCount) TickCheck = False;

Total = OrdersTotal();

Order = SIGNAL_NONE;

//+------------------------------------------------------------------+

//| Variable Begin |

//+------------------------------------------------------------------+

double Var1 = iADX(NULL, 0, 14, PRICE_OPEN, MODE_PLUSDI, Current + 0);

double Var2 = iADX(NULL, 0, 14, PRICE_OPEN, MODE_MINUSDI, Current + 0);

double Var3 = iMA(NULL, 0, 5, 0, MODE_EMA, PRICE_OPEN, Current + 0);

double Var4 = iMA(NULL, 0, 8, 0, MODE_EMA, PRICE_OPEN, Current + 0);

double Var5 = iMA(NULL, 0, 5, 0, MODE_EMA, PRICE_OPEN, Current + 1);

double Var6 = iMA(NULL, 0, 8, 0, MODE_EMA, PRICE_OPEN, Current + 1);

double Var7 = iADX(NULL, 0, 14, PRICE_OPEN, MODE_PLUSDI, Current + 1);

double Var8 = iADX(NULL, 0, 14, PRICE_OPEN, MODE_MINUSDI, Current + 1);

double Buy1_1 = iMA(NULL, 0, 5, 0, MODE_EMA, PRICE_OPEN, Current + 0);

double Buy1_2 = iMA(NULL, 0, 8, 0, MODE_EMA, PRICE_OPEN, Current + 0);

double Buy2_1 = iADX(NULL, 0, 14, PRICE_OPEN, MODE_PLUSDI, Current + 0);

double Buy2_2 = iADX(NULL, 0, 14, PRICE_OPEN, MODE_MINUSDI, Current + 0);

double Buy3_1 = iMA(NULL, 0, 8, 0, MODE_EMA, PRICE_OPEN, Current + 0);

double Buy3_2 = iMA(NULL, 0, 8, 0, MODE_EMA, PRICE_OPEN, Current + 1);

double Buy4_1 = iADX(NULL, 0, 14, PRICE_OPEN, MODE_PLUSDI, Current + 1);

double Buy4_2 = iADX(NULL, 0, 14, PRICE_OPEN, MODE_MINUSDI, Current + 1);

double Sell1_1 = iMA(NULL, 0, 5, 0, MODE_EMA, PRICE_OPEN, Current + 0);

double Sell1_2 = iMA(NULL, 0, 8, 0, MODE_EMA, PRICE_OPEN, Current + 0);

double Sell2_1 = iMA(NULL, 0, 8, 0, MODE_EMA, PRICE_OPEN, Current + 0);

double Sell2_2 = iMA(NULL, 0, 8, 0, MODE_EMA, PRICE_OPEN, Current + 1);

double Sell3_1 = iADX(NULL, 0, 14, PRICE_OPEN, MODE_MINUSDI, Current + 0);

double Sell3_2 = iADX(NULL, 0, 14, PRICE_OPEN, MODE_PLUSDI, Current + 0);

double Sell4_1 = iADX(NULL, 0, 14, PRICE_OPEN, MODE_MINUSDI, Current + 1);

double Sell4_2 = iADX(NULL, 0, 14, PRICE_OPEN, MODE_PLUSDI, Current + 1);

double CloseBuy1_1 = iADX(NULL, 0, 14, PRICE_OPEN, MODE_PLUSDI, Current + 0);

double CloseBuy1_2 = iADX(NULL, 0, 14, PRICE_OPEN, MODE_MINUSDI, Current + 0);

double CloseSell1_1 = iADX(NULL, 0, 14, PRICE_OPEN, MODE_MINUSDI, Current + 0);

double CloseSell1_2 = iADX(NULL, 0, 14, PRICE_OPEN, MODE_PLUSDI, Current + 0);

//+------------------------------------------------------------------+

//| Variable End |

//+------------------------------------------------------------------+

 

//Check position

bool IsTrade = False;

for (int i = 0; i < Total; i ++) {

OrderSelect(i, SELECT_BY_POS, MODE_TRADES);

if(OrderType() <= OP_SELL && OrderSymbol() == Symbol()) {

IsTrade = True;

if(OrderType() == OP_BUY) {

//Close

//+------------------------------------------------------------------+

//| Signal Begin(Exit Buy) |

//+------------------------------------------------------------------+

if (CloseBuy1_1 < CloseBuy1_2) Order = SIGNAL_CLOSEBUY;

//+------------------------------------------------------------------+

//| Signal End(Exit Buy) |

//+------------------------------------------------------------------+

if (Order == SIGNAL_CLOSEBUY && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {

OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, MediumSeaGreen);

if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + " Close Buy");

if (!EachTickMode) BarCount = Bars;

IsTrade = False;

continue;

}

//Trailing stop

if(UseTrailingStop && TrailingStop > 0) {

if(Bid - OrderOpenPrice() > Point * TrailingStop) {

if(OrderStopLoss() < Bid - Point * TrailingStop) {

OrderModify(OrderTicket(), OrderOpenPrice(), Bid - Point * TrailingStop, OrderTakeProfit(), 0, MediumSeaGreen);

if (!EachTickMode) BarCount = Bars;

continue;

}

}

}

} else {

//Close

//+------------------------------------------------------------------+

//| Signal Begin(Exit Sell) |

//+------------------------------------------------------------------+

if (CloseSell1_1 < CloseSell1_2) Order = SIGNAL_CLOSESELL;

//+------------------------------------------------------------------+

//| Signal End(Exit Sell) |

//+------------------------------------------------------------------+

if (Order == SIGNAL_CLOSESELL && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {

OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, DarkOrange);

if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + " Close Sell");

if (!EachTickMode) BarCount = Bars;

IsTrade = False;

continue;

}

//Trailing stop

if(UseTrailingStop && TrailingStop > 0) {

if((OrderOpenPrice() - Ask) > (Point * TrailingStop)) {

if((OrderStopLoss() > (Ask + Point * TrailingStop)) || (OrderStopLoss() == 0)) {

OrderModify(OrderTicket(), OrderOpenPrice(), Ask + Point * TrailingStop, OrderTakeProfit(), 0, DarkOrange);

if (!EachTickMode) BarCount = Bars;

continue;

}

}

}

}

}

}

//+------------------------------------------------------------------+

//| Signal Begin(Entry) |

//+------------------------------------------------------------------+

while(Buy1_1 > Buy1_2 && Buy3_1 > Buy3_2)

{

if (Buy2_1 > Buy2_2 && Buy4_1 <= Buy4_2) Order = SIGNAL_BUY;

}

while(Sell1_1 < Sell1_2 && Sell2_1 < Sell2_2)

{

if (Sell3_1 > Sell3_2 && Sell4_1 <= Sell4_2) Order = SIGNAL_SELL;

}

//+------------------------------------------------------------------+

//| Signal End |

//+------------------------------------------------------------------+

//Buy

if (Order == SIGNAL_BUY && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {

if(!IsTrade) {

//Check free margin

if (AccountFreeMargin() < (1000 * Lots)) {

Print("We have no money. Free Margin = ", AccountFreeMargin());

return(0);

}

if (UseStopLoss) StopLossLevel = Ask - StopLoss * Point; else StopLossLevel = 0.0;

if (UseTakeProfit) TakeProfitLevel = Ask + TakeProfit * Point; else TakeProfitLevel = 0.0;

Ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage, StopLossLevel, TakeProfitLevel, "Buy(#" + MagicNumber + ")", MagicNumber, 0, DodgerBlue);

if(Ticket > 0) {

if (OrderSelect(Ticket, SELECT_BY_TICKET, MODE_TRADES)) {

Print("BUY order opened : ", OrderOpenPrice());

if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + " Open Buy");

} else {

Print("Error opening BUY order : ", GetLastError());

}

}

if (EachTickMode) TickCheck = True;

if (!EachTickMode) BarCount = Bars;

return(0);

}

}

//Sell

if (Order == SIGNAL_SELL && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {

if(!IsTrade) {

//Check free margin

if (AccountFreeMargin() < (1000 * Lots)) {

Print("We have no money. Free Margin = ", AccountFreeMargin());

return(0);

}

if (UseStopLoss) StopLossLevel = Bid + StopLoss * Point; else StopLossLevel = 0.0;

if (UseTakeProfit) TakeProfitLevel = Bid - TakeProfit * Point; else TakeProfitLevel = 0.0;

Ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage, StopLossLevel, TakeProfitLevel, "Sell(#" + MagicNumber + ")", MagicNumber, 0, DeepPink);

if(Ticket > 0) {

if (OrderSelect(Ticket, SELECT_BY_TICKET, MODE_TRADES)) {

Print("SELL order opened : ", OrderOpenPrice());

if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + " Open Sell");

} else {

Print("Error opening SELL order : ", GetLastError());

}

}

if (EachTickMode) TickCheck = True;

if (!EachTickMode) BarCount = Bars;

return(0);

}

}

if (!EachTickMode) BarCount = Bars;

return(0);

}

//+------------------------------------------------------------------+

 

guys please, some help would be highly appreciated.

Amit

 

hi,

i make this EA purely based on your criteria:

Buy When EMA 5 is above EMA8 and +DI14 crosses up -DI14. Close when +DI14 crosses down -DI14. This buy order repeats itself whenever +DI14 crosses up -DI14 while EMA 5 is above EMA8.

Sell When EMA 5 is below EMA8 and -DI14 crosses up +DI14. Close when -DI14 crosses down +DI14. This sell order repeats itself whenever -DI14 crosses up +DI14 while EMA 5 is above EMA8.

Opening/Closing order after the bar closed, ie at the opening of the next bar.

hope this help

Files:
emadi.mq4  3 kb
 

Hi sangmane, you rock..

thanks for the quick reply. I have still not been able to download the EA though as it is pending approval. As soon as itis approved i will test it and get back to you. However, if it is not too much to ask, can you please make another EA with the following specs.

Buy When EMA 5 is above EMA80 and +DI14 crosses up -DI14 and the EMA80 angle is on an upward trend. Close when +DI14 crosses down -DI14. This buy order repeats itself whenever +DI14 crosses up -DI14 while EMA 5 is above EMA80 and EMA80 angle is on an upward trend.

Sell When EMA 5 is below EMA80 and -DI14 crosses up +DI14 and the EMA80 angle is on a downward trend. Close when -DI14 crosses down +DI14. This sell order repeats itself whenever -DI14 crosses up +DI14 while EMA 5 is above EMA80 and EMA80 angle is on a downward trend.

I am using the attached signals for this system.

Thanks for everything.

Amit

 

hi,

i will add the MA angle signal after you review the EA that i've posted. imho you must use another signal to exit. we can't just rely on ema cross to enter and exit, bcos most of the time this will result in a loss. in a strong up trend, fast ema cross slow ema can be viewed as a start of a trend, and you buy when price is moving up. but when there is no strong trend, you actually buy at the higher price and sell (exit) at the lower price.

regards,

sangmane

 

Hi Sangmane,

I checked the EA you had posted and following are the observations:

EURUSD, H1, 01.10.2009 to 08.10.2009

Date Time Trade Observation

02.10.2009 10 Sell Open Here -DI was down.

02.10.2009 11 Sell Close

02.10.2009 13 Sell Open OK

02.10.2009 17 Sell Close Should be 16, EMA cross at 15

02.10.2009 18 Sell Open OK

02.10.2009 20 Sell Close Should be 05.10.2009 01.00

05.10.2009 2 Buy Open Should be 1, Ema cross at 00.00

05.10.2009 13 Buy Close OK

05.10.2009 18 Buy Open Should be 17, Ema cross at 16

06.10.2009 20 Buy Close OK, Ema cross at 19

07.10.2009 1 Buy Open OK

07.10.2009 3 Buy Close OK

07.10.2009 7 Buy Open Should be 8, Ema cross at 7

07.10.2009 8 Buy Close Should be 13, Ema cross at 12

08.10.2009 2 Buy Open Should be 1, Ema cross at 00.00

08.10.2009 13 Buy Close OK

08.10.2009 15 Buy Open Should be 14, Ema cross at 13.

08.10.2009 17 Buy Close OK

08.10.2009 18 Buy Open Should be 17, Ema cross at 16.

09.10.2009 2 Buy Close Should be 1, Ema cross at 00.00

Can you pleasetweak the new EA to open and close positions at the correct time.

Thanks

Amit

 

Hi Sangmane,

I manually tested the following strategy using EURUSD for 2001, 2002 and 2003 in the daily chart and the results are fairly impressive.

Buy When EMA 5 is above EMA80 and +DI14 crosses up -DI14. Close when +DI14 crosses down -DI14. This buy order repeats itself whenever +DI14 crosses up -DI14 while EMA 5 is above EMA80 and EMA80.

Sell When EMA 5 is below EMA80 and -DI14 crosses up +DI14. Close when -DI14 crosses down +DI14. This sell order repeats itself whenever -DI14 crosses up +DI14 while EMA 5 is below EMA80.

I have used the ADXDMI indicator for this and the values are the opening values of the next bar after DI cross as you had previously suggested.

Amit

 

hi amit,

i found it difficult to crosscheck your observation. it seems we have different history

data. it'll be better if you can upload the picture of your chart.

about entry trigger, i'm affraid i coded it differently from what you mean. let's make it

clear.

entry criteria for the last EA (EmaDMI):

ema and adx using PRICE_CLOSE and not PRICE_OPEN

BUY: open buy order when:

(ema 5 has been above ema 8) AND (+di just crossed up -di)

OR

(+di has been above -di) AND (ema 5 just crossed up ema 8)

close buy order when ema 5 crossed down ema 8 OR +di cross down -di

SELL: open sell order when:

(ema 5 has been below ema 8) AND (+di just crossed down -di)

OR

(-di has been below +di) AND (ema 5 just crossed down ema 8)

close sell order when ema 5 crossed up ema 8 OR +di cross up -di

pls check the attached picture below. i can't see any false signal that you mentioned, so i

can't tweak the code until i get clear where is the false signal

i'm using built-in ADX indicator. the second ea below (EmaDMI2) using your ADXDMI indicator,

with different buy/sell criteria:

BUY: open buy order when:

(ema 5 has been above ema 8) AND (+di just crossed up -di)

SELL: open sell order when:

(ema 5 has been below ema 8) AND (+di just crossed down -di)

u can use expert properties to change ema period (i.e to change ema 8 to ema 80) or to

change adx period and adx smooth

cheers

Files:
 

Hi Sangmane,

I tested EmaDI2 for the year 2005, EURUSD in daily chart. I also backtested manually with the same parameters. There i a lot of difference in both. I have attached the strategy tester html report and the manual report in excel sheet. Please have a look. Following were the rules when i tested manually:

While Ema5 is above Ema80:

Buy open when +DI crosses above -DI. Here i open at the opening value of the next bar after the crossing.

Close Buy when +DI crosses down -DI. Here i close at the opening value of the next bar after the crossing.

While Ems5 is below Ema80:

Sell open when -DI crosses above +DI. Here i open at the opening value of the next bar after the crossing.

Close Sell when -DI crosses down +DI. Here i close at the opening value of the next bar after the crossing.

The dates of open and close mentioned in the manual testing report are the next date from the date of crossing and the currency value is the opening value.

Thanks for your time.

Amit

Files:
Reason: