Moving Average
Forum on trading, automated trading systems and testing trading strategies
My EA is firing off multiple trades instead of 1 per candle.
Vladimir Karputov, 2022.06.28 09:56
You made a huge mistake - you create several handles on each tick! Remember: according to the MQL5 style, the indicator handle should be created ONCE and it should be done in OnInit.
void OnInit() { MA_handle=iMA(_Symbol,PERIOD_M5,28,0,MODE_EMA,PRICE_CLOSE); CopyBuffer(MA_handle,0,0,100,MA); ArraySetAsSeries(MA,true); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void OnTick() { open = iOpen(NULL,0,0); close = iClose(_Symbol,PERIOD_CURRENT,0); Ask = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits); Bid = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits); high = iHigh(Symbol(),Period(),0); low = iLow(Symbol(),Period(),0); double TakeProfit = 250; double StopLoss = 100; double TakeProfitLevel = Bid + TakeProfit*Point(); //0.0001 // Take Profit value defined double StopLossLevel = Bid - StopLoss*Point(); static datetime dtBarCurrent = WRONG_VALUE; datetime dtBarPrevious = dtBarCurrent; dtBarCurrent = (datetime) SeriesInfoInteger(_Symbol, _Period, SERIES_LASTBAR_DATE); bool boolNewBarFlag = (dtBarCurrent != dtBarPrevious); if(open<MA[0] && Ask > MA[0] && boolNewBarFlag) { m_trade.Buy(0.1,_Symbol,0,StopLossLevel,TakeProfitLevel,NULL); } if(open>MA[0] && Bid < MA[0] && boolNewBarFlag) { m_trade.Sell(0.1,_Symbol,0,StopLossLevel,TakeProfitLevel,NULL); } }If I understood you correctly this was what was meant to be done however, now boolNewBarFlag seems to be always False
I stand corrected
this make more sense, however it seems open logic still has some flows as it opens then on the same bar as the bar crossing
void OnInit() { MA_handle=iMA(_Symbol,PERIOD_M5,28,0,MODE_EMA,PRICE_CLOSE); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void OnTick() { CopyBuffer(MA_handle,0,0,100,MA); ArraySetAsSeries(MA,true); open = iOpen(NULL,0,0); close = iClose(_Symbol,PERIOD_CURRENT,0); Ask = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits); Bid = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits); high = iHigh(Symbol(),Period(),0); low = iLow(Symbol(),Period(),0); double TakeProfit = 250; double StopLoss = 100; double TakeProfitLevel = Bid + TakeProfit*Point(); //0.0001 // Take Profit value defined double StopLossLevel = Bid - StopLoss*Point(); static datetime dtBarCurrent = WRONG_VALUE; datetime dtBarPrevious = dtBarCurrent; dtBarCurrent = (datetime) SeriesInfoInteger(_Symbol, _Period, SERIES_LASTBAR_DATE); bool boolNewBarFlag = (dtBarCurrent != dtBarPrevious); if(open<MA[0] && Ask > MA[0] && boolNewBarFlag) { m_trade.Buy(0.1,_Symbol,0,StopLossLevel,TakeProfitLevel,NULL); } if(open>MA[0] && Bid < MA[0] && boolNewBarFlag) { m_trade.Sell(0.1,_Symbol,0,StopLossLevel,TakeProfitLevel,NULL); } }
Some of them seem completely random like those sells
CopyBuffer(MA_handle,0,0,100,MA); ArraySetAsSeries(MA,true);
Try deciding how you want the buffer filled before filling it.
CopyBuffer - Timeseries and Indicators Access - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
CTrade m_trade; //--- double MA[]; // array for iMA indicator values //---- handles for indicators int MA_handle; datetime lastClose; double open; double close; double Ask; double Bid; double high; double low; double TakeProfit; // trading object //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ 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); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void OnInit() { MA_handle=iMA(_Symbol,PERIOD_M5,28,0,MODE_EMA,PRICE_CLOSE); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void OnTick() { CopyBuffer(MA_handle,0,0,28,MA); ArraySetAsSeries(MA,true); open = iOpen(NULL,0,0); close = iClose(_Symbol,PERIOD_CURRENT,0); Ask = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits); Bid = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits); high = iHigh(Symbol(),Period(),0); low = iLow(Symbol(),Period(),0); double TakeProfit = 250; double StopLoss = 100; double TakeProfitLevelBUY = Ask + TakeProfit*Point(); double StopLossLevelBUY = Ask - StopLoss*Point(); double TakeProfitLevelSELL = Bid - TakeProfit*Point(); double StopLossLevelSELL = Bid + StopLoss*Point(); static datetime dtBarCurrent = WRONG_VALUE; datetime dtBarPrevious = dtBarCurrent; dtBarCurrent = (datetime) SeriesInfoInteger(_Symbol, _Period, SERIES_LASTBAR_DATE); bool boolNewBarFlag = (dtBarCurrent != dtBarPrevious); if(open<MA[27] && Ask > MA[27] && boolNewBarFlag) { m_trade.Buy(0.1,_Symbol,0,StopLossLevelBUY,TakeProfitLevelBUY,NULL); } if(open>MA[27] && Bid < MA[27] && boolNewBarFlag) { m_trade.Sell(0.1,_Symbol,0,StopLossLevelSELL,TakeProfitLevelSELL,NULL); } }Something still off with the BUY and Sell is never placed :/
Just have a look at the example EAs on you pc: ...\MQL5\Experts\Examples\MACD and \MQL5\Experts\Examples\Moving Average\
There you find good ways to do it - it saves you a lot of time!

- Free trading apps
- Free Forex VPS for 24 hours
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi what I am trying to acomplish is to detect on 28 peroid MovingAverage if the bar crossed it then for the next bar if it opened also on the same side as the crossing bar I want to place either Buy Or Sell. However, It seems to do random things and I cannot pinpoint what I am doing wrong.
Any help appreciated.
so Example;
if current bar all of the sudden goes from below MA to above we have one True Condition then we see that Ask price is also above since the current bar did not open there that is going to be false one but the next one should True. Now I need to wait for the next bar so I don't end up with tons of Buys so I need to wait for the next bar so it's a one time event.
Thanks in advance :)