Linear Weighted MA CrossOver EA

 

Hi,

I am a newbie programmer and i am trying to improve my coding skills by trying to create my own EA. "It is easy" they said but that is far from the truth. Anyway i am trying to create a 'simple' EA that opens trades when two LWMAs meet. I used the 8 and 14 MA Periods and created two variations. One with a shift of 2 and another with 0.So when the 2 LWMAs meet depending on the trend it is suppose to open a new order and close a previous one but i cant seem to make it work. Still new to this so don't judge.



double MA1=iMA(Symbol(),PERIOD_H1,8,0,MODE_LWMA,PRICE_CLOSE,0);

double MA2=iMA(Symbol(),PERIOD_H1,14,0,MODE_LWMA,PRICE_CLOSE,0);


double MA3=iMA(Symbol(),PERIOD_H1,8,0,MODE_LWMA,PRICE_CLOSE,-2);

double MA4=iMA(Symbol(),PERIOD_H1,14,0,MODE_LWMA,PRICE_CLOSE,-2);



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

//| Start function                                             |

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

int start()

  {

//---

   Open_Order();

   Close_Orders();

   return 0;

  }

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

//| Open New Order

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

void Open_Order(){


if(OrdersTotal()>=0){


if(MA1<=MA2 && MA3>=MA4)

OrderSend(Symbol(),OP_SELL,0.1,Bid,0,NULL,NULL); 


else if(MA1>=MA2 && MA3<=MA4){

OrderSend(Symbol(),OP_BUY,0.1,Ask,0,NULL,NULL);//,Bid-30*Point,Bid+300*Point);

}else{

return;


}

return;

}

}

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

//| Close Order

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


void Close_Orders(){


for(int i=1;i<=OrdersTotal();i++){

if(OrderSelect(i-1,SELECT_BY_POS)==true){

if(OrderSymbol()!=Symbol())continue;


RefreshRates();


int Ticket=OrderTicket();

double Lots=OrderLots();


if(OrderType()==OP_SELL && (MA1>=MA2 && MA3<=MA4)){

OrderClose(Ticket,Lots,Ask,0);

}

else if((OrderType()==OP_BUY) && (MA1<=MA2 && MA3>=MA4)){

OrderClose(Ticket,Lots,Bid,0,clrWhite);

}


}

}

}

 
  1. Don't paste code
    Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. double MA1=iMA(Symbol(),PERIOD_H1,8,0,MODE_LWMA,PRICE_CLOSE,0);
    double MA2=iMA(Symbol(),PERIOD_H1,14,0,MODE_LWMA,PRICE_CLOSE,0);
    double MA3=iMA(Symbol(),PERIOD_H1,8,0,MODE_LWMA,PRICE_CLOSE,-2);
    double MA4=iMA(Symbol(),PERIOD_H1,14,0,MODE_LWMA,PRICE_CLOSE,-2);
    int start() ...
    These variables never change. Assign them in start.
  3. Candles are numbered zero (the forming one) to Bars - 1. You can never read the future.
  4. if(OrdersTotal()>=0){
    Using OrdersTotal directly and/or no Magic number filtering on your OrderSelect loop means your code is incompatible with every EA (including itself on other charts and manual trading.) Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 forum
  5. OrderSend(Symbol(),OP_BUY,0.1,Ask,0,NULL,NULL);
    • You can use NULL in place of _Symbol only in those calls that the documentation specially says you can. iHigh does, MarketInfo does not. OrderSend does not.
    • Don't use NULL (except for pointers where you explicitly check for it.)
  6. Check your return codes What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
  7. for(int i=1;i<=OrdersTotal();i++){
    In the presence of multiple orders (one EA multiple charts, multiple EA's, manual trading)
Reason: