Download MetaTrader 5

Order at a specific Time: Hour and Minute - Help

To add comments, please log in or register
Launch agents on an idle PC. It is profitable!
ZaraLonga
16
ZaraLonga 2016.06.06 19:26 

Hello guys,

I am new to MQL4 and trying to self learn it.

The purpose of the code below would be to insert an order at a specific Hour and Minute of the day if certain conditions are met (Price above or below a certain level). The order details would vary in its S/L or T/P or entry price according if it's a BUY or a SELL one.

I have 2 types of problems:

- I get 6 warnings of 2 types:

  1. "possible use of uninitialized variable" (highlited in green below) -> It's inside my ticket variable definition but have no idea why
  2. "expression has no effect" (highlighted in yellow below) -> it's inside the last IF statements nested inside the other IFs

- If I test the code by mean of the Tester it doesn't seem reading the time conditions and it only puts BUY orders

Any of you guys has already encountered these kind of problems and could help me?

Thank you very much!!
F

===================================================== 

extern double DataFrequency = 1440;


extern double Lots = 2.1;              

extern double TakePips = 55.9;

extern double StopPips = 99.4;            

extern int Slippage=3;


extern int Hour_Apertura =17;

extern int Minute_Apertura =35;


extern string Buy_Sell = "SELL";    

extern double Price_Level = 1.45;


void OnTick(void)

{  

   int Sign_Order;

   double Book_Entry, Book_Exit;

   double Price_Stop, Price_Take;

      

      if(Buy_Sell=="BUY")

       {

       Sign_Order = 0;     //0 = OP_BUY 

       Book_Entry = Ask;

       Book_Exit = Bid;

       Price_Stop = Book_Entry - StopPips * Point;

       Price_Take = Book_Entry + TakePips * Point;

       }      

      else if(Buy_Sell=="SELL")

       {

       Sign_Order = 1;     //1 = OP_SELL 

       Book_Entry = Bid;

       Book_Exit = Ask;

       Price_Stop = Book_Entry+StopPips*Point;

       Price_Take = Book_Entry-TakePips*Point;

       } 


   int total, ticket,Hour_Int, Minute_Int;

   

   total = OrdersTotal();

   ticket= OrderSend(Symbol(),Sign_Order,Lots,Book_Entry,Slippage,Price_Stop,Price_Take,"",0,0,Orange);

   

   Hour_Int = Hour();

   Minute_Int = Minute();

   

   if(Hour_Int == Hour_Apertura && Minute_Int == Minute_Apertura)

   {//Print("Time Conditions are satisfied");

      if(total<1)

      {//Print("There are no orders open"); 

         if(AccountFreeMargin()<(1000*Lots))

         {//Print("There are no funds"); 

            if(Buy_Sell=="BUY")

            { 

               if(Ask>=Price_Level) 

                  ticket; 

               else

               Print("Signal not confirmed"); 

            }

            else if(Buy_Sell=="SELL")

            { 

               if(Bid<=Price_Level) 

               ticket;    

               else

               Print("Signal not confirmed"); 

            }

         }

         

      }


   }              

   else

   Print("Conditions are not satisfied");

 }

======================================================= 

Biantoro Kunarto
14899
Biantoro Kunarto 2016.06.07 14:56  

Please check this code:

input double DataFrequency = 1440;
input double Lots = 2.1;              
input double TakePips = 55.9;
input double StopPips = 99.4;            
input int Slippage=3;
input int Hour_Apertura =17;
input int Minute_Apertura =35;
input string Buy_Sell = "SELL";    
input double Price_Level = 1.45;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   int Sign_Order = -1;
   double Book_Entry = 0, Book_Exit = 0;
   double Price_Stop = 0, Price_Take = 0;
      if(Buy_Sell=="BUY")
       {
       Sign_Order = 0;     //0 = OP_BUY 
       Book_Entry = Ask;
       Book_Exit = Bid;
       Price_Stop = Book_Entry - StopPips * Point;
       Price_Take = Book_Entry + TakePips * Point;
       }      
      else if(Buy_Sell=="SELL")
       {
       Sign_Order = 1;     //1 = OP_SELL 
       Book_Entry = Bid;
       Book_Exit = Ask;
       Price_Stop = Book_Entry+StopPips*Point;
       Price_Take = Book_Entry-TakePips*Point;
       } 
       
   int total, ticket,Hour_Int, Minute_Int;
   total = OrdersTotal();
   Hour_Int = Hour();
   Minute_Int = Minute();
   if(Hour_Int == Hour_Apertura && Minute_Int == Minute_Apertura)
   {//Print("Time Conditions are satisfied");
      if(total<1)
      {//Print("There are no orders open"); 
         if(AccountFreeMargin()<(1000*Lots))
         {//Print("There are no funds"); 
            if(Buy_Sell=="BUY")
            { 
               if(Ask>=Price_Level) 
                  ticket= OrderSend(Symbol(),Sign_Order,Lots,Book_Entry,Slippage,Price_Stop,Price_Take,"",0,0,Orange); 
               else
               Print("Signal not confirmed"); 
            }
            else if(Buy_Sell=="SELL")
            { 
               if(Bid<=Price_Level) 
               ticket= OrderSend(Symbol(),Sign_Order,Lots,Book_Entry,Slippage,Price_Stop,Price_Take,"",0,0,Orange);    
               else
               Print("Signal not confirmed"); 
            }
         }         
      }
   }              
   else
   Print("Conditions are not satisfied");
  }
Rizal Dwi Satria
332
Rizal Dwi Satria 2016.06.07 17:29  
you need to limit trade or it spam trades .
ZaraLonga
16
ZaraLonga 2016.06.08 13:04  

biantoro kunarto: tested the code you suggested and I do not get the errors any more, thank you. 

So, in order to understand what I was missing:

- I had to initialize and deinitialize the EA - may I ask you the purpose of this part?

- I had to initialize my variables before attributing them values inside IFs

Rizal Dwi Satria: how do you limit trades?

@ both: The other problem I had when launching the test procedure still persists. With the corrected code above now the back testing  does not open any position at all. I tried for instance to test the period  3-23 of May 2016 with "input string Buy_Sell = "BUY";" it should open 4 orders. Do you have any idea what I am missing?

 Tester

 

Thank you very much,
F

Biantoro Kunarto
14899
Biantoro Kunarto 2016.06.09 03:32  

Please change this :

if(AccountFreeMargin()<(1000*Lots))

to this :

if(AccountFreeMargin()>(1000*Lots))
Biantoro Kunarto
14899
Biantoro Kunarto 2016.06.09 03:33  

So all code will be like this :

input double DataFrequency = 1440;
input double Lots = 2.1;              
input double TakePips = 55.9;
input double StopPips = 99.4;            
input int Slippage=3;
input int Hour_Apertura =17;
input int Minute_Apertura =35;
input string Buy_Sell = "SELL";    
input double Price_Level = 1.45;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   int Sign_Order = -1;
   double Book_Entry = 0, Book_Exit = 0;
   double Price_Stop = 0, Price_Take = 0;
      if(Buy_Sell=="BUY")
       {
       Sign_Order = 0;     //0 = OP_BUY 
       Book_Entry = Ask;
       Book_Exit = Bid;
       Price_Stop = Book_Entry - StopPips * Point;
       Price_Take = Book_Entry + TakePips * Point;
       }      
      else if(Buy_Sell=="SELL")
       {
       Sign_Order = 1;     //1 = OP_SELL 
       Book_Entry = Bid;
       Book_Exit = Ask;
       Price_Stop = Book_Entry+StopPips*Point;
       Price_Take = Book_Entry-TakePips*Point;
       } 
       
   int total, ticket,Hour_Int, Minute_Int;
   total = OrdersTotal();
   Hour_Int = Hour();
   Minute_Int = Minute();
   if(Hour_Int == Hour_Apertura && Minute_Int == Minute_Apertura)
   {//Print("Time Conditions are satisfied");
      if(total<1)
      {//Print("There are no orders open"); 
         if(AccountFreeMargin()>(1000*Lots))
         {//Print("There are no funds"); 
            if(Buy_Sell=="BUY")
            { 
               if(Ask>=Price_Level) 
                  ticket= OrderSend(Symbol(),Sign_Order,Lots,Book_Entry,Slippage,Price_Stop,Price_Take,"",0,0,Orange); 
               else
               Print("Signal not confirmed"); 
            }
            else if(Buy_Sell=="SELL")
            { 
               if(Bid<=Price_Level) 
               ticket= OrderSend(Symbol(),Sign_Order,Lots,Book_Entry,Slippage,Price_Stop,Price_Take,"",0,0,Orange);    
               else
               Print("Signal not confirmed"); 
            }
         }         
      }
   }              
   else
   Print("Conditions are not satisfied");
  }
Biantoro Kunarto
14899
Biantoro Kunarto 2016.06.09 03:36  

About Initialization or Deinitialization, you can read this :

OnInit

The OnInit() function is the Init event handler. It must be of void or int type, with no parameters:

void OnInit();

The Init event is generated immediately after an Expert Advisor or an indicator is downloaded; this event is not generated for scripts. The OnInit() function is used for initialization. If OnInit() has the int type of the return value, the non-zero return code means unsuccessful initialization, and it generates the Deinit event with the code of deinitialization reason REASON_INITFAILED.

To optimize input parameters of an Expert Advisor, it is recommended to use values of the ENUM_INIT_RETCODE enumeration as the return code. These values are used for organizing the course of optimization, including the selection of the most appropriate testing agents. During initialization of an Expert Advisor before the start of testing you can request information about the configuration and resources of an agent (the number of cores, amount of free memory, etc.) using the TerminalInfoInteger() function. Based on the information obtained, you can either allow to use this testing agent, or reject using it during the optimization of this Expert Advisor.

ENUM_INIT_RETCODE

Identifier

Description

INIT_SUCCEEDED

Successful initialization, testing of the Expert Advisor can be continued.

This code means the same as a null value — the Expert Advisor has been successfully initialized in the tester.

INIT_FAILED

Initialization failed; there is no point in continuing testing because of fatal errors. For example, failed to create an indicator that is required for the work of the Expert Advisor.

This return value means the same as a value other than zero - initialization of the Expert Advisor in the tester failed.

INIT_PARAMETERS_INCORRECT

This value means the incorrect set of input parameters. The result string containing this return code is highlighted in red in the general optimization table.

Testing for the given set of parameters of the Expert Advisor will not be executed, the agent is free to receive a new task.

Upon receiving this value, the strategy tester will reliably not pass this task to other agents for retry.

INIT_AGENT_NOT_SUITABLE

No errors during initialization, but for some reason the agent is not suitable for testing. For example, not enough memory, no OpenCL support, etc.

After the return of this code, the agent will not receive tasks until the end of this optimization.

The OnInit() function of the void type always denotes successful initialization.

OnDeinit

The OnDeinit() function is called during deinitialization and is the Deinit event handler. It must be declared as the void type and should have one parameter of the const int type, which contains the code of deinitialization reason. If a different type is declared, the compiler will generate a warning, but the function will not be called. For scripts the Deinit event is not generated and therefore the OnDeinit() function can't be used in scripts.

void OnDeinit(const int reason);

The Deinit event is generated for Expert Advisors and indicators in the following cases:

before reinitialization due to the change of a symbol or chart period, to which the mql5 program is attached;

before reinitialization due to the change of input parameters;

before unloading the mql5 program.

Biantoro Kunarto
14899
Biantoro Kunarto 2016.06.09 03:36  
Or read this link
ZaraLonga
16
ZaraLonga 2016.06.09 11:37  

All solved and understood!! Thank you very much biantoro kunarto!! Really appreciated 

Biantoro Kunarto
14899
Biantoro Kunarto 2016.06.09 14:27  
you're welcome
To add comments, please log in or register