Backtesting Chinese holidays
Author: tradercn
Creation Date: 9/28/2011 4:05 PM
profile picture

tradercn

#1
In China's stock market, oct 1st is the national day, and thus a holiday. I want to buy at the open of the last trading day before oct 1st, and sell at the close of the first trading day after oct 1st.

So I built a simple rule: Buy at Market: Date is before a Calendar Date(Oct 1); Sell at Market: Date is after a Calendar Date(Oct 1). I have two problems here.
1, how to buy at open? there is no such command.
2, from the chart, the trading signals all are wrong. For example, the buying signal was triggered on 2010/1/5, and the selling signal was triggered on 2010/10/11. I looked through the code, but could not see any mistake. Here is the code:

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

Eugene

#2
1 - BuyAtMarket is buy at open.

2 -

a) 2010/1/5 is perfectly before Oct 1, isn't it? The entry does what it's programmed to do: buy if Date < Oct 1. It will buy whenever a date falls between last exit and Sept 30, on ANY date.

b) So does the exit: sell at Close after Oct 10.
profile picture

tradercn

#3
You are amazing, Eugene.
How can I set the last trading day before or the first trading day after some specified day with a program? I am not a programmer, please understand my difficulty. Many thanks.
profile picture

Cone

#4
Although the exit signal will work, it would be difficult to create this type of entry rule with the Strategy Builder - although I think it's possible by combining several more conditions with day-of-week logic. The weekends are the complicating factor.

But there are plenty of coding solutions here. One way is to let the script peek ahead to see if the next bar is in October AND that the current bar is in September. The resulting script, however, could be used only for backtesting because it couldn't process the most-recent bar.

A better way is to program a function that finds the number of weekdays (assumed to be trading days) before the specified date. If the function returns 2, buy at market on the next bar. Run this script after today's trading, you should get an Alert to buy.

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

tradercn

#5
thanks for your code and idea. :)
profile picture

tradercn

#6
There is a problem. If I test the new year date, this strategy doesn't work. I tried to figure out the solution, eventually saw a function which is included in Community Components library. It is TradingDaysBetweenDates. But from the update log of Community Components library, it says "6/22/2011 Changed: Calculate.GetWeekNumber/DaysBetweenDates/GetRemainingTradingDays/TradingDaysBetweenDates became obsolete, were moved to DateTimeFunctions. Due to their exclusion in a later release, use their DateTimeFunctions analogues. " I don't understand what the last sentence means. could you please explain? I saw "1/11/2011 Added: DateTimeFunctions_US class with useful Date-related functions (assembled by Sammy_G) " Is that what you mean?
Thanks.
profile picture

tradercn

#7
I looked through the source code of wealthlab.components.community, there is a region, which is Date/Time functions. I guess that is what you mentioned. I have two questions.
1, ItsAHoliday indicates solar calendar holidays. However it refers the holiday property of marketInfo object. (How many holidays are included in this property please? Can I customize which date is a holiday?) How can I add Chinese lunar calendar holidays here? (Since ItsAHoliday is referred in other functions.) Do you have any idea? Do I need to make a dll specially designed for Chinese lunar calendar holidays? Can I includes some source code files regarding lunar holidays out of my strategy? If yes, how to do that please? Or, I have to write these codes in my strategy?
2, In the region of Date/Time functions, I saw many functions. For example, ItsAHoliday, ItsAWeekend, CalendarDaysBetweenDates, GetNextTradingdate, AddTradingDays, and etc. Do you have a document to describe these functions, to show how to use it, as API document? For my above case, to tell the last trading day before the new year day, is DaysBetweenDates or TradingDaysBetweenDates good for replacing WeekdaysBeforeDate? I am afraid that both are not. Correct? Because dt1 and dt2 are not the same year.
At last, is there any example to show how to refer functions in WealthLab.Components.Community?
My questions might be very simple to you, but please be patient because I am very new. Thanks for your help.
profile picture

tradercn

#8
Alright, I know how to get the detailed information regarding functions in wealthlab.components.community and thus how to refer the functions in it as well. So two concerns are still left. Thanks.
For the first concern, should I manually add the Chinese holiday each year to Markets.xml which I just saw in Sammy's post.
profile picture

tradercn

#9
It took me several weeks to figure out china's holidays, but eventually it was almost done except one question for this strategy. I am very excited because this is my first C# program by my hands. :)
Here is my strategy idea. There are three parameters. The first is for choosing which holiday in the 7 major China holidays including solar and lunar holidays. The default value is 4, for National Day. It varies from 1 to 7, 1 is for New Year Day. The second and the third parameter are used to buy and sell before and after how many days offset from the holiday.
The only problem I am facing might be very simple for you. From the above posts, Cone provided a function WeekdaysBeforeDate to choose the date to buy. However, first it doesn't work for the new year day because the years buy and sell are different. Second, for each holiday, the number of days the market which is off is not specified. Since this strategy is for back testing only, so when date>holiday, sell at bar+intDaysAfterHoliday -1. And buy at bar-DaysBeforeHoliday+1. In my understanding, it should be fine. You can see those at the last several lines. But it doesn't generate any trading signal. Could you please help? The code is very long, you can focus on the last several sentences only.
Once it is done, it can be contributed to WL. There are so called bonus trends before the major holidays in China's stock market. With this strategy, this kind of saying can be proved or not.
Thanks.

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

tradercn

#10
Since the code includes some Chinese characters, they are not showed properly. I would like to send you the whole strategy code. but how can I attach a text file to you? Or, can I have your email address? Thanks.
profile picture

Cone

#11
Wow, a lot of questions here and no answers.. yet.

Re: 10/7/2011 3:18 PM
TradingDaysBetweenDates originally existed in the Calculate class, but the new class, DateTimeFunctions, was created to make it more obvious where to find functions related to dates. Both classes will still work for now, but the latter is preferred.

Re: 10/9/2011 10:52 AM
1. Re: ItsAHoliday. This is a simple function: Bars.MarketInfo.Holidays.Contains(DateTime dt). You can add holidays to the Markets.xml file, which can be found in the main installation's \Data folder. Make sure to keep with the proper xml formatting. Solar calendar only. You're on your own for a lunar calendar.

2. Re: ItsAHoliday, ItsAWeekend, CalendarDaysBetweenDates, GetNextTradingdate, AddTradingDays, and etc...
Documentation, if it exists is on the Wiki site. But since these are self-explanatory, and you have the source code, I think the only thing you need know is what I just described about Holidays being in the Markets.xml file.

QUOTE:
For my above case, to tell the last trading day before the new year day, is DaysBetweenDates or TradingDaysBetweenDates good for replacing WeekdaysBeforeDate?
Use TradingDaysBetweenDates, the dates can be in any year. Keep in mind the the holidays in Market.xml. It seems to be key for you.

Re: 10/9/2011 11:44 AM
I should have read all of what you wrote first!

Re: 10/28/2011 10:35 AM
CODE:
Please log in to see this code.
Sure it works if the dates are in different years. It only tells you the number of Weekdays there are between 2 dates.

QUOTE:
Second, for each holiday, the number of days the market which is off is not specified
I'm not entirely sure what you mean by this. You should enter each day that the market is "off" that is not a weekend in Markets.xml.

Maybe it's a stupid question, but why are there Chinese characters in your code? I'll put your code between CODE tags for you (please click the CODE button before pasting code next time), but I can't promise that we have time to look over all your code.
profile picture

Cone

#12
The error that you get when compiling is due to attempting to use a StrategyParameter as a number. You must use the .Value or .ValueInt method to return a double or integer value, respectively, for a StrategyParameter.

CODE:
Please log in to see this code.
Actually you had the "int" conversions earlier in the code, but you just missed it here.
profile picture

Eugene

#13
QUOTE:
Maybe it's a stupid question, but why are there Chinese characters in your code?

btw here's the original class:
http://www.wowpc.cn/thread-10948-1-1.html
http://www.cnblogs.com/Csharpblogs/articles/2122124.html
profile picture

tradercn

#14
Eugene, are you Chinese? :) You are able to read Chinese. Haha! The reason I keep Chinese in the above code is because I am sensitive to Chinese than English.
Yeah, I copied this class as you pointed, and simplified it. After that, I created two functions based on this class. Function one, IsChinaStockHoliday(DateTime dt), is used to tell if the specified date is China's holiday, and return what the holiday it is, for example, New Year, Spring Festival, and etc. Function two, HolidayDate(int intYear, int intHoliday) is used to return the specified solar calendar date by providing the year and what holiday option such as Spring Festival.
Actually all past questions are cleared except one. Let me show you guys the modified strategy code part without the class ChineseCalendar. That contains the problem I could not figure out.
CODE:
Please log in to see this code.

Thanks for your help for this post, especially Cone. I modified the code with Cone's modification above. But I did not use the ValueInt, in stead, I created variables intDaysBeforeHoliday and intDaysAfterHoliday. This strategy still can't generate trade signals. I went though again and again, but no clue so far.
profile picture

tradercn

#15
Found the problem, but don't know how to fix it.
IsLastPositionActive is returned as False. so that if (IsLastPositionActive) was flowed to do nothing. I changed this sentence to be if (IsLastPositionActive==false) , the compiler showed a run-time error. what's wrong with it please?

profile picture

Eugene

#16
Certainly your WealthScript strategy is problematic though it's not the biggest trouble. It should look something like this:
CODE:
Please log in to see this code.

However, the problem as I see it lies in a rather poor design of the ChineseCalendar class, forcing its user to create an instance of it on each bar of the loop due to the fact that it's initialized with a single Date. Therefore it should be refactored to have a parameterless constructor and a Date property that would be looked up by any of its methods.
profile picture

tradercn

#17
Eugene, I changed my codes in your way. But the number of the trades are not right based on the index data from Jan 1 1997 to Dec 31 2006. But thanks anyway first.
1, If I set HolidayOption as 1, or New Year Day, There are 0 trade;
2, If I set HolidayOption as 2, or Labor Day. There are only 7 trades in 10 years data;
3, If I set HolidayOption as 3, or National Day. There are only 4 trades in 10 years data;
4, If I set HolidayOption as 4, or Spring Festival. There is no trade;
5, If I set HolidayOption as 5, or Ching Ming Festival. There are only 7 trades in 10 years data;
6, If I set HolidayOption as 6, or Dragon Boat Festival.There are only 8 trades in 10 years data;
7, If I set HolidayOption as 7, or Mid-autumn Festival, There are only 7 trades in 10 years data.
Yes, the ChineseCalendar class do create an instance of it on each bar of the loop. But without doing that, how can we tell when is closest holiday. My program knowledge is very limited, so far I can't fully understand what you said technically. Even I can't tell what is the problem of ChineseCalendar. You might be able to modify it in your understanding. :)
I will try to work on solve the above problem, but not confident. If you are able to help me for these problems, you will be greatly appreciated.
profile picture

Eugene

#18
What if you change it this way?
CODE:
Please log in to see this code.
profile picture

tradercn

#19
Hi Eugene, Thanks for your modification. It is ok but trade signals are triggered in the wrong date. however it gave me a clue to change it. Here is my modification:
CODE:
Please log in to see this code.

It is almost perfect for all china stock market holidays, but except New Year Day. No trade signals are generated for New Year Day. :) I don't have any clue for that. Could you please take time to think about this code again? Sorry to bother you again.
profile picture

Eugene

#20
If one particular holiday date (i.e. New Year) is problematic, then I think it's time to revisit the part of the ChineseCalendar class which is in charge of determining New Year.
profile picture

tradercn

#21
Hi Eugene,

To prove your answer is right, I removed the ChineseCalendar class and my two customized functions completely from my strategy. Then the holiday was forced to be the new year day specially. Here is the code, but it can't generate trade signal neither.
CODE:
Please log in to see this code.

That means the reason of no signal is regardless of ChineseCalendar class. There should be a simple reason, I guess. Maybe it is something we ignored. FYI.
Although I tested trading new year day in a manual way, it is not a good idea, but I wish this strategy is perfect. Hopefully you understand.
profile picture

Eugene

#22
How about peeking into the future a bit? Buying on one of the preceding bars seems perfectly valid in the context:
CODE:
Please log in to see this code.

P.S. Instead of casting a .Value to (int), simply use .ValueInt like shown above.
profile picture

tradercn

#23
It is much better. But if I change the DaysAfterHoliday value, it is interesting that the sell dates don't change when DaysAfterHoliday varies from 1 to 2. When I set DaysAfterHoliday as 1, the sell signal triggers at the 2nd day rather than the 1st day after the holiday. I don't know why. thanks.
profile picture

tradercn

#24
Based on your guys help, I modified my code, eventually it works perfect. Here is the code.

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

Eugene

#25
Good news!

You might want to publish the strategy's original XML file (which preserves Chinese characters):

Upload your Strategy

If you get to publishing it, please provide some description (use the Description box in Strategy Editor in WLD). Thanks!
profile picture

tradercn

#26
Hi Eugene, I did not see the description box as you mentioned above, that's why I uploaded this strategy directly.
To make this strategy successfully, you and cone helped me a lot. Thanks for both of you.
profile picture

Eugene

#27
QUOTE:
I did not see the description box as you mentioned above

Already replied in a support ticket.
profile picture

tradercn

#28
It is done. Please let me know if it is good.
profile picture

Eugene

#29
You need to upload it and click "Publish".
profile picture

tradercn

#30
I guess it is ok now. You are so patient, Eugene. You guided me step by step. Thanks so much. :)
profile picture

Eugene

#31
We're almost there. One more step left. See the technician comment I just posted to your strategy.
profile picture

tradercn

#32
Eugene, I don't understand the technician comment even after you explained in the support. I could not see the description you mentioned in the previous ticket.
profile picture

Eugene

#33
It was attached to my reply to your closed ticket. Notice the attachment icon? I even left complete instructions.
profile picture

tradercn

#34
Great! It is re-uploaded! Thank you so much.
profile picture

tradercn

#35
Hi Eugene,

I saw this strategy was published successfully and was approved, but where can I see it on the forum? Thanks.

I found it eventually. Forget this reply please. Thank you anyway.
This website uses cookies to improve your experience. We'll assume you're ok with that, but you can opt-out if you wish (Read more).