ma crossover 5min

 

Hello guys.


I have a strategy in which i am facing issues.

//+------------------------------------------------------------------+
//|                                               Indicator: macross.mq4 |
//|                                       Created with EABuilder.com |
//|                                            https://eabuilder.com |
//+------------------------------------------------------------------+
#property copyright "Created with EABuilder.com"
#property link      "https://eabuilder.com"
#property version   "1.00"
#property description ""

#include <stdlib.mqh>
#include <stderror.mqh>

//--- indicator settings
#property indicator_chart_window
#property indicator_buffers 2

#property indicator_type1 DRAW_ARROW
#property indicator_width1 1
#property indicator_color1 0xFFFFFF
#property indicator_label1 "Buy"

#property indicator_type2 DRAW_ARROW
#property indicator_width2 1
#property indicator_color2 0xFFFFFF
#property indicator_label2 "Sell"

//--- indicator buffers
double Buffer1[];
double Buffer2[];

datetime time_alert; //used when sending alert
bool Audible_Alerts = true;
bool Push_Notifications = true;
double myPoint; //initialized in OnInit

void myAlert(string type, string message)
  {
   int handle;
   if(type == "print")
      Print(message);
   else if(type == "error")
     {
      Print(type+" | macross @ "+Symbol()+","+IntegerToString(Period())+" | "+message);
     }
   else if(type == "order")
     {
     }
   else if(type == "modify")
     {
     }
   else if(type == "indicator")
     {
      Print(type+" | macross @ "+Symbol()+","+IntegerToString(Period())+" | "+message);
      if(Audible_Alerts) Alert(type+" | macross @ "+Symbol()+","+IntegerToString(Period())+" | "+message);
      handle = FileOpen("macross.txt", FILE_TXT|FILE_READ|FILE_WRITE|FILE_SHARE_READ|FILE_SHARE_WRITE, ';');
      if(handle != INVALID_HANDLE)
        {
         FileSeek(handle, 0, SEEK_END);
         FileWrite(handle, type+" | macross @ "+Symbol()+","+IntegerToString(Period())+" | "+message);
         FileClose(handle);
        }
      if(Push_Notifications) SendNotification(type+" | macross @ "+Symbol()+","+IntegerToString(Period())+" | "+message);
     }
  }

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {   
   IndicatorBuffers(2);
   SetIndexBuffer(0, Buffer1);
   SetIndexEmptyValue(0, EMPTY_VALUE);
   SetIndexArrow(0, 233);
   SetIndexBuffer(1, Buffer2);
   SetIndexEmptyValue(1, EMPTY_VALUE);
   SetIndexArrow(1, 234);
   //initialize myPoint
   myPoint = Point();
   if(Digits() == 5 || Digits() == 3)
     {
      myPoint *= 10;
     }
   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
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 - prev_calculated;
   //--- counting from 0 to rates_total
   ArraySetAsSeries(Buffer1, true);
   ArraySetAsSeries(Buffer2, true);
   //--- initial zero
   if(prev_calculated < 1)
     {
      ArrayInitialize(Buffer1, 0);
      ArrayInitialize(Buffer2, 0);
     }
   else
      limit++;
   
   //--- main loop
   for(int i = limit-1; i >= 0; i--)
     {
      if (i >= MathMin(5000-1, rates_total-1-50)) continue; //omit some old rates to prevent "Array out of range" or slow calculation   
      //Indicator Buffer 1
      if(iMA(NULL, PERIOD_M5, 4, 0, MODE_SMA, PRICE_CLOSE, iBarShift(Symbol(), PERIOD_M5, Time[i])) > iMA(NULL, PERIOD_M5, 5, 0, MODE_SMA, PRICE_CLOSE, iBarShift(Symbol(), PERIOD_M5, Time[i+1]))
      && iMA(NULL, PERIOD_M5, 4, 0, MODE_SMA, PRICE_CLOSE, iBarShift(Symbol(), PERIOD_M5, Time[i])+1) < iMA(NULL, PERIOD_M5, 5, 0, MODE_SMA, PRICE_CLOSE, iBarShift(Symbol(), PERIOD_M5, Time[i])+1) //Moving Average crosses above Moving Average
      )
        {
         Buffer1[i+1] = iLow(NULL, PERIOD_M5, 1+iBarShift(Symbol(), PERIOD_M5, Time[i])); //Set indicator value at Candlestick Low
         if(i == 1 && Time[1] != time_alert) myAlert("indicator", "Buy"); //Alert on next bar open
         time_alert = Time[1];
        }
      else
        {
         Buffer1[i] = EMPTY_VALUE;
        }
      //Indicator Buffer 2
      if(iMA(NULL, PERIOD_M5, 4, 0, MODE_SMA, PRICE_CLOSE, iBarShift(Symbol(), PERIOD_M5, Time[i])) < iMA(NULL, PERIOD_M5, 5, 0, MODE_SMA, PRICE_CLOSE, iBarShift(Symbol(), PERIOD_M5, Time[i+1]))
      && iMA(NULL, PERIOD_M5, 4, 0, MODE_SMA, PRICE_CLOSE, iBarShift(Symbol(), PERIOD_M5, Time[i])+1) > iMA(NULL, PERIOD_M5, 5, 0, MODE_SMA, PRICE_CLOSE, iBarShift(Symbol(), PERIOD_M5, Time[i])+1) //Moving Average crosses below Moving Average
      )
        {
         Buffer2[i+1] = iHigh(NULL, PERIOD_M5, 1+iBarShift(Symbol(), PERIOD_M5, Time[i])); //Set indicator value at Candlestick High
         if(i == 1 && Time[1] != time_alert) myAlert("indicator", "Sell"); //Alert on next bar open
         time_alert = Time[1];
        }
      else
        {
         Buffer2[i] = EMPTY_VALUE;
        }
     }
   return(rates_total);
  }
//+------------------------------------------------------------------+


Here is the Logic. the candle in which the ma 4ma cross 5ma show arrow on crossing candle and give alert on new bar so next candle to be taken on 4ma direction.

I have this made from free eabuilder and having issue getting alert delay.

need someone to guide. am not a coder/ Thanks and I appreciate.

 

My goal is to have


1. arrow appear on crossover during 5min candle if it cross during that and stays on same candle.

2. alert opened up if arrow stayed after crossover and as soon as new candle formed so there is no mismatch timing.

this is for only 5min candles.

 
i am having problem of getting notification in middle of running candle instead. can anyone guide ?
Reason: