I need Help

 

guys..... read this calmand tell me what to do


I have created buy sell custom indicator using RSI indicator by shifting -3.

(yes I know, RSI hasn’t  settings to shift in default)

My Indicator look back 5000 bars from current bar to show the signals( buy or sell arrow) history. And omits the oldest 50 bars to keep away from “Array out of range” error and keep running on chart. 

When I load this indicator for the first time to the chart (at any moment), or refreshing chart window/indicator after few minutes (example – 40 minitues) from first loading time, indicator shows buy or sell signals from current bar to 5000 bars backward.


But the problem is,


If I don’t refresh the chart window/indicator after the first loaded time or from last refreshed time, Indicator shows nothing even if time passes for months.

Its only shows the history from first loaded time or last refreshed time. If I want to use indicator, I has to be refreshing it all the time.


guys As you think, what would be the reason for this ?

can you tell me the code I should use to solved this ?


I attached the code-script in bellow,

//+------------------------------------------------------------------+
//|                                Indicator: RSI test shift  -3.mq4 |
//|                                       Created with EABuilder.com |
//|                                        https://www.eabuilder.com |
//+------------------------------------------------------------------+
#property copyright "Created with EABuilder.com"
#property link      "https://www.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 0x6600FF
#property indicator_label1 "Buy"

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

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

extern int rsi = 8;
extern int arrows_shift = 2;
extern int rsi_shift = -3;
extern double RSI_ob = 80;
extern double RSI_OS = 20;
double myPoint; //initialized in OnInit

void myAlert(string type, string message)
  {
   if(type == "print")
      Print(message);
   else if(type == "error")
     {
      Print(type+" | RSI test shift  -3 @ "+Symbol()+","+IntegerToString(Period())+" | "+message);
     }
   else if(type == "order")
     {
     }
   else if(type == "modify")
     {
     }
  }

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {   
   IndicatorBuffers(2);
   SetIndexBuffer(0, Buffer1);
   SetIndexEmptyValue(0, EMPTY_VALUE);
   SetIndexArrow(0, 234);
   SetIndexBuffer(1, Buffer2);
   SetIndexEmptyValue(1, EMPTY_VALUE);
   SetIndexArrow(1, 233);
   //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, EMPTY_VALUE);
      ArrayInitialize(Buffer2, EMPTY_VALUE);
     }
   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(iRSI(NULL, PERIOD_CURRENT, rsi, PRICE_CLOSE, rsi_shift+i) < 80
      && iRSI(NULL, PERIOD_CURRENT, rsi, PRICE_CLOSE, rsi_shift+i+1) > 80 //Relative Strength Index crosses below fixed value
      )
        {
         Buffer1[i] = High[i] + arrows_shift * myPoint; //Set indicator value at Candlestick High + fixed value
        }
      else
        {
         Buffer1[i] = EMPTY_VALUE;
        }
      //Indicator Buffer 2
      if(iRSI(NULL, PERIOD_CURRENT, rsi, PRICE_CLOSE, rsi_shift+i) > 20
      && iRSI(NULL, PERIOD_CURRENT, rsi, PRICE_CLOSE, rsi_shift+i+1) < 20 //Relative Strength Index crosses above fixed value
      )
        {
         Buffer2[i] = Low[i] - arrows_shift * myPoint; //Set indicator value at Candlestick Low - fixed value
        }
      else
        {
         Buffer2[i] = EMPTY_VALUE;
        }
     }
   return(rates_total);
  }
//+------------------------------------------------------------------+

 

In such cases:

  1. look in the logs, Expert & Journal!!
  2. Learn tu use the debugger:
    https://www.metatrader5.com/en/metaeditor/help/development/debug // Code debugging
    https://www.mql5.com/en/articles/2041 // Error Handling and Logging in MQL5
    https://www.mql5.com/en/articles/272 // Tracing, Debugging and Structural Analysis of Source Code
    https://www.mql5.com/en/articles/35 // scrol down to: "Launching and Debuggin"

Code debugging - Developing programs - MetaEditor Help
Code debugging - Developing programs - MetaEditor Help
  • www.metatrader5.com
MetaEditor has a built-in debugger allowing you to check a program execution step by step (by individual functions). Place breakpoints in the code...
 
  1.       if(iRSI(NULL, PERIOD_CURRENT, rsi, PRICE_CLOSE, rsi_shift+i) > 20
             ⋮
             Buffer1[i] = High[i] + …
    
    Why did you post your MT4 question in the MT5 General section instead of the MQL4 section, (bottom of the Root page)?

              General rules and best pratices of the Forum. - General - MQL5 programming forum? (2017)
    Next time, post in the correct place. The moderators will likely move this thread there soon.

  2.       if (i >= MathMin(5000-1, rates_total-1-50)) continue; //omit some old rates to prevent "Array out of range" or slow calculation         

    This broken code only allows a maximum look back of 50. See How to do your lookbacks correctly #9#14 & #19.

  3. Your look back is eight (rsi). If your first run has insufficient bars, you return rates_total instead of prev_calculated.


Reason: