attach indicator to EA .....

 

I have an heiken ashi indicator which works great by itself & the EA works by itself (without the #include statement) but when I try to attach it to the EA I get "array out of range error". I am learning coding but there are no simple explanations of why this should happen. The indicator has an .mqh extension and is in the Include folder.

Indicator code:

//+------------------------------------------------------------------+

//|                                                   haramitest.mqh |

//|                        Copyright 2016, MetaQuotes Software Corp. |

//|                                             https://www.mql5.com |

//+------------------------------------------------------------------+

#property copyright "Copyright 2016, MetaQuotes Software Corp."

#property link      "https://www.mql5.com"

//+------------------------------------------------------------------+

//| defines                                                          |

//+------------------------------------------------------------------+

// #define MacrosHello   "Hello, world!"

// #define MacrosYear    2010

//+------------------------------------------------------------------+

//| DLL imports                                                      |

//+------------------------------------------------------------------+

// #import "user32.dll"

//   int      SendMessageA(int hWnd,int Msg,int wParam,int lParam);

// #import "my_expert.dll"

//   int      ExpertRecalculate(int wParam,int lParam);

// #import

//+------------------------------------------------------------------+

//| EX5 imports                                                      |

//+------------------------------------------------------------------+

// #import "stdlib.ex5"

//   string ErrorDescription(int error_code);

// #import

//+------------------------------------------------------------------+

//--- indicator settings

#property indicator_chart_window

#property indicator_buffers 5

#property indicator_plots   1

#property indicator_type1   DRAW_COLOR_CANDLES

#property indicator_color1  DodgerBlue, Red, Green

#property indicator_label1  "Heiken Ashi Open;Heiken Ashi High;Heiken Ashi Low;Heiken Ashi Close"

//--- indicator buffers

double ExtOBuffer[];

double ExtHBuffer[];

double ExtLBuffer[];

double ExtCBuffer[];

double ExtColorBuffer[];

//+------------------------------------------------------------------+

//| Custom indicator initialization function                         |

//+------------------------------------------------------------------+

void OnInit()

  {

//--- indicator buffers mapping

   SetIndexBuffer(0,ExtOBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,ExtHBuffer,INDICATOR_DATA);

   SetIndexBuffer(2,ExtLBuffer,INDICATOR_DATA);

   SetIndexBuffer(3,ExtCBuffer,INDICATOR_DATA);

   SetIndexBuffer(4,ExtColorBuffer,INDICATOR_COLOR_INDEX);

//---

   IndicatorSetInteger(INDICATOR_DIGITS,_Digits);

//--- sets first bar from what index will be drawn

   IndicatorSetString(INDICATOR_SHORTNAME,"Heiken Ashi");

//--- sets drawing line empty value

   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);

//--- initialization done

  }

//+------------------------------------------------------------------+

//| Heiken Ashi                                                      |

//+------------------------------------------------------------------+

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 &TickVolume[],

                const long &Volume[],

                const int &Spread[])

  {

   int i,limit;

//--- preliminary calculations

   if(prev_calculated==0)

     {

      //--- set first candle

      ExtLBuffer[0]=Low[0]; <<<<< ERROR STARTS HERE >>>>>

      ExtHBuffer[0]=High[0];

      ExtOBuffer[0]=Open[0];

      ExtCBuffer[0]=Close[0];

      limit=1;

     }

   else limit=prev_calculated-1;


//--- the main loop of calculations

   for(i=limit;i<rates_total && !IsStopped();i++)

     {

      double haOpen=(ExtOBuffer[i-1]+ExtCBuffer[i-1])/2;

      double haClose=(Open[i]+High[i]+Low[i]+Close[i])/4;

      double haHigh=MathMax(High[i],MathMax(haOpen,haClose));

      double haLow=MathMin(Low[i],MathMin(haOpen,haClose));


      ExtLBuffer[i]=haLow;

      ExtHBuffer[i]=haHigh;

      ExtOBuffer[i]=haOpen;

      ExtCBuffer[i]=haClose;


      //--- set candle color

      if(haOpen<haClose) ExtColorBuffer[i]=0.0; // set color DodgerBlue

      else               ExtColorBuffer[i]=1.0; // set color Red

     }

//--- done

   return(rates_total);

  }

//+------------------------------------------------------------------+

EA Code:

#property copyright "Copyright 2016, MetaQuotes Software Corp."

#property link      "https://www.mql5.com"

#property version   "1.00"

#include <haramitest.mqh>

//+------------------------------------------------------------------+

//| Expert initialization function                                   |

//+------------------------------------------------------------------+

void OnTick() 

  { 

//--- 

   double Ask,Bid; 

   int Spread; 

   Ask=SymbolInfoDouble(Symbol(),SYMBOL_ASK); 

   Bid=SymbolInfoDouble(Symbol(),SYMBOL_BID); 

   Spread=SymbolInfoInteger(Symbol(),SYMBOL_SPREAD); 

//--- Output values in three lines 

   Comment(StringFormat("Show prices\nAsk = %G\nBid = %G\nSpread = %d",Ask,Bid,Spread)); 

  }  

Any explanation would be a help & another step along this long learning curve I'm on. Much appreciated 

Automated Trading and Strategy Testing
Automated Trading and Strategy Testing
  • www.mql5.com
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions
 

When #include is used, the indicator doesn't work as normal(buffer resizing etc.).

Use iCustom() in an EA.

int handle;

double High[];
double Low[];
double Close[];

int OnInit()
  {
   handle=iCustom(NULL,0,"Heikin_Ashi");
   
void OnTick()
  {
   if(CopyBuffer(handle,1,0,2,High)<2)
      return;
   ArraySetAsSeries(High,true);
   printf("Current HeikenAshi high=%."+(string)_Digits+"f",High[0]);
   printf("Previous HeikinAshi high=%."+(string)_Digits+"f",High[1]);
 
Ernst Van Der Merwe:

When #include is used, the indicator doesn't work as normal(buffer resizing etc.).

Use iCustom() in an EA.

Thank you, I have been searching for days & none of the tutorials have said anything as simple as that. I have totally been looking at the wrong information, much appreciated
 
Ernst Van Der Merwe:

When #include is used, the indicator doesn't work as normal(buffer resizing etc.).

Use iCustom() in an EA.

Thanks for posting Ernst, i am having a few issues with
"cannot load custom indicator 'Heiken_Ashi' [4802]"

- However I appreciate your post, but for others, I would like to point out the possible typo between
Heiken_Ashi and Heik in_Ashi


 
Nigel Martin:

Thanks for posting Ernst, i am having a few issues with
"cannot load custom indicator 'Heiken_Ashi' [4802]"

- However I appreciate your post, but for others, I would like to point out the possible typo between
Heiken_Ashi and Heik in_Ashi


I am updating my previous post to Ernst. 
The example you provided is now working ok using the "Examples/" path for the Heiken_Ashi indicator. 

HaHandle = iCustom(_Symbol,0, "Examples/Heiken_Ashi.ex5");  

(MetaTrader v5.00 build 2190)

 
thriller372:

I have an heiken ashi indicator which works great by itself & the EA works by itself (without the #include statement) but when I try to attach it to the EA

  1. Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
              Messages Editor

  2. There is no OnCalculate in EAs.
              Event Handling Functions - Functions - Language Basics - MQL4 Reference

  3. Don't try to do that. There are no buffers, no IndicatorCounted() or prev_calculated. No way to know if older bars have changed or been added (history update.)
    Just get the value(s) of the indicator(s) into the EA (using iCustom) and do what you want with it.
    You should encapsulate your iCustom calls to make your code self-documenting.
              Detailed explanation of iCustom - MQL4 programming forum