How to get Weekday as array from date?

Hello everyone! I am trying to get weekday as an array from date, as which is some possible to 'DateTimeFormat( "%w", --)', but by this outcome only as string and its parameter is also limited to a number. But I want to pass a parameter as array. please suggest me to code it right way. Thanks in advance!


`_SECTION_BEGIN("Price1");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%)
{{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
_SECTION_END();

NumDate = DateNum();
AddDay = 2;
NextDateTime = DateTimeConvert(0, DateTimeAdd( DateTimeConvert( 1, NumDate), AddDay, inDaily));

WEEKDAY = DayOfWeek();
DateTime_ = DateTime();
MyWeekDay = DateTimeFormat( "%w", LastValue(DateTime_)); //this will return a string and argument as a number,
//but I want to pass an array here as specified datenum or any date like:
SpecifiedWeekDay = DateTimeFormat( "%w", NextDateTime );

GfxTextOut("WEEKDAY: "+WEEKDAY, 100, 100);
GfxTextOut("NumDate: "+NumDate, 100, 120);
GfxTextOut("MyWeekDay: "+MyWeekDay, 100, 140);
GfxTextOut("SpecifiedWeekDay: "+SpecifiedWeekDay, 100, 160);`

DayOfWeek() is an Array. Use Valuewhen Function to get the week day of specific Day.

AFL Function Reference - DAYOFWEEK (amibroker.com)

AFL Function Reference - VALUEWHEN (amibroker.com)

Thanx for reply, but sir when the particular date which is not available in database like holidays or any future date then

MyDayOfWeek= valuewhen(MyDay == 1181014, dayofweek());  //1181014 is not available in db

will not give the correct output.

You need to find out the work around solution for that.

For Holidays you can define Values...MyDayOfWeek= valuewhen(MyDay == 1181014, 0);

But you have specified your requirement where You want to implement this one.

If you want to implement this for Option expiry of Thursday or Tuesday then Totally different approach can be used for that.

Yes it may be, but sir to handle all the db holidays, is a difficult task. I want to do it as for all calendar days. like 'DateTimeAdd()' can give us any next or previous date without depending on our db.

Look at this LINK

Thanks a lot, yes I got it and some more exploration also I visit :slight_smile:
It goes something like this:

Taking example of date 4th August,1999

Step 1:

0 3 3 6 1 4 6 2 5 0 3 5 are magic numbers for the months

Jan-0, feb-3, mar-3, apr-6, may-1,jun-4

Jul-6, aug-2, sep-5, oct-0, nov-3, dec-5

only when the year is a non leap year but when a year is a leap year number changes for jan and feb so for a leap year it is :

6 2 3 6 1 4 6 2 5 0 3 5 are magic numbers for the months

Jan-6 feb-2 mar-3, apr-6, may-1,jun-4

Jul-6, aug-2, sep-5, oct-0, nov-3, dec-5

so for August magic number is 2

Step 2:

For the given year ( 1999 in this case ) if the first 2 digits are of the form then the number corresponding to it is

4n → 6

4n+1 → 4

4n+2 → 2

4n+3 → 0

As 19 is the first two digits of 1999, it is of the form 4n+3 so number corresponding to it is 0

Step 3:

Now divide the last two digits by 4 and note the quotient and round it off to nearest integer

99/4=24

Step 4:

Add the following

date + magic number of month from step 1 + number obtained in step 2 + quotient obtained in step 3 + last two digits of the year

4+2+0+24+99 = 129

Step 5:

Divide the number obtained in step 5 by 7 and note the remainder obtained.

If the remainder is as follows then it corresponds to following day

0 → Sunday

1 → Monday

2 → Tuesday

3 → Wednesday

4 → Thursday

5 → Friday

6 → Saturday

Dividing 129 by 7 we get remainder 3 and 3 corresponds to Wednesday

so 4th August 1999 was Wednesday

1 Like

You can do simply this:

  1. Enable View->Pad Non-Trading days
  2. Use DayOfWeek() function.

Also even without padding, you can get earliest working day before requested data simply using Cross

MyDayOfWeek= valuewhen( Cross( MyDay, 1181014 ), dayofweek());

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.