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:
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

- www.mql5.com
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? :-)
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 not just use _Symbol?
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 { if( // First Buy condition ( (MorningStar()==true) &&(CandleOpenTime!=iTime(_Symbol,PERIOD_M5,0)) &&(MathAbs(TrendChannelGradient)>=TrendChannelGradientReq) &&(MathAbs(QuickChannelGradient)>=QuickChannelGradientReq) &&(QuickChannelValue1<QuickChannelValue2) &&(TrendChannelValue1<TrendChannelValue2) ) ) { CloseSELL(); if(CanITrade()) { int lowV = iLowest(_Symbol,PERIOD_M5,MODE_LOW,3,0); trade.Buy(LotSize,NULL,ask,(iLow(_Symbol,PERIOD_M5,lowV)-(ATR_Array[0]*0.1*ATRMultiplier*pips)),ask+(TakePofitPips*pips),NULL); 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)) &&(MathAbs(TrendChannelGradient)>=TrendChannelGradientReq) &&(MathAbs(QuickChannelGradient)>=QuickChannelGradientReq) &&(QuickChannelValue1>QuickChannelValue2) &&(TrendChannelValue1>TrendChannelValue2) ) ) { CloseBUY(); if(CanITrade()) { int highV = iHighest(_Symbol,PERIOD_M5,MODE_HIGH,3,0); trade.Sell(LotSize,NULL,bid,(iHigh(_Symbol,PERIOD_M5,highV)+(ATR_Array[0]*0.1*ATRMultiplier*pips)),bid-(TakePofitPips*pips),NULL); CandleOpenTime = iTime(_Symbol,PERIOD_M5,0);//If it was a new candle, save the new time in CandleOpenTime } } } }
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? :-)
READ the WHOLE Documentation!
"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...READ the WHOLE Documentation!
"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!!

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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