My Heiken Ashi code

 

Unfortunately, it is not working as expectedly ... I could not find where to fix :(

I would be very grateful for any help.


//+------------------------------------------------------------------+
//|                                                   HeikenAshi.mq5 |
//|                        Copyright 2014, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window
#property indicator_buffers 5
#property indicator_plots   1
//--- plot Candle
#property indicator_label1  "Candle"
#property indicator_type1   DRAW_COLOR_CANDLES
#property indicator_color1  clrFireBrick,clrSteelBlue
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- indicator buffers
double         CandleOpen[];
double         CandleHigh[];
double         CandleLow[];
double         CandleClose[];
double         CandleColors[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,CandleOpen,INDICATOR_DATA);
   SetIndexBuffer(1,CandleHigh,INDICATOR_DATA);
   SetIndexBuffer(2,CandleLow,INDICATOR_DATA);
   SetIndexBuffer(3,CandleClose,INDICATOR_DATA);
   SetIndexBuffer(4,CandleColors,INDICATOR_COLOR_INDEX);
//---
   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[])
  {
//---
   ArraySetAsSeries(CandleOpen,true);
   ArraySetAsSeries(CandleHigh,true);
   ArraySetAsSeries(CandleLow,true);
   ArraySetAsSeries(CandleClose,true);
   ArraySetAsSeries(open,true);
   ArraySetAsSeries(high,true);
   ArraySetAsSeries(low,true);
   ArraySetAsSeries(close,true);
   
   int bars = rates_total - 2;
   if(prev_calculated > 0) bars = rates_total - prev_calculated;
   
   for(int i = bars; i >= 0; i--)
     {
      CandleOpen[i] = (open[i+1] + close[i+1])/2;
      CandleClose[i] = (open[i]+high[i]+low[i]+close[i])/4;
      CandleHigh[i] = MathMax(high[i],MathMax(open[i],close[i]));
      CandleLow[i] = MathMin(low[i],MathMin(open[i],close[i]));
      
      if(CandleOpen[i]<CandleClose[i]) CandleColors[i]=1;
      if(CandleOpen[i]>CandleClose[i]) CandleColors[i]=0;
     }

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

Unfortunately, it is not working as expectedly ... I could not find where to fix :(

I would be very grateful for any help.


What is "not working as expected" ?
Reason: