Download MetaTrader 5
To add comments, please log in or register
Write articles, help other developers and make money
mbouali72
166
mbouali72 2014.03.02 17:49 

Hi: I am trying to code a very simple EA just by reading the documentation and trying to follow the explanations but its not working for me. The EA is not opening any trade. I just want to open a trade at specific time of the day. This is how I did it:

extern double LOT=0.01;

extern int stoploss=10;

extern int takeprofit=50;

extern bool USE_ENTRY_TIME=true;

extern int ENTRY_TIME=0; //hours from 0-23

extern int MAGIC=2014;

//+------------------------------------------------------------------+

//| Expert initialization function |

//+------------------------------------------------------------------+

int OnInit()

{

return(INIT_SUCCEEDED);

}

//+------------------------------------------------------------------+

//| Expert deinitialization function |

//+------------------------------------------------------------------+

void OnDeinit(const int reason)

{

}

//+------------------------------------------------------------------+

//| Expert tick function |

//+------------------------------------------------------------------+

void OnTick()

{

}

//+------------------------------------------------------------------+

int start()

{

//OPENING THE TRADE

if(OrdersTotal()==0 && (USE_ENTRY_TIME && Hour()>=ENTRY_TIME))

{

int ticket=OrderSend(Symbol(),OP_BUY,LOT,Ask,3,stoploss,takeprofit,MAGIC,0,Green);

}

}

//+------------------------------------------------------------------+

What I did wrong? I also get a warning message saying: "not all control paths return a value"

Can someone help me with this? Thanks a lot


Simon Gniadkowski
Moderator
17963
Simon Gniadkowski 2014.03.02 18:01  
massielpinos:

Hi: I am trying to code a very simple EA just by reading the documentation and trying to follow the explanations but its not working for me. The EA is not opening any trade. I just want to open a trade at specific time of the day. This is how I did it:

<REMOVED>


Please use the SRC button to post code . . .
Simon Gniadkowski
Moderator
17963
Simon Gniadkowski 2014.03.02 18:03  
massielpinos:

Hi: I am trying to code a very simple EA just by reading the documentation and trying to follow the explanations but its not working for me. The EA is not opening any trade. I just want to open a trade at specific time of the day. This is how I did it:


OnTick() is used instead of start() please read the documentation.
Alain Verleyen
Moderator
28558
Alain Verleyen 2014.03.02 20:04  
massielpinos:

Hi: I am trying to code a very simple EA just by reading the documentation and trying to follow the explanations but its not working for me. The EA is not opening any trade. I just want to open a trade at specific time of the day. This is how I did it:

What I did wrong? I also get a warning message saying: "not all control paths return a value"

Can someone help me with this? Thanks a lot


Welcome to mql4.com forum,

Please use the SRC button when you post code. Thank you.


This time, I edited it for you.


Tjipke de Vries
6277
Tjipke de Vries 2014.03.02 20:59  
massielpinos:

Hi: I am trying to code a very simple EA just by reading the documentation and trying to follow the explanations but its not working for me. The EA is not opening any trade. I just want to open a trade at specific time of the day. This is how I did it:

What I did wrong? I also get a warning message saying: "not all control paths return a value"

Can someone help me with this? Thanks a lot



stoploss = 10

if you buy EURUSD you have to set your orderstoploss lower then buyprice

10 is bigger

Daniel Petrovai
440
Daniel Petrovai 2014.03.03 03:40  
massielpinos:

Hi: I am trying to code a very simple EA just by reading the documentation and trying to follow the explanations but its not working for me. The EA is not opening any trade. I just want to open a trade at specific time of the day. This is how I did it:

What I did wrong? I also get a warning message saying: "not all control paths return a value"

Can someone help me with this? Thanks a lot



@massielpinos: Hi,

I've looked trough you code and there are a few things you did not understand properly :

Variables that hold your stop loss and take profit have to be the double type since they hold price which is a double.

You cannot set from the start the value of stop loss and take profit since you don't know the entry price yet.

You can only set the distance from the entry price so the actual stop and take profit HAS to be calculated

The code bellow is for educational purpose as you surely understand

Read all the comments within the code and if you still have questions let us know or pm me.

The code compiles with no errors and runs. Orders are placed including stop and take profit.

Save it and play with it. It's free.

Don't be scared to change settings, optimize, add things,etc.

You can always come back here and get a fresh copy if you messed it up. Enjoy.

Learning mql may seem quite hard at first but what isn't ?

Have fun

//+------------------------------------------------------------------+
//|                                            Proj_TimeEntry_EA.mq4 |
//|                        Copyright 2014, MetaQuotes Software Corp. |
//|                                              https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#include <stdlib.mqh> 
extern double LOT=0.01;
extern int stoploss=250;
extern int takeprofit=500;
extern bool USE_ENTRY_TIME=true;
extern int ENTRY_TIME=0;
extern int MagicNumber=2014;
extern string EaComment="TimeEntry_EA";
int myTrades=0;// will hold the value of number of orders open by this EA
int ticket;// int ticket defined as a global variable

// your settings are in points here not pips for stop and take profit
// you may want to consider adding minute and maybe day of the month
// ENTRY_TIME hours from 0-23


//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- You may want to add code here to 
// Catch bad input. One way to do it is :
   if(ENTRY_TIME>23 || ENTRY_TIME<0)// if value enterd is bigger than 23 or smaller than 0
     {
      Alert("ENTRY_TIME is too large. Please revise settings ! ");
      // maybe some other code here
     }

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   double tp=0,sl=0;
//OPENING THE TRADE
// OrdersTotal() will count all orders on all symbols not just those placed by this EA
// You may want to use a custom function  to count only this EA trades
// An easy to understand example is CountTrades() function bellow
// Use it if you want.

//if(OrdersTotal()==0 && (USE_ENTRY_TIME && Hour()>=ENTRY_TIME)) 
// Maybe you want to use it like this :
   myTrades=CountTrades();// this will count the trades placed by this EA and place that value in myTrades

   if(myTrades<1)// so if no orders are found....
     {
      // Look for entry conditions
      // Condition 1. Server hour same as ENTRY_TIME
      // you can add more conditions if you want
      if(Hour()==ENTRY_TIME)//if condition true, place order (why a buy order and not a sell?)
        {
         // you can't just use two int variable , Ask and Bid are double variables so you need to define new variables :
         sl=NormalizeDouble(Ask-(stoploss*Point),Digits);// now stoploss value is in points !
         tp=NormalizeDouble(Ask+(takeprofit*Point),Digits);// Same for taking profit. You can define variable and asign value at the same time :
         // Before you have valid stop and take profit levels you need to Normalize them 

         //Calculated stops and take profit levels may not be in the correct format for OrderSend() function
         // so if you use NormalizeDouble(), will get them to the correct format
         // You can highlite NormalizeDouble and press F1 for help
         // Now you're ready to send the order
         // A better practice is to define ticket as a global variable instead of local so it can be accessed by other functions as well.

         // int ticket=OrderSend(Symbol(),OP_BUY,LOT,Ask,3,stoploss,takeprofit, EaComment,MagicNumber,0,Green);

         ticket=OrderSend(Symbol(),OP_BUY,LOT,Ask,3,sl,tp,EaComment,MagicNumber,0,Green);

         if(ticket<0)//OrderSend will return -1 if it fails
            //  you want to know if there was an error, what was it you may want to add code as follows :
           {
            Alert("Failed to place Buy order ",ErrorDescription(GetLastError()));
            return;
           }
        }
     }

  }
//+------------------------------------------------------------------+
int CountTrades()
  {
   int count=0;
   int trade;
   for(trade=OrdersTotal();trade>=0;trade--)
     {
      OrderSelect(trade,SELECT_BY_POS,MODE_TRADES);
      if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=MagicNumber)// Not this EA orders 
         continue;
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)// If EA order , count them
         if(OrderType()==OP_SELL || OrderType()==OP_BUY)
            count++;
     }//for
   return(count);
  }
//========================================================================
Daniel Petrovai
440
Daniel Petrovai 2014.03.03 03:53  
massielpinos:

Hi: I am trying to code a very simple EA just by reading the documentation and trying to follow the explanations but its not working for me. The EA is not opening any trade. I just want to open a trade at specific time of the day. This is how I did it:

What I did wrong? I also get a warning message saying: "not all control paths return a value"

Can someone help me with this? Thanks a lot



Hello again,

There are some other people looking for the same thing and it may be useful to read this post as well.

https://www.mql5.com/en/forum/150036/page2

The EA there is a bit more complicated than the example posted before, but kinda does the thing you want among other things.

Have a look, and remember : if you have questions, ask away. Nothing to lose.

/
To add comments, please log in or register