Indicator not working in #property strict mode .

 
#property version   "1.00"
#property strict
#property indicator_chart_window


#property indicator_buffers 3
#property indicator_color1 Magenta
#property indicator_style1 STYLE_DOT

#define INDICATOR_VERSION "v2.0"
#define VTS_OBJECT_PREFIX "vtsbh2483-"


//---- input parameters

extern bool UseATRMode = true;
extern int NonATRStopPips = 40;
extern int ATRPeriod = 9;
extern double ATRMultiplier = 3.0;
extern int ATRSmoothing = 0;
extern color UpArrowColor = DodgerBlue;
extern color DnArrowColor = OrangeRed;
extern int ArrowDistance = 0;
extern bool AlertSound = true;
extern bool AlertMail = false;
extern bool ShowComment = true;


//---- buffers
double TrStopLevel[];

//---- variables
double ATRBuffer[];
double SmoothBuffer[];
string ShortName;
int ArrowObjects = 0;
int ArrowNumber;
int PrevArrows = 0;
datetime LastArrowTime = 0;
bool LastArrowSignal = 0;
datetime LastAlertBar = 0;
datetime CurrentBarTime = 0;
string ObjName;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
 int DrawBegin = 0;
    if ( UseATRMode ) {
        DrawBegin = ATRPeriod;
    }
    
    // Set the smoothing to 1 if it is zero or less
    if ( ATRSmoothing <= 0 ) {
        ATRSmoothing = 1;
    }
    
    IndicatorBuffers( 1 );
    SetIndexStyle( 0, DRAW_LINE, STYLE_DOT, 1 );
    SetIndexBuffer( 0, TrStopLevel );
    SetIndexDrawBegin( 0, DrawBegin );
    
    
    SetIndexLabel( 0, "" );
    
    
        
    // Cleanup any leftover objects from previous runs
    DeleteAllArrowObjects();
    ArrowNumber = 0;
    ArrowObjects = 0;

   
//---
   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 ictr;
        
    int limit = rates_total-(prev_calculated>0 ? prev_calculated : 1);
    ictr = limit - 1;
    
    if ( UseATRMode && Bars < ATRPeriod ) {
        return( 0 );
    }
    
    // Make sure buffers are sized correctly
    int buff_size = ArraySize( TrStopLevel );
    if ( ArraySize( ATRBuffer ) != buff_size ) {
        ArraySetAsSeries( ATRBuffer, false );
        ArrayResize( ATRBuffer, buff_size );
        ArraySetAsSeries( ATRBuffer, true );

        ArraySetAsSeries( SmoothBuffer, false );
        ArrayResize( SmoothBuffer, buff_size );
        ArraySetAsSeries( SmoothBuffer, true );
    }
    
    int xctr;
    
    if ( UseATRMode ) {
        // First calculate the ATR
        for ( xctr = 0; xctr < limit; xctr++ ) {
            ATRBuffer[xctr] = iATR( NULL, 0, ATRPeriod, xctr );
        }
            
        // Smooth the ATR
        for ( xctr = 0; xctr < limit; xctr++ ) {
            SmoothBuffer[xctr] = Wilders( ATRBuffer, ATRSmoothing, xctr );
        }
    }
    
    
    for ( xctr = ictr; xctr >= 0; xctr-- ) {
         // Calculate the stop amount
        double DeltaStop = NonATRStopPips * Point;
        
        // Calculate our stop value based on ATR if required
        if ( UseATRMode ) {
            DeltaStop = NormalizeDouble( SmoothBuffer[xctr] * ATRMultiplier, 4 );
        }
        
        // Figure out where the current bar's stop level should be
        double NewStopLevel;
        double PrevStop = TrStopLevel[xctr+1]; // it was TrStopLevel[xctr+1] array out of range
        //Comment(ArraySize(TrStopLevel));
        bool Up = false;
        bool Dn = false;
        
        if ( iClose(NULL,0,xctr) == PrevStop ) {
            NewStopLevel = PrevStop;
        }
        else {
            if ( iClose(NULL,0,xctr+ 1) <= PrevStop && iClose(NULL,0,xctr) < PrevStop ) {
                NewStopLevel = MathMin( PrevStop, ( iClose(NULL,0,xctr) + DeltaStop ) );
            }
            else {
                if ( iClose(NULL,0,xctr+1) >= PrevStop && iClose(NULL,0,xctr) > PrevStop ) {
                    NewStopLevel = MathMax( PrevStop, ( iClose(NULL,0,xctr) - DeltaStop ) );
                }
                else {
                    if ( iClose(NULL,0,xctr) > PrevStop ) {
                        NewStopLevel = iClose(NULL,0,xctr) - DeltaStop;
                        Up = true;
                    }
                    else {
                        NewStopLevel = iClose(NULL,0,xctr) + DeltaStop;
                        Dn = true;
                    }
                }
            }
        }
        
        
        TrStopLevel[xctr] = NewStopLevel;
        
        // Can't do the arrows until the bar closes
        if ( xctr > 0 ) {
            if ( Up ) { 
                if ( Time[xctr] > LastArrowTime ) {
                    double UpSignal = TrStopLevel[xctr] - ( ArrowDistance * Point );
                    ObjName = GetNextObjectName();
                    ObjectCreate( ObjName, OBJ_ARROW, 0, Time[xctr], UpSignal );
                    ObjectSet( ObjName, OBJPROP_COLOR, UpArrowColor );
                    ObjectSet( ObjName, OBJPROP_ARROWCODE, 233 );
                    ArrowObjects++;
                    LastArrowTime = Time[xctr];
                    LastArrowSignal = true;
                }
            }
            
            if ( Dn ) {
                if ( Time[xctr] > LastArrowTime ) {
                    double DnSignal = TrStopLevel[xctr] + ( 2 * Point ) + ( ArrowDistance * Point );
                    ObjName = GetNextObjectName();
                    ObjectCreate( ObjName, OBJ_ARROW, 0, Time[xctr], DnSignal );
                    ObjectSet( ObjName, OBJPROP_COLOR, DnArrowColor );
                    ObjectSet( ObjName, OBJPROP_ARROWCODE, 234 );
                    ArrowObjects++;
                    LastArrowTime = Time[xctr];
                    LastArrowSignal = false;
                }
            }
        }
        
        // Do the alerting
        if ( xctr == 1 ) {
            if ( LastArrowTime == Time[xctr] ) {
                DoAlerts();
            }
        }
        
        // Check to see if we've closed a bar and redraw the objects
        if ( xctr == 0 ) {
            if ( Time[xctr] != CurrentBarTime ) {
                ObjectsRedraw();
                CurrentBarTime = Time[xctr];
            }
        }
                
    }
    
    if ( ArrowObjects != PrevArrows ) {
        Print( "Total Arrow Objects: ", ArrowObjects );
        PrevArrows = ArrowObjects;
    }


   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//------------------------------------------------------------------+


//+------------------------------------------------------------------+
//| Gets the next object index so they can be deleted later          |
//+------------------------------------------------------------------+
string GetNextObjectName() {
    //int rand_val = MathRand() + 1;
    ArrowNumber++;
    //string retval = VTS_OBJECT_PREFIX + rand_val;
    string retval = VTS_OBJECT_PREFIX +( string)ArrowNumber;
    return( retval );
}
        

//+------------------------------------------------------------------+
//| Wilders Calculation                                              |
//+------------------------------------------------------------------+
double Wilders( double& indBuffer[], int Periods, int shift ) {
    double retval = 0.0;
    retval = iMAOnArray( indBuffer, 0, ( Periods * 2 ) - 1, 0, MODE_EMA, shift );
    return( retval );
}
    
//+------------------------------------------------------------------+
//| Delete all the arrow objects                                     |
//+------------------------------------------------------------------+
void DeleteAllArrowObjects() {
    int delcnt = 0;
    for ( int ictr = 1; ictr <= ArrowNumber; ictr++ ) {
        ObjName = VTS_OBJECT_PREFIX + (string)( ictr + 1 );
        if ( ObjectDelete( ObjName ) ) {
            delcnt++;
        }
    }
    Print( "Objects deleted: ", delcnt );
    return;
}

//+------------------------------------------------------------------+
//| Handles alerting via sound/email                                 |
//+------------------------------------------------------------------+
void DoAlerts() {
    if ( LastArrowTime > LastAlertBar ) {
        if ( AlertSound ) {
            PlaySound( "alert.wav" );
        }
        
        if ( AlertMail ) {
            int per = Period();
            string perstr = "";
            
            switch( per ) {
                case PERIOD_M1:
                    perstr = "M1";
                    break;
                    
                case PERIOD_M5:
                    perstr = "M5";
                    break;
                    
                case PERIOD_M15:
                    perstr = "M15";
                    break;
                    
                case PERIOD_M30:
                    perstr = "M30";
                    break;
                    
                case PERIOD_H1:
                    perstr = "H1";
                    break;
                    
                case PERIOD_H4:
                    perstr = "H4";
                    break;
                    
                case PERIOD_D1:
                    perstr = "D1";
                    break;
                    
                case PERIOD_W1:
                    perstr = "W1";
                    break;
                    
                case PERIOD_MN1:
                    perstr = "MN1";
                    break;
                    
                default:
                    perstr = "" + (string)per + " Min";
                    break;
            }
                    
            datetime curtime = TimeCurrent();
            string strSignal = "LONG";
            if ( !LastArrowSignal ) {
                strSignal = "SHORT";
            }
            string str_subject = "Alert " + TimeToStr( curtime, TIME_DATE | TIME_SECONDS );
            SendMail( str_subject,
                      " " +
                      strSignal +
                      " signal for pair " +
                      Symbol() +
                      " " + perstr + "." );
        }
        
        LastAlertBar = LastArrowTime;
    }
}
    

//+----------------------------------------------------------
 
Not working in #property strict mode . 
 
Komoles Kumar:
TrStopLevel : array out of range 
 
Komoles Kumar:
Not working in #property strict mode . 

MT4 crush ,Hang :( 

 
Komoles Kumar:

MT4 crush ,Hang :( 

Precisely with the property strict, then you will easily find out writing errors in your program. Check and count the bars you need for looping.

Documentation on MQL5: Language Basics / Preprocessor / Program Properties (#property)
Documentation on MQL5: Language Basics / Preprocessor / Program Properties (#property)
  • www.mql5.com
Path to the file of an image that will be used as an icon of the EX5 program. Path specification rules are the same as for resources. The property must be specified in the main module with the MQL5 source code. The icon file must be in the ICO format. When launching a script or an Expert Advisor on the chart, the stack of at least 8 MB is...
 
Roberto Jacobs:

Precisely with the property strict, then you will easily find out writing errors in your program. Check and count the bars you need for looping.

Thanks for your advice but problem is other. There is Array out of range error that i cant found out why . I know about #property strict .

 
Komoles Kumar:

Thanks for your advice but problem is other. There is Array out of range error that i cant found out why . I know about #property strict .

Highlight the section of your code where the error happens.

 
Komoles Kumar:

Resize your TrStopLevel arrays with limit.

 
Keith Watford:

Highlight the section of your code where the error happens.

This is location .

where array out of range.

 
 // Smooth the ATR
 for ( xctr = 0; xctr < limit; xctr++ ) {
    //SmoothBuffer[xctr] = Wilders( ATRBuffer, ATRSmoothing, xctr );
    SmoothBuffer[xctr] = iMAOnArray( ATRBuffer, 0, ( ATRSmoothing * 2 ) - 1, 0, MODE_EMA, xctr );
 }
//+------------------------------------------------------------------+
//| Wilders Calculation                                              |
//+------------------------------------------------------------------+
/*
double Wilders( double& indBuffer[], int Periods, int shift ) {
    double retval = 0.0;
    retval = iMAOnArray( indBuffer, 0, ( Periods * 2 ) - 1, 0, MODE_EMA, shift );
    return( retval );
}*/

MT4 crash was avoided.

But there are plenty of things to fix.

 
Nagisa Unada:

MT4 crash was avoided.

But there are plenty of things to fix.

Thank you. 

Reason: