Download MetaTrader 5

CisNewBar in Indicator

To add comments, please log in or register
imamushroom
157
imamushroom  

Hi,

I'm using the code from this article https://www.mql5.com/en/articles/159 to calculate when a new bar opens but it's not displaying the historical data for the indicator. I modified the TimeCurrent() to iTime(_Symbol,_Period,shift) to try to handle this but it's not working.

Could you tell me what I'm doing wrong please?

 Thanks. 

#property indicator_separate_window

#property indicator_buffers 1

#property indicator_color1 RoyalBlue



#include <Lib_CisNewBar.mqh>

CisNewBar current_chart;



//---- input parameters

extern int    Length=18;      // Bollinger Bands Period

extern int    Deviation=2;    // Deviation was 2

extern double MoneyRisk=1.00; // Offset Factor

extern int    Signal=1;       // Display signals mode: 1-Signals & Stops; 0-only Stops; 2-only Signals;

extern int    Line=1;         // Display line mode: 0-no,1-yes  

extern int    Nbars=1000;



//---- indicator buffers

double TrendBuffer[];

extern bool SoundON=true;

bool TurnedUp = false;

bool TurnedDown = false;

//+------------------------------------------------------------------+

//| Custom indicator initialization function                         |

//+------------------------------------------------------------------+

  int init()

  {

   string short_name;

//---- indicator line

   

   SetIndexBuffer(0,TrendBuffer);

   SetIndexStyle(0,DRAW_LINE,0,1);

   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));

   short_name="Example ("+Length+","+Deviation+")";

   IndicatorShortName(short_name);

   SetIndexLabel(0,"Trend Value");

//----

   SetIndexDrawBegin(0,Length);

//----

   return(INIT_SUCCEEDED);

  }



void deinit()

{

}



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    shift;

   

   for (shift=Nbars;shift>=0;shift--)

   {

      TrendBuffer[shift]=0;

   }



   for (shift=Nbars-Length-1;shift>=0;shift--)

   {    



      int period_seconds=PeriodSeconds(_Period);                    

      datetime new_time=iTime(_Symbol,_Period,shift)/period_seconds*period_seconds; 



      if(current_chart.isNewBar(new_time)) 

      {

         Print("time[shift] = "+TimeToString(time[shift]));



         if( Close[shift] > Close[shift+1] )

            TrendBuffer[shift]=1;

              else if(Close[shift] < Close[shift+1] ) 

            TrendBuffer[shift]=-1;

         else

                 TrendBuffer[shift]=0;

         }

   }

        return(0);      

} 

The "New Bar" Event Handler
The "New Bar" Event Handler
  • 2010.10.11
  • Konstantin Gruzdev
  • www.mql5.com
MQL5 programming language is capable of solving problems on a brand new level. Even those tasks, that already have such solutions, thanks to object oriented programming can rise to a higher level. In this article we take a specially simple example of checking new bar on a chart, that was transformed into rather powerful and versatile tool. What tool? Find out in this article.
To add comments, please log in or register