Strategy buy first day of the month  [SOLVED]

Questions about MultiCharts and user contributed studies.
tranitroom
Posts: 91
Joined: 01 Jun 2011
Has thanked: 11 times
Been thanked: 1 time

Strategy buy first day of the month

Postby tranitroom » 22 Mar 2013

how can I write a formula that buy in opening the first day of every month?
i have writhe this but don't work well

Code: Select all

Input: MyStopLoss(1100);

If dayofmonth(date)=1 and CloseM(1) < OpenM(0) then buy this bar at close;

setexitonclose;
setstoploss(MyStopLoss);

User avatar
furytrader
Posts: 353
Joined: 30 Jul 2010
Location: Chicago, IL
Has thanked: 155 times
Been thanked: 215 times

Re: Strategy buy first day of the month

Postby furytrader » 22 Mar 2013

The problem with your code is that the first day of the month may not have the day number of 1, if the new month starts on a Saturday or Sunday.

Try something like:

Code: Select all

If Month(Date) <> Month(Date[1]) ....


In this way, it compares the month of the current date versus the month of the date one bar previously. If they do not match, we have a new month.

Hope this helps.

tranitroom
Posts: 91
Joined: 01 Jun 2011
Has thanked: 11 times
Been thanked: 1 time

Re: Strategy buy first day of the month

Postby tranitroom » 22 Mar 2013

thanks fury,
I tried so but skips a few months

Code: Select all

Input: MyStopLoss(1100);

If Month(Date) <> Month(Date[1])then begin
   If dayofmonth(date)=1 then buy this bar at close;
end;

setexitonclose;
setstoploss(MyStopLoss);

User avatar
Henry MultiСharts
Posts: 8878
Joined: 25 Aug 2011
Has thanked: 1233 times
Been thanked: 2859 times

Re: Strategy buy first day of the month

Postby Henry MultiСharts » 27 Mar 2013

tranitroom wrote:thanks fury,
I tried so but skips a few months

Most probably these months fall into MaxBarsBack bars.
You can check your MaxBarsBack value in Format->Strategy properties->Properties tab.
(8) [FAQ] MaxBarsBack

User avatar
TJ
Posts: 7012
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 984 times
Been thanked: 2008 times

Re: Strategy buy first day of the month

Postby TJ » 27 Mar 2013

tranitroom wrote:how can I write a formula that buy in opening the first day of every month?
i have writhe this but don't work well

Code: Select all

Input: MyStopLoss(1100);

If dayofmonth(date)=1 and CloseM(1) < OpenM(0) then buy this bar at close;

setexitonclose;
setstoploss(MyStopLoss);

Which months are missing?

Can you clarify, are you looking to buy on

the first day of every month? (eg. Dec 01, 2012 is a Saturday)
or
the first trading day of every month?

There is a difference in coding.

User avatar
furytrader
Posts: 353
Joined: 30 Jul 2010
Location: Chicago, IL
Has thanked: 155 times
Been thanked: 215 times

Re: Strategy buy first day of the month

Postby furytrader » 27 Mar 2013

This is what I was trying to explain before (admittedly, maybe my point wasn't clear):

If you use the code

Code: Select all

If dayofmonth(date)=1 ...


It will only register TRUE if the first trading day of the month is ALSO the first day of the month. If the first trading day of the month is NOT the first calendar day of the month, then it will register FALSE.

User avatar
TJ
Posts: 7012
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 984 times
Been thanked: 2008 times

Re: Strategy buy first day of the month

Postby TJ » 27 Mar 2013

tranitroom wrote:thanks fury,
I tried so but skips a few months

Code: Select all

Input: MyStopLoss(1100);

If Month(Date) <> Month(Date[1])then begin
   If dayofmonth(date)=1 then buy this bar at close;
end;

setexitonclose;
setstoploss(MyStopLoss);


ps.

see post #7
[FAQ] This Bar on Close
viewtopic.php?f=16&t=10811

tranitroom
Posts: 91
Joined: 01 Jun 2011
Has thanked: 11 times
Been thanked: 1 time

Re: Strategy buy first day of the month  [SOLVED]

Postby tranitroom » 30 Mar 2013

thanks guys for the support I solved with this formula and it works

If Month(Date) <> Month(Date[1])then begin
If ((dayofmonth(date)=1)or(dayofmonth(date)=2)or(dayofmonth(date)=3)or
(dayofmonth(date)=4)or(dayofmonth(date)=5))
then buy this bar at close;
end;

User avatar
TJ
Posts: 7012
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 984 times
Been thanked: 2008 times

Re: Strategy buy first day of the month

Postby TJ » 30 Mar 2013

tranitroom wrote:thanks guys for the support I solved with this formula and it works

If Month(Date) <> Month(Date[1])then begin
If ((dayofmonth(date)=1)or(dayofmonth(date)=2)or(dayofmonth(date)=3)or
(dayofmonth(date)=4)or(dayofmonth(date)=5))
then buy this bar at close;
end;


Thanks for sharing your code.

An easier short cut would be:

Code: Select all

If Month(Date) <> Month(Date)[1] then
begin
     buy ... ;
end;



ps.
[FAQ] How to Post Codes
viewtopic.php?f=16&t=11713

automaton
Posts: 34
Joined: 04 Apr 2017
Has thanked: 2 times
Been thanked: 4 times

Re: Strategy buy first day of the month

Postby automaton » 06 Dec 2018

TJ

Does your solution target the first trading day of month, regardless if after a weekend, weekday, or holiday?


Return to “MultiCharts”