// ThreeMTF_EMA.mq4 — H1, H4 and D1 EMAs on whatever chart this is attached to #property indicator_chart_window #property indicator_buffers 3 #property indicator_color1 clrGoldenrod #property indicator_color2 clrSteelBlue #property indicator_color3 clrSeaGreen input int MaPeriod = 50; input int MaMethod = MODE_EMA; input bool UseClosedBarsOnly = true; double bufH1[], bufH4[], bufD1[]; int OnInit() { SetIndexBuffer(0, bufH1); SetIndexLabel(0, "EMA H1"); SetIndexBuffer(1, bufH4); SetIndexLabel(1, "EMA H4"); SetIndexBuffer(2, bufD1); SetIndexLabel(2, "EMA D1"); return(INIT_SUCCEEDED); } double MtfMA(int tf, datetime t) { int shift = iBarShift(Symbol(), tf, t, false); if(shift < 0) return(EMPTY_VALUE); if(UseClosedBarsOnly) shift += 1; // skip the bar still forming return(iMA(Symbol(), tf, MaPeriod, 0, MaMethod, PRICE_CLOSE, shift)); } 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 limit = rates_total - MathMax(prev_calculated - 1, 1); for(int i = 0; i < limit; i++) { datetime t = Time[i]; bufH1[i] = MtfMA(PERIOD_H1, t); bufH4[i] = MtfMA(PERIOD_H4, t); bufD1[i] = MtfMA(PERIOD_D1, t); } return(rates_total); }