Multi-timeframe indicator without repainting: how do you read the higher-TF value safely?

Hello everyone,

I am working on a small multi-timeframe trend panel (M15 chart showing H1 and H4 values) and I keep running into the same three issues that I see in many MTF indicators shared online:

  1. Repainting: reading the current, still-open higher-TF bar (shift 0) means the value on old M15 bars changes until the H1/H4 bar closes. On history it looks perfect, live it behaves differently.
  2. History not ready: right after the terminal starts, or when the higher TF is not loaded yet, iBarShift() returns -1 or CopyBuffer() copies nothing, and the indicator draws zeros or garbage.
  3. Speed: calling CopyBuffer() once per chart bar on full history is very slow on a fresh chart.

For point 1 and 2 this is the helper I use at the moment. It always takes the last closed higher-TF bar for a given chart bar, and returns false if the data is not ready, so the caller can return 0 from OnCalculate() and try again on the next tick:

bool GetClosedHTFValue(const int handle, const ENUM_TIMEFRAMES htf,
                       const datetime barTime, double &value)
  {
   if(BarsCalculated(handle) <= 0)
      return false;                    // higher-TF indicator not calculated yet

   int shift = iBarShift(_Symbol, htf, barTime, false);
   if(shift < 0)
      return false;                    // higher-TF history not loaded yet

   double buf[1];
   if(CopyBuffer(handle, 0, shift + 1, 1, buf) != 1)   // +1 = last CLOSED HTF bar
      return false;

   value = buf[0];
   return true;
  }

The trade-off is clear: no repainting, but the signal shows up one higher-TF bar later (up to 1 hour late on H1). For a trend filter I am fine with that, for entries maybe not.

For point 3 I am thinking about copying the whole higher-TF buffer once per new higher-TF bar into an array, and then only mapping chart bars to it with iBarShift(), instead of calling CopyBuffer() for every bar.

Questions for other developers:

  • Do you use shift + 1 (closed bar only), or do you show the live higher-TF value and accept the repaint on the last few bars?
  • What is your preferred way to wait for higher-TF history: return 0 from OnCalculate(), a timer, or something else?
  • Is copying the full higher-TF buffer once and mapping it really faster in your tests, or is there a better pattern?

Thanks, I will share the final version once it is tested on gold and a few forex pairs.

Such things have been discussed a lot of times on this forum, with a lot of inputs and ideas. Did you make a search before posting ?