Welcome to NexusFi: the best trading community on the planet, with over 150,000 members Sign Up Now for Free
Genuine reviews from real traders, not fake reviews from stealth vendors
Quality education from leading professional traders
We are a friendly, helpful, and positive community
We do not tolerate rude behavior, trolling, or vendors advertising in posts
We are here to help, just let us know what you need
You'll need to register in order to view the content of the threads and start contributing to our community. It's free for basic access, or support us by becoming an Elite Member -- see if you qualify for a discount below.
-- Big Mike, Site Administrator
(If you already have an account, login at the top of the page)
I need help coding something that I have had trouble with. All I want to do is buy the 7 trading day of each month and sellshort the 17 trading day of the month. Pretty simple strategy but I am getting no order fills on my chart. I am using tradestation 9.5
Here is the code for your reference
Can you help answer these questions from other members on NexusFi?
You actually were getting 1 fill on the chart, but that trade would never close because your exits are within the "marketposition=0" if block (which is never true once you are in a trade.)
when you apply the strategy to a daily chart your entries might come one bar later than you want them.
The long entry condition for example evaluates to true on the end of the seventh bar of the month and the entry would get
triggered at the open of the eighth day.
If month(date) = month(date[6]) then buy next bar at market;
This condition would also evaluate to true on all remaining days of the month and could trigger another trade from a flat position.
Something along the lines of
month(date) = month(date[5]) and month(date) <> month(date[6])
could trigger a trade at the open of the seventh bar of the month and would prevent additional entries during the same month.
Regards,
ABCTG
jburke75
It is always something simple. LOL Thank you so much.
Thank you for the correction. I was getting the extra fills as you said. I’m not sure I understand the logic behind the code. When I read this code i read....if day of month =5 and day of month is not = 6 then buy next bar at market. I am not sure how this negates a trade on the 8th day of the month while the other code allows the trade to occur.
looking at the two parts of the condition should clarify this. Keeping in mind that your code does not check for day of the month, but for month i.e. month(date) evaluates to a number from 1 to 12.
If month(date) = month(date[5])
The first part would evaluate to true (when applied to a daily chart without intrabar order generation) at the end of the sixth day of the month.
However, it would also evaluate to true on the seventh, eighth etc. until a new month begins. Therefore, we need something to make sure that the condition
can only evaluate to true at the end of the sixth daily bar.
month(date) <> month(date[6])
The above evaluates to true if the current bar's month number is different than the number six bars to the left. This is true starting with the very first bar of a new month in your example, but no longer than the sixth bar of the month. After that month(date[6]) will evaluate to the same number as month(date) until a new month starts.
Regards,
ABCTG
jburke75
Thank you for the correction. I was getting the extra fills as you said. I’m not sure I understand the logic behind the code. When I read this code i read....if day of month =5 and day of month is not = 6 then buy next bar at market. I am not sure how this negates a trade on the 8th day of the month while the other code allows the trade to occur.
it can make sense to use the built-in Custom 1 Line indicator on your chart using "month(date)" (without the "") as input for Formula. This way you can see how and when the number changes.
Regards,
ABCTG
jburke75
Thank you for the correction. I was getting the extra fills as you said. I’m not sure I understand the logic behind the code. When I read this code i read....if day of month =5 and day of month is not = 6 then buy next bar at market. I am not sure how this negates a trade on the 8th day of the month while the other code allows the trade to occur.
Thank you for the correction. I was getting the extra fills as you said. I’m not sure I understand the logic behind the code. When I read this code i read....if day of month =5 and day of month is not = 6 then buy next bar at market. I am not sure how this negates a trade on the 8th day of the month while the other code allows the trade to occur.
Are you _sure_ you want to use the month() function call? By my reading of the dictionary in my multicharts code editor, it looks like the month() function returns one of the numbers in the range from 1 - 12.
I thought you wanted a "trading day of the month", which looks like it will require the use of DayOfMonth() and some logic around holidays and what day the first of the month falls on. The good news is that the 7th trading day is mostly the 9th of the month. The bad news is that it is not sometimes. ;-)
Are you _sure_ you want to use the month() function call? By my reading of the dictionary in my multicharts code editor, it looks like the month() function returns one of the numbers in the range from 1 - 12.
I thought you wanted a "trading day of the month", which looks like it will require the use of DayOfMonth() and some logic around holidays and what day the first of the month falls on. The good news is that the 7th trading day is mostly the 9th of the month. The bad news is that it is not sometimes. ;-)
Have fun,
Rob
I didn't know there was this dayofmonth fuction but here is the final code that actually works in TS for the idea I have originally which was buy only on a certain number trading day of the month and sellshort on another.
//short side
//set your charst to daily, day session only, custom day session 830-1510 central time.
//ending 5 minutes early to exit on close
//sell short on the 830 oprn of the 5th trading day of the month
//starting with the 2nd day in the trade, exit on the first 3:10pm custom session close
//that is higher than the previous days close
//do not take new short signal if currently long
//long side
//buy on the 830 open of the 5th trading day of the month
//starting with the 2nd day in the trade, exit on the first 3:10pm custom session close
//that is lower than the previous days close
//do not take new long signal if currently short
If marketposition=0 then begin
If month(date) <> month(date[4])
and month(date) = month(date[3])
and month(date) = month(date[2])
and month(date) = month(date[1])
then sell short next bar at market;
end;
If barssinceentry>=2 and close>close[1] then buytocover next bar at market;
If marketposition=0 then begin
If month(date) <> month(date[19])
and month(date) = month(date[1])
and month(date) = month(date[2])
and month(date) = month(date[3])
and month(date) = month(date[4])
and month(date) = month(date[5])
and month(date) = month(date[6])
and month(date) = month(date[7])
and month(date) = month(date[8])
and month(date) = month(date[9])
and month(date) = month(date[10])
and month(date) = month(date[11])
and month(date) = month(date[12])
and month(date) = month(date[13])
and month(date) = month(date[14])
and month(date) = month(date[15])
and month(date) = month(date[16])
and month(date) = month(date[17])
and month(date) = month(date[18])
then buy next bar at market;
end;
If barssinceentry>=2 and close<close[1] then sell next bar at market;
Are you _sure_ you want to use the month() function call? By my reading of the dictionary in my multicharts code editor, it looks like the month() function returns one of the numbers in the range from 1 - 12.
I thought you wanted a "trading day of the month", which looks like it will require the use of DayOfMonth() and some logic around holidays and what day the first of the month falls on. The good news is that the 7th trading day is mostly the 9th of the month. The bad news is that it is not sometimes. ;-)
Have fun,
Rob
I am not sure if this DayOfMonth function would be very helpful for me as I am trying return a yes/no answer about every month of every year.
see definition
DayOfMonth (Reserved Word)
Returns the day of month for the specified calendar date.
DayOfMonth(cDate);
Where cDate is a numeric expression representing the six or seven digit calendar date in the format YYMMDD or YYYMMDD respectively (1999 = 99, 2001 = 101).
I would have to put in the exact date of each 5th day of the month. that would not work for what I am trying to write code for.
Please correct me if I am wrong. I am so new at all this that I question everything