BarsSince Reverse Cum(array)

Good Day!

Im looking forward to output a count of bars barssince() when a reverse cumulation operation are reached: barssince( reverse(cum(volume)) >= 1000000 shares but certainly Im doing something wrong:

What Im understanding till now is that a regular cum(array) function do something like this:
From array [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] cum(array) outputs: [ 0, 1, 3, 6, 10, 15, 21, 28, 36, 45]

Then I would like to perform a cum() but in the reverse direction, which shluld look like:
[45, 45, 44, 42, 39, 35, 30, 24, 17, 9]

From what I was reading in forums and other resources, and from my newbie perspective:

A) Do it with Amibroker’s Reverse() function: I was trying with this method but plotting the Reverse(cum(volume)) seems to do the calculations always from left to right (not right to left as I want) and only plot this in reverse order.

B) Take difference from total cum() and intercept value with barssince(): I think this method is the way to go, but I think I must deal with varset functions to make number static and I don’t want to make a mess in performance here. From my understanding, I need to create an array holding the difference of cum(volume) - staticSharesValue in a static way in order to intercept the value with barsinsce() function, but don’t find how to do it :frowning:

C) Any other correct or better way to achieve this and Im still not aware of.

Thanks in advance.

I don’t quite understand what it is that you’re trying to achieve, and judging by the lack of responses to your question I’m guessing that others don’t either. Perhaps you could explain it once more with a concrete example, and no reference to the AmiBroker functions that you’re trying to use. While I applaud your attempt to code this yourself, I think the functions you’re referencing may be confusing the issue rather than clarifying your purpose.

Matt

Thanks a lot @mradtke for your response.

What Im trying to achive is to make a cumulative sumation from right to left of an array in order to find a condition given by an imput parameter.

Example: cumulate volume from the very last bar (from right) to the oldest one (left) until reach some value, then count barssince and return result.

I found something here but for python’s numpy.. Then I try to search for a similar example but in C (As i know AFL is based on) but found some complicated loops procedures.

Found a solution for my own problem with try and error.

I take the "B" solution, but have not used a very useful Amibroker function to make array containing the difference with a static value: LasValue().

My final code was as simple as:

_SECTION_BEGIN("BarsSinceRevCumSUM");

x 	= Cum(Volume);
y 	= 5000000000;
z 	= LastValue(x - y);
out = BarsSince(x <= z);

Plot( x, "Cumulative Volume", colorRed, ParamStyle("Style") );
Plot( out, "BarsSince", colorlime, styleOwnScale );

_SECTION_END();

TEST-ReverseCumAMIBROKER

Thanks for your response and help @mradtke

Cheers!

The thing is that you did not want to calculate cumulative volume in reverse order (right to left), but you just wanted to accumulate normally (left to right) and subtract that value from final accumulated. That is why you don’t need Reverse() but only LastValue().

Reverse(cum(volume)) reverses the result of Cum(), while Cum( Reverse( volume ) ) would do cumulative sum in reverse order. But as I wrote above, neither is what you really wanted.

As @Tomasz said, you may not really need to do a reverse cumulative sum. But if did, you could simply use Reverse() twice, like this:

cumRtoL = Reverse(Cum(Reverse(V)));

Now the last value of cumRtoL will be equal to the last value of the Volume array. The second to last value of cumRtoL will be equal to the sum of the last two values in the Volume array, etc.

Matt

3 Likes

Thanks for the clarification @Tomasz. Im not a programer but like a lot to learn, Im just came with my question from examples I found crawling at the web regarding this issue as a “reverse array operand” like the python example I linked. Will try to make more objective and simplier questions in future as @mradtkesugested! Lot’s of failures but finally, taking deeper reading at Ami’s Reference Function manuals help me found my own way! Thanks for those excelent material @Tomasz

@mradtke Thanks for your additional tip! Always welcome if another challenge surges. Will try it later when Im at my Ami’s terminal to compare results.

Cheers!

Finally could achieve output what mathematically I want to do to a plot, using AMI internals ValueWhen and SelectedValue functions:

Array		= abs(ROC(Close,1));  // This could be anything like ROC or Volatility.
Condition	= 100;				  // Second Input for Condition Algorithm.
Cumulative	= Cum(Array) ;
Cond_algo	= ValueWhen( Cumulative <= SelectedValue(Cumulative-Condition), BarIndex() );
COUNTER_OUT	= SelectedValue(BarIndex()) - Cond_algo;  // Transform this output to array series ?within a loop?

My concern is to make COUNTER_OUT an array output for each bar. From what I was reading, I need to transform SelectedValue instances to a loop from within a function right? function(Array,Condition).

Looping is still a kind of headache for me! Can you give me a little light on doing this guys?

Thanks in Advance!