custom indicator color, not working

 

Hi,


I want to have a Custom moving average with a specific color, following this post https://www.mql5.com/en/code/19864 I have done the following:

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

int handle_iMA_First;
int OnInit()
  {
   handle_iMA_First=iCustom(_Symbol,PERIOD_CURRENT,"Custom Moving Average Input Color",21,0,MODE_SMMA,clrYellow, PRICE_CLOSE);
   ChartIndicatorAdd(0,0,handle_iMA_First);


   int subwindow=(int)ChartGetInteger(0,CHART_WINDOWS_TOTAL);
   return 0;
   }

void OnDeinit(const int reason)
  {

  }
void OnTick()
  {
  }


But nothing on the chart..




Any idea?


Regards

Custom Moving Average Input Color
Custom Moving Average Input Color
  • www.mql5.com
A modification of the "Custom Moving Average" indicator: now the line color can be passed in input parameters.
 
AYMERIC75: But nothing on the chart..
  1. Check your return codes (iCustom), and report your errors . Don't look at GLE/LE unless you have an error.

  2. Your indicator has four (4) parameters, your iCustom passes five (5).

 
William Roeder #:
iCustom

Thanks,


1)

I have added this code:

    if(handle_iMA_First==INVALID_HANDLE)
     {
      //--- tell about the failure and output the error code 
      PrintFormat("Failed to create handle of the iMA indicator for the symbol %s/%s, error code %d",
                  Symbol(),
                  EnumToString(Period()),
                  GetLastError());
      //--- the indicator is stopped early 
      return(INIT_FAILED);
     }
   


And I get


Failed to create handle of the iMA indicator for the symbol BTCUSD/PERIOD_H1, error code 4802

 
AYMERIC75 #I have added this code: And I get — Failed to create handle of the iMA indicator for the symbol BTCUSD/PERIOD_H1, error code 4802

Always show all relevant code. What you have shown is insufficient and incomplete to be able to analyse the issue.

Please note the following from the documentation:

name

[in]  Custom indicator name. If the name starts with the reverse slash '\', the EX5 indicator file is searched for relative to the MQL5\Indicators indicator root directory. Thus, when calling iCustom(Symbol(), Period(), "\FirstIndicator"...), the indicator is downloaded as MQL5\Indicators\FirstIndicator.ex5. If the path contains no file, the error 4802 (ERR_INDICATOR_CANNOT_CREATE) occurs.

 
Fernando Carreiro #:
the error 4802 (ERR_INDICATOR_CANNOT_CREATE) occurs.

Full code:


#include <Trade/Trade.mqh>

int OnInit()
  {
  
   handle_iMA_First=iCustom(_Symbol,PERIOD_CURRENT,"Custom Moving Average Input Color",21,0,MODE_SMMA,clrYellow, PRICE_CLOSE);
      
      Print(handle_iMA_First);
   
    if(handle_iMA_First==INVALID_HANDLE)
     {
      PrintFormat("Failed to create handle of the iMA indicator for the symbol %s/%s, error code %d",
                  Symbol(),
                  EnumToString(Period()),
                  GetLastError());
      return(INIT_FAILED);
     }
    ChartIndicatorAdd(0,0,handle_iMA_First);
    return 0;

}

void OnDeinit(const int reason)
  {

  }

void OnTick()
  {

}
   
   



So instead of "Custom Moving Average Input Color" I should put a real indicator that exists in MQL5/indicators... will try this, thanks!

 
AYMERIC75 #: Full code:

Why do you ask for help and then ignore the advise?

William Roeder #: Your indicator has four (4) parameters, your iCustom passes five (5).
AYMERIC75 #: So instead of "Custom Moving Average Input Color" I should put a real indicator that exists in MQL5/indicators

No, you should use the intended Indicator (compiled), placed it in the correct location.

 
Fernando Carreiro #:

Why do you ask for help and then ignore the advise?

No, you should use the intended Indicator (compiled), placed it in the correct location.


My EA:

int OnInit()
  {
 
   handle_iMA_First=iCustom(_Symbol,PERIOD_CURRENT,"CustomSMA",200, clrRed);
   ChartIndicatorAdd(0,0,handle_iMA_First);

}


The indicator called "CustomSMA" and placed in the indicator folder:


//+------------------------------------------------------------------+
#property copyright "Aymeric"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window

#property indicator_buffers   1
#property indicator_plots     1
#property indicator_type1  DRAW_LINE
#property indicator_label1 "SMA"
#property indicator_style1 STYLE_SOLID
#property indicator_width1 4


input int InpMAPeriod;                   
input color indicator_color;
ENUM_MA_METHOD MAMode = MODE_SMMA;
double Buffer[];
int MaxPeriod;
int Handle;


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0, Buffer, INDICATOR_DATA);
   MaxPeriod = (int)(InpMAPeriod);
   Handle = iMA(Symbol(), Period(), InpMAPeriod, 0, MAMode, PRICE_CLOSE);   
   PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, MaxPeriod);

//---
   return(INIT_SUCCEEDED);
  }

  void OnDeinit(const int reason){
   if(Handle!=INVALID_HANDLE) IndicatorRelease(Handle);
  }


//+------------------------------------------------------------------+
//| 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[])
  {
//---

   if (IsStopped()) return(0);
   
   if (rates_total<MaxPeriod) return(0); // check that we have enough bars available to calculate the MAs
   
   // check that the moving averages have all been calculated
   if(BarsCalculated(Handle)<rates_total) return(0);
   
   
   int copyBars = 0;
   
   if(prev_calculated>rates_total || prev_calculated<=0) {
      copyBars = rates_total;
   } else {
      copyBars = rates_total-prev_calculated;
      if(prev_calculated>0) copyBars++;
   }
   
   if (IsStopped()) return(0);
   
   if(CopyBuffer(Handle, 0, 0, copyBars, Buffer) <= 0) return(0);

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

  


When I attach the EA  to the chart I get:




So, apparently the custom indicator does not take into account the color passed to iCustom function (ClrRed)


Regards

 
AYMERIC75 #: So, apparently the custom indicator does not take into account the color passed to iCustom function (ClrRed)

Correct. The Indicator code you have shown ignores the parameter "indicator_color". The parameter is never used elsewhere in the code. You will need to fix it.

To set the colour of a plot in an indicator, you need to use the PlotIndexSetInteger function ...

PlotIndexSetInteger( plot_index, PLOT_LINE_COLOR, indicator_color ); // Drawing Colour
Documentation on MQL5: Custom Indicators / PlotIndexSetInteger
Documentation on MQL5: Custom Indicators / PlotIndexSetInteger
  • www.mql5.com
PlotIndexSetInteger - Custom Indicators - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Fernando Carreiro #:
PLOT_LINE_COLOR

Thanks a lot!