Is it possible to use a timeframe override on an imported script?

I want to be able to do calculations on variables assigned to imported scripts using a different timeframe to the chart that the host script is being applied to.

Case study:

Script_A has the following formula to calculate the weekly value of the Bollinger Upper Band:

BB(Week(PERIODAMOUNT=1), BARS=9, DEFAULT=UpperLine, CALC=Close, STDDEV=2.000000);

When Script_A is plotted on a daily chart it behaves as expected. (It has a sort of ‘stepped’ look because it is only plotting the end-of-week value, which doesn’t change from Monday-Thursday).

Let’s now import Script_A into Script_B. Script_B looks like this:

BBWeeklyUpperLine = SCRIPT(SCRIPTNAME=Script_A);

BBWeeklyUpperLine

What happens on a daily chart with Script_B is that we get a huge sawtooth pattern because Script_B outputs 0 (zero) for Monday, Tuesday, Wednesday, Thursday and then outputs the expected end-of-week value on Friday. (If you take SPX for example, in the last few weeks you’ll see Script_B jump from zero to over 3,000 every Friday).

This makes it rather difficult to do comparisons of variables assigned to imported scripts using different timeframes to the chart. For example, what is the difference between this week’s weekly Upper Bollinger Band and last week’s weekly Upper Bollinger Band on a daily chart? When on a weekly chart the formula is simple and returns the expected results:

BBWeeklyUpperLine = SCRIPT(SCRIPTNAME=Script_A);

BBWeeklyUpperLine[0] – BBWeeklyUpperLine[1]

But if you use that same formula on a daily chart it compares today’s value to yesterday’s value, not this week’s end-of-week-value to last week’s end-of-week-value, even though the imported script was explicitly set to a weekly timeframe.

Rather than the shorthand notation above (using [0] and [1]), I’ve tried using OFFSET with a weekly timeframe override as follows:

BBWeeklyUpperLine = SCRIPT(SCRIPTNAME=Script_A);

OFFSET(Week(PERIODAMOUNT=1), BBWeeklyUpperLine, OFFSET=0) – OFFSET(Week(PERIODAMOUNT=1), BBWeeklyUpperLine, OFFSET=1)

but get the same output as the shorthand notation. Optuma seems determined to use a chart's default timeframe for any offset applied to an imported script.

Has anyone had a similar experience or have any suggestions on how to overcome this?

Many thanks, Dean

With much gratitude to Matthew and the Optuma support team, there is an easy (an in hindsight, obvious) solution to this problem.

Simply apply the timeframe override to the SCRIPT function.

Do this even if the imported script has an explicit timeframe override too.

This needs to be done because by default, imported scripts using the SCRIPT function will be assigned the chart’s default timeframe. An explicit timeframe override must be applied to the SCRIPT function to instruct OPTUMA to use the timeframe override for the imported script, not the default chart timeframe.

Referring back to the case study of the original post:

Script_A has the following formula to calculate the weekly value of the Bollinger Band upper line:

BB(Week(PERIODAMOUNT=1), BARS=9, DEFAULT=UpperLine, CALC=Close, STDDEV=2.000000);

Script_B imports Script_A:

BBWeeklyUpperLine = SCRIPT(Week(PERIODAMOUNT=1), SCRIPTNAME=Script_A); BBWeeklyUpperLine

The explicit timeframe override in the SCRIPT function can be seen in bold text. Now Optuma displays the imported script as expected. In this case, the imported script is a weekly Bollinger Band upper line, plotted on a daily chart.

Again, thanks to Matthew and the Optuma support team for their help.

In reference to the original post and the first reply, there is an even better outcome, which could even be considered ‘best practice’.

Do not set the timeframe override within the imported script. Set the timeframe override only when it is imported with the SCRIPT function.

To explain, let’s go back to the original case study:

Script_A has the following formula to calculate the value of the Bollinger Band upper line:

BB(BARS=9, DEFAULT=UpperLine, CALC=Close, STDDEV=2.000000);

Script_B imports Script_A:

BBWeeklyUpperLine = SCRIPT(Week(PERIODAMOUNT=1), SCRIPTNAME=Script_A); BBWeeklyUpperLine

Now, the only time the timeframe override is used is when Script_A is imported using the SCRIPT function.

Why is this good or ‘best practice’? Because now Script_A can be used to provide daily, weekly, monthly or yearly data, without changing its code.

To illustrate, try this:

Script_A remains the same, with no timeframe override within it.

BB(BARS=9, DEFAULT=UpperLine, CALC=Close, STDDEV=2.000000);

Script_B imports Script_A:

Plot1 = SCRIPT(SCRIPTNAME=Script_A); Plot2 = SCRIPT(Week(PERIODAMOUNT=1), Script_A); Plot3 = SCRIPT(Month(PERIODAMOUNT=1), Script_A); Plot4 = SCRIPT(Year(PERIODAMOUNT=1), Script_A);

If you plot Script_B on a daily chart, you will see daily, weekly, monthly and yearly plots of the Bollinger Band Upper line, without changing anything in Script_A.

While Script_A is a trivial formula in this case study, imagine if was several or many lines of code. Imagine how using imported scripts like this can reduce the amount of repeated code. ‘Write once and use many times’ is the mantra of computer software systems.

With this powerful capability you can build well-structured, re-useable, modular, highly efficient code for your trading strategies.