Selling at the end of each half year
Author: bishop
Creation Date: 4/22/2015 7:09 PM
profile picture

bishop

#1
Hi,

I am trying to code a simple strategy that sells on the last trading day of each half year, on June 30 and December 31, and then re-enters the position the next day using BuyAtMarket order.

I've created a code below from Rules that is assumed to sell on June 30, then re-enter the market on July 1. Then sell on December 31 and buy again on January 1. However, the code simply buys and sells on every second bar.

I know it is far from perfect, I am still trying to get how Date functions work and I have a few questions:

1. Why does the code avoids selling at the end of each half and instead sells on every second day? It seems that selling rules are coded correctly.

2. How do I do when a year doesn't have 6/30 or 12/31 trading days? Is the lastBarOfMonth function found in "Sell in May and go away" appropriate for identifying the actual last day of the month which I can use to find the last trading days in June and December?

Thank you.
profile picture

bishop

#2
Forgot to attach the code

CODE:
Please log in to see this code.
profile picture

Eugene

#3
1 - The code functions exactly as programmed, exiting either when the date is greater than June 29 or is greater than December 30. Due to the unconditional BuyAtMarket, virtually it happens on any given date. Yesterday when replying to your question, I pointed you also at this discussion which explains and illustrates it. Check out post #12 for some theory/practice.

2 - Good question. The lastBarOfMonth function is useless in this context because of the possible holiday (no trading = no daily bar). What should help find the actual trading day is the IsLastTradingDayOfMonth method (and/or IsLastTradingDayOfQuarter), documented in the WealthScript Programming Guide > WealthLab.Rules Classes > DateRules. I'm not sure what is the sense in your trading rules as they are not too much different from Buy & Hold but here they are:

CODE:
Please log in to see this code.


P.S. In addition to the DateRules, there are more date helper methods scattered around (well, not exactly scattered -- actually collected in Community Components). One day you may need them:

DateOfNextTradingDay
TomorrowIsLastTradingDayOfWeek
GetRemainingTradingDays
DaysBetweenDates and TradingDaysBetweenDates
TomorrowIsLastTradingDayOfWeek
GetNextEarningsDate
GetWeekNumber
profile picture

bishop

#4
Eugene,

I looked at this thread very carefully but only now I can understand the logic.

Thank you for your code. Actually, it does exactly what I am looking for.

However, I one question please: Why does your code skips one bar after exiting the trade? Is there a way to make it trade immediately on the next bar, like Cone's code in Rule Based Rebalancing Strategy thread?

Actually, I have modified Cone's version by putting
CODE:
Please log in to see this code.


right after the
CODE:
Please log in to see this code.
and the strategy now buys on the first trading day of July and December but I am wondering why does your code skips. Where is the catch?

Thank you for the links.
profile picture

Eugene

#5
QUOTE:
Is there a way to make it trade immediately on the next bar, like Cone's code in Rule Based Rebalancing Strategy thread?

For example, you can achieve that with this little tweak:

CODE:
Please log in to see this code.

becomes
CODE:
Please log in to see this code.
profile picture

bishop

#6
Understood. Thanks