Incorporating End-of-Day Data into a Monthly Strategy
Author: bblidner
Creation Date: 8/20/2010 10:01 AM
profile picture

bblidner

#1
Is it possible to program a monthly strategy built in Wealth Lab Pro 5.6 to take advantage of daily changes that occur during the current month?

I have been back-testing a program that involves, in part, the StochK and the MACD using monthly data and have noticed that the “monthly values” for these indicators change as “daily” prices change during the current month. I would like to determine whether my back-test results would be improved if the end-of-month buy and sell signals were replaced with end-of-day StochK, MACD and price changes that occur during the current month.

I recognize that using end-of-day data within the current month may generate Buy/Sell signals that are not confirmed by “true” end-of-month signals. I expect that these transient signals will likely have a negative effect on back-test results. The question is whether these negative effects offset the probable benefits of Buying/Selling before the true end-of-month signal is generated.

For this program to be realistic, the first end-of-day signal that occurs during the current month needs to override the true end-of-month signal; i.e., even if the first end-of-day signal during the current month is “negated” before or at the end of the month, it would necessary to “force” the first end-of-day signal as the end-of-monthly signal.

Can a Wealth Lab Pro 5.6 based program be written to incorporate these functions? If so, could you please provide the code to accomplish this goal?? I am not a programmer.

Thank you for your assistance.

BBB
profile picture

Cone

#2
Of course, just about anything can be programmed if the time and motivation are there. The strategy, however, is not a trivial change because you must derive/calculate those "Monthly" indicators on Daily basis.

Just as an example (because I already had done it) this is what it looks like to do this for a Daily SMA indicator on an intraday basis. Run this script on, say, a 5-Minute chart. You'll see that the "Daily-based intraday indicator synchs up to the Daily indicator on the last bar of the day.

CODE:
Please log in to see this code.

My advice is to develop your Strategy using Daily indicators if you want to trade Daily. If Daily is too fast (too noisy) and Monthly is too long to wait, then try the Weekly scale. Explore these options first before trying something that will probably end up being a "toss up" in helping your Monthly strategy, i.e., for sure sometimes it will help and other times it will hurt.

10/8/2010 Edit: Corrected code, now accurate.
profile picture

avishn

#3
Multi-timeframe analysis would've been so much easier in WL if Synchronize() always assumed that the currently available last bar completes last period... Any of the trickery with manually calculating last period's value wouldn't be needed. I had to develop a separate set of intraday indicators just to work around that problem.
profile picture

Cone

#4
QUOTE:
always assumed that the currently available last bar completes last period.
It's not a "problem" that Wealth-Lab calculates Daily indicators from Daily bars and synchronizes them as such in an intraday chart. This is proper, by design, and what 98% of everyone is looking for. If you want to trade partial bar information on an intraday basis for your Daily indicators, then fine, but you're not really trading the daily indicator. Let's call it a hybrid indicator, something altogether different from the traditional use of indicators. If you think it gives you an advantage, that's great, but I'm not convinced... and I just may try to prove that in an upcoming AT article. Thanks for the idea.
profile picture

avishn

#5
I agree, "problem" is probably a little bit strong word here. The reason I'm mentioning it is because it is not unusual to backtest and then trade AtClose of the period (be it a day or a month), which in practical trading terms translates into ability to estimate the value of an indicator when approaching the moment of period's closing. Both the PartialValue and the multi-timeframe with intraday bars approaches make it difficult to do that. The former fails for multi-symbol strategies, the latter forces you to develop a separate library of hybrid indicators.
profile picture

Cone

#6
QUOTE:
ability to estimate the value of an indicator
Sure, but let's make up an example. You're looking to buy a stock after it turns up from a deeply oversold condition - for simplicity "RSI crosses above 20". Keeping with the context of bblidner's request, it's a Monthly strategy but 2 days before the month ends, the current month's RSI is showing 38 at whatever the current price is, x.

You can now estimate that barring some catastrophic event, the strategy will buy the stock 3 trading days later on the open of the first of the next month, but based on the present conditions you buy tomorrow, on the penultimate day of the month. You're entry price is x.

On the last day of the month, the opening price is x - $0.50, monthly RSI is now 32, so the monthly strategy indeed triggers the Alert to buy at market "tomorrow". But on the first of the month, opening price is x - $1.50. The basis price for entering the trade is $1.50 better for having stuck with the Strategy.

Of course the minus signs could be replaced with "+" signs, and you might have entered the trade with a better basis price too, and that's the whole point. Unless you have another Daily indicator that can predict where prices are heading with some statistical significance (such as an trained Neuro-Lab indicator) in the next 2 or 3 days, you must consider it a toss up entering the trade early. Of course this is perfectly valid too as long as you understand the complexity of the process (which I think avishn does, but someone asking about it may not).
profile picture

bblidner

#7
Cone,

Thanks for your help. I especially appreciate your insight regarding the Daily being too noisy/fast relative to Monthly. In this context, I believe I should look at Weekly changes during the current month as well as Daily changes.

Unfortunately, I have been unable to interpolate the information you provided regarding the application of intraday sma data to daily sma data to my script. Would you please show me how to set up my script, using the information provided below, so that end-of-the-month buy and sell signals are replaced with end-of-the-day and end-of-the-week stochk, stochd and macd changes that occur during the current month.
I assume the resulting script should be run on a daily (or weekly) chart.

Thank you for your assistance.

BBB


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

Cone

#8
Tip: Just before posting code, please click the CODE button right above the text box and paste your code between the code tags. tia.

That script works only for Intraday data and was just to show you that it's a small programming challenge to create indicators that do what you want to do. Sorry, but for at least the next 2 days I probably won't have time to create a Daily/Monthly example of the SMA for this.

Maybe avishn would like to share his library of these indicators?
profile picture

avishn

#9
It appears that daily/monthly and daily/weekly synchronization, unlike intraday/daily, works just fine. I.e. last bar always closes last period. Am I missing something? I never looked into it before, being more interested in intraday/daily combination and now find results a little bit surprising and inconsistent across different time frames.


(last bar is 8/20/2010)

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

Cone

#10
The Monthly series is synching to the last bar of the month, this is correct and by design and precisely what your chart shows. The last bar at 8/20 is synching because it's the last bar available for the month.
profile picture

bblidner

#11
Cone and avishn,

I'm making progress. Per the following code, I can now see the daily/monthly synchronization for prices based on the help you provided. Thanks.

CODE:
Please log in to see this code.



Is it possible to also show the end-of-the-day daily changes for the stochk, the stochd and the macd (i.e., can the daily and monthly values for these indicators be visualized in a manner analogous to the daily/monthly synchronization for prices). Your assistance in adding these features to the above code would be appreciated.

Also, I've noticed that when I include the Buy/Sell rules to the above code that the execution prices are based on the end-of-the-month prices, rather than the end-of-the-day closing price of the current month in which a "daily" Buy/Sell signal is generated. Is it possible to replace the end-of-the-month price with the first end-of-the-day price that generates a Buy/Sell signal. If so, could you provide the code that would enable this capability.

Thanks for your continued help.

BBB
profile picture

Cone

#12
QUOTE:
Is it possible
It's possible to code about anything you can think of.

QUOTE:
to also show
From the following choices, which do you want?

1. See Stochs and MACD based on Daily data. This is easy to do.
- OR -
2. See Stochs and MACD on a "partial-bar" Monthly basis. This is complicated because it means you need to recalculate the Monthly indicator based on all the previous months' data, but always use the current day's data as the current month's values. With the intraday example for SMA, I was trying to show you how complicated this is just for only 1 simple indicator.

QUOTE:
replace the end-of-the-month price with the first end-of-the-day price that generates a Buy/Sell signal
Anything is possible, but the path to get there depends on how you answer above.
profile picture

bblidner

#13
I'd like to see Stochs and MACD based on Daily data added to the paneStoch and paneMACD, respectively, to the code provided above.

Also, please recall that I have been back-testing a program that involves, in part, the StochK and the MACD using monthly data and have noticed that the “monthly values” for these indicators change as “daily” prices change during the current month. My goal is to determine whether my back-test results would be improved if the end-of-month buy and sell signals were replaced with end-of-day (or end-of-week) StochK, MACD and price changes that occur during the current month.

Thanks

BBB
profile picture

Cone

#14
To get the Daily indicators, just drag and drop them into your chart. If you want to see them as Weekly indicators, then just change to the Weekly scale. To see the code of the dropped indicators, click the "Push all Indicators..." button on the far right of the Function Toolbar.

CODE:
Please log in to see this code.
This will happen for Wealth-Lab too. But if you're trading a Monthly strategy, you shouldn't run it (or even look at it to avoid emotion) until the end of the month. If you want to trade on those daily changes on Monthly indicators, then you should develop and backtest the strategy that way (option 2).
profile picture

bblidner

#15
I've not been able to get a working code based on your 8/25 directions.

Using the Wealth-Lab Pro 5.6 code I posted on 8/24, I can drag and drop daily stochk, daily stochk and daily MACD into the chart. There are two problems with the resulting chart: 1) The daily stochd and the daily stochk are not in the current stoch panel (these drag and drop procedures create two new panels) and 2) The daily stochd, stochk and macd are not synchronized with the monthly values for these indicators. Can these daily indicators be placed in the current stoch panel and can these daily values be synchronized with the monthly values for these indicators.

Additionally, when I push these 3 new indicators into the code, multiple coding errors are noted (lots of red underlining). The coding that results from these pushed indicators cannot be successfully compiled.

Can you tell me (or provide the code) that addresses these problems. For reference, below is the code I see once I push the three daily indicators into the code I posted on 8/24.

CODE:
Please log in to see this code.


Thanks
BBB
profile picture

Cone

#16
First, for a quick lesson on plotting. See the WealthScript Programming Guide: Painting the Chart > Plotting DataSeries > How to Plot a DataSeries.

The point I want to make here is that if something plots in different panes, it's very easy to fix - use only one of the pane references.

So, if you want the Daily and Monthly Stochs all in the same pane, just delete the ChartPane paneStochK1 = CreatePane(40,true,true); statement, and replace paneStochK1 with paneStoch in the PlotSeries statements.

Anyway, let me clean this up for you because dragging and dropping doesn't really give you an easy way to use a reference to those series in the Strategy itself.

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

bblidner

#17
The panes are great.

Before I attempt to code the strategy (thanks for the hint that I will have to pay attention to macd[bar], macd_m[bar], etc.), could you please tell me (or provide the code) as to how to synch up the daily macd, stochk and stochd with a) the end of the month values for these indicators and b) the end-of-day values for the current month.

Thanks again.

BBB

profile picture

Cone

#18
They won't synch up. You chose "option 1". You have indicators based on Daily bars and indicators based on Monthly bars.

To get "option 2" (monthly bars updating with each Daily close as if it were the end of the next month), you need to recode all your indicators in a similar way that I showed you using the SMA example in my first post. Seriously, I think it would take even me at least an entire day to do that (but I'm generally slow).
profile picture

Cone

#19
Okay, it took me about an hour to figure this out for just the StochK. This is what is involved for each one of your indicators.

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

bblidner

#20
Above code looks great. We are definitely on the right track. Thank you very much for your time and efforts.

I've run this code against SPY. Based on what I saw
1) I changed the plot of the monthlyStoK_daily to LineStyle.Solid to enable visualizing stocK_m, monthlyStoK_daily, stocK_D, monthlyStoD_daily in the same pane
2) Regardless of how the monthlyStoK_daily is plotted, I get the following Debug and Error Messages:

0.170 0.170
16.832 16.832
31.548 31.548
39.481 39.481
39.066 39.066
63.944 63.944
94.926 94.926
93.970 93.970
84.379 84.379
95.968 95.968
96.538 96.538
78.946 78.946
84.518 84.518
96.257 96.257
90.582 90.582
46.882 46.882
16.186 16.186
43.545 43.545

Are these messages a concern??


Going forward, I will attempt to modify your code to achieve a similar result for the stochd. In this regard, is it approriate to apply your exact coding for the stochk synchronization to stochd synchronization? i.e., is it correct (and sufficient) to code the _dperiod as CreateParameter("Sto.D Period", 10, 2, 21, 1)? I would like to make sure that the stochd uses a smoothing value of 3. In the meantime, could you please show me how to synch the monthly macd with the daily macd.

Thanks for your continued help and support.

BBB
profile picture

Cone

#21
QUOTE:
Are these messages a concern??
Of course not. It's showing you that on the last day of the month, the StochK calculated in 2 different ways (Monthly and "Monthly every day") are the same. The script is just a "mock up". It would be difficult to use this indicator in the form given.


QUOTE:
I will attempt
This is not the way you will need to consume these indicators. If you do this 3 more times, you'll have a bunch of "spaghetti code" that you won't be able to troubleshoot. You need to create functions for these indicators, and it's highly recommended that you put them into a formal library, which is something it sounds like you're not going to be able to do.

avishn spoke up before and said that he had done this already for his indicators. Sure would be nice if he offered his components so that you don't have to spend days trying to repeat it.

As for me, I really think this exercise is a waste of time for the reasons I already mentioned.
profile picture

bblidner

#22
I'm more than willing to switch focus from synchronizing monthly-daily to synchronizing monthly-weekly. Would this be a better utilization of your time? I'd appreciate your continued support.

Thanks,
BBB
profile picture

Cone

#23
Not at all. The code works for Weekly as well. Just change the scale to Weekly.
profile picture

bblidner

#24
OK

Based on your code to synch the monthly stochk with the daily stochk, I've attempted to write code to synch the monthly stochd with the daily stochd. The resulting code (see below) plots the monthly stochd and daily stochd just fine, however, these the stochs do not synch at the end of the month. Would you please tell/show me where I went wrong.

CODE:
Please log in to see this code.


Thanks again,
BBB
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).