Indicator: Calculating Standard Deviations Between Moving Averages

 

I am trying to create an indicator that calculates the average distance between MA's, display those calculations in STDeviation form, and the creates a buy/sell signal if the deviation threshold variable is exceeded. However I am stuck on the errors...

OnCalculate function declared with wrong type or/and parameters 57 8

OnCalculate function not found in custom indicator 1 1

I am new to coding and if anyone could point me in the right direction to correcting these errors, I would greatly appreciate it.

//+------------------------------------------------------------------+
//|                    Moving Average Distance Deviation.mq5 |
//|                                Copyright 2022, Richard Keith    |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, Richard Keith"
#property version   "1.00"
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   2

input group "General Settings";
input int calcBarsBack = 25;
input double deviationThreshold = 2.0;

input group "Slow MA Settings";
input int slowPeriod = 20;
input int slowMaShift = 0;

input group "Fast MA Settings";
input int fastPeriod = 5;
input int fastMaShift = 0;

//--- plot slowMa
#property indicator_label1  "slowMa"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1

//--- plot fastMa
#property indicator_label2  "fastMa"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrLime
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1

//--- indicator buffers
double         slowMaBuffer[];
double         fastMaBuffer[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
   //--- indicator buffers mapping
   SetIndexBuffer(0,slowMaBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,fastMaBuffer,INDICATOR_DATA);
   
   //---
   return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Custom indicator iteration function                           |
//+------------------------------------------------------------------+
double OnCalculate(const int rates_total,
                   const int prev_calculated,
                   const MqlRates& rates[]){
   int limit;
   if(prev_calculated == 0)
      limit = rates_total - 1;
   else
      limit = rates_total - prev_calculated;

   ArrayInitialize(fastMaBuffer, 0);
   ArrayInitialize(slowMaBuffer, 0);

   for(int i = rates_total - 1; i >= 0; i--)
   {
      // calculate slow and fast moving averages
      double slowMa = iMA(_Symbol, PERIOD_CURRENT, slowPeriod, slowMaShift, MODE_SMA, PRICE_CLOSE);
      double fastMa = iMA(_Symbol, PERIOD_CURRENT, fastPeriod, fastMaShift, MODE_SMA, PRICE_CLOSE);

      // calculate difference between moving averages
      double diff = slowMa - fastMa;

      // calculate average distance between moving averages over a defined period
      double sumDist = 0;
      for(int j = i; j < i + calcBarsBack; j++)
      {
         double slowMaJ = iMA(_Symbol, PERIOD_CURRENT, slowPeriod, slowMaShift, MODE_SMA, PRICE_CLOSE);
         double fastMaJ = iMA(_Symbol, PERIOD_CURRENT, fastPeriod, fastMaShift, MODE_SMA, PRICE_CLOSE);
         double dist = slowMaJ - fastMaJ;
         sumDist += dist;
      }
      double avgDist = sumDist / calcBarsBack;

      // calculate deviation from average distance
      double deviation = diff - avgDist;

      // generate buy/sell signals based on deviation threshold
      if(deviation > deviationThreshold)
      {
         // generate sell signal
         fastMaBuffer[i] = rates[i].close;
         slowMaBuffer[i] = 0;
      }
      else if(deviation < -deviationThreshold)
      {
         // generate buy signal
         slowMaBuffer[i] = rates[i].close;
         fastMaBuffer[i] = 0;
      }
      else
      {
         // no signal
         slowMaBuffer[i] = slowMa;
         fastMaBuffer[i] = fastMa;
      }
   }

   return(rates_total);
}
Documentation on MQL5: Language Basics / Preprocessor / Program Properties (#property)
Documentation on MQL5: Language Basics / Preprocessor / Program Properties (#property)
  • www.mql5.com
Program Properties (#property) - Preprocessor - Language Basics - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
      double slowMa = iMA(_Symbol, PERIOD_CURRENT, slowPeriod, slowMaShift, MODE_SMA, PRICE_CLOSE);
A mixture of MT4 (returning a double) and MT5 (iMA handle).
Stop using ChatGPT.
          Help needed to debug and fix an AI EA - Trading Systems - MQL5 programming forum (2023)

ChatGPT (the worst), “Bots Builder”, “EA builder”, “EA Builder Pro”, EATree, “Etasoft forex generator”, “Forex Strategy Builder”, ForexEAdvisor (aka. ForexEAdvisor STRATEGY BUILDER, and Online Forex Expert Advisor Generator), ForexRobotAcademy.com, forexsb, “FX EA Builder”, fxDreema, Forex Generator, FxPro, Molanis, Octa-FX Meta Editor, Strategy Builder FX, Strategy Quant, “Visual Trader Studio”, “MQL5 Wizard”, etc., are all the same. You will get something quick, but then you will spend a much longer time trying to get it right, than if you learned the language up front, and then just wrote it.

Since you haven't learned MQL4/5, therefor there is no common language for us to communicate. If we tell you what you need, you can't code it. If we give you the code, you don't know how to integrate it into yours.

We are willing to HELP you when you post your attempt (using Code button) and state the nature of your problem, but we are not going to debug your hundreds of lines of code. You are essentially going to be on your own.

ChatGPT
  1. Even it says do not use it for coding.*
  2. Mixing MT4 and MT5 code together.
  3. Creating multiple OnCalculate/OnTick functions.
  4. OnCalculate returning a double.
  5. Filling buffers with zero in OnInit (they have no size yet). Setting buffer elements to zero but not setting Empty Value to correspond.
  6. Calling undefined functions.
  7. Sometimes, not using strict (MT4 code).
  8. Code that will not compile.
  9. Creating code outside of functions.*
  10. Creating incomplete code.*
  11. Initialization of Global variables with non-constants.*
  12. Assigning a MT5 handle to a double or missing the buffer and bar indexes in a MT4 call.*
  13. Useing MT4 Trade Functions without first selecting an order.*
  14. Uses NULL in OrderSend.*
bot builder Creating two OnInit() functions.*
EA builder
  1. Counting up while closing multiple orders.
  2. Not useing time in new bar detection.
  3. Not adjusting for 4/5 digit brokers, TP/SL and slippage.*
  4. Not checking return codes.
EATree Uses objects on chart to save values — not persistent storage (files or GV+Flush.) No recovery (crash/power failure.)
ForexEAdvisor
  1. Non-updateing global variables.
  2. Compilation errors.
  3. Not checking return codes.
  4. Not reporting errors.
FX EA Builder
  1. Not checking return codes.
  2. Loosing open tickets on terminal restart. No recovery (crash/power failure.)
  3. Not adjusting stops for the spread .*
  4. Using OrdersTotal directly.
  5. Using the old event handlers.
 
William Roeder #:
A mixture of MT4 (returning a double) and MT5 (iMA handle).
Stop using ChatGPT.
          Help needed to debug and fix an AI EA - Trading Systems - MQL5 programming forum (2023)

ChatGPT (the worst), “Bots Builder”, “EA builder”, “EA Builder Pro”, EATree, “Etasoft forex generator”, “Forex Strategy Builder”, ForexEAdvisor (aka. ForexEAdvisor STRATEGY BUILDER, and Online Forex Expert Advisor Generator), ForexRobotAcademy.com, forexsb, “FX EA Builder”, fxDreema, Forex Generator, FxPro, Molanis, Octa-FX Meta Editor, Strategy Builder FX, Strategy Quant, “Visual Trader Studio”, “MQL5 Wizard”, etc., are all the same. You will get something quick, but then you will spend a much longer time trying to get it right, than if you learned the language up front, and then just wrote it.

Since you haven't learned MQL4/5, therefor there is no common language for us to communicate. If we tell you what you need, you can't code it. If we give you the code, you don't know how to integrate it into yours.

We are willing to HELP you when you post your attempt (using Code button) and state the nature of your problem, but we are not going to debug your hundreds of lines of code. You are essentially going to be on your own.



Thank you very much William. Your reply was quick and very much needed. I have been trying to soak up as much information as I can when attempting to learn. I have zero dollars for a legit education in coding at the moment. Chat GPT came out and believe it or not, I am learning. Granted it is not the proper foundation for any education, but I will continue to learn what I can, when I can, and how I can. The chart you provided is of big help and much appreciated.

 
LEAHI #:

Thank you very much William. Your reply was quick and very much needed. I have been trying to soak up as much information as I can when attempting to learn. I have zero dollars for a legit education in coding at the moment. Chat GPT came out and believe it or not, I am learning. Granted it is not the proper foundation for any education, but I will continue to learn what I can, when I can, and how I can. The chart you provided is of big help and much appreciated.

Learn to search here. Searching is our ChatGPT and the results are lot better and they all do work :)

https://www.mql5.com/en/search

There's virtually nothing that hasn't already been programmed for MT4/MT5 and is ready even for you!

Reason: