Indicator Coding Question: Is it possible to get the index for the newest candle when the indicator started?

 

I am trying to write an indicator that calculates some cumulative change over time.

Let's say it starts from the bar with index [50] with an initial value and depending on the bars on the way the value will become different when it reaches the right end of the chart. It will be drawn like a line.

The value depends on the initial conditions at [50] (for example moving average at [50]), but when a new bar forms that same candle is going to be [51], and if the new [50] has a different moving average, the line will be radically different.

So in order to keep it stable, I want the indicator to keep finding the [50]th bar from when I start running the indicator, how can I do this? (Or rather the newest candle when the indicator started)

Thanks in advance.


Edit: 

Solved it by just calculating from [50] at (prev_calculated = 0) or something like that, and just update the new bars. Thanks for the comments!
Moving Average - Trend Indicators - MetaTrader 5 Help
Moving Average - Trend Indicators - MetaTrader 5 Help
  • www.metatrader5.com
The Moving Average Technical Indicator shows the mean instrument price value for a certain period of time. When one calculates the moving average...
 

I have decided to use datetime variable to detect new bar, and set a counter for number of new bars since indicator start and just use [50 + counter].

If there is a better solution, welcome to post here.

 
Unnecessary. Just write the indicator like any other.
          How to do your lookbacks correctly #9#14 & #19
 
Kelvin Nelson #:

I have decided to use datetime variable to detect new bar, and set a counter for number of new bars since indicator start and just use [50 + counter].

If there is a better solution, welcome to post here.

Kelvin Nelson:

I am trying to write an indicator that calculates some cumulative change over time.

Let's say it starts from the bar with index [50] with an initial value and depending on the bars on the way the value will become different when it reaches the right end of the chart. It will be drawn like a line.

The value depends on the initial conditions at [50] (for example moving average at [50]), but when a new bar forms that same candle is going to be [51], and if the new [50] has a different moving average, the line will be radically different.

So in order to keep it stable, I want the indicator to keep finding the [50]th bar from when I start running the indicator, how can I do this? (Or rather the newest candle when the indicator started)

Thanks in advance.

many indicators working as your description.

see the codebase