WilliamsFractal Bars Ahead with WilliamsAlligator


Category:
0
0

I’m not sure how to properly title this one. One of the main concerns is not finding from “X” bars ago but looking for bars ahead and putting a trigger.

In the attached photo that is labeled Example 1 here is the issue:

I’m using WilliamsFractal study and on the first candle highlighted on the left you see the fractal above the yellow candle. Based on my code, I get an alert when the fractal is above the green, red and blue lines and those lines are “stacked”. However based on the WilliamsAlligator study those lines are print 3 bars ahead. It’s stated in the code lipsdisplace -3. So technically when that fractal in question is formed my code says the 3 lines aren’t stacked and doesn’t form an arrow but based on his study it is stacked because it is 3 bars ahead (as show by the white box). So how can I fix this code on my trigger to look bars ahead or that particular study ahead?

Here is my current code:

input price = hl2;
input jawLength = 13;
input teethLength = 8;
input lipsLength = 5;
input jawDisplace = -8;
input teethDisplace = -5;
input lipsDisplace = -3;
input averageType = AverageType.WILDERS;

def na = Double.NaN;

plot Jaw = MovingAverage(averageType, price[-jawDisplace], jawLength);
plot Teeth = MovingAverage(averageType, price[-teethDisplace], teethLength);
plot Lips = MovingAverage(averageType, price[-lipsDisplace], lipsLength);

Jaw.SetDefaultColor(Color.BLUE);
Teeth.SetDefaultColor(Color.RED);
Lips.SetDefaultColor(Color.GREEN);

def bullfract = WilliamsFractal ();

plot LongTrigger = if bullfract then bullfract > Lips and Lips > Teeth and Teeth > Jaw else na;

plot ShortTrigger = if bullfract then bullfract < Lips and Lips < Teeth and Teeth < Jaw else na;

LongTrigger.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
LongTrigger.SetLineWeight(3);
LongTrigger.SetDefaultColor(Color.GREEN);
LongTrigger.HideBubble();
LongTrigger.HideTitle();

ShortTrigger.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
ShortTrigger.SetLineWeight(3);
ShortTrigger.SetDefaultColor(Color.RED);
ShortTrigger.HideBubble();
ShortTrigger.HideTitle();

#Trigger alerts
Alert(longtrigger[1], “Long Trigger”, Alert.BAR, Sound.Ding);
Alert(ShortTrigger[1], “Short Trigger”, Alert.BAR, Sound.Ding);

Second part (Example 2)

I know we don’t have WilliamsFractal code so I’m not sure if this will work or not.

Right now if I wanted to show a short trigger it waits for a fractal to appear at the top of the candle. I’m wanting it to be the bottom fractal at the bottom of the candle. As you can see in the white box on the left this is where the trigger should’ve been hit instead of the right white box as the fractal is at the top. Is there a way w/o knowing the code to distinguish this and fix that short alert?

I think my basic coding is getting better. Thanks for all the help.

Attachments:
Marked as spam
Posted by (Questions: 21, Answers: 47)
Asked on August 13, 2019 11:47 am
376 views
0
Private answer

Without even taking a slight glance at the code. Try this and see if it clears things up for you.

This plots the close from 3 bars ago:

plot closeThreeBarsAgo = close[3];

This plot the close from three bars in the future:

plot closeThreeBarsIntoFuture = close[-3];

The same exact method applies to every variable you write into the code.

def myVariable = close;
plot myVariableThreeBarsAgo = myVarialbe[3];
plot myVariableThreeBarsIntoFuture = myVariable[-3];

However when referencing bars into the future you should be sure to check if there is a value there before trying to reference it:

plot myVariableThreeBarsIntoFuture = if !IsNaN(myVariable[-3] then myVariable[-3] else 0;

Marked as spam
Posted by (Questions: 37, Answers: 4079)
Answered on August 13, 2019 12:58 pm
0
I get that part but I'm either tired or can't figure out how to input into the current code. I'm sure I'll have an ah ha moment. Then the second part of the question, is it possible to determine the fractal at the bottom or top of the candle w/o knowing the code?
( at August 13, 2019 7:09 pm)
0
So I added some code and I'm getting the arrow to point 3 candles ahead. So it is recognizing the conditions at that time. However the candle should point on the candle w/the fractal. #WilliamsAlligator Original Code input price = hl2; input jawLength = 13; input teethLength = 8; input lipsLength = 5; input jawDisplace = -8; input teethDisplace = -5; input lipsDisplace = -3; input averageType = AverageType.WILDERS; def na = Double.NaN; plot Jaw = MovingAverage(averageType, price[-jawDisplace], jawLength); plot Teeth = MovingAverage(averageType, price[-teethDisplace], teethLength); plot Lips = MovingAverage(averageType, price[-lipsDisplace], lipsLength); Jaw.SetDefaultColor(Color.BLUE); Teeth.SetDefaultColor(Color.RED); Lips.SetDefaultColor(Color.GREEN); def bullfract = WilliamsFractal (); def closeThreeFutureBars = close[-3]; def gatoropen = jaw < teeth and teeth < lips; def gatorclose = jaw > teeth and teeth > lips; #However when referencing bars into the future you should be sure to check if there is a value there before trying to reference it: #plot myVariableThreeBarsIntoFuture = if !IsNaN(myVariable[-3] then myVariable[-3] else 0; #Entry Triggers plot LongTrigger = if bullfract[3] then closeThreeFutureBars[-3] and gatoropen else na; plot ShortTrigger = if bullfract[3] then closeThreeFutureBars [-3] and gatorclose else na; LongTrigger.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP); LongTrigger.SetLineWeight(3); LongTrigger.SetDefaultColor(Color.GREEN); LongTrigger.HideBubble(); LongTrigger.HideTitle(); ShortTrigger.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN); ShortTrigger.SetLineWeight(3); ShortTrigger.SetDefaultColor(Color.RED); ShortTrigger.HideBubble(); ShortTrigger.HideTitle(); #Trigger alerts Alert(longtrigger[1], "Long Trigger", Alert.BAR, Sound.Ding); Alert(ShortTrigger[1], "Short Trigger", Alert.BAR, Sound.Ding);
( at August 13, 2019 7:58 pm)
0
Private answer

New Solution

I took some time to evaluate the code and you really don't have any issue with shifting plots left or right. Or reading plots from x number of bars into the future. The issue has nothing to do with those thing at all. Not to the least degree.

How to Reference Plots from Another Study

For my solution, I am going to show you how to debug your code. If you don't know how to debug your code you have no idea what is going on under the hood. It's like driving blind. Nothing good comes from that.

Let's take a look at the code for the WilliamsFractal (this is a candlestick pattern and you will not find this listed under studies). The code for the WilliamsFractal is located under "Patterns". It's the button right next to the "Studies" button at the top of your charts.

Here are the two plot statements from WilliamsFractal:

plot UpFractal = if upRightSide == sequenceCount and upLeftSide == sequenceCount then high else Double.NaN;
plot DownFractal = if downRightSide == sequenceCount and downLeftSide == sequenceCount then low else Double.NaN;

Two things we need to learn here. First is that these plot statements are either Double.NaN or they are the high/low of the bar that triggers them. But we need to convert them to a true/false value in our code. The second thing we learn from this is you cannot reference either one of these with this line of code:

def bullfract = WilliamsFractal();

Instead, you need to also include the name of the plot. Like so:

def bullfract = WilliamsFractal().UpFractal;

Converting Plots into True/False Statements

Now I will show you the first step in converting these fractal plots into true/false statements:

plot wFractals = if !IsNaN(WilliamsFractal().UpFractal) then -1 else if !IsNaN(WilliamsFractal().DownFractal) then 1 else 0;

Remember that I said these plots are either the high/low of the bar OR they are Double.NaN. So in this one statement I create a variable that is either -1, 0, or +1 depending which of these two plots is NOT Double.NaN. We use a function named IsNaN() to accomplish this. Details here:

http://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Math---Trig/IsNaN.html

Ok? We place the "!" symbol in front of the function because that symbol means "NOT". So we are testing if each plot from WillamsFractal is NOT Double.NaN. Then we assign a value of -1 if it's a down fractal, +1 if it's an up fractal, and zero if it's neither.

Next, we convert this to a true false condition by simply checking if the variable named wFractals is equal to -1 or 1. Then I apply the variable named gatorOpen and gatorClose to complete the rules for your triggers.

plot LongTrigger = wFractals == 1 and gatorOpen;
plot ShortTrigger = wFractals == -1 and gatorClose;

Now I may have gotten this backwards from your intended direction. I don't know for sure. So you many need to flip those around to get what you intended. But you should have the basic idea at this point.

How to Debug Your Code

As a bonus, I will provide the code I used to troubleshoot this issue. Along with a screenshot showing how this plots on a chart. I'll explain what you see in the lower subgraph.

declare lower;
input price = hl2;
input jawLength = 13;
input teethLength = 8;
input lipsLength = 5;
input jawDisplace = -8;
input teethDisplace = -5;
input lipsDisplace = -3;
input averageType = AverageType.WILDERS;
def Jaw = MovingAverage(averageType, price[-jawDisplace], jawLength);
def Teeth = MovingAverage(averageType, price[-teethDisplace], teethLength);
def Lips = MovingAverage(averageType, price[-lipsDisplace], lipsLength);
plot wFractals = if !IsNaN(WilliamsFractal().UpFractal) then -1 else if !IsNaN(WilliamsFractal().DownFractal) then 1 else 0;
plot gatorOpen = jaw < teeth and teeth < lips;
plot gatorClose = jaw > teeth and teeth > lips;
plot LongTrigger = wFractals == 1 and gatorOpen;
LongTrigger.SetPaintingStrategy(PaintingStrategy.POINTS);
LongTrigger.SetLineWeight(3);
plot ShortTrigger = wFractals == -1 and gatorClose;
ShortTrigger.SetPaintingStrategy(PaintingStrategy.POINTS);
ShortTrigger.SetLineWeight(3);

The Cyan line is the plot from the gatorOpen variable. The Magenta line is the plot from the gatorClose variable. They plot at value 1 when true and value zero when false. The yellow line is the plot from the variable named wFractals. As described above this is either 1, 0 or -1. The red and green dots are your LongTrigger and ShortTrigger variables. The are value 1 when gatorOpen is true and wFractals == 1 OR when gatorClose is true and wFractals == -1.

Attachments:
Marked as spam
Posted by (Questions: 37, Answers: 4079)
Answered on August 14, 2019 12:07 pm