Easy question on using Sumsince to count something within a specific range of bars.

Hi All,

Let say....
I want to count "Cross(C, MA(C,20))" within only the range starting from the 200th previous bar till the recent bar.
(Then probable entry buy at the second or the third one)

I would use SumSince( condition, array ) function to count.
The array part......I will code SumSince( condition, Cross(C, MA(C,20)) ).
I don't know how to code the "condition" part.

This must be very easy for many of you here. Please help.

Thank you,
Patchawat K.

1 Like

If I understand your post correctly then use Sum function instead.

num = 3; // minimum number of cross occurrences
prevbars = 200; // bars range to sum up
condition = Cross(C, MA(C,20));
Buy = Sum( condition, prevbars ) >= num;
5 Likes

Here is alternative code in case previous one is not what you were looking for.

num = 3; // minimum number of cross occurrences
prevbars = 200; // bars range to sum up
condition = Cross(C, MA(C,20));
cs = SumSince(Ref(condition, -prevbars), condition);
Buy = cs == num;

Filter = 1;
AddMultiTextColumn(condition, "\nTrue", "Cross", 1);
AddColumn(cs, "Sum", 1);
AddMultiTextColumn(Buy, "\nTrue", "Buy", 1);

2018-06-08_225536

4 Likes

Here is another (different) version (as upper ones have been "at-midnight" ideas).

About below one:
Within every n-bar range it sums up cross occurrences and "buys" if 3 occurrences have been reached. (In the example I have set to 20 bars range because 200 bars are too long for picture view):

/// @link https://forum.amibroker.com/t/easy-question-on-using-sumsince-to-count-something-within-a-specific-range-of-bars/6335/4
num = 3; // minimum number of cross occurrences
bars = 20; // bars range to sum up
maperiod = 20;
arr = Cross(C, MA(C, maperiod));
condition = Cum(1) % bars == 0 OR Status( "firstbarinrange" ); 
cs = SumSince(condition, Nz(arr)) + ValueWhen(condition, arr);
Buy = cs == num;

Filter = 1;
AddRankColumn();
AddmultiTextColumn(condition, StrFormat("\n--- Start of new %g-bars range ---", bars), "CumBars", 1, -1, -1, 200);
AddmultiTextColumn(arr, "\nTrue", "Cross", 1);
AddColumn(cs, "Sum", 1);
AddmultiTextColumn(Buy, "\nTrue", "Buy", 1);

113340

6 Likes

And last version (BTW, where is @golfpatchawat all of the sudden?).

It is update to previous one and adds code to either set backward or forward count of bars (previous one only did forward bars count)

/// @link https://forum.amibroker.com/t/easy-question-on-using-sumsince-to-count-something-within-a-specific-range-of-bars/6335/5

Version( 6.20 ); // minimum required version to run this code

num = 3; // minimum number of cross occurrences
bars = 20; // bars range to sum up
forward_count = true;// set bars counter: counting fowards (true) or backwards (false)

arr = Cross(C, MA(C, 20));

bir = Status( "barinrange" );// to flag set analysis date range
if ( forward_count )	cumbars = Cum(bir); // count bars forwards within analysis date range
else					cumbars = Reverse(Cum(Reverse(bir))); // count bars backwards within analysis date range

condition = cumbars % bars == 0;
cs = SumSince(condition, Nz(arr)) + ValueWhen(condition, arr);

Buy = cs == num;

Filter = 1;
AddColumn(cumbars, "#bars", 1);
AddmultiTextColumn(condition OR IIf(forward_count, Status( "firstbarinrange" ), 0), StrFormat("\n--- Start of new %g-bars range ---", bars), "Bars range status", 1, -1, -1, 200);
AddmultiTextColumn(arr, "\nTrue", "Cross", 1);
AddColumn(cs, "Sum", 1);
AddmultiTextColumn(Buy, "\nTrue", "Buy", 1);

So if you set forward_count = true; then it behaves as previous one
2018-06-09_140748


On the other hand if you set forward_count = false; then bars counter (for setting start of bars range) starts to count from last bar of range (Note: crosses are still getting forward counted! See arrows in picture below).

2018-06-09_140646

12 Likes

Hi Fxshrat, Thank you very much for your help. I just see your post as I have just come back from an all busy day of Seminar. I will read through all your reply here but I think only the first one of yours will do help me enough.

Appriciated :smile:

1 Like