Newbies Doubts

 

Hi

I'm a newbie to MQL so I decided to try with the newest one, MQL5. I'm trying to follow the newbies information from Articles and make small changes to the indicators and check if it work. I would like to thanks to russian programmers for their expertise and the information they made available. They are awesome. So in my case I decided to change the SMA from newbies article in 2 color SMA depending on trend (up or down).. After a few hours and many attempts I'm forced to give up. It comes in 2 colours but not with the trend. I think that maybe could be created a forum for the newbies and not mix it with the pro's but if there is please tell me about. Or someplace to learn about MQL5.  So the code is in attachement.

 

 

Thanls

 

//+------------------------------------------------------------------+
//|                                                          SMA.mq5 |
//|                        Copyright 2009, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   1
//#property indicator_type1   DRAW_LINE
#property indicator_type1   DRAW_COLOR_LINE
#property indicator_color1  Green, Red
#property indicator_width1  2
  
input int MAPeriod = 13;
input int MAShift = 0; 
  
double ExtLineBuffer[]; 
double dColor[];

input color    Up    = Green;    // bull color (uptrend)
input color    Down  = Red;   // bear color (downtrend)

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+  
void OnInit()
  {
   SetIndexBuffer(0, ExtLineBuffer, INDICATOR_DATA);
   PlotIndexSetInteger(0, PLOT_SHIFT, MAShift);
   PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, MAPeriod - 1);
//---
   SetIndexBuffer(1,dColor,INDICATOR_COLOR_INDEX);
   ArraySetAsSeries(dColor,true);   
// setting the buffer colors
   PlotIndexSetInteger(0,PLOT_LINE_COLOR,0,Up);
   PlotIndexSetInteger(0,PLOT_LINE_COLOR,1,Down);   

  }


//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total, 
                const int prev_calculated, 
                const int begin, 
                const double &price[])
  {
   if (rates_total < MAPeriod - 1)
    return(0);
    
   int first, bar, iii;
   double Sum, SMA;
   
   if (prev_calculated == 0)
    first = MAPeriod - 1 + begin;
   else first = prev_calculated - 1;

   for(bar = first; bar < rates_total; bar++)
    {
     Sum = 0.0;
     iii = 0;

     for(iii = 0; iii < MAPeriod; iii++)
       
      Sum += price[bar - iii];
        
       
     SMA = Sum / MAPeriod;
     ExtLineBuffer[bar] = SMA;
     Comment( " bar  " , ExtLineBuffer[bar], " bar-1 ", ExtLineBuffer[bar-1], " bar +1 " , ExtLineBuffer[bar+1]);
//           dColor[bar]=0;
     if (ExtLineBuffer[bar] > ExtLineBuffer[bar-1])
         {
         dColor[bar]=0;
        
         }
     else    
         {
         dColor[bar]=1;
           
         }
    }
     ChartRedraw();    
   return(rates_total);
  }
//+------------------------------------------------------------------+
Reason: