Simple buy order based on three consecutive ascending close prices

 

Hello,

We want to create an EA for back-testing purposes only.
We are trying to make a simple code with the following criteria: It should send a buy order when there have been three consecutive bars with ascending closing prices in all three of them, altogether.

The code we have is:

//+------------------------------------------------------------------+
//|                                                  BackToBasic.mq4 |
//|                                                             MDAM |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "MDAM"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict


//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+



int StopLossLevel = 10;
int TakeProfitLevel = 10;
int i;
double ClosePrice_0=Close[0];
double ClosePrice_1=Close[3];

int init()
{

if(ClosePrice_0<ClosePrice_1)

{
   OrderSend(Symbol(), OP_BUY, 1.0, Ask, 10, StopLossLevel, TakeProfitLevel, NULL);
 
   }
   return(0);
}


When we back-testing this EA, it doesn't buy anything at all. We don't get any errors as well, and the compilation is successful.

Can you please help us - what are we doing wrong here?

Thanks in advance.

 
MartinDalskov:

Hello,

We want to create an EA for back-testing purposes only.
We are trying to make a simple code with the following criteria: It should send a buy order when there have been three consecutive bars with ascending closing prices in all three of them, altogether.

The code we have is:

When we back-testing this EA, it doesn't buy anything at all. We don't get any errors as well, and the compilation is successful.

Can you please help us - what are we doing wrong here?

Thanks in advance.

Your approach is completely wrong and outdated as well. Listing and explaining what is wrong would be a very lengthy discussion, so I suggest that you first do some research, by reading the following and looking at example code:

  • Read the online Book which is a kind of tutorial and which has some examples of code. However, this is an old tutorial which uses the old style of code, so you should also have a look at the newer style which is explained in the section Updated MQL4 of the documentation.
  • Consult the main Documentation for reference, especially the sections Program Running and Client Terminal Events.
  • You should also look at example EA code, such as the samples provided by MetaQuotes (such as "MACD Sample.mq4" and "Moving Average.mq4"), but also the many, many examples in the Code Base.
  • There are also many Articles that explain particular aspects of developing an EA.
 
MartinDalskov:

Hello,

We want to create an EA for back-testing purposes only.
We are trying to make a simple code with the following criteria: It should send a buy order when there have been three consecutive bars with ascending closing prices in all three of them, altogether.

The code we have is:


When we back-testing this EA, it doesn't buy anything at all. We don't get any errors as well, and the compilation is successful.

Can you please help us - what are we doing wrong here?

Thanks in advance.

You use

#property strict

so when you compile, you should get a warning that the return from OrderSend should be checked. If you had checked, you would probably get "Invalid stops."

You cannot send an order with SL 10 and TP 10. They must be the entry price + or - a value.

It is not a good idea to send orders from init, price values etc may not be updated yet after starting MT4.


double ClosePrice_0=Close[0];
double ClosePrice_1=Close[3];

doesn't make sense, giving the variable ClosePrice_1 the value of Close[3] can be confusing and in more complicated code may make finding errors more difficult.

Why use internediate variables amyway? Simply use....

if(Close[0]<Close[1])

bt anyway, that is not what you said that you are trying to do

It should send a buy order when there have been three consecutive bars with ascending closing prices

 
GumRai: You use ... so when you compile, you should get a warning that the return from OrderSend should be checked. If you had checked, you would probably get "Invalid stops."You cannot send an order with SL 10 and TP 10. They must be the entry price + or - a value. It is not a good idea to send orders from init, price values etc may not be updated yet after starting MT4. doesn't make sense, giving the variable ClosePrice_1 the value of Close[3] can be confusing and in more complicated code may make finding errors more difficult. Why use internediate variables amyway? Simply use.... bt anyway, that is not what you said that you are trying to do

@GumRai. At this point, it is not worth specifying all the invalid points, because the OP still needs to learn the basics first (e.g. currently has all his code in the "init()" event handler instead of the "start()" or the newer "OnTick()" ).

All that can be said, such as what you have already stated as being wrong, is probable not going to be understood by the OP at this time. He first needs to understand the basic structure of an EA in MQL, and for that he should follow the tutorial and study some example code. Only after that, will he be able to understand any of the faults that we may list here.

 
MartinDalskov:

Hello,

We want to create an EA for back-testing purposes only.
We are trying to make a simple code with the following criteria: It should send a buy order when there have been three consecutive bars with ascending closing prices in all three of them, altogether.

The code we have is:


When we back-testing this EA, it doesn't buy anything at all. We don't get any errors as well, and the compilation is successful.

Can you please help us - what are we doing wrong here?

Thanks in advance.

/*-----------------------------------------------------------------------------------------------*/
      //Broker Digits
      TickSize=MarketInfo(Symbol(),MODE_TICKSIZE);
      double Pips;
      if(TickSize==0.00001 || TickSize==0.001)
         Pips=TickSize*10;
      else Pips=TickSize;

      double Place_BuyOrder;
      double Place_SellOrder;
      int StopLossLevel=10;
      int TakeProfitLevel=10;
        {

         if(Close[1]<Close[2])
            if(Close[2]<Close[3])
              {
               Place_BuyOrder=OrderSend(Symbol(),OP_BUY,1.0,Ask,0,Ask-(StopLossLevel*Pips),Ask+(TakeProfitLevel*Pips),"",0);
               if(Place_BuyOrder<0){Alert(GetLastError());}
              }
         if(Close[1]>Close[2])
            if(Close[2]>Close[3])
              {
               Place_SellOrder=OrderSend(Symbol(),OP_SELL,1.0,Bid,0,Bid+(StopLossLevel*Pips),Bid-(TakeProfitLevel*Pips),"",0);
               if(Place_SellOrder<0){Alert(GetLastError());}
              }
        }
/*-------------------------------------------------------------------------------------------------*/

is this what you are looking for?

Places a buy order when you have 3 descending close prices and places a sell order after 3 ascending close prices.