What's the best way to set an indicator refresh loop?

 

One issue I'm having modifying pre-existing indicators is the indicator plot on the graph will stop updating and return to zero, until I recompile or change timeframes and back again. I've been trying everything.

What's the best code for an indicator loop?

Here's my code, trying to combine Bears and Bulls, but for some reason it stops working. Does it have to do with how I coded the Signal line(MA on Bears+Bulls)?

#property description "Bears Power + Bulls Power = Chariot Power"
//--- indicator settings
#property indicator_separate_window
#property indicator_buffers 3
#property indicator_plots   2
#property indicator_label1 "Chariot"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrSilver
#property indicator_width1  1
#property indicator_label2 "Signal"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrRed
#property indicator_width2  1



#include <MovingAverages.mqh>
#define ON_TIMER_PERIOD 60 // Timer event period (in seconds)
//--- input parameters
input int ChariotPeriod=13; // Chariot Period
input int ChariotSignalPeriod = 3; // Signal Period
input ENUM_MA_METHOD ChariotMethod = MODE_LWMA; // Chariot MA Method
input ENUM_APPLIED_PRICE ChariotPrice = PRICE_CLOSE; // Applied Price
//--- indicator buffers
double    ChariotBuffer[];
double    ExtTempBuffer[];
double    SignalBuffer[];
//--- handle of EMA
int       ExtEmaHandle;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0, ChariotBuffer,INDICATOR_DATA);
   SetIndexBuffer(1, SignalBuffer, INDICATOR_DATA);
   SetIndexBuffer(2, ExtTempBuffer,INDICATOR_CALCULATIONS);
//---
   IndicatorSetInteger(INDICATOR_DIGITS,_Digits+1);
//--- sets first bar from what index will be drawn
   // PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,ChariotPeriod);
   PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,ChariotSignalPeriod-1);
//--- name for DataWindow and indicator subwindow label
   string short_name=StringFormat("Chariot Power (%d) Signal (%d)",ChariotPeriod,ChariotSignalPeriod);
   IndicatorSetString(INDICATOR_SHORTNAME,short_name);
//--- get MA handle
   ExtEmaHandle=iMA(Symbol(),PERIOD_CURRENT,ChariotPeriod,0,ChariotMethod,ChariotPrice);
  
  return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Average True Range                                               |
//+------------------------------------------------------------------+
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[])
  {
   if(rates_total<ChariotPeriod)
      return(0);
//--- not all data may be calculated
   int calculated=BarsCalculated(ExtEmaHandle);
   if(calculated<rates_total)
     {
      Print("Not all data of ExtEmaHandle is calculated (",calculated," bars). Error ",GetLastError());
      return(0);
     }
//--- we can copy not all data
   int to_copy;
   if(prev_calculated>rates_total || prev_calculated<0)
      to_copy=rates_total;
   else
     {
      to_copy=rates_total-prev_calculated;
      if(prev_calculated>0)
         to_copy++;
     }
//--- get ma buffers
   if(IsStopped()) // checking for stop flag
      return(0);
   if(CopyBuffer(ExtEmaHandle,0,0,to_copy,ExtTempBuffer)<=0)
     {
      Print("getting ExtEmaHandle is failed! Error ",GetLastError());
      return(0);
     }
//--- first calculation or number of bars was changed
   int start;
   if(prev_calculated<ChariotPeriod)
      start=ChariotPeriod;
   else
      start=prev_calculated-1;
//--- the main loop of calculations
   for(int i=start; i<rates_total-prev_calculated; i++)
      ChariotBuffer[i]=(tick_volume[i]*(low[i]-ExtTempBuffer[i]))+(tick_volume[i]*(high[i]-ExtTempBuffer[i]));
      
      ExponentialMAOnBuffer(rates_total,prev_calculated,0,ChariotSignalPeriod,ChariotBuffer,SignalBuffer);
 
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

I've tried 2 days to correct whatever issues there are, copy and pasting code from working indicators and modifying, etc, reverting back and trying again. A bit frustrating. Compile and put on a chart and watch it return to Zero line when a new bar forms.. lol! pain.exe

 
for(int i=start; i<rates_total-prev_calculated; i++)
      ChariotBuffer[i]=(tick_volume[i]*(low[i]-ExtTempBuffer[i]))+(tick_volume[i]*(high[i]-ExtTempBuffer[i]));

Why is this "rates_total-prev_calculated", shouldn't it be "rates_total - 1"?

 
Nagisa Unada #:

Why is this "rates_total-prev_calculated", shouldn't it be "rates_total - 1"?

I've deleted the "-1" and "-prev_calculated" and it cleared the issue right up. It updates in real time on the current bar, the way I wanted.

Reason: