Download MetaTrader 5

Custom indicator in different colors

To add comments, please log in or register
Articles can help you to solidify your knowledge. Come and read them!
Gabor Keresztessy
201
Gabor Keresztessy 2015.10.08 20:33 

Hi, please help me, I am confused:

I try to make a custom indicator based on MACD. If the MACD gives a long signal, my custom indicator gets green histogram, at short signal get red color.

Everything is working except the color changing. I tried this code to change indicator color:

if (macd_line[i] > macd_signal_line[i])
      {
      SetIndexStyle(0,DRAW_HISTOGRAM,0,2,Green);
      macd_true[i]=1;
      }
if (macd_line[i] < macd_signal_line[i])
      {
      SetIndexStyle(0,DRAW_HISTOGRAM,0,2,Red);
      macd_true[i]=-1;
      }
I got as custom indicator +1 and -1 values, but the color of the histogram did not change, stay at green always. What's the problem?
whroeder1
14517
whroeder1 2015.10.09 00:33  
A histogram is always two buffers, histogram and the next; from A to B is one color, from B to A is the other.
Gabor Keresztessy
201
Gabor Keresztessy 2015.10.09 19:07  
WHRoeder:
A histogram is always two buffers, histogram and the next; from A to B is one color, from B to A is the other.
Thx! It works!
Gabor Keresztessy
201
Gabor Keresztessy 2015.10.10 14:01  

I got wrong data.

The basic situation: I try to make a custom indicator that shows the long or short based on higher timeframe. I like to know on M5 chart without changing timeframe what is it on H1. But the code has some errors, please help me to find:

//+------------------------------------------------------------------+
//|                                                        total.mq4 |
//|                        Copyright 2015, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2015, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_separate_window
#property  indicator_buffers 2
#property  indicator_color1  Green
#property  indicator_color2  Red

enum enum_trading_timeframe_type{
         M1 = 1,
         M5 = 5,
         M15 = 15,
         M30 = 30,
         H1 = 60,
         H4 = 240,
         D1 = 1440,
         W1 = 10080,
         MN1 = 43200,
};

input enum_trading_timeframe_type               MA_tf = H1;
input int                                       FastMAPeriod = 12;
input int                                       SlowMAPeriod = 26;
input int                                       SignalMAPeriod = 9;  
double macd_line[], macd_signal_line[];
//double macd_value[];
double macd_true_long[],macd_true_short[];  
int MA_tf_bar=0;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   IndicatorDigits(Digits+1);
//--- drawing settings
//   SetIndexStyle(0,DRAW_LINE,0,1,clrWhite);
//   SetIndexDrawBegin(0,0);
//   SetIndexDrawBegin(1,0);
   
//--- indicator buffers mapping
   SetIndexBuffer(0,macd_true_long);
   SetIndexBuffer(1,macd_true_short);
//--- name for DataWindow and indicator subwindow label
   IndicatorShortName("MACD True ("+IntegerToString(FastMAPeriod)+","+IntegerToString(SlowMAPeriod)+","+IntegerToString(SignalMAPeriod)+")");
   SetIndexLabel(0,"MACD True_long");
   SetIndexLabel(1,"MACD True_short");

   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| 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[])
  {
int i,limit,oszto,ii2;

ArraySetAsSeries(macd_true_long,false);
ArraySetAsSeries(macd_true_short,false);
ArraySetAsSeries(macd_line,false);
ArraySetAsSeries(macd_signal_line,false);
ArrayResize(macd_line,100000);
ArrayResize(macd_signal_line,100000);
ArrayResize(macd_true_long,100000);
ArrayResize(macd_true_short,100000);

//---
oszto=MA_tf/Period();
//MA_tf_bar=limit/oszto;
if(prev_calculated>1)
      limit=prev_calculated-1;
      else limit=0;
for(i=limit; i<rates_total; i++)
   {
   for (ii2=0; ii2<oszto; ii2++)
   {
      macd_line[i+ii2]=iCustom(Symbol(),MA_tf,"MACD True",FastMAPeriod,SlowMAPeriod,SignalMAPeriod,0,MA_tf_bar);
      macd_signal_line[i+ii2]=iCustom(Symbol(),MA_tf,"MACD True",FastMAPeriod,SlowMAPeriod,SignalMAPeriod,1,MA_tf_bar);
      
     if (macd_line[i+ii2] > macd_signal_line[i+ii2])
      {
      SetIndexStyle(0,DRAW_HISTOGRAM,EMPTY,2,indicator_color1);
      macd_true_long[i+ii2]=1;
      }
      if (macd_line[i+ii2] < macd_signal_line[i+ii2])
      {
      SetIndexStyle(1,DRAW_HISTOGRAM,EMPTY,2,indicator_color2);
      macd_true_short[i+ii2]=-1;
      }
   }
//   Print(macd_line[i]);
   i=i+oszto;
   MA_tf_bar++;
   }
//---
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

To add comments, please log in or register