I've written a version which is far simpler to read and matches the one used by Gemini (Exchange), plus I made the fractal length adjustable.
Please let me know if it's of any use.
It can be turned into a Backtesting Strategy by un-commenting the last 2 lines.
http://tos.mx/ls25cP8
PS: It doesn't use any forward-looking code like the this version
https://usethinkscript.com/threads/fractal-chaos-bands-for-thinkorswim.134/
, so what you see is on the chart is exactly how it behaves with realtime data.
PPS: What I define as "Repainting" is when an indicator updates only the latest bar using realtime data. Whereas the older Fractal Chaos Bands by BenTen & WalkingBallista was based on the WilliamsFractal code from TOS, which looks ahead into the future (relative to the current position) by up to 10 bars! (I call that "Forward-Looking", which can result in the entire chart being redrawn.)
Anything using the built-in WilliamsFractal study/code will appear to give incredible results on historical charts, but live trading performance won't be comparable in the slightest. (IMO the WilliamsFractal should only be used as a drawing tool, not as a trading tool).
I was deeply disappointed when I found out the Zig-Zag indicator was not performing live as it appeared to on old data. (TTM_ScalperAlert is another one that looks ahead into the future, but at least that is limited to 3 bars - which might not be bad on 1min charts, but could lead to large losses on a 5min or greater timeframes.).
I made a couple of revisions to allow users to toggle the repainting (last bar only) & twist prevention on/off.
Just replace the 2 plot lines with these 6 lines (To perfectly match the output from Gemini's JS engine the settings would be: Repainting = yes, PreventTwisting = no):
I'm experimenting with adding markers/different colored clouds, but so far they're just cluttering up the simplicity of the system. Right now I just wanted to get a clean version out the door.
Please let me know if it's of any use.
It can be turned into a Backtesting Strategy by un-commenting the last 2 lines.
http://tos.mx/ls25cP8
PS: It doesn't use any forward-looking code like the this version
https://usethinkscript.com/threads/fractal-chaos-bands-for-thinkorswim.134/
, so what you see is on the chart is exactly how it behaves with realtime data.
PPS: What I define as "Repainting" is when an indicator updates only the latest bar using realtime data. Whereas the older Fractal Chaos Bands by BenTen & WalkingBallista was based on the WilliamsFractal code from TOS, which looks ahead into the future (relative to the current position) by up to 10 bars! (I call that "Forward-Looking", which can result in the entire chart being redrawn.)
Anything using the built-in WilliamsFractal study/code will appear to give incredible results on historical charts, but live trading performance won't be comparable in the slightest. (IMO the WilliamsFractal should only be used as a drawing tool, not as a trading tool).
I was deeply disappointed when I found out the Zig-Zag indicator was not performing live as it appeared to on old data. (TTM_ScalperAlert is another one that looks ahead into the future, but at least that is limited to 3 bars - which might not be bad on 1min charts, but could lead to large losses on a 5min or greater timeframes.).
Code:
#//# SD_FractalChaosBands_FCB
#//# Color scheme is a clone of the FCB used on Gemini.com
#//# by Steve Day [2023/02/19]
declare upper;
input fractal_length = 5;
Assert(fractal_length >= 3, "ERROR: The fractal_length must be greater than 3. (fractal_length = " + (fractal_length) + ")");
#//# If the fractal length is even then +1 to it.
def frac_len = if fractal_length % 2 == 0 then fractal_length + 1 else fractal_length;
#//# Fractal Variables
def side_length = Floor(frac_len / 2);
def middle = Ceil(frac_len / 2);
#//# Highest Left & Right of Middle High..
def high_left = Highest(HIGH[middle], side_length);
def high_middle = HIGH[middle - 1];
def high_right = Highest(HIGH, side_length);
#//# Lowest Left & Right of Middle Low..
def low_left = Lowest(LOW[middle - 1], side_length);
def low_middle = LOW[middle - 1];
def low_right = Lowest(LOW, side_length);
#//# High/Low Fractal code..
def high_frac;
def low_frac;
high_frac = if high_middle >= Max(high_left, high_right) then high_middle
else high_frac[1];
low_frac = if low_middle <= Min(low_left, low_right) then low_middle
else low_frac[1];
plot "Fractal High" = Max(high_frac[1], low_frac[1]);
plot "Fractal Low" = Min(high_frac[1], low_frac[1]);
"Fractal High".SetDefaultColor(CreateColor(0,127,255));
"Fractal Low".SetDefaultColor(CreateColor(133,201,158));
input Show_Cloud = yes;
DefineGlobalColor("Fractal Cloud", CreateColor(225,225,225));
AddCloud(if Show_Cloud == yes then "Fractal Low" else Double.NaN, "Fractal High", GlobalColor("Fractal Cloud"), GlobalColor("Fractal Cloud"), showborder = no);
#//# For Backtesting..
#AddOrder(OrderType.BUY_AUTO, CLOSE > "Fractal High", tickcolor = Color.CYAN, arrowcolor = Color.CYAN, name = "FCB_Long");
#AddOrder(OrderType.SELL_AUTO, CLOSE < "Fractal Low", tickcolor = Color.MAGENTA, arrowcolor = Color.MAGENTA, name = "FCB_Short");
I made a couple of revisions to allow users to toggle the repainting (last bar only) & twist prevention on/off.
Just replace the 2 plot lines with these 6 lines (To perfectly match the output from Gemini's JS engine the settings would be: Repainting = yes, PreventTwisting = no):
Code:
input Repainting = yes;
input PreventTwisting = no;
def max_frac = Max(high_frac[!Repainting], low_frac[!Repainting]);
def min_frac = Min(high_frac[!Repainting], low_frac[!Repainting]);
plot "Fractal High" = if !PreventTwisting then high_frac[!Repainting] else max_frac;
plot "Fractal Low" = if !PreventTwisting then low_frac[!Repainting] else min_frac;
I'm experimenting with adding markers/different colored clouds, but so far they're just cluttering up the simplicity of the system. Right now I just wanted to get a clean version out the door.
Last edited: