Custom Indicator sending invalid data to tester and EA

 

Hi All,

First time reaching out on the forum. I normally try fixing issues by using the documentation and other online references but this has proven fruitless this time around.

I have created a custom indicator based on a MA and some calculations (Code below). When I apply it to the screen, it works fine (Screenshot 1 attached). I have created an EA to use BufferSlope[] from the custom indicator, passed through using the iCustom() function, as a signal. When testing the EA and printing out the value of the BufferSlope[i], I am getting nothing but zeroes. So I then ran the indicator through the tester and got the same results, zeroes. (Screenshot 2 attached) It loads and plots data fine prior to the testing period but as soon as the test starts, nothing but zeroes.

I have created a custom indicator before based on several MA's which create bands based on ATR and it works fine. I am currently using that indicator in an EA which is currently running flawlessly on a chart. I have compared everything about the 2 indicators and 2 EA's and cannot find the issue with the new indicator.

Any help you can offer would be greatly appreciated.


/*
     MASlope.mq5
     Copyright 2023, SilverLotto                                                                        |
*/
#property copyright "Copyright 2023, SilverLotto"
#property link      ""
#property version   "1.00"
#property indicator_separate_window

#property indicator_buffers 2
#property indicator_plots 1

#include <MovingAverages.mqh>
//Slope MA plot
#property indicator_type1 DRAW_LINE
#property indicator_label1 "Slope"
#property indicator_color1 clrRed
#property indicator_style1 STYLE_SOLID
#property indicator_width1 1

//Slope MA input parameters
input int InpOffset = 5; //Bars to go back for slope
input double InpLimit = 5; //Slope Min/Max
input double InpLimitFactor = 2; //Slope Limit Factor
input int InpTrendPeriod = 160; //Trend MA Periods
input ENUM_MA_METHOD InpTrendMode = MODE_LWMA; //Trend MA Method
input ENUM_APPLIED_PRICE InpTrendSource = PRICE_CLOSE; //Trend MA Source

//Indicator data buffers
 
   double BufferSlope[];
   double BufferTrend[];

// Internal indicator handles
int      HandleTrend;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   
  SetIndexBuffer(0,BufferSlope);
  SetIndexBuffer(1,BufferTrend,INDICATOR_CALCULATIONS);
  
  ArraySetAsSeries(BufferSlope, true);
  ArraySetAsSeries(BufferTrend, true);
  
  PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,1);

  HandleTrend = iMA(_Symbol, _Period, InpTrendPeriod,0, InpTrendMode, InpTrendSource);
  
//--- set accuracy 
   IndicatorSetInteger(INDICATOR_DIGITS,2); 
//--- set levels 
   IndicatorSetInteger(INDICATOR_LEVELS,5);
   IndicatorSetDouble(INDICATOR_MAXIMUM,20);
   IndicatorSetDouble(INDICATOR_MINIMUM,-20);  
   IndicatorSetDouble(INDICATOR_LEVELVALUE,0,0); 
   IndicatorSetDouble(INDICATOR_LEVELVALUE,1,InpLimit);
   IndicatorSetDouble(INDICATOR_LEVELVALUE,2,InpLimit*-1);
   IndicatorSetDouble(INDICATOR_LEVELVALUE,3,InpLimit*InpLimitFactor);
   IndicatorSetDouble(INDICATOR_LEVELVALUE,4,InpLimit*(-1*InpLimitFactor));
   IndicatorSetInteger(INDICATOR_LEVELCOLOR,0, clrGray);
   IndicatorSetInteger(INDICATOR_LEVELCOLOR,1, clrDodgerBlue);
   IndicatorSetInteger(INDICATOR_LEVELCOLOR,2, clrDodgerBlue);
   IndicatorSetInteger(INDICATOR_LEVELCOLOR,3, clrRed);
   IndicatorSetInteger(INDICATOR_LEVELCOLOR,4, clrRed);
   IndicatorSetInteger(INDICATOR_LEVELSTYLE,0, STYLE_DOT);
   IndicatorSetInteger(INDICATOR_LEVELSTYLE,1, STYLE_DASH);
   IndicatorSetInteger(INDICATOR_LEVELSTYLE,2, STYLE_DASH);
   IndicatorSetInteger(INDICATOR_LEVELSTYLE,3, STYLE_DASHDOT);
   IndicatorSetInteger(INDICATOR_LEVELSTYLE,4, STYLE_DASHDOT);
  
  
   return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason) {

   IndicatorRelease(HandleTrend);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const int begin,
                const double &price[])
  {

//How many bars to calculate
  
   int count = rates_total - prev_calculated;
   if(prev_calculated > 0) count++;
     
   if(CopyBuffer(HandleTrend, 0, 0, count, BufferTrend)<count) return(0);
   
   for(int i = count-1-InpOffset; i >=0; i--) {
      BufferSlope[i] = MathArctan(BufferTrend[i] - BufferTrend[i+InpOffset])/_Point;
      Print(BufferSlope[i]);
   }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
Files:
 
Your topic has been moved to the section: Technical Indicators
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
 
Fernando Carreiro #:
Your topic has been moved to the section: Technical Indicators
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
Ok, thanks.