Really Strange Error (OO)

 

This Is My Expert Code.

I am going to have Automatic Trading according another indicator data (Test_SellBuyPoints).

Whenever I want do debug my code and step by step running, I give "Critical error occurred, debugging is stopped", message in line 38 of my code which is 

if(IndHandle<0)

nothing is wrong with this simple "if" statement!

Here is my Complete Code

//+------------------------------------------------------------------+
//|                                                 My_First_EA6.mq5 |
//|                        Copyright 2020, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2010, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#property version   "1.00"

//--- input parameters
input int      StopLoss=0;      // Stop Loss
input int      TakeProfit=0;   // Take Profit
input double   Lot=0.1;          // Lots to Trade
//--- Other parameters
int IndHandle;

double SellBuffer[];
double BuyBuffer[];
double p_close; // close value of a bar
int STP, TKP;   // Stop Loss & Take Profit values

#include <Trade\Trade.mqh>
CTrade trade;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
      
   SetIndexBuffer(0,SellBuffer,INDICATOR_DATA);   
   SetIndexBuffer(1,BuyBuffer,INDICATOR_DATA);   
     
   IndHandle=iCustom(NULL,0,"Test_SellBuyPoints",0,14,51,21,0.1,3);
  
   if(IndHandle<0)
     {
      Alert("Error Creating Handles for indicators - error: ",GetLastError(),"!!");
      return(-1);
     }

   STP = StopLoss;
   TKP = TakeProfit;
   return(0);
  }

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//--- Do we have enough bars to work with

   if(Bars(_Symbol,_Period)<60) // if total bars is less than 60 bars
     {
      Alert("We have less than 60 bars, EA will now exit!!");
      return;
     }  
   static datetime Old_Time;
   datetime New_Time[1];
   bool IsNewBar=false;

  

// copying the last bar time to the element New_Time[0]
   int copied=CopyTime(_Symbol,_Period,0,1,New_Time);
   if(copied>0) // ok, the data has been copied successfully
     {
      if(Old_Time!=New_Time[0]) // if old time isn't equal to new bar time
        {
         IsNewBar=true;   // if it isn't a first call, the new bar has appeared
         if(MQL5InfoInteger(MQL5_DEBUGGING)) 
         Print("We have new bar here ",New_Time[0]," old time was ",Old_Time);
         Old_Time=New_Time[0];            // saving bar time
        }
     }
   else
     {
      Alert("Error in copying historical times data, error =",GetLastError());
      ResetLastError();
      return;
     }

//--- EA should only check for new trade if we have a new bar
   if(IsNewBar==false)
     {
      return;
     }
 
//--- Do we have enough bars to work with
   int Mybars=Bars(_Symbol,_Period);
   if(Mybars<60) // if total bars is less than 60 bars
     {
      Alert("We have less than 60 bars, EA will now exit!!");
      return;
     }

//--- Define some MQL5 Structures we will use for our trade
   MqlTick latest_price;      // To be used for getting recent/latest price quotes
   MqlRates mrate[];          // To be used to store the prices, volumes and spread of each bar

   ArraySetAsSeries(mrate,true);
  
   ArraySetAsSeries(SellBuffer,true);
   ArraySetAsSeries(BuyBuffer,true);
   


//--- Get the last price quote using the MQL5 MqlTick Structure
   if(!SymbolInfoTick(_Symbol,latest_price))
     {
      Alert("Error getting the latest price quote - error:",GetLastError(),"!!");
      return;
     }

//--- Get the details of the latest 3 bars
   if(CopyRates(_Symbol,_Period,0,3,mrate)<0)
     {
      Alert("Error copying rates/history data - error:",GetLastError(),"!!");
      ResetLastError();
      return;
     }


   if(CopyBuffer(IndHandle,0,0,1,SellBuffer)<0 ){
   Alert("Error copying  indicator SellBuffer - error:",GetLastError());
      ResetLastError();
      return;
   }

   if( CopyBuffer(IndHandle,1,0,1,BuyBuffer)<0){
   Alert("Error copying  indicator Buybuffer - error:",GetLastError());
      ResetLastError();
      return;
   }
  
   p_close=mrate[1].close;  // bar 1 close price

//--- Declare bool type variables to hold our Buy Conditions
   bool Buy_Condition_1;
   Buy_Condition_1=(BuyBuffer[1]>0);

   double PB,PS;
   if(Buy_Condition_1)
     {
        PB=NormalizeDouble(latest_price.ask,_Digits);
        if(!trade.Buy(Lot,_Symbol,PB,0,0,"Buy By Expert"))
            Print("Buy() failed. Return code=",trade.ResultRetcode(),". Code description: ",trade.ResultRetcodeDescription());
     }
   else     Print("Buy() executed . Return code=",trade.ResultRetcode()," (",trade.ResultRetcodeDescription(),")");
         
        
   bool Sell_Condition_1;
   Sell_Condition_1 =(SellBuffer[1]>0);
   if(Sell_Condition_1)
     {
      PS=NormalizeDouble(latest_price.bid,_Digits);
      if(!trade.Sell(Lot,_Symbol,PS,PS-.25,PS+.25,"Sell By Expert"))
          Print("Sell() failed. Return code=",trade.ResultRetcode(),". Code description: ",trade.ResultRetcodeDescription());
    }
    else   Print("Sell() executed successfully. Return code=",trade.ResultRetcode()," (",trade.ResultRetcodeDescription(),")");
   
   return;
  
  }

//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   IndicatorRelease(IndHandle);
  }  
//+------------------------------------------------------------------+

Every Helps is appricate.

Documentation on MQL5: MQL5 programs / Runtime Errors
Documentation on MQL5: MQL5 programs / Runtime Errors
  • www.mql5.com
The executing subsystem of the client terminal has an opportunity to save the error code in case it occurs during a MQL5 program run. There is a predefined variable _LastError for each executable MQL5 program. Before starting the OnInit function, the _LastError variable is reset...
 
Boobwood:

This Is My Expert Code.

I am going to have Automatic Trading according another indicator data (Test_SellBuyPoints).

Whenever I want do debug my code and step by step running, I give "Critical error occurred, debugging is stopped", message in line 38 of my code which is 

nothing is wrong with this simple "if" statement!

Here is my Complete Code

Every Helps is appricate.

Maybe something is wrong with the IndHandle value which you use in the if-statement? Have you tried to insert a print statement on line 37 to print its current value?
 
WindmillMQL:
Maybe something is wrong with the IndHandle value which you use in the if-statement? Have you tried to insert a print statement on line 37 to print its current value?

The IndHandle is 10

Nothing wrong with its value.