First, Second and Third Bar of Today's Chart

Hi Experts,

I have written an AFL code which initializes the OHLC values of first 3 candles created. But when I compare those values in 'Exploration' window, the results are erroneous.

Can anyone look into the below code and verify if I am missing something!

// Today first 3 bars
dn = Day();
bi = BarIndex();
firstB = dn != Ref( dn, -1 );
secondB = BarsSince(firstB)==1;
thirdB = BarsSince(firstB)==2;

// First Candle OHLC
firstB_Op = ValueWhen(firstB, O);
firstB_Hi = ValueWhen(firstB, H);
firstB_Lo = ValueWhen(firstB, L);
firstB_Cl = ValueWhen(firstB, C);

// Second Candle OHLC
secondB_Op = ValueWhen(secondB, O);
secondB_Hi = ValueWhen(secondB, H);
secondB_Lo = ValueWhen(secondB, L);
secondB_Cl = ValueWhen(secondB, C);

// Third Candle OHLC
thirdB_Op = ValueWhen(thirdB, O);
thirdB_Hi = ValueWhen(thirdB, H);
thirdB_Lo = ValueWhen(thirdB, L);
thirdB_Cl = ValueWhen(thirdB, C);

Any comments will be very helpful.

Sandeep

Hi Sandeep,

Instead of just posting your AFL, it would be helpful to post your chart and exploration output marked with what differences you see.
Also the settings used should be the same, even if the timeframes or the from-to range etc are different you will get different results.

For exploration output, you should set Filter special variable to something.

ValueWhen() use does not seem correct if you are referencing Bars since day open.
If you got BarIndex() at day open with firstB, then you can add 1 and 2 to that and access those bars.

1 Like

I did it for the C prices, you can add O H L in the same way.

bi = BarIndex();
nd = Day() != Ref(Day(), -1);
firstb = ValueWhen( nd, bi); 
secb = firstb + 1;
thirdb = firstb + 2;

firstB_Op  = ValueWhen(bi == firstb, C);
secondB_Op = ValueWhen(bi == secb, C);
thirdB_Op  = ValueWhen(bi == thirdb, C);

printf("\n %g %g %g \n", firstB_Op, secondB_Op, thirdB_Op);

Thanks a lot for the suggestions. It helped me a lot in understanding the bar references.

1 Like

What does erroneous actually mean?
Have you read here? Please do so if not having done yet.

If you mean difference between chart and analysis then most common mistake users make is not setting desired interval at Analysis settings - General - Periodicity. So make sure periodicity is the one you want to get results for.

13

Also you can get info about applied interval via

AddTextColumn(Interval(2), "Interval", 1);

13

Last but not least your posted code is incomplete exploration code. If you have made exploration code than you should post it too.


Other than that... Your code is OK!
The thing I would add is storing BarsSince(firstB) to a separate variable.

// Today first 3 bars
dn = Day();
firstB = dn != Ref( dn, -1 );// first flag
bars = BarsSince(firstB);
secondB = bars == 1;// 2nd flag
thirdB = bars == 2;// 3rd flag

// rest of code below...

Why making it more complicated than what is in first post?
There is no benefit.

So your statement is incorrect. His code is correct.

3 Likes

Thanks fxshrat for giving another point of view. I am looking into both of the code thoroughly to clear my understanding.

I executed your code in Exploration but surprisingly it didn't provide me any results!

// First 3 bars
bi = BarIndex();
newDay = Day() != Ref(Day(), -1);
firstB = ValueWhen( newday, bi);

// First Bar OHLC
firstB_Op = ValueWhen(bi == firstB, O); //first bar open
firstB_Hi = ValueWhen(bi == firstB, H); //first bar high
firstB_Lo = ValueWhen(bi == firstB, L); //first bar low
firstB_Cl = ValueWhen(bi == firstB, C); //first bar close

Cond1 = (firstB_Op > firstB_Cl); //explore all scripts whose first bar have open > close
Parameters = Cond1;
Filter = Parameters;

Yesterday I changed my code and applied the logic you provided. Here is my code:

// Today first 2 bars
bi = BarIndex();
nd = Day() != Ref(Day(), -1);
firstB = ValueWhen( nd, bi); 
secondB = firstb + 1;
thirdB = firstb + 2;

// First Bar OHLC
firstB_Op = ValueWhen(bi == firstB, O); //first bar open
firstB_Hi = ValueWhen(bi == firstB, H); //first bar high
firstB_Lo = ValueWhen(bi == firstB, L); //first bar low
firstB_Cl = ValueWhen(bi == firstB, C); //first bar close

// Second Bar OHLC
secondB_Op = ValueWhen(bi == secondB, O); //second bar open
secondB_Hi = ValueWhen(bi == secondB, H); //second bar high
secondB_Lo = ValueWhen(bi == secondB, L); //second bar low
secondB_Cl = ValueWhen(bi == secondB, C); //second bar close

Cond1 = (firstB_Op > firstB_Cl) AND (secondB_Cl > secondB_Op); //explore all scripts whose first bar is red and second bar is green

Parameters = Cond1;
Filter = Parameters;

Surprisingly, this logic also includes scripts whose second bar is red. I am at a loss what the mistake is!

My first post suspected some issue with different settings in Analysis.

Mr Fxshrat was right and in my next post i used a different logic but that is not the problem so i got side tracked from the issue.
Specify your complete analysis settings.

Really,

  1. It is not my code!

  2. What about reading carefully?

    Do you see anything being equal to the quoted code of yours?

  3. The other part of my previous post was addressed to @nsm51.


Next....

It does but just Ticker and DateTime!

14

If you want to output further columns like Price etc then you need to add further AddColumn lines!
http://www.amibroker.com/guide/h_exploration.html

BTW, ValueWhen keeps TRUE till next occurrence.


Upload analysis project file... To save project file go to File - Save... if having analysis opened.

Here is the setting that I am using (mostly default values of Amibroker).Capture

please take some time and don't hurry. We cannot see your screen so you can post all the output and show where there is a mismatch.
The code is working fine as pointed out also by Mr Fxshrat.
see his post where he added some change to BarsSince() and use that.

Paste the whole formula in one go.

Really sorry. I didn't provided the entire code of the analysis. The rest of the code goes like this:

STS = WriteIf( Parameters, "demo", "demo" );
Col = IIf( Parameters, colorGreen, 0 );

AddTextColumn( sts, "Action", 1.0, Col, colorWhite, 50 );
AddColumn( C, "Trigger", 1.2, colorBlack, 7 );
AddSummaryRows( 16, 1.2 );
SetSortColumns( 2 );

First of all I read your code carefully. Let me introduce myself to you. I am doing programming (SAP ABAP) since 15 years now, so I know the modalities of programming very well.

As you have pointed out I didn't provided the entire code, so I am submitting it in entirety.

#pragma nocache

// Today first 3 bars
bi = BarIndex();
dn = Day();
firstB = dn != Ref( dn, -1 ); // 1st bar of the day
bars = BarsSince(firstB);
secondB = bars == 1; // 2nd bar of the day
thirdB = bars == 2; // 3rd bar of the day

// First Bar OHLC
firstB_Op = ValueWhen(bi == firstB, O); // 1st bar open
firstB_Hi = ValueWhen(bi == firstB, H); // 1st bar high
firstB_Lo = ValueWhen(bi == firstB, L); // 1st bar low
firstB_Cl = ValueWhen(bi == firstB, C); // 1st bar close

// Second Bar OHLC
secondB_Op = ValueWhen(bi == secondB, O); // 2nd bar open
secondB_Hi = ValueWhen(bi == secondB, H); // 2nd bar high
secondB_Lo = ValueWhen(bi == secondB, L); //3rd bar low
secondB_Cl = ValueWhen(bi == secondB, C); //2nd bar close

Cond1 = (firstB_Op > firstB_Cl) AND (secondB_Cl > secondB_Op); // all scripts whose 1st bar is red and 2nd bar is green

Parameters = Cond1;

Filter = Parameters;

STS = WriteIf( Parameters, "list", "list" );
Col = IIf( Parameters, colorGreen, 0 );

AddTextColumn( sts, "Action", 1.0, Col, colorWhite, 50 );
AddColumn( C, "Trigger", 1.2, colorBlack, 7 );
AddSummaryRows( 16, 1.2 );
SetSortColumns( 2 );

As you can see I have inducted all of your code. When I run the above exploration with 5 minute periodicity, it does not provide me any results. Here is the screenshot.

analysis

You have mixed two codes and formula is incorrect now.

See this

// Today first 3 bars
bi = BarIndex();
dn = Day();
firstB = dn != Ref( dn, -1 ); // 1st bar of the day
bars = BarsSince(firstB);
secondB = bars == 1; // 2nd bar of the day
thirdB = bars == 2; // 3rd bar of the day

// First Bar OHLC
firstB_Op = ValueWhen(firstB, O); // 1st bar open

secondB_Op = ValueWhen(secondB, O); // 2nd bar open

thirdB_Op = ValueWhen(thirdB, O); // 3rd bar open

No, you didn't .

Also I clearly wrote your code is OK.

So why do you add this one??

// First Bar OHLC
firstB_Op = ValueWhen(bi == firstB, O); // 1st bar open
firstB_Hi = ValueWhen(bi == firstB, H); // 1st bar high
firstB_Lo = ValueWhen(bi == firstB, L); // 1st bar low
firstB_Cl = ValueWhen(bi == firstB, C); // 1st bar close

// Second Bar OHLC
secondB_Op = ValueWhen(bi == secondB, O); // 2nd bar open
secondB_Hi = ValueWhen(bi == secondB, H); // 2nd bar high
secondB_Lo = ValueWhen(bi == secondB, L); //3rd bar low
secondB_Cl = ValueWhen(bi == secondB, C); //2nd bar close

Instead of rest of the code of 1st post of yours plus exploration code?

// Today first 3 bars
bi = BarIndex();
dn = Day();
firstB = dn != Ref( dn, -1 ); // 1st bar of the day
bars = BarsSince(firstB);
secondB = bars == 1; // 2nd bar of the day
thirdB = bars == 2; // 3rd bar of the day

// First Candle OHLC
firstB_Op = ValueWhen(firstB, O);
firstB_Hi = ValueWhen(firstB, H);
firstB_Lo = ValueWhen(firstB, L);
firstB_Cl = ValueWhen(firstB, C);

// Second Candle OHLC
secondB_Op = ValueWhen(secondB, O);
secondB_Hi = ValueWhen(secondB, H);
secondB_Lo = ValueWhen(secondB, L);
secondB_Cl = ValueWhen(secondB, C);

// Third Candle OHLC
thirdB_Op = ValueWhen(thirdB, O);
thirdB_Hi = ValueWhen(thirdB, H);
thirdB_Lo = ValueWhen(thirdB, L);
thirdB_Cl = ValueWhen(thirdB, C);

Cond1 = (firstB_Op > firstB_Cl) AND (secondB_Cl > secondB_Op); // all scripts whose 1st bar is red and 2nd bar is green

Parameters = Cond1;

Filter = Parameters;

STS = WriteIf( Parameters, "list", "list" );
Col = IIf( Parameters, colorGreen, 0 );

AddTextColumn( sts, "Action", 1.0, Col, colorWhite, 50 );
AddColumn( C, "Trigger", 1.2, colorBlack, 7 );
AddSummaryRows( 16, 1.2 );
SetSortColumns( 2 );
1 Like

Do you want to run in circles?

What does that mean when I write that?
Does that mean to add code by @nsm51 as you did.
Or does that mean to add rest of code of your code being OK?

1 Like

There was some sort of misunderstanding from my part as I mixed both your's and @nsm51 solutions. Yes, you are correct on your part. Sincere apologies. I have thoroughly gone again on all the comments and understood the error on my part. Thanks for taking time and help clear my logic. Really appreciate.

1 Like

So here is the corrected exploration code as you have mentioned: (Logic:- 1st bar of the day is Red and 2nd bar is Green)

#pragma nocache

bi = BarIndex();
dn = Day();
firstB = dn != Ref( dn, -1 ); // 1st bar of the day
bars = BarsSince(firstB);
secondB = bars == 1; // 2nd bar of the day
thirdB = bars == 2; // 3rd bar of the day

// First Candle OHLC
firstB_Op = ValueWhen(firstB, O);
firstB_Hi = ValueWhen(firstB, H);
firstB_Lo = ValueWhen(firstB, L);
firstB_Cl = ValueWhen(firstB, C);

// Second Candle OHLC
secondB_Op = ValueWhen(secondB, O);
secondB_Hi = ValueWhen(secondB, H);
secondB_Lo = ValueWhen(secondB, L);
secondB_Cl = ValueWhen(secondB, C);

// Third Candle OHLC
thirdB_Op = ValueWhen(thirdB, O);
thirdB_Hi = ValueWhen(thirdB, H);
thirdB_Lo = ValueWhen(thirdB, L);
thirdB_Cl = ValueWhen(thirdB, C);

Cond1 = (firstB_Op > firstB_Cl) AND (secondB_Cl > secondB_Op); // all scripts whose 1st bar is red and 2nd bar is green

Parameters = Cond1;

Filter = Parameters;

STS = WriteIf( Parameters, "list", "list" );
Col = IIf( Parameters, colorGreen, 0 );

AddTextColumn( sts, "Action", 1.0, Col, colorWhite, 50 );
AddColumn( C, "Trigger", 1.2, colorBlack, 7 );
AddSummaryRows( 16, 1.2 );
SetSortColumns( 2 );

The issue is as you can see the "Cond1" includes logic whose 1st bar is Red and 2nd bar is green [ Cond1 = (firstB_Op > firstB_Cl) AND (secondB_Cl > secondB_Op) ]. But when I run the exploration it also includes those scripts whose 2nd bar is red (instead of green). So there lies the problem where I thought there must be something wrong in my first code.

3

So as you can see, I have implemented the code provided by you on "as is" basis but still not getting desired results on exploration. That prompted me to think that there must be some error on the code from my part.

Please read this thread..

You need to do debug your code.

Output all four variables of the condition Cond1 via AddColumn and then compare.

// ....
// INCOMPLETE SNIPPET!
AddColumn(firstB_Op, "firstB_Op", 1.2);
AddColumn(firstB_Cl, "firstB_Cl", 1.2);
AddColumn(secondB_Cl, "secondB_Cl", 1.2);
AddColumn(secondB_Op, "secondB_Op", 1.2);
// ....

I told you in previous post that...

So for debugging modify Filter to

Filter = Parameters AND bars <= 1;// output till second bar of day

You will see that first bar of day is output too and it gets results of previous day's 2nd bar.

14

So you have to change filter to

// output AT second bar of day
Filter = Parameters AND bars == 1;

or (if wanting to get output till bar of day starting from 2nd bar of day)

// output SINCE second bar of day
Filter = Parameters AND bars >= 1;

or

// output at last bar of range (minimum 2nd bar)
Filter = Parameters AND bars >= 1 AND Status("LastBarInRange");

Note: If you include 3rd bar of day check to Filter then modify to ... bars == 2 or bars >= 2 etc depending on what you look for.

1 Like