CopyBuffer() is called in the main function.
void OnInit() { // Displayed indicator buffer: SetIndexBuffer(0,TrendBuffer,INDICATOR_DATA); ArraySetAsSeries(TrendBuffer,true); PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,EMA200); PlotIndexSetString(0,PLOT_LABEL,"FanTrendDetector"); handle18 = iMA(NULL,PERIOD_CURRENT,EMA18,0,MODE_EMA,PRICE_CLOSE); handle50 = iMA(NULL,PERIOD_CURRENT,EMA50,0,MODE_EMA,PRICE_CLOSE); handle100 = iMA(NULL,PERIOD_CURRENT,EMA100,0,MODE_EMA,PRICE_CLOSE); handle200 = iMA(NULL,PERIOD_CURRENT,EMA200,0,MODE_EMA,PRICE_CLOSE); ArraySetAsSeries(Buffer18,true); ArraySetAsSeries(Buffer50,true); ArraySetAsSeries(Buffer100,true); ArraySetAsSeries(Buffer200,true); min_rates_total=2; } //--------------------------------------------------------------------- // Need for indicator recalculation event handler: //--------------------------------------------------------------------- int OnCalculate(const int rates_total, const int prev_calculated, const datetime &time[], const double &open[], const double &high[], const double &low[], const double &close[], const long &tick_volume[], const long &volume[], const int &spread[]) { int first,bar; if(rates_total<=EMA200 || BarsCalculated(handle200)<=EMA200) return(0); int toCopy=(prev_calculated<=0)?rates_total:rates_total-prev_calculated+1; if(CopyBuffer(handle18,0,0,toCopy,Buffer18)!=toCopy || CopyBuffer(handle50,0,0,toCopy,Buffer50)!=toCopy || //Using BarsCalculated to get around Array out of bounds errors... CopyBuffer(handle100,0,0,toCopy,Buffer100)!=toCopy || CopyBuffer(handle200,0,0,toCopy,Buffer200)!=toCopy) return(0); if(prev_calculated>rates_total || prev_calculated<=0) { first=rates_total-EMA200; } else first=rates_total-prev_calculated; for(bar=first; bar>=0 && !IsStopped(); bar--) { TrendBuffer[bar]=TrendDetector(bar); } return(rates_total); }
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Can anyone help me figure out what is wrong with this Indicator that I am trying to write? I tried numerous methods for pulling the EMA moving averages but at this point, I think the problem is somewhere in the main loop of reading the candles and the buffer sizes of the EMA's. I am a heavy java programmer so I thought this would be simple, but it is proving challenging as I spent weeks trying to figure this out before posting for help. I tested the TrendDetector() function and it works as it should. I even tested all the EMA logic and the values that are being read in are correct so I can't figure out why the values in the Histogram are not correct. All it is suppose to do is compare 4 EMA moving averages and show when there is fan up or fan down on the histogram. I would like to eventually use colors for the Bullish/Bearish lines and also add in checking the trend compared to the 200EMA on two higher timeframes, which is what I started to set this up for now, but is not being used yet. I like trading bounces off any of the EMA's on the shorter timeframe, only when all three things are in alignment. Thanks for any help..