The Danger Of Weekly Scripting

by Dec 13, 2017All Articles, Technical Analysis, Education

I want to take a different approach in this post. Often we talk about the breakthroughs that we are working on, or the market in general. This time I want to draw attention to a testing error that I’ve seen others perform—and it’s all to do with understanding data.

This error can be so critical that it can make a mediocre system test into the veritable holy grail that returns billions in a decade. It happened to me while I was working on a model for a hedge fund system.

Here’s how it goes: I produce a script for a strategy, and I get ok returns, but too many trades. So I think to myself “self – what if I use weekly data?” Unfortunately “self” is easily swayed— it’s ok to have voices in my head isn’t it? Anyway the trap that many of us fall into is that we may convert perhaps one of the conditions into weekly, but leave the others as daily. We run a test and get sensational results… “Honey, quit your job because we’ve hit the big time!”.

So what is going on? This is something that you must be aware of if you are mixing timeframes in tests. It all comes down to how a weekly bar is built. NOTE: we are going to fix this in an update next year, but until we do, you need to be aware of this. When we are trading day-by-day, a weekly bar grows with every new day that is added to it. Remember that a bar is the first, highest, lowest, and last value for the period. So a weekly bar opens on Monday, records the week’s highest and lowest values, and then records the last value on Friday. On Tuesday, we have just two bars that have contributed to the weekly bar. But that’s not the final bar. The final bar is going to include all the days of the week.

This image highlights the problem. For the whole week we are actually testing with the final values for the week.

When we test and we use a WEEK() function, we are actually getting the final values for the whole week. That is, that test on Tuesday actually includes the data for the whole week! So essentially we have some future data that we are using. This is a huge issue, and one that we are going to solve, but in the meantime please, please, please be careful of this.

Let’s take this one step further as we’ve seen some people mix up the placement of the WEEK() function as well. Compare these two scripts that are run on daily data:

#1: MA(WEEK())

#2: WEEK(MA())

If you are not careful, you may think they will give you the same result, but they don’t. In #1, Optuma is converting the daily data to weekly bars, and then calculating the moving average on those weekly bars. In #2, Optuma is calculating the moving average on the daily data, and then converting those average values into a weekly open, high, low, and close. Just remember that inside functions get calculated first.

What can we do to solve the Week error in testing? If you offset the script to use last week’s values, then there is no issue (because it’s only the current week that should have been building). For example, if I want a signal when the price crossed above a weekly Moving Average, I could do this:

The “[1]” after the variable I named “V1” will tell Optuma to get the previous value. That is not going to change as new data is added, so it is safe to use.

The other way to solve this is to change the Signal, or back-tester to use weekly data in the properties of the test and not consider daily at all. There are no issues if we do that. Our concern is only in mixing daily and weekly (or weekly and monthly, you get the idea).

This is a technical post, but an important one if you are using these features. Regardless, look for this to be solved in the first major Optuma update in 2018.

Our developers have just fixed this —the weekly bars will update with each daily bar now—but the fix will not be released until Optuma 1.3 which is due End Q1 2018. Each of those three lines below are the Weekly High, Close, and Low on a daily chart. You can see how the Close is the same as the day (no longer getting Friday’s close on a Tuesday), and the high and low build.

Red: High(Week())
Black: Week()
Blue: Low(Week())

Share Link
Mathew Verdouw, CMT, CFTe

Mathew Verdouw, CMT, CFTe

CEO / Founder Optuma

As a Computer Systems Engineer, Mathew started Market Analyst (now Optuma) within 18 months of completing his degree. From that point on, Mathew has made it his mission to build the very best software tools available.

Since 1996 Mathew has been learning about all aspects of financial analysis, and in 2014 earned the CMT designation (Chartered Market Technician). In 2015, he was also awarded the CFTe designation. In 2017, Mathew started to teach the required content for the CMT exams at He is the only person in the world who teaches all three levels due to his broad exposure to all forms of financial analysis.

As someone who has dedicated his life to find better ways to analyse financial markets, Mathew is set to drive innovation in this sector for many years to come.


Submit a Comment

Your email address will not be published. Required fields are marked *

Blog Signup

Pin It on Pinterest

Share This