Detect last trading day of the month  [SOLVED]

Questions about MultiCharts and user contributed studies.
nuno-online
Posts: 174
Joined: 31 Jan 2006
Has thanked: 74 times
Been thanked: 5 times

Detect last trading day of the month

Postby nuno-online » 08 Feb 2015

Hello

is there a easylanguage function or an easier way to detect the last trading day of each month?
( for example sell the last day of month...)

thanks

Nuno

User avatar
TJ
Posts: 7151
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 988 times
Been thanked: 2041 times

Re: Detect last trading day of the month

Postby TJ » 08 Feb 2015

Hello
is there a easylanguage function or an easier way to detect the last trading day of each month?
( for example sell the last day of month...)
thanks
Nuno
One way to do it is to check to see the last day (30th, or 31st, or whatever) is not on a Saturday or Sunday. Beyond that, you have to make sure the day is not on a holiday list.

That means you have to do your checking 4 or 5 days before the end of the month.
eg. If 31st is on a Sunday, then you want to complete your evaluation and decided your sell order by the Thursday prior.
These users thanked the author TJ for the post:
nuno-online

User avatar
JoshM
Posts: 2099
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1527 times
Been thanked: 1502 times
Contact:

Re: Detect last trading day of the month  [SOLVED]

Postby JoshM » 09 Feb 2015

Re: Detect last trading day of the month
is there a easylanguage function or an easier way to detect the last trading day of each month?
( for example sell the last day of month...)
If you don't want to take holidays into account (or don't have a historical list of holidays for the instruments in question), I think two things need to be done:

* The next business day will need to be determined. When the current trading day is Friday, the date of the next business day is + 3; otherwise, it's + 1. (Assuming the instrument doesn't trade on Saturday)

* The month of the next business day will need to be compared with the current day's month. When the next business day happens in another month, the current day can be considered the last day of the month.

In code, the above reasoning would look like:

Code: Select all

Variables:
isLastDayOfMonth(False),
isSameMonth(False),
nextBusinessDay(0);

// 'nextBusinessDay' is the next date in YYYMMdd EasyLanguage format
if (DayOfWeek(Date) = Friday) then
nextBusinessDay = JulianToDate(DateToJulian(Date) + 3)
else
nextBusinessDay = JulianToDate(DateToJulian(Date) + 1);

// 'isSameMonth' is true when the next business day is the same month as today
isSameMonth = (Month(Date) = Month(nextBusinessDay));

// 'isLastDayOfMonth' is true when the next business day is in a
// different month, excluding the possibility of (bank) holidays
if (not isSameMonth) then
isLastDayOfMonth = true
else
isLastDayOfMonth = false;

// Check with plotting
if (isLastDayOfMonth = true) then
Plot1(1)
else
Plot1(0);
Visually, it looks correct (but do check it yourself :] ):

Image
Attachments
scr.09-02-2015 08.43.42.png
scr.09-02-2015 08.43.42.png (6.9 KiB) Viewed 1755 times
These users thanked the author JoshM for the post:
nuno-online

User avatar
JoshM
Posts: 2099
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1527 times
Been thanked: 1502 times
Contact:

Re: Detect last trading day of the month

Postby JoshM » 09 Feb 2015

By the way, my above post complicates things a little bit if you only want to backtest. Because with running a script on historical data, the Symbol_Date keyword can be used to access the date of the next bar.

That way you can take (bank) holidays into account and don't need to correct for weekends. Because in both of those situations, there wouldn't be a trading bar on that day and so `Symbol_Date` would return the bar after that.

Just keep in mind that you don't reference a bar too far in the future, since that would trigger an error that would turn off the script. In other words, `Symbol_Date[-1]` (for the next bar) can only be referenced as long as Symbol_CurrentBar is less than Symbol_Length.

nuno-online
Posts: 174
Joined: 31 Jan 2006
Has thanked: 74 times
Been thanked: 5 times

Re: Detect last trading day of the month

Postby nuno-online » 09 Feb 2015

Hi TJ & JoshM

Thanks for your reply

with an indicator detecting the last trading day of the month, i want to backtest a portfolio in order to take a decision (example sell the last trading day)

Nuno

arjfca
Posts: 1282
Joined: 23 Nov 2010
Has thanked: 724 times
Been thanked: 222 times

Re: Detect last trading day of the month

Postby arjfca » 09 Feb 2015

This indicator will show the end of the week and end of the month

Look trough it to find how I decode the month

Code: Select all

// Week and months deliminator by Martin Theriault 2012/12/10
Inputs:

WeekColor (Blue),
MonthColor ( darkGreen),
MonthLineSize(0.5),
MonthLineStyle(Tool_Dotted);

Var:
LineTime_1 (0),
LineNumHigh_Intra (0),
LineNumLow_Intra (0),
Offset(0),
HighIntra (0),
LowIntra (0),
StartTheHighLow (false),
Bardone (false),
UpperPrice (0),
LowerPrice (0);

Offset = 50* Minmove/PriceScale;
//LineTime_2 = calctime_S(lineTime,120);
{
If barnumber = 0 then begin
LineNumHigh_Intra = TL_New_s (Date, Time_s,0, Date, Time_s, 0);
LineNumLow_Intra = TL_New_s (Date, Time_s,0, Date, Time_s, 0);
end;
}
If barstatus = 2 then begin

Lowerprice = getappinfo(ailowestDispValue)*.5;
UpperPrice = getappinfo(aiHighestDispValue)*4;


If weekbegin = true then begin
Bardone = True;
Value1 = TL_New_S(D,Time_S,H+(Offset),D,Time_S,upperprice);
TL_SetColor(Value1,WeekColor);
TL_SetStyle(Value1,Tool_dotted);
TL_SetSize(Value1,0.5);
Value10 = TL_New_S(D,Time_s,l-offset,D,Time_S,lowerPrice);
TL_SetColor(Value10,WeekColor);
TL_SetStyle(Value10,Tool_dotted);
TL_SetSize(Value10,0.5);
End;

If Monthbegin = true then begin
Bardone = True;
Value1 = TL_New_S(D,Time_S,H+(Offset),D,Time_S,upperprice);
TL_SetColor(Value1,MonthColor);
TL_SetStyle(Value1,MonthLineStyle);
TL_SetSize(Value1,MonthLineSize);
Value10 = TL_New_S(D,Time_s,l-offset,D,Time_S,lowerPrice);
TL_SetColor(Value10,MonthColor);
TL_SetStyle(Value10,MonthLineStyle);
TL_SetSize(Value10,MonthLineSize);
End;
End; //Barstatus =2
http://my.jetscreenshot.com/21599/20150210-xhkm-181kb
Martin

Edit: I forgot that the MonthBegin was a function

Code: Select all

//MonthBegin
// Return true if the bar is the first bar of a new month

MonthBegin = False;
If (Month(date) <> Month(date[1])) then MonthBegin = true;
These users thanked the author arjfca for the post (total 2):
TJnuno-online

nuno-online
Posts: 174
Joined: 31 Jan 2006
Has thanked: 74 times
Been thanked: 5 times

Re: Detect last trading day of the month

Postby nuno-online » 10 Feb 2015

arjfca
thank you for your indicator

In this forum http://forum.mql4.com/51054, i found a script about last trading day
I wrote this script in easylanguage .... but something is wrong:
the indicator doesn't detect the last trading day!

Code: Select all

vars: LastTradingDay(false), Mytime_48hours_1sec(0);

LastTradingDay = false;
If month(date+1) <> month(date) then
LastTradingDay = true;

Mytime_48hours_1sec = 60*60*24*2+1; // Seconds*Minutes*Hours*Days+(1s)
If dayofweek(date) = Friday and Month(CurrentTime) <> Month(CurrentTime+Mytime_48hours_1sec) then
LastTradingDay = true;

If LastTradingDay then
plot1(1, "LastTradingDay")
else
plot1(0, "LastTradingDay");
Nuno

User avatar
JoshM
Posts: 2099
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1527 times
Been thanked: 1502 times
Contact:

Re: Detect last trading day of the month

Postby JoshM » 11 Feb 2015

arjfca
thank you for your indicator

In this forum http://forum.mql4.com/51054, i found a script about last trading day
I wrote this script in easylanguage .... but something is wrong:
the indicator doesn't detect the last trading day!

Code: Select all

If month(date+1) <> month(date) then
LastTradingDay = true;
Because `Date` is in YYYMMdd EasyLanguage format, you cannot perform accurate arithmetic on it. It first need to be converted. I showed how in the code example I posted earlier in this thread.

Code: Select all

If dayofweek(date) = Friday and Month(CurrentTime) <> Month(CurrentTime+Mytime_48hours_1sec) then
LastTradingDay = true;
Month requires a date in YYYMMdd format while `CurrentTime` returns a time in HHmm format. You probably want to use `CurrentDate` here instead.

Personally, I'd convert times in HHmm format to DateTime first before arithmetic on it, since otherwise it's waiting on bugs to happen. In this case, let's say `CurrentTime` returns 910 (9:10 am). Adding 48 hours plus 1 second gives 173711 (910 +(60*60*24*2+1)), which will be 17:37:11 if you'd use `CurrentTime_s`. Incidentally, that would be a valid time, but certainly not 48 hours plus 1 second later.

nuno-online
Posts: 174
Joined: 31 Jan 2006
Has thanked: 74 times
Been thanked: 5 times

Re: Detect last trading day of the month

Postby nuno-online » 12 Feb 2015

JoshM

Many thanks for your help
I corrected the error with your advice for the first part of the indicator

Code: Select all

LastTradingDay = false;
If month(JulianToDate(DateToJulian(Date) + 1)) <> month(date) then
LastTradingDay = true;
but I d'ont see how to fix the second part (in order to progress in EasyLanguage)

Code: Select all

Mytime_48hours_1sec = 60*60*24*2+1; // Seconds*Minutes*Hours*Days+(1s)
If dayofweek(date) = Friday and Month(CurrentTime_s) <> Month(CurrentTime_s+Mytime_48hours_1sec) then
LastTradingDay = true;
Finally I will keep your indicator that is very effective

Nuno


Return to “MultiCharts”