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, noob here.

//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.

 
Kidfisto: I want to create my own EA
  1. Why did you post your MT4 question in the Root / MT5 EA section instead of the MQL4 section, (bottom of the Root page?)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
    Next time post in the correct place. The moderators will likely move this thread there soon.

  2. When you post code please use the CODE button (Alt-S)! (For large amounts of code, attach it.) Please edit your (original) post.
              General rules and best pratices of the Forum. - General - MQL5 programming forum
              Messages Editor

  3. Help you with what? You haven't stated a problem, you stated a want.
    You have only four choices:
    1. Search for it,
    2. Beg at
    3. learn to code it. If you don't learn MQL4/5, there is no common language for us to communicate. If we tell you what you need, you can't code it. If we give you the code, you don't know how to integrate it into yours.
    4. or pay (Freelance) someone to code it.
    We're not going to code it for you (although it could happen if you are lucky or the problem is interesting.) We are willing to help you when you post your attempt (using CODE button) and state the nature of your problem.
              No free help
              urgent help.

  4. And unless you want to open an order every tick, you need to test for open orders or a new bar.
  5. And you are looking at bar zero MA. That may cross and uncross multiple times as price moves.
 

Hi,


thank you for your answer.

1. As this is my first time writing this on this forum I made a mistake which was not intentional.

2. Changed!

3. O I stated the problem o right, the problem is that I don't know how to code my intention.

In the mean time I managed to make some progress but your number 4. is right where my next problem is, I am having milion of orders opened. I suspect this is because of the "void OnThick" function.

I am trying to learn the code but my intention is not to learn the whole thing but only what I need.


4. Like I said before now I am getting many trades open on every thick and this is not what I want, can you point me in the right direction?

5. Unfortunately, I have no clue what does "bar zero" mean.

Thanks for the links!


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.
 
Kidfisto:

Hi,


thank you for your answer.

1. As this is my first time writing this on this forum I made a mistake which was not intentional.

2. Changed!

3. O I stated the problem o right, the problem is that I don't know how to code my intention.

In the mean time I managed to make some progress but your number 4. is right where my next problem is, I am having milion of orders opened. I suspect this is because of the "void OnThick" function.

I am trying to learn the code but my intention is not to learn the whole thing but only what I need.


4. Like I said before now I am getting many trades open on every thick and this is not what I want, can you point me in the right direction?

5. Unfortunately, I have no clue what does "bar zero" mean.

Thanks for the links!


This is my current code:


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 yeah, you got the moving averages a little mixed up .
 
lippmaje:
Probably you got so many orders placed because you mixed slow with fast average. Slow is 20, fast 10.

Hi,


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

 
Kidfisto:

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-X*Point and TP ASK+X*Point and for sell , sell at bid , stop at ASK+X*Point and TP at BID-X*Point.
 

...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
 
Kidfisto:

Hi,


thank you for your answer.

1. As this is my first time writing this on this forum I made a mistake which was not intentional.

2. Changed!

3. O I stated the problem o right, the problem is that I don't know how to code my intention.

In the mean time I managed to make some progress but your number 4. is right where my next problem is, I am having milion of orders opened. I suspect this is because of the "void OnThick" function.

I am trying to learn the code but my intention is not to learn the whole thing but only what I need.


4. Like I said before now I am getting many trades open on every thick and this is not what I want, can you point me in the right direction?

5. Unfortunately, I have no clue what does "bar zero" mean.

Thanks for the links!


This is my current code:


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 Kidfisto,

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!

Reason: