Williams Alligator For ThinkOrSwim

diazlaz

Well-known member
2019 Donor
VIP
Its interesting, i'm noticing differences in the candle price data from ToS and Trading View. Some of the candles have different values, but overall, It looks like it's working correctly. This observation on the data differences is when looking at it in the 1 minute candle.

Original:
SGOz4io.png


Port:
P0pWHLs.png


Code:

Ruby:
# Bill William Bull/Bear divergent bars
# See: Book, Trading Chaos by Bill Williams
#
# https://www.tradingview.com/script/lLgCdjag-Bill-Williams-Divergent-Bars/
# Author: polyclick
#
# A bullish (green) divergent bar, signals a trend switch from bear -> bull
# The current bar has a lower low than the previous bar, but closes in the
# upper half of the candle.
# This means the bulls are pushing from below and are trying to take over,
# potentially resulting in a trend switch to bullish.
# We also check if this bar is below the three alligator lines to avoid false positives.
#
# A bearish (red) divergent bar, signals a trend switch
# from bull -> bear
# The current bar has a higher high than the previous bar, but closes in
# the lower half of the candle.
#
# This means the bears are pushing the price down and are taking over,
# potentially resulting in a trend switch to bearish.
# We also check if this bar is above the three alligator lines to avoid false positives.
#
#
# @@EMMA request
#
# 2019.12.13 @diazlaz - logic bug fixes.
# 2019.12.13 @diazlaz - initial port/interpretation.
#

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

def lips = MovingAverage(averageType, price[-lipsDisplace], lipsLength);
def jaw = MovingAverage(averageType, price[-jawDisplace], jawLength);
def teeth = MovingAverage(averageType, price[-teethDisplace], teethLength);

def bullDivSignal = low  < low[1]  and close > hl2 and high < lips and high < teeth and high < jaw;
def bearDivSignal = high > high[1] and close < hl2 and low  > lips and low  > teeth and low  > jaw;
def sState = if bullDivSignal then 100 else if bearDivSignal then -100 else sState[1];

# ARROWS
input showArrows = yes;
plot pUP = showArrows  and bullDivSignal;
pUP.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
pUP.SetDefaultColor(Color.GREEN);
pUP.SetLineWeight(2);

plot pDown = showArrows  and bearDivSignal;
pDown.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
pDown.SetDefaultColor(Color.RED);
pDown.SetLineWeight(2);#

#COLORBARS
input showColorBars = yes;
AssignPriceColor(if showColorBars then
if sState > 0 then Color.GREEN
  else Color.RED
else
  COLOR.CURRENT
);

#LABELS
input showLabels = yes;
AddLabel(showLabels, "Buy",
if isNaN(sState) then COLOR.DARK_GRAY else
If bullDivSignal then COLOR.GREEN else COLOR.DARK_GRAY);

AddLabel(showLabels, "Sell",
if isNaN(sState) then COLOR.DARK_GRAY else
If bearDivSignal then COLOR.RED else COLOR.DARK_GRAY);

# END OF Bill William Bull/Bear divergent bars
 
Last edited by a moderator:
Looks like the code did not include "showArrows in the pUP and pDOWN. Below is with showArrows added to the plot code.

Code:
# ARROWS
input showArrows = no;
plot pUP = showArrows and bullDivSignal;
pUP.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
pUP.SetDefaultColor(Color.GREEN);
pUP.SetLineWeight(2);

plot pDown = showArrows and bearDivSignal;
pDown.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
pDown.SetDefaultColor(Color.RED);
pDown.SetLineWeight(2);#
 
Shouldn't Tesla have triggered on 1/16 on a daily chart? It did for the Simpler trading moderator who defines a divergent bar the way it is defined in the header of the tradingview port version on post #17. Can someone take a look and see if I am wrong somehow? I would like a version that matches what they are doing at Simpler ideally. Thanks....

Okay I figured it out - the Simpler Trading version doesn't use the alligator lines to help confirm the signal, once I removed that part of the code it matches the Simpler trading one.
 
Last edited by a moderator:
Can someone please make a williams alligator crossover scan for tos? I would say one for when the green crosses above the green and another for when it crosses below the red. Thanks in advance!
 
I just added the indicator to my chart and I see 3 different colors: green, red, and blue. Regardless, you can still setup the scanner using a condition like this:

Example: Jaw (blue) crosses above Teeth (red).

mGqEyl4.png
 
Hello, I was wondering if someone could help me right the script for William's Alligator Crossover? Was just looking for a simple crossover that's shown with up/down arrows rather than having to follow the lines.
 
@Gabrielx77 There are 3 lines in the indicator. Can you specify the crossover?
Of course. I wanted a crossover of 3 simple moving averages, each one being the 5 sma with 3 displacement, the 8 sma with 5 displacement, and the 13 sma with 8 displacement. The up arrow would basically come when the 5 sma cross over both the 8 and 13 sma. The down arrow would come when the 5 sma cross under the 8 and 13 sma.

PS If the arrows could appear only when the 5 sma crosses over/under the 13 sma first, before the 8 sma crosses over/under the 13 sma, that would be awesome.
 
Last edited:
Many years ago I read the Bill Williams book, "Trading Chaos" (1995) which discussed the Alligator Indicator. I keep track of all books that I read, and also rate them. I gave this book an "excellent." So I think it is a good read, if only to glean one or two ideas from it. It should be noted that Bill Williams should not be confused with Larry Williams, who is considerably more famous. I proceeded to code this indicator in MS Excel and in Metastock's language. After considerable testing, I found that it was "average" in its effectiveness, meaning there are many other indicators that are more promising. Granted, I only traded (and still trade) NASDAQ 100 products and its derivatives, so there is that. :) I guess the fact remains that anyone can use any indicator with varying amounts of success as long as they tweak it or employ it within the framework of their own abilities and expectations. I look forward to seeing what results traders here get from using it in scanning and trading.
 
Last edited:
@Gabrielx77 Try this:

Code:
#
# TD Ameritrade IP Company, Inc. (c) 2012-2020
#

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;

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);

plot UpSignal = if Lips crosses above Jaw then low else Double.NaN;
plot DownSignal = if Lips crosses below Jaw then high else Double.NaN;

UpSignal.SetDefaultColor(Color.MAGENTA);
UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
DownSignal.SetDefaultColor(Color.CYAN);
DownSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
 
Very nice, Ben. Looks super clean and professional, I appreciate it. Also, I'd like to share some results that I back tested the other day, I hope it helps anyone who's looking for a new strategy when it comes to trading.

The reason why I was looking for a script for this Alligator Strategy was because I read into someone using it with great success; it seemed very easy to follow and very concise, even when I looked back at the previous entry and exit points from months ago.

Basically, with this strategy, you would wait until the 5 ma crosses under or over both the 8 ma and the 13 ma; but as you can see in my picture, you would've missed most of the move. After reading more into this strategy, I found someone who has found a solution for not having to miss the move.

In the picture you will see:

1. 3 Smooth Moving Averages: 5 with -3 displacement (green) 8 with -5 displacement (red) and the 13 with a -8 displacement (blue)
2. Two Ovals: 1st one being my entry point and the 2nd being my exit point
3, Magenta Down Arrow: Representation for when the 5 ma crosses under both the 8 and 13 ma

As I said before, usually your entry point would be when the 3 moving averages cross, but you would miss most of the move; the exit would be when the candles close back into the moving averages (aka the gator).

To avoid the miss of the move, you would want to enter the trade when the candle crosses and closes above or below the whole gator instead. At the same time, you would need to make sure your MACD is following the trend of the candle. As you can see, the MACD is converging towards the down side, which means it's a strong signal and you can buy.

Traded options the other day on the SPY, and although the trade took up some time, the result was about a $125-$150 gain per contract.

V5J2cvb.jpg


I got this specific strategy from a user on reddit that goes by: rogerdog1 if anyone was wondering.
 
Last edited:
@Gabrielx77 The Alligator indicator is popular with novices for several reasons... First, it is easy to understand... Second, it is fairly safe and reliable... However, the indicator does have some deficits as well... The main negative is that entry signals are somewhat to considerably later than other indicators, as are exits... While a late entry helps reduce false signals it also cuts into potential profits...

If you choose to use the Alligator indicator for trade entries you would be better off using another method to determine your trade exits... This is a common problem with most all moving average crossover indicators... One exit option would be to use a profit target so you can, hopefully, exit while the price is rising rather than trying to catch a falling knife... Another exit option would be something along the lines of a trailing stop to limit potential profit losses...

I have run backtests on the Alligator indicator and other moving average indicators and if you rely on the crossovers for entry and exit you will not end up with a profitable overall trading strategy... Just my two cents...
 
@rad14733 Yes I totally agree with you! That's why I focus on the candles closing above or below the averages to take the trade. If you look at my picture, my entry was when the candle closed below all 3 moving averages (the first oval), which was before the cross over, and then I scaled out of positions until I finally closed all remaining positions when my candles entered back into the alligator (the second oval). I never go solely based on an crossover, because yes, it's definitely too late for entries and exits. Instead, I use it as a zone correlating with MACD.
 
Last edited:
@Gabrielx77

I saw that but I would have closed the trade as soon as the first red bar closed below the top green moving average line, which coincides with the MACD downward crossover... That's why I mentioned the trailing stop idea due to the fact that the trade had definitely weakened in trend... I prefer to base my exits off the high point of the trade but, then again, I scalp and day trade so I'm not going to wait and see if there is a rebound... And if there was a strong rebound I'd open another trade if there appeared to be momentum and the price not near recent highs... I've ridden trades from winner to loser waiting for rebounds that never transpired so now I tend to take the sure things... Just goes to show that traders have different trade criteria and expectations... You'll never go broke taking profits early... ;)(y)
 
@rad14733 100%; I actually did take profits on the close of that candle. I had bought 15 contracts and sold 8. I would've sold the rest if the green candle broke the previous red candles high, nearing my entry point, but it didn't so I held. I also scalp, so I understand what you're saying; when I closed my final position at the 2nd oval in my pic, I was only holding 3 contracts left.
 
This question is in regards to the Gator Oscillator. Can a scan be developed to identify the 4 phases of the Gator Oscillator ?
  • Sleeping Phases
  • Awakening Phases
  • Eating Phases
  • Sated Phases
Also can the code be modified to plot the 4 different phases on the Chart, such as a arrow for the sleeping phases, a circle for the awakening phase, a triangle for eating phase etc.

Here is the code: Any help or suggestion would be very appreciated. Thanks

Code:
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 = WilliamsAlligator(price, jawLength, teethLength, lipsLength, jawDisplace, teethDisplace, lipsDisplace, averageType).Jaw;
def teeth = WilliamsAlligator(price, jawLength, teethLength, lipsLength, jawDisplace, teethDisplace, lipsDisplace, averageType).Teeth;
def lips = WilliamsAlligator(price, jawLength, teethLength, lipsLength, jawDisplace, teethDisplace, lipsDisplace, averageType).Lips;

plot Top = AbsValue(jaw - teeth);
plot Bottom = -AbsValue(teeth - lips);
plot ZeroLine = 0;

Top.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
Top.DefineColor("Up", Color.UPTICK);
Top.DefineColor("Down", Color.DOWNTICK);
Top.AssignValueColor(if Top > Top[1] then Top.Color("Up") else Top.Color("Down"));
Bottom.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
Bottom.DefineColor("Up", Color.UPTICK);
Bottom.DefineColor("Down", Color.DOWNTICK);
Bottom.AssignValueColor(if Bottom < Bottom[1] then Bottom.Color("Up") else Bottom.Color("Down"));
ZeroLine.SetDefaultColor(GetColor(0));
 
Define the following:
  • Sleeping Phases
  • Awakening Phases
  • Eating Phases
  • Sated Phases
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
160 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top