Download MetaTrader 5

Range not behaving as expected - page 2

To add comments, please log in or register
whroeder1
14765
whroeder1 2016.09.23 12:40  
When drawing pivots, there is no need to redraw bar zero as the value doesn't change. Unlike a moving average.
        #define REDRAW_BAR_ZERO false           // Don't redraw Bar zero.
        return rates_total - MathMax(REDRAW_BAR_ZERO, iLast);
whroeder1
14765
whroeder1 2016.12.04 14:35  
Here's how to do the drawing in groups (when the terminal becomes unresponsive on startup.)
   #define LAST 0
   const int   LB_MA    = LengthMMA-1;
   int      iMA     = rates_total - 1 - MathMax(LB_MA, prev_calculated);
   int      iLast   = MathMax(LAST, iMA - 2000);        // Do in groups
   for(; iMA >= iLast; --iMA){
      :
   }  // iMA
//----
   #define REDRAW_BAR_ZERO true     // Redraw Bar zero.
   return rates_total - MathMax(REDRAW_BAR_ZERO, iLast);
whroeder1
14765
whroeder1 2016.12.11 15:43  
The last line above can be optimized (and must be for MTF version.)
   return   rates_total - REDRAW_BAR_ZERO - iLast;
For multi-timeframe indicators, there are changes.
// Input(s)
extern ENUM_TIMEFRAMES TimeFrame    = PERIOD_CURRENT;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit(){
   if(TimeFrame == PERIOD_CURRENT)  TimeFrame   = (ENUM_TIMEFRAMES) _Period;
   if(TimeFrame < _Period) return INIT_PARAMETERS_INCORRECT;
  1. The look back must adjusted when the look back is from a higher TF.
  2. Don't mix apples and oranges
   const int   lookback    = TimeFrame/_Period; // * 1
   int      iBar  =  Rates_total - 1 - MathMax(lookback, prev_calculated);
   #define LAST 0
   int      iLast = MathMax(LAST, iBar - 2000);   // Do in groups
   for(; iBar >= iLast; --iBar){
      int       iTF = iBarShift(NULL, TimeFrame, Time[iBar]);
      double   hiCur    = iHigh(NULL, TimeFrame, iTF)  //High[iBar],  
               hiPre    = iHigh(NULL, TimeFrame, iTF+1); //High[iBar+1];
      :
Redraw bar zero of the higher TF to get the stair step not just the chart's bar zero.
(See images Multi TimeFrame Moving Average or Moving Average Ex from the codebase)
   #define  REDRAW_BAR_ZERO true       // Redraw Bar zero.
   iLast = iBarShift(NULL, 0, Time[iLast] - Time[iLast] % (TimeFrame * 60));
   return   rates_total - REDRAW_BAR_ZERO - iLast;
12
To add comments, please log in or register