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.
- www.mql5.com
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.
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; }
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.
Thank you for your assistance.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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: