MT5 indicator code error: need help fixing it

 

Hi guys,

Am trying to fix this code but was giving me some errors, wouldn't mind your help fixing it

//+------------------------------------------------------------------+
//|                                             SBD Vix Lockdown.mq5 |
//|                              Copyright 2020, SoftBlues Digitals. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, SoftBlues Digitals."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots 2

#property indicator_type1 DRAW_ARROW
#property indicator_width1 3
#property indicator_color1 0xFF7300
#property indicator_label1 "BUY"

#property indicator_type2 DRAW_ARROW
#property indicator_width2 3
#property indicator_color2 0x4800FF
#property indicator_label2 "SELL"

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

input int ATRPeriod1 = 14;
input int Indicator_Shift1 = -3;
input int Indicator_Shift2 = 0;
datetime time_alert; //Instant alert, only once per bar
input bool Send_Email = true;
input bool Audible_Alerts = true;
input bool Push_Notifications = true;
int DEMA_handle;
double DEMA[];
int DEMA_handle2;
double DEMA2[];
double Low[];
int ATR_handle;
double ATR[];
double High[];


void myAlert(string type, string message)
  {
   if(type == "print")
      Print(message);
   else if(type == "error")
     {
      Print(type+" | SBD Vix Lockdown @ "+Symbol()+","+EnumToString(Period())+" | "+message);
     }
   else if(type == "order")
     {
     }
   else if(type == "modify")
     {
     }
   else if(type == "Signal")
     {
      if(Audible_Alerts) Alert(type+" | SBD Vix Lockdown @ "+Symbol()+","+EnumToString(Period())+" | "+message);
      if(Send_Email) SendMail("SBD Vix Lockdown", type+" | SBD Vix Lockdown @ "+Symbol()+","+EnumToString(Period())+" | "+message);
      if(Push_Notifications) SendNotification(type+" | SBD Vix Lockdown @ "+Symbol()+","+EnumToString(Period())+" | "+message);
     }
  }



//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {   
   SetIndexBuffer(0, Buffer1);
   PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, EMPTY_VALUE);
   PlotIndexSetInteger(0, PLOT_ARROW, 241);
   SetIndexBuffer(1, Buffer2);
   PlotIndexSetDouble(1, PLOT_EMPTY_VALUE, EMPTY_VALUE);
   PlotIndexSetInteger(1, PLOT_ARROW, 242);
   DEMA_handle = iDEMA(NULL, PERIOD_CURRENT, 25, 0, PRICE_OPEN);
   if(DEMA_handle < 0)
     {
      Print("The creation of iDEMA has failed: DEMA_handle=", INVALID_HANDLE);
      Print("Runtime error = ", GetLastError());
      return(INIT_FAILED);
     }
   
   DEMA_handle2 = iDEMA(NULL, PERIOD_CURRENT, 25, 0, PRICE_TYPICAL);
   if(DEMA_handle2 < 0)
     {
      Print("The creation of iDEMA has failed: DEMA_handle2=", INVALID_HANDLE);
      Print("Runtime error = ", GetLastError());
      return(INIT_FAILED);
     }
   
   ATR_handle = iATR(NULL, PERIOD_CURRENT, ATRPeriod1);
   if(ATR_handle < 0)
     {
      Print("The creation of iATR has failed: ATR_handle=", INVALID_HANDLE);
      Print("Runtime error = ", GetLastError());
      return(INIT_FAILED);
     }
   
   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++;
   datetime Time[];
   
   if(CopyBuffer(DEMA_handle, 0, 0, rates_total, DEMA) <= 0) return(rates_total);
   ArraySetAsSeries(DEMA, true);
   if(CopyBuffer(DEMA_handle2, 0, 0, rates_total, DEMA2) <= 0) return(rates_total);
   ArraySetAsSeries(DEMA2, true);
   if(CopyLow(Symbol(), PERIOD_CURRENT, 0, rates_total, Low) <= 0) return(rates_total);
   ArraySetAsSeries(Low, true);
   if(CopyBuffer(ATR_handle, 0, 0, rates_total, ATR) <= 0) return(rates_total);
   ArraySetAsSeries(ATR, true);
   if(CopyHigh(Symbol(), PERIOD_CURRENT, 0, rates_total, High) <= 0) return(rates_total);
   ArraySetAsSeries(High, true);
   if(CopyTime(Symbol(), Period(), 0, rates_total, Time) <= 0) return(rates_total);
   ArraySetAsSeries(Time, true);
   //--- 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(DEMA[Indicator_Shift1+i] > DEMA2[Indicator_Shift2+i]
      && DEMA[Indicator_Shift1+i+1] < DEMA2[Indicator_Shift2+i+1] //Double Exponential Moving Average crosses above Double Exponential Moving Average
      )
        {
         Buffer1[i] = Low[i] - ATR[i]; //Set indicator value at Candlestick Low - Average True Range
         if(Time[i] != time_alert) { myAlert("Signal", "Buy"); time_alert = Time[i]; } // this is only for buy signal
        }
      else
        {
         Buffer1[i] = EMPTY_VALUE;
        }
      //Indicator Buffer 2
      if(DEMA[Indicator_Shift1+i] < DEMA2[Indicator_Shift2+i]
      && DEMA[Indicator_Shift1+i+1] > DEMA2[Indicator_Shift2+i+1] //Double Exponential Moving Average crosses below Double Exponential Moving Average
      )
        {
         Buffer2[i] = High[i] + ATR[i]; //Set indicator value at Candlestick High + Average True Range
         if(Time[i] != time_alert) { myAlert("Signal", "Sell"); time_alert = Time[i]; } // this is only for sell signal
        }
      else
        {
         Buffer2[i] = EMPTY_VALUE;
        }
     }
   return(rates_total);
  }
//+------------------------------------------------------------------+

It does gives popup signals but shows all the previous signal concurrently with the present signal.


Thanks

 

Why do you copy ALL BARS on every tick? This is the BIGGEST ERROR!

   if(CopyBuffer(DEMA_handle, 0, 0, rates_total, DEMA) <= 0) return(rates_total);
   ArraySetAsSeries(DEMA, true);
   if(CopyBuffer(DEMA_handle2, 0, 0, rates_total, DEMA2) <= 0) return(rates_total);
   ArraySetAsSeries(DEMA2, true);
   if(CopyLow(Symbol(), PERIOD_CURRENT, 0, rates_total, Low) <= 0) return(rates_total);
   ArraySetAsSeries(Low, true);
   if(CopyBuffer(ATR_handle, 0, 0, rates_total, ATR) <= 0) return(rates_total);
   ArraySetAsSeries(ATR, true);
   if(CopyHigh(Symbol(), PERIOD_CURRENT, 0, rates_total, High) <= 0) return(rates_total);
   ArraySetAsSeries(High, true);
   if(CopyTime(Symbol(), Period(), 0, rates_total, Time) <= 0) return(rates_total);
   ArraySetAsSeries(Time, true)
 
cryptadels: but was giving me some errors, wouldn't mind your help fixing it
  1. "Doesn't work" is meaningless — just like saying the car doesn't work. Doesn't start, won't go in gear, no electrical, missing the key, flat tires — meaningless.
    Do you really expect an answer with the information you've given? There are no mind readers here and our crystal balls are cracked.

  2. Help you with what? You haven't stated a problem, you stated a want. Show us your attempt (using the CODE button) and state the nature of your problem.
              No free help 2017.04.21
 
Vladimir Karputov:

Why do you copy ALL BARS on every tick? This is the BIGGEST ERROR!

Thanks for you help Vladimir, but am getting error after correcting as you pointed out. Pls can you check through and fix it as appropriate for me pls

 
cryptadels :

Thanks for you help Vladimir, but am getting error after correcting as you pointed out. Pls can you check through and fix it as appropriate for me pls

Please read the help. That's not difficult. 

Documentation on MQL5: Technical Indicators / iDEMA
Documentation on MQL5: Technical Indicators / iDEMA
  • www.mql5.com
//|                                                   Demo_iDEMA.mq5 | //|                        Copyright 2011, MetaQuotes Software Corp. | //|                                             https://www.mql5.com | "The method of creation of the handle is set through the 'type' parameter (function type...
 
Vladimir Karputov:

Please read the help. That's not difficult. 

Thanks will try it out