Duplicate: Reduction of 3 elements to 2

I have a little problem which I failed to resolve with arrays or boolean values.
I only want to reduce the following formula

value1 = IIf( L > X, 1, IIf( H < X, -1, 0));

(where X is a calculated value for a line for exp.) to only the last 1 or -1 results
in case the result is 0, but as several 0 can follow each other I can't juste
refer to the previous element. So how to proceed in this case ?
Thank you for answer(s).

So the most recent non-zero result if "current" or last bar returns zero?

X = Ref(HHV(C, 20), -1);
value1 = IIf( L > X, 1, IIf( H < X, -1, 0));
recent_nonzero = IIf(value1 == 0, ValueWhen(value1 != 0, value1), 0);
last_recent = LastValue(recent_nonzero);

13

Or just

X = Ref(HHV(C, 20), -1);
value1 = IIf( L > X, 1, IIf( H < X, -1, 0));
recent_nonzero = ValueWhen(value1 != 0, value1);
last_recent = LastValue(recent_nonzero);

13

1 Like

Thank you very much for your answer.
I see well that this is prefectly logical and working well
for your exemple. But for some reason it juste doesn't want work
in my exemple which is the Kijunsen:

_SECTION_BEGIN("Test1");
P = Param("Base Period", 10, 2, 200, 1, 0);
X = (HHV(H,P)+LLV(L,P))/2;
value1 = IIf(L>X,1,IIf(H<X-1,0));
recent_nonzero = IIf(value1 == 0, ValueWhen(value1 != 0, value1), 0);
last_recent = LastValue(recent_nonzero);
for( i = 0; i < 100; i++ )
{
printf( NumToStr(last_recent));
}
_SECTION_END();

it only prints 0 for all 100 values, same if I formulate the whole as array
by placing a [i] behind all the concerned items and the

for( i = 0; i < 100; i++ )
{

on second line (after P).
What am I doing wrong ?

Incorrect.
It works for all cases.

YOUR addition just makes zero sense.

Everything of that.

You do not need loop for output. Also your loop is wrong.


To get output of previous 100 bars you just need explorer

Filter = 1;
AddColumn(recent_nonzero, "recent_nonzero", 1);

13

1 Like

This is a duplicate of existing threads, such as:

and this:

https://forum.amibroker.com/t/duplicate-keep-previous-value-of-array-was-war-with-the-third/19309/4

DO NOT CREATE duplicate threads.

Thank you again, so far I understood the proceedings and it works well.
But now when I only want to duplicate this same procedure with
another indicator and then try to get the simple addition of both results
I fail again and get 2 empty columns, only the initial column works.
That's quite nerf racking, I wonder what I missed this time .

_SECTION_BEGIN("Test2");
P = Param("Base Period", 10, 2, 200, 1, 0);
BL = (HHV(H,P)+LLV(L,P))/2;
value1 = IIf(L>BL,1,IIf(H<BL,-1,0));
recent_nonzero = ValueWhen(value1 != 0, value1);
last_recent = LastValue(recent_nonzero);
Filter = 1;
AddColumn(recent_nonzero, "recent_nonzero", 1);
////
value2 = EMA(C,20);
value3 = ref(Value1,-1);
slp = value2 - value3;
emaslopeF = EMA(slp,13);
emaslopeS = EMA(slp,20);
value4 = IIf(emaslopeF>emaslopeS,1,IIf(emaslopeF<emaslopeS,-1,0));
recent_nonzero_ES = IIf(value4 == 0, ValueWhen(value4 != 0, value4), 0);
last_recent_ES = LastValue(recent_nonzero_ES);
Filter = 1;
AddColumn(recent_nonzero_ES, "recent_nonzero_ES", 1);
////
value5 = recent_nonzero + recent_nonzero_ES;
recent_nonzero_5 = IIf(value5 == 0, ValueWhen(value5 != 0, value5), 0);
last_recent_5 = LastValue(recent_nonzero_5);
AddColumn(recent_nonzero_5, "recent_nonzero_5", 1);
_SECTION_END();

First of all recent_nonzero_ES and recent_nonzero_5 are not empty but do return zero.

Secondly recent_nonzero never becomes zero and value4 almost never becomes zero and value5 never becomes zero because of recent_nonzero never returning zero.

So results of

recent_nonzero_ES = IIf(value4 == 0, ValueWhen(value4 != 0, value4), 0);

and

recent_nonzero_5 = IIf(value5 == 0, ValueWhen(value5 != 0, value5), 0);

become always zero.

It makes no sense to write z = IIf(x == 0, y, 0) if x never becomes zero and as such z becomes zero all the time.

To get non zero results for second and third column remove IIf() (at least for recent_nonzero_ES).

_SECTION_BEGIN("Test2");
P = Param("Base Period", 10, 2, 200, 1, 0);
BL = (HHV(H,P)+LLV(L,P))/2;
value1 = IIf(L>BL,1,IIf(H<BL,-1,0));
recent_nonzero = ValueWhen(value1 != 0, value1);
////
value2 = EMA(C,20);
value3 = ref(Value1,-1);
slp = value2 - value3;
emaslopeF = EMA(slp,13);
emaslopeS = EMA(slp,20);
value4 = IIf(emaslopeF>emaslopeS,1,IIf(emaslopeF<emaslopeS,-1,0));
recent_nonzero_ES = ValueWhen(value4 != 0, value4);
////
value5 = recent_nonzero + recent_nonzero_ES;
recent_nonzero_5 = ValueWhen(value5 != 0, value5);
///
Filter = 1;
AddColumn(recent_nonzero, "recent_nonzero", 1);
AddColumn(recent_nonzero_ES, "recent_nonzero_ES", 1);
AddColumn(recent_nonzero_5, "recent_nonzero_5", 1);
_SECTION_END();

BTW, please read this post here

You can come to conclusions yourself easily.
You just try to go easy route because of being impatient (been proven by e.g. creating duplicate threads).

Thank you again for your answer, unfortunately the 2. and 3. columns
remain empty even if replace the recent_nonzero_ES simply with value4.

Btw. I don't think I would have these problems on filling an Excel spreadsheet
with those columns to get these 1, -1 and 0 for the 2 indicators and add them,
I think I could do this easily, but with afl it's not so obvious apparently...

I am not getting empty results for entire column at all. You would only get empty cells at start because of EMA periods. So scroll or load more data.

6


Check whether you get no line output if you plot those values on chart.
See, it doesn't look like empty to me, does it?
13

Once again please use debug techniques.

1 Like

I see, may be it's because I apply actually this code on a 5 min chart
which only has around 20 days daily history. This was in fact also
my hopefully last question in this contexte , how to change the timeframe
of the exploration.
May be also I make an error by applying the application, as I apply it
always in the AFL Formula Editor. I never used exploration before
and I didin't find the way to open it from the chart.

OK, I found the solution changing my settings for Analysis and now
the results indeed correspond to my chart in what concerns the timeframe.
Nevertheless it still doesn't work the way I would like to see it, as
the columns 2 and 3 are just filled with 0, what should not happen.
Here my code

_SECTION_BEGIN("Test1");
P = Param("Base Period", 10, 2, 200, 1, 0);
BL = (HHV(H,P)+LLV(L,P))/2;
value1 = IIf(L>BL,1,IIf(H<BL,-1,0));
recent_nonzero = ValueWhen(value1 != 0, value1);
last_recent = LastValue(recent_nonzero);
Filter = 1;
AddColumn(recent_nonzero, "recent_nonzero", 1);
////
value2 = EMA(C,20);
slp = value2 - ref(Value2,-1);
emaslopeF = EMA(slp,13);
emaslopeS = EMA(slp,20);
value4 = IIf(emaslopeF>emaslopeS,1,IIf(emaslopeF<emaslopeS,-1,0));
recent_nonzero_ES = IIf(value4 == 0, ValueWhen(value4 != 0, value4), 0);
last_recent_ES = LastValue(recent_nonzero_ES);
Filter = 1;
AddColumn(recent_nonzero_ES, "recent_nonzero_ES", 1);
////
value5 = recent_nonzero + recent_nonzero_ES;
recent_nonzero_5 = IIf(value5 == 0, ValueWhen(value5 != 0, value5), 0);
last_recent_5 = LastValue(recent_nonzero_5);
AddColumn(recent_nonzero_5, "recent_nonzero_5", 1);
_SECTION_END();

and so looks my exploration
image
only 0 in column 2 and 3 until the end...

You once again do same mistake and post (almost) same code as before in post 5. :roll_eyes:
Why??

Why using iif(value4 == 0,..) if value4 never becomes nonzero?
Why using iif(value5 == 0,...) if value5 never becomes nonzero?
Why using multiple times Filter=1;?
Why not putting a bit better structure into code... IPO (input-processing-output)?

Please re-read my post!


Also it looks as if you still have not read here

You can come to conclusions yourself easily.
Have you checked value4 and value5?
I guess answer is "No".

1 Like

OK, thank you very much for your patience !
Finally I got it right now, I thought I needed repeat the same
like for the 1. item, that's why I stuck there.
So this code works correctly now

_SECTION_BEGIN("Test1");
P = Param("Base Period", 10, 2, 200, 1, 0);
BL = (HHV(H,P)+LLV(L,P))/2;
value1 = IIf(L>BL,1,IIf(H<BL,-1,0));
recent_nonzero = ValueWhen(value1 != 0, value1);
last_recent = LastValue(recent_nonzero);
Filter = 1;
AddColumn(recent_nonzero, "recent_nonzero", 1);
////
value2 = EMA(C,20);
slp = value2 - ref(Value2,-1);
emaslopeF = EMA(slp,13);
emaslopeS = EMA(slp,20);
value4 = IIf(emaslopeF>emaslopeS,1,IIf(emaslopeF<emaslopeS,-1,0));
AddColumn(value4, "value4", 1);
////
value5 = recent_nonzero + value4; //recent_nonzero_ES;
AddColumn(value5, "value5", 1);
_SECTION_END();

giving the correct results in exploration
image
Thanks a lot and have a nice week !