# Limiting number of trades per candle

Good day,

I am trying to limit the number of trades per candle to 1. I am using a method that has previously worked for me but I can't figure out why it doesn't work in the function below:

```void BuyAndSell()
{
static int CandleOpenTime;
if((CandleOpenTime==iTime("XX",PERIOD_M5,0)))
{
Print("It is NOT a new candle");
return;
}
else

{

(
(MorningStar()==true)
&&(CandleOpenTime!=iTime("XX",PERIOD_M5,0))
&&(QuickChannelValue1<QuickChannelValue2)
&&(TrendChannelValue1<TrendChannelValue2)
)
)

{
CloseSELL();
{
int lowV = iLowest("XX",PERIOD_M5,MODE_LOW,3,0);
CandleOpenTime = iTime("XX",PERIOD_M5,0);//If it was a new candle, save the new time in CandleOpenTime
}

}

//+------------------------------------------------------------------+
//|                    Sell Conditions                               |
//+------------------------------------------------------------------+

if(//First Sell condition
(
(EveningStar()==true)
&&(CandleOpenTime!=iTime("XX",PERIOD_M5,0))
&&(QuickChannelValue1>QuickChannelValue2)
&&(TrendChannelValue1>TrendChannelValue2)
)
)

{
{
int highV = iHighest("XX",PERIOD_M5,MODE_HIGH,3,0);
CandleOpenTime = iTime("XX",PERIOD_M5,0);//If it was a new candle, save the new time in CandleOpenTime
}
}

}

}```

I am calling void BuyAndSell () in void OnTick ().

Any constructive help will be appreciated

Jan-HarmZ63:

Good day,

I am trying to limit the number of trades per candle to 1. I am using a method that has previously worked for me but I can't figure out why it doesn't work in the function below:

I am calling void BuyAndSell () in void OnTick ().

Any constructive help will be appreciated

If you read carefully the documentation of iTime() you will find the reason of the failure of your code...

;)

https://www.mql5.com/en/docs/series/itime

Documentation on MQL5: Timeseries and Indicators Access / iTime
• www.mql5.com
iTime - Timeseries and Indicators Access - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5

Flavio Jarabeck:

If you read carefully the documentation of iTime() you will find the reason of the failure of your code...

;)

https://www.mql5.com/en/docs/series/itime

Hi Flavio,

Thanks for the reply. Are you referring to the fact that I saved the time to an "int" and not "datetime"?

If not can you please spoon feed me a little? :-)

Jan-HarmZ63:

Good day,

I am trying to limit the number of trades per candle to 1. I am using a method that has previously worked for me but I can't figure out why it doesn't work in the function below:

I am calling void BuyAndSell () in void OnTick ().

Any constructive help will be appreciated

Why 'XX' in your iTime? Why are you not including the symbol?

Joe Varcoe:
Why 'XX' in your iTime? Why are you not including the symbol?

I just removed the Symbol name and added "XX" as a placeholder for this post.

Sorry for the confusion.

Jan-HarmZ63: I just removed the Symbol name and added "XX" as a placeholder for this post.

Why not just use _Symbol?

William Roeder:

Why not just use _Symbol?

Sorry for the confusion.

Any idea what could be wrong with the functions behind CandleOpenTime and trying to to limit trades to 1 per candle?

Did you look up the return value of iTime() yet ?

```datetime  iTime(
string           symbol,          // symbol
int              timeframe,       // timeframe
int              shift            // shift
);```

Re-posting code to clear the confusion created on my part. Sorry for that

```void BuyAndSell()
{
static datetime CandleOpenTime;
Print(
"Candle Open Time is: " + CandleOpenTime
+ "\n" + "iTime is: " + " " + iTime(_Symbol,PERIOD_M5,0)
);

if((CandleOpenTime==iTime(_Symbol,PERIOD_M5,0)))
{
Print("It is NOT a new candle");
return;
}
else

{

(
(MorningStar()==true)
&&(CandleOpenTime!=iTime(_Symbol,PERIOD_M5,0))
&&(QuickChannelValue1<QuickChannelValue2)
&&(TrendChannelValue1<TrendChannelValue2)
)
)

{
CloseSELL();
{
int lowV = iLowest(_Symbol,PERIOD_M5,MODE_LOW,3,0);
CandleOpenTime = iTime(_Symbol,PERIOD_M5,0);//If it was a new candle, save the new time in CandleOpenTime
}

}

//+------------------------------------------------------------------+
//|                    Sell Conditions                               |
//+------------------------------------------------------------------+

if(//First Sell condition
(
(EveningStar()==true)
&&(CandleOpenTime!=iTime(_Symbol,PERIOD_M5,0))
&&(QuickChannelValue1>QuickChannelValue2)
&&(TrendChannelValue1>TrendChannelValue2)
)
)

{
{
int highV = iHighest(_Symbol,PERIOD_M5,MODE_HIGH,3,0);
CandleOpenTime = iTime(_Symbol,PERIOD_M5,0);//If it was a new candle, save the new time in CandleOpenTime
}
}

}

}```

Jan-HarmZ63:

Hi Flavio,

Thanks for the reply. Are you referring to the fact that I saved the time to an "int" and not "datetime"?

If not can you please spoon feed me a little? :-)

"The function always returns actual data. For this purpose it performs a request to the timeseries for the specified symbol/period during each call. This means that if there is no ready data during the first function call, some time may be taken to prepare the result.

The function does not store previous calls results, and there is no local cache for quick value return."

Probably you are getting Random fails... using your code like this...

Flavio Jarabeck:

"The function always returns actual data. For this purpose it performs a request to the timeseries for the specified symbol/period during each call. This means that if there is no ready data during the first function call, some time may be taken to prepare the result.

The function does not store previous calls results, and there is no local cache for quick value return."

Probably you are getting Random fails... using your code like this...

Hi Flavio,

Thanks for this. You are hitting the nail on the head. Probably also why the problem seems to occur more frequent when running a strategy test.

Thank you!!

Reason: