NexusFi: Find Your Edge


Home Menu

 





Please help coding day of month strategy!!


Discussion in Traders Hideout

Updated
      Top Posters
    1. looks_one FastNCurious with 7 posts (2 thanks)
    2. looks_two ABCTG with 4 posts (5 thanks)
    3. looks_3 kevinkdog with 1 posts (2 thanks)
    4. looks_4 biffhero with 1 posts (1 thanks)
      Best Posters
    1. looks_one kevinkdog with 2 thanks per post
    2. looks_two ABCTG with 1.3 thanks per post
    3. looks_3 biffhero with 1 thanks per post
    4. looks_4 FastNCurious with 0.3 thanks per post
    1. trending_up 3,088 views
    2. thumb_up 10 thanks given
    3. group 3 followers
    1. forum 11 posts
    2. attach_file 0 attachments




 
Search this Thread

Please help coding day of month strategy!!

  #1 (permalink)
 
FastNCurious's Avatar
 FastNCurious 
saint louis MO
 
Experience: Intermediate
Platform: TradeStation
Trading: NQ, ES, YM, CL, GC
Posts: 149 since Oct 2017
Thanks Given: 95
Thanks Received: 177

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

 
Code
///////////////////////////////////////begin comments///////////////////////////////////////////

//long side
//buy on the 830 open of the 7th 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



//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 17th 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


///////////////////////////////////////begin strategy logic///////////////////////////////////////////

If marketposition=0 then begin
If month(date) = month(date[6]) then buy next bar at market;
If barssinceentry>=2 and close<close[1] then set exit on close;
end;


If marketposition=0 then begin
If month(date) = month(date[16]) then sell short next bar at market;
If barssinceentry>=2 and close>close[1] then buytocover next bar at market;
end;

Visit my NexusFi Trade Journal Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Pivot Indicator like the old SwingTemp by Big Mike
NinjaTrader
Trade idea based off three indicators.
Traders Hideout
What broker to use for trading palladium futures
Commodities
REcommedations for programming help
Sierra Chart
ZombieSqueeze
Platforms and Indicators
 
  #2 (permalink)
 kevinkdog   is a Vendor
 
Posts: 3,666 since Jul 2012
Thanks Given: 1,892
Thanks Received: 7,360


jburke75 View Post
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


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.)

try this, it works:


 
Code
If marketposition=0 then begin
If month(date) = month(date[6]) then buy next bar at market;

end;

If barssinceentry>=2 and close<close[1] then setexitonclose;


If marketposition=0 then begin
If month(date) = month(date[16]) then sell short next bar at market;

end;

If barssinceentry>=2 and close>close[1] then buytocover next bar at market;

Follow me on Twitter Reply With Quote
Thanked by:
  #3 (permalink)
 
FastNCurious's Avatar
 FastNCurious 
saint louis MO
 
Experience: Intermediate
Platform: TradeStation
Trading: NQ, ES, YM, CL, GC
Posts: 149 since Oct 2017
Thanks Given: 95
Thanks Received: 177



kevinkdog View Post
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.)

try this, it works:


 
Code
If marketposition=0 then begin
If month(date) = month(date[6]) then buy next bar at market;

end;

If barssinceentry>=2 and close<close[1] then setexitonclose;


If marketposition=0 then begin
If month(date) = month(date[16]) then sell short next bar at market;

end;

If barssinceentry>=2 and close>close[1] then buytocover next bar at market;

It is always something simple. LOL Thank you so much.

Visit my NexusFi Trade Journal Started this thread Reply With Quote
  #4 (permalink)
 ABCTG   is a Vendor
 
Posts: 2,436 since Apr 2013
Thanks Given: 482
Thanks Received: 1,629

jburke75,

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.
 
Code
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
 
Code
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 View Post
It is always something simple. LOL Thank you so much.


Follow me on Twitter Reply With Quote
Thanked by:
  #5 (permalink)
 
FastNCurious's Avatar
 FastNCurious 
saint louis MO
 
Experience: Intermediate
Platform: TradeStation
Trading: NQ, ES, YM, CL, GC
Posts: 149 since Oct 2017
Thanks Given: 95
Thanks Received: 177

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.


Sent using the NexusFi mobile app

Visit my NexusFi Trade Journal Started this thread Reply With Quote
  #6 (permalink)
 ABCTG   is a Vendor
 
Posts: 2,436 since Apr 2013
Thanks Given: 482
Thanks Received: 1,629

jburke75,

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.
 
Code
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.
 
Code
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 View Post
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.


Sent using the NexusFi mobile app


Follow me on Twitter Reply With Quote
Thanked by:
  #7 (permalink)
 ABCTG   is a Vendor
 
Posts: 2,436 since Apr 2013
Thanks Given: 482
Thanks Received: 1,629

jburke75,

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 View Post
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.


Sent using the NexusFi mobile app


Follow me on Twitter Reply With Quote
Thanked by:
  #8 (permalink)
biffhero
San Jose, CA
 
Posts: 10 since Sep 2020
Thanks Given: 3
Thanks Received: 1


jburke75 View Post
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.


@jburke75,

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

Reply With Quote
Thanked by:
  #9 (permalink)
 
FastNCurious's Avatar
 FastNCurious 
saint louis MO
 
Experience: Intermediate
Platform: TradeStation
Trading: NQ, ES, YM, CL, GC
Posts: 149 since Oct 2017
Thanks Given: 95
Thanks Received: 177


biffhero View Post
@jburke75,

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.

 
Code
//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;

Visit my NexusFi Trade Journal Started this thread Reply With Quote
Thanked by:
  #10 (permalink)
 
FastNCurious's Avatar
 FastNCurious 
saint louis MO
 
Experience: Intermediate
Platform: TradeStation
Trading: NQ, ES, YM, CL, GC
Posts: 149 since Oct 2017
Thanks Given: 95
Thanks Received: 177



biffhero View Post
@jburke75,

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

Visit my NexusFi Trade Journal Started this thread Reply With Quote




Last Updated on November 2, 2020


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts