Skip to content

Instantly share code, notes, and snippets.

@elja
Created February 27, 2022 11:36
Show Gist options
  • Save elja/cea0b8e41543a2ac179091fd21db9d40 to your computer and use it in GitHub Desktop.
Save elja/cea0b8e41543a2ac179091fd21db9d40 to your computer and use it in GitHub Desktop.
//@version=5
// Copyright all rights reserved to DaviddTech
indicator('DEMA + Vumanuchi Swing (Alert)', overlay=true)
// Timeframe 5m, BTCUSDT
// ****************************************************************************//
// ---------------------------------> Edit here ------------------------------ //
// ****************************************************************************//
st_isPlot = input.bool(true, 'Plot strategy indicators(except oscillators)', group='Strategy: base')
// st_timeStart = input.time(timestamp("2021-01-01T00:00:00"), "Backtest Start Date")
// st_timeEnd = input.time(timestamp("2022-01-01T00:00:00"), "Backtest End Date")
// st_isTime = time >= st_timeStart and time < st_timeEnd
st_isTime = true
// Calculations
// #####################################
// DEMA
st_demaLen = input.int(200, title='DEMA Length', group='Strategy: base')
st_dema = request.security(syminfo.tickerid, '60', (2 * ta.ema(close, st_demaLen) - ta.ema(ta.ema(close, st_demaLen), st_demaLen)))
plot(st_isPlot ? st_dema : na, 'DEMA')
// #####################################
// Range Filter - B&S Signals, @Jaza
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
//Functions
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
//Range Size Function
st_rng_size(x, qty, n)=>
// AC = Cond_EMA(abs(x - x[1]), 1, n)
wper = (n*2) - 1
avrng = ta.ema(math.abs(x - x[1]), n)
AC = ta.ema(avrng, wper)*qty
//st_rng_size = AC
AC
//Range Filter Function
st_rng_filt(x, rng_, n)=>
r = rng_
var rfilt = array.new_float(2, x)
array.set(rfilt, 1, array.get(rfilt, 0))
if x - r > array.get(rfilt, 1)
array.set(rfilt, 0, x - r)
if x + r < array.get(rfilt, 1)
array.set(rfilt, 0, x + r)
rng_filt1 = array.get(rfilt, 0)
hi_band = rng_filt1 + r
lo_band = rng_filt1 - r
st_rng_filt_val = rng_filt1
[hi_band, lo_band, st_rng_filt_val]
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
//Inputs
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
//Range Source
st_rng_src = input.source(defval=close,title="Swing Source", group='Strategy:Range Filter')
//Range Period
st_rng_per = input.int(defval=12, minval=1, title="Swing Period", group='Strategy:Range Filter')
//Range Size Inputs
st_rng_qty = input.float(defval=3, minval=0.0000001, title="Swing Multiplier", group='Strategy:Range Filter')
//Bar Colors
st_use_barcolor = input.bool(defval=false, title="Bar Colors On/Off", group='Strategy:Range Filter')
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
//Definitions
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
//Range Filter Values
[st_h_band, st_l_band, st_filt] = st_rng_filt(st_rng_src, st_rng_size(st_rng_src, st_rng_qty, st_rng_per), st_rng_per)
//Direction Conditions
var st_fdir = 0.0
st_fdir := st_filt > st_filt[1] ? 1 : st_filt < st_filt[1] ? -1 : st_fdir
st_upward = st_fdir==1 ? 1 : 0
st_downward = st_fdir==-1 ? 1 : 0
//Trading Condition
st_longCond = st_rng_src > st_filt and st_rng_src > st_rng_src[1] and st_upward > 0 or st_rng_src > st_filt and st_rng_src < st_rng_src[1] and st_upward > 0
st_shortCond = st_rng_src < st_filt and st_rng_src < st_rng_src[1] and st_downward > 0 or st_rng_src < st_filt and st_rng_src > st_rng_src[1] and st_downward > 0
st_condIni = 0
st_condIni := st_longCond ? 1 : st_shortCond ? -1 : st_condIni[1]
st_rangeFilterLongCondition = st_longCond and st_condIni[1] == -1
st_rangeFilterShortCondition = st_shortCond and st_condIni[1] == 1
//Colors
st_filt_color = st_upward ? #05ff9b : st_downward ? #ff0583 : #cccccc
st_bar_color = st_upward and (st_rng_src > st_filt) ? (st_rng_src > st_rng_src[1] ? #05ff9b : #00b36b) :
st_downward and (st_rng_src < st_filt) ? (st_rng_src < st_rng_src[1] ? #ff0583 : #b8005d) : #cccccc
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
//Outputs
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
//Filter Plot
st_filt_plot = plot(st_isPlot ? st_filt : na, color=st_filt_color, transp=67, linewidth=3, title="Filter")
//Band Plots
st_h_band_plot = plot(st_isPlot ? st_h_band : na, color=color.new(#05ff9b, 100), title="High Band")
st_l_band_plot = plot(st_isPlot ? st_l_band : na, color=color.new(#ff0583, 100), title="Low Band")
//Band Fills
fill(st_h_band_plot, st_filt_plot, color=color.new(#00b36b, 92), title="High Band Fill")
fill(st_l_band_plot, st_filt_plot, color=color.new(#b8005d, 92), title="Low Band Fill")
//Bar Color
barcolor(st_isPlot and st_use_barcolor ? st_bar_color : na)
//Plot Buy and Sell Labels
plotshape(st_isPlot ? st_rangeFilterLongCondition : na, title = "Buy Signal", text ="BUY", textcolor = color.white, style=shape.labelup, size = size.normal, location=location.belowbar, color = color.new(color.green, 0))
plotshape(st_isPlot ? st_rangeFilterShortCondition : na, title = "Sell Signal", text ="SELL", textcolor = color.white, style=shape.labeldown, size = size.normal, location=location.abovebar, color = color.new(color.red, 0))
alertcondition(st_rangeFilterLongCondition, title='Buy Signal')
alertcondition(st_rangeFilterShortCondition, title='Sell Signal')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment