MA with two colors

fxwalb

Master Trader
Dec 18, 2009
31
0
62
Hi to all!

Is there anybody around who can help me with my code for MT5 platform. I tried to code a MA with two colors for up and down direction.
But there is still only the updirection on my chart. Must be a bug somewhere.
Here is my code so far.


MQL5:
//+------------------------------------------------------------------+
//|                                              TestCopyBuffer1.mq5 |
//|                        Copyright 2009, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "2009, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   1
 
//---- plot MA
 
#property indicator_label1  "MA"
#property indicator_type1   DRAW_LINE
#property indicator_color1  Lime, Red
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
 
 
//--- input parameters
 
 
input int                MA_Period=15;
input ENUM_MA_METHOD     MA_Mode=MODE_EMA;
input ENUM_APPLIED_PRICE MA_Price=PRICE_CLOSE;
input int                MA_Shift=0;
 
 
 
 
 
 
//--- indicator buffers
double                   MABuffer[];
double                   dUpMABuffer[];
double                   dDownMABuffer[];
 
 
int                      ma_handle;
 
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
 
 
   SetIndexBuffer(0,dUpMABuffer,INDICATOR_DATA);
   SetIndexBuffer(1,dDownMABuffer,INDICATOR_DATA);
 
 
   IndicatorSetString(INDICATOR_SHORTNAME,"MA["+MA_Period+"]");
//---
   ma_handle=iMA(Symbol(),0,MA_Period,MA_Shift,MA_Mode,MA_Price);
 
   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[])
  {
//--- Copy the values of the moving average in the buffer MABuffer
 
 
 
   int copied=CopyBuffer(ma_handle,0,0,rates_total,MABuffer);
 
//---
for (int i = rates_total; i > 1; i--)
   {
      dUpMABuffer[rates_total - i + 1] = 0;
      dDownMABuffer[rates_total - i + 1] = 0;
 
      double myMA_now      = MABuffer[rates_total - i+1];
      double myMA_previous = MABuffer[rates_total - i]; //MA one bar ago
 
      if (myMA_now >= myMA_previous)  {
      dUpMABuffer[rates_total - i + 1] = myMA_now;
 
      if(dUpMABuffer[rates_total - i ]==0.0) dUpMABuffer[rates_total - i ]=myMA_previous;
      }
 
      if (myMA_now < myMA_previous)  {
      dDownMABuffer[rates_total - i + 1] = myMA_now;
 
       if(dDownMABuffer[rates_total - i]==0.0) dDownMABuffer[rates_total - i ]=myMA_previous;
      }
      }
//----
 
 
   //--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
Another version of the indi is this, which draws a line without changing colors.


MQL5:
//+------------------------------------------------------------------+
//|                                              TestCopyBuffer1.mq5 |
//|                        Copyright 2009, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "2009, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_plots   1
 
//---- plot MA
 
#property indicator_label1  "MA"
#property indicator_type1   DRAW_LINE
#property indicator_color1  Lime, Red
#property indicator_style1  STYLE_SOLID
#property indicator_width1  2
 
 
//--- input parameters
 
 
input int                MA_Period=15;
input ENUM_MA_METHOD     MA_Mode=MODE_EMA;
input ENUM_APPLIED_PRICE MA_Price=PRICE_CLOSE;
input int                MA_Shift=0;
 
 
 
 
 
 
//--- indicator buffers
double                   MABuffer[];
double                   ExtColorBuffer[];
 
 
int                      ma_handle;
 
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
 
 
   SetIndexBuffer(0,MABuffer,INDICATOR_DATA);
   SetIndexBuffer(1,ExtColorBuffer,INDICATOR_COLOR_INDEX);
 
 
 
   IndicatorSetString(INDICATOR_SHORTNAME,"MA["+MA_Period+"]");
//---
   ma_handle=iMA(Symbol(),0,MA_Period,MA_Shift,MA_Mode,MA_Price);
 
   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[])
  {
//--- Copy the values of the moving average in the buffer MABuffer
 
 
 
   int copied=CopyBuffer(ma_handle,0,0,rates_total,MABuffer);
 
//---
for (int i = rates_total; i > 1; i--)
   {
 
 
      if (MABuffer[rates_total-i] >= MABuffer[rates_total-i+1]) ExtColorBuffer[rates_total-i+1]=0.0;  // set color Lime
       else                         ExtColorBuffer[rates_total-i+1]=1.0;            // set color Red
     }
 
 
//----
 
 
   //--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
 
Last edited by a moderator:

Enivid

Administrator
Staff member
Nov 30, 2008
19,164
1,494
144
Odesa
www.earnforex.com
The main reason for not drawing in color is that you've set the type of the plot to DRAW_LINE instead of DRAW_COLOR_LINE. But there are also some other minor errors in the code. Please, have a look at the attached Color_MA that is based on your code, but has no errors.

By the way, now it's possible to attach .mq5 files to the posts, so no need to list the code inside the forum messages.
 

Attachments

  • Color_MA.mq5
    2.6 KB · Views: 94

fxwalb

Master Trader
Dec 18, 2009
31
0
62
The main reason for not drawing in color is that you've set the type of the plot to DRAW_LINE instead of DRAW_COLOR_LINE. But there are also some other minor errors in the code. Please, have a look at the attached Color_MA that is based on your code, but has no errors.

By the way, now it's possible to attach .mq5 files to the posts, so no need to list the code inside the forum messages.


Yes this code is working properly, thanks.

I tried to plot the MA value on the chart with


ObjectSetString (0, "myMA_now_1", OBJPROP_TEXT,"MA ["+IntegerToString(MA_Period)+"] @ "+DoubleToString(MATempBuffer[0],4));


Unfortunatly the value is way off the actual reading.
What can I do?
 
Last edited:

fxwalb

Master Trader
Dec 18, 2009
31
0
62
MATempBuffer[0] contains the value of MA for the oldest bar (if timeseries isn't set to true on the buffer, and in my Color_MA indicator it's not). You have to use MATempBuffer[rate_total-1] in this case.

Thank you.
 

xx2

Trader
Jul 1, 2011
1
0
12
if you do not want only 200 bar length but the full windows, then use this little modification