EA coding help needed for study purpose.

 

I have been studying MQL4 for a while now hoping to someday code my strategy into an EA but i am still having problems here and there. Just for study purpose (something i can make reference to) can somebody PLEASE HELP me combine the MT4 platform Custom indicators "MACD and CCI" (attached below) to create an EA using the iCustom function.

Rules:

EA should place a Buy order when the MACD histogram crosses the 0.00 line into the positive side "IF AND ONLY IF" the value of the CCI indicator is less than 100 (ie. if and only if the value of CCI<100)

EA should place a sell order when the MACD histogram crosses the 0.00 line into the negative side "IF AND ONLY IF" the value of the CCI indicator is above -100 (ie. If and only if the value of CCI>-100)

Thank you all for your anticipated positive response and assistance.

Files:
macd.mq4  3 kb
cci.mq4  4 kb
 
Maybe you should study this link: MT4 & MT5 coding study guide
 
Hi,
This is the simplest EA I've ever seen :) For study purpose only, without any error handling.

Regards!

extern int MacdFastEma = 12;
extern int MacdSlowEma = 26;
extern int MacdSignalSma = 9;
extern int CciPeriods = 14;

extern double Lots = 0.1;
extern double StopPoints = 300;
extern double TargetPoints = 600;

datetime LastCandleTime;

int init() {
   LastCandleTime = 0;
   return(0);
}

int start() {
   if (LastCandleTime == Time[0]) {
      return(0);
   }
   double MacdPrevPrev = iCustom(NULL, 0, "MACD", MacdFastEma, MacdSlowEma, MacdSignalSma, 0, 2);
   double MacdPrev = iCustom(NULL, 0, "MACD", MacdFastEma, MacdSlowEma, MacdSignalSma, 0, 1);
   double CciPrev = iCustom(NULL, 0, "CCI", CciPeriods, 0, 1);

   if (MacdPrevPrev < 0.0 && MacdPrev > 0.0 && CciPrev < 100.0) {
      OrderSend(Symbol(), OP_BUY, Lots, Ask, 3, Bid - StopPoints * Point, Ask + TargetPoints * Point, "Example", 6384, 0, Green);
   }
   if (MacdPrevPrev > 0.0 && MacdPrev < 0.0 && CciPrev > -100.0) {
      OrderSend(Symbol(), OP_SELL, Lots, Bid, 3, Ask + StopPoints * Point, Bid - TargetPoints * Point, "Example", 6384, 0, Red);
   }
   LastCandleTime = Time[0];
   return(0);
}
 
erzo:
Hi,
This is the simplest EA I've ever seen :) For study purpose only, without any error handling.

Regards!

Is it better to give a bad example or no example at all ?
 
Thank You very very much. You are Highly appreciated. I will work with this and see what i can make of it.
 
RaptorUK:
Is it better to give a bad example or no example at all ?

Please what are you insinuating with your question?
 
nobleman247:

Please what are you insinuating with your question?
Simple, the example given is a bad one . . . as already mentioned, there is no error handling, or reporting, and error handiling and reporting is what yo will need the most when you aare starting.
 

There are common mistakes that are made day after day and people come to this Forum asking for help . . . . for example, error 130, some error reporting and printing of variables will help with error 130 . . . some peope just will not do it even if you suggest it 3 times.

A function gives a return value for a reason . . . learn to take notice of that return value and you will make life easy for yourself.

 
Please allow me ask this question what is "error handiling and reporting"...?
 
nobleman247:
Please allow me ask this question what is "error handiling and reporting"...?

It is what you need to learn.

Look at aany of the Trading Functions . . . for example, OrderSend() the Documentation says . . . "Returns number of the ticket assigned to the order by the trade server or -1 if it fails." so we chack the return value, if it is equal to -1 then we have an error . . . so we need to handle this error, "error handling".

int ticket = OrderSend( . . . . .  . . );

if(ticket == -1)
   {
   Print("OrderSend failed, error: ", GetLastError() );
   Print("Bid = ", Bid, " Ask = ", Ask, etc, etc);
   }

A little bit of error handling and reporting goes a long way . . . if you make an error on the OrderSend then your error handling will catch it and report it to the log . . . then you can look at the log and have more chance of figuring out what is wrong and fixing it.

 

RaptorUK,

My is a very bad example for real account, but it's enough for strategy tester, and begin to play with coding and see how the visual tester trading with it. Learning how indicators can combine.

It is a ten-row-snippet, same that i'm seaching for when I start coding something that new to me, least code, that focuses to the essence. As java developer my life was saved by such ten rows many times, because almost everything has been programmed and discussed of for now. With mq4 code searching is different, developers seems not willing to share codes, maybe because it is all of money, and a working idea is working only while just a few people using it.

Error handling is the hardest task, and after 800 row of it I'm at nowhere, and not sure I ever dare place it on any real account. Best way to buy a proven ea from the top 5 and try fortune with it. But playing around indicators and visual tester hurts no one.

And as I see I could provocate even you to share some useful code...