One trade per candle on lower timeframe.

 

I have an EA that runs on the 4 hour chart and opens trade in a grid based on the new candle formation on the 15 minute timeframe.

I am trying to limit the number of trades the EA takes by only allowing a certain number of trades on the 15 minute candle from the 4 hour chart.

For example a new 15 minute bar forms and EA adds 2 trades waits for new 15 minute bar and signal and opens 2 more trades etc..

Can anyone lead me to the right direction to do this? Thank you in advance for your help.

My code for new candle formation: 

double lastClose;
bool IsNewCandle()
{
if (iClose(_Symbol,PERIOD_15,1) == lastClose){
return (false);}
if(iClose(_Symbol,PERIOD_15,1)!=lastClose){
lastClose=iClose(_Symbol,gttimeframe,1);
return(true);}
return(false);
}
 
Digitals113:

I have an EA that runs on the 4 hour chart and opens trade in a grid based on the new candle formation on the 15 minute timeframe.

I am trying to limit the number of trades the EA takes by only allowing a certain number of trades on the 15 minute candle from the 4 hour chart.

For example a new 15 minute bar forms and EA adds 2 trades waits for new 15 minute bar and signal and opens 2 more trades etc..

Can anyone lead me to the right direction to do this? Thank you in advance for your help.

My code for new candle formation: 

Consider using time (i.e. iTime) instead of iClose, because nothing stops consecutive candles from having the same close price, however rare.

Also consider using https://www.mql5.com/en/docs/globals to store the lastTime instead, as you need it to 'remember' the last candle even after a crash, or shutdown/restart.

Documentation on MQL5: Global Variables of the Terminal
Documentation on MQL5: Global Variables of the Terminal
  • www.mql5.com
Global variables are kept in the client terminal for 4 weeks since the last access, then they will be deleted automatically. An access to a global variable is not only setting of a new value, but reading of the global variable value, as well.
 
Digitals113:

I have an EA that runs on the 4 hour chart and opens trade in a grid based on the new candle formation on the 15 minute timeframe.

I am trying to limit the number of trades the EA takes by only allowing a certain number of trades on the 15 minute candle from the 4 hour chart.

For example a new 15 minute bar forms and EA adds 2 trades waits for new 15 minute bar and signal and opens 2 more trades etc..

Can anyone lead me to the right direction to do this? Thank you in advance for your help.

My code for new candle formation: 

You could look at using an array to save, say,  the ticket #'s. That way you could access the number of array items to give you the total orders, and only allow to your max.
 
andrew4789:
You could look at using an array to save, say,  the ticket #'s. That way you could access the number of array items to give you the total orders, and only allow to your max.
int NumberofOrdersonCandle(){
   datetime Ordertime=iTime(Symbol(),PERIOD_M15,1);
   int EarliestSellTicket = -1;
   for(int pos = OrdersTotal()-1; pos >= 0 ; pos--)
   {
      if (!OrderSelect(pos,SELECT_BY_POS))return (-1);
      if (OrderSymbol()==Symbol() && OrderOpenTime()==Ordertime)
      
       EarliestSellTicket = OrdersTotal();

        }
        return(EarliestSellTicket);
}

datetime lastClose;
bool IsNewCandle()
{
if (iTime(_Symbol,PERIOD_M15,1) == lastClose){
return (false);}
if(iTime(_Symbol,PERIOD_M15,1)!=lastClose){
lastClose=iTime(_Symbol,PERIOD_M15,1);
return(true);}
return(false);
}

if(IsNewCandle() && NumberofOrdersonCandle()<2)
{
entry code
}

This is the code I came up with, unsure if its correct.

 

Something like this

bool IsNewCandle(void)
  {
   static datetime t_bar=iTime(_Symbol,PERIOD_15,0);
   datetime time=iTime(_Symbol,PERIOD_15,0);
//---
   if(t_bar==time)
      return false;
   t_bar=time;
//---
   return true;
  }
 
For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart,) volume is unreliable (miss ticks,) Price is unreliable (duplicate prices and The == operand. - MQL4 and MetaTrader 4 - MQL4 programming forum.) Always use time.
I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
          New candle - MQL4 and MetaTrader 4 - MQL4 programming forum
 
bool Candletime()
{
datetime lastClose = iTime(_Symbol,PERIOD_M15,0);

if(TimeMinute(TimeCurrent())==TimeMinute(lastClose))
if(TimeMinute(NumberofOrdersonCandle())==TimeMinute(lastClose))
return true;
//---
return false;
}

datetime NumberofOrdersonCandle(){
   datetime Ordertime=iTime(Symbol(),PERIOD_M15,1);
   static datetime EarliestSellTicket = -1;
   for(int pos = OrdersTotal()-1; pos >= 0 ; pos--)
   {
      if (!OrderSelect(pos,SELECT_BY_POS))return (-1);
      if (OrderSymbol()==Symbol() && OrderOpenTime()==Ordertime)
      
       EarliestSellTicket = OrderOpenTime();

        }
        return(EarliestSellTicket);
}
 
William Roeder: The main thing for him to understand the principle. And how to work with it? He will decide.
 
Konstantin Nikitin:
William Roeder: The main thing for him to understand the principle. And how to work with it? He will decide.

Thank you for your assistance. 

 
Konstantin Nikitin #:

Something like this

Thank you!