day of month

Studies that have been contributed to the community by other users. If you’ve got something useful to share, that’s great!
User avatar
arnie
Posts: 1594
Joined: 11 Feb 2009
Location: Portugal
Has thanked: 481 times
Been thanked: 514 times

day of month

Postby arnie » 01 Oct 2010

Hi.

I'd like some help with the following signal:

Code: Select all

// set the begin of a new month with a true/false condition
if month(date) <> month(date)[1] then
condition1 = true
else
condition1 = false;

// set te limit for the week with a true/false condition
If DayOfWeek(Date) >= Monday and DayOfWeek(Date) <= Friday then
condition2 = true
else
condition2 = false;

// set a long position at the open of the nex bar if the previous 2 conditions are true
if condition1 = true and condition2 = true then
buy next bar at open;

// close the long position opened previously at the session open
setexitonclose;
This signal buys the market at the open and close that same position at the close for the first day of each month.
Now, my question is, what about the second or the third day of the month?

How can I specified a specific day of the month, taking in consideration that condition2 is true? How can I say that I want to buy at the 11th day of the month, in this case, the 11th trading day of the month and not the 11th calendar day of the month?

Best regards,
Fernando

User avatar
TJ
Posts: 7742
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2222 times

Re: day of month

Postby TJ » 01 Oct 2010

... How can I say that I want to buy at the 11th day of the month, in this case, the 11th trading day of the month and not the 11th calendar day of the month?

Best regards,
Fernando
just create a counter...

reset the counter to zero at the beginning of the month,
increment the counter for each new day...
since there is no Saturday, Sunday or holidays on the chart,
the counter will only count trading days.

User avatar
arnie
Posts: 1594
Joined: 11 Feb 2009
Location: Portugal
Has thanked: 481 times
Been thanked: 514 times

Re: day of month

Postby arnie » 02 Oct 2010

Hi TJ.

OK, counter created:

Code: Select all

inputs:
TradingDayNumber (1);

variables:
newMonth (false),
newDay (false),
monthCounter (0),
dayCounter (0);

// set the begin of a new month with a true/false condition
if month(date) <> month(date)[1] then begin
newMonth = true;
dayCounter = 1; // reset day count
end
else begin
newMonth = false;

// initiate day count
if date <> date[1] then begin
dayCounter = dayCounter + 1;
end;
end;

// set a long position at the open of the nex bar if the previous 2 conditions are true
if newMonth = true then
buy next bar at open;

// close the long position opened previously at the session open
setexitonclose;
Now I have the problem with linking the input TradingDayNumber with the variable DayCounter, so I can choose the day number on which I want to apply the signal.

Can you shine some light on it?
Tried something like:

Code: Select all

if newMonth = true then begin
if TradingDayNumber = dayCounter then
buy next bar at open;
end;
but had no luck :(

Fernando

User avatar
TJ
Posts: 7742
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2222 times

Re: day of month

Postby TJ » 02 Oct 2010

Hi TJ.
...but had no luck :(

Fernando
what do you mean by "...but had no luck" ?

-- no trigger?
-- triggered on wrong day? wrong bar?

they are 2 different problems and have to be handled differently.

User avatar
TJ
Posts: 7742
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2222 times

Re: day of month

Postby TJ » 02 Oct 2010

This is my suggestion for an intraday strategy.
note: this is the first coding run, I have not tested it on MC.

highlights:
1. no need for month count.
2. [intrabarordergeneration=true]
3. the order will be triggered on the first tick of "TradingDayNumber"

Code: Select all

[intrabarordergeneration=true]

inputs:
TradingDayNumber (11);

variables:
dayCounter (0);

// reset daycount to zero at beginning of month
if month(date) <> month(date)[1] then
dayCounter = 0;

// increment daycount everyday
if date <> date[1] then
dayCounter = dayCounter + 1;

// set a long position at the open if TradingDayNumber = dayCounter
if TradingDayNumber = dayCounter then
buy next bar at market;

// close the long position at EOD
setexitonclose;

User avatar
TJ
Posts: 7742
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2222 times

Re: day of month

Postby TJ » 02 Oct 2010

suggestion:

when coding a strategy,
always start with an indicator...

instead of a buy statement, use a plot statement to verify the logic is triggered in the right time/place.

when the indicator works, then replace the plot with the buy statement.

User avatar
TJ
Posts: 7742
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2222 times

Re: day of month

Postby TJ » 02 Oct 2010

note:

setexitonclose maybe used for testing, but it is not recommended for live trading.

It takes time to transmit the order to the exchange floor,
most brokers require the last order to be submitted at least 3 minutes before the session close.

User avatar
arnie
Posts: 1594
Joined: 11 Feb 2009
Location: Portugal
Has thanked: 481 times
Been thanked: 514 times

Re: day of month

Postby arnie » 03 Oct 2010

Hi TJ.

Thanks for the updates.

I took in consideration your suggestion and note and now I have created a time stop for the open long position:

Code: Select all

[intrabarordergeneration = true]

inputs:
TradingDayNumber (11),
endTime (1500);

variables:
dayCounter (0),
dayClose (0);

// reset the counter every month
if month(date) <> month(date[1]) then
dayCounter = 0; // reset day count to zero at beginning of month

// increment daycount everyday
if date <> date[1] then begin
dayCounter = dayCounter + 1;
// set a long position if TradingDayNumber = dayCounter
if TradingDayNumber = dayCounter then begin
buy next bar at market;
end;
end;

// close the long position at the time selected in the input
if time = endTime then begin
if marketposition = 1 then // confirms that we have a long position open
sell next bar at market;
end;
I was forced to move the buy order statement inside the daycount statement since the way you wrote it was generating 2 orders, one on the selected day and the other on the following day.

User avatar
TJ
Posts: 7742
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2222 times

Re: day of month

Postby TJ » 05 Oct 2010

I am glad to hear you have got it working.


Return to “User Contributed Studies and Indicator Library”