Is there any way of calculating the last Thursday of the month on the first day of the month?

@Karan there are a couple of attempts in the User Library (I have not tried them, so test them out).

http://www.amibroker.com/members/library/detail.php?id=1417

http://www.amibroker.com/members/library/detail.php?id=461

I came up with a quick few lines to identify the third Thursday of each month (in past). But verify that this is correct as I only had a quick look. Also you probably want some sort of method of counting how many days remain until the next third Thursday occurs?

```
dow = DayOfWeek();
ThirdThursd = dow==4 AND (Day() >=15 AND Day() <=21);
// Explore to debug //
Filter=1;
AddColumn( IIf( dow==1, 'M', IIf(dow==2, 'T', iif(dow==3, 'W', IIf(dow==4, 'R', 'F' )))), "Day", formatChar );
AddColumn(DayOfWeek(), "D.O.W.", 1.0);
AddColumn(thirdThursd, "thirdThursd", 1.0, colorDefault, IIf(thirdThursd, colorGreen, colorDefault));
// Chart //
Plot(thirdThursd, "thirdThursd", colorBlue, styleHistogram|styleThick|styleOwnScale);
```

Are you trying to find the last Thursday of the month for back testing purposes (when all relevant bars are already available), or are you trying to look into the future to see when the next option expiry is? For example, you run an Exploration today and want to know that June 28 2018 is an expiration date (assuming it is not a holiday).

I misread the original post, Matt pointed out he wants the “last” Thursday of the month so my code was not aimed at identifying the correct day.

I want to see what the next option expiry is. For eg on 1st June 2018 ,after running an exploration I should know that the expiry date is 28th June 2018.

That should be pretty straightforward by using the `Day()`

and 'DayOfWeek()` functions illustrated in @portfoliobuilder's example, right? Have you tried to code this yourself yet? You probably want to do something like this:

- Determine the current day of the month
- Find the day of the month corresponding to the next Thursday
- Add an appropriate multiple of 7 to that day to get to the end of the month, which is a little bit tricky only because months have different numbers of days.

So I have used this code .

```
function Lastthursday()
{
Daysinmonth=IIf(Month()==1 OR Month()==3 OR Month()==5 OR Month()==7 OR Month()==8 OR Month()==10 OR Month()==12,31,30);
Daysinmonthfeb=IIf(Year()%4 == 0 AND Year()%100!=0,29,28);
Daysinmonthfinal=IIf(Month()==2,Daysinmonthfeb,Daysinmonth);
Lastthursday=IIf(Daysinmonthfinal-Day()<7 AND DayOfWeek()==4,Datenum(),Null);
return returnvalue;
}
RolloverDate = ValueWhen(Lastthursday , DateNum()) ;
So when I do an exploration , I get the Expiry date on the date of expiry because of Lastthursday=IIf(Daysinmonthfinal-Day()<7 AND DayOfWeek()==4,Datenum(),Null);
But on the First of every month all i get is Null
```

That's why I asked if you were trying to find a future date. The last element of all your arrays, including the array returned by `Datenum()`

, will correspond to the last bar of data available for the current symbol, or the symbol to which you Pad & Align, or perhaps to one bar beyond that if you're using the Artificial Future Bar option. The point is, you can't use the `Datenum()`

array to find the future date you're looking for because that array probably does not extend far enough into the future.

Instead, try thinking about how to build your own value in Datenum format by starting with the current date. Also keep in mind that the current date could be after the current month's expiry, so you will need to deal with looking for your expiration date in the next month.

Hello, I have corrected various calculations according to indications to get the third Friday of each month. What happens is that I have it the other way around, the days pass by graphing me in histogram but nope he paints me with a bar the day that corresponds to the third Friday, it is blank. Can you help me to paint only on the indicator the bar of the third Friday of the month?

Cheers

```
function DaysInMonth(MonthNum, YearNum)
{
_Daysinmonth=IIf(MonthNum == 1 OR MonthNum == 3 OR MonthNum == 5 OR MonthNum == 7 OR MonthNum == 8 OR MonthNum == 10 OR MonthNum == 12, 31, 30);
Daysinmonthfeb=IIf( (YearNum % 4 == 0 AND YearNum % 100 != 0) OR (YearNum % 4 == 0 AND YearNum % 400 == 0), 29, 28);
_Daysinmonth=IIf(MonthNum==2, Daysinmonthfeb, _Daysinmonth);
return _Daysinmonth;
}
function DaysToThirdFriday()
{
d = Day();
wd = DayOfWeek();
DaysToFriday = IIf(5-wd<0, (12-wd) % 7, (5 - wd) % 7);
ThirdFriday = ((d + DaysToFriday) % 7)+14;
ThirdFriday = IIf(ThirdFriday==14, 21, ThirdFriday); //corrects problem if the first of the month is a Saturday
_DaysToThirdFriday = ThirdFriday - d;
/* To change behavior after the third friday, but before the end of the month,
to use zeroes, instead of counting down to next month, uncomment the second
line, below */
_DaysToThirdFriday = IIf(_DaysToThirdFriday >= 0, _DaysToThirdFriday,
ThirdFriday+IIf(ThirdFriday+14>DaysInMonth(Month(),Year()),28,35)-d);
//_DaysToThirdFriday = IIf(_DaysToThirdFriday >= 0, _DaysToThirdFriday, 0);
return _DaysToThirdFriday;
}
Plot(DaysToThirdFriday(), "Days to 3rd Friday", colorBlack, styleHistogram);
//
//
```

@LOL111, when you copy code then you should insert link to source of the code into the code.

As for the

```
Plot(DaysToThirdFriday() == 0, "3rd Friday", colorBlack, styleHistogram);
```

ohhhh thanks!!!!! I had not noticed this detail. I will study it in other indicators. Thank you