Trying to learn how to code EA's

Hi, I’m trying to code my first EA, and it turns out I have no idea what I’m doing.

I’m just wondering if anyone would be kind enough to have a look at what I’ve done so far and then either let me know if I’m on the right track, or if I should throw in the towel. Thanks heaps!

Basically I figured a moving average crossover system would be nice and easy to start with. In plain english what I want is:

“When two MA’s cross each other, put a pending order at the cross”

Whenever I see an EA code, it’s always hundreds of lines of code, and mine has like 30, which is why I’m thinking I have no idea what I’m doing. I’m not sure on the etiquette here, so I’m just going to copy paste the text here, if that’s wrong, please let me know so I can delete it.

Anyway, here it is. Am I way off or am I almost there?

Thanks again.

// Variables

extern double Lots = 0.1;
extern double Maximum risk = 0.01;

extern double Stoploss = 10
extern double Takeprofit = 15

extern double MA1 = 12;
extern double MA2 = 25;

extern bool EnterOpenBar = true;

extern int FastMATime = 0;
extern int FastMAPeriod = 12;
extern int FastMAType = 1; //0:SMA 1:EMA 2:SMMA 3:LWMA
extern int FastMAPrice = close;
extern int SlowMATime = 0;
extern int SlowMAPeriod = 24;
extern int SlowMAType = 0; //0:SMA 1:EMA 2:SMMA 3:LWMA
extern int SlowMAPrice = close;

//Initialization

//Start Function

int start()

FastMACurrent = iMA(NULL, FastMATime, FastMAPeriod, FastMAType, FastMAPrice, Current + 0);

FastMAPrevious = iMA(NULL, FastMATime, FastMAPeriod, FastMAType, FastMAPrice, Current + 1);

SlowMACurrent = iMA(NULL, SlowMATime, SlowMAPeriod, SlowMAType, SlowMAPrice, Current + 0);

SlowMAPrevious = iMA(NULL, SlowMATime, SlowMAPeriod, SlowMAType, SlowMAPrice, Current + 1);

//Order Logic

if (FastMACurrent > SlowMACurrent&& FastMAPrevious < SlowMAPrevious
&& OpenBar){
OpenBuylimit=true;
}

if (FastMACurrent < SlowMACurrent&& FastMAPrevious > SlowMAPrevious
&& OpenBar){
Openselllimit=true;
}

  • “Maximum risk” is not a valid variable name, empty space cannot be used in variable names
  • ; at the end of the row is must
  • FastMAPrice = close; - this thing “close” means nothing, there is no such predefined constant. There is a constant PRICE_CLOSE (Price Constants - MQL4 Documentation), but it can be used for the 6th parameter of iMA (apply_close) iMA - MQL4 Documentation
  • FastMACurrent and the other similar variables are also not defined. Each variable that you use must be defined locally or globally
  • You practically don’t have start() function. When you don’t use {}, then the body of the function is only the following row, not all rows that follows. In short - always use {} to wrap the body of each function

Seems. You have a long winding road ahead.
Good Luck

I can only wish you luck… I have been wanting to learn the same, but my only exposure has been creating indicators for TradingView.com charts using PINE, which I feel seems very archaic and mechanical.

I need to take a deeper look at using MT4 and I believe Lua, correct?