Disappearance of indicator

To add comments, please log in or register
Tete Adate Adjete
768
Tete Adate Adjete  

I need your help on the subject.


i created an MT5 indicator which works fine but disappears from the screen when i refreshed the chart it is attached to.

This problem arose when I shifted its signals back 3 minutes using the shift function; but if the shift is on 0 it does not disappear from the screen.


I would like to ask if there is a solution or if someone could help me with it


thank you in advance


Here is the source code of the indicator in question, its signals appear as an arrow on the graph


shiftB and shiftS correspond respectively to Buy and Sell


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

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

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

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

input int _K_period = 5;
input int _D_period = 3;
input int Slowing = 3;
input int ShiftB = -3;
input int ShiftS = -3;
datetime time_alert; //used when sending alert
input bool Audible_Alerts = true;
input bool Push_Notifications = true;
double myPoint; //initialized in OnInit
int Stochastic_handle;
double Stochastic_Main[];
int Stochastic_handle2;
double Stochastic_Signal2[];
double Stochastic_Main2[];
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+" | stoch kaka @ "+Symbol()+","+IntegerToString(Period())+" | "+message);
     }
   else if(type == "order")
     {
     }
   else if(type == "modify")
     {
     }
   else if(type == "indicator")
     {
      if(Audible_Alerts) Alert(type+" | stoch kaka @ "+Symbol()+","+IntegerToString(Period())+" | "+message);
      if(Push_Notifications) SendNotification(type+" | stoch kaka @ "+Symbol()+","+IntegerToString(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);
   //initialize myPoint
   myPoint = Point();
   if(Digits() == 5 || Digits() == 3)
     {
      myPoint *= 10;
     }
   Stochastic_handle = iStochastic(NULL, PERIOD_CURRENT, _K_period, _D_period, Slowing, MODE_SMA, STO_LOWHIGH);
   if(Stochastic_handle < 0)
     {
      Print("The creation of iStochastic has failed: Stochastic_handle=", INVALID_HANDLE);
      Print("Runtime error = ", GetLastError());
      return(INIT_FAILED);
     }
   
   Stochastic_handle2 = iStochastic(NULL, PERIOD_CURRENT, 5, 3, 3, MODE_SMA, STO_LOWHIGH);
   if(Stochastic_handle2 < 0)
     {
      Print("The creation of iStochastic has failed: Stochastic_handle2=", INVALID_HANDLE);
      Print("Runtime error = ", GetLastError());
      return(INIT_FAILED);
     }
   
   ATR_handle = iATR(NULL, PERIOD_CURRENT, 14);
   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(BarsCalculated(Stochastic_handle) <= 0) 
      return(0);
   if(CopyBuffer(Stochastic_handle, MAIN_LINE, 0, rates_total, Stochastic_Main) <= 0) return(rates_total);
   ArraySetAsSeries(Stochastic_Main, true);
   if(BarsCalculated(Stochastic_handle2) <= 0) 
      return(0);
   if(CopyBuffer(Stochastic_handle2, SIGNAL_LINE, 0, rates_total, Stochastic_Signal2) <= 0) return(rates_total);
   ArraySetAsSeries(Stochastic_Signal2, true);
   if(BarsCalculated(Stochastic_handle2) <= 0) 
      return(0);
   if(CopyBuffer(Stochastic_handle2, MAIN_LINE, 0, rates_total, Stochastic_Main2) <= 0) return(rates_total);
   ArraySetAsSeries(Stochastic_Main2, true);
   if(CopyLow(Symbol(), PERIOD_CURRENT, 0, rates_total, Low) <= 0) return(rates_total);
   ArraySetAsSeries(Low, true);
   if(BarsCalculated(ATR_handle) <= 0) 
      return(0);
   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(10000-1, rates_total-1-50)) continue; //omit some old rates to prevent "Array out of range" or slow calculation   
      
      //Indicator Buffer 1
      if(Stochastic_Main[ShiftB+i] > Stochastic_Signal2[i]
      && Stochastic_Main[ShiftB+i+1] < Stochastic_Signal2[i+1] //Stochastic Oscillator crosses above Stochastic Oscillator
      && Stochastic_Main2[i] < 20 //Stochastic Oscillator < fixed value
      )
        {
         Buffer1[i] = Low[i] - ATR[i]; //Set indicator value at Candlestick Low - Average True Range
         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(Stochastic_Main[ShiftS+i] < Stochastic_Signal2[i]
      && Stochastic_Main[ShiftS+i+1] > Stochastic_Signal2[i+1] //Stochastic Oscillator crosses below Stochastic Oscillator
      && Stochastic_Main2[i] > 80 //Stochastic Oscillator > fixed value
      )
        {
         Buffer2[i] = High[i] + ATR[i]; //Set indicator value at Candlestick High + Average True Range
         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);
  }
//+------------------------------------------------------------------+
John Davis
14389
John Davis  
Tete Adate Adjete:

I need your help on the subject.


i created an MT5 indicator which works fine but disappears from the screen when i refreshed the chart it is attached to.

This problem arose when I shifted its signals back 3 minutes using the shift function; but if the shift is on 0 it does not disappear from the screen.


I would like to ask if there is a solution or if someone could help me with it


thank you in advance


Here is the source code of the indicator in question, its signals appear as an arrow on the graph


shiftB and shiftS correspond respectively to Buy and Sell


Ok, took a look at the link you sent on my computer and I have a guess as to what's going on here.

If you shift the arrow to the right it is overridden by the value that results when the indicator calculates the value for that particular i-value. For example if you have 10 bars and you are calculating the values from 10 down to zero and you decide to place a value into the 7th bar when you are calculating the 10th bar value, when the indicator gets to the 7th bar it may change the value that was placed there. This would not be a problem if you are just working with and placing the value of the current ith bar.


You should be able to fix this by removing the Buffer[i] = EMPTY_VALUE else statement so that when you set a value it stays and is not overridden.

Remove the below from both your buy and sell indicator calculations and it should work the way you expect.

      else
        {
         Buffer1[i] = EMPTY_VALUE;
        }

I am guessing that the buffer it is automatically filled with EMPTY_VALUE so you would not need this anyway, however I could be wrong about this and some code to mitigate invalid values may be necessary.

Tete Adate Adjete
768
Tete Adate Adjete  
John Davis:

Ok, took a look at the link you sent on my computer and I have a guess as to what's going on here.

If you shift the arrow to the right it is overridden by the value that results when the indicator calculates the value for that particular i-value. For example if you have 10 bars and you are calculating the values from 10 down to zero and you decide to place a value into the 7th bar when you are calculating the 10th bar value, when the indicator gets to the 7th bar it may change the value that was placed there. This would not be a problem if you are just working with and placing the value of the current ith bar.


You should be able to fix this by removing the Buffer[i] = EMPTY_VALUE else statement so that when you set a value it stays and is not overridden.

Remove the below from both your buy and sell indicator calculations and it should work the way you expect.

I am guessing that the buffer it is automatically filled with EMPTY_VALUE so you would not need this anyway, however I could be wrong about this and some code to mitigate invalid values may be necessary.

thank you very much for responding, I tried this but it didn't work
To add comments, please log in or register