Trying to implement an order on MA cross

Hi,

I want to create my own EA, but I am stuck because programming is not my forte.

I have created/copied a simple alert indicator with which I am happy, but instead of Comment SELL or BUY I want the EA to enter a trade.

Can someone please replace the comment part with the code necessary to enter an order? the first part about the variables is still commented because i don’t know how to proceed after “if” statement.

Keep ti simple please

//extern double TakeProfit = 50;
//extern double Lots = 0.2;
//extern double TrailingStop = 30;
//int OrderSend ("EURUSD",OP_SELL,0.2,ask,,Bid-50*Point,Bid+50*Point);

void OnTick()
{
double SlowMovingAverage = iMA(NULL,0,10,0, MODE_EMA,PRICE_CLOSE,0);
  
double LastSlowMovingAverage = iMA(NULL,0,10,0, MODE_EMA,PRICE_CLOSE,1);

double FastMovingAverage = iMA(NULL,0,20,0, MODE_EMA,PRICE_CLOSE,0);
  
double LastFastMovingAverage = iMA(NULL,0,20,0, MODE_EMA,PRICE_CLOSE,1);

if ((LastFastMovingAverage < LastSlowMovingAverage)
      && (FastMovingAverage > SlowMovingAverage))

Comment ("SELL");


if ((LastFastMovingAverage > LastSlowMovingAverage)
      && (FastMovingAverage < SlowMovingAverage))

Comment ("BUY");
 
} 

Thanks in advance

this is my current code

void OnTick()
{
double SlowMovingAverage = iMA(NULL,0,10,0, MODE_EMA,PRICE_CLOSE,0);
   
double LastSlowMovingAverage = iMA(NULL,0,10,0, MODE_EMA,PRICE_CLOSE,1); 

double FastMovingAverage = iMA(NULL,0,20,0, MODE_EMA,PRICE_CLOSE,0);
   
double LastFastMovingAverage = iMA(NULL,0,20,0, MODE_EMA,PRICE_CLOSE,1);

if ((LastFastMovingAverage < LastSlowMovingAverage)
      && (FastMovingAverage > SlowMovingAverage))

//Comment ("SELL");
OrderSend (Symbol(),OP_SELL,0.3,Bid,2,Bid-50*Point,Bid+70*Point);

if ((LastFastMovingAverage > LastSlowMovingAverage)
   && (FastMovingAverage < SlowMovingAverage))

//Comment ("BUY");
OrderSend (Symbol(),OP_BUY,0.3,Ask,10,Ask-50*Point,Ask+70*Point);


}

Probably you got so many orders placed because you mixed slow with fast average. Slow is 20, fast 10.

You have got to integrate a “Calculate Open Orders” function and let the EA open trade only if the number of opened orders is zero . And, you got the moving averages a little mixed up .

Hi,

thank you for your input, I have corrected the numbers but am still getting many orders opened. It has to be onThick function.

May i sugest you to try to start from the Moving Average sample provided with MT4 , implement your moving averages there and bar the volume condition for opening orders and on closing conditions istead of the conditions for closing just input “Close[1]”. And in defining OrderSend function , for correct calculation of stoploss , for buy order it should be buy at ask , stoploss at BID-XPoint and TP ASK+XPoint and for sell , sell at bid , stop at ASK+XPoint and TP at BID-XPoint.

…and check your open conditions, do you really want to sell when fast MA up-crosses slow MA?

…and the SL/TP values for the sell order are wrong

…and always check return values they are meaningful

Try this…

If(OrdersTotal()==0){

// OrderSend etc

}

Problem is the more u learn, the more you will want to learn…

Thank you all for participating!

I did not expect this at all, every single one of your posts helped me in coming closer to my full functional EA.

I will try to perfect it for couple of more days and when I get stuck, I will report back here.

My Ideas are:

  1. Implement a trailing stop after the order has been issued, so I can have a larger takeprofit margin.

  2. Implementing a time option to trade only during London hours.

  3. Enter the trade after MA’s have crossed on the next closed bar which has to be in the “direction” of the cross.

I wont mind if anybody writes some hints about these ideas.

Thank you once again guys, really appreciate it!

Hi

I understand you as a newb, I was in the same case before and did not know how to build any EA. Then one week-end I read the whole documentation and all became clear…

You say you don’t want to learn the whole thing but you cannot have anything for nothing. Either you pay it or you learn it.

Your question about bar zero : bar zero is the current bar. If you don’t check that the bar is new, then you are checking the current tick, which is crazy, your orders can be triggered every second.

One tip for you. After I read the documentation, I went into an online tool that would generate the code for me with a few clicks (it is called EA builder). By reading the source code of what it generates, it dramatically improved my understanding of the whole thing.

Then I read “clean code” by M. Fowler, and I definitely reached another level of knowledge, far above what you can expect. I would highly recommend you to learn more. It’s fun!

Hows it going, some updates on progress?