0
votes
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © ProfitMeistro

//@version=4
strategy(title="Fractal Breakout", shorttitle="Fractal Breakout", overlay=true)
    
LookBack = input(title="Periods", defval=3, minval=1, maxval=10, type=input.integer)

th = high[2]>high[1] and high[2]>high and high[2]>high[3] and high[2]>high[4] ? -1 : 0

bl = low[2]<low[1] and low[2]<low and low[2]<low[3] and low[2]<low[4] ? 1 : 0

tot = th + bl
pl = abs(tot)>=1 ? 1 : 0

src = security(syminfo.tickerid, timeframe.period, close[1], lookahead=barmerge.lookahead_on)

///Inputs
ploton_ea = input(false,title="Plot Standard BB%B? ")
smoothon_ea = input(false,title="Smooth On ? ")
s2_ea = input(title="Smooth BB%b with xx EMA", defval=9)
x=100

bb_rel_period_ea = input(20, minval=1,title="BB%B Period",type=input.integer)
multiplier_ea = input(2.0, minval=0.001, maxval=50,title="BB%B Multiplier",type=input.float)
atrperiod_ea = input(14,title="ATR Period",type=input.integer)


///Make it Easy

easyon = input(false,title="What is All This? I want it easy! ")
myperiod = input(defval=60,title="And My Chart Period in minutes is", type=input.integer)
easyval=myperiod
easyvalmp=2
easyvals2 = 9

///make it switch
bb_rel_period = easyon ? easyval/3 : bb_rel_period_ea
atrperiod = easyon ? easyval/5 : atrperiod_ea
multiplier = easyon ? easyvalmp : multiplier_ea

smoothon = easyon ? true : smoothon_ea
ploton = easyon ? true : ploton_ea

s2 = easyon ? easyvals2 : s2_ea



///Calculations BB_rel

basis = sma(src, bb_rel_period)
stdev = multiplier * stdev(src, bb_rel_period)
upper = basis + stdev
lower = basis - stdev
bb_rel = (src - lower)/(upper - lower)
norm_bb = (40*bb_rel+30)

///Calculations ATR
atr = atr(atrperiod)

basis_ATR = sma(atr, bb_rel_period)
stdev_ATR = multiplier * stdev(atr, bb_rel_period)
upper_ATR = basis_ATR + stdev_ATR
lower_ATR = basis_ATR - stdev_ATR
bb_rel_ATR = (atr - lower_ATR)/(upper_ATR - lower_ATR)
norm_bb_ATR = (40*bb_rel_ATR+30)

//Smoothing
plotbb=ema(norm_bb_ATR,s2)
plotsnbb=ema(norm_bb,s2)

norm_bb_plot = smoothon ? plotsnbb : norm_bb

avgbb=avg(plotsnbb,plotsnbb)
up = norm_bb > avgbb

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

uAwesomeBO    = input(false, "Use Awesome Break Out Strategy")
nLengthSlow   = input(34, minval=1, title="Awesome Slow Length")
nLengthFast   = input(5,  minval=1, title="Awesome Fast Length")


// SMMA function to Calculate Alligaor Trend Lines
//
smma(src, length) =>
    s = 0.0
    s := na(s[1]) ? sma(src, length) : ( (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]

TriggerPrice = input(title="Trigger Price", type=input.source, defval=close)

// plotarrow(pl==1 ? tot : na,colorup=green,colordown=red,offset=-2,maxheight=10)

lowline = (valuewhen(tot==1, low[2], 0) + (LookBack>1 ? valuewhen(tot==1, low[2], 1) : 0) + (LookBack>2 ? valuewhen(tot==1, low[2], 2) : 0) + (LookBack>3 ? valuewhen(tot==1, low[2], 3) : 0) + (LookBack>4 ? valuewhen(tot==1, low[2], 4) : 0) + (LookBack>5 ? valuewhen(tot==1, low[2], 5) : 0) + (LookBack>6 ? valuewhen(tot==1, low[2], 6) : 0) + (LookBack>7 ? valuewhen(tot==1, low[2], 7) : 0) + (LookBack>8 ? valuewhen(tot==1, low[2], 8) : 0) + (LookBack>9 ? valuewhen(tot==1, low[2], 9) : 0)) /LookBack

highline = (valuewhen(tot==-1, high[2], 0) + (LookBack>1 ? valuewhen(tot==-1, high[2], 1) : 0) + (LookBack>2 ? valuewhen(tot==-1, high[2], 2) : 0) + (LookBack>3 ? valuewhen(tot==-1, high[2], 3) : 0) + (LookBack>4 ? valuewhen(tot==-1, high[2], 4) : 0) + (LookBack>5 ? valuewhen(tot==-1, high[2], 5) : 0) + (LookBack>6 ? valuewhen(tot==-1, high[2], 6) : 0) + (LookBack>7 ? valuewhen(tot==-1, high[2], 7) : 0) + (LookBack>8 ? valuewhen(tot==-1, high[2], 8) : 0) + (LookBack>9 ? valuewhen(tot==-1, high[2], 9) : 0))/LookBack

midline = (highline+lowline)/2

// Risk management
inpTakeProfit   = input(defval = 1000, title = "Take Profit", minval = 0)
inpStopLoss     = input(defval = 200, title = "Stop Loss", minval = 0)
inpTrailStop    = input(defval = 200, title = "Trailing Stop Loss", minval = 0)
inpTrailOffset  = input(defval = 0, title = "Trailing Stop Loss Offset", minval = 0)

// === RISK MANAGEMENT VALUE PREP ===
// if an input is less than 1, assuming not wanted so we assign 'na' value to disable it.
useTakeProfit   = inpTakeProfit  >= 1 ? inpTakeProfit  : na
useStopLoss     = inpStopLoss    >= 1 ? inpStopLoss    : na
useTrailStop    = inpTrailStop   >= 1 ? inpTrailStop   : na
useTrailOffset  = inpTrailOffset >= 1 ? inpTrailOffset : na

// === INPUT BACKTEST RANGE ===
fromMonth = input(defval = 1,    title = "From Month",      type = input.integer, minval = 1, maxval = 12)
fromDay   = input(defval = 1,    title = "From Day",        type = input.integer, minval = 1, maxval = 31)
fromYear  = input(defval = 2020, title = "From Year",       type = input.integer, minval = 1970)
thruMonth = input(defval = 1,    title = "Thru Month",      type = input.integer, minval = 1, maxval = 12)
thruDay   = input(defval = 1,    title = "Thru Day",        type = input.integer, minval = 1, maxval = 31)
thruYear  = input(defval = 2112, title = "Thru Year",       type = input.integer, minval = 1970)

// === INPUT SHOW PLOT ===
showDate  = input(defval = true, title = "Show Date Range", type = input.bool)

// === FUNCTION EXAMPLE ===
start     = timestamp(fromYear, fromMonth, fromDay, 00, 00)        // backtest start window
finish    = timestamp(thruYear, thruMonth, thruDay, 23, 59)        // backtest finish window
window()  => time >= start and time <= finish ? true : false 

// General
alligatorUpTrend    = (lips[lipsOffset+1] > teeth[teethOffset+1]) and (teeth[teethOffset+1] > jaw[jawOffset+1])
alligatorDownTrend  = (jaw[jawOffset+1] > teeth[teethOffset+1]) and (teeth[teethOffset+1] > lips[lipsOffset+1])
alligatorAwake      = (alligatorUpTrend or alligatorDownTrend)

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

ao = sma(hl2,5) - sma(hl2,34)
ac = ao - sma( ao, 5 )

Longcondition = crossover(ao, 100)
LongClose = crossunder(close, lips)
if (Longcondition and TriggerPrice>teeth)
    strategy.entry(id="Long Entry", long=true, when=Longcondition and AOup==1 or AOup==2 and up==true and strategy.opentrades==0 and window())
if (LongClose)
    strategy.close(id="Long Entry", when=LongClose)
    
sell = crossunder(close, lowline)
ShortClose = crossover(close, lips)
if (sell and TriggerPrice<teeth)
    strategy.entry(id="sell short", long=false, when=sell==true and AOdown==1 or AOdown==2 and up==false and strategy.opentrades==0 and window())
if (ShortClose)
    strategy.close(id="sell short", when=ShortClose)
    
//TRAILING STOP CODE
trailStop = input(title="Long Trailing Stop (%)", type=input.float, minval=0.0, step=0.1, defval=10) * 0.01

longStopPrice = 0.0
shortStopPrice = 0.0
longStopPrice := if strategy.position_size > 0
    stopValue = close * (1 - trailStop)
    max(stopValue, longStopPrice[1])
else
    0
shortStopPrice := if strategy.position_size < 0
    stopValue = close * (1 + trailStop)
    min(stopValue, shortStopPrice[1])
else
    999999
    
//PLOT TSL LINES
plot(series=strategy.position_size > 0 ? longStopPrice : na, color=color.red, style=plot.style_linebr, linewidth=1, title="Long Trail Stop", offset=1, title="Long Trail Stop")
plot(series=strategy.position_size < 0 ? shortStopPrice : na, color=color.red, style=plot.style_linebr, linewidth=1, title="Short Trail Stop", offset=1, title="Short Trail Stop")


//EXIT TRADE @ TSL
if strategy.position_size > 0
    strategy.exit(id="Close Long", stop=longStopPrice)
if strategy.position_size < 0
    strategy.exit(id="Close Short", stop=shortStopPrice)

Let me know if you have any suggestions. I've combined a lot of elements and really need a better formula for not trading when the market is consolidating. I use the fractal breakout strategy but not sure if that's even a good indicator for best buy and sell signals. Open to suggestions and feedback. Thanks.

1

1 Answers

0
votes

You can try simple techniques like volatility or ROC avg:

  1. https://www.tradingview.com/script/lWZvV78I-MA-Filter/
  2. highVolatility = atr(10) > atr(40) as you'll find here
study("My Script")
f = sma(roc(close, 3), 20)
plot(f, "f", abs(f) > 1 ? f > 0 ? color.green : color.red : color.silver, 1, plot.style_area)

enter image description here