Welcome Guest, please sign in to participate in a discussion. Search | Active Topics |

Taking moving averages of custom formulae for conditions Rate this Topic:
Previous Topic · Next Topic Watch this topic · Print this topic ·
eugenevoronin
Posted : Tuesday, October 29, 2013 12:30:44 PM
Registered User
Joined: 7/23/2013
Posts: 5

Hello there,

I'd like to set up a scan to look for possible *relative* squeeze conditions, which is to say, a Bollinger BandWidth that is some fraction (1/2, 1/4, etc: ) of the average Bollinger BandWidth over the last N (eg. 20, 60, 180) days, or even simply the minimum over the past N days as recommended by Bollinger.

I have the following formula for the BBwidth already (directly from Bollinger on Bollinger Bands):

(4*SQR(((C1 - AvgC20)^2 + (C2 - AvgC20)^2 + (C3 - AvgC20)^2 + (C4 - AvgC20)^2 + (C5 - AvgC20)^2 + (C6 - AvgC20)^2 + (C7 - AvgC20)^2 + (C8 - AvgC20)^2 + (C9 - AvgC20)^2 + (C10 - AvgC20)^2 + (C11 - AvgC20)^2 + (C12 - AvgC20)^2 + (C13 - AvgC20)^2 + (C14 - AvgC20)^2 + (C15 - AvgC20)^2 + (C16 - AvgC20)^2 + (C17 - AvgC20)^2 + (C18 - AvgC20)^2 + (C19 - AvgC20)^2 + (C20 - AvgC20)^2)/20))/(AvgC20)

I can turn that into a condition by saying "< 0.04" or whatnot, but of course, i would like to replace that 0.04 with something like "0.5*Avg(BBwidth)20"

Now, I can do a moving average on the Bollinger BandWidth clip, and you can do moving averages on custom indicator charts, so the system is capable of this. Since I can't put multiple operators into the AVG() function nor define my own variables/functions, I can't figure out how to turn that into a condition unless I manually, painstakingly define BBwidth inside the PCF for each and every single day I want to include in the calculation with no way of referring to the "previous N days" in a programmatic manner. 

I'm considering writing a Python script to generate that code, but before I resort to that, is there any shorthand way to do what I'm trying to do?

Standard deviation is not strictly linear, so I'm not a big fan of attempting to distribute the avg operation (though of course any help with this to simplify the resulting equation would be most welcome).

Thank you kindly.

Bruce_L
Posted : Tuesday, October 29, 2013 1:20:08 PM


Worden Trainer

Joined: 10/7/2004
Posts: 65,138

We can create a condition (but not a Condition Formula) to determine if the Bollinger Bandwidth is at its minimum over the most recent x days, but I cannot think of an efficient way to create a condition for Bollinger Bandwidth to be at some percentage of its average over x days. The very long formula you have already proposed is probably as good as it gets.

I will start by pointing out that your formula is actually a mix between a Bollinger Bandwidth calculation for the current bar and a Bollinger Bandwidth calculation for the previous bar. The AVGC20 portions of the formula are for the current bar (0 bars ago) but the C1 through C20 portions of the formula are for 1 bar ago through 20 bars ago.

Modelling Bollinger Bands (& Standard Deviation) in a TC PCF

A single pass method of calculating the current Bollinger Bandwidth could be written as:

4 * SQR(ABS(C ^ 2 + C1 ^ 2 + C2 ^ 2 + C3 ^ 2 + C4 ^ 2 + C5 ^ 2 + C6 ^ 2 + C7 ^ 2 + C8 ^ 2 + C9 ^ 2 + C10 ^ 2 + C11 ^ 2 + C12 ^ 2 + C13 ^ 2 + C14 ^ 2 + C15 ^ 2 + C16 ^ 2 + C17 ^ 2 + C18 ^ 2 + C19 ^ 2 - 20 * AVGC20 ^ 2) / 20) / AVGC20

A double pass method of calculating the current Bollinger Bandwidth could be written as:

4 * SQR(((C - AVGC20) ^ 2 + (C1 - AVGC20) ^ 2 + (C2 - AVGC20) ^ 2 + (C3 - AVGC20) ^ 2 + (C4 - AVGC20) ^ 2 + (C5 - AVGC20) ^ 2 + (C6 - AVGC20) ^ 2 + (C7 - AVGC20) ^ 2 + (C8 - AVGC20) ^ 2 + (C9 - AVGC20) ^ 2 + (C10 - AVGC20) ^ 2 + (C11 - AVGC20) ^ 2 + (C12 - AVGC20) ^ 2 + (C13 - AVGC20) ^ 2 + (C14 - AVGC20) ^ 2 + (C15 - AVGC20) ^ 2 + (C16 - AVGC20) ^ 2 + (C17 - AVGC20) ^ 2 + (C18 - AVGC20) ^ 2 + (C19 - AVGC20) ^ 2) / 20) / AVGC20

A single pass method of calculate the Bollinger Bandwidth for the previous bar could be written as:

4 * SQR(ABS(C1 ^ 2 + C2 ^ 2 + C3 ^ 2 + C4 ^ 2 + C5 ^ 2 + C6 ^ 2 + C7 ^ 2 + C8 ^ 2 + C9 ^ 2 + C10 ^ 2 + C11 ^ 2 + C12 ^ 2 + C13 ^ 2 + C14 ^ 2 + C15 ^ 2 + C16 ^ 2 + C17 ^ 2 + C18 ^ 2 + C19 ^ 2 + C20 ^ 2 - 20 * AVGC20.1 ^ 2) / 20) / AVGC20.1

A double pass method of calculating the Bollinger Bandwidth for the previous bar could be written as:

4 * SQR(((C1 - AVGC20.1) ^ 2 + (C2 - AVGC20.1) ^ 2 + (C3 - AVGC20.1) ^ 2 + (C4 - AVGC20.1) ^ 2 + (C5 - AVGC20.1) ^ 2 + (C6 - AVGC20.1) ^ 2 + (C7 - AVGC20.1) ^ 2 + (C8 - AVGC20.1) ^ 2 + (C9 - AVGC20.1) ^ 2 + (C10 - AVGC20.1) ^ 2 + (C11 - AVGC20.1) ^ 2 + (C12 - AVGC20.1) ^ 2 + (C13 - AVGC20.1) ^ 2 + (C14 - AVGC20.1) ^ 2 + (C15 - AVGC20.1) ^ 2 + (C16 - AVGC20.1) ^ 2 + (C17 - AVGC20.1) ^ 2 + (C18 - AVGC20.1) ^ 2 + (C19 - AVGC20.1) ^ 2 + (C20 - AVGC20.1) ^ 2) / 20) / AVGC20.1

Note that the single pass and double pass versions are mathematically identical when done with infinite precision. The single pass version ends up being more computationally efficient on a computer, but the the double pass version ends up being more accurate on a computer because of the limited precision of the calculations (but in most cases it doesn't make a difference).

The formulas for the both of the current Bollinger Bandwidth formulas give the same results as adding Bollinger Bandwidth indicator to the chart and adjusting the settings to match.

To create a condition for Bollinger Bandwidth being at its minimum over x bars, add a Bollinger Bandwidth indicator to the chart (or the desired formula as a Custom PCF Indicator) and adjust its settings as desired. Add Donchian Channels to the Bollinger Bandwidth indicator set to a Period of x and an Offset of 0.

Now we can click on the Bollinger Bandwidth indicator and select Create Scan Condition to create a Condition for Bollinger Bandwith vs Donchion Channels to have a Channel Position Range of -100 to Min.

Create Conditions from Your Chart

Note that in the TC2000 version 12.4 beta, you could add a Stochastic to Bollinger Bandwidth and then create a condition based on the Stochastic, but this is not possible in TC2000 version 12.3.



-Bruce
Personal Criteria Formulas
TC2000 Support Articles
eugenevoronin
Posted : Wednesday, October 30, 2013 10:32:33 AM
Registered User
Joined: 7/23/2013
Posts: 5

Thank you Bruce, much obliged!

As for the error in the formula, I was obtaining hits on my scan with BBwidth greater than my indicated value and didn't know why. Thank you for pointing out that fundamental error.

 

If I end up writing a script to generate my condition, I will be sure to link it here.

Bruce_L
Posted : Wednesday, October 30, 2013 10:46:06 AM


Worden Trainer

Joined: 10/7/2004
Posts: 65,138

You're welcome.



-Bruce
Personal Criteria Formulas
TC2000 Support Articles
Users browsing this topic
Guest-1

Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.