Exploration of the code

Hi,

I am exploring the below snippet code .

Flag= (Day() != Ref( Day(), -1 ) ) ;

ami_flag

Every time the above condition is true.But why only first time the flag variable set to "1".
If i run the code on 25th jan 2019 so the condition should always 25 !=24 and it is always true.But the flag variable set to "0" for the remaining runs. Can any expert please explain.

You assumption is wrong. Day is not equal to DateTime. Day() function returns "current" bar's day of month (1-31, but no month, no year and no time). The day value in your example picture is always the same one. It is 25 there.

But your flag is saying only return true (1) if "current" bar's day is not equal to previous bar's day. So since before 25/01/2019 09:15:00 the day of month has been 24 you have a new start of day in your yellow marked row. So it is returned true only when a new day starts.

If you want to return true when day of current bar is equal to day of previous bar then

Flag= Day() == Ref( Day(), -1 );

Upper flag would return true for all intra-day bars except for the first bar of day (well, since it is the opposite of your other flag).

== is equality check.

!= is inequality check.

As for meaning of operators take a look at following page
https://www.amibroker.com/guide/a_language.html

1 Like

@dav if you had just added a little to your exploration you would have seen that "a picture is worth a thousand words", and your "Flag" is just "First Bar Of New Day"

Flag= (Day() != Ref( Day(), -1 ) ) ;

Filter=1;
AddColumn(Flag, "Flag", 1.0);
AddColumn(Day(), "Day()", 1.0);

image

2 Likes

Great help for the newbie @fxshrat and @portfoliobuilder . So Day() will give current bar's day(1-31).
Like that i am exploring further to get current bar's time. I found the timenum();
is this is the correct way to get the current bar's time? or else any other way is there?

If you want to get most recent "current" date/time output in Explorer then you do not need any coding.
All you need is setting to 1 recent bars in range setting of analysis toolbar.

36

But you may also use Filter variable and assigning lastbarinrange to it. It does the same thing even if you have set to different Range in Analysis toolbar.

Filter = Status( "LastBarInRange" );

If you want to output time number in addition to already existing datetime column:

Filter = Status( "LastBarInRange" );
AddColumn( TimeNum(), "Time Number", 1 );

TimeNum() function is an array of time numbers (max. 6 digits, min. one digit -> "00:00:00" returns as 0 (zero)).

Another function containing time information is DateTime() function.


If you want to output current time number or any most recent element anywhere then you may use LastValue() function.

last_time = LastValue(TimeNum());

or

last_dt = LastValue(DateTime());

// print last datetime in Information window
printf( "Last DateTime: %s", DateTimeToStr(last_dt));

// Output last datetime in Explorer
Filter = 1; // or  Status( "LastBarInRange" );
AddColumn( last_dt, "Last DateTime", formatDateTime);
AddColumn( TimeNum(), "TimeNum array", 1 );
AddColumn( LastValue(TimeNum()), "Last Time Number", 1);

etc.

549

LastValue is an element of array.

Please read how AFL works to understand the difference between array and elements of array.

3 Likes

thanks @fxshrat. If i know the barIndex can i get the Timenum() for the bar?

[type or paste code here](https://forum.amibroker.com/t/filter-low-volume-activity-stocks/10982/4)
/// @link https://forum.amibroker.com/t/filter-low-volume-activity-stocks/10982/3
if ( Interval() != in5Minute && Status( "actionex" ) != actionExEditVerifyFormula )
    Error( "Set to 5-minute interval!" );
//
SetBarsRequired(2*inDaily/Max(1,Interval()));
//
bi = BarIndex();
dn = DateNum();
newday = dn != Ref( dn, -1);
//
StartBI = ValueWhen(newday, bi, 2);
EndBI = ValueWhen(newday, bi, 1);
bars_yesterday = EndBI-StartBI;


Here i know the StartBI and EndBI. So from StartBI to EndBI i am fetching the timenum values.

condition1= StartBI<BarIndex()<EndBI ;
time=ValueWhen(condition1,TimeNum(),1); 

I know the condition is wrong. Can you put me into correct direction.

@beppe, @portfoliobuilder can you guys guide me. That, is it possible to get the timenum() values of the bar, if we know the corresponding BarIndex value ?

Yes. but explain in detail.
Your previous post isn't very clear to me.

condition1= StartBI<BarIndex()<EndBI ;

What do you mean by this?

x = 10 < 15 < 20;
// simply means
y = 10 < 15; 
// which is either 1 or 0 and then 
x = y < 20;
// in this case, 1 < 20, so,
x value is 1;

Use IIF() instead if you wanted to extract a range of bars.

1 Like

Thanks @travick. By below code we can get the barindex values of yesterday.

[https://forum.amibroker.com/t/filter-low-volume-activity-stocks/10982/4](https://forum.amibroker.com/t/filter-low-volume-activity-stocks/10982/4)
SetBarsRequired(2*inDaily/Max(1,Interval()));
//
bi = BarIndex();
dn = DateNum();
newday = dn != Ref( dn, -1);
//
StartBI = ValueWhen(newday, bi, 2);
EndBI = ValueWhen(newday, bi, 1);
bars_yesterday = EndBI-StartBI;

I am trying to get the Open price values of yesterday for every bar .
So, to get this what approach should i follow?

see, through this whole thread, the point keeps getting diverted and I still don't exactly get what you are trying to achieve.

Can you take time out and properly explain this

I am trying to get the Open price values of yesterday for every bar .

Use a chart if necessary.

Thanks for your quick response.
I am planning to get open,high,low,close values of each intraday bar in yesterday.
So valuewhen or IIF() or for loop which approach i need to follow.If possible can you give sample code. By the code we know the start BarIndex and End BarIndex of yesterday
So By using barindex can we get the open,high,low and close prices?

That depends on what you want to do.
Take time and explain in detail.

Thanks for your quick response.
Not useful to the cause

I am planning to get open,high,low,close values of each intraday bar in yesterday.
Just partially describing what you want to do.

So valuewhen or IIF() or for loop which approach i need to follow.If possible can you give sample code. By the code we know the start BarIndex and End BarIndex of yesterday
So By using barindex can we get the open,high,low and close prices?
This is for some member like me to suggest based on what you want to do. Does not describe your goal in any way.

If we keep running around in circles, its not going to lead you anywhere.