Moving Average EA

Back to topics list To post a new topic, please log in or register
avatar
18
BartWinter 2015.03.11 22:03
 

Hello,

I'm new to MQL4 Coding and have been learning it only for a couple of months. I am attempting of writing an EA which opens a trade when the MAs cross and close when the cross for a second time. I can get the EA to open in the right direction but I cannot get it to close. I could really use a helping hand. Thank you.

Here's the code.

 

extern double    Lots=0.2;

extern int       StopLoss=20;

extern string    Ma1;

extern int       Ma1Period=6;

extern int       Ma1Shift=0;

extern int       Ma1Method=0;

extern string    Ma2;

extern int       Ma2Period=3;

extern int       Ma2Shift=0;

extern int       Ma2Method=0;


#define MAX_ORDERS 4 //This controls the total number of that can be placed at any one time.



void start() //This process repeats itself after every tick (change in price).

{

Ma1=iMA(Symbol(),PERIOD_M1,6,0,MODE_SMA,PRICE_TYPICAL,0); //Defining the moving average

Ma2=iMA(Symbol(),PERIOD_M1,3,0,MODE_SMA,PRICE_TYPICAL,0); //Defining the moving average

int ticketbuy, ticketsell;  

   {if(OrdersTotal()<MAX_ORDERS) //Total number of orders

      //Buy Criteria

      {

      if(Ma1<Ma2)

      {

      ticketbuy = OrderSend(Symbol(),OP_BUY,Lots,Ask,10,StopLoss,0,"test_1",0,0,Green);

      

      }

      // Sell Criteria

      else

      {

      ticketsell = OrderSend(Symbol(),OP_SELL,Lots,Bid,10,StopLoss,0,"test_1",0,0,Red);

      }

      

//The section below is with regards to closing a live trade

if(Ma1>Ma2)// Condition to close the trade

  {

  bool buyingresult;

  buyingresult = OrderClose(ticketbuy,Lots,Bid,10,Blue);

  }


if(Ma1<Ma2)// Condition to close the trade

  {

  bool buyingresult;

  buyingresult = OrderClose(ticketsell,Lots,Bid,10,Blue);

  }

}


}

}

 

avatar
12995
WHRoeder 2015.03.11 22:23 #
 
  1. Don't paste code
    Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. Check your return codes (OrderClose) 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
  3. Your ticketbuy/ticketsell are not global or static.

avatar
Moderator
3416
GumRai 2015.03.12 00:45 #
 

Why do you have these?

extern string    Ma1;
extern string    Ma2;

 They should be doubles, not strings and no need for the extern

avatar
18
BartWinter 2015.03.12 09:31 #
 
Thanks for the help guy it has removed all my warnings. However, is the code regarding closing the trade satisfactory? Because for some reason I cannot get it to close any of my live trades. Thanks again for the advice.
avatar
Moderator
3416
GumRai 2015.03.12 10:12 #
 

As WHRoeder has already pointed out, for ticketbuy and ticketsell to retain their values between ticks they have to be declared globally or as static variables.

Also, do you really want to work with the current bar? It is possible that MAs can cross and recross many times during the life of a single candle which can result in opening and closing lots of orders. 

avatar
18
BartWinter 2015.03.12 12:55 #
 
At the moment I don't mind. I'm not looking for profits at the moment I just want the EA to close trades. I'm really struggling for it to do so. Is the code correct in terms of closing live trades? 
avatar
2017
gooly 2015.03.12 13:44 #
 
BartWinter:
At the moment I don't mind. I'm not looking for profits at the moment I just want the EA to close trades. I'm really struggling for it to do so. Is the code correct in terms of closing live trades? 

Why don't you look at the sample EA of MT4 in you expert folder or you search for trade manager and study how all of them manage open trades and close them.

You'll profit a lot more!

avatar
Moderator
3416
GumRai 2015.03.12 17:16 #
 
BartWinter:
At the moment I don't mind. I'm not looking for profits at the moment I just want the EA to close trades. I'm really struggling for it to do so. Is the code correct in terms of closing live trades? 

No.

WHRoeder:

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

  2. Check your return codes (OrderClose) 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
  3. Your ticketbuy/ticketsell are not global or static.

GumRai:

As WHRoeder has already pointed out, for ticketbuy and ticketsell to retain their values between ticks they have to be declared globally or as static variables.

Also, do you really want to work with the current bar? It is possible that MAs can cross and recross many times during the life of a single candle which can result in opening and closing lots of orders. 

 

avatar
18
BartWinter 2015.03.12 18:33 #
 
Thanks a lot for your help guys I have no more warnings. I'm going to give the advice which I have been given a go. THANK YOU!!
Back to topics list  

To add comments, please log in or register