Download MetaTrader 5
To add comments, please log in or register
Follow the news. View our company's official channel!
Jack Rock
52
Jack Rock 2015.10.21 19:54 

Hi everyone, I have been trying to learn MQL coding for the last few months, but the more I learn by myself and with no one to assist me and guiding me, I have reached no where.

I wish to create a simple EA, Moving average EA where a SMA period 10 crosses over/under a SMA period 50. It should open the trade only once every time the condition is met, NOT opening multiple orders or continues to open orders once the condition of cross over has happened.

Can someone please assist me with this simple EA please? I would appreciate this immensely. Please ask if you need more info or do not understand and can assist please.

Sincerely, Jack

Keith Watford
Moderator
7899
Keith Watford 2015.10.21 21:14  
Post your code and I am sure that someone will give you some pointers
Demos Stogios
184
Demos Stogios 2015.10.21 22:23  
rocktheworld:

Hi everyone, I have been trying to learn MQL coding for the last few months, but the more I learn by myself and with no one to assist me and guiding me, I have reached no where.

I wish to create a simple EA, Moving average EA where a SMA period 10 crosses over/under a SMA period 50. It should open the trade only once every time the condition is met, NOT opening multiple orders or continues to open orders once the condition of cross over has happened.

Can someone please assist me with this simple EA please? I would appreciate this immensely. Please ask if you need more info or do not understand and can assist please.

Sincerely, Jack

Hello,

can I ask the ways you are trying to learn MQL? I am asking so because I am getting the impression that you are going directly to MQL, which is normally not getting you anywhere, since there are not any real documents out there to help us about. So, ff that is the case, I recommend to start learning the basics of C programming, as C is a nice and simple language and MQL is based on it; you may consider C++ to be the same, as we can usually see the C/C++ writing, but make no mistake, C++ is different from C. Of course you can pick whatever language you like, but you can either grab a book, e-book or online resources and get your self started right on

 best regards 

Keith Watford
Moderator
7899
Keith Watford 2015.10.22 05:20  
Demos:

Hello,

can I ask the ways you are trying to learn MQL? I am asking so because I am getting the impression that you are going directly to MQL, which is normally not getting you anywhere, since there are not any real documents out there to help us about. So, ff that is the case, I recommend to start learning the basics of C programming, as C is a nice and simple language and MQL is based on it; you may consider C++ to be the same, as we can usually see the C/C++ writing, but make no mistake, C++ is different from C. Of course you can pick whatever language you like, but you can either grab a book, e-book or online resources and get your self started right on

 best regards 

Interesting. I always thought that MQL4 was based on C++

Is C easier to learn?

I learned MQL4 from "The Book" and reading posts here and asking the odd question. I had no knowledge of C or ++.

I would like to learn more in order to be able to use structs and stuff in the include files etc as I simply don't understand them.

I have looked at some online resources for learning C++, but haven't made any progress. Possibly because I have been unable to find a way to incorporate C++ int mq4 immediately.

So you think that it may be worth my while checking out C? 

Jack Rock
52
Jack Rock 2015.10.22 12:52  

The little knowledge I have about MQL code is through online EA Building sites and learning from the code that is generated and through online courses on YouTube, yet the very little basic knowledge I have gathered made me more confused every time I tried to manipulate or change codes and adding to it which resulted in just non functioning EA's with no result at the end of the day.

So can anyone assist me in this simple EA please?

Jack Rock
52
Jack Rock 2015.10.22 13:39  
This is the code that I have, but when I test this, it creates endless orders after each order has closed after profit taking. I just want this code to do the 1 order when the conditions are met, thus when the 2 MA's crossover.

Please can someone assist me with this, I would appreciate it.

extern int MagicNumber=10001;
extern double Lots =0.1;
extern double StopLoss=50;
extern double TakeProfit=50;
extern int TrailingStop=50;
extern int Slippage=3;
//+------------------------------------------------------------------+
//    expert start function
//+------------------------------------------------------------------+
int start()
{
  double MyPoint=Point;
  if(Digits==3 || Digits==5) MyPoint=Point*10;
  
  double TheStopLoss=0;
  double TheTakeProfit=0;
  if( TotalOrdersCount()==0 ) 
  {
     int result=0;
     if((iMA(NULL,0,10,0,MODE_SMA,PRICE_CLOSE,0)>iMA(NULL,0,50,0,MODE_SMA,PRICE_CLOSE,1))) // Here is your open buy rule
     {
        result=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,MagicNumber,0,Blue);
        if(result>0)
        {
         TheStopLoss=0;
         TheTakeProfit=0;
         if(TakeProfit>0) TheTakeProfit=Ask+TakeProfit*MyPoint;
         if(StopLoss>0) TheStopLoss=Ask-StopLoss*MyPoint;
         OrderSelect(result,SELECT_BY_TICKET);
         OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(TheStopLoss,Digits),NormalizeDouble(TheTakeProfit,Digits),0,Green);
        }
        return(0);
     }
     if((iMA(NULL,0,10,0,MODE_SMA,PRICE_CLOSE,0)<iMA(NULL,0,50,0,MODE_SMA,PRICE_CLOSE,1))) // Here is your open Sell rule
     {
        result=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0,MagicNumber,0,Red);
        if(result>0)
        {
         TheStopLoss=0;
         TheTakeProfit=0;
         if(TakeProfit>0) TheTakeProfit=Bid-TakeProfit*MyPoint;
         if(StopLoss>0) TheStopLoss=Bid+StopLoss*MyPoint;
         OrderSelect(result,SELECT_BY_TICKET);
         OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(TheStopLoss,Digits),NormalizeDouble(TheTakeProfit,Digits),0,Green);
        }
        return(0);
     }
  }
  
  for(int cnt=0;cnt<OrdersTotal();cnt++)
     {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if(OrderType()<=OP_SELL &&   
         OrderSymbol()==Symbol() &&
         OrderMagicNumber()==MagicNumber 
         )  
        {
         if(OrderType()==OP_BUY)  
           {
            if(TrailingStop>0)  
              {                 
               if(Bid-OrderOpenPrice()>MyPoint*TrailingStop)
                 {
                  if(OrderStopLoss()<Bid-MyPoint*TrailingStop)
                    {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TrailingStop*MyPoint,OrderTakeProfit(),0,Green);
                     return(0);
                    }
                 }
              }
           }
         else 
           {
            if(TrailingStop>0)  
              {                 
               if((OrderOpenPrice()-Ask)>(MyPoint*TrailingStop))
                 {
                  if((OrderStopLoss()>(Ask+MyPoint*TrailingStop)) || (OrderStopLoss()==0))
                    {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Ask+MyPoint*TrailingStop,OrderTakeProfit(),0,Red);
                     return(0);
                    }
                 }
              }
           }
        }
     }
   return(0);
}

int TotalOrdersCount()
{
  int result=0;
  for(int i=0;i<OrdersTotal();i++)
  {
     OrderSelect(i,SELECT_BY_POS ,MODE_TRADES);
     if (OrderMagicNumber()==MagicNumber) result++;

   }
  return (result);
}
whroeder1
13605
whroeder1 2015.10.22 14:02  
rocktheworld: it creates endless orders
if((iMA(NULL,0,10,0,MODE_SMA,PRICE_CLOSE,0)<iMA(NULL,0,50,0,MODE_SMA,PRICE_CLOSE,1))

Of course it does.

  1. You will get multiple signals using bar zero
  2. You check for a level, not a change in signal
    bool sigCurr = iMA(NULL,0,10,0,MODE_SMA,PRICE_CLOSE,0)<iMA(NULL,0,50,0,MODE_SMA,PRICE_CLOSE,1);
    bool sigPrev = iMA(NULL,0,10,0,MODE_SMA,PRICE_CLOSE,1)<iMA(NULL,0,50,0,MODE_SMA,PRICE_CLOSE,2);
    if(sigCurr && !sigPrev)

Jack Rock
52
Jack Rock 2015.10.22 14:10  
ok, so what do I change in the code to fix this? Can you please guide me!
Demos Stogios
184
Demos Stogios 2015.10.22 21:44  
GumRai:

Interesting. I always thought that MQL4 was based on C++

Is C easier to learn?

I learned MQL4 from "The Book" and reading posts here and asking the odd question. I had no knowledge of C or ++.

I would like to learn more in order to be able to use structs and stuff in the include files etc as I simply don't understand them.

I have looked at some online resources for learning C++, but haven't made any progress. Possibly because I have been unable to find a way to incorporate C++ int mq4 immediately.

So you think that it may be worth my while checking out C? 

Hello :) If you ask me, C is ok because it is simple for the most part, I mean there is not much nonsense in there. In the other hand, I find C++ to include alot of that, but of course it is up to everyone to pick up what s/he likes. But as a fact, since C++ is "bigger" it is normally more difficult to get started with  (and I am not even sure that there is any significant knowledge to gain from that :) )  Regarding MQL, is as similar to C as C++ is; the syntax is C, which is a significant part of each language and then MQL adds or removes some parts here or there. MQL5 is even more C++ like, in the sense that it is more object oriented but MQL4 looks more like C

 best regards 

Jack Rock
52
Jack Rock 2015.10.23 14:02  

ok, since everyone on this topic is totally off the topic and now talking about code without sticking to the original issue, where can I learn this C code you are talking about?

rudy Van Eekelen
178
rudy Van Eekelen 2015.10.23 14:51  

 Change your iMA functions to look at bar 1 and 2 in stead of bar 0 and 1.

Bar 0 is the current bar and therefore changing constantly. The moving average can move up and down the other one multiple times while it is still in the make.

If you look at bar 1 and 2 this won't happen as they are already closed and "set in stone". 

 

So in stead of using: 

if((iMA(NULL,0,10,0,MODE_SMA,PRICE_CLOSE,0)<iMA(NULL,0,50,0,MODE_SMA,PRICE_CLOSE,1)) 

 

 use this:

if((iMA(NULL,0,10,0,MODE_SMA,PRICE_CLOSE,1)<iMA(NULL,0,50,0,MODE_SMA,PRICE_CLOSE,2))  

/
To add comments, please log in or register