Historical Buffer data changes every time I refresh or re-compile.

 
//------------------------------------------------------------------
#property copyright "Copyright Harry 2023."
#property link      ""
#property description "Harry"
//------------------------------------------------------------------
#property indicator_separate_window
#property indicator_buffers 8
#property indicator_color1  LimeGreen
#property indicator_color2  PaleVioletRed
#property indicator_color3  LimeGreen
#property indicator_color4  PaleVioletRed
#property indicator_color5  LimeGreen
#property indicator_color6  PaleVioletRed
#property indicator_color7  LimeGreen
#property indicator_color8  PaleVioletRed
#property indicator_minimum 0
#property indicator_maximum 5

//
//
//
//
//


extern bool Use_tf_0 = true; //Use timeframe 1
extern ENUM_TIMEFRAMES tf_0 = PERIOD_M30; //timeframe 1

extern bool Use_tf_1 = true;  //Use timeframe 2
extern ENUM_TIMEFRAMES tf_1 = PERIOD_M15; //timeframe 2

extern bool Use_tf_2 = true;  //Use timeframe 3
extern ENUM_TIMEFRAMES tf_2 = PERIOD_M5; //timeframe 3

extern bool Use_tf_3 = true;  //Use timeframe 4
extern ENUM_TIMEFRAMES tf_3 = PERIOD_M1; //timeframe 4

extern ENUM_MA_METHOD  MaMethod1    = MODE_SMA;
extern int    MaPeriod1             = 1;
extern ENUM_MA_METHOD  MaMethod2    = MODE_SMA;
extern int    MaPeriod2             = 1;
extern bool   UseHAHighLow          = true;
extern string UniqueID              = "Heiken on 4 timeframe";
extern int    LinesWidth            =  0;
extern color  LabelsColor           = clrDarkGray;
extern int    LabelsHorizontalShift = 5;
extern double LabelsVerticalShift   = 1.5;

extern bool   alertsOn              = true;
extern bool   alertsMessage         = true;
extern bool   alertsSound           = false;
extern bool   alertsEmail           = false;

extern bool   Show_Arrows           = true;
extern int    Arr_width             = 1;
extern color  Arr_Up_colour         = clrLime;
extern color  Arr_Dn_colour         = clrRed;
extern int    History               = 20000;
//
//
//
//
//

double ha1u[];
double ha1d[];
double ha2u[];
double ha2d[];
double ha3u[];
double ha3d[];
double ha4u[];
double ha4d[];





//+------------------------------------------------------------------+
//| Initialization Function                                          |
//+------------------------------------------------------------------+
int OnInit()
{
   IndicatorBuffers(9);
   
   if(Use_tf_0 == true) {SetIndexBuffer(0,ha1u);  SetIndexLabel(0, StringSubstr(EnumToString(tf_0),7,-1)+" (bull)");}
   if(Use_tf_0 == true) {SetIndexBuffer(1,ha1d);  SetIndexLabel(1, StringSubstr(EnumToString(tf_0),7,-1)+" (bear)");}
   if(Use_tf_1 == true) {SetIndexBuffer(2,ha2u);  SetIndexLabel(2, StringSubstr(EnumToString(tf_1),7,-1)+" (bull)");}
   if(Use_tf_1 == true) {SetIndexBuffer(3,ha2d);  SetIndexLabel(3, StringSubstr(EnumToString(tf_1),7,-1)+" (bear)");}
   if(Use_tf_2 == true) {SetIndexBuffer(4,ha3u);  SetIndexLabel(4, StringSubstr(EnumToString(tf_2),7,-1)+" (bull)");}
   if(Use_tf_2 == true) {SetIndexBuffer(5,ha3d);  SetIndexLabel(5, StringSubstr(EnumToString(tf_2),7,-1)+" (bear)");}
   if(Use_tf_3 == true) {SetIndexBuffer(6,ha4u);  SetIndexLabel(6, StringSubstr(EnumToString(tf_3),7,-1)+" (bull)");}
   if(Use_tf_3 == true) {SetIndexBuffer(7,ha4d);  SetIndexLabel(7, StringSubstr(EnumToString(tf_3),7,-1)+" (bear)");}
   
     
      for (int i=0; i<8; i++) 
      {
         SetIndexStyle(i,DRAW_ARROW,EMPTY,LinesWidth); SetIndexArrow(i,110); 
      }
      

      IndicatorShortName(UniqueID);
      
      
      
            int window = WindowFind(UniqueID);
            for (int t=3; t>=0; t--)
            {
              
                if(t == 3 && Use_tf_0 == true) {Text_Create(tf_0,window,t);}  
                if(t == 2 && Use_tf_1 == true) {Text_Create(tf_1,window,t);} 
                if(t == 1 && Use_tf_2 == true) {Text_Create(tf_2,window,t);} 
                if(t == 0 && Use_tf_3 == true) {Text_Create(tf_3,window,t);} 
            }               
         

   return(0);
}

//+------------------------------------------------------------------+
//|  De-Initialization Function                                      |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
   //for (int t=0; t<4; t++) ObjectDelete(UniqueID+t);
   for (int i = ObjectsTotal()-1; i >= 0; i--)   
     {
       if (StringSubstr(ObjectName(i), 0, StringLen(UniqueID)) == UniqueID)  {ObjectDelete(ObjectName(i));}
     }
     
   if(Use_tf_0 == false) {ObjectDelete(0,StringConcatenate(UniqueID,0));}
   if(Use_tf_1 == false) {ObjectDelete(0,StringConcatenate(UniqueID,1));}
   if(Use_tf_2 == false) {ObjectDelete(0,StringConcatenate(UniqueID,2));}
   if(Use_tf_3 == false) {ObjectDelete(0,StringConcatenate(UniqueID,3));}
   
}




//+------------------------------------------------------------------+
//|  On Calculate 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;
   limit=rates_total-prev_calculated;
   if(prev_calculated==0) {limit= MathMin(History,limit);}
      
        for(int i =limit; i>=0; i--)
          {
              for(int n= 3; n>=0; n--)
                {
                     if(n == 3 && Use_tf_0 == true) {Data_Get(i, n, tf_0);}
                     if(n == 2 && Use_tf_1 == true) {Data_Get(i, n, tf_1);}  
                     if(n == 1 && Use_tf_2 == true) {Data_Get(i, n, tf_2);}  
                     if(n == 0 && Use_tf_3 == true) {Data_Get(i, n, tf_3);}  
                }
                   
              if( 
                    (ha1u[i] == 4 || (Use_tf_0 == false && ArraySize(ha1u) == 0)) && 
                    (ha2u[i] == 3 || (Use_tf_1 == false && ArraySize(ha2u) == 0)) && 
                    (ha3u[i] == 2 || (Use_tf_2 == false && ArraySize(ha3u) == 0)) && 
                    (ha4u[i] == 1 || (Use_tf_3 == false && ArraySize(ha4u) == 0)) &&
        
                    (ha1d[i+1] == 4 || ha2d[i+1] == 3 || ha3d[i+1] == 2 || ha4d[i+1] == 1) && i>0
                )
                    {arrows_wind(i,"Up",Arr_Up_colour,1); }
                   
              if( 
                    (ha1d[i] == 4 || (Use_tf_0 == false && ArraySize(ha1d) == 0)) && 
                    (ha2d[i] == 3 || (Use_tf_1 == false && ArraySize(ha2d) == 0)) && 
                    (ha3d[i] == 2 || (Use_tf_2 == false && ArraySize(ha3d) == 0)) && 
                    (ha4d[i] == 1 || (Use_tf_3 == false && ArraySize(ha4d) == 0)) &&
                         
                    (ha1u[i+1] == 4 || ha2u[i+1] == 3 || ha3u[i+1] == 2 || ha4u[i+1] == 1) && i>1
                )
                   {arrows_wind(i,"Dn",Arr_Dn_colour,2);}
          }
              
            

{
//Condition for Alert
    if( 
        (ha1u[1] == 4 || (Use_tf_0 == false && ArraySize(ha1u) == 0)) && 
        (ha2u[1] == 3 || (Use_tf_1 == false && ArraySize(ha2u) == 0)) && 
        (ha3u[1] == 2 || (Use_tf_2 == false && ArraySize(ha3u) == 0)) && 
        (ha4u[1] == 1 || (Use_tf_3 == false && ArraySize(ha4u) == 0)) &&
        
        
        (ha1d[2] == 4 || ha2d[2] == 3 || ha3d[2] == 2 || ha4d[2] == 1)
      )
       {arrows_wind(1,"Up",Arr_Up_colour,1); Do_Alert(1);}
                   
    if( 
        
        (ha1d[1] == 4 || (Use_tf_0 == false && ArraySize(ha1d) == 0)) && 
        (ha2d[1] == 3 || (Use_tf_1 == false && ArraySize(ha2d) == 0)) && 
        (ha3d[1] == 2 || (Use_tf_2 == false && ArraySize(ha3d) == 0)) && 
        (ha4d[1] == 1 || (Use_tf_3 == false && ArraySize(ha4d) == 0)) &&
        //ha1d[1] == 4 && ha2d[1] == 3 && ha3d[1] == 2 && ha4d[1] == 1 &&
         
       (ha1u[2] == 4 || ha2u[2] == 3 || ha3u[2] == 2 || ha4u[2] == 1) 
      )
       {arrows_wind(1,"Dn",Arr_Dn_colour,2); Do_Alert(2);}
}



         if (prev_calculated == rates_total)
         {
            int window = WindowFind(UniqueID);
            for (int t=3; t>=0; t--)
            {
              
                if(t == 3 && Use_tf_0 == true) {Text_Create(tf_0,window,t);}  
                if(t == 2 && Use_tf_1 == true) {Text_Create(tf_1,window,t);} 
                if(t == 1 && Use_tf_2 == true) {Text_Create(tf_2,window,t);} 
                if(t == 0 && Use_tf_3 == true) {Text_Create(tf_3,window,t);} 
            }               
         }
         
    
    
  
    Comment(limit," ",i," ",prev_calculated," ",rates_total);
  
   return(rates_total);
}





//+------------------------------------------------------------------+
//|  Draw Arrows on chart                                            |
//+------------------------------------------------------------------+
void arrows_wind(int k, string N,color clr,int signal_type)                 
{           
   if(Show_Arrows == false) {;}
   
   else
    {
      string objName = UniqueID+"_"+N+TimeToStr(iTime(_Symbol,_Period,k));
      double gap = 13*_Point;
      gap = MathMax(gap,0.65*(iHigh(_Symbol,_Period,k)-iLow(_Symbol,_Period,k)));
   

      if (signal_type == 1)
        {
          ObjectCreate(objName, OBJ_ARROW,0,iTime(_Symbol,_Period,k),0);
          ObjectSet(objName, OBJPROP_COLOR, clr);  
          ObjectSet(objName, OBJPROP_ARROWCODE,233);
          ObjectSet(objName, OBJPROP_WIDTH,Arr_width);
          ObjectSet(objName, OBJPROP_ANCHOR,ANCHOR_BOTTOM);
          ObjectSet(objName,OBJPROP_PRICE1,iLow(_Symbol,_Period,k)-gap);
        }
      if (signal_type == 2)
        { 
          ObjectCreate(objName, OBJ_ARROW,0,iTime(_Symbol,_Period,k),0);
          ObjectSet(objName, OBJPROP_COLOR, clr);  
          ObjectSet(objName, OBJPROP_ARROWCODE,234);
          ObjectSet(objName, OBJPROP_WIDTH,Arr_width);
          ObjectSet(objName,OBJPROP_ANCHOR,ANCHOR_TOP);
          ObjectSet(objName,OBJPROP_PRICE1,iHigh(_Symbol,_Period,k)+gap);
        }
    
    }
    
//---
}



datetime Counter_Alert  = D'01.01.1970';
//+------------------------------------------------------------------+
//| Send out Alert                                                   |
//+------------------------------------------------------------------+
void Do_Alert(int signal_type)
{
  if(iTime(_Symbol,_Period,0) > Counter_Alert && alertsOn == true)
    {
      string sig = ""; if(signal_type==1){sig = "Bullish";} if(signal_type==2){sig = "Bearish";}
      
      
      string msg = _Symbol+": "+sig+" signal formed for 4-timeframe-Heiken-Ashi on "+StringSubstr(EnumToString( ENUM_TIMEFRAMES(Period())),7,-1);
  
  
      Alert(msg);  SendNotification(msg);
     
     
      Counter_Alert = iTime(_Symbol,_Period,0);
    }
}



//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Text_Create(ENUM_TIMEFRAMES tf_nn, int window, int t)
{
   string label = StringSubstr(EnumToString(tf_nn),7,-1);
               ObjectCreate(UniqueID+t,OBJ_TEXT,window,0,0);
                  ObjectSet(UniqueID+t,OBJPROP_COLOR,LabelsColor);
                  ObjectSet(UniqueID+t,OBJPROP_PRICE1,t+LabelsVerticalShift);
                  ObjectSetText(UniqueID+t,label,8,"Arial");
                  
    for (t=0; t<4; t++) {ObjectSet(UniqueID+t,OBJPROP_TIME1,iTime(_Symbol,_Period,0)+Period()*LabelsHorizontalShift*60);}
                  
}


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Data_Get(int i, int n, ENUM_TIMEFRAMES tf_n)
{

   int b = iBarShift(_Symbol,tf_n,iTime(_Symbol,_Period,i));
           
           
           color CLR =clrNONE;
                       if ( 
                           iCustom(_Symbol,tf_n,"Heiken Ashi",CLR,CLR,CLR,CLR, 1, b) >=  iCustom(_Symbol,tf_n,"Heiken Ashi",CLR,CLR,CLR,CLR, 3, b) &&
                           iCustom(_Symbol,tf_n,"Heiken Ashi",CLR,CLR,CLR,CLR, 3, b) >=  iCustom(_Symbol,tf_n,"Heiken Ashi",CLR,CLR,CLR,CLR, 2, b) && 
                           iCustom(_Symbol,tf_n,"Heiken Ashi",CLR,CLR,CLR,CLR, 2, b) >=  iCustom(_Symbol,tf_n,"Heiken Ashi",CLR,CLR,CLR,CLR, 0, b) 
                          )
                          {
                            if (n==3) {ha1u[i] = n+1; ha1d[i] = EMPTY_VALUE;} 
                            if (n==2) {ha2u[i] = n+1; ha2d[i] = EMPTY_VALUE;}
                            if (n==1) {ha3u[i] = n+1; ha3d[i] = EMPTY_VALUE;} 
                            if (n==0) {ha4u[i] = n+1; ha4d[i] = EMPTY_VALUE;}
                          }
       
                       if ( 
                           iCustom(_Symbol,tf_n,"Heiken Ashi",CLR,CLR,CLR,CLR, 0, b) >=  iCustom(_Symbol,tf_n,"Heiken Ashi",CLR,CLR,CLR,CLR, 2, b) &&
                           iCustom(_Symbol,tf_n,"Heiken Ashi",CLR,CLR,CLR,CLR, 2, b) >=  iCustom(_Symbol,tf_n,"Heiken Ashi",CLR,CLR,CLR,CLR, 3, b) && 
                           iCustom(_Symbol,tf_n,"Heiken Ashi",CLR,CLR,CLR,CLR, 3, b) >=  iCustom(_Symbol,tf_n,"Heiken Ashi",CLR,CLR,CLR,CLR, 1, b) 
                          )
                          
                          {
                            if (n==3) {ha1d[i] = n+1; ha1u[i] = EMPTY_VALUE;}
                            if (n==2) {ha2d[i] = n+1; ha2u[i] = EMPTY_VALUE;}
                            if (n==1) {ha3d[i] = n+1; ha3u[i] = EMPTY_VALUE;}
                            if (n==0) {ha4d[i] = n+1; ha4u[i] = EMPTY_VALUE;}
                          }


}
 
Your topic has been moved to the section: MQL4 and MetaTrader 4
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
 
Chukwuemena Okonkwo:
This is a problematic approach:

   if(Use_tf_0 == true) {SetIndexBuffer(0,ha1u);  SetIndexLabel(0, StringSubstr(EnumToString(tf_0),7,-1)+" (bull)");}
   if(Use_tf_0 == true) {SetIndexBuffer(1,ha1d);  SetIndexLabel(1, StringSubstr(EnumToString(tf_0),7,-1)+" (bear)");}
   if(Use_tf_1 == true) {SetIndexBuffer(2,ha2u);  SetIndexLabel(2, StringSubstr(EnumToString(tf_1),7,-1)+" (bull)");}
   if(Use_tf_1 == true) {SetIndexBuffer(3,ha2d);  SetIndexLabel(3, StringSubstr(EnumToString(tf_1),7,-1)+" (bear)");}
   if(Use_tf_2 == true) {SetIndexBuffer(4,ha3u);  SetIndexLabel(4, StringSubstr(EnumToString(tf_2),7,-1)+" (bull)");}
   if(Use_tf_2 == true) {SetIndexBuffer(5,ha3d);  SetIndexLabel(5, StringSubstr(EnumToString(tf_2),7,-1)+" (bear)");}
   if(Use_tf_3 == true) {SetIndexBuffer(6,ha4u);  SetIndexLabel(6, StringSubstr(EnumToString(tf_3),7,-1)+" (bull)");}
   if(Use_tf_3 == true) {SetIndexBuffer(7,ha4d);  SetIndexLabel(7, StringSubstr(EnumToString(tf_3),7,-1)+" (bear)");}

You must keep the sequence of the buffers in correct order. You cannot assume a reliable buffering for your data, if you do not stick to the sequence.

Else, concerning your issue, please show logs to pinpoint your problem. Use some print statements to show the values, you are referring to.

 
Dominik Egert #:
This is a problematic approach:


You must keep the sequence of the buffers in correct order. You cannot assume a reliable buffering for your data, if you do not stick to the sequence.

Else, concerning your issue, please show logs to pinpoint your problem. Use some print statements to show the values, you are referring to.


Here it is Boss.

I ran this in the Strategy Tester. It generated the result in the first file...

I paused the tester and recompiled and it generated the second file...

The buffer values have changed.

Please why does it change? 
 

Here is a sample....

I ran it in the Strategy Tester. It generated this data.


This is before I recompiled


 

This is after I recompiled

Still in the Strategy Tester

After recompiling some arrows deleted because the data changed some are not in the same Arrow are not in tbe same position


After compiling


 

The Arrows are drawn when the 4 buffers align.

But the buffer data changes after recompiling after being formed...

 
Chukwuemena Okonkwo #:

The Arrows are drawn when the 4 buffers align.

But the buffer data changes after recompiling after being formed...

Have you already fixed the buffer counting sequence?

Please add print statements into your code so that you can track what is happening according to your own logic.

As a hint, you are doing a lot of assumptions in your code, you should do much more checking of what is actually happening.

Use the Debugger to figure out what's going on, if required.

Once you have narrowed down these issues and you cannot solve your problem, you at least can provide great details about what your code is doing, and someone here on the forum is probably able to help you.
 
Chukwuemena Okonkwo #:

The Arrows are drawn when the 4 buffers align.

But the buffer data changes after recompiling after being formed...

BTW:

You should use #property strict
 
Dominik Egert #:
Have you already fixed the buffer counting sequence?

Please add print statements into your code so that you can track what is happening according to your own logic.

As a hint, you are doing a lot of assumptions in your code, you should do much more checking of what is actually happening.

Use the Debugger to figure out what's going on, if required.

Once you have narrowed down these issues and you cannot solve your problem, you at least can provide great details about what your code is doing, and someone here on the forum is probably able to help you.


Hello Boss,

Please could you point out parts of my code I made assumptions and every other thing you think I did wrong.

 
Dominik Egert #: This is a problematic approach:
   if(Use_tf_0 == true) {SetIndexBuffer …

You should be able to read your code out loud and have it make sense. You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So don't write if(bool == true), just use if(bool) or if(!bool).

Reason: