Recovering historical values of an array.
Compared to normal variables, arrays don’t create their historical values at the end of bar but retain their current value.
Using simple maths:
A + B = C , which can be transposed to , C – A = B or C – B = A
Also, A – B = D which is the difference between A and B.
Looking at the sequence of events, the first formula, takes value A then adds value B, which results in C.
The transposed formula, take the result , C and subtracts either A or B which gets the missing value.
Looking closer at this sequence, you could say the later value C, requires an earlier value, A or B, to find the missing value.
So applying that to arrays, arr[0] =10, needs the value prior to it which could be store in arr-1[0] = 6.
Therefore, arr[0] – arr-1[0] = 10 – 6 = 4 the difference is the missing value, A – B = D.
Looking at the history values of 1 element, with original values in arr.
See table 1.
res = arr-arr-1 gives the difference between the current and prior value, A – B = D.
Instead of storing the original values, we store the sum of the values. Which is like adding the difference between new and current value. Therefore D – B = A or C – B = A if your following.
Here, D is the accumulated value and B is the prior accumulated value.
Doing this, encodes the original data values or information. In normal array operation, the current value, is lost when the original element values get overwritten. Or to put it another way, originally we have A = C where A and C don’t retain any information from the earlier values as they go forward and just represents the new value.
Looking at the history values of 1 element, with an accumulated value in arr.
See table 2.
Note: the difference value of Res=arr-arr-1 is now the same as the original arr values.
Magic…
Using the two arrays, along with the accumulative values, allows, not only to find a specific prior value of an array, but depending on the range of element index for both arrays chosen, you can get a summed value over a range of array elements.
A few things to point out,
Using the barindex value for the array index, aligns the arrays with respect to the chart bars, which is helpful when trying to look-back to recover values. This also means that the arrays need to be updated each bar to keep the alignment.
Now the above is the Rule and I’m sure there could be exception to it, depending on application, type of data, and context.
In simple example, the look-back value is retrieved from both CLOSE[LB] and from the arrays.
// avoid changing text value
defparam drawonlastbaronly = true
// avoid look-back beyond bar 0
if barindex > 0 then
// copy last bars arr value to arrM1
$arrM1[barindex] = $arr[barindex-1]
else
$arrM1[0]=-1 // define array by assigning a default value
endif
// new arr value accumulated
$arr[barindex] = $arrM1[barindex] + close
// on current bar, look-back LB bars and recover the close value
if islastbarupdate then
LB = 5
barMx = $arr[barindex-LB]-$arrM1[barindex-LB]
closeLB = close[5]
// display the look-back close value
drawtext("arr { #LB#} = #barMx#",0,0)anchor(middle,xshift,yshift)
drawtext("close [ #LB#] = #closeLB#",0,-40)anchor(middle,xshift,yshift)
// identify the look-back bar
drawVline(barindex-LB)
endif
return barindex as"barindex", close as"close", $arr[barindex] as "acc total"