BB Could Fractal

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 6

//@version=5

indicator(shorttitle="BB", title="Bollinger Bands", overlay=true, timeframe="",


timeframe_gaps=true)
length = input.int(20, minval=1)
maType = input.string("SMA", "Basis MA Type", options = ["SMA", "EMA", "SMMA
(RMA)", "WMA", "VWMA"])
src = input(close, title="Source")
mult = input.float(2.0, minval=0.001, maxval=50, title="StdDev")

ma(source, length, _type) =>


switch _type
"SMA" => ta.sma(source, length)
"EMA" => ta.ema(source, length)
"SMMA (RMA)" => ta.rma(source, length)
"WMA" => ta.wma(source, length)
"VWMA" => ta.vwma(source, length)

basis = ma(src, length, maType)


dev = mult * ta.stdev(src, length)
upper = basis + dev
lower = basis - dev
offset = input.int(0, "Offset", minval = -500, maxval = 500, display =
display.data_window)
plot(basis, "Basis", color=#2962FF, offset = offset)
p1 = plot(upper, "Upper", color=#F23645, offset = offset)
p2 = plot(lower, "Lower", color=#089981, offset = offset)
fill(p1, p2, title = "Background", color=color.rgb(33, 150, 243, 95))

/////indicator(title="Ichimoku Cloud", shorttitle="Ichimoku", overlay=true)


conversionPeriods = input.int(9, minval=1, title="Conversion Line Length")
basePeriods = input.int(26, minval=1, title="Base Line Length")
laggingSpan2Periods = input.int(52, minval=1, title="Leading Span B Length")
displacement = input.int(26, minval=1, title="Lagging Span")
donchian(len) => math.avg(ta.lowest(len), ta.highest(len))
conversionLine = donchian(conversionPeriods)
baseLine = donchian(basePeriods)
leadLine1 = math.avg(conversionLine, baseLine)
leadLine2 = donchian(laggingSpan2Periods)
plot(conversionLine, color=#2962FF, title="Conversion Line")
plot(baseLine, color=#B71C1C, title="Base Line")
plot(close, offset = -displacement + 1, color=#43A047, title="Lagging Span")
p11 = plot(leadLine1, offset = displacement - 1, color=#A5D6A7,
title="Leading Span A")
p22 = plot(leadLine2, offset = displacement - 1, color=#EF9A9A,
title="Leading Span B")
plot(leadLine1 > leadLine2 ? leadLine1 : leadLine2, offset = displacement - 1,
title = "Kumo Cloud Upper Line", display = display.none)
plot(leadLine1 < leadLine2 ? leadLine1 : leadLine2, offset = displacement - 1,
title = "Kumo Cloud Lower Line", display = display.none)
fill(p1, p2, color = leadLine1 > leadLine2 ? color.rgb(67, 160, 71, 90) :
color.rgb(244, 67, 54, 90))

//////indicator(title='Fractal and Alligator Alerts R2 by JustUncleL',


overlay=true, shorttitle='FractalAlligator')

//
// By: JustUncleL
// Date: 12-Sep-2017
// Version: R2
//
// Description:
// This is based on one well known Bill Williams Fractal and Alligator
// strategy. I was unable to find anyone who has coded this yet so here
// is my attempt.
//
// The following code is an implementation of the strategy specified
// here:
// http://forexwot.com/how-to-trade-high-accuracy-bill-williams-fractals-
alligator-trading-system.html
//
// This was achieved by combining some of the ideas from three other
// indicators:
// - True Williams Alligator (SMMA) by the_batman
// - Fractals and Levels by JustUncleL
// - Awesome Oscillator
//
// There are three types of Fractal / Alligator Strategies included in this
indicator:
// - Fractal Reversal : In an uptrend defined by Low Fractal that is above the
Alligator teeth and
// the Alligator mouth is completed open in an uptrend. The
opposite for
// downtrends. (Green and Red Arrows)
//
// - Fractal BreakOut : In an uptrend, at the start of Alligator open we look
back for the first Fractal
// High above Alligator Teeth. Alligator teeth must be above
mouth.
// (Aqua and Fuchsia arrows)
//
// - Awesome BreakOut : In an uptrend, at the start of Alligator open we look
back for the first Bar close
// above Alligator, Alligator Lips above Teeth and Jaw,
Awesome Oscillator just started
// changing to lime. The opposite for downtrends. (Teal and
Orange arrows)
//
// Modifications:
//
// R1 : Original
//
// 19-Oct-2017:
// - Added code to show the Alligator offset extensions.
//
// R2 : New signal Type.
// - Added new signal Type Awesome Oscillator Break Out.
//
// References:
// - [RS]fractals V# - RicardoSantos
// - http://www.tradingstrategyguides.com/fractal-trading-strategy/
//
// -----------------------------------------------------------------------------
// Copyright 2017 JustUncleL
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// The GNU General Public License can be found here
// <http://www.gnu.org/licenses/>.
//
// -----------------------------------------------------------------------------
//

//
// === INPUTS ===
//
jawLength = input(13, 'Jaw Length')
teethLength = input(8, 'Teeth Length')
lipsLength = input(5, 'Lips Length')
//
jawOffset = input(8, 'Jaw Offset')
teethOffset = input(5, 'Teeth Offset')
lipsOffset = input(3, 'Lips Offset')
asrc = input(hl2, 'Alligator Source')
//
hideAlligator = input(false)
showAlligatorState = input(false)
//
hidefractals = input(false)
hidelevels = input(false)
maxLvlLen = input(0)
uFractalRev = input(false, 'Use Fractal Reversal Strategy')
uFractalBO = input(false, 'Use Fractal Break Out Strategy')
//
uAwesomeBO = input(false, 'Use Awesome Break Out Strategy')
nLengthSlow = input.int(34, minval=1, title='Awesome Slow Length')
nLengthFast = input.int(5, minval=1, title='Awesome Fast Length')
//
// === /INPUTS ===

// === FUNCTIONS ===

// ||--- Completed Fractals Recognition Functions:


-----------------------------------------------------||
isIdealFractal(mode) =>
ret = mode == 1 ? high[4] < high[3] and high[3] < high[2] and high[2] > high[1]
and high[1] > high[0] : mode == -1 ? low[4] > low[3] and low[3] > low[2] and low[2]
< low[1] and low[1] < low[0] : false
ret

isRegularFractal(mode) =>
ret = mode == 1 ? high[4] < high[2] and high[3] <= high[2] and high[2] >
high[1] and high[2] > high[0] : mode == -1 ? low[4] > low[2] and low[3] >= low[2]
and low[2] < low[1] and low[2] < low[0] : false
ret

//
||---------------------------------------------------------------------------------
--------------------||

//
// SMMA function to Calculate Alligaor Trend Lines
//
smma(src, length) =>
s = 0.0
sma_1 = ta.sma(src, length)
s := na(s[1]) ? sma_1 : (s[1] * (length - 1) + src) / length
s

// === /FUNCIONS ===

// === SERIES ===

// Calculate Offsetted Alligator curves


jaw_ = smma(asrc, jawLength)
teeth_ = smma(asrc, teethLength)
lips_ = smma(asrc, lipsLength)

// Re-allign Alligator to current price action.


jaw = jaw_[jawOffset]
teeth = teeth_[teethOffset]
lips = lips_[lipsOffset]

// Fractals.
topfractal = isRegularFractal(1)
botfractal = isRegularFractal(-1)

//Count How many candles for current Pivot Level, If new reset.
topcnt = 0
botcnt = 0
topcnt := topfractal ? 0 : nz(topcnt[1]) + 1
botcnt := botfractal ? 0 : nz(botcnt[1]) + 1

topfractals = 0.0
botfractals = 0.0
topfractals := topfractal ? high[2] : topfractals[1]
botfractals := botfractal ? low[2] : botfractals[1]

topfc = topfractals != topfractals[1] ? na : color.green


botfc = botfractals != botfractals[1] ? na : color.red

// Alligator trend States down(<0), up(>0), considation(0) conditions.


// 1 = lips above teeth above jaw in order, 2 = lips above teeth and jaw, 3 = lips
above teeth.
//
AlligatorState = lips < teeth and teeth < jaw ? -1 : lips > teeth and teeth > jaw ?
1 : lips < teeth and lips < jaw ? -2 : lips > teeth and lips > jaw ? 2 : lips <
teeth ? -3 : lips > teeth ? 3 : 0

// Awesome Oscillator
xSMA1_hl2 = ta.sma(hl2, nLengthFast)
xSMA2_hl2 = ta.sma(hl2, nLengthSlow)
xSMA1_SMA2 = xSMA1_hl2 - xSMA2_hl2
// AO State
// 1 = start above zero and ascending, 2 = above zero but descending.
AOstate = xSMA1_SMA2 >= 0 ? xSMA1_SMA2 > xSMA1_SMA2[1] ? 1 : 2 : xSMA1_SMA2 >
xSMA1_SMA2[1] ? -2 : -1
// === /SERIES ===

// === PLOTTING ===


//
// Plot Alligator
plot(hideAlligator ? na : jaw_, 'Jaw', color.new(color.blue, 10), offset=jawOffset,
linewidth=2)
plot(hideAlligator ? na : teeth_, 'Teeth', color.new(color.red, 10),
offset=teethOffset, linewidth=2)
plot(hideAlligator ? na : lips_, 'Lips', color.new(color.green, 10),
offset=lipsOffset, linewidth=2)

// Plot Fractals
plotshape(hidefractals ? na : topfractal, color=color.new(color.green, 0),
style=shape.triangleup, location=location.abovebar, offset=-2, size=size.auto)
plotshape(hidefractals ? na : botfractal, color=color.new(color.red, 0),
style=shape.triangledown, location=location.belowbar, offset=-2, size=size.auto)

// Build The Fractal Level lines, fill in the gaps.


plot(not hidelevels and topcnt <= 2 ? topfractals : na, color=topfc, linewidth=1,
offset=-2, title='Top Levels -2', transp=20)
plot(not hidelevels and botcnt <= 2 ? botfractals : na, color=botfc, linewidth=1,
offset=-2, title='Bottom Levels -2', transp=20)
plot(not hidelevels and topcnt <= 3 ? topfractals : na, color=topfc, linewidth=1,
offset=-1, title='Top Levels -1', transp=20)
plot(not hidelevels and botcnt <= 3 ? botfractals : na, color=botfc, linewidth=1,
offset=-1, title='Bottom Levels -1', transp=20)
plot(not hidelevels and (maxLvlLen == 0 or topcnt < maxLvlLen) ? topfractals : na,
color=topfc, linewidth=1, offset=0, title='Top Levels 0', transp=20)
plot(not hidelevels and (maxLvlLen == 0 or botcnt < maxLvlLen) ? botfractals : na,
color=botfc, linewidth=1, offset=0, title='Bottom Levels 0', transp=20)

// === /PLOTTING ===

//
// === ALERTS ===
//

//
// Type 1 - Fractal Reversal Signal
//
highRev = 0
lowRev = 0
highRev := AlligatorState <= 0 ? 0 : AlligatorState == 1 ? AlligatorState[2] > 0
and botfractal and low[2] > teeth[2] ? nz(highRev[1]) + 1 : highRev[1] > 0 ?
highRev[1] + 1 : 0 : 0
lowRev := AlligatorState >= 0 ? 0 : AlligatorState == -1 ? AlligatorState[2] < 0
and topfractal and high[2] < teeth[2] ? nz(lowRev[1]) + 1 : lowRev[1] > 0 ?
lowRev[1] + 1 : 0 : 0

// Show Fractal Reversal alert Arrows


plotarrow(uFractalRev ? highRev == 1 ? 1 : lowRev == 1 ? -1 : na : na,
title='Fractal Reversal Arrow', colorup=color.new(color.lime, 20),
colordown=color.new(color.red, 20), offset=0, minheight=40, maxheight=60)

//
// Type 2 - Fractal Break Out Signal
//
highBO = 0
lowBO = 0
valuewhen_1 = ta.valuewhen(topfractal, high[2], 0)
valuewhen_2 = ta.valuewhen(topfractal, teeth[2], 0)
highBO := AlligatorState <= 0 or low < teeth ? 0 : AlligatorState > 0 ? valuewhen_1
> valuewhen_2 ? nz(highBO[1]) + 1 : highBO[1] > 0 ? highBO[1] + 1 : 0 : 0
valuewhen_3 = ta.valuewhen(botfractal, low[2], 0)
valuewhen_4 = ta.valuewhen(botfractal, teeth[2], 0)
lowBO := AlligatorState >= 0 or high > teeth ? 0 : AlligatorState < 0 ? valuewhen_3
< valuewhen_4 ? nz(lowBO[1]) + 1 : lowBO[1] > 0 ? lowBO[1] + 1 : 0 : 0

// Show Break out alert arrows


plotarrow(uFractalBO ? highBO == 1 ? 1 : lowBO == 1 ? -1 : na : na, title='Fractal
BreakOut Arrow', colorup=color.new(color.aqua, 20),
colordown=color.new(color.fuchsia, 20), offset=0, minheight=40, maxheight=60)

//
// Type 3 - Awesome Break Out Signal
//
highABO = 0
lowABO = 0
highABO := AlligatorState <= 0 or low < teeth ? 0 : AlligatorState <= 2 ? AOstate
== 1 and close > lips ? nz(highABO[1]) + 1 : 0 : 0
lowABO := AlligatorState >= 0 or high > teeth ? 0 : AlligatorState >= -2 ? AOstate
== -1 and close < lips ? nz(lowABO[1]) + 1 : 0 : 0

// Show Break out alert arrows


plotarrow(uAwesomeBO ? highABO == 1 ? 1 : lowABO == 1 ? -1 : na : na,
title='Fractal BreakOut Arrow', colorup=color.new(color.teal, 20),
colordown=color.new(color.orange, 20), offset=0, minheight=40, maxheight=60)

// show Alligator Trend Direction State.


plotshape(showAlligatorState ? true : na, title='Alligator Trend Direction',
location=location.bottom, style=shape.square, color=AlligatorState == 1 ?
color.green : AlligatorState == -1 ? color.red : color.yellow, transp=10)

// send alerts to alarm subsystem


alertcondition(highRev == 1, title='Fractal Reversal Long', message='REV LONG')
alertcondition(lowRev == 1, title='Fractal Reversal Short', message='REV SHORT')
alertcondition(highBO == 1, title='Fractal BreakOut Long', message='BO LONG')
alertcondition(lowBO == 1, title='Fractal BreakOut Short', message='BO SHORT')

// === /ALERTS ===


//EOF

You might also like