Arrow colour

 

Hi,

I am a complete noob, and trying to learn how to code an indicator.

At the moment I am simply trying to draw an arrow when a moving average cross occurs. It is working, except that I would like the colour of the arrow to correspond with the direction of the cross.

I have tried the simplest method that I believe should work, which I am presenting here, and several others, but the arrow always takes the first colour that I have defined.

I am sure that I am only missing a very small little detail. Any help will be most welcome, please.


#property copyright "2009-2020, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   1
//--- plot Cross
#property indicator_label1  "Cross"
#property indicator_type1   DRAW_ARROW
#property indicator_color1  Green,Red
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- input parameters
input int      SlowPeriod=13;
input int      FastPeriod=48;
//--- indicator buffers
double         CrossBuffer[];
double         CrossColour[];
//---
int            FastHandle;
int            SlowHandle;

int            Shift=20;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,CrossBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,CrossColour,INDICATOR_COLOR_INDEX);
//--- setting a code from the Wingdings charset as the property of PLOT_ARROW
   PlotIndexSetInteger(0,PLOT_ARROW_SHIFT,Shift);
   PlotIndexSetInteger(0,PLOT_ARROW,225);
   
   FastHandle = iMA(_Symbol, _Period, FastPeriod, 0, MODE_EMA, PRICE_CLOSE);
   SlowHandle = iMA(_Symbol, _Period, SlowPeriod, 0, MODE_EMA, PRICE_CLOSE);
   
//---
   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[])
  {
//---

      double         Fast[];
      double         Slow[];
      int            SlowCnt;
      int            FastCnt;

      if (prev_calculated<=0) {
         SlowCnt = CopyBuffer(SlowHandle, 0, 0, rates_total, Slow);
         FastCnt = CopyBuffer(FastHandle, 0, 0, rates_total, Fast);
         
         if (SlowCnt > 0 && FastCnt > 0) {
            for (int i=0; i<rates_total; i++) {
               if (i<SlowPeriod+2) {
                  CrossBuffer[i] = 0;
                  CrossColour[i] = 0;
               } else {
                  if (Fast[i] > Slow[i] && Fast[i-1] <= Slow[i-1]) {
                     CrossBuffer[i] = low[i];
                     CrossColour[i] = 0;
                  } else if (Fast[i] < Slow[i] && Fast[i-1] >= Slow[i-1]) {
                     CrossBuffer[i] = low[i];
                     CrossColour[i] = 1;
                  } else {
                     CrossBuffer[i] = 0;
                     CrossColour[i] = -1;
                  }
               }
            }
         }
         
      } else {
      
         SlowCnt = CopyBuffer(SlowHandle, 0, 0, 2, Slow);
         FastCnt = CopyBuffer(FastHandle, 0, 0, 2, Fast);
         
         // Print("Fast[1] ", Fast[1], "Slow[1] ", Slow[1]);
         // Print("Fast[0] ", Fast[0], "Slow[0] ", Slow[0]);
         
         if (SlowCnt > 0 && FastCnt > 0) {
            if (Fast[1] > Slow[1] && Fast[0] <= Slow[0]) {
               CrossBuffer[rates_total-1] = low[rates_total-1];
               CrossColour[rates_total-1] = 0;
            } else if (Fast[1] < Slow[1] && Fast[0] >= Slow[0]) {
               CrossBuffer[rates_total-1] = low[rates_total-1];
               CrossColour[rates_total-1] = 1;
            } else {
               CrossBuffer[rates_total-1] = 0;
               CrossColour[rates_total-1] = -1;
            }
         }
      }
  
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
 

You have green and red.

These are CrossColour[0] = Green and CrossColour[1] = Red.

So you can not do CrossColour[-1]  

 
Marco vd Heijden:

You have green and red.

These are CrossColour[0] = Green and CrossColour[1] = Red.

So you can not do CrossColour[-1]  

Hi, and thanks for the response.

I agree that the -1 is not sensible, but it is a value used only when no arrow is drawn, i.e. CrossBuffer[i] is 0 at that point.

I did actually set it as zero for no arrow initially, and the -1 was simply my attempts at trying to find the problem. Using a 0 or 1 gives the same result.

Thanks again.

 

Hello you can see example here: https://www.mql5.com/en/articles/135

//+------------------------------------------------------------------+
//|                                                   cand_color.mq5 |
//|                                                             ProF |
//|                                                          http:// |
//+------------------------------------------------------------------+
#property copyright "ProF"                      //Author
#property indicator_chart_window                //Indicator in separate window

                                          //Specify the number of buffers of the indicator
//4 buffer for candles + 1 color buffer + 1 buffer to serve the RSI data
#property indicator_buffers 6

//Specify the names in the Data Window
#property indicator_label1 "Open;High;Low;Close"

#property indicator_plots 1                     //Number of graphic plots
#property indicator_type1 DRAW_COLOR_CANDLES    //Drawing style - color candles
#property indicator_width1 3                    //Width of the graphic plot (optional)

                                          //Declaration of buffers
double buffer_open[],buffer_high[],buffer_low[],buffer_close[]; //Buffers for data
double buffer_color_line[];    //Buffer for color indexes
double buffer_tmp[1];           //Temporary buffer for RSI data copying
double buffer_RSI[];            //Indicator buffer for RSI
int handle_rsi=0;               //Handle for the RSI indicator
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
/**
        *       The order of the buffers assign is VERY IMPORTANT!
        *  The data buffers are first
        *       The color buffers are next
        *       And finally, the buffers for the internal calculations.
        */
//Assign the arrays with the indicator's buffers
   SetIndexBuffer(0,buffer_open,INDICATOR_DATA);
   SetIndexBuffer(1,buffer_high,INDICATOR_DATA);
   SetIndexBuffer(2,buffer_low,INDICATOR_DATA);
   SetIndexBuffer(3,buffer_close,INDICATOR_DATA);

//Assign the array with color indexes with the indicator's color indexes buffer
   SetIndexBuffer(4,buffer_color_line,INDICATOR_COLOR_INDEX);

//Assign the array with the RSI indicator data buffer
   SetIndexBuffer(5,buffer_RSI,INDICATOR_CALCULATIONS);

//Define the number of color indexes, used for a graphic plot
   PlotIndexSetInteger(0,PLOT_COLOR_INDEXES,2);

//Set color for each index
   PlotIndexSetInteger(0,PLOT_LINE_COLOR,0,Blue);   //Zeroth index -> Blue
   PlotIndexSetInteger(0,PLOT_LINE_COLOR,1,Orange); //First index  -> Orande
   
//Get handle of RSI indicator, it's necessary to get the RSI indicator values
   handle_rsi=iCustom(_Symbol,_Period,"Examples\\RSI");
   return(0);
  }
//+------------------------------------------------------------------+
//| 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[])
  {
//In the loop we fill the data buffers and color indexes buffers for each bar
   for(int i=prev_calculated;i<=rates_total-1;i++)
     {
      //Copying the RSI indicator's data to the temporary buffer - buffer_tmp
      CopyBuffer(handle_rsi,0,BarsCalculated(handle_rsi)-i-1,1,buffer_tmp);
      //Copying the values from the temporary buffer to the indicator's buffer
      buffer_RSI[i]=buffer_tmp[0];

      //Set data for plotting
      buffer_open[i]=open[i];  //Open price
      buffer_high[i]=high[i];  //High price
      buffer_low[i]=low[i];    //Low price
      buffer_close[i]=close[i];//Close price

                               //Add a simple condition -> If RSI less 50%:
      if(buffer_RSI[i]<50)
        {   buffer_color_line[i]=0;   } //Assign the bar with color index, equal to 0
      else
        {  buffer_color_line[i]=1;   }  //Assign the bar with color index, equal to 1
     }
   return(rates_total-1); //Return the number of calculated bars, 
                         //Subtract 1 for the last bar recalculation
  }
//+------------------------------------------------------------------+
Creating Multi-Colored Indicators in MQL5
Creating Multi-Colored Indicators in MQL5
  • www.mql5.com
In this article, we will consider how to create multi-colored indicators or convert the existing ones to multi-color. MQL5 allows to represent the information in the convenient form. Now it isn't necessary to look at a dozen of charts with indicators and perform analyses of the RSI or Stochastic levels, it's better just to paint the candles with different colors depending on the values of the indicators.
 
Marco vd Heijden:

Hello you can see example here: https://www.mql5.com/en/articles/135

Thanks for the link.

I used that article and several others and some examples in my attempts to get as far as I have. I cannot see what I am doing that is not the same as in the article.

 
Vrassoc:

Thanks for the link.

I used that article and several others and some examples in my attempts to get as far as I have. I cannot see what I am doing that is not the same as in the article.

#property indicator_type1   DRAW_ARROW

replace it with 

#property indicator_type1   DRAW_COLOR_ARROW

And, as you have already been told : color index -1 does not exist. Color indexes are going from 0 to +nnn (depending how much colors you have)

 
Mladen Rakic:

replace it with 

And, as you have already been told : color index -1 does not exist. Color indexes are going from 0 to +nnn (depending how much colors you have)

Thank you SO much! I knew it was some tiny thing that I was overlooking.

Much appreciated that you took the time to check my code.